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

  • Attention Mask (Padding Mask): Blocks the model from looking at filler/empty space. It hides the <pad> tokens added to the end of short sentences to keep batch sizes uniform. It can mask elements anywhere in the sequence. [1, 2, 3, 4, 5]
  • Causal Mask (Look-Ahead Mask): Blocks the model from looking at the future. It hides subsequent words to enforce chronological order. It is always a rigid, upper-triangular matrix shape

it is a modifier matrix applied inside the self-attention mechanism to restrict what the model can see.

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
In an autoregressive model (like GPT), standard self-attention is computed by multiplying Queries (Q) and Keys (K) to create raw attention scores. Left unrestricted, this allows a token to look into the future. [1, 2]
To fix this, the causal mask matrix (M) is added directly to those raw attention scores right before the Softmax activation function: [1, 2]
 
Attention Weights   \(       \text{Alignment Scores}(Q,K) = \text{softmax}         \left(      \frac{QK^{T}}{\sqrt{d_{k}}}+M       \right)     \)
 
 
attention is a weighted probability distribution that determines how much focus to place on different parts of an input sequence. While your formula specifically computes the attention weights (or alignment scores), the complete attention mechanism multiplies these weights by a Value matrix V to produce a weighted sum of the inputs.
 
Causal Self-Attention=  \(\text{Causal\ Attention}(Q,K,V) = \text{softmax}\left(\frac{QK^{T}}{\sqrt{d_{k}}}+M\right)V\)
The causal mask matrix M (filled with 0s and -∞)  blocks tokens from looking forward right after calculating (\(QK^{T}\)), but before the values are converted into final probabilities by the 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

  1. Query ($Q$): $(N \times d_k)$

  2. Key ($K$): $(N \times d_k)$

  3. Key Transpose ($K^T$): $(d_k \times N)$

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

  5. Scaling Factor ($\sqrt{d_k}$): A scalar value, so it does not change the dimensions of $(N \times N)$.

  6. Causal Mask ($M$): $(N \times N)$

    Added element-wise to the $(N \times N)$ score matrix.

  7. Softmax Function: Applied row-wise, maintaining the $(N \times N)$ shape.

    Attention Weights Matrix: $(N \times N)$

  8. Value Matrix ($V$): $(N \times d_v)$

  9. 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$
 
 
for example  

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 / Component Symbol Shape / Dimensions Explanation
Query Matrix $Q$ $2048 \times 1000$ (Sequence Length $\times$ Query Dimension $d_k$)
Key Matrix $K$ $2048 \times 1000$ (Sequence Length $\times$ Key Dimension $d_k$)
Key Transpose $K^T$ $1000 \times 2048$ Transposed from $K$
Value Matrix $V$ $2048 \times 1000$ (Sequence Length $\times$ Value Dimension $d_v$)
Attention Mask $M$ $2048 \times 2048$ Square matrix matching the sequence length ($N \times N$)
Product $QK^T$ $QK^T$ $2048 \times 2048$ $(2048 \times 1000) \times (1000 \times 2048)$ resulting in $N \times N$
Scaled Score + Mask $\frac{QK^T}{\sqrt{d_{k}}} + M$ $2048 \times 2048$ Element-wise addition preserves dimensions
Attention Weights $\text{softmax}(\dots)$ $2048 \times 2048$ Applied row-wise, preserving the $N \times N$ shape
Final Output Causal Attention $2048 \times 1000$ $(2048 \times 2048) \times (2048 \times 1000)$ resulting in Sequence Length $\times d_v$
 
While
 
 
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
 
 
 
 
 
 
 
 
 
 
 
The Masking Visualized
Gemini:
If you are processing the phrase “Data science is fun”, the standard attention scores create a full grid where every word looks at every word. The causal mask matrix (M) looks like this: [1]
\(\left(\begin{matrix}0&-\infty &-\infty &-\infty \\ 0&0&-\infty &-\infty \\ 0&0&0&-\infty \\ 0&0&0&0\end{matrix}\right)\)
When this mask is added to the attention scores, the positions with -∞ get completely zeroed out by the Softmax function (\(\text{e}^{-\infty} = 0\)). As a result: [1, 2]
  • “Data” can only see “Data”.
  • “science” can see “Data” and “science”.
  • “is” can see “Data”, “science”, and “is”. [1]
This allows the Transformer to train on the entire sentence in parallel while perfectly mimicking the step-by-step nature of sequential human text generation.

=-=-=-=-=-=-=-=-=-=-=-

 

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

To create the causal mask matrix for your specific text data, we must first map the dataset into a Vocabulary (unique words) and determine our sequence length.
When dealing with multiple sentences of different lengths, we use the length of the longest sentence as our max sequence length ($N$). Any shorter sentences are extended using a special pad token (e.g., <PAD>).
Here is how the causal mask matrix is created step-by-step for your dataset.

Step 1: Analyze the Sequences

Let’s break down your sentences into tokens (words).
 
  1. Sentence 1: [I, love, to, eat, pizza, with, extra, cheese, on, top] (10 tokens)
  2. Sentence 2: [I, love, to, eat, burgers, with, extra, ketchup] (8 tokens)
  3. Sentence 3: [On, top, of, the, pizza, is, extra, cheese] (8 tokens)
  4. Sentence 4: [Cheese, and, burgers, go, well, together] (6 tokens)

The longest sequence is Sentence 1, which contains 10 tokens. Therefore, our core causal mask matrix must be an $10 \times 10$ matrix ($N = 10$). Shorter sentences will use a slice of this matrix or combine it with a padding mask.

Step 2: The $10 \times 10$ Base Causal Mask Matrix ($M$)

For any sequence processed by the model, the causal mask matrix is generated using a lower-triangular structure. It places 0 where attention is allowed (past and present) and $-\infty$ where it must be blocked (future). [1]
For Sentence 1 (or any sequence padded to 10 tokens), the matrix $M$ is structurally created as follows:
$$M = \begin{pmatrix} 0 & -\infty & -\infty & -\infty & -\infty & -\infty & -\infty & -\infty & -\infty & -\infty \\ 0 & 0 & -\infty & -\infty & -\infty & -\infty & -\infty & -\infty & -\infty & -\infty \\ 0 & 0 & 0 & -\infty & -\infty & -\infty & -\infty & -\infty & -\infty & -\infty \\ 0 & 0 & 0 & 0 & -\infty & -\infty & -\infty & -\infty & -\infty & -\infty \\ 0 & 0 & 0 & 0 & 0 & -\infty & -\infty & -\infty & -\infty & -\infty \\ 0 & 0 & 0 & 0 & 0 & 0 & -\infty & -\infty & -\infty & -\infty \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & -\infty & -\infty & -\infty \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -\infty & -\infty \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & -\infty \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \end{pmatrix}$$

Step 3: Application to Your Sentences

To visualize exactly how this affects the text, we map the tokens of your sentences against the matrix rows (Queries) and columns (Keys).

Sentence 1 Matrix Map (10 Tokens)

Every word can only look at itself and words to its left.

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)

When a sentence is shorter, modern frameworks typically generate a causal mask for the active tokens and merge it with a Padding Mask to block the filler space completely.
For Sentence 4, the combined mask looks like this:

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$
(Note: Notice how columns 7–10 are entirely $-\infty$ because they represent future padding, and rows 7–10 are entirely $-\infty$ because padding tokens should not calculate outgoing predictions).
 

Loading