Scaling Attention via Feature Sparsity
Organizations: School of Electronic Engineering, Xidian University · Stony Brook University · TUM · MIT · Amazon AGI SF Lab
Abstract
Scaling Transformers to ultra-long contexts is bottlenecked by the cost of self-attention. Existing methods reduce this cost along the sequence axis through local windows, kernel approximations, or token-level sparsity, but these approaches consistently degrade accuracy. In this paper, we instead explore an orthogonal axis: feature sparsity. We propose Sparse Feature Attention (SFA), where queries and keys are represented as -sparse codes that preserve high-dimensional expressivity while reducing the cost of attention from to . To make this efficient at scale, we introduce FlashSFA, an IO-aware kernel that extends FlashAttention to operate directly on sparse overlaps without materializing dense score matrices. Across GPT-2 and Qwen3 pretraining, SFA matches dense baselines while improving speed by up to and reducing FLOPs and KV-cache by nearly 50%. On synthetic and downstream benchmarks, SFA preserves retrieval accuracy and robustness at long contexts, outperforming short-embedding baselines that collapse feature diversity. These results establish feature-level sparsity as a complementary and underexplored axis for efficient attention, enabling Transformers to scale to orders-of-magnitude longer contexts with minimal quality loss. Code is available at https://github.com/YannX1e/Sparse-Feature-Attention.
Figures & tables
| Model | Variant | General Tasks | NIAH Acc | |||||
|---|---|---|---|---|---|---|---|---|
| GSM-8K | Arxiv | PubMed | 4096 | 8192 | 16384 | 32768 | ||
| Qwen3-0.6B | Base | 59.59 | 13.65 | 10.48 | 90 | 87 | 77 | 52 |
| Fine-tune | 63.42 | 41.17 | 40.54 | 94 | 92 | 79 | 55 | |
| SFA ( ) | 61.46 | 39.14 | 39.03 | 95 | 93 | 77 | 53 | |
| Qwen3-4B | Base | 75.44 | 31.52 | 29.19 | 97 | 95 | 90 | 81 |
| Fine-tune | 76.18 | 49.31 | 49.05 | 99 | 96 | 92 | 84 | |
Appendix figures & tables15 assets
Supplementary material from the paper’s appendix.
Appendix
| Size | #Parameters | hidden_size | num_layers | num_heads | short_hidden | position_embedding |
|---|---|---|---|---|---|---|
| Small | 124M | 768 | 12 | 12 | 384 | APE |
| Medium | 350M | 1024 | 24 | 16 | 512 | APE |
| Large | 596M | 1024 | 28 | 16/8 | 512 | RoPE |
| Model | Dataset | Epoch | Batch_Size | Lr | Warmup_Ratio | Gradient_Ckecpointing |
|---|---|---|---|---|---|---|
| Qwen3-0.6B | GSM8K | 3 | 256 | 6e-4 | 0.1 | False |
| Arxiv | 2 | 256 | 1e-5 | 0.05 | False | |
| PubMed | 2 | 256 | 2e-5 | 0.05 | False | |
| NIAH | 3 | 256 | 2e-5 | 0.05 | False | |
| Qwen3-4B | GSM8K | 3 | 256 | 6e-6 | 0.1 | True |
| Arxiv | 2 | 256 | 2e-6 | 0.1 | True |
| 8192 | 16384 | 32768 | 65536 | |||||
|---|---|---|---|---|---|---|---|---|
| Config | TFLOPs | INOPs | TFLOPs | INOPs | TFLOPs | INOPs | TFLOPs | INOPs |
| Dense_128 | / | / | / | / | ||||
| Sparse_32/128 | ||||||||
| Sparse_16/128 | ||||||||
| Sparse_8/128 | ||||||||
| Dense_64 | / | / | / | / | ||||
| Kernel | Dense | Dense w/o compute | FlashSFA | FlashSFA w/o compute |
|---|---|---|---|---|
| HBM Bandwidth (GB/s) | 14.22 | 1194.34 | 17.14 | 919.38 |
| Context Length ( ) | 1024 | 4096 | 8192 | 16384 | 32768 | 65536 |
|---|---|---|---|---|---|---|
| torch.topk | 0.730 | 2.701 | 5.336 | 10.684 | 21.205 | 42.374 |
| RTop- (Ours) | 0.221 | 0.589 | 1.089 | 2.080 | 4.057 | 8.080 |
| Ratio of RTop- (%) | 10.51 | 2.10 | 1.96 | 1.90 | 1.03 | 0.51 |
| Context Length | ||||||
| Variant | 1024 | 4096 | 8192 | 16384 | 32768 | 65536 |
| Dense_256 | ||||||
| Sparse_32/256 | ||||||
| Sparse_24/256 | ||||||
| Sparse_16/256 | ||||||
| Sparse_12/256 | ||||||
| Model | Variant | Latency@128k | PPL | Acc | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Decode | Forward | OWT/Pile | PiQA | LAMBDA | ARC-e | ARC-c | HellaS | Avg | ||
| GPT2 124M | Dense (full) | 17.08 | 16.86 | 17.29 | 56.34 | 22.78 | 28.35 | 14.32 | 19.61 | 28.28 |
| Token-Level Operation | ||||||||||
| Longformer | 6.75 | 7.93 | 18.73 | 54.25 | 21.27 | 28.02 | 13.01 | 18.92 | 28.10 | |
| +SFA ( ) | 5.23 | 6.18 | 19.30 | 52.81 | 20.54 | 26.39 | 12.59 | 17.24 | 25.91 | |
| Feature-Level Operation | ||||||||||
| Model | Latency@128k | PPL | Acc | |||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Decode | Forward | OWT/Pile | PiQA | LAMBDA | ARC-e | ARC-c | HellaS | Avg | ||
| GPT2 124M | Dense (full) | 17.08 | 16.86 | 17.29 | 42.74 | 22.78 | 28.35 | 8.12 | 19.61 | 24.32 |
| SFA | 14.12 | 9.41 | 18.17 | 41.62 | 21.03 | 28.41 | 7.39 | 19.26 | 23.54 | |
| Token Sparse (Training) | ||||||||||
| Routing | 7.92 | 8.37 | 18.64 | 41.39 | 21.08 | 28.31 | 7.11 | 18.89 | 23.35 | |
| Longformer | 6.75 | 7.93 | 18.73 | 41.28 | 21.27 | 28.02 | 7.01 | 18.92 | 23.30 | |
| Context Length | 1k | 2k | 3k | 4k | Speedup@4k |
|---|---|---|---|---|---|
| Dense(full) | 93 | 87 | 79 | 62 | 1.0x |
| SFA( ) | 95 | 90 | 80 | 66 | 1.5x |
| SFA( ) | 96 | 90 | 83 | 71 | 1.2x |