Remember to Forget: Gated Adaptive Positional Encoding

arXiv:2605.10414 · cs.LG · Submitted 2026-05-11 · Read on arXiv

Listen

Radio episode about this paper

Transcript

Introduction to the show: ident: AI Radio. Generated commentary on the latest Artificial Intelligence papers.

Tom: Today's paper: "Remember to Forget: Gated Adaptive Positional Encoding".

Jane: The gist The GAPE (Gated Adaptive Positional Encoding) introduces a drop-in augmentation for Rotary Positional Encoding that decouples distance-based suppression from token importance through querydependent and keydependent gates,

Tom: First, who's behind it and why it matters.

Title and authors: Tom: So we're looking at this paper, "Remember to Forget: Gated Adaptive Positional Encoding," and it’s authored by Riccardo Ali, Alessio Borgi, Mario Severino, and Pietro Liò. The title itself suggests something about forgetting or remembering context.

Jane: Yeah, that sounds intriguing because it tackles a known problem with Rotary Positional Encoding, which is RoPE. We've heard that when you extend the sequence length beyond what the model was trained on, those rotary phases can get into out-of-distribution regimes, leading to spurious long-range alignments and degraded retrieval.

Lu: Exactly. Existing fixes like p-RoPE are trying to mitigate this trade off by leaving some channels unrotated, but they’re effectively just increasing the wavelengths, so they're still exposed to the same underlying instabilities that RoPE has.

Meng: So what's new here? Is it just another tweak to the rotation angles? We need something that handles context importance differently than just distance.

Tom: That’s right. GAPE introduces a drop-in augmentation for positional encodings that uses a content-aware mask directly on the attention logits, which preserves the underlying rotary geometry but adds adaptive routing signals.

Jane: So how does it actually work in plain terms? It seems to decouple distance-based suppression from what's actually important about each token.

Lu: It achieves this through two adaptive routing signals: a query gate that controls how strongly a query token contracts its context, and a key landmark that protects keys from distance-based penalization.

Meng: A key landmark sounds like it’s essentially marking important tokens so they don't get penalized by the distance calculation. That’s what I need to see if it actually helps at scale.

Tom: It does, and this mechanism selectively suppresses irrelevant background context while preserving distant useful information without changing the underlying rotary geometry itself.

Jane: That selective suppression sounds like exactly what we need when dealing with long documents where you want the model to focus on specific relevant pieces of information rather than getting lost in noise.

The paper's summary: Tom: Let’s talk about the core idea of "Remember to Forget: Gated Adaptive Positional Encoding." The authors are proposing GAPE as a way to address the limitations we talked about earlier with RoPE and its variants.

Jane: So, what is this paper actually saying in terms of solving that problem? It seems they recognize that robust long-context modeling needs both high-resolution local structure and stable, selective attenuation of irrelevant distant information.

Lu: They argue that RoPE keeps the precise local relative geometry, but it lacks a native way to explicitly attenuate irrelevant long-range context. Conversely, additive encodings like ALiBi stabilize contexts through explicit distance penalties but impose rigid suppression that can erase useful distant signals.

Meng: So GAPE is trying to combine those two things without forcing a fixed trade off between local detail and long-range stability.

Tom: Precisely. GAPE augments the native rotary geometry with a data-dependent content-aware mask over the attention logits, introducing these two adaptive routing signals. The query gate, which we can call 'gi', controls how strongly each query token contracts its context.

Jane: And then you have the key landmark 'lj', which continuously protects selected keys from distance-based suppression. It’s like a switch for how much protection a specific key gets from that distance penalty.

Lu: The paper shows that these components work together to suppress irrelevant long-range context while preserving distant information, complementing RoPE’s positional geometry with adaptive structural routing.

Tom: They even report improved perplexity in 2B-parameter models when they use this method, which is a pretty strong signal for the stability of the model's behavior <ref:2605.10414#pg1>.

Jane: That’s significant because it shows that this isn't just a theoretical fix; it translates into better performance on actual language modeling tasks under extended contexts.

The paper's improvements: Tom: So, let’s look at the specific improvements the authors detail in "Remember to Forget: Gated Adaptive Positional Encoding." They break down how this works structurally.

