Skip to content

[Fix] sgl-kernel: Support arbitrary expert counts in no-copy-engine transfer - #32769

Open
mrcha033 wants to merge 6 commits into
sgl-project:mainfrom
mrcha033:mrcha033/fix-cpu-offloader-deepep
Open

mrcha033 wants to merge 6 commits into
sgl-project:mainfrom
mrcha033:mrcha033/fix-cpu-offloader-deepep

Conversation

@mrcha033

@mrcha033 mrcha033 commented Jul 29, 2026 •

Copy link
Copy Markdown

Motivation

Fixes #32507.

CPU OffloaderV2's CPU mode requires the DeepEP expert-count vector to reach
the GPU without using a copy engine. copy_to_gpu_no_ce only instantiated
N=32, 64, and 72, so a valid 256-expert/EP16 configuration produced N=16 and
failed during prefill warmup with RuntimeError: unexpected N.

This is the same fixed-shape failure class previously addressed for N=32 in
#23550.

Modifications

  • Add an exact N=16 specialization for the reported configuration.
  • Preserve the existing compact N=32/64/72 launch paths.
  • Add a bounded, by-value fallback for other sizes up to 512, without using a
    copy engine.
  • Validate empty and oversized inputs with descriptive errors.
  • Cover N=16, a previously unsupported N=17, existing specializations, the
    N=512 boundary, and N=513 rejection.

Compared with the always-generic approach in #32515, the hybrid dispatch keeps
small launch arguments for common expert counts while remaining robust to new
counts.

Accuracy Tests

The modified CUDA source was compiled and exercised directly on an NVIDIA L40S
with CUDA 12.8 and PyTorch 2.10:

correctness: PASS
sizes: 16, 17, 32, 64, 72, 512
oversized input rejection: 513 PASS

The operation is an exact int32 control-vector copy; it does not alter model
arithmetic.

Local static checks:

python -m py_compile python/sglang/kernels/aot/tests/test_copy.py
clang-format --dry-run --Werror python/sglang/kernels/aot/csrc/elementwise/copy.cu
git diff --check

Speed Tests and Profiling

NVIDIA L40S, CUDA 12.8, PyTorch 2.10, median of 7 repetitions with 20,000
operations per repetition:

N=16 path Latency
Hybrid exact specialization 1.831 us
Generic MAX_N=512 kernel (#32515 approach) 1.981 us
Pinned-memory H2D copy 3.477 us

The exact specialization was 7.6% lower latency than always passing the
512-element generic argument and 47.3% lower latency than the copy-engine
fallback in this microbenchmark.

Checklist

  • Format code according to the project conventions.
  • Add unit tests for the reported regression and fallback boundaries.
  • Provide exact-copy correctness results.
  • Provide a targeted latency comparison.
  • No documentation update is needed for this internal kernel fix.

CI States

Latest PR Test (Base): ❌ Run #42526380267
Latest PR Test (Extra): ❌ Run #42526380256
Latest PR Test (AMD ROCm 10): ❌ Run #42526380298

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

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@mrcha033
mrcha033 marked this pull request as ready for review July 29, 2026 11:08
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

Copy link
Copy Markdown
Author

@BBuf The initial CI fan-out failed only because the PR was opened as a draft (PR is draft. Blocking CI.). It is now ready for review. Could you or another authorized maintainer please run /tag-and-rerun-ci extra so the run-ci/run-ci-extra labels are applied and the sgl-kernel plus DeepEP checks execute? The L40S correctness and latency results are included in the PR body.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 506ea95664

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +70 to +71
cudaStream_t stream = at::cuda::getCurrentCUDAStream();
copy_to_gpu_no_ce_fallback_kernel<<<grid, block, 0, stream>>>(input_array, n, output.data_ptr<int>());

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Launch the fallback on the output tensor's device

When output is on a CUDA device other than the current device, which the input checks currently permit, a fallback-size call such as N=17 obtains the current device's stream and launches there while passing a pointer owned by output.device(). This can fail with an invalid-device-pointer error or, when peer access is enabled, perform an unordered cross-device write. Guard output.device() before retrieving the stream and launching the kernel.

Useful? React with 👍 / 👎.

@mrcha033

mrcha033 commented Nov 1, 2026

Copy link
Copy Markdown
Author

This is still needed on current main (c108a65): copy_to_gpu_no_ce still accepts only N = 32, 64 and 72 and fails with unexpected N otherwise, and copy_list_to_gpu_no_ce still sends the expert-count list through it. The N=16 case from #32507 (256 experts, EP16) therefore still fails, even though that issue has been closed.

I've merged current main into the branch so CI runs with the current workflows; the extra tier I asked for earlier has since been retired. The change touches python/sglang/kernels/aot/, so it needs the full PR CI rather than a selective rerun.

@BBuf @ispobock could one of you run /tag-and-rerun-ci nv-extra? Thanks.

@github-actions

This comment has been minimized.

@mrcha033 mrcha033 changed the title [Bugfix] Support arbitrary expert counts in no-copy-engine transfer [Fix] sgl-kernel: Support arbitrary expert counts in no-copy-engine transfer Nov 1, 2026
@BBuf

BBuf commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator

[BBuf Review Agent] @mrcha033 /tag-and-rerun-ci nv-extra

Confirmed, c108a65 still rejects N=16, and this AOT change needs a full rerun to rebuild the kernel.

@BBuf BBuf left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[BBuf Review Agent] One thing to fix before this can go in.

Still open from my last review:

  • Guard the output device before resolving the launch stream (python/sglang/kernels/aot/csrc/elementwise/copy.cu:70)

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.

[Bug] CPU OffloaderV2 with DeepEP fails when local expert count is not in copy_to_gpu_no_ce supported sizes

2 participants