QKV Projections Require a Fraction of Their Memory

Malik Khalaf, Yara Shamshoum, Nitzan Hodos, Yuval Sieradzki, Assaf Schuster

TL;DR

Training Large Language Models (LLMs) requires massive amounts of GPU memory, with a significant chunk consumed by storing intermediate activations for the $Q$, $K$, and $V$ linear projections. This paper introduces Point-Approximate Matrix Multiplication (PAMM), a tensor compression technique that slashes this specific memory footprint by up to 512x without hurting model performance. PAMM acts as a highly effective, plug-and-play complement to existing optimizations like FlashAttention.

The Overlooked Memory Bottleneck

Memory is the primary bottleneck in training modern LLMs. While a massive amount of research has focused on optimizing the memory used by the attention mechanism itself (e.g., FlashAttention, Linformer), the linear layers that happen just before attention are often ignored.

Before computing attention, the input tensor $X$ must be projected into Query ($Q$), Key ($K$), and Value ($V$) tensors. To calculate gradients during the backward pass, the massive input tensor $X$ must be saved in GPU memory during the forward pass. Because this tensor scales with both batch size and sequence length, it quickly accumulates, accounting for up to 20% of the peak GPU memory required by the attention blocks. The authors aim to eliminate this overlooked memory hog without slowing down training or degrading the model's final quality.

Methods: Point-Approximate Matrix Multiplication (PAMM)

To solve this, the authors propose Point-Approximate Matrix Multiplication (PAMM). Instead of storing the full activation matrix $X$ for the backward pass, PAMM exploits the massive redundancy present across the sequence and batch dimensions of LLM training data.

Overview of the PAMM compression and approximate matrix multiplication process.

Here is how it works:

  1. Random Generation: During the forward pass, PAMM randomly samples a tiny subset of the token embeddings from $X$ to act as "generators" (e.g., keeping just 1 out of every 512 tokens).
  2. Approximate Clustering: For every other token in the batch, PAMM finds the generator it is most similar to (via cosine similarity). It then discards the original token and simply stores a pointer to that generator, along with a single scalar value representing the scale/magnitude difference.
  3. Approximate Backward Pass: During backpropagation, instead of performing a massive matrix multiplication with the full $X$ tensor, PAMM reconstructs an approximate gradient using only the tiny set of generators and their scaling factors.

This approach mimics on-the-fly clustering but avoids the prohibitive quadratic compute costs of actual clustering algorithms (like K-Means) by relying on random sampling and simple vector projections.

Key Insights

  • The batch dimension is highly redundant: Because the total number of tokens in a training batch is vastly larger than the hidden dimension size, the activation matrices are inherently low-rank. You don't need to memorize every token's exact state to get a reliable gradient.
  • Random sampling is surprisingly sufficient: You might assume you need a smart heuristic to pick the "generator" tokens. However, the authors prove mathematically and empirically that as batch sizes grow, the density of the data ensures that uniform random sampling is enough to capture the distribution accurately.
  • LLMs are incredibly robust to gradient noise: The authors measured the relative L2 error of the gradients produced by PAMM and found them to be quite high (meaning the mathematical approximation of the gradient is technically quite noisy). Despite this, the models learned just as well—and sometimes slightly better—than the exact baselines, proving that LLM optimization can easily smooth over this specific type of compression noise.

Key Results

Across pretraining (C4 dataset), language finetuning (GLUE), and vision-language finetuning (AID benchmark), PAMM demonstrated exceptional efficiency.

Peak attention memory consumption across different model sizes, comparing full training to PAMM compression.
  • Massive Memory Reduction: PAMM reduced the memory footprint of $Q, K, V$ projection activations by over 97% across the board.
  • Pretraining Quality: When pretraining LLaMA-1B, the baseline full-rank model used 3 GB of memory for these activations and achieved a perplexity of 15.56. Using PAMM with a 1/512 compression ratio, memory dropped to just 18 MB, and perplexity actually improved slightly to 15.36.
  • Finetuning Quality: On RoBERTa-base (GLUE), activation memory dropped from 288 MB to 3.37 MB while maintaining an average benchmark score of 86.18 (compared to the 86.28 baseline).
  • Negligible Throughput Cost: Because the approximation math is highly parallelizable, the runtime overhead is tiny. For LLaMA-1B, total training throughput degraded by only 2.67%; for LLaMA-7B, it degraded by just 2.1%.
  • Multi-modal & PEFT Ready: PAMM was successfully combined with LoRA to finetune a 12B parameter Vision-Language Model (Pixtral), saving >99% of the target activation memory with virtually no drop in F1 score.

Limitations and Future Work

Currently, PAMM is only applied to the $Q, K, V$ input projections. The authors intentionally avoided applying it to the attention output projection layer because that tensor is shared directly with FlashAttention; altering it could introduce complex error accumulation.

Future work will explore extending PAMM to approximate activations in other parts of the transformer, such as the memory-heavy Feed-Forward Network (FFN) layers. Additionally, while random sampling works well, exploring more strategic, data-aware ways to select the "generator" points could yield even better compression-to-quality tradeoffs, alongside jointly optimizing PAMM to work safely with FlashAttention's output buffers.

Summary updated

BibTeX
@inproceedings{khalaf2026qkv,
  author = {Malik Khalaf and Yara Shamshoum and Nitzan Hodos and Yuval Sieradzki and Assaf Schuster},
  title = {{QKV Projections Require a Fraction of Their Memory}},
  booktitle = {International Conference on Learning Representations},
  year = {2026},
  url = {https://openreview.net/forum?id=Xvpk1g02u2},
}
Figure