liveBook · Manning

Masked attention

As explained in Transformers in Action

Causal masking restricts a sequence position from attending to later positions so predictions depend only on preceding context.

Masked attention, also called causal attention, prevents a sequence position from attending to future tokens. A decoder can therefore predict using only context available at that position. In autoregressive generation, each new token is fed back into the input stream to help predict the next one. section 3.2

The constraint distinguishes decoder self-attention from unrestricted attention over a sequence. In an encoder–decoder transformer, the decoder also attends to the encoder’s output, so it can use input-text context while generating output in order. A decoder-only model instead relies on its prior outputs. section 2.2.1 section 3.2

Which positions are available?

When predicting from the second position, a later third position must be unavailable. Once generation reaches the third position, the earlier outputs can provide context. This is the restriction behind next-token prediction rather than a rule that removes earlier context. section 2.2.1 section 3.2

Listing 3.2 Masked multihead self-attention in decoder-only models
def attention_mask(nd, ns, *, dtype):

    i = tf.range(nd)[:, None]   #1
    j = tf.range(ns)
    m = i >= j - ns + nd
    return tf.cast(m, dtype)

def mask_attn_weights(w):

    _, nd, ns = shape_list(w)   #2

    b = attention_mask(nd, ns, dtype=w.dtype)   #3

    b = tf.reshape(b, [1, 1, nd, ns])   #4

    w = w + b * -tf.cast(1e10, w.dtype) * (1 - b)  #5
    return w

The TensorFlow listing builds a position mask, reshapes it for attention weights, and attempts to suppress disallowed weights. Check the final update carefully: for a binary mask b, its factor b × (1 − b) is always zero, so the expression as printed does not suppress any weights. section 3.2

How Transformers in Action covers it

How deep it goes
Defines the causal restriction and shows mask construction and an attention-weight update, but stops short of a verified working implementation.
Examples
Uses an attention-masking figure and a TensorFlow listing that constructs a position mask for decoder-only self-attention.
What it assumes
Assumes basic familiarity with tokens and attention while defining autoregressive prediction from prior outputs.
What you can do after
Diagnose/debug with it by checking whether an attention-weight update actually suppresses access to future positions.
How it connects
Links masked self-attention to decoder-only transformers, encoder–decoder transformers, autoregressive generation, and next-token prediction.

Also explained in

Compare how 6 books explain masked attention →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Transformers in Action. The section links point to the parts of the book it draws on.