Attention Masking › Key Parameters

Michael BrenndoerferUpdated May 27, 20251 min read
Language AI HandbookAll chapters
Part 13 of 67Self-Attention
Chapter 93Attention Masking › Key Parameters
Reading section 11 of 13 · Key Parameters · 1 minSection 11 of 13 · 1 min

Book contents

Section 11 of 13 · 1 min read

Attention Masking › Key ParametersOne section · Sections in this chapter11 / 13 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

When implementing attention masking, several parameters control the mask behavior:

  • mask_value: The large negative value used for masked positions (typically −109-10^9 or -float('inf')). Using −109-10^9 rather than true infinity avoids numerical issues with some operations while still producing near-zero attention weights after softmax. If every position in a row is masked, using −∞-\infty produces NaN via 0/00/0 in softmax, while −109-10^9 produces a uniform distribution that is mathematically wrong but numerically stable.

  • window_size (local attention): Controls how many positions on each side a token can attend to. Smaller windows reduce computation but may miss important long-range dependencies. Common values range from 128 to 512 tokens in production systems.

  • stride (strided attention): Determines the spacing between attended positions in sparse patterns. A stride of kk means attending to every kk-th position, reducing complexity while maintaining some global connectivity.

  • pad_token_id: The token ID used for padding in tokenized sequences. This must match the padding token used during tokenization to correctly identify positions to mask.