﻿# Grouped-query attention: what changes when queries share keys

Grouped-query attention shares key-value heads among query groups. Follow the sharing map, cache representation and conversion implications without treating GQA as a runtime switch.

Author: Retrivium editorial team
Updated: 2026-11-03
Canonical URL: https://retrivium.ai/journal/grouped-query-attention-sharing-mechanism/
Language: en

A model can use several query heads without keeping an independent key-value head for every query head. Grouped-query attention defines an intermediate sharing arrangement. Understanding the mapping explains what is shared, what remains distinct and why the checkpoint must support the chosen architecture.

## Place sharing between the multi-head and multi-query cases

Ordinary multi-head attention can have a key-value head corresponding to every query head. Multi-query attention shares a common key-value representation more broadly. GQA groups query heads so that a group shares key-value projections while query projections remain distinct. The original GQA paper studies this design and an uptraining approach. The architectural definition identifies a sharing relation; it does not guarantee that arbitrary existing weights can be grouped without a change in the resulting model behavior.

## A shared key does not force identical attention weights

Different query heads can compare their own queries against the same group’s keys and obtain different score distributions. They can consequently aggregate shared value representations with different weights. Sharing the key-value source is not equivalent to copying the full output of one attention head into every query head. This is a useful distinction when reading diagrams or code: the repeated relationship concerns which projections supply keys and values, while query-dependent matching still contributes its own computation.

## Inspect the grouping with a controlled head map

A proposed diagnostic assigns recognizable values to key-value heads and checks which query heads can consume each group. An incorrect ordering can produce legal tensor dimensions but use the wrong source group. After wiring is verified, compare a small implementation against the intended mathematical reference. The test should retain position transforms and masks because the grouping relation does not replace those parts of attention. This is a correctness exercise, separate from measuring a particular kernel’s memory traffic or request latency.

## Relate storage savings to the exact state being shared

During cached decoding, fewer distinct key-value heads can reduce the corresponding cache inventory relative to an otherwise comparable arrangement. Total model memory still includes weights, other state and implementation overhead. The size reduction must therefore name its denominator rather than become an unqualified whole-service saving. Read query-head count, key-value-head count and head dimensions together in the actual configuration. GQA is a concrete model design with consequences for state representation, not a label that alone establishes quality, performance or universal compatibility of a converted checkpoint.

## Primary research and implementation references

- [Attention Is All You Need](https://arxiv.org/abs/1706.03762).

- [GQA: Training Generalized Multi-Query Transformer Models](https://arxiv.org/abs/2305.13245).

- [PyTorch: MultiheadAttention](https://docs.pytorch.org/docs/2.15/generated/torch.nn.MultiheadAttention.html).

## Related engineering context

[Reproducible inference stacks: preserving a GPU pilot’s result](/journal/software-stack-reproducibility/); [A GPU inference benchmark you can trust](/journal/inference-benchmark-method/).
