Add decode (flash-decoding) attention kernels to contributed/#129
Open
varuntej07 wants to merge 2 commits into
Open
Add decode (flash-decoding) attention kernels to contributed/#129varuntej07 wants to merge 2 commits into
varuntej07 wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of changes:
Adds a new community kernel file,
contributed/decode_attention.py, implementing the decode (single-query) step of autoregressive attention. This is the memory-bound complement to the compute-bound prefill kernel incontributed/pipelined_attention.py.Two kernels are included:
decode_attention_fwd- simplest correct version: one head, one KV tile,seqlen_kv <= 128. A single query position (seqlen_q = 1) attends over the full cached K/V, running QK -> scale -> softmax -> PV with the softmax scale applied.decode_attention_gqa_fwd- lifts the length limit with KV tiling and a running online softmax (state carried across tiles), and adds grouped-query attention so query heads sharing a KV head also share its K/V loads (loaded once per group).Both use the d-on-partition-axis layout from the existing attention tutorials.
IO layouts:
decode_attention_fwd: q(d, 1), k(d, seqlen_kv), v(d, seqlen_kv)-> o(1, d)decode_attention_gqa_fwd: q(d, n_q_heads), k/v(n_kv_heads, d, seqlen_kv)-> o(n_q_heads, d)Current limits:
d <= 128; GQA requiresn_q_heads % n_kv_heads == 0andseqlen_kv % TILE_KV == 0(no padding yet); single batch element. Split-KV flash-decoding for very long context is noted as planned future work.This kernel targets
contributed/, notsrc/reference/, so the baremetal /benchmark / integration requirements (which CONTRIBUTING.md scopes tosrc/reference/) do not apply. Correctness is validated on CPU vianki.simulate_kernelagainst NumPy reference implementations included in the file (numpy_decode_referenceandnumpy_decode_gqa_reference, therepeat_kv+per-head softmax oracle). Runpython contributed/decode_attention.pyto execute both checks; the GQA case exercises 4 KV tiles with group = 4 and asserts agreement withinatol = rtol = 1e-2. The kernels are not yet run on Neuron hardware; this is stated in the module docstring WARNING.