Jane: The core idea is that the query gate 'gi' adapts to the retrieval regime; close targets induce larger values and therefore stronger contraction, whereas far targets keep gi near zero, which preserves a broader effective horizon.

Lu: And then you have the key landmark 'lj' continuously protecting selected keys from distance-based suppression; it approaches one, meaning full protection for those specific keys.

Meng: So the query gate is dynamic based on the query itself, and the landmark gate is dynamic based on which key we’re looking at. That seems like a lot of computation if you have to calculate these gates for every token in every layer.

Tom: The good news from their analysis is that this structural bias is realized exactly as a rank-at-most-two correction to the pre-softmax logits through the same matrix multiplication used for attention.

Jane: That means it's implemented in a way that’s compatible with things like FlashAttention, which is great because it keeps the computational cost manageable.

Lu: They also show that GAPE learns non-uniform head-wise masks, where some heads develop strong biases while others remain weakly biased, which suggests the model learns to specialize its attention routing.

Meng: So if we deploy this, we have to worry about head specialization. Does that mean different parts of the model will behave very differently depending on what they’re processing?

Tom: It means different heads learn different routing strategies based on their specific task needs, which is a sign of a flexible architecture.

Conclusion: Jane: So to wrap up this discussion on "Remember to Forget: Gated Adaptive Positional Encoding," the main implication is that GAPE successfully separates context contraction from key preservation. The query gate 'gi' controls how strongly the current query contracts distant context, while the landmark gate 'lj' continuously modulates how strongly each key is exposed to that contraction.

Tom: And the head-specific amplitude, h, calibrates the overall strength of this structural pathway. It retains selected distant information while suppressing weakly protected background context, and its factorized form is compatible with fused SDPA and FlashAttention.

Lu: The results support the complementary roles of positional extrapolation and adaptive structural routing, showing how they can work together effectively in practice.

Meng: From an engineering standpoint, the fact that this can be retrofitted into a pretrained 7B model without degrading standard capabilities suggests it’s a very practical enhancement for production systems.

Lalam: I'm confident in my assessment; GAPE retains selected distant information while suppressing weakly protected background context, and its factorized form is compatible with fused SDPA and FlashAttention.

Tom: So, the big picture here is that we have a mechanism that handles long-context generalization better than before, achieved through adaptive structural routing. That’s what we’ve got on "Remember to Forget: Gated Adaptive Positional Encoding."

Riccardo Ali, Alessio Borgi, Mario Severino, Pietro Liò, Christopher Irwin

Department of Computer Science and Technology, University of Cambridge · Department of Information Engineering, University of Padua · Department of Computer, Control and Management Engineering, Sapienza University of Rome

cs.LG

Submitted: 2026-05-11

Updated: 2026-10-08

License: http://creativecommons.org/licenses/by-nc-sa/4.0/

Importance score: 92/100

The gist: The gist The GAPE (Gated Adaptive Positional Encoding) introduces a drop-in augmentation for Rotary Positional Encoding that decouples distance-based suppression from token importance through

Key concepts

GAPE Augmentation
A method that adds adaptive gates (query and key dependent) over attention logits to modify Rotary Positional Encoding. It selectively suppresses distant, irrelevant context while preserving important, distant tokens without changing the underlying geometry of RoPE.
Query Gate (gi)
Controls how strongly a specific query token modulates the contraction of its context. This gate adapts based on the retrieval regime: close targets induce stronger contraction, while far targets keep it near zero, helping preserve a broader effective positional horizon.
Landmark Gate (lj)
Continuously protects selected keys from distance-based suppression. As lj approaches 1, protection becomes full. This mechanism ensures that important distant information is preserved even when the structural penalty for distance is applied.
Structural Bias Realization
The structural bias introduced by GAPE is realized exactly as a rank-at-most-two correction to the pre-softmax logits using the same matrix multiplication employed in standard attention mechanisms, ensuring compatibility with existing hardware and architectures.

Terminology

Summary

The gist The GAPE (Gated Adaptive Positional Encoding) introduces a drop-in augmentation for Rotary Positional Encoding that decouples distance-based suppression from token importance through querydependent and keydependent gates, improving long-context robustness up to 8× the training length.

