Differential Transformer (Diff Transformer): Mathematical Foundations of Differential Attention, Noise Cancellation, and Hallucination Suppression
Standard Transformer architectures rely on softmax attention to route information between tokens in an autoregressive sequence. While effective across scale, softmax attention suffers from a structural tendency to allocate non-trivial probability mass to irrelevant context tokens. In long-context retrieval, multi-hop reasoning, and document summarization, this residual probability mass (termed attention noise) accumulates across layers, diluting relevant signals and driving contextual hallucinations.
Differential Transformer (Diff Transformer), introduced by Microsoft Research and Tsinghua University (Ye et al., ICLR 2025 Oral), addresses this dynamic by reformulating the core attention operator. Drawing on the principle of differential amplification from electrical engineering, Diff Transformer computes attention as the difference between two separate softmax attention maps. This subtraction nulls out common-mode attention noise, yielding sparse attention patterns, reducing activation outliers, and cutting the parameter and token budget required to reach baseline loss targets.

The Attention Noise Mechanism in Standard Transformers
Standard scaled dot-product attention computes token representations via:
Because the softmax function maps any real-valued input vector to the standard simplex where elements are strictly positive and sum to 1:
every token in the sequence receives a non-zero attention weight regardless of its relevance to the current query. When sequence length scales to thousands or tens of thousands of tokens, the sum of small, spurious attention weights assigned to irrelevant background tokens often exceeds the weight assigned to the critical target token.
In key-information retrieval tasks such as Needle-In-A-Haystack, standard Transformers allocate as much as 49% to 54% of their cumulative attention weight to background noise context, while allocating under 10% to the target answer span. This diffuse attention distribution degrades token contextualization and causes downstream projection layers to incorporate extraneous information into the residual stream.
Mathematical Formulation of Differential Attention
Differential attention replaces the single softmax operator with a pair of softmax functions over split subspace projections.
Given an input matrix , the model projects into two pairs of query and key matrices and a single value matrix:
where , , and represents the per-head dimension. The differential attention operator is formulated as:
where is a learnable scalar parameter that regulates the cancellation strength.
Learnable Scalar Reparameterization
To stabilize optimization and prevent from drifting into unstable negative regimes or oscillating during early training iterations, is reparameterized through learnable inner-product vectors:
where are learnable vectors initialized to zero, and is a layer-dependent initialization constant.
The default layer initialization schedule follows an exponential curve:
where is the layer index. This schedule sets in early layers, where broader contextual mixing is beneficial, and increases asymptotically toward in deeper layers, where sharp, noise-free routing is necessary.
Multi-Head Differential Attention and Normalization Dynamics
Multi-head Differential Attention distributes computation across attention heads, where to match the parameter count and computational complexity of a standard Transformer with head dimension .
For each head , the output is computed and normalized:
where is the output projection matrix.
Gradient Alignment and Headwise RMSNorm
Because differential attention allows individual attention map entries to take negative values before value multiplication, the variance of outputs across heads varies more widely than in standard softmax attention. Applying independently per head (equivalent to GroupNorm with group size equal to head dimension) standardizes the scale across heads.
The fixed multiplier scales the normalized head output to ensure that parameter gradient magnitudes remain equivalent to standard Transformer baselines:
Because $\operatorname{softmax}(Q_1 K_1^T / \sqrt{d}) - \lambda \operatorname{softmax}(Q_2 K_2^T / \sqrt{d})$ matches the gradient order of standard attention, optimizers invariant to gradient scaling (such as AdamW) can train Diff Transformer models using identical learning rates and warmup schedules without modification.

Empirical Properties and Benchmark Findings
Empirical evaluations across language modeling, key-information retrieval, and contextual hallucination benchmarks reveal several structural advantages over conventional Transformer baselines.
1. Compute and Parameter Scaling Efficiency
Scaling experiments across parameter ranges from 830M to 13.1B demonstrate consistent power-law improvements:
- A 6.8B Diff Transformer matches the validation loss of an 11B standard Transformer, utilizing 62.2% of the parameter budget.
- A 7.8B Diff Transformer achieves performance parity with a 13.1B standard Transformer, requiring 59.5% of the parameters.
- When scaling training tokens on 3B-parameter models, Diff Transformer trained on 160B tokens reaches the validation loss of a standard Transformer trained on 251B tokens (63.7% of compute).
2. Multi-Needle Retrieval and Long-Context Stability
On the Needle-In-A-Haystack benchmark extended to 64K context windows:
- In 4K contexts with 6 inserted needles and 2 queries (), standard Transformer retrieval accuracy degrades to 55%, while Diff Transformer maintains 85% accuracy (a 30 percentage point margin).
- In 64K context windows where target needles are positioned at 25% depth, Diff Transformer achieves an absolute accuracy improvement of 76% over standard Transformers.
- Attention score distributions show that Diff Transformer allocates 27% to 40% of attention weight directly to target answer spans, while reducing background noise attention from ~50% down to 1% to 2%.
3. Contextual Hallucination Reduction
Evaluating 3B-parameter models on single-document and multi-document question answering and summarization via LongBench and Long-Context QA tasks:
- XSum Summarization: Diff Transformer achieves 0.53 factual accuracy versus 0.44 for standard Transformers.
- MultiNews Summarization: Accuracy reaches 0.61 versus 0.42.
- HotpotQA Multi-Hop QA: Accuracy reaches 0.46 versus 0.36.
- Qasper Technical QA: Accuracy reaches 0.39 versus 0.28.
4. Suppression of Activation Outliers
Standard LLM architectures exhibit severe activation outliers in pre-softmax attention logits and hidden states, which complicate post-training quantization (PTQ) and low-bit serving (INT8/INT4/FP8).
Comparative profiling of 3B models indicates that differential attention suppresses outlier emergence:
- Attention Logits (Top-1 Value): Drops from 318.0 in standard Transformer to 38.8 in Diff Transformer (an 87.8% reduction).
- Attention Logits (Top-10 Value): Drops from 284.7 to 32.0.
- Hidden States (Top-1 Value): Drops from 3608.6 to 1688.2 (a 53.2% reduction).
This suppression provides structural benefits for uniform integer quantization kernels without requiring specialized outlier-migration steps like SmoothQuant.
Kernel Implementation via FlashAttention
Differential attention can be implemented without custom low-level CUDA kernels by decomposing the operation into two standard FlashAttention forward and backward calls:
import torch
import flash_attn
def differential_attention(
q1: torch.Tensor,
q2: torch.Tensor,
k1: torch.Tensor,
k2: torch.Tensor,
v: torch.Tensor,
lambda_val: torch.Tensor,
sm_scale: float,
) -> torch.Tensor:
# Compute first attention map output: softmax(Q1 K1^T / sqrt(d)) V
attn1 = flash_attn.flash_attn_func(
q1, k1, v, softmax_scale=sm_scale, causal=True
)
# Compute second attention map output: softmax(Q2 K2^T / sqrt(d)) V
attn2 = flash_attn.flash_attn_func(
q2, k2, v, softmax_scale=sm_scale, causal=True
)
# Differential cancellation
return attn1 - lambda_val * attn2Because FlashAttention tiles computation directly in GPU SRAM, executing two passes over avoids materializing the attention matrices in HBM while preserving the exact numerical cancellation properties of differential attention.
Sources
- Differential Transformer (arXiv:2410.05258)
- Differential Transformer ICLR 2025 Paper
- Attention Is All You Need (arXiv:1706.03762)
- FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning (arXiv:2307.08691)
- Root Mean Square Layer Normalization (arXiv:1910.07467)
- GLU Variants Improve Transformer (arXiv:2002.05202)
