Research project · Projects
Positional encodings and length generalization
Small decoder-only Transformers, built from PyTorch primitives, were trained to copy or reverse sequences of up to 16 tokens, then tested on longer ones. The only thing that changes between models is how they encode position. It is a CPU-scale replication of Kazemnejad et al., NeurIPS 2023.
- Every model at 0% by length
- 24
trained only up to length 16
- RoPE, one token past training
- 100% → 25%
exact match on copy, n = 16 to 17
- Models trained
- 8 × 3
configurations × seeds, on a laptop CPU
Length explorer
How accuracy falls off past the training length
Exact match means every output token and the end-of-sequence token are right. Lines are the mean of 3 seeds; the shaded band spans the best and worst seed. Drag across the chart or use the slider to read off any length.
- NoPE
- Sinusoidal
- RoPE
- ALiBi
All numbers in a table
| Task | Encoding | n = 16 | n = 17 | n = 18 | n = 19 | n = 20 | n = 24 |
|---|---|---|---|---|---|---|---|
| Copy | NoPE | 99.0 | 97.0 | 81.1 | 34.1 | 7.6 | 0.0 |
| Copy | Sinusoidal | 97.0 | 93.1 | 58.6 | 22.1 | 3.5 | 0.0 |
| Copy | RoPE | 100.0 | 25.0 | 0.1 | 0.0 | 0.0 | 0.0 |
| Copy | ALiBi | 100.0 | 98.6 | 53.4 | 8.6 | 0.4 | 0.0 |
| Reverse | NoPE | 92.1 | 91.3 | 87.4 | 60.2 | 36.8 | 0.0 |
| Reverse | Sinusoidal | 97.8 | 95.2 | 83.2 | 39.7 | 7.7 | 0.0 |
| Reverse | RoPE | 100.0 | 51.0 | 11.2 | 0.0 | 0.0 | 0.0 |
| Reverse | ALiBi | 100.0 | 56.8 | 0.3 | 0.0 | 0.0 | 0.0 |
Inside the model
Where the attention goes
To reverse a sequence, each output step has to look back at one specific input token. Every model learns a "pointer" attention head that does this, visible as an anti-diagonal. Below is the strongest pointer head of each seed-0 model on the reverse task, at the longest training length and at twice that. Brighter cells mean more attention.
Rows are output steps, columns are input positions. Each map shows one example; the on-target figure is averaged over 64 sequences.
Findings
What it means
- No encoding generalizes far. Every model of every encoding is at 0% exact match by length 24, only 1.5× the longest training sequence.
- RoPE breaks first. It goes from 100% at n = 16 to 25% on copy one token later, and its pointer head has dissolved by n = 32. That is consistent with the paper, which groups RoPE with absolute encodings as poorly suited to length generalization.
- ALiBi depends on the task. It is the best encoding on copy just past training (99% at n = 17), where every source token sits the same distance back, but falls to 57% on reverse and to 0% one token later.
- Sinusoidal holds up just past training on reverse. It leads one token past training (95%) and is close behind NoPE one token later (83%), a narrow window the paper's aggregate results over lengths up to twice the training length do not separate out.
- NoPE lasts longest. With no positional encoding at all, it is the best model on both tasks at n = 18–20 (81% on copy and 87% on reverse at n = 18), still at 37% on reverse at n = 20, and the last to reach zero on reverse, even though it is the weakest model on reverse inside the training range (97% averaged over n = 1–16, 92% at n = 16). That matches the paper's main finding.
Inference
KV cache
Generation keeps each layer's keys and values, so every decoding step feeds only the newest token instead of re-running the whole sequence. A test checks that cached and uncached generation produce identical tokens for every encoding. Uncached decoding recomputes the growing prefix at every step; caching avoids repeating those projections.
| Batch | New tokens | Cached | Uncached | Speedup |
|---|---|---|---|---|
| 1 | 32 | 0.40 s | 0.75 s | 1.9× |
| 1 | 64 | 0.92 s | 1.26 s | 1.4× |
| 1 | 128 | 1.77 s | 3.36 s | 1.9× |
| 64 | 32 | 1.04 s | 6.01 s | 5.8× |
| 64 | 64 | 2.21 s | 22.46 s | 10.2× |
| 64 | 128 | 4.91 s | 100.95 s | 20.6× |
At batch size 1 this small model is dominated by per-step overhead, so the gain is modest. Timings were taken on a laptop shared with other jobs, so treat them as approximate.
Method
Setup and limits
| Model | Decoder-only Transformer, d_model 64, 4 heads, 3 layers, built from PyTorch tensor operations |
|---|---|
| Encodings | None (NoPE), sinusoidal, RoPE, ALiBi; everything else identical |
| Training | Lengths 1–16, 5,000 steps, batch 64, AdamW |
| Evaluation | Every length 1–48, 256 sequences each, identical for every model |
| Verification | Attention checked numerically against PyTorch's built-in implementation; causality tested for every encoding |
- Small scale: about 150k parameters, far smaller than the paper's models, so this tests the qualitative ranking, not exact numbers.
- Variance is high right at the boundary: RoPE on reverse at n = 17 ranges from 0.0% to 99.6% across seeds.
- Two tasks only; the paper also covers arithmetic and other reasoning tasks.
- T5's relative bias, the strongest explicit encoding in the paper, is not tested here.