Autoregressive (AR) Models

An autoregressive (AR) model represents a joint distribution as a product of conditional distributions, predicting each element given the preceding elements:

pθ(x1,…,xn)=∏i=1npθ(xi∣x1,…,xi−1),p_\theta(x_1,\ldots,x_n) = \prod_{i=1}^{n} p_\theta(x_i\mid x_1,\ldots,x_{i-1}),

where the first factor is pθ(x1)p_\theta(x_1). It generates samples one element at a time.

Training objective: Minimize the average negative log-likelihood over the training data. For one sequence:

L(θ;x)=−log⁡pθ(x1,…,xn)=−∑i=1nlog⁡pθ(xi∣x<i),\mathcal{L}(\theta;x) = -\log p_\theta(x_1,\ldots,x_n) = -\sum_{i=1}^{n}\log p_\theta(x_i\mid x_{<i}),

where x<i=(x1,…,xi−1)x_{<i}=(x_1,\ldots,x_{i-1}). 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.

PPL⁡(x)=exp⁡(L(θ;x)n).\operatorname{PPL}(x) = \exp\left(\frac{\mathcal{L}(\theta;x)}{n}\right).

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:

x^i∼pθ( ⋅∣x^<i).\hat{x}_i\sim p_\theta(\,\cdot\mid \hat{x}_{<i}).

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:

x^i=arg⁡max⁡vpθ(v∣x^<i).\hat{x}_i = \arg\max_v p_\theta(v\mid\hat{x}_{<i}).

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:

ht=fW(ht−1,xt)h_t = f_W(h_{t-1}, x_t)

Here, hth_t is used to predict xt+1x_{t+1}.

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 XX, define Q=XWQQ=XW_Q, K=XWKK=XW_K, and V=XWVV=XW_V. Each attention head computes:

Attention⁡(Q,K,V)=softmax⁡(QK⊤dk+M)V,\operatorname{Attention}(Q,K,V) = \operatorname{softmax}\left( \frac{QK^\top}{\sqrt{d_k}}+M \right)V,

where dkd_k is the key dimension.

To predict the next token xi+1x_{i+1} using only x1,…,xix_1,\ldots,x_i, we add a causal mask to the attention scores before softmax. This blocks attention to future tokens and prevents information leakage during training.

Mij={0,j≤i,−∞,j>i.M_{ij}= \begin{cases} 0, & j\le i,\\ -\infty, & j>i. \end{cases}

图片说明

The final representation hth_t is used to predict xt+1x_{t+1}:

pθ( ⋅∣x≤t)=softmax⁡(Woht+bo).p_\theta(\,\cdot\mid x_{\le t}) = \operatorname{softmax}(W_o h_t+b_o).

Computational Complexity Comparison

Let nn 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 tt Generation: total cost
RNN O(n)O(n) O(1)O(1) O(n)O(n)
Transformer O(n2)O(n^2) O(t)O(t) O(n2)O(n^2)

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:

TRNN=∑t=1nO(1)=O(n),TTransformer=∑t=1nO(t)=O(n2).T_{\mathrm{RNN}}=\sum_{t=1}^{n}O(1)=O(n), \qquad T_{\mathrm{Transformer}}=\sum_{t=1}^{n}O(t)=O(n^2).

Without KV caching, recomputing a full Transformer forward pass over every prefix instead costs ∑t=1nO(t2)=O(n3)\sum_{t=1}^{n}O(t^2)=O(n^3).