Weight Folding, CUDA Streams, and the Bug That Made My Model Speak Backwards — Filip Makraduli
- Source
- youtube.com
- Author
- AI Engineer
- Date
Filip Makraduli and Nils Graef built FlashNorm to attack a strange inefficiency: RMS norm layers do almost none of a transformer's arithmetic, yet get launched around 33 times in a single decode step, and GPUs are fast at math but slow at everything else. FlashNorm folds the norm's gain into the projection weights offline, defers the scalar divide onto a separate CUDA stream so it runs alongside the matrix multiply instead of blocking it, and in architectures that normalize twice in a row, drops the redundant second norm entirely. Together the changes buy a 33 to 35 percent speedup on the norm-plus-projection step, and the folded checkpoint still works with torch compile and quantized models. The interesting failure came from the stream split: unit tests and perplexity checks both passed, but over long generations the model began repeating itself with a one-token lag, because the post-scale step sometimes read a stale buffer from a matrix multiply that had not finished. The fix was making the post-scale explicitly wait on both streams before reading either one.
Context
Running GPU calculations in parallel can make a model faster while introducing errors that short tests miss. The published account of this FlashNorm talk, about a speedup for a normalization step in language models, describes an optimization that ran a matrix multiplication alongside a calculation used to rescale its result.
The account says unit tests and a language-model quality check passed, but longer generation began repeating earlier output. The final rescaling step could read an old buffer because one parallel calculation had not finished. The fix explicitly made that step wait for both calculations. The reported failure shows why correct arithmetic is only part of GPU correctness: dependent operations must also use results from the right completed computation.
- RMS norm layers do almost none of a 's arithmetic, yet a single decode step can launch one roughly 33 times -- the cost FlashNorm targets is kernel-launch and data-movement overhead, not compute.
- In newer architectures that normalize twice in a row, FlashNorm drops the second norm entirely, since the operation is scale-invariant and the second application adds nothing.
- The bug surfaced only after unit tests and perplexity checks both looked normal; it took a long generation run for the model to start repeating itself with a one-step lag before anyone noticed.
- Root cause: the deferred divide ran on a separate CUDA stream from the matmul, and the join between the two was implicit, so the post-scale step sometimes read a stale buffer from a multiply that hadn't finished yet.
- Deploying the modified checkpoint required Superlinked's own open, self-hosted engine, since this kind of kernel surgery isn't possible on a rented inference endpoint.
- token — The chunk of text a model reads and writes in — roughly three-quarters of a word — and the unit AI usage is billed in.
- transformer — The neural network architecture behind modern AI models, built on attention — letting every word directly consider every other word in parallel.
- inference — Running a trained model to get answers — the phase where AI is actually used, as opposed to trained.
videoWhy AI Makes You 100x Faster but Not Your Company - Mitchell Troyanovsky, Basis
videoBuilding the AI Agent Is Easy. Proving It's Safe Isn't - Jonathan Osborne, NatWest
videoDon't Approve the AI Model. Approve the App - Doneyli De Jesus, Langfuse
videoWithout Caching, Your AI Agent Costs 10x More - George Cameron, Artificial Analysis
Checking sign-in…
Loading comments…


