Skip to content

[DeepSeek V3] Defer moe finalize and fused it with main stream add - #27720

Merged
Fridge003 merged 15 commits into
mainfrom
kp/defer-moe-finalize
Jun 13, 2026
Merged

Fridge003 merged 15 commits into
mainfrom
kp/defer-moe-finalize

Conversation

@kpham-sgl

@kpham-sgl kpham-sgl commented Jun 9, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

Optimizing Kimi K2.5 NVFP4
Note: this PR does not change behaviour during prefill phase yet (still 2 individual kernel launches). More work needed to speedup there

Modifications

  • Swap the execution order. Main stream now execute routed experts, alt stream execute shared experts
  • On main stream, defer finalized (do_finalize = False) and fuse finalize with add kernel

Accuracy Tests

Speed Tests and Profiling

Resulted in -2us per layer time. 1-2% TPOT speedup for 80k, ISL 220, with Kimi K2.5 NVFP4 on TP=8

Before
Screenshot 2026-06-10 at 3 35 10 PM

After
Screenshot 2026-06-10 at 3 35 36 PM

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, /tag-run-ci-label, /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): 🚫 Run #27448273418
Latest PR Test (Extra): ❌ Run #27448273296

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

@kpham-sgl kpham-sgl changed the title Kp/defer moe finalize [DeepSeek V3] Defer moe finalize and fused it with main stream add Jun 10, 2026
@kpham-sgl
kpham-sgl marked this pull request as ready for review June 10, 2026 22:36
@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!

@kpham-sgl

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 Jun 10, 2026
@kpham-sgl

Copy link
Copy Markdown
Collaborator Author

/rerun-test test/registered/models_e2e/test_deepseek_v3_fp4.py test/registered/spec/eagle/test_deepseek_v3_fp4_mtp_small.py test/registered/models_e2e/test_deepseek_v32_fp4_mtp_tp.py
test/registered/models_e2e/test_deepseek_v32_fp4_mtp_dp.py

@github-actions

github-actions Bot commented Jun 10, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/models_e2e/test_deepseek_v3_fp4.py test/registered/spec/eagle/test_deepseek_v3_fp4_mtp_small.py test/registered/models_e2e/test_deepseek_v32_fp4_mtp_tp.py:

🚀 4-gpu-b200 (3 tests): ✅ View workflow run

cd test/ && python3 registered/models_e2e/test_deepseek_v3_fp4.py
cd test/ && python3 registered/spec/eagle/test_deepseek_v3_fp4_mtp_small.py
cd test/ && python3 registered/models_e2e/test_deepseek_v32_fp4_mtp_tp.py

# Sglang Cache Dir
SGLANG_CACHE_DIR = EnvStr(os.path.expanduser("~/.cache/sglang"))
SGLANG_FLASHINFER_AUTOTUNE_CACHE = EnvBool(True)
SGLANG_ENABLE_MOE_DEFERRED_FINALIZE = EnvBool(False)

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.

Could we enable it by default, if it's always better

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.

Should always be better perf wise, small concern about correctness. See FIXME note

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.

Add a tracking issue for flashinfer

Comment thread python/sglang/jit_kernel/csrc/moe/moe_finalize_fuse_shared.cu Outdated
@kpham-sgl

Copy link
Copy Markdown
Collaborator Author

I will merge this first and try out the new kMoEFinalizeARResidualRMSNorm in flashinfer (which should fuse all of moe finalize + add + AR + RMSNorm + fp4 quant)

The host launcher referenced tokenspeed:: for MAX_TOPK, BF16 and
dispatchFinalize, but these are defined in namespace sglang. The stale
tokenspeed:: prefix (leftover from the forked source) broke the JIT
compile. Use sglang:: to match the definitions.

Co-authored-by: Cursor <cursoragent@cursor.com>
@kpham-sgl

Copy link
Copy Markdown
Collaborator Author

/rerun-test test/registered/models_e2e/test_deepseek_v3_fp4.py test/registered/spec/eagle/test_deepseek_v3_fp4_mtp_small.py test/registered/models_e2e/test_deepseek_v32_fp4_mtp_tp.py
test/registered/models_e2e/test_deepseek_v32_fp4_mtp_dp.py

@github-actions

github-actions Bot commented Jun 11, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/models_e2e/test_deepseek_v3_fp4.py test/registered/spec/eagle/test_deepseek_v3_fp4_mtp_small.py test/registered/models_e2e/test_deepseek_v32_fp4_mtp_tp.py:

🚀 4-gpu-b200 (3 tests): ✅ View workflow run

cd test/ && python3 registered/models_e2e/test_deepseek_v3_fp4.py
cd test/ && python3 registered/spec/eagle/test_deepseek_v3_fp4_mtp_small.py
cd test/ && python3 registered/models_e2e/test_deepseek_v32_fp4_mtp_tp.py

@kpham-sgl

Copy link
Copy Markdown
Collaborator Author

/rerun-failed-ci

@kpham-sgl
kpham-sgl requested a review from Qiaolin-Yu June 12, 2026 03:06
Comment thread python/sglang/srt/models/deepseek_v2.py
@Fridge003
Fridge003 merged commit d1a39b0 into main Jun 13, 2026
113 of 129 checks passed
@Fridge003
Fridge003 deleted the kp/defer-moe-finalize branch June 13, 2026 02:00
Chronostasys pushed a commit to MindLab-Research/sglang that referenced this pull request Aug 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bypass-fastfail deepseek jit-kernel run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants