Decoder Architecture › Visualizing Causal Attention Patterns

Michael BrenndoerferUpdated June 19, 20252 min read
Language AI HandbookAll chapters
Part 16 of 67Transformer Architectures
Chapter 112Decoder Architecture › Visualizing Causal Attention Patterns
Reading section 8 of 16 · Visualizing Causal Attention Patterns · 2 minSection 8 of 16 · 2 min

Book contents

Section 8 of 16 · 2 min read

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.

In[19]:
Code
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)
Out[20]:
Visualization
Lower triangular heatmap showing attention weights for head 0 with varying intensity.
Head 0 attention pattern. This head shows a strong diagonal pattern, focusing primarily on recent positions with causal masking creating the characteristic lower-triangular structure.
Lower triangular heatmap showing attention weights for head 1 with different pattern from head 0.
Head 1 attention pattern. Different heads learn different attention strategies. This head may focus on different positional relationships or semantic patterns.

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.