Skip to content

[Fix] kda: Keep varlen KDA prefill free of host syncs under torch.com… - #53610

Open
rumitdesai wants to merge 1 commit into
sgl-project:mainfrom
rumitdesai:fix/kda-prefill-graph-capture
Open

rumitdesai wants to merge 1 commit into
sgl-project:mainfrom
rumitdesai:fix/kda-prefill-graph-capture

Conversation

@rumitdesai

@rumitdesai rumitdesai commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

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:

Where Problem under capture
FLA prepare_chunk_indices Built the (sequence, chunk) table with a Python loop over .tolist(), a host sync whose length depends on the data
FLA tensor_cache Appends its arguments to a Python list. Traced, a symbolic argument became an extra graph output
KDAAttnBackend padding trim Branched on logical_num_tokens < physical_num_tokens, which depends on the data
_convolve_prefill Sized the causal-conv launch grid from the host list extend_seq_lens_cpu
forward_extend Read forward_metadata.logical_num_tokens, a host int

Modifications

FLA (kernels/ops/attention/fla):

  • prepare_chunk_indices builds the table with tensor ops (repeat_interleave with an explicit output size).
  • Under capture, callers pass max_num_chunks and the table is padded to that fixed size.
    • The KDA callers pass cdiv(tokens, chunk_size) + num_sequences, from _compiled_chunk_index_capacity.
    • Padded rows point past the last sequence's end, where the kernels already exit early.
    • Lengths and offsets are int64 under capture.
  • tensor_cache calls 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_padding and _restore_kda_token_padding replace 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.
  • Under capture, _convolve_prefill passes causal_conv1d_fn a block_table. The table is built on the device by prepare_chunk_indices and padded the same way. causal_conv1d_fn already supports this launch mode.
  • Under capture, forward_extend runs on every row instead of trimming. _mask_kda_padding_rows then zeroes the rows after query_start_loc[-1]. Padding rows never feed a sequence or a state update, because the conv and chunk kernels are driven by query_start_loc.

Eager behavior is unchanged. Without capture and without max_num_chunks, prepare_chunk_indices returns 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 with fullgraph=True and compares the result with eager:

  • The chunk table with data-dependent cu_seqlens, for int32 and int64. Fails before this PR.
  • A too-small max_num_chunks raises.
  • The eager table is unchanged.
  • tensor_cache adds no graph outputs. Fails without the utils.py change.
  • The conv block table is a tensor built on the device.
  • The padding trim and restore round-trip with a token count that depends on the data.
  • The padding rows are zeroed without a host token count.

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

  1. Ping Merge Oncalls to start the process. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • Common commands include /tag-and-rerun-ci, /approve-ci, /rerun-failed-ci
  4. After green CI and required approvals, ask Merge Oncalls or people with Write permission to merge the PR.

CI States

Latest PR Test (Base): ⊘ Skipped -- a maintainer comments /tag-and-rerun-ci to run CI tests.
Latest PR Test (Extra): ⊘ Skipped -- a maintainer comments /tag-and-rerun-ci to run CI tests.
Latest PR Test (AMD ROCm 10): ⚠️ Run #46051203074

Tip

Base CI only covers the core cases, and its workload may not exercise your change. Use /rerun-test to run the tests your change touches, or copy this prompt for an agent:

Find the registered tests this PR affects and run them with /rerun-test, following https://github.com/sgl-project/sglang/blob/main/.agents/skills/rerun-test/SKILL.md

…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.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant