Repository navigation
[Fix] kda: Keep varlen KDA prefill free of host syncs under torch.com… - #53610
Open
rumitdesai wants to merge 1 commit into
Open
rumitdesai wants to merge 1 commit into
rumitdesai wants to merge 1 commit into
Conversation
…pile Capturing KDA prefill with torch.compile and a data-dependent token count failed or recompiled in five places: - FLA's prepare_chunk_indices built the (sequence, chunk) table with a loop over .tolist() of the chunk counts, a host sync with a data-dependent length. It now uses tensor ops. Under capture, callers pass max_num_chunks, cdiv(tokens, chunk) + sequences from _compiled_chunk_index_capacity, and the table is padded to that size. Padded rows point past the last sequence's end, where the kernels exit early. Lengths and offsets are int64 under capture. - tensor_cache appends its arguments to a Python list. Traced, a symbolic argument such as that capacity became an extra graph output. The cache is skipped under capture. - KDAAttnBackend trimmed MoE token padding with a branch on logical_num_tokens < physical_num_tokens. Under capture, _trim_kda_token_padding and _restore_kda_token_padding assert the size relation and always slice and pad. - _convolve_prefill sized the causal-conv grid from extend_seq_lens_cpu, so a graph warmed up on a one-token batch guarded on that length. Under capture it passes a block_table built on the device and padded the same way. - forward_extend read logical_num_tokens, a host int. Under capture it runs on every row and zeroes the rows after query_start_loc[-1] at the end. Eager behavior is unchanged.
rumitdesai
requested review from
hanming-lu,
hebiao064,
yizhang2077 and
yuan-luo
as code owners
November 27, 2026 18:40
This branch has not been deployed
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.
Motivation
An OOT graph backend captures KDA prefill with a symbolic token count. There, a host sync whose length depends on the data cannot be traced. Reading a host-side token count makes Dynamo specialize the graph on that value: a graph warmed up on a one-token batch then fails on the first real prompt with "Recompilation triggered with skip_guard_eval_unsafe stance".
KDA prefill did each of these in five places:
prepare_chunk_indices(sequence, chunk)table with a Python loop over.tolist(), a host sync whose length depends on the datatensor_cacheKDAAttnBackendpadding trimlogical_num_tokens < physical_num_tokens, which depends on the data_convolve_prefillextend_seq_lens_cpuforward_extendforward_metadata.logical_num_tokens, a hostintModifications
FLA (kernels/ops/attention/fla):
prepare_chunk_indicesbuilds the table with tensor ops (repeat_interleave with an explicit output size).cdiv(tokens, chunk_size) + num_sequences, from_compiled_chunk_index_capacity.int64under capture.tensor_cachecalls the wrapped function directly under capture. A cache keyed on Python object identity means nothing inside a compiled graph.KDA backend (
srt/layers/attention/linear/kda_backend.py):_trim_kda_token_paddingand_restore_kda_token_paddingreplace the inline trim and restore code. Under capture they check the size relation with torch._check and always slice and pad; eager keeps the early returns._convolve_prefillpassescausal_conv1d_fnablock_table. The table is built on the device byprepare_chunk_indicesand padded the same way.causal_conv1d_fnalready supports this launch mode.forward_extendruns on every row instead of trimming._mask_kda_padding_rowsthen zeroes the rows afterquery_start_loc[-1]. Padding rows never feed a sequence or a state update, because the conv and chunk kernels are driven byquery_start_loc.Eager behavior is unchanged. Without capture and without
max_num_chunks,prepare_chunk_indicesreturns the same table as before.Tests
New CPU test
test/registered/unit/layers/attention/linear/test_kda_prefill_graph_capture.py. Each case compiles one helper withfullgraph=Trueand compares the result with eager:cu_seqlens, forint32andint64. Fails before this PR.max_num_chunksraises.tensor_cacheadds no graph outputs. Fails without theutils.pychange.Accuracy Tests
No change to how these paths run eagerly.
Speed Tests and Profiling
No change for eager or CUDA graphs: the new paths run only under
torch.compile.Checklist
Review and Merge Process
/tag-and-rerun-ci,/approve-ci,/rerun-failed-ciCI States
Latest PR Test (Base): ⊘ Skipped -- a maintainer comments⚠️ Run #46051203074
/tag-and-rerun-cito run CI tests.Latest PR Test (Extra): ⊘ Skipped -- a maintainer comments
/tag-and-rerun-cito run CI tests.Latest PR Test (AMD ROCm 10):
Tip
Base CI only covers the core cases, and its workload may not exercise your change. Use
/rerun-testto run the tests your change touches, or copy this prompt for an agent: