AI Glossary / Generative AI and language / Intermediate

Attention mask

Definition

An attention mask specifies which positions may participate in an attention calculation. It can prevent a model from attending to padding tokens, future tokens or other disallowed positions. The mask controls information flow within the attention operation; it is distinct from the learned attention weights.

What it means

Models often process several sequences together, even when their lengths differ. Padding makes their arrays the same length, but those extra positions should not be treated as ordinary content. A padding mask identifies positions that attention should ignore.

Causal language models need another restriction: a prediction must not obtain information from later tokens that would be unavailable at generation time. A causal mask enforces the allowed direction of attention during sequence processing.

These restrictions can be combined. The precise representation depends on the interface, including whether one or true means allowed or blocked. A mask must be interpreted through the receiving function's documented convention, not its appearance alone.

How it works

Attention normally calculates compatibility scores between query and key positions. A common masking implementation adds a prohibitive negative value to disallowed scores before softmax. Those positions then receive zero attention weight in the idealized calculation, and the allowed positions are normalized together.

A padding mask often has one indicator per sequence position and is expanded internally. A causal mask describes allowed query-key pairs, commonly forming a triangular pattern for an ordinary sequence. More specialized patterns can restrict attention to local windows or separate packed examples.

Masks must align with the actual positions, including any padding, cached tokens and differences between query and key lengths. A correctly shaped but misaligned mask can silently enforce the wrong information boundary.

Masking keys does not necessarily set every output at padded query positions to zero. Loss calculation and pooling may need their own masks. An attention mask also differs from replacing a word with a special mask token during masked-language-model training.

Three practical examples

  • A text classifier uses a padding mask when short and long documents share a processing batch.
  • A next-token model uses causal masking during training so earlier positions cannot read later target information.
  • A training system that packs separate examples into one sequence can use a suitable mask to prevent unintended attention across example boundaries.

Think of it this way

Think of giving a student selected pages for an exercise and covering pages they are not permitted to consult. The remaining pages can still receive different amounts of attention.

The analogy concerns allowed information paths. A software mask is not a general security or privacy boundary, and covering one path does not establish that information is absent from every other part of a larger system.

Worked example

Padding and causality answer different questions

Consider a teaching batch row with positions [A, B, C, PAD, PAD]. Under a convention where one means available content, its padding mask is [1, 1, 1, 0, 0].

In bidirectional self-attention, a query at B may consult A, B and C. In ordinary causal self-attention, that same position may consult A and B but not C. Both versions exclude the padding positions, but only one excludes the future content position.

Suppose the allowed A and B scores are equal. After normalization their weights are 0.5 and 0.5. The blocked positions receive no contribution to that weighted sum. The binary mask does not prescribe a weight of one for A and B; it defines eligibility before learned scores determine the actual weights.

Illustrative case study

A fictional document service, Alder Forms, moves its attention code to a different software interface. The old padding mask uses true for positions to ignore. The new attention function interprets true as positions allowed to participate.

The arrays still have compatible dimensions, so the integration runs, but short documents produce unreliable outputs. A small test with known content and padding reveals that the intended rule has been reversed.

The team converts the convention explicitly and checks attention boundaries on short, long and padded examples. It also verifies that its pooling step excludes padding separately. The fictional case demonstrates why successful execution and matching array shapes are insufficient evidence of correct masking.

Common misconceptions and mistakes

An attention mask is not an attention map. The former restricts eligibility; the latter typically displays computed weights after scoring and normalization.

Do not assume every library uses the same binary convention. For example, documented PyTorch interfaces distinguish allowed-position masks from key-padding masks with opposite boolean meanings.

Do not assume padding tokens are harmless just because their input identifier is zero or their initial embedding is zero. They can still affect normalization or other computations without correct handling. Also handle queries with no allowed keys explicitly, since ordinary softmax over entirely blocked positions can create numerical problems.

Business use

For most users, masking is an implementation detail, but mistakes can invalidate model evaluation or cause unpredictable batching behavior. Include representative boundary cases when accepting a model integration.

The same logical input processed alone and in a padded batch should behave consistently within the intended numerical and stochastic settings. For causal models, changes to future tokens should not affect earlier-position outputs when the full information-flow design is correct. These checks connect the mask's intended rule with observable system behavior.

People also ask

Is an attention mask always optional?

An interface may supply defaults, but correct padding and causal behavior still requires the appropriate restriction somewhere in the computation.

Does a one in a mask mean attention weight one?

No. Under an allowed-position convention it permits participation; the learned scores determine the subsequent weight.

Is a mask token the same as an attention mask?

No. A mask token is an input symbol used in some training tasks. An attention mask controls which positions can exchange information through attention.

Check your understanding

A sequence contains three real tokens and two padding positions. Does excluding padding alone prevent the first token from attending to the third?

Answer: No. A padding mask excludes the padding positions. A causal or other pairwise restriction is needed to block that future-token connection.

Related terms

  • Attention
  • Self-attention
  • Softmax
  • Masked language modeling
  • padding
  • causal language model
  • key
  • query
  • loss mask
  • sequence packing

Learn next

Draw a small query-by-key grid and mark allowed connections for bidirectional and causal attention. Add padding and combine the restrictions. Then check the exact mask convention of one implementation and test a sequence both alone and in a padded batch.

  • Attention

More in Generative AI and language

  • Adaptive Synthetic Sampling - Adaptive Synthetic Sampling, usually abbreviated ADASYN, is an oversampling method for imbalanced classification.
  • AI slop - AI slop is an informal, critical term for low-quality AI-generated content produced with an emphasis on quantity rather than usefulness, accuracy or...
  • Attention head - An attention head is one attention computation within a multi-head attention mechanism.
  • Attribute sampling - Attribute sampling is a decision-tree training technique that randomly selects a subset of input features to consider when choosing a split.
  • Autoencoder - An autoencoder is a neural network trained to reconstruct an input from an internal representation of that input.
  • Auxiliary Classifier GAN - An Auxiliary Classifier GAN, or AC-GAN, is a class-conditional generative adversarial network whose discriminator predicts both whether an example is real...

Sources

Sources: Hugging Face Transformers glossary, attention mask: https://huggingface.co/docs/transformers/main/en/glossary#attention-mask Vaswani and colleagues, Transformer decoder masking: https://arxiv.org/abs/1706.03762 PyTorch, scaled dot-product attention mask semantics: https://docs.pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html PyTorch, MultiheadAttention and key-padding masks: https://docs.pytorch.org/docs/stable/generated/torch.nn.MultiheadAttention.html

Previous: Attention head Next: Attenuation

  • Home
  • Daily AI Updates
  • Blog
  • AIFBL Academy
  • AI Glossary
  • Products and Tools
  • Store
  • Free CFO AI Prompt Pack
  • About Shihan Sheriff
  • Privacy Policy
  • Terms of Use
  • Refund Policy
  • Cookie Policy
  • Earnings Disclaimer