# Exact output-head row compaction

This is a training optimisation for a Transformer whose final vocabulary projection is used only by an unweighted tokenwise cross-entropy. It is not token pruning: the prompt still runs through the full Transformer and can influence every supervised prediction. This is an exact algebraic identity, not a new fused cross-entropy algorithm. Apple's [Cut Cross-Entropy](https://arxiv.org/abs/2411.09009) is related production work that also avoids materialising the full logit matrix.

## Closed form

Let (H) have (N) prediction rows and hidden width (D), let (W) be the vocabulary-by-hidden projection, and let (b) be its optional bias. After next-token alignment, select exactly the rows with non-ignored target labels. Let (K) be their count and (S) the (K)-by-(N) row selector. With (K>0):

```text
H_a = S H
Z_a = H_a Wᵀ + 1 bᵀ
L   = (1/K) Σ_i [ logsumexp(Z_a[i,:]) − Z_a[i,y_i] ]
E   = (softmax_rows(Z_a) − one_hot(y)) / K

∂L/∂W = Eᵀ H_a
∂L/∂b = Eᵀ 1
∂L/∂H = Sᵀ E W
```

Proof sketch: the dense logit adjoint is (S^T E), since ignored losses are constant zero and row projection does not mix positions. Substitution gives ((S^T E)^T H=E^T S H), and (S1_N=1_K). Multiplication by (W) propagates the selected adjoint back to hidden rows. Unselected final hidden rows receive zero **direct** loss gradient, but their earlier representations can still get gradients through attention from later positions. The same upstream adjoint yields the same backbone gradient given the same forward computation. If embeddings and the head share a parameter, the unchanged input-side contribution is added to the unchanged head contribution. No bitwise agreement in finite precision is implied.

The mean is undefined when (K=0). Use a zero-contribution loss sum plus count; skip the optimiser step only when the *whole normalisation group* has no supervised labels. Under gradient accumulation or distributed training, reduce loss sums and valid counts over the same group (account for any world-size division by the gradient reducer). Averaging per-worker or per-microbatch means with unequal counts is not equivalent.

## Cost

The three dense head matrix multiplies (forward, weight gradient, hidden gradient) contain (3NDV) multiply-accumulates, versus (3KDV) for gathered rows, if both gradients are computed. Bias, scan, gather/scatter, softmax and activation traffic are separate. The removed head GEMM fraction is (1-K/N); at zero masking it is zero. The materialised logits shrink from (NV) to (KV), not the whole activation budget. If the measured baseline step spends fraction (p) in work that scales exactly with retained rows and new overhead is a fraction (h) of that baseline step, the idealised total speedup is:

```text
1 / [(1 − p) + p(K/N) + h]
```

This is a cost model, not a throughput promise. Do not apply if another loss or metric consumes skipped logits, if a non-rowwise operation mixes them, or if full logits are required by the caller. Class weighting and alternative normalisations require preserving the original denominator; ignored labels and attention masks are not interchangeable.

## What the theorem does not say

- Dividing by all positions is not interchangeable with dividing by valid positions. One supervised row of loss 6 plus an ignored row gives 6 under the valid-token mean, but 3 under the all-position mean.
- The objective mask does not remove prompt tokens from the network. With uniform causal attention, a supervised row of value 2 attending to a preceding prompt value 0 gets mean 1. Deleting that prompt from attention changes the mean to 2. Selecting output rows after the Transformer leaves the mean at 1.
- Lower operation count does not imply a faster wall clock. In the cost model above, speed improves exactly when (h < p(1-K/N)), assuming positive baseline and new step times. With all targets valid there is nothing to save; a mask scan can still add overhead.
