Injecting and Controlling Locality Priors in Transformer Self-Attention
Results • Training • Analysis • Installation • Quick Start
Standard Transformer self-attention treats all positions equally, requiring models to learn locality from scratch. This is sample-inefficient and leads to "attention noise"—models wasting probability mass on irrelevant distant tokens.
Adaptive Locality Attention (ALA) addresses this by injecting a learnable locality prior directly into attention logits, with a per-token gate that allows the model to dynamically switch between local and global attention modes.
- Locality Prior: A learnable distance penalty that encourages attention to nearby tokens
- Adaptive Gating: Per-token gates that allow the model to bypass locality when global context is needed
- Mechanistic Analysis: Beyond perplexity—I analyze attention distance, local mass concentration, and gate behavior
- Reproducible Framework: Multi-seed experiments with statistical significance testing
Experiments on WikiText-103 (~51M parameters, 10K training steps, 3 seeds):
| Variant | Val Perplexity ↓ | Val Loss ↓ | Top-1 Acc ↑ | Top-5 Acc ↑ |
|---|---|---|---|---|
| Baseline | 36.20 ± 0.44 | 3.589 ± 0.012 | 38.56% | 59.43% |
| DistBias | 35.54 ± 0.27 | 3.571 ± 0.008 | 38.79% | 59.74% |
| ALA (Gated) | 35.71 ± 0.33 | 3.575 ± 0.009 | 38.62% | 59.59% |
- ~0.65 PPL improvement from injecting locality prior
- >50% reduction in mean attention distance (92 → 43-50 tokens)
- Adaptive gating works: g ≈ 0.75 shows the model learns a nuanced policy
- Minimal overhead: ~12-17% throughput reduction with unchanged O(T²) complexity
Both locality-biased variants achieve lower perplexity than baseline. DistBias shows the strongest improvement, while ALA (gated) trades a small amount of PPL for adaptive control.
All variants converge stably. The locality-biased models (distbias, gated) show consistently lower validation loss throughout training.
The biased variants show a slight but consistent edge in validation accuracy, confirming that the locality prior helps the model make better predictions.
Beyond perplexity, I analyze how the attention mechanism changes internally.
| Variant | Mean Expected Distance E[|i-j|] ↓ | |:--------|:---------------------------------:| | Baseline | 92.67 ± 2.14 | | DistBias | 43.70 ± 2.83 | | ALA (Gated) | 50.54 ± 1.15 |
Key insight: The baseline model spreads attention broadly (~93 tokens back on average). The locality prior drastically reduces this to ~44-50 tokens—a >50% reduction in attention distance.
| Variant | LocalMass@32 ↑ | LocalMass@64 ↑ |
|---|---|---|
| Baseline | 33.9% | 52.0% |
| DistBias | 55.6% | 77.1% |
| ALA (Gated) | 53.0% | 73.7% |
Key insight: In the baseline, nearly half of attention is cast outside the 64 most recent tokens. With locality bias, over 70% of attention stays local—a significant sharpening of focus.
Lower entropy indicates more concentrated (less uniform) attention distributions. The biased variants show reduced entropy, confirming sharper attention patterns.
Average Gate Value: 0.7504 ± 0.0014
Key insight: The model does NOT collapse to g=1 (pure local) or g=0 (pure global). Instead, it learns a nuanced policy—applying ~75% of the maximum locality penalty on average, while retaining the ability to reduce it for tokens that need long-range context. This is clear evidence of adaptive control.
| Variant | Throughput (tok/s) | Overhead | Parameters |
|---|---|---|---|
| Baseline | 150,547 ± 193 | — | 51.20M |
| DistBias | 131,584 ± 74 | -12.6% | 51.20M |
| ALA (Gated) | 124,396 ± 192 | -17.3% | 51.23M |
- Complexity: O(T²) unchanged from baseline
- Memory: Nearly identical footprint
- Parameter overhead: ALA adds only ~33K parameters (the gating projection)
The throughput reduction comes from extra element-wise operations (distance matrix computation and gating), but remains practical for research and production use.
Problem: Standard attention computes similarity without positional preference:
Solution: Add a learnable distance penalty:
Innovation (ALA): Make the penalty conditional via a per-token gate:
- When g ≈ 1: Head enforces strong locality (syntax, morphology)
- When g ≈ 0: Head ignores distance (coreference, retrieval)
- The model learns when to be local vs. global
| Variant | Description | Added Parameters |
|---|---|---|
baseline |
Standard scaled dot-product attention | None |
distbias |
+ Learnable per-head distance penalty (αₕ) | H scalars |
gated_distbias (ALA) |
+ Per-token gate to modulate penalty | + Linear(d→H) |
- Python 3.9+
- PyTorch 2.2+
- CUDA-capable GPU (recommended)
# Clone the repository
git clone https://github.com/yourusername/adaptive-locality-attention.git
cd adaptive-locality-attention
# Create virtual environment
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
# Install dependencies
pip install -r requirements.txttorch>=2.2
transformers>=4.40
datasets>=2.18
tqdm>=4.66
numpy>=1.24
pandas>=2.0
matplotlib>=3.8
pyyaml>=6.0
tabulate>=0.9
This trains all 3 variants with 3 seeds each and generates all results:
bash scripts/run_wt103_512_suite.shOutputs:
results/wt103_bs512_aggregate.md— Aggregated metrics (mean ± std)results/wt103_bs512_runs.md— Per-run detailed resultsresults/wt103_bs512_*.png— All training curves and bar charts
python src/train.py --config configs/wt103_512_gated.yamlpython src/analyze_attention.py --checkpoint runs/your_run/checkpoint.ptadaptive_locality_attention/
├── configs/ # YAML experiment configurations
│ ├── wt103_512_baseline.yaml
│ ├── wt103_512_distbias.yaml
│ └── wt103_512_gated.yaml
├── src/ # Source code
│ ├── attention.py # ALA implementation
│ ├── model.py # Transformer architecture
│ ├── train.py # Training loop
│ ├── eval.py # Evaluation utilities
│ ├── analyze_attention.py # Mechanistic analysis
│ ├── aggregate_results.py # Multirun aggregation
│ └── plot_metrics.py # Visualization
├── scripts/ # Automation scripts
│ └── run_wt103_512_suite.sh # Full experiment suite
├── results/ # Generated plots and tables
│ ├── wt103_bs512_*.png # All visualizations
│ ├── wt103_bs512_aggregate.md
│ └── wt103_bs512_runs.md
├── reports/ # Documentation
│ ├── ALA_Report.pdf # Full technical report
├── presentation/ # Presentation slides
├── requirements.txt
└── README.md
Experiments are controlled via YAML configs:
# configs/wt103_512_gated.yaml
seed: 1337
data:
dataset: wikitext103
tokenizer_name: gpt2
block_size: 512
batch_size: 16
model:
n_layers: 8
n_heads: 8
d_model: 512
d_ff: 2048
dropout: 0.1
# ALA-specific
attn_variant: gated_distbias # baseline | distbias | gated_distbias
alpha_init: 0.01 # Initial locality strength
alpha_learnable: true # Learn α during training
gate_enabled: true # Enable per-token gating
train:
max_steps: 10000
lr: 0.0002
warmup_steps: 400
weight_decay: 0.1The key innovation is in the attention score computation:
# Standard attention logits
att = (q @ k.transpose(-2, -1)) / math.sqrt(self.head_dim)
# Add locality bias
if self.use_distbias:
alpha = F.softplus(self.raw_alpha) # Ensure α > 0
dist = self.dist[:T, :T] # |i-j| matrix
if self.gate_enabled:
gate = torch.sigmoid(self.gate_proj(x)) # Per-token gate
bias = -alpha * gate * dist
else:
bias = -alpha * dist
att = att + bias| Run | Variant | Val PPL | Val Acc1 | E[|i-j|] | LocalMass@64 | Gate | |:----|:--------|:-------:|:--------:|:--------:|:------------:|:----:| | seed1337 | baseline | 36.63 | 38.54% | 94.91 | 50.97% | — | | seed42 | baseline | 35.60 | 38.68% | 89.79 | 53.02% | — | | seed2025 | baseline | 36.37 | 38.46% | 93.31 | 52.10% | — | | seed1337 | distbias | 35.68 | 38.75% | 47.57 | 74.71% | — | | seed42 | distbias | 35.16 | 38.62% | 40.86 | 79.13% | — | | seed2025 | distbias | 35.76 | 39.01% | 42.68 | 77.52% | — | | seed1337 | gated | 35.31 | 38.41% | 52.17 | 72.59% | 0.749 | | seed42 | gated | 36.13 | 39.05% | 49.81 | 74.42% | 0.751 | | seed2025 | gated | 35.69 | 38.41% | 49.65 | 74.18% | 0.752 |
| Document | Description |
|---|---|
reports/ALA_Report.pdf |
Full technical report |
presentation/ |
Presentation slides |
results/wt103_bs512_aggregate.md |
Aggregated results |
All experiments use:
- 3 random seeds: 1337, 42, 2025
- Fixed hyperparameters across variants
- Same data splits and preprocessing
Results are reported as mean ± standard deviation across seeds.
# Reproduce all results
bash scripts/run_wt103_512_suite.sh- Sparse Attention: Use learned locality to prune distant keys → O(T log T) or O(T)
- Multi-scale Gating: Different α values for different distance ranges
- Cross-attention: Apply locality priors to encoder-decoder attention
- Larger Scale: Validate on billion-parameter models
@misc{agnihotri2025ala,
author = {Agnihotri, Aman},
title = {Adaptive Locality Attention: Injecting and Controlling
Locality Priors in Transformer Self-Attention},
year = {2025},
howpublished = {Independent Research Project},
institution = {Delhi Skill & Entrepreneurship University}
}- Vaswani, A., et al. (2017). Attention Is All You Need. NeurIPS.
- Merity, S., et al. (2017). Pointer Sentinel Mixture Models. ICLR.
Aman Agnihotri
B.Sc. Data Analytics, Delhi Skill & Entrepreneurship University
Independent Research Project • December 2025
A solo research project exploring interpretable and controllable attention mechanisms in Transformers.