How it works

GAPE augments the native rotary geometry of RoPE with a data-dependent content-aware mask over the attention logits, introducing two adaptive routing signals: a query gate that allows each query token to modulate how strongly its context is contracted, and a key landmark that controls how strongly each key is protected from distance-based penalization This mechanism selectively suppresses irrelevant background context while preserving distant useful information, without modifying the underlying rotary geometry The structural bias is realized exactly as a rank-at-most-two correction to the pre-softmax logits through the same matrix multiplication used for attention

The GAPE mask separates four factors with distinct roles: the relative distance (i − j)/T defines the positional envelope, gi controls query-dependent contraction, (1 − lj) modulates how strongly that contraction applies to key j, and Γh sets the head-wise strength The landmark gate lj continuously protects selected keys from distance-based suppression, with lj → 1 approaching full protection The query gate gi controls the effective positional horizon, while lj controls how strongly each key is exposed to the structural penalty

Key Mechanisms and Properties

The paper details several theoretical properties of GAPE, including its equivalence to other formulations and its behavior under specific conditions

[i] We introduce GAPE, a drop-in augmentation for positional encodings that contracts weakly protected distant context while preserving selected keys, with a FlashAttention-compatible implementation

[ii] We show that GAPE provides a mechanism for suppressing spurious OOD long-range alignments while preserving distant relevant tokens, and validate the resulting robustness in controlled Needle-in-a-Haystack retrieval up to 8× the training context length

[iii] We show that GAPE improves long-context robustness in language modeling and can be retrofitted into a pretrained 7B model without degrading standard capabilities, while improving performance at the longest evaluated context

Empirical Validation

The framework is validated through controlled retrieval and language modeling experiments

[i] GAPE improves long-context robustness across controlled retrieval and language-modeling experiments, extrapolating up to 8× the training length

[ii] GAPE improves retrieval robustness under OOD context extension by adapting context contraction while preserving relevant distant keys, consistent with the mitigation mechanism analyzed above

[iii] Across rotary encodings, augmenting RoPE and p-RoPE with GAPE improves robustness beyond the training horizon

The results show that GAPE yields lower perplexity in the OOD regime, especially at the largest evaluated context lengths

Mechanistic Insights

The analysis of the routing variables reveals how GAPE actively uses its structural pathway

[i] The landmark gate lj protects keys from distance-based suppression, and the query gate gi controls the strength of context contraction

[ii] GAPE learns non-uniform head-wise masks: several heads develop strong biases, while others remain weakly biased

The query gate gi adapts to the retrieval regime: close targets induce larger values and therefore stronger contraction, whereas far targets keep gi near zero, preserving a broader effective horizon

Conclusion

GAPE separates context contraction from key preservation: gi controls how strongly the current query contracts distant context, while lj continuously modulates how strongly each key is exposed to that contraction The head-specific amplitude Γh calibrates the overall strength of this structural pathway GAPE retains selected distant information while suppressing weakly protected background context, and its factorized form is compatible with fused SDPA and FlashAttention. The results support the complementary roles of positional extrapolation and adaptive structural routing.

--- Page 33 ---

Preprint

Remember to Forget:

Gated Adaptive Positional Encoding

Supplementary Material

CONTENTS

A Gated Adaptive Positional Encoding: Proofs and Further Explanations 15

A.1 Landmark Protection.............) 15

A.2 Modulation of Recency Bias 15

A.3 Softmax Normalization Growth 17

A.4 Entropy Contraction 18

A.5 Hardware-Native Factorisation 33

A.6 Translation Invariance 20

A.7 Mitigating Phase Hallucinations 21

A.8 GAPE Theoretical Properties in Needle-In-A-Haystack Retrieval 23

B Extended Experiments 25

B.1 nanoGPT: Training Setup and Datasets........) 25

B.2 OLMo-3 7B Retrofit: Setup and Detailed Results 25

B.3 Ablation Study of the GAPE Mask Components 27

B.4 Needle-In-A-Haystack Synthetic Experiment 28

B.5 Frequency-Channel Norm Distribution 30

B.6 FULL ROUTING DYNAMICS OF GAPE AND HEAD SPECIALISATION 32

