TECHNOLOGY

Broadcasting an attention mask: align meanings before shapes

In this article
THE QUICK TAKE

Broadcasting reuses values along compatible dimensions; correct attention masking additionally requires an explicit query-key axis interpretation.

Broadcasting saves repetitive tensor construction by allowing compatible arrays to participate in a larger operation. Attention masks make the idea concrete: a compact rule must be aligned with the query, key, batch and head dimensions it is meant to control.

Compatibility is checked from the trailing dimensions

Broadcasting compares dimensions from the end. Matching sizes and singleton dimensions can be compatible; missing leading dimensions can also be treated as singleton dimensions. This is an array operation rule rather than knowledge about language modelling. A framework cannot infer that a dimension means a user or a token. Consequently, an operation that passes broadcasting checks can still apply one example’s mask to another semantic axis. The programmer must supply the intended correspondence before treating successful execution as a correct attention policy.

Separate a key-validity rule from a pairwise rule

A padding mask can describe which key positions contain actual text for each example. A causal mask describes which query-key pairs may communicate. Their meanings differ even if broadcasting allows both to combine with attention scores. A singleton head dimension often expresses that the same rule applies to every head. A singleton query dimension can express a key-validity rule shared by all queries. Neither insertion should be chosen merely to silence a dimension error; each declares where the rule is being reused.

Construct a tiny example with unequal lengths

Imagine a batch containing a short sentence and a longer sentence. Their padded storage lengths match, but their valid key positions do not. Give each valid value a distinguishable pattern and compute a query near the short sentence’s end. The query must not gain information from that example’s padding or from a different batch member. This is a proposed diagnostic exercise. Unequal sentence lengths make a mistakenly shared mask easier to detect than a batch whose examples happen to have identical validity patterns.

Inspect the effective mask at the computation boundary

Before a fused attention call, inspect a small expanded interpretation of the mask together with the documented boolean or additive convention. The same boolean value does not necessarily mean allowed across every API. An additive mask acts on scores before normalization and needs the appropriate numerical treatment. Avoid materializing huge masks solely for inspection; a tiny equivalent case can reveal the rule. The operational lesson is to test the effective information flow, retaining batch identity and token validity while distinguishing array compatibility from application correctness.

Primary research and implementation references

Reproducible inference stacks: preserving a GPU pilot’s result; A GPU inference benchmark you can trust.

Earn rental income from GPUs

Have idle GPUs or looking to fund AI infrastructure? Retrivium finds a rental platform, sets up the server and helps you assess income and costs with a pilot.

Explore GPU hosting
← All articles