Decoder Architecture › Visualizing Causal Attention Patterns
Language AI HandbookAll chapters Decoder Architecture › Visualizing Causal Attention PatternsOne section · Sections in this chapter8 / 16 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Each attention head in a decoder develops specialized patterns. Some heads attend strongly to the previous token (local context). Others attend to the beginning of the sequence. Still others develop positional patterns or semantic groupings.
Let's examine attention patterns in a decoder block.
def extract_attention_weights(block, x):
"""
Extract attention weights from a decoder block.
Returns attention weights of shape (n_heads, seq_len, seq_len).
"""
attn = block.attention
seq_len = x.shape[0]
# Replicate attention computation to capture weights
normed = rms_norm(x, block.norm1_gamma)
Q = normed @ attn.W_q
K = normed @ attn.W_k
Q = Q.reshape(seq_len, attn.n_heads, attn.d_k).transpose(1, 0, 2)
K = K.reshape(seq_len, attn.n_heads, attn.d_k).transpose(1, 0, 2)
scores = Q @ K.transpose(0, 2, 1) / np.sqrt(attn.d_k)
scores = scores + create_causal_mask(seq_len)
weights = softmax(scores, axis=-1)
return weights
# Extract attention from first block
x_test = np.random.randn(8, 256) * 0.02
attn_weights = extract_attention_weights(model.blocks[0], x_test)

Both heads exhibit the causal structure with zeros in the upper triangle. The intensity patterns within the lower triangle vary between heads, showing how different heads attend to different parts of the available context. With random initialization, these patterns are not yet meaningful, but training shapes them into useful attention strategies.