This Attention & Transformers concept is the current idea: keep the same invariant visible across Intuition, Math, Code, Interactive Demo.
Attention & Transformers
Causal Self-Attention Mask
How decoder-only Transformers prevent future-token leakage by masking attention logits before softmax.
Concept Structure
Causal Self-Attention Mask
Start with the picture, metaphor, or geometric mechanism.
Make the objects explicit and connect them with notation.
Mirror the equations with runnable implementation details.
Manipulate the mechanism and watch the idea respond.
Learner Contract
What this page should let you do.
2 prerequisites listed; refresh them before leaning on the math or code.
Explain the mechanism, trace the main notation, and test one prediction in the live demo.
Read the intuition before the notation; the math should name a mechanism you already felt.
Follow this edge after making one prediction here; the next page should reuse the result, not restart the route.
Claim/source review status
Substantive review recorded
1/1 claims have bounded review metadata; still check caveats and source scope.Metadata-derived; review may be AI-assisted. Not a human certification.01
Intuition
Build the mental picture first so the rest of the page has something to attach to.
In a decoder-only language model, position is trying to predict what comes next using only the tokens it is allowed to know.
Without a causal mask, the token at position could attend to position , position , and so on. During training, those future tokens are sitting in the same matrix. If the model can read them, the task is no longer honest next-token prediction.
The causal mask is the read-permission rule:
query row can read key columns , and cannot read future columns .
This is different from the loss mask in the previous page. A loss mask decides which labels contribute to the objective. A causal attention mask decides which token representations can be read while computing an attention output.
02
Math
Translate the story into symbols, assumptions, and a derivation you can inspect.
Let be the sequence of hidden states. For one attention head,
with .
The unmasked score from query position to key position is
For causal self-attention, define a mask
Then attention probabilities are computed row-wise:
So every future column receives zero probability:
The output for position is
The mask changes which values can enter that sum. It does not change which labels count toward the loss.
03
Code
Keep the implementation aligned with the notation so the algorithm is legible.
import numpy as np
def softmax(x):
x = x - np.max(x)
e = np.exp(x)
return e / e.sum()
scores = np.array([
[0.20, 0.50, -0.10, 0.30, 1.40, 0.90],
[0.10, 0.30, 0.70, 0.20, 1.10, 0.40],
[0.40, 0.20, 0.50, 0.80, 0.90, 0.10],
[0.31, 0.47, 0.15, 0.25, 1.10, 0.90],
[0.05, 0.15, 0.35, 0.20, 0.60, 0.70],
[0.20, 0.10, 0.30, 0.05, 0.40, 0.55],
])
T = scores.shape[0]
mask = np.triu(np.ones((T, T)), k=1).astype(bool)
masked_scores = scores.copy()
masked_scores[mask] = -np.inf
i = 3
before = softmax(scores[i])
after = softmax(masked_scores[i])
print("query position:", i)
print("allowed keys:", [j for j in range(T) if j <= i])
print("future mass before mask:", round(before[i + 1:].sum(), 3))
print("future mass after mask:", round(after[i + 1:].sum(), 3))
assert np.allclose(after[i + 1:], 0.0)
04
Interactive Demo
Use direct manipulation to connect the explanation to a moving system.
Before reveal, pick which key positions a query row is allowed to read. The reveal shows the lower-triangular mask, the leak that would happen without it, and the masked softmax row where future-token probability is exactly zero.
Live Concept Demo
Explore Causal Self-Attention Mask
The stage is code-native and interactive. Use it to test the explanation against the mechanism.
Manipulate one control and predict the visible change.
Commit to what Causal Self-Attention Mask should make visible before reading the result.
After The First Pass
Turn the concept into an inspected object.
Once the invariant is visible in the intuition, math, code, and demo, use these panels to inspect the mechanism visually, check source support, practice the idea, and attach a grounded research question.
Mechanism Storyboard
See the idea move before the page explains it
How decoder-only Transformers prevent future-token leakage by masking attention logits before softmax.
Start with the picture, metaphor, or geometric mechanism.
Before reading further, choose the kind of change Causal Self-Attention Mask should make visible.
Visual Inquiry
Make the image answer a mathematical question
How decoder-only Transformers prevent future-token leakage by masking attention logits before softmax.
Which visible object should carry the first intuition?
Pick the cue that should make Causal Self-Attention Mask easier to reason about before the page gives the answer.
Source Grounding
Canonical references for the mechanism on this page.
Primary Transformer source. The decoder self-attention layer masks subsequent positions so autoregressive predictions cannot depend on future outputs.
Open sourcePedagogical source for decoder self-attention attending only up to the query position and preserving autoregression.
Open sourceCourse-level route source for implementing language-model components from scratch; this page uses it for route placement, not exact code claims.
Open sourceClaim Review
How decoder-only Transformers prevent future-token leakage by masking attention logits before softmax.
Claims without a substantive review badge still need exact source-support review.
vaswani-2017-attention, d2l-transformer-decoder, cs336-language-modeling-from-scratch
Use equations, runnable code, and demos to check whether the source support is operational.
Vaswani et al. state that decoder self-attention is modified to prevent positions from attending to subsequent positions and that illegal connections are masked by setting softmax inputs to negative infinity. D2L states that decoder self-attention may use positions only up to the query position to preserve autoregression.
Sources: Attention Is All You Need, Dive into Deep Learning: Transformer DecoderThis checks the causal read-permission mask only. Padding masks, document-boundary masks, packed-sequence block masks, efficient kernels, KV-cache layout, and exact framework APIs are separate implementation details.A bounded review summary is present; still check caveats and exact reference scope.Checked Vaswani et al. sections 3.1 and 3.2.3 plus D2L Transformer decoder text for masked decoder self-attention. The page is review-status pending GPT Pro publication critique.
Reviewer: codex-local-source-audit; reviewed 2026-07-02Source support candidates
paper 2017Attention Is All You NeedPrimary Transformer source. The decoder self-attention layer masks subsequent positions so autoregressive predictions cannot depend on future outputs.
reference 2026Dive into Deep Learning: Transformer DecoderPedagogical source for decoder self-attention attending only up to the query position and preserving autoregression.
course-notes 2026Stanford CS336: Language Modeling from ScratchCourse-level route source for implementing language-model components from scratch; this page uses it for route placement, not exact code claims.
Practice Loop
Try the idea before it explains itself
How decoder-only Transformers prevent future-token leakage by masking attention logits before softmax.
Before touching the demo, predict one visible change that should happen in Causal Self-Attention Mask.
Reveal when your model needs a nudge.
Reveal when your model needs a nudge.
Reveal when your model needs a nudge.
A concrete answer is on the canvas.
The answer names why the claim should hold.
It touches the page context or a neighboring idea.
Research Room
Attach the question to a claim, equation, code, or demo
Pick the concept, equation, source, runnable code, claim, misconception, or demo state before asking for help. The handoff keeps that page item in context.Open the draft below to save one note and next action in this browser.
Causal Self-Attention Mask
What is the smallest example that makes Causal Self-Attention Mask click without losing the math?
Local action draftNo local draft saved yetExpand only when ready to capture one local next action
This draft stays in this browser, attached to the selected learning item.
- References to inspect: attached references on this page.
- Definition, prerequisite, and contrast concept links
- The equation or runnable code that makes the concept operational
- One demo state that shows the invariant instead of a slogan
- The learner can state the mechanism in their own words
- The learner can name the prerequisite that would repair confusion
- The learner can predict how the mechanism changes under one perturbation
I am working in Continuous Function's research reading room. Object: concept - Causal Self-Attention Mask Selected item key: recorded for copy. Context: Attention & Transformers Page anchor: recorded for copy. Open question: What is the smallest example that makes Causal Self-Attention Mask click without losing the math? Evidence to inspect: - References to inspect: attached references on this page. - Definition, prerequisite, and contrast concept links - The equation or runnable code that makes the concept operational - One demo state that shows the invariant instead of a slogan What would resolve this: - The learner can state the mechanism in their own words - The learner can name the prerequisite that would repair confusion - The learner can predict how the mechanism changes under one perturbation Answer as a careful research tutor: stay source-grounded, separate verified evidence from assumptions, name the relevant math objects, and end with one next action.
concept/concept-notebook/attention-transformers/causal-self-attention-mask
concept:attention-transformers/causal-self-attention-mask