In many large model tasks, you usually want the self-attention mechanism to look only at tokens that appear before the current position when predicting the next token in a sequence.
Causal attention is also called masked attention.
When computing attention scores, it restricts the model to considering only the previous input and the current input in the sequence. Compared with the previous section, the standard attention mechanism lets the model access the entire sequence at once.

CleanShot 2024-10-30 at 12 .13.12@2x.png
In the example above, the text after the current input in the sequence is masked out, and the result is then normalized.
First: Apply a Causal Attention Mask

CleanShot 2024-10-30 at 12 .15.27@2x.png
queries = sa_v2.W_query(inputs) #1
keys = sa_v2.W_key(inputs)
attn_scores = queries @ keys.T
attn_weights = torch.softmax(attn_scores / keys.shape[-1]**0.5, dim=-1)
print(attn_weights)
## result
tensor([[0.1921, 0.1646, 0.1652, 0.1550, 0.1721, 0.1510],
[0.2041, 0.1659, 0.1662, 0.1496, 0.1665, 0.1477],
[0.2036, 0.1659, 0.1662, 0.1498, 0.1664, 0.1480],
[0.1869, 0.1667, 0.1668, 0.1571, 0.1661, 0.1564],
[0.1830, 0.1669, 0.1670, 0.1588, 0.1658, 0.1585],
[0.1935, 0.1663, 0.1666, 0.1542, 0.1666, 0.1529]], grad_fn=<SoftmaxBackward0>)
The code above follows the earlier process for computing attention scores in the standard attention mechanism. Here, you can use PyTorch’s tril method to create a mask.
context_length = attn_scores.shape[0]
mask_simple = torch.tril(torch.ones(context_length, context_length)) print(mask_simple)
## result
tensor([[1., 0., 0., 0., 0., 0.],
[1., 1., 0., 0., 0., 0.],
[1., 1., 1., 0., 0., 0.],
[1., 1., 1., 1., 0., 0.],
[1., 1., 1., 1., 1., 0.],
[1., 1., 1., 1., 1., 1.]])
masked_simple = attn_weights*mask_simple
print(masked_simple)
## result
tensor([[0.1921, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
[0.2041, 0.1659, 0.0000, 0.0000, 0.0000, 0.0000],
[0.2036, 0.1659, 0.1662, 0.0000, 0.0000, 0.0000],
[0.1869, 0.1667, 0.1668, 0.1571, 0.0000, 0.0000],
[0.1830, 0.1669, 0.1670, 0.1588, 0.1658, 0.0000],
[0.1935, 0.1663, 0.1666, 0.1542, 0.1666, 0.1529]], grad_fn=<MulBackward0>)
Then renormalize it.
row_sums = masked_simple.sum(dim=-1, keepdim=True)
masked_simple_norm = masked_simple / row_sums print(masked_simple_norm)
## result
tensor([[1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
[0.5517, 0.4483, 0.0000, 0.0000, 0.0000, 0.0000],
[0.3800, 0.3097, 0.3103, 0.0000, 0.0000, 0.0000],
[0.2758, 0.2460, 0.2462, 0.2319, 0.0000, 0.0000],
[0.2175, 0.1983, 0.1984, 0.1888, 0.1971, 0.0000],
[0.1935, 0.1663, 0.1666, 0.1542, 0.1666, 0.1529]], grad_fn=<DivBackward0>)
Improving the Mask Step: Optimizing the Process Above
Using a mathematical property of the softmax function, you can compute the masked attention weights more efficiently and in fewer steps.
CleanShot 2024-10-30 at 12 .24.54@2x.png
The softmax function turns its input into a probability distribution. When a row contains negative infinity (-∞), softmax treats it as zero probability. (Mathematically, this is because e^–∞ approaches 0.)
mask = torch.triu(torch.ones(context_length, context_length), diagonal=1)
masked = attn_scores.masked_fill(mask.bool(), -torch.inf) print(masked)
## result
tensor([[0.2899, -inf, -inf, -inf, -inf, -inf],
[0.4656, 0.1723, -inf, -inf, -inf, -inf],
[0.4594, 0.1703, 0.1731, -inf, -inf, -inf],
[0.2642, 0.1024, 0.1036, 0.0186, -inf, -inf],
[0.2183, 0.0874, 0.0882, 0.0177, 0.0786, -inf],
[0.3408, 0.1270, 0.1290, 0.0198, 0.1290, 0.0078]], grad_fn=<MaskedFillBackward0>)
attn_weights = torch.softmax(masked / keys.shape[-1]**0.5, dim=1) print(attn_weights)
## result
tensor([[1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
[0.5517, 0.4483, 0.0000, 0.0000, 0.0000, 0.0000],
[0.3800, 0.3097, 0.3103, 0.0000, 0.0000, 0.0000],
[0.2758, 0.2460, 0.2462, 0.2319, 0.0000, 0.0000],
[0.2175, 0.1983, 0.1984, 0.1888, 0.1971, 0.0000],
[0.1935, 0.1663, 0.1666, 0.1542, 0.1666, 0.1529]], grad_fn=<SoftmaxBackward0>)
A small adjustment to the self-attention mechanism that is very useful for reducing overfitting when training large language models.
Second: Masking Additional Attention Weights with Dropout
Dropout in deep learning is a technique in which randomly selected hidden-layer units are ignored during training—this is effectively “dropping” them. Its main purpose is to prevent overfitting.
When building an LLM, dropout is used twice.
- After computing the attention weights
- After applying the attention weights to the vectors

Applying a dropout mask to the attention mask
Code
Where Are We?

Here’s what we’ve done so far.
We started with a simplified attention mechanism, added trainable weights, and then added a causal attention mask. Next, we’ll extend the causal attention mechanism and implement multi-head attention, which will be used in our LLM.