Basis Lab

From a Matrix Multiply to a Forward Pass

Every Operation Has a Fixed Cost to Start, and the Small Ones Are Nearly All Start

Last timeWhat Half the Bits Buys

Starting any operation on the device costs a fixed time that does not shrink with the work. Measure it, and the small operations turn out to be a third of a pass.

Lesson one found that the operations between the multiplies are four ten thousandths of the arithmetic of a block. If time followed arithmetic, this lesson would not exist. It does not, for a reason that has nothing to do with arithmetic.

A fixed cost that does not shrink

Each operation of a pass is a function the host starts on the device. The vendor calls this a kernel launch, and How a Program Runs on a GPU showed that the launch is not the work. Before any worker runs, the host has chosen which function to use, checked the shapes of the tables, reserved somewhere for the result and queued the request. All of it takes time, and none of it depends on how many numbers the operation will touch.

FIG 1The time of one operation
the fixed cost to start, spent mostly on the host, the same for four numbers or four million
the work: a cost per number, times the count of numbers the operation touches
For an element by element operation on two-byte numbers, each number is two bytes read and two written. On the representative machine at 3 terabytes a second, c is 1.33 millionths of a microsecond.

Measuring the start

No data sheet prints ss. It depends on the framework, the host processor and the operation, so it is a thing to measure, and there is one trap in doing so. The programming guide for the device says a launch returns to the host before the device has completed the task. The documentation of PyTorch says the same of its own operations and draws the consequence: timings taken without waiting for the device are not accurate.

The lesson stops here

5 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 Fetchedopening only
  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 Startyou are here
  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