Measure wave quantization of a tiled kernel

Why it matters Wave quantization explains why a slightly bigger matrix can run noticeably slower, and it guides tile size and split-K choices.

A tiled matmul launches one block per output tile. The GPU runs them in waves of num_sms blocks (one block per SM here). If the tile count is just above a multiple of num_sms, the last wave is almost empty and much of the GPU idles.

Write wave_stats(M, N, tile_m, tile_n, num_sms). tiles = ceil(M / tile_m) * ceil(N / tile_n), waves = ceil(tiles / num_sms), efficiency = tiles / (waves * num_sms). Return a dict {"tiles": ..., "waves": ..., "efficiency": ...}.

Signaturedef wave_stats(M, N, tile_m, tile_n, num_sms)

Example

M 1024, N 1024, tiles 128 x 128, 60 SMs gives {"tiles": 64, "waves": 2, "efficiency": 0.5333}.

Understand the idea

Waves are like rounds of a lift with a fixed capacity. If 61 people need a lift that carries 60, it makes two full trips and the second trip carries one person. That second trip takes as long as the first. Worked example: a 1024 x 1024 output with 128 x 128 tiles has 8 x 8 = 64 tiles. With 60 SMs, the first wave runs 60 tiles and the second runs 4, so waves = 2 and efficiency = 64 / 120 = 0.53. Rule: tiles = ceil(M / tm) ceil(N / tn), waves = ceil(tiles / SMs), efficiency = tiles / (waves SMs). Fixes include smaller tiles, split-K to make more work items, or persistent kernels that balance tiles across SMs dynamically.

solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks all 4.

  • Sample 1: examplewave_stats(1024, 1024, 128, 128, 60)
  • Sample 2: ragged tileswave_stats(1000, 300, 128, 128, 16)
  • Test 3: perfectwave_stats(512, 512, 64, 64, 64)
  • Test 4: smallwave_stats(1, 4096, 64, 256, 132)