Skip to content

Attention Mask

A mechanism in the transformer that restricts which tokens the model can look at when calculating attention. Most commonly, it prevents the model from "peeking into the future".

Why is it needed

During generation, the model needs to predict the next word without knowing it. The mask hides future tokens, leaving only previous tokens visible. This is called a causal mask.

How it works

In the attention matrix of "token × token", the mask zeroes out or makes infinitesimally small the weights of forbidden positions. After softmax, they give zero probability — the model ignores them.

Other applications

  • sequence alignment of different lengths (padding mask);
  • prohibition of certain positions in special architectures.

See also

← All terms