Contextual Position Encoding: Learning to Count What's Important
About
The attention mechanism is a critical component of Large Language Models (LLMs) that allows tokens in a sequence to interact with each other, but is order-invariant. Incorporating position encoding (PE) makes it possible to address by position, such as attending to the i-th token. However, current PE methods use token counts to derive position, and thus cannot generalize to higher levels of abstraction, such as attending to the i-th sentence. In this paper, we propose a new position encoding method, Contextual Position Encoding (CoPE), that allows positions to be conditioned on context by incrementing position only on certain tokens determined by the model. This allows more general position addressing such as attending to the $i$-th particular word, noun, or sentence. We show that CoPE can solve the selective copy, counting and Flip-Flop tasks where popular position embeddings fail, and improves perplexity on language modeling and coding tasks.
Related benchmarks
| Task | Dataset | Result | Rank | |
|---|---|---|---|---|
| Grid Navigation | 2D Grid Navigation S 256/128/0.8 (OOD-sparse) | Accuracy70 | 12 | |
| Grid Navigation | 2D Grid Navigation Sequence length 128, grid width 64 (IID) | Accuracy76 | 12 | |
| Grid Navigation | 2D Grid Navigation D 64/32/0.2 (OOD-dense) | Accuracy86 | 12 | |
| Grid Navigation | 1D Grid Navigation Sequence length 128, grid width 64 (IID) | Accuracy88 | 12 | |
| Grid Navigation | 1D Grid Navigation 64/32/0.2 (OOD-dense D) | Accuracy91 | 12 | |
| Grid Navigation | 1D Grid Navigation OOD-sparse S 256/128/0.8 | Accuracy65 | 12 | |
| Selective-Copy Task | Selective-Copy OOD Dense split: 64/128 blank/non-blank | Accuracy100 | 8 | |
| Selective-Copy Task | Selective-Copy OOD Sparse 256 128 blank non-blank | Accuracy100 | 8 | |
| Selective-Copy Task | Selective-Copy IID 128/128 blank/non-blank | Accuracy100 | 8 | |
| Grid Navigation | 5D Navigation (IID) | Accuracy94 | 5 |