Skip to content

[Perf] kda: Reuse int64 prefill offsets across layers - #50102

Merged
kpham-sgl merged 2 commits into
sgl-project:mainfrom
alphabetc1:port-k31-kda-metadata-review
Nov 13, 2026
Merged

kpham-sgl merged 2 commits into
sgl-project:mainfrom
alphabetc1:port-k31-kda-metadata-review

Conversation

@alphabetc1

@alphabetc1 alphabetc1 commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Original author: @kpham-sgl (Khoa Pham); credited with a Co-authored-by trailer in the port commit.

Port of #48414

Takes only the per-forward int64 cu_seqlens reuse from f87a6998703. The pinned FlashInfer 0.7.4 casts int32 sequence offsets to a new int64 tensor on each KDA prefill call, while its host-metadata cache keys on the live tensor identity. Passing a new tensor at every layer defeats that cache and repeats cpu().tolist() on the CUDA offsets.

References: FlashInfer's conversion and host-metadata memo.

Modifications

  • Build one int64 copy in KDA eager prefill forward metadata when the configured extend kernel requests it; keep the original int32 offsets for existing consumers.
  • Pass that same tensor to each FlashInfer KDA layer. A new forward creates fresh metadata; decode and target verify do not create this copy.
  • Other linear-attention kernels keep the default opt-out. Breakable prefill graphs use their existing dedicated path.

The selected source hunks are unchanged. Left in #48414: pinned checkpoint-metadata H2D, verify block-limit changes, state-free decode radix, FlashMLA token-packing runtime integration, and megakernel changes.

Validation

  • pre-commit on all four changed files and git diff --check: passed.
  • Six local CPU checks passed with real PyTorch tensors, executing AST-extracted source methods and FlashInfer 0.7.4's actual _cu_seqlens_contents helper: int64 values/dtype, fresh metadata on the next forward, backend opt-out, decode, target verify, and memo reuse. The metadata builder and initializer execute their source code; GPU imports and unrelated pool setup are isolated.
  • Instrumenting the upstream helper's host-read line across four layer calls gives four reads with separate int32-to-int64 casts and one read with the shared int64 tensor. This is a mechanism check, not a latency benchmark.
  • These are local focused checks, not a run of the full registered test suite. No new test or benchmark file is committed. Existing GPU coverage to run: registered/kernels/ops/attention/test_kda_prefill.py and registered/kernels/ops/attention/test_kda_prefill_graph.py.

Performance

This removes repeated metadata conversion and host-content reads in the eager FlashInfer KDA path. No GPU speedup or model-accuracy result is claimed. GPU validation and engine-step benchmarking on current main remain outstanding.


CI States

Latest PR Test (Base): 🚫 Run #43214595030
Latest PR Test (Extra): ❌ Run #43214594802
Latest PR Test (AMD ROCm 10): ❌ Run #43214594904

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

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

@alphabetc1
alphabetc1 force-pushed the port-k31-kda-metadata-review branch from 1f4d448 to 02ab384 Compare November 12, 2026 11:52
@alphabetc1 alphabetc1 added the run-ci CI: run the baseline test suite on this PR label Nov 12, 2026
@kpham-sgl
kpham-sgl merged commit 2091b21 into sgl-project:main Nov 13, 2026
156 of 166 checks passed
hnyls2002 added a commit that referenced this pull request Nov 15, 2026
kevin-mii added a commit that referenced this pull request Nov 15, 2026
Brings in the c13 catch-up (main up to a56dbcf, #50102) and the
k3_ar_fusion workspace-provider follow-up. No conflicts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
MatsueYu added a commit to MatsueYu/sglang that referenced this pull request Nov 16, 2026
… kernel

KDAAttnBackend.init_forward_metadata reads
extend_kernel.wants_int64_query_start_loc unconditionally since sgl-project#50102.
The Ascend _AscendKDAExtendKernel is a duck-typed kernel that does not
inherit LinearAttnKernelBase, so Kimi-K3 prefill failed with
AttributeError.

The kernel casts query_start_loc to int64 inside extend(), so the shared
int64 offset copy is unnecessary; declare the flag as False.
lihenga added a commit to lihenga/sglang-sugon that referenced this pull request Nov 18, 2026
Build the optional int64 query offsets once per eager non-speculative extend
forward and keep that tensor alive for all KDA layers. FlashInfer's host
contents memoization can then reuse its tensor identity instead of repeating
the int32-to-int64 conversion and host read in every layer.

Keep other kernels opted out and preserve the original-offset fallback.
Adapt the offsets reuse change from upstream SGLang PR sgl-project#50102.

Validation: the combined worktree was exercised on TP8 B300 Kimi-K3 with
Mooncake L4, including 100-document mixed-replica replay, deterministic
answers, external-cache restore, and the trace-disabled path. A separate
profile confirmed the 16/32/48-byte copies each fell from 69 calls to one;
profile wall times are not a controlled speed comparison. Static checks pass.
No unit tests were run, following the workspace instruction.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants