Pre-softmax bias matrix B added to QK^T / sqrt(d_k). Carries masking (-inf entries) and positional bias (ALiBi-style linear distance penalty).
Picture a music mixing board where every track has a single 'tweak' knob that the engineer can use for whatever they need on that song. For one song the knob is used to fully mute certain instruments before they hit the speakers. For another song the same knob is used to subtly turn down anything that should sound far away. Same knob, different purpose each time, and both purposes happen before the final mix is heard. The attention bias matrix is that knob. Set entries to negative infinity to mute keys (masking). Set them to distance-based penalties to make far keys quieter (ALiBi).
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
The attention bias matrix B is a small but load-bearing piece of the transformer architecture. It is the single additive per-position slot at the pre-softmax score level, and the same slot serves at least two completely different purposes across modern models.
This deep dive walks through the canonical attention formula with the bias included, explains why the pre-softmax stage is the only sensible place for this kind of injection, lists the major uses (masking and positional bias), and connects to how production kernels handle the bias.
Mental model: B is a single (n, n) per-head matrix added to the scores before softmax. Set entries to -inf to block; set entries to finite values to bias.
The canonical formula with B
Where B lives
The full attention formula with bias is:
Then the context vector is weights @ V. B is a (seq_len, seq_len) matrix that can be per-head or shared across heads.
Why pre-softmax
Three stages of attention could in principle accept a position-aware modification: pre-softmax scores, post-softmax weights, or the value vectors V. Only pre-softmax is well-behaved.
- Post-softmax additions break the probability-sum invariant (weights must sum to 1 across keys).
- Modifications to V change the output magnitude without affecting routing.
- Pre-softmax additions compose naturally with softmax: -inf produces 0, finite values produce reweighting.
This is why every attention scheme that needs a per-position signal puts it at the pre-softmax bias slot.
Why -inf produces zero
Softmax of x = exp(x) / sum(exp(...)). For x = -inf, exp(x) = 0, so the contribution to both numerator and denominator is exactly 0. Other entries that are finite get their share of the remaining probability. This is mathematically clean and is the standard way to implement attention masking.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
| Use | Entries of B | Effect after softmax | Example |
|---|---|---|---|
| Causal mask | -inf above diagonal | Zero weight to future positions | Every decoder-only LLM |
| Padding mask | -inf at padding positions | Zero weight to padding tokens | Batched inference with variable lengths |
| ALiBi | -m_h * |i - j| | Distance-decayed weighting per head | BLOOM, MPT |
| Sliding window | -inf outside window | Attention confined to window | Mistral 7B, Llama with SWA |
Real products, models, and research that use this idea.
- Every causal LLM (Llama 4 Maverick, GPT-5.5, Claude Opus 4.7, Mistral, Qwen 3.5) uses the bias slot to apply the causal mask.
- BLOOM and MPT use the bias slot for ALiBi per-head linear distance penalties.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow does FlashAttention handle the bias matrix?
FlashAttention takes B as an optional argument or computes it on the fly as a closed-form function of i and j inside the kernel. The latter is cheaper because it avoids allocating a full (n, n) tensor in HBM. ALiBi composes naturally with this on the fly approach because the bias is just -m_h * |i - j|.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Placing the bias at the wrong stage of the pipeline, after V, inside the dot product, or at the output projection. The canonical attention bias sits at the pre-softmax score matrix.
60 second bullets to scan on the way to the call.
Canonical attention formula with the bias slot
Why -inf entries produce 0 weight after softmax
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.