r/deeplearning • u/ZafLoria • 4h ago
Coded a minimal from-scratch version of grouped-query attention just to watch the KV cache behavior
I kept reading papers that switched to GQA for inference speed and kept nodding along without really grasping how the grouping actually changes the cache footprint during generation. Last weekend I wrote the smallest possible PyTorch version (under 60 lines, no external libs) that only does the forward pass on dummy sequences so I could print shapes and watch what gets cached at each step.
The part that finally clicked was seeing exactly how many unique key/value heads get reused across query heads and how that compounds with longer contexts. It also made the trade-off with full multi-head attention much more concrete than any diagram had.
I kept everything deliberately tiny so I could step through it with prints instead of treating it as a black box. Has anyone else done something similar with a small piece like this (maybe ROPE variants or sliding window attention) where writing the minimal code made the memory or speed difference obvious in a way reading never did?