Autoregressive (AR) Models
An autoregressive (AR) model represents a joint distribution as a product of conditional distributions, predicting each element given the preceding elements:
where the first factor is . It generates samples one element at a time.
Training objective: Minimize the average negative log-likelihood over the training data. For one sequence:
where . During training, each prediction is conditioned on the observed prefix (teacher forcing).
Evaluation: Perplexity is the exponential of the average token-level negative log-likelihood on held-out data.
Lower perplexity is better when evaluated on the same data with the same tokenization.
Generation: Sample one element at a time, conditioned on the previously generated elements:
Start from an empty sequence or a given prompt, and append each sampled element. Stop when an end-of-sequence token is generated or the specified length is reached.
Alternatively, greedy decoding selects the most probable next token at each step:
Autoregressive models can be implemented in various ways, including n-gram models and fixed-window models. Among these approaches, RNNs and Transformers are two effective and well-known choices.
RNN-Based Autoregressive Models
An RNN updates its hidden state using the previous state and the current token, then uses the updated state to predict the next token:
Here, is used to predict .
The architecture is as follows.

Unlike fixed-window models, RNNs can carry information beyond a fixed context window through their hidden states, although their ability to retain long-range information is limited.
Transformer-Based Autoregressive Models
A decoder-only Transformer combines causal self-attention, feed-forward layers, and positional information to model next-token distributions.
For input representations , define , , and . Each attention head computes:
where is the key dimension.
To predict the next token using only , we add a causal mask to the attention scores before softmax. This blocks attention to future tokens and prevents information leakage during training.

The final representation is used to predict :
Computational Complexity Comparison
Let be the sequence length. Assume fixed model size, full causal attention, and generation from an empty prefix.
| Model | Training: total cost | Generation: cost at step | Generation: total cost |
|---|---|---|---|
| RNN | |||
| Transformer |
Generation reuses the RNN hidden state or a Transformer KV cache.
Training: RNNs process positions sequentially. Transformers can process positions in parallel during teacher-forced training, enabling efficient GPU computation despite their higher total operation count.
Generation: Both models generate tokens sequentially. Their total costs are:
Without KV caching, recomputing a full Transformer forward pass over every prefix instead costs .


