liveBook · Manning

Attention_mask

As explained in Build a Text-to-Image Generator (from Scratch)

A tensor or pattern that hides selected sequence positions from a model during processing or prediction.

An attention mask is a tensor or pattern that controls which sequence positions a model may use. Padding masks hide filler tokens; causal masks hide future tokens during prediction. section 2.2.2 section 8.2.2

Apply the mask at the stage where irrelevant positions would otherwise affect attention:

  • Pad variable-length sequences to a common batch length, then mark real tokens as active and padding positions as inactive. section 8.2.2
  • Build a source mask such as (src != pad), adding dimensions so attention can broadcast it across the sequence. section 2.2.2
  • Build a decoder target mask that combines padding protection with a causal pattern, preventing each position from seeing subsequent tokens. section 2.2.2 section 2.3.2
  • Pass the mask into attention while generating; the decoder then predicts one token at a time without incorporating padding or disallowed future positions. section 12.3.2

Two padded captions

For ['[CLS]', 'two', 'dogs', 'run', '[SEP]', '[PAD]', '[PAD]', '[PAD]'], the mask is [1,1,1,1,1,0,0,0]. A caption without padding receives all ones, so every position remains visible. section 8.2.2

attention_mask=text_tokens.not_equal(1)[:, None, None, :]    #1
attention_state=torch.zeros(size=(24,4,256,2048),    #2
    device=device)
image_tokens=torch.full((1, 256 + 1),    #3
    2 ** 14 - 1,dtype=torch.long,device=device)

Inspect the compact PyTorch construction that compares text tokens with the padding index and reshapes the result for decoder attention. section 12.3.2

How Build a Text-to-Image Generator (from Scratch) covers it

How deep it goes
Implements binary padding masks, broadcast dimensions, and causal decoder masking, but does not detail attention-score masking internals.
Examples
Uses padded captions, German-English batches, decoder targets, and 256-token image generation.
What it assumes
Assumes tokenized sequences and padding indices, while demonstrating batch preparation and decoder visibility constraints.
What you can do after
Implement it with PyTorch tensors to exclude padding and prevent future-token visibility during decoding.
How it connects
Connects tokenization, padding, causal self-attention, encoder-decoder translation, and autoregressive generation.

Also explained in

Compare how 2 books explain attention_mask →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Text-to-Image Generator (from Scratch). The section links point to the parts of the book it draws on.