--- Page 34 ---

Preprint

Remember to Forget:

Gated Adaptive Positional Encoding

FLOPs. For a single Transformer layer, both RoPE and GAPE share the same costs across the QKV projection, causal attention, output projection, and MLP FLOPsROPE = FLOPsGAPE = 2BHT2d + 24BT C2 + O(BT C)

KV-cache Memory at Inference. During autoregressive decoding, each layer stores past keys and values in a KV cache For GAPE, the landmark value and positional information are already encoded in the modified key tensor, so the modified key kt and the value vt are cached Thus, both RoPE and GAPE cache tensors of identical shape K, V ∈ R B×H×Tctx×d

Decode FLOPs per layer = 24C 2 + 4CTctx + O(C) where the O(C) term contains the positional-encoding computation

--- Page 35 ---

Preprint

Remember to Forget:

Gated Adaptive Positional Encoding

Attention Maps Entropy Analysis. Having verified GAPE’s gating dynamics in a controlled synthetic setting in Section 4, we now examine whether the same behavior emerges on real longcontext inputs GAPE produces lower-entropy attention maps than RoPE and p-RoPE across layers, suggesting that the learned mask suppresses diffuse background context

--- Page 36 ---

Preprint

Remember to Forget:

Gated Adaptive Positional Encoding

Head Specialization in Attention Maps.

Improvements for AI systems

  1. Bold header: GAPE Augmentation for Long-Context Retrieval Robustness

This improvement allows AI systems to maintain retrieval accuracy when sequences are extended beyond their training horizon by introducing a content-aware mask that contracts weakly protected distant context while preserving selected keys. This directly addresses the OOD failure mode of spurious long-range alignments, enabling reliable performance in controlled retrieval up to 8× the training length.

  1. Bold header: Adaptive Context Contraction via Query Gate

The system can dynamically adjust its focus based on query relevance by employing a query gate that allows each query token to modulate how strongly its context is contracted. This means the model can learn to strengthen recency when the answer is local and to suppress it when successful retrieval requires preserving long-range evidence, effectively setting an effective context length tailored to the specific task.

  1. Bold header: Landmark Protection for Distant Evidence

The architecture can selectively retain critical distant information through a key landmark that controls how strongly each key is protected from distance-based penalization. This mechanism ensures that selected keys can remain accessible through landmark protection, preventing the loss of semantically relevant distant tokens that might otherwise be erased by rigid distance penalties.

  1. Bold header: Improved Language Modeling Stability

By employing GAPE, language models exhibit improved performance in extrapolation regimes, as demonstrated by lower OOD perplexity compared to standard RoPE or p-RoPE. This allows the model to generate text with greater stability and accuracy when processing genuinely long documents that exceed the training context window.

  1. Bold header: Retrofit Capability for Pretrained Models

GAPE can be retrofitted into a pretrained 7B model without degrading standard capabilities, suggesting that existing, high-performing models can be enhanced for long-context generalization with minimal retraining effort, making adaptation more practical.

Abstract

Rotary Positional Encoding (RoPE) is widely used in modern large language models. However, when sequences are extended beyond the range seen during training, rotary phases can enter out-of-distribution regimes, leading to spurious long-range alignments, diffuse attention, and degraded retrieval. Existing remedies only partially address these failures, as they often trade local positional resolution for long-context stability. We propose GAPE (Gated Adaptive Positional Encoding), a drop-in augmentation for positional encodings that introduces a content-aware bias directly into the attention logits while preserving the rotary geometry. GAPE decouples distance-based suppression from token importance through a query-dependent gate that contracts irrelevant context and a key-dependent gate that preserves salient distant tokens. We show that weakly protected distant context is exponentially attenuated as a function of the query gate, while selected keys can remain accessible through landmark protection. We further show that GAPE can be implemented within standard scaled dot-product attention. Empirically, GAPE improves long-context robustness across controlled retrieval and language-modeling experiments, extrapolating up to 8x the training length. We further retrofit GAPE into a pretrained 7B model, maintaining performance on standard benchmarks and improving performance at the longest evaluated context. These results support adaptive context suppression as a complement to positional representation for long-context generalization.

Sources

Related papers