﻿# Tensor axes in a language model: batch, sequence and hidden width

A practical explanation of batch, sequence and hidden axes in language-model tensors, with a worked shape example and checks that detect semantic axis mistakes.

Author: Retrivium editorial team
Updated: 2026-11-03
Canonical URL: https://retrivium.ai/journal/tensor-axes-batch-sequence-hidden/
Language: en

Language models manipulate arrays whose axes carry different meanings. Reading a shape becomes useful when batch members, token positions and feature coordinates remain distinguishable throughout the computation. A shape-compatible operation can still mix the wrong objects.

## Name the axes before calculating storage

A common hidden-state layout uses batch, sequence and hidden width. The batch axis separates examples, the sequence axis orders positions inside an example, and hidden width holds learned features for each position. The names are conceptual; a library may choose another physical ordering. Attaching names prevents a frequent mistake: assuming that every dimension of equal length is interchangeable. Two examples and two token positions can happen to share a dimension size without sharing meaning or permitting the same reduction.

## Follow one illustrative shape through a projection

Consider an illustrative hidden tensor with shape [2, 5, 8]. It represents two examples, five positions and eight features. A learned linear projection from eight features to twelve changes the final axis, producing [2, 5, 12]. It does not create extra examples or positions. Those numbers are an arithmetic example, not a specification for any commercial model. Describing both the input and output axes makes it clear why the same projection can be applied independently at every position.

## Trace reductions as changes of meaning

A mean over the feature axis produces one summary per position. A mean over the sequence axis produces a summary per example. A mean over the batch axis combines examples, which can be useful for an objective but unsuitable for a per-user representation. Padding complicates sequence reductions because empty positions need explicit treatment. The resulting array might have a plausible shape even when padding polluted its values. Shape tests should therefore be accompanied by tiny examples whose expected reductions can be calculated by hand.

## Use examples that expose accidental axis swaps

A useful learning exercise fills each example and position with a distinguishable pattern, then applies the intended projection or reduction. Compare semantic outputs, not only dimension lengths. Broadcasting can make an incorrect operation legal, especially when singleton dimensions are inserted in the wrong place. Tensor views add another distinction: logical axes and physical memory layout need not coincide. Recording axis names, layout changes and reduction intent gives a reader a reliable path from a diagram to actual model code without depending on one framework convention.

## Primary research and implementation references

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

- [PyTorch: Tensor Views](https://docs.pytorch.org/docs/2.15/tensor_view.html).

- [PyTorch: Autograd Mechanics](https://docs.pytorch.org/docs/2.15/notes/autograd.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/).
