Basis Lab

From a Matrix Multiply to a Forward Pass

A Multiply Is Cut Into Tiles So That Each Block of Workers Reuses What It Fetched

Last timeAlmost Everything Is a Multiply

Done one cell at a time, a matrix multiply fetches every number thousands of times. Cut into tiles, each fetch feeds as many operations as the tile is wide.

The last lesson counted the bytes of a multiply by counting each table once. That count is a best case, and somebody has to arrange for it. This lesson is the arrangement.

What happens with no arrangement

Take the square case, two tables of 4,096 by 4,096 two-byte numbers multiplied together. The obvious program gives each output cell to one worker. The worker fetches a row of the first table and a column of the second, 8,192 numbers, and does 8,192 operations on them. That is one operation per number fetched, or half an operation per byte.

Across all 16,777,216 cells the workers fetch 274.9 gigabytes from two tables that hold 67 megabytes between them. Each table is read 4,096 times. On the representative machine of What It Costs to Run a Model, which moves 3 terabytes a second, the fetching takes 91.6 milliseconds. The arithmetic, 137.4 billion operations at 300 trillion a second, would take 0.46. The device spends 199 parts in 200 of the multiply waiting for bytes it has already fetched before.

A tile for each block

How a Program Runs on a GPU described storage that every worker in a block can read and that is far quicker to reach than the device's main memory. The vendor calls it shared memory. The repair is to make each block of workers responsible for a square tile of the result, of side tt, and have it copy what it needs into that storage before doing arithmetic on it.

The lesson stops here

4 more paragraphs to go

You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.

The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.

See the planThe contents

This is the reading half

Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.

The contents

The rest of this course

  1. 01A Forward Pass Is a List of Matrix Multiplies, and You Can Count Each One
  2. 02A Multiply Is Cut Into Tiles So That Each Block of Workers Reuses What It Fetchedyou are here
  3. 03A Unit That Only Multiplies Small Matrices Is Worth Eight Times, on the Multiplies That Can Feed Itopening only
  4. 04Halving the Size of a Number Is Worth Exactly Two to Memory and Whatever the Units Say to Arithmeticopening only
  5. 05Every Operation Has a Fixed Cost to Start, and the Small Ones Are Nearly All Startopening only
  6. 06A Run of Small Operations Joined Into One Pass Pays for One Start and One Trip Through Memoryopening only
  7. 07A Multiply Fills the Device Only Above a Certain Batch, and the Batch Can Be Computedopening only
  8. 08A Device Reported as Fully Busy May Be Using One Hundredth of Its Arithmeticopening only

Read alongside