Skip to content

Fix FlashInfer A2A top-k ID dtype - #29929

Merged
Fridge003 merged 8 commits into
sgl-project:mainfrom
nvpohanh:fix-flashinfer-a2a-topk-int32
Jul 16, 2026
Merged

Fridge003 merged 8 commits into
sgl-project:mainfrom
nvpohanh:fix-flashinfer-a2a-topk-int32

Conversation

@nvpohanh

@nvpohanh nvpohanh commented Jul 2, 2026 •

Copy link
Copy Markdown
Collaborator

[by Codex]

Summary

  • Materialize bypassed top-k output before FlashInfer MoE all-to-all dispatch.
  • Convert selected expert IDs to torch.int32, as required by FlashInfer's MoeAlltoAll.dispatch API.
  • Add a registered four-GPU B200 GLM-5.2 NVFP4 test covering both DEP4+FlashInferA2A+StaticEP and the default DEP4 backend.

Motivation

SGLang's static expert-parallel dispatch remaps selected experts through an int64 expert-location table. FlashInfer 0.6.12 requires int32 expert IDs, so GLM-5.2 NVFP4 with DEP4, FlashInfer A2A, and --ep-dispatch-algorithm=static aborts during CUDA graph capture with:

Check failed: tokenSelectedExperts.dtype() == dl_int32 (int64 vs. int32)

Normalizing the IDs at the FlashInfer dispatcher boundary keeps SGLang's internal routing representation independent of FlashInfer's ABI.

FlashInfer ABI

SGLang pins FlashInfer 0.6.12. MoeAlltoAll.dispatch forwards token_selected_experts to the underlying API. Its moe_a2a_dispatch docstring specifies that token_selected_experts is an int32 tensor.

B200 verification

Ran the exact registered test in test/registered/4-gpu-models/test_glm52_nvfp4.py with four B200 GPUs and nvidia/GLM-5.2-NVFP4:

  • Before this PR (26b15a0825e32ff9fef4ff58b253f1c40a942981): DEP4+FlashInferA2A+StaticEP deterministically failed during CUDA graph capture with the int64 versus int32 check above.
  • With this PR (478f904912): the full combined test exited 0 in 1767 seconds with OVERALL: ALL TESTS PASSED.
    • DEP4+FlashInferA2A+StaticEP: performance PASS (804.1 output tok/s at batch size 16), GSM8K PASS (0.958 versus 0.920 baseline).
    • DEP4-default: performance PASS (861.9 output tok/s at batch size 16), GSM8K PASS (0.955 versus 0.920 baseline).

Additional checks:

  • python3 -m black --check test/registered/4-gpu-models/test_glm52_nvfp4.py
  • python3 -m py_compile test/registered/4-gpu-models/test_glm52_nvfp4.py
  • git diff --check 26b15a0825e32ff9fef4ff58b253f1c40a942981..HEAD

CI States

Latest PR Test (Base): ❌ Run #29388844304
Latest PR Test (Extra): ❌ Run #29388844184

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request updates the FlashInfer MoE token dispatcher to cast topk_ids to torch.int32 and adds a unit test to verify this behavior. The review feedback suggests ensuring robustness against BypassedTopKOutput by calling to_standard() if available, preventing potential AttributeErrors.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@nvpohanh

nvpohanh commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator Author

[by Codex] Addressed the review concern in ffaa7c6a4f: FlashInfer A2A now materializes BypassedTopKOutput before casting IDs, and the regression test covers both standard and bypassed top-k carriers. The focused test passed in lmsysorg/sglang:nightly-dev-cu13-20260701-a01afdd5 (2 tests, 0 failures; OCI job 168393).

@nvpohanh
nvpohanh marked this pull request as draft July 2, 2026 11:30
@nvpohanh
nvpohanh marked this pull request as ready for review July 2, 2026 16:15
@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!

@nvpohanh

nvpohanh commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator Author

[by Codex]
/tag-and-rerun-ci

@nvpohanh

nvpohanh commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator Author

@leejnau @nvjullin @trevor-m Agents told me that this patch is needed to run GLM-5 + FlashInfer A2A. Do you think it makes sense?

@nvpohanh nvpohanh added the run-ci CI: run the baseline test suite on this PR label Jul 2, 2026
@nvjullin

nvjullin commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

I'm not that familiar with moe dispatch. However, I'm concerned that the conversion to int32 is unconditional. Did the dispatch ever work for other backend combinations? Wouldn't those break?

@nvpohanh

nvpohanh commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator Author

[by Codex] Thanks, the conversion is not unconditional across SGLang backends. It is scoped to FlashinferDispatcher, which is instantiated only for --moe-a2a-backend flashinfer; DeepEP, NIXL, Mooncake, and standard dispatchers are unchanged.

The FlashInfer 0.6.12 MoeAlltoAll contract documents token_selected_experts as int32, and this dispatcher already sizes both the workspace and ID payload as router_topk * 4. Passing the router-produced int64 tensor therefore violated that boundary. The three MoE runners SGLang permits with FlashInfer A2A are also int32-compatible: FlashInfer Cutlass casts IDs with .to(torch.int), CuteDSL casts to int32, and the TRT-LLM runner PackTopkIds explicitly asserts int32.

