﻿# Reading attention through einsum index contractions

Read attention as named index contractions with einsum: how query, key and value axes combine, what is reduced and where softmax belongs.

Author: Retrivium editorial team
Updated: 2026-11-03
Canonical URL: https://retrivium.ai/journal/attention-einsum-index-contractions/
Language: en

An einsum expression can describe tensor algebra without hiding its axes behind a chain of reshapes. In attention, that description separates feature matching from value aggregation. It becomes especially useful when several dimensions have equal sizes but distinct meanings.

## Label the operands with their semantic indices

Suppose query representations carry batch, head, query-position and feature indices. Keys carry batch, head, key-position and the compatible feature index. Their score calculation multiplies matching features and sums over that feature index, retaining both position indices. This explains why a score matrix represents pairs of positions rather than another hidden representation. Einsum makes the contraction explicit, but its letters are arbitrary symbols. A clear explanation supplies a glossary of their meanings rather than assuming that one conventional letter guarantees correctness.

## Normalization is a separate stage in the calculation

The raw matching scores are scaled and masked before softmax converts eligible keys into weights for each query. An einsum contraction alone does not provide this normalization. Applying softmax over query positions would answer a different question from distributing a query’s weight across keys. A causal mask changes eligible pairs, while padding excludes absent text. Keeping these stages separate in a learning implementation makes it possible to inspect each mathematical object before replacing the sequence with a fused attention operation.

## Value aggregation contracts the key-position index

Attention weights combine with value vectors. The key-position index disappears through summation, while query position and the value-feature dimension remain. This is a different contraction from the earlier query-key feature match. It explains why values can conceptually use another feature width, provided the surrounding architecture supports it. A proposed small exercise assigns a recognizable vector to each value position and verifies that a query receives the expected weighted combination. Such a fixture reveals whether the implementation aggregated positions or accidentally mixed feature coordinates.

## Use explicit algebra as a reference implementation

A readable unfused expression can serve as a correctness reference for a more efficient path on small inputs. Compare masks, output values and gradient behavior where training is relevant. Numerical differences need a defined tolerance and data type rather than a requirement for identical implementation steps. Einsum itself is not a promise of a particular speed or storage layout. The lesson is to maintain a transparent dependency map: reference algebra states what must be computed, while kernels and layouts determine how a particular system executes it.

## Primary research and implementation references

- [PyTorch: Broadcasting Semantics](https://docs.pytorch.org/docs/2.15/notes/broadcasting.html).

- [PyTorch: Einstein Summation](https://docs.pytorch.org/docs/2.15/generated/torch.einsum.html).

- [PyTorch: Matrix Multiplication](https://docs.pytorch.org/docs/2.15/generated/torch.matmul.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/).
