---
title: "Same Boolean Mask, Opposite Attention Membership"
description: "Leviathan Systems — Technical content Published 2026-10-21; updated 2026-10-21."
canonical: "https://www.leviathansystems.co/articles/pytorch-attention-mask-boolean-membership-contract"
last-updated: "2026-10-21"
---

# Same Boolean Mask, Opposite Attention Membership

Leviathan Systems — Technical content
Published 2026-10-21; updated 2026-10-21.

In PyTorch 2.14, a Boolean True in MultiheadAttention's attn_mask excludes a query-key position; in scaled_dot_product_attention's attn_mask, it permits participation. An unchanged Boolean array can therefore select the opposite keys. In fictional MASK-30, the MHA mask [[False, False, True]] permits K0 and K1 and produces 4. The same bits in SDPA permit only K2 and produce 100. Assess the named API, mask role, dtype, axes and resulting participating set before accepting a claim that an attention implementation preserves the required behavior.

## Specify which keys may contribute to each query

An attention handoff needs a statement about participation: for this query, which key/value positions may influence the result? The word mask and a correctly shaped array leave that statement incomplete. A Boolean entry can mean permission or exclusion, depending on the receiving interface.

The PyTorch 2.14 references, checked October 21, 2026, assign opposite meanings to True in the two attn_mask parameters. MultiheadAttention, abbreviated MHA, disallows the corresponding query-key position. Functional scaled_dot_product_attention, abbreviated SDPA, allows it. MHA's separate Boolean key_padding_mask also uses True to ignore a key, but has a different role and shape.

Begin a proposal comparison with the required participating set and the parameter that interprets the array. This separates the application's intended dependency from its Boolean encoding.

