SHAPE : Shifted Absolute Position Embedding for Transformers

Shun Kiyono, Sosuke Kobayashi, Jun Suzuki, Kentaro Inui

Introduction

Transformers have revolutionized natural language processing, yet they remain fundamentally "permutation invariant." Without explicit positional information, a Transformer treats a sentence as an unordered bag of words. To address this, researchers typically use Absolute Position Embeddings (APE) or Relative Position Embeddings (RPE). While APE is computationally efficient, it often fails to generalize to sequences longer than those seen during training. Conversely, RPE generalizes well but introduces significant computational overhead.

This paper introduces SHAPE (Shifted Absolute Position Embedding), a simple "drop-in" modification that grants APE the generalization capabilities of RPE without sacrificing speed.

The Problem: Efficiency vs. Generalization

Absolute Position Embeddings (APE) assign a unique vector to each index (0, 1, 2...). Because these indices are fixed, the model often fails on "extrapolation" tasks—processing sequences longer than the training maximum. Relative Position Embeddings (RPE) solve this by encoding the distance between tokens, making the model "shift-invariant." However, RPE can slow down training by up to 25% and complicates the core self-attention architecture.

The authors propose a middle ground: can we achieve shift invariance using standard absolute embeddings?

The SHAPE Method

SHAPE achieves shift invariance through a form of positional data augmentation. The core algorithm is straightforward:

  1. Training Phase: For each input sequence, a random integer offset $k$ is sampled from a uniform distribution $[0, K]$.
  2. Index Shifting: Instead of using the standard index $i$, the model uses the shifted index $i + k$ to look up the position embedding: $PE(i + k)$.
  3. Inference Phase: The offset $k$ is set to 0, reverting to standard APE behavior.
Comparison of APE, RPE, and the proposed SHAPE mechanism.

By randomly shifting positions during training, the model is prevented from memorizing specific absolute indices. Instead, it learns to rely on the relative relationships between tokens, effectively becoming shift-invariant.

Experimental Results

The researchers evaluated SHAPE on the WMT 2016 English-German translation task across scenarios designed to test generalization:

  • EXTRAPOLATE: Training on short sequences (<50 tokens) and testing on longer ones.
  • INTERPOLATE: A benchmark using concatenated sentences to create sparse positional distributions, testing the model's ability to handle rare words at various offsets.
BLEU score improvements of SHAPE over APE on extrapolation and interpolation tasks.

Key Insights

  • Extrapolation Performance: In the EXTRAPOLATE task, SHAPE achieved a BLEU score of 29.80, nearly matching RPE (29.86) and significantly outperforming standard APE (29.22).
  • Computational Efficiency: SHAPE introduces zero overhead. In contrast, RPE was 25% slower in standard settings and became "prohibitively slow" (20x slower per step) on very long sequences in the interpolation task.
  • Rare Word Robustness: SHAPE significantly improves the prediction of rare words. By shifting positions, the model sees rare words in a variety of positional contexts, preventing them from being "trapped" in poorly trained positional buckets.
  • Shift Invariance: Analysis of hidden states via cosine similarity confirmed that SHAPE-trained models produce consistent representations regardless of the absolute shift, a property standard APE lacks.

Conclusion

SHAPE offers a powerful, low-cost alternative to complex relative position mechanisms. By simply shifting absolute indices during training, Transformers can learn shift-invariant representations that generalize to long sequences and improve robustness to rare words. As a "drop-in" replacement for APE, SHAPE is highly recommended for practitioners seeking better generalization without computational penalties.

Summary updated

BibTeX
@inproceedings{kiyono2021shape,
  author = {Shun Kiyono and Sosuke Kobayashi and Jun Suzuki and Kentaro Inui},
  title = {{SHAPE : Shifted Absolute Position Embedding for Transformers}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2021},
  doi = {10.18653/v1/2021.emnlp-main.266},
}
Figure