Model and Inference EngineeringTransformer and model mechanicsStaff
How should a sliding-window attention mask align short queries with a longer cached key sequence?
By Amit Kumar3 min readWorked answer includedReviewed 28 Sept 2026
The question
Interview question
A local-attention wrapper passes full-prefill tests but fails incremental decode. Eight keys represent positions 0 through 7, and two new queries represent positions 6 and 7. The intended window includes the current token and three prior positions. Derive the allowed keys, explain how top-left alignment can pass prefill yet fail decode, and design reference tests for padding, eviction and uneven batch lengths.
Draft your approach first
Take a few minutes before you read the answer. Name the constraint that matters most, the failure you are designing for and how you would know it worked.
Reveal the worked answerWorked answer
An attention mask specifies which past tokens a query may read. In incremental decoding, a short query tensor often represents the newest positions of a much longer key tensor. Its row number is not necessarily its position in the sequence.
I would look at the mask alignment before blaming the cache contents. In prefill, query and key lengths often match, so a top-left triangular mask and a mask aligned to the last key positions can look the same. In incremental decode, we might have one new query and 1,000 keys that include earlier tokens. That query is position 999, not position zero. A local window must be placed around its actual position in the full sequence.
Take a tiny case with eight keys and two new queries. Those queries correspond to positions 6 and 7. With three prior positions in the left window and no right window, query 6 may read keys 3 through 6, and query 7 may read keys 4 through 7. If our wrapper builds a two-row mask as if the queries were positions 0 and 1, it points at the oldest keys, or masks every valid key. The shapes still pass and the output can still be finite. The FlashAttention interface documents bottom-right alignment when query and key lengths differ and defines its local window relative to both lengths. Check the contract of the particular kernel version in use.
I would write the expected allowed-key indices by hand for sequence lengths 1, 2 and 8, then compare a simple reference attention implementation with the optimized path. Include one-token decode, chunked decode, left padding, different cache lengths in one batch and the boundary where an old key leaves the window. Compare masks first, then attention outputs and logits. A prefill-only parity test misses the exact shape that fails.
If the interviewer says the sliding cache already evicted old keys, that changes the available key range but does not excuse a wrong alignment over the keys that remain. Ask whether the kernel sees absolute positions, compacted cache positions or both. Do not apply an offset twice. The sliding-window KV cache fits. Why did evicting every layer break long prompts? asks which layers are allowed to evict history. This question assumes local attention is intended and tests whether the query reads the right local keys at decode time.
For this API convention, query row i has aligned key position i+key_length-query_length. With lengths 2 and 8, row 0 therefore aligns to 6. A left extent of 3 and right extent of 0 allows positions from 3 through 6. Row 1 aligns to 7 and allows 4 through 7. This definition includes the current position, giving four visible keys. Some APIs call that a window of four, while others accept the left extent 3. Name the convention instead of assuming the integer has a universal meaning.
FlashAttention’s documented causal alignment changed in version 2.1, and its local-attention support was introduced separately. Pin the deployed version and wrapper contract when comparing behavior. A correct fix should translate logical positions into the kernel’s coordinate system once. If the wrapper already offsets a cropped cache and the kernel also applies a length offset, adding another correction can shift the window a second time. Reference cases need explicit key identities so an apparently plausible average cannot conceal the wrong selected range.
Related questions
Keep practising model and inference engineering
Follow another question in this area, or search the complete library.
Get my essays on AI engineering and distributed systems by email. Subscribe to ArchCrux