﻿# Teacher forcing: parallel training of a sequential generator

Teacher forcing uses known previous tokens during training, allowing causal Transformer predictions to be computed in parallel without seeing future targets.

Author: Retrivium editorial team
Updated: 2026-11-03
Canonical URL: https://retrivium.ai/journal/teacher-forcing-parallel-causal-training/
Language: en

A decoder-only language model can calculate losses for many sequence positions in one training pass, yet generate a response token by token. The apparent contradiction disappears when the available inputs are made explicit. Training already has the reference sequence; generation has only the prompt and the tokens produced so far.

## Reference prefixes replace generated prefixes

Teacher forcing supplies the known preceding tokens from the training example instead of feeding the model’s sampled continuation back into every training position. For a sentence about a blue boat, the prediction after blue receives the actual earlier words. Another position can receive a longer reference prefix in the same pass. The loss asks whether the next reference token was assigned adequate probability. It does not require the model to have generated every earlier reference word correctly before that position can contribute a gradient.

## Causal attention preserves the direction

Parallel calculation does not authorize reading the target from the future. A causal attention mask restricts each position to itself and permitted earlier positions. The representation used to predict the next token cannot attend to the position containing that next token in the input sequence. The shifted target is compared afterward. A missing mask would change the task, letting a representation exploit information unavailable during deployment. Correct tensor shapes alone would not expose that conceptual leak; permitted dependencies must also be checked.

## A position table makes alignment visible

Consider the reference sequence rain, wets, roads. The state after rain predicts wets, and the state after wets predicts roads. Both states can be computed with the same forward pass because their complete reference inputs are present and causally masked. During generation, roads cannot be predicted from a newly generated wets until wets has actually been selected and added to the context. This timing difference comes from missing future inputs, not from using a fundamentally different prediction head or a different vocabulary.

## Training and generation expose different histories

An incorrect sampled token changes subsequent generation history. Teacher-forced loss usually evaluates later positions against the original reference history instead. Consequently, a low reference loss is useful but does not fully describe behavior after the model’s own mistakes. Keep those evaluations distinct: inspect likelihood on fixed text, then assess sampled trajectories under declared decoding settings. A controlled debugging fixture can compare shared-prefix logits between training evaluation and incremental generation before the first sampled divergence, isolating alignment and masking errors from ordinary branching.

## Primary research and implementation references

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

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

- [Hugging Face Transformers: Causal Language Modeling](https://huggingface.co/docs/transformers/tasks/language_modeling).

## 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/).
