Zenaique

Name the additive bias term in attention and the two things it most commonly carries.

MCQ·Easy·4.0 · 0·~1 min·Asked atHclLangChainNeptune Ai·Relevant atAi4bharatCerebrasDeepseekMicrosoft
Attempt it
TL;DR

Pre-softmax bias matrix B added to QK^T / sqrt(d_k). Carries masking (-inf entries) and positional bias (ALiBi-style linear distance penalty).

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

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.

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:

scores=QKdk+B,weights=softmax(scores)\text{scores} = \frac{QK^\top}{\sqrt{d_k}} + B, \quad \text{weights} = \text{softmax}(\text{scores})

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.

Use 1: Masking
Use 2: Positional bias
Implementation, kernels, and production knobs
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.
UseEntries of BEffect after softmaxExample
Causal mask-inf above diagonalZero weight to future positionsEvery decoder-only LLM
Padding mask-inf at padding positionsZero weight to padding tokensBatched inference with variable lengths
ALiBi-m_h * |i - j|Distance-decayed weighting per headBLOOM, MPT
Sliding window-inf outside windowAttention confined to windowMistral 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.
Sign in to see more production examples.

What an interviewer would ask next. Try answering before peeking at the approach.

QHow does FlashAttention handle the bias matrix?
A

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|.

2 more follow-ups an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

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.

Sign in to see all red flags and common mistakes.

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

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
Explain scaled dot product attention.
Short answer·Medium