A causal mask matrix (also known as a look-ahead mask) is a mathematical grid used in autoregressive Transformer models like GPT to prevent the model from “cheating” by looking at future tokens during training.
|
causal mask matrix is a specific type of attention mask. An attention mask is a broad category of matrices used to hide tokens, while a causal mask is a distinct variation with a very specific structural purpose. [1, 2] The Key Difference
it is a modifier matrix applied inside the self-attention mechanism to restrict what the model can see. |
softmax function.- Q (Query): The current vector looking for context.
- \(K^{T}\) (Key Transpose): The vectors being checked against.
- \(QK^{T}\) (Dot Product): Measures geometric similarity between vectors.
- \(\sqrt{d_{k}}\) (Scaling Factor): Prevents vanishing gradients during training.
- M (Mask): Forces illegal positions to -∞.
- Softmax: Converts scores into probabilities summing to 1.
- V (Value): The actual content being aggregated. [1, 2, 3, 4, 5]
To determine the matrix dimensions for the attention equation, let’s break down each component based on standard Transformer architecture conventions (such as Vaswani et al., “Attention Is All You Need”).
Variable Definitions & Dimensions
-
$N$ (or $T$): Sequence length (number of tokens in the input/output sequence).
-
$d_k$: Dimensionality of the Queries ($Q$) and Keys ($K$).
-
$d_v$: Dimensionality of the Values ($V$). (Often $d_v = d_k$, but they can be separate).
-
$M$: Causal mask matrix (used in autoregressive models like GPT to prevent attending to future tokens).
Component-by-Component Matrix Dimensions
-
Query ($Q$): $(N \times d_k)$
-
Key ($K$): $(N \times d_k)$
-
Key Transpose ($K^T$): $(d_k \times N)$
-
Matrix Multiplication ($QK^T$):
$$(N \times d_k) \times (d_k \N \text{ or } N \times d_k \text{ transposed}) \rightarrow (N \times N)$$This creates a square matrix representing the raw alignment score between every token and every other token.
-
Scaling Factor ($\sqrt{d_k}$): A scalar value, so it does not change the dimensions of $(N \times N)$.
-
Causal Mask ($M$): $(N \times N)$
Added element-wise to the $(N \times N)$ score matrix.
-
Softmax Function: Applied row-wise, maintaining the $(N \times N)$ shape.
Attention Weights Matrix: $(N \times N)$
-
Value Matrix ($V$): $(N \times d_v)$
-
Final Causal Attention Output:
$$\text{Attention Weights } (N \times N) \times V (N \times d_v) \rightarrow (N \times d_v)$$
Summary Table
| Matrix / Component | Mathematical Notation | Dimensions |
| Query ($Q$) | $Q$ | $N \times d_k$ |
| Key ($K$) | $K$ | $N \times d_k$ |
| Key Transpose ($K^T$) | $K^T$ | $d_k \times N$ |
| Score / Mask Matrix | $\frac{QK^T}{\sqrt{d_k}} + M$ | $N \times N$ |
| Attention Weights | $\text{softmax}(\dots)$ | $N \times N$ |
| Value ($V$) | $V$ | $N \times d_v$ |
| Final Output | $\text{Attention}(Q,K,V)$ | $N \times d_v$ |
if We have single head
Sequence length number of tokens =2048
attention head size = 1000
Dimensionality of the Values=1000
Since you specified a single head, the token embedding dimension (or projection space for that layer) is 1000.
|
| Matrix | Symbol | Shape / Dimensions | Explanation |
| Query Weight Matrix | $W_q$ | $1000 \times 1000$ | ($d_{model} \times d_k$) — Projects input embedding to Query space |
| Key Weight Matrix | $W_k$ | $1000 \times 1000$ | ($d_{model} \times d_k$) — Projects input embedding to Key space |
| Value Weight Matrix | $W_v$ | $1000 \times 1000$ | ($d_{model} \times d_v$) — Projects input embedding to Value space |
| Output Weight Matrix | $W_o$ | $1000 \times 1000$ | ($d_v \times d_{model}$) — Projects the attention output back to the model dimension |
- “Data” can only see “Data”.
- “science” can see “Data” and “science”.
- “is” can see “Data”, “science”, and “is”. [1]
=-=-=-=-=-=-=-=-=-=-=-
Example with padding (without sequence packing):
The creation of causal mask matrix for heap
1. I love to eat pizza with extra cheese on top
2. I love to eat burgers with extra ketchup
3. On top of the pizza is extra cheese
4. Cheese and burgers go well together
<PAD>).Step 1: Analyze the Sequences
- Sentence 1:
[I, love, to, eat, pizza, with, extra, cheese, on, top](10 tokens) - Sentence 2:
[I, love, to, eat, burgers, with, extra, ketchup](8 tokens) - Sentence 3:
[On, top, of, the, pizza, is, extra, cheese](8 tokens) - Sentence 4:
[Cheese, and, burgers, go, well, together](6 tokens)
Step 2: The $10 \times 10$ Base Causal Mask Matrix ($M$)
0 where attention is allowed (past and present) and $-\infty$ where it must be blocked (future). [1] Step 3: Application to Your Sentences
Sentence 1 Matrix Map (10 Tokens)
| Token | I | love | to | eat | pizza | with | extra | cheese | on | top |
|---|---|---|---|---|---|---|---|---|---|---|
| I | 0 | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ |
| love | 0 | 0 | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ |
| to | 0 | 0 | 0 | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ |
| eat | 0 | 0 | 0 | 0 | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ |
| pizza | 0 | 0 | 0 | 0 | 0 | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ |
| with | 0 | 0 | 0 | 0 | 0 | 0 | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ |
| extra | 0 | 0 | 0 | 0 | 0 | 0 | 0 | $-\infty$ | $-\infty$ | $-\infty$ |
| cheese | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | $-\infty$ | $-\infty$ |
| on | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | $-\infty$ |
| top | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Sentence 4 Matrix Map (6 Tokens + 4 Padding Tokens)
| Token | Cheese | and | burgers | go | well | together | <PAD> |
<PAD> |
<PAD> |
<PAD> |
|---|---|---|---|---|---|---|---|---|---|---|
| Cheese | 0 | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ |
| and | 0 | 0 | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ |
| burgers | 0 | 0 | 0 | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ |
| go | 0 | 0 | 0 | 0 | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ |
| well | 0 | 0 | 0 | 0 | 0 | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ |
| together | 0 | 0 | 0 | 0 | 0 | 0 | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ |
<PAD> |
$-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ |
<PAD> |
$-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ |
<PAD> |
$-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ |
<PAD> |
$-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ | $-\infty$ |
![]()
