Skip to main content

Why the same attention mask let 2 through in one function and 8 in another

Two numbers sit side by side, 2 and 8. Beside 2 a programmer writes ‘true.’ Beside 8 they write ‘false.’ The marks say which number may enter a calculation, but which mark means permission? In PyTorch the answer depends on the function.

Two PyTorch functions read those marks in opposite ways. In our small calculation only one attention position could contribute. The first function blocked 2 and returned 8. The second allowed 2 and returned 2. To get 8 from the second function as well, we had to reverse the marks. Switching functions without changing the marks could let a model use the very position a programmer meant to block.

The calculation is called attention. A model can combine information from several positions in its input, giving each position a weight. A program can also exclude a position altogether. A list of true/false marks that controls these choices is a Boolean mask.

We tried two PyTorch 2.9.1 functions on a CPU: torch.nn.MultiheadAttention and torch.nn.functional.scaled_dot_product_attention, or SDPA. Both received the same numbers. Test script · Recorded results

Which value does each ‘true’ allow?

Our calculation asked one question, called a query. Each of the two input positions had a key to compare with the query, and a value that might contribute to the answer. The values available here were 2 and 8. Both query–key scores were zero, so without a mask they had equal weight and their average was 5. The mask had one row and two marks, a shape of 1 × 2.

With [True, False], MultiheadAttention blocked the first position. Only the second value, 8, was left. With the same mask, SDPA allowed the first position. Only 2 was left. Giving SDPA [False, True] instead meant it allowed the second position and returned 8. The table follows the same choices in both functions. None means no mask was supplied. MultiheadAttention documentation · SDPA documentation

What each function returned for the same marks, reversed marks and no marks
ComparisonMultiheadAttention maskSDPA maskMultiheadAttention outputSDPA output
Same mask[True, False][True, False]82
Inverted SDPA mask[True, False][False, True]88
UnmaskedNoneNone55
Same mask with entries swapped[False, True][False, True]28

With no mask, both query–key scores were zero, so both positions got the same weight. Both functions returned the average, (2 + 8) / 2 = 5. Allow only one position and its number supplies the whole output. Swapping the marks switched which number each function returned. The MultiheadAttention results stayed the same when we requested only the output with need_weights=False.

Keeping the same positions when switching functions

  1. Start with one query and two positions whose scores are equal but whose values differ. Decide which position should be available. With only that position allowed, you know what number should come out.
  2. For programmers switching functions, first check that each mark lines up with the right score. A two-dimensional query-by-key mask may already fit both functions. MultiheadAttention can also use a separate mask for each head, the part doing one attention calculation. Such a mask can have shape (batch × heads, queries, keys). SDPA must be able to expand its mask to (batch, ..., heads, queries, keys). If your SDPA inputs separate batch and head dimensions, reshape the mask to fit. Once the shapes match, ~mha_attn_mask flips each Boolean mark from true to false or false to true. That preserves the positions the query may use.
  3. Check the two masked results against the expected number. Remove the masks and check that the functions agree then too. A module can transform its queries, keys, values and final output, so matching the masks alone needn’t make arbitrary outputs identical. This reversal applies to the Boolean attn_mask. Account separately for masks that hide padding or prevent looking ahead. A floating-point mask has a different job: it adds numerical adjustments to the attention scores.

Reproduce the comparison

The replay uses Python 3.13.17 and PyTorch 2.9.1+cpu, with one head on CPU and float32 inputs. The module uses identity projections, which leave these test values unchanged, no bias and zero dropout. Each tested mask leaves at least one position available. The test doesn’t cover a row with every position blocked.

Run python replay.py in that environment and compare the four cases with the saved JSON. Pinned PyTorch source · Replay script · Recorded outputs