In this article
Map SDPA query rows to absolute key positions; nonsquare upper-left causality and a lower-right suffix bias express different contexts.
A cached decode test passes tensor-shape checks but attends only to the beginning of its key sequence. The query is a short suffix, while keys include earlier context. PyTorch SDPA documents upper-left alignment for a nonsquare is_causal mask. That is different from the lower-right alignment commonly needed when query rows represent the latest positions.
Map queries to absolute positions
Record which sequence positions each query row represents and which positions appear in keys and values. Equal head dimensions do not establish that mapping. A suffix query over a longer cache has a position offset that a mask must express. Preserve padding and any excluded cache positions as separate constraints rather than inferring causality solely from tensor lengths.
Read the SDPA flag contract
The scaled_dot_product_attention reference states that is_causal uses upper-left causal alignment when the attention matrix is nonsquare. A short query matrix therefore does not automatically mean that its first row represents the final key position. Correctly shaped output can still omit required history. Verify the flag’s documented meaning instead of assuming every attention implementation gives causal the same offset convention.
Construct the intended bias
PyTorch’s causal_lower_right factory describes an explicit diagonal offset from key and query lengths. Use that documented bias only when it matches the actual query-position mapping. It is not the correct choice for every nonsquare operation. Prefix queries, arbitrary selected positions and ragged layouts can require a different explicit mask with their own supported representation and backend constraints.
Preserve mask API semantics
Follow the installed SDPA API’s contract for an explicit attention bias and the is_causal argument rather than enabling both independently. Boolean mask participation also has a documented meaning that differs from some padding-mask interfaces. Keep the chosen representation and library version with the test. A copied mask can have the right dimensions while reversing which keys are permitted.
Test values that expose alignment
Use distinguishable values at early and late key positions and compare the cached operation with a reference expressing absolute query coordinates. Test square and nonsquare cases, since square inputs can hide the alignment difference. Check partial suffixes and padding before timing. Acceptance establishes the intended visible context. It does not guarantee a particular fused backend, identical floating-point bits or faster decode merely because the mask now has correct semantics.
Primary references
PyTorch Scaled Dot Product Attention.
PyTorch Lower-Right Causal Bias.
PyTorch Causal Bias Implementation.
Related reading: Causal masks define information flow during parallel training; Broadcasting an attention mask: align meanings before shapes.
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.