The patch normalizes IDs only at that A2A boundary and returns the received IDs as int32 for those runners. The focused standard and bypassed-top-k tests passed in the pinned nightly container (OCI job 168393), and the GLM-5 DEP2 E2E path subsequently passed. I added the ABI rationale beside the cast in b2642c7db8.

@nvpohanh
nvpohanh marked this pull request as draft July 6, 2026 13:19
@nvpohanh nvpohanh removed the run-ci CI: run the baseline test suite on this PR label Jul 6, 2026
@nvpohanh

nvpohanh commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator Author

convert to draft for now since I need to verify that the added GLM-5.2 test could actually catch this failure

@github-actions github-actions Bot added the blackwell SM100/SM120 label Jul 7, 2026
@nvpohanh
nvpohanh force-pushed the fix-flashinfer-a2a-topk-int32 branch from 6f4239e to b9fc462 Compare July 7, 2026 07:40
@nvpohanh
nvpohanh marked this pull request as ready for review July 7, 2026 07:43
@nvpohanh

nvpohanh commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator Author

/tag-and-rerun-ci

@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Jul 7, 2026
@nvpohanh

nvpohanh commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator Author

/rerun-failed-ci

@leejnau

leejnau commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

@leejnau @nvjullin @trevor-m Agents told me that this patch is needed to run GLM-5 + FlashInfer A2A. Do you think it makes sense?

my agents agree 🕵️

@nvpohanh

Copy link
Copy Markdown
Collaborator Author

@b8zhong Could you help me to review this? This fixes a topk_id dtype mismatch issue when using flashinfer alltoall. Thanks!

All the NV pipelines have passed.

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

/rerun-test test/registered/4-gpu-models/test_glm52_nvfp4.py

@b8zhong

b8zhong commented Jul 10, 2026

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/4-gpu-models/test_glm52_nvfp4.py

@github-actions

github-actions Bot commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/4-gpu-models/test_glm52_nvfp4.py:

🚀 4-gpu-b200 (1 test): ✅ View workflow run

cd test/ && python3 registered/4-gpu-models/test_glm52_nvfp4.py

@nvpohanh

Copy link
Copy Markdown
Collaborator Author

@b8zhong is this PR okay to merge? Or do we need more reviews? Thanks!

@b8zhong b8zhong self-assigned this Jul 14, 2026
from sglang.test.run_combined_tests import run_combined_tests
from sglang.test.test_utils import CustomTestCase, ModelLaunchSettings

register_cuda_ci(est_time=1800, suite="nightly-4-gpu-b200", nightly=True)

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.

Can we make it a subtest of test/registered/ep/test_flashinfer_a2a.py instead
A nightly test with 1800s is too heavy

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

makes sense. Will update it

@nvpohanh nvpohanh Jul 15, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

[by Codex] Addressed in 51678e2. I moved the GLM-5.2 NVFP4 regression coverage into test/registered/ep/test_flashinfer_a2a.py as TestFlashinferA2ACutedslStaticFP4 and removed the standalone 1,800-second nightly accuracy/performance matrix. The consolidated case exercises Static EP, flashinfer_cutedsl, and FlashInfer A2A, then performs one generation request. Validated on AIHub B200x4 (job 1379463): TestFlashinferA2ACutedslStaticFP4.test_generate passed — 1 test in 471.443s, OK. The branch is also rebased onto latest origin/main at c00131e.

@nvpohanh
nvpohanh force-pushed the fix-flashinfer-a2a-topk-int32 branch from 1911569 to 51678e2 Compare July 15, 2026 04:23
@nvpohanh

Copy link
Copy Markdown
Collaborator Author

@b8zhong @Fridge003 I have updated the test definition. Could you review again? thanks!

@Fridge003

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/ep/test_flashinfer_a2a.py

@github-actions

github-actions Bot commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/ep/test_flashinfer_a2a.py:

🚀 4-gpu-gb300 (1 test): ❌ View workflow run

cd test/ && python3 registered/ep/test_flashinfer_a2a.py

@Fridge003

Copy link
Copy Markdown
Collaborator

@Fridge003
Fridge003 merged commit 5d004a2 into sgl-project:main Jul 16, 2026
145 of 167 checks passed
Chronostasys pushed a commit to MindLab-Research/sglang that referenced this pull request Aug 24, 2026
mmangkad pushed a commit that referenced this pull request Oct 12, 2026
- test_flashinfer_dispatcher: add regression cases for both parts of
  #29929. One dispatches int64 expert IDs, as static EP
  dispatch produces, and asserts MoeAlltoAll receives int32. The other
  dispatches a bypassed top-k output, as runners that fuse routing produce,
  and asserts MoeAlltoAll receives routed int32 expert IDs and weights.
  Both run per-PR on CPU. Move the file's cases into a CustomTestCase class.
- test_flashinfer_a2a: drop the GLM-5.2 static-dispatch class, which
  checked the expert-ID dtype with a full server launch. FlashInfer A2A
  with the CuteDSL runner stays covered by test_flashinfer_a2a_cutedsl_v2
  and test_nvidia_nemotron_3_super_nvfp4.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blackwell SM100/SM120 run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants