Skip to content

[AMD] sgl-kernel: enable NGRAM speculative decoding on ROCm (build ngram_utils) - #29346

Closed
michaelzhang-ai wants to merge 2 commits into
mainfrom
amd/enable-ngram-kernel-rocm
Closed

michaelzhang-ai wants to merge 2 commits into
mainfrom
amd/enable-ngram-kernel-rocm

Conversation

@michaelzhang-ai

@michaelzhang-ai michaelzhang-ai commented Jun 25, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Enables NGRAM speculative decoding on ROCm by building the missing kernel op into the ROCm sgl_kernel.

Root cause

srt/speculative/ngram_worker.py:_prepare_for_speculative_decoding calls sgl_kernel's reconstruct_indices_from_tree_mask, but that op was CUDA-only:

  • sgl-kernel/setup_rocm.py compiled only csrc/speculative/eagle_utils.cu — not csrc/speculative/ngram_utils.cu, where the op is defined.
  • The op was registered only in csrc/common_extension.cc (CUDA); the ROCm registration file csrc/common_extension_rocm.cc never declared it.

On ROCm this raised, at the first speculative-decode step:

AttributeError: '_OpNamespace' 'sgl_kernel' object has no attribute 'reconstruct_indices_from_tree_mask'

which crashed the scheduler — so any NGRAM-spec run on ROCm dies immediately (independent of attention backend / model).

Fix

  • Add csrc/speculative/ngram_utils.cu to the ROCm build sources in setup_rocm.py.
  • Register reconstruct_indices_from_tree_mask in common_extension_rocm.cc (the decl already exists in sgl_kernel_ops.h).

The kernel is plain CUDA C++ (no warp intrinsics / tensor cores) and already carries a #ifdef USE_ROCM include branch, so it hipifies cleanly.

Test plan

Verified locally on MI355X (gfx950), ROCm 7.2:

  • Rebuilt the ROCm sgl_kernel with this change; torch.ops.sgl_kernel.reconstruct_indices_from_tree_mask now dispatches.
  • Before: python -m sglang.launch_server ... --speculative-algorithm NGRAM --attention-backend triton crashed with the AttributeError above.
  • After: the same server launches (ready to roll) and serves correct output (Qwen2.5-Coder-7B-Instruct, e.g. "The capital of France is" → "Paris. Paris is located in the northern part of France...").

Note: validated on gfx950; the AMD CI NGRAM-spec lanes (e.g. test_spec_ngram*) run on gfx942/MI325 — this unblocks them there as well. A follow-up will enable the triton NGRAM-spec test for AMD CI on top of this.


CI States

Latest PR Test (Base): ❌ Run #28258198996
Latest PR Test (Extra): ❌ Run #28258198792

…coding

NGRAM speculative decoding calls sgl_kernel reconstruct_indices_from_tree_mask
(srt/speculative/ngram_worker.py:_prepare_for_speculative_decoding), but the op
was CUDA-only: setup_rocm.py compiled only eagle_utils.cu (not ngram_utils.cu),
and the op was registered only in common_extension.cc. On ROCm this raised
"AttributeError: '_OpNamespace' 'sgl_kernel' object has no attribute
reconstruct_indices_from_tree_mask", crashing the scheduler at the first
speculative decode step.

The kernel is plain CUDA (no CUDA-only intrinsics) and already carries a
USE_ROCM include branch, so it hipifies cleanly. Add csrc/speculative/
ngram_utils.cu to the ROCm build sources and register the op in
common_extension_rocm.cc.

Verified on MI355X (gfx950): the op now dispatches, and an NGRAM + triton
attention server (Qwen2.5-Coder-7B) launches and serves correct output.
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@michaelzhang-ai michaelzhang-ai added the run-ci CI: run the baseline test suite on this PR label Jun 26, 2026
@michaelzhang-ai

Copy link
Copy Markdown
Collaborator Author

Expanded this PR to also enable the triton NGRAM-spec test on AMD (test/registered/spec/test_spec_ngram_extra.py): the two flashinfer-only classes are guarded with @skipIf(is_hip()) (precedent: test_spec_standalone_extra.py) and the file is registered for extra-a-test-1-gpu-large-amd. Keeping the kernel fix and the test in one branch so CI builds the kernel and exercises NGRAM end-to-end on the real gfx942 runners (a standalone test PR couldn't pass until the kernel lands on main).

Dispatched both AMD extra lanes to verify:

@michaelzhang-ai
michaelzhang-ai force-pushed the amd/enable-ngram-kernel-rocm branch from e524489 to f332ef9 Compare June 26, 2026 18:41
@michaelzhang-ai

Copy link
Copy Markdown
Collaborator Author

Scoped this PR back to kernel-only (reverted the test_spec_ngram_extra enablement).

Verification from the dispatched AMD extra lanes:

  • ROCm sgl_kernel builds cleanly with ngram_utils.cu on gfx942/MI325, no regression — extra-a-test-1-gpu-small-amd and extra-a-test-2-gpu-large-amd (mi325) passed (they share the same kernel build/install step).
  • Locally on gfx950: reconstruct_indices_from_tree_mask now dispatches and an NGRAM + triton server launches and serves correct output.

The triton NGRAM-spec test failed on gfx942 extra-a-test-1-gpu-large-amd (separate from this kernel change; logs aren't retained on these runners, so the failing gate — likely GSM8K accuracy/accept-length — needs follow-up). Enabling that test for AMD CI will be a separate PR once this kernel fix lands and the gate is characterized.

This PR stands alone as a correctness fix: NGRAM speculative decoding previously crashed at the first decode step on ROCm; it now runs.

@Jiminator
Jiminator deleted the amd/enable-ngram-kernel-rocm branch September 14, 2026 04:43
@alexnails
alexnails restored the amd/enable-ngram-kernel-rocm branch September 14, 2026 05:32
@cursor
cursor Bot deleted the amd/enable-ngram-kernel-rocm branch September 17, 2026 07:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant