Comparing Architecture Families Fairly › Memory: Two Very Different Problems
Language AI HandbookAll chapters Report training and inference memory separately. They include different tensors as well as the weights.
State the measurement phase, precision, batch, sequence length and allocation placement.
Training memory
One all-resident mixed-precision Adam configuration uses about 16 bytes per parameter: two for the weight, two for its gradient, four for an fp32 master weight, and eight for the two fp32 moments. At 7B parameters that exceeds 80 GB before activations. Sharding, offloading, or changing the optimizer layout changes the budget.
On top of that sit activations, which scale with batch, sequence length, and depth, and which are the reason activation checkpointing exists. Attention activations are the interesting case. A naive implementation materializes an score matrix per head per layer, which is quadratic. FlashAttention avoids materializing it, so its activation memory is linear in rather than quadratic, at the cost of recomputing some intermediates during the backward pass. This is a memory result achieved by an implementation, and again it is not an architecture property.
Eliminating the stored quadratic attention term can leave other activations or model state as the largest allocation. The outcome depends on the workload, optimizer, checkpointing and sharding.
Inference memory
At inference the picture changes completely. Optimizer state is gone. What remains is weights plus persistent state plus a modest activation workspace.
Weights are simple: times bytes per parameter. For a 7B model in bf16 that is 14 GB, and it does not depend on context length, batch size, or family.
For attention layers that share a cache layout, this formula gives the KV-cache bytes per retained position and sequence across those layers.
where:
- : the number of attention layers that store KV entries
- : query-head count times head width in this form. If KV head width differs, use the actual KV head width and count.
- : the number of key/value heads under grouped-query attention
- : the number of query heads
- bytes per element: the storage size of one cache entry (2 for bf16)
- the leading factor 2 is for keys and values
Consider 33 attention layers, width 4,096 and two bytes per cached element. Full multi-head attention stores 540,672 bytes (528 KiB) per position. With one KV head per eight query heads and the same head dimension, grouped-query attention stores 67,584 bytes (66 KiB) per position. At 128,000 token positions the payloads are about 69.2 GB and 8.65 GB. Adding 14 GB for the weights of an illustrative 7B BF16 model exceeds an 80 GB budget with the first cache. The second leaves room before allocator and serving overhead. This illustration holds the number of token positions fixed. The serving workload below holds raw text bytes fixed and computes positions separately.
Grouped-query attention uses one eighth of the full-head KV payload in this example. With the same 14 GB of weights, the full-head case totals 83.21 GB and the grouped-query case totals 22.65 GB. The ratio is about 3.67 before allocator and workspace costs. Report the cache layout when comparing attention with a compressed-state model.
If your attention baseline uses full multi-head attention and your alternative uses a compressed state, you are comparing cache layouts as much as sequence-mixing mechanisms. Fix the KV-head ratio across attention-bearing families so the comparison isolates the layer type.
A selective state-space layer carries recurrent state. Its recurrent tensor has elements. At , and two bytes per element, that is bytes per layer and sequence. A layer that allocates convolution-history entries per channel also stores elements in a separate buffer. Sum the buffers across layers using their respective storage precisions. The serving proxy below uses the recurrent tensor as its only SSM-specific state term. A hybrid stores KV entries for its attention layers.
That is the entire memory argument for constant-state models, and it is sizable. It is also only a memory argument. Whether the compressed state carries enough information to do the task is a separate question, covered in the section on long-context quality.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!