References: [MHA1](https://docs.pytorch.org/docs/2.14/generated/torch.nn.MultiheadAttention.html), [SDPA1](https://docs.pytorch.org/docs/2.14/generated/torch.nn.functional.scaled_dot_product_attention.html)

## MASK-30 fixes one query, three keys and the required set

Fictional MASK-30 has one head, one query and three keys, ordered K0, K1, K2. Their corresponding scalar values are [2, 6, 100]. The already-projected, scaled query-key score row is [0, 0, 0]. The requirement is that K0 and K1 participate and K2 be excluded.

Dropout is zero. There is no causal mask, additional score bias, second mask or added key/value position. Every participating set considered below is nonempty. These are exact real-number calculations from stipulated inputs, with no PyTorch execution or model result claimed.

For a permitted set A, each key's weight is its exponentiated score divided by the sum of exponentiated scores over A. Excluded positions contribute no weight. The output is the sum of each value multiplied by its weight. Equal zero scores therefore give equal weights among the permitted keys.

The calculation concerns the attention core after the Q/K projections. Any interpretation as a complete MHA module additionally stipulates identity value and output projections and zero projection biases, with the Q/K projections supplying the stated score row. These premises isolate membership rather than equate arbitrary complete modules.

References: [SDPA1](https://docs.pytorch.org/docs/2.14/generated/torch.nn.functional.scaled_dot_product_attention.html), [MHA1](https://docs.pytorch.org/docs/2.14/generated/torch.nn.MultiheadAttention.html)

## The MHA Boolean mask yields weights [1/2, 1/2, 0]

Interpret [[False, False, True]] as MHA's Boolean attn_mask. Its one row belongs to the query; the three columns correspond to K0, K1 and K2. False leaves the first two positions available, while True excludes the third. The participating set is exactly the required {K0, K1}.

Each permitted score is zero, so each exponential is exp(0) = 1. The normalization denominator is 1 + 1 = 2. In key order, the weights are [1/2, 1/2, 0].

The weighted value is (1/2 × 2) + (1/2 × 6) + (0 × 100) = 4. The third value is large, but it has no contribution under the stipulated exclusion. Its presence in the supplied value array does not make it a member of this query's attention calculation.

References: [MHA1](https://docs.pytorch.org/docs/2.14/generated/torch.nn.MultiheadAttention.html), [SDPA1](https://docs.pytorch.org/docs/2.14/generated/torch.nn.functional.scaled_dot_product_attention.html)

## The same bits in SDPA yield weights [0, 0, 1]

Now interpret that identical Boolean array as SDPA's attn_mask. True permits participation, so the first two positions are excluded and K2 alone remains. The allowed set becomes {K2}.

Its sole permitted score is zero. The denominator is exp(0) = 1, producing weights [0, 0, 1] and output 100. The copied array has preserved every bit while reversing which keys contribute.

Both masks have shape 1 × 3. Suppressing batch and head dimensions, both attention-core outputs have shape 1 × 1: one query and one scalar value feature. Both weight rows sum to one, and both outputs lie in the supplied values' interval [2, 100]. Shape, normalized weights and this coarse output-range check cannot establish the required membership.

Under the same premises, the SDPA Boolean array [[True, True, False]] describes {K0, K1}. It gives weights [1/2, 1/2, 0] and output 4. This is the semantic match for MASK-30: different Boolean entries represent the same required participating set across the two APIs.

References: [SDPA1](https://docs.pytorch.org/docs/2.14/generated/torch.nn.functional.scaled_dot_product_attention.html), [MHA1](https://docs.pytorch.org/docs/2.14/generated/torch.nn.MultiheadAttention.html)

## A float array [0, 0, 1] introduces a third calculation

Consider a proposal that turns the MHA Boolean entries into floating-point numbers and retains [0, 0, 1] as the mask row. The documented floating-point attn_mask treatment is additive score bias. The entries now modify scores instead of expressing Boolean exclusion.

With MASK-30's original zero scores, the biased row becomes [0, 0, 1]. Its exponentials are [1, 1, e], where e = exp(1), and its denominator is 2 + e. The weights are [1/(2 + e), 1/(2 + e), e/(2 + e)]. All three positions have positive weight in this exact calculation.

The output is (2 + 6 + 100e)/(2 + e) = (8 + 100e)/(2 + e), approximately 59.31. It differs from both 4 and 100. The dtype change has introduced a score preference for K2 while leaving K0 and K1 involved.

Hard exclusion can be expressed mathematically by a negative-infinity score bias, whose exponential contribution is zero. An arbitrary finite bias is a different contract. In particular, converting True to the finite number 1 supplies neither MHA's exclusion meaning nor SDPA's Boolean membership meaning.

References: [SDPA1](https://docs.pytorch.org/docs/2.14/generated/torch.nn.functional.scaled_dot_product_attention.html), [MHA1](https://docs.pytorch.org/docs/2.14/generated/torch.nn.MultiheadAttention.html)

## Interpret shape together with the mask's role and axes

The 2D MHA attn_mask shape is (L, S), with target/query length L and source/key length S. Its documented 3D alternative is (N × number of heads, L, S), where N is batch size. By contrast, MHA's batched key_padding_mask has shape (N, S); its first axis identifies a batch entry. A first-axis length of one would not by itself tell a reader whether it identifies one query or one batch entry.

SDPA requires attn_mask to be broadcastable to attention weights shaped (N, ..., Hq, L, S), where Hq counts query heads. Broadcasting describes where entries apply. Boolean polarity describes whether those entries permit participation. A valid broadcast does not settle that meaning.

A useful specification names the API/version and parameter, dtype, axis meanings and key order. It then states the effective participating keys for each relevant query, batch entry and head. MASK-30 makes these fields visible even though it has only one query and one head; larger dimensions make those distinctions more consequential.

References: [MHA1](https://docs.pytorch.org/docs/2.14/generated/torch.nn.MultiheadAttention.html), [SDPA1](https://docs.pytorch.org/docs/2.14/generated/torch.nn.functional.scaled_dot_product_attention.html)

## Choose the offer that accounts for the required participating set

Suppose fictional Offer A says that it preserves the MHA mask unchanged when supplying SDPA. Offer B identifies SDPA's Boolean meaning and specifies [[True, True, False]] for the same ordered keys. For MASK-30's requirement, A selects the wrong set and yields 100; B describes the required set and yields 4 under the fixed premises.

B's useful contribution is an explicit connection from the requirement to the receiving API's meaning, the participating keys, the normalized weights and the resulting value. A statement that the array is unchanged, the dimensions match or the output looks numerically ordinary cannot supply that connection. Likewise, the float-bias proposal needs assessment as a different calculation with all three keys involved.

For an actual implementation, ask the appointed software owner for evidence that the delivered system implements its stated contract, alongside the relevant projection and other attention settings. MASK-30 supplies a mathematical comparison, not implementation acceptance. Its nonempty sets and absent causal interaction leave all-masked rows and combined masking behavior outside this conclusion.

The related FlashAttention guide addresses attention workspace; the GQA guide addresses retained key/value heads; the padding guide addresses input layout and request boundaries. Those mechanisms can coexist with a mask, but their counts do not identify its allowed keys. Membership must be correct before a separate kernel, memory or speed comparison can answer whether the offered implementation is useful.

References: [MHA1](https://docs.pytorch.org/docs/2.14/generated/torch.nn.MultiheadAttention.html), [SDPA1](https://docs.pytorch.org/docs/2.14/generated/torch.nn.functional.scaled_dot_product_attention.html)

## Connect a separately needed equipment project to installation

If the selected application also requires physical equipment installation, Leviathan's rack-and-stack service covers agreed equipment mounting, IT cabling, labels and physical installation records. Use the related service and project inquiry with a nonconfidential equipment list, site and released rack/cable package to discuss scope and availability. Attention implementation and application acceptance remain with the appointed software team.

References: [LS1](https://www.leviathansystems.co/services/rack-and-stack)

## Primary sources

- [SDPA1 — PyTorch 2.14 — torch.nn.functional.scaled_dot_product_attention](https://docs.pytorch.org/docs/2.14/generated/torch.nn.functional.scaled_dot_product_attention.html). Checked 2026-10-21. Boolean participation, additive float-mask semantics, query/key/value formulation and broadcastable mask/output shapes. The reference marks the API beta. MASK-30 uses zero dropout and no causal interaction; its arithmetic is not an executed-backend result.
- [MHA1 — PyTorch 2.14 — torch.nn.MultiheadAttention](https://docs.pytorch.org/docs/2.14/generated/torch.nn.MultiheadAttention.html). Checked 2026-10-21. Forward parameter definitions establish Boolean attn_mask exclusion, the separate key_padding_mask role and their shapes. The projection formulation bounds comparisons between the attention core and a complete module.
- [LS1 — Leviathan Systems — Rack & Stack / Colocation](https://www.leviathansystems.co/services/rack-and-stack). Checked 2026-10-21. Agreed physical mounting, IT cabling, labels and installation records support a separately identified equipment installation inquiry.

## Related Leviathan resources

- [FlashAttention workspace and dense attention arithmetic](/articles/flashattention-tiling-attention-score-memory)
- [Query heads and retained key/value heads](/articles/grouped-query-attention-kv-head-cache-population)
- [Input padding, ragged layouts and request boundaries](/articles/inference-padding-ragged-batching-useful-token-positions)
- [Rack-and-stack installation scope](/services/rack-and-stack)
- [Discuss a separate physical equipment installation](/contact?service=rack-and-stack&from=%2Farticles%2Fpytorch-attention-mask-boolean-membership-contract)

## Does selected equipment need physical installation?

Share a nonconfidential equipment list, site and released rack/cable package to discuss the physical installation scope and availability.

[Discuss the scope](/contact?service=rack-and-stack&from=/articles/pytorch-attention-mask-boolean-membership-contract)
