Part 5 of 9 in Training Metis. Previous: The Tokenizer Bought Time, Not Quality · Next: 223 Calls in One Message
In the baseline we measured after the 400-million-token round, our big matrix multiplications ran at 82 TFLOPS on the 7900 XTX. Flash attention, the kernel that computes attention without writing the whole score table to memory, ran at about 34. The profiler said why. 55 to 68 percent of its cycles went to one small on-chip memory, and only 20 to 23 percent to math.
The fix that worked was boring. We stopped writing two intermediate tables into that memory and kept them in registers. Two ideas that looked better on paper did nothing or made things slower, a third (a second stream) was measured and not built, and a fused backward kernel was withdrawn when we did the arithmetic. This part is the measurements.
The card was waiting on shared memory, not on math
Metis is our 72.6-million-weight model, trained on a single 7900 XTX. In that baseline the card used 278 W of its 290 W cap, 56 TFLOPS in all, which is 46 percent of its peak. Where was the rest going?
A GPU runs its threads in lock-step groups called waves. Each group of waves shares a small fast memory called the LDS (local data share). We traced waves with our own tool, wavewait, on a quiet card at a 2,048-token sequence length. In the three flash attention kernels (forward, dq and dkv) the LDS accounted for 55 to 68 percent of wave cycles, counting instructions and waiting. The math, including the matrix-multiply instructions, took 20 to 23 percent. Waiting at barriers took 5 to 13 percent.
The instruction count pointed the same way. A traced forward wave issued about 3,200 LDS instructions. By our reading of the source, there should be 122 per tile of keys, next to 32 matrix-multiply instructions.
The first lever landed small
A wave can skip a tile of keys that is masked out for all its rows, because causal attention never looks ahead. We added the skip. The check was strict: parameters after 6 updates had to be byte-identical to main's trainer, all 290 MB of them. They were. The forward kernel got faster by 7.7, 6.2 and 4.3 percent per launch in three interleaved runs. The dq and dkv kernels moved inside the spread between runs of the same code, which was 1.7 to 6 percent. At the level of a whole training step, the box could not resolve any change.
Two levers lost, and we wrote down why
We tried moving two loads in the dkv kernel to before a barrier. It got 16.9 percent slower, so we removed it. We tried 16 waves per tile block. No gain, removed. We noted not to propose either again without a new reason.
A second idea was running the memory-bound kernels on a second stream so they overlap with the math. We measured before building. The kernels for query and key normalization, rotary position and casts all run at 730 to 880 GB/s by bytes over time, so memory is their limit. The only slack left was the drain at each launch boundary, about 4.7 ms per update. We did not build it.
Measuring is a draw, so we measured twice
One trace is one sample of the card's state. In the dkv kernel, one capture showed 25.6 percent memory wait because another job shared the card. A quiet capture showed 3.1 percent. The capture tool declares 2 GB and runs alongside other work. So we took two captures, read the maximum column and checked the queue.
The same code built into two binaries differed by 1.7 to 6 percent per kernel. A gain under that needs more rounds before it counts.
What worked: no table in the shared memory at all
For the forward and dq kernels, we computed the score table transposed. Then we packed the probabilities and score gradients straight into the operand layout of the next matrix-multiply instruction, in registers, with v_permlanex16_b32. Neither table is written to the LDS anymore.
Per launch, with 4 interleaved rounds of 40 updates under rocprofv3 and byte-equal output to main:
| kernel | change per launch |
|---|---|
| flash forward, packed | -22 to -24% |
| flash dq, packed | -17% |
Three findings that no source count showed
A shuffle is a memory instruction. In the old forward kernel, warp.shuffle_xor compiles on this chip (gfx1100) to ds_bpermute_b32. That goes through the LDS crossbar and a wait on its counter. There were 64 of them per key tile, a third of the kernel's LDS instructions, and none of them showed up in a count of the lines we wrote.
The register count decides. Each SIMD has 1,536 vector registers, allotted in steps of 24. A kernel using 192 registers runs 8 waves, 216 gives 7, 240 gives 6 and 264 gives 5. The dq kernel at 242 registers did 29 percent less work per wave and gained nothing. At 176 registers it was 17 percent faster. In the register-count trials the forward kernel at 193 registers got 17 percent, and at 191 got 24 to 32 percent (the final four-round table above says 22 to 24). We read the gap as occupancy: 191 fits 8 waves and 193 does not. Traced cycles are wall time times resident waves, so we compare traces only at equal occupancy.
A division is not a multiply. A separate speed-up for generation, a megakernel for serving the model, folded runtime arguments into constants, and the first version was not byte-equal. total / N was a hardware division in one kernel and a multiply by 2^-10 in the other, one ulp apart on rare inputs. Tokens and agreement did not see it. The byte dump did. The landed, byte-equal version ran 2,633 tokens per second against 1,766 for the normal launch path, same job, 20 prompts, 64 tokens each. It only works for one-shot greedy runs, so serve mode, sampling and prompts of 128 positions or more stay on the launch path.
What we withdrew
We had planned one fused backward kernel instead of the separate backward kernels. In the current design, S is computed 3 times and dP 2 times per layer, 309 GFLOP executed for 206 useful. Fusing saves 69 GFLOP. But the cross-block sums for dK and dV move 1.14 GB each way per layer, about 2.6 ms of memory traffic against about 1.3 ms of math saved. Recomputing S from Q and K, which is 33 MB, is the cheap side. We withdrew the kernel on that arithmetic.
A packed version of dkv was not faster either. It needed 252 registers and two workgroups per WGP, and we left it behind a flag.
What this does not show
All gains above are per launch, not per training step. After the first change, the step-level difference was below what our box can resolve. For the register-packed kernels we have the per-launch table, not a tokens-per-second figure for a whole run. The x4096 run in part 4 used the masked-tile skip and a flash LDS fix labelled K1, so its 52.2 minutes already includes some of this work.
The check is simple to state. Build the trainer at commit c0d969f, run the flash8-ab gate against main with a byte comparison after 6 updates and rocprofv3 per-kernel timings. If dq does not fall by about 17 percent, or one byte differs, the claim is wrong. Our part-1 notes list two more levers: fewer LDS instructions per matrix multiply, and fusing normalization with rotary position, worth about 0.9 ms per update by bytes. We have not re-checked either since part 2.

Comments
No comments yet.
Log in to comment.