liveBook · Manning

Masked attention

As explained in Machine Learning Algorithms in Depth

Masked attention restricts attention connections so that a position cannot use information from disallowed positions, such as future tokens.

Masked attention restricts attention connections so a decoder position cannot use disallowed information, especially future output tokens. It preserves causality during one-token-at-a-time generation. section 11.3

The masking mechanism operates as follows:

  • Construct a mask matrix M containing zeros for permitted connections and negative infinity for prohibited connections. section 11.3
  • Add M to the attention scores before applying softmax. section 11.3
  • Let softmax preserve permitted connections while converting masked negative-infinity scores to zero, removing those connections from the output. section 11.3

Autoregressive translation

While generating a translated sentence, the decoder masks tokens that have not yet been generated, preventing later output words from influencing earlier predictions. section 11.3

Figure 11.10 Transformer architecture
figure

Use the transformer architecture figure to locate masked multihead self-attention inside the decoder. section 11.3

How Machine Learning Algorithms in Depth covers it

How deep it goes
Defines the mask values and explains their softmax effect, but does not derive attention scores or implement the layer from scratch.
Examples
Uses autoregressive machine translation, where output tokens are generated one at a time.
What it assumes
Assumes familiarity with multihead self-attention, decoder layers, query-key-value attention, and softmax.
What you can do after
Understand how negative-infinity masking prevents future-token information from affecting decoder predictions.
How it connects
Connects masked attention with decoders, multihead self-attention, softmax, and encoder-decoder attention.

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 Machine Learning Algorithms in Depth. The section links point to the parts of the book it draws on.