From Pieces to a Fast Kernel

Introduction

Project introduction

From Pieces to a Fast Kernel

You have written the map from program id to tile, the masks for edge blocks, a count of the bytes each tiling moves, a roofline estimate, and a kernel with the bias and ReLU fused in. That is every piece a fast matrix multiply needs.

In the notebook those pieces come solved, so you can use them. You write a vector add, launch the tiled kernel and check it against torch.matmul on sizes that do not divide evenly, and then run experiments that only make sense once every piece exists.

What you will see

A kernel that counts its own loads confirms the traffic formula exactly, and you use those counts to choose the tile shape that fits a small on-chip budget. On an A100, the roofline shows that even 256 by 256 tiles would be memory bound if every load went to main memory, and you find that only a 512 by 512 tile would escape, far more than one core can hold. That is why the cache and the order of tiles matter. A small cache simulation reproduces the 90 versus 54 block example from Triton's tutorial, and you use it to tune the group size of the launch order.

Then a kernel with one missing mask passes every round-numbered test and returns garbage for the rest. You find the sizes that expose it and write the rule.

One program, one tile of C

program 0

A

×

B

=

C

Step 1 of 4 along K loads a block of A and a block of B and adds their product

Program idwhich copy am I
Tilerows and columns of C
Loop over Kmasked loads, tl.dot
Storewrite the tile once

How the notebook is organized

01

Write kernels

A vector add, then a tiled matmul launched and checked against PyTorch.

02

Count the speedups

Pick a tile by its measured loads, find where memory stops being the limit, and tune the launch order with a cache simulation.

03

Find the bug

Track down a missing mask that only breaks some sizes.

Write and measure the kernel

Run the whole project in the browser, about 50 minutes.

Open the notebook project