Skip to content

[RL] fix deepseek v4 MXFP8 flashinfer_trtllm_routed MoE weight update - #28676

Merged
Fridge003 merged 10 commits into
sgl-project:mainfrom
xiuhu17:v4_mxfp8_main
Jul 1, 2026
Merged

Fridge003 merged 10 commits into
sgl-project:mainfrom
xiuhu17:v4_mxfp8_main

Conversation

@xiuhu17

@xiuhu17 xiuhu17 commented Jun 18, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

DeepSeek-V4 (MXFP8) on the flashinfer_trtllm_routed MoE path breaks after the
first RL weight update: train_rollout_logprob_abs_diff jumps from ~0.06 to
~3.83. Steady-state is fine — the bug is specific to the weight-reload path.

Root cause

align_mxfp8_moe_weights_for_flashinfer_trtllm shuffles MoE weights/scales into
the kernel layout using row-permutation index tensors that depend only on shape,
so they're memoized in the GPU cache _flashinfer_trtllm_shuffle_row_indices_cache_mxfp8
(added in #21280).

These cached index tensors are GPU-resident and live in the pauseable weights memory region. On a weight update sglang releases that region's physical memory on pause and reallocates it on resume; since the cache is a plain module global outside the saved/restored static state, its tensors are left pointing at stale memory. We drop it so the indices get recomputed.

The cachestill hits (same shape) but the contents are now garbage, so the post-update
align permutes the new weights with stale indices → corrupted layout → the 3.83
blowup. Affects any FP8 MoE on the flashinfer-trtllm path that goes through a
weight update, not just V4.

Changes

  1. Bug fix — add clear_mxfp8_shuffle_index_cache() in flashinfer_trtllm.py
    and call it from the weight-load funnel in fused_moe_triton/layer.py
    (_weight_loader_impl + weight_loader_fused, gated on Fp8MoEMethod). This
    funnel covers both the colocate and distributed/EP update paths, so the next
    align recomputes correct indices after any reload.
  2. V4 enablement — implement apply_routed_scaling_factor_on_output in
    hash_topk.py (store the flag, drop the not implemented assert, apply
    topk_weights *= routed_scaling_factor in forward), as required by the
    routed kernel.

Testing

5-step Pinaster/DeepSeek-V4-Flash-FP8-4layer MXFP8 RL run (weight update before each step):

step 0 1 2 3 4
train_rollout_logprob_abs_diff 0.069 0.062 0.068 0.065 0.062

All steps healthy (~0.06), no 3.83 blowup.


CI States

Latest PR Test (Base): ❌ Run #28472150683
Latest PR Test (Extra): ❌ Run #28472150366

Signed-off-by: zhihaow6 <[email protected]>
@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!

@xiuhu17 xiuhu17 changed the title [RL] MXFP8 Flashinfer trtllm routed MoE for V4 [RL] MXFP8 flashinfer_trtllm_routed MoE for V4 Jun 18, 2026
@xiuhu17 xiuhu17 changed the title [RL] MXFP8 flashinfer_trtllm_routed MoE for V4 [RL] fix deepseek v4 MXFP8 flashinfer_trtllm_routed MoE weight update Jun 22, 2026

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

The cache was intentionally added there as a perf optimization, otherwise the weight update can be throttled by shuffle index recompute and take several minutes (https://github.com/sgl-project/sglang/pull/21280/changes#r2984699405) .

With the cache optimization the entire weight update for mxfp8 deepseek v3 can be done in <10s. We should verify this time does not change before and after the fix.

We should consider:

  • Option 1 (I prefer): Non-colocate mode RL does not have this issue, so only clear cache if sglang offload used by colocate RL is triggered. When sglang weight is restored, flashinfer sees a cache miss and recompute and refresh cache once.
  • Option 2: Clear the cache once and recompute the cache only once per weight update cycle, so the cache is still reused within the same cycle.

@zianglih

Copy link
Copy Markdown
Contributor

Could you use this test and maybe replace update_timeout with a tight budget like 10s so ci captures perf regression in the future:

@xiuhu17

xiuhu17 commented Jun 29, 2026 •

Copy link
Copy Markdown
Contributor Author

Could you use this test and maybe replace update_timeout with a tight budget like 10s so ci captures perf regression in the future:

10s is too short for this test, 120s is enough for update weight and could pass the test

Signed-off-by: zhihaow6 <[email protected]>
@zianglih

zianglih commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

Verified no extra update overheads:

SGLANG_NSA_PREFILL_DENSE_ATTN_KV_LEN_THRESHOLD=0 python3 -m sglang.launch_server --kv-cache-dtype bf16 --model /data/models/ziangli_v32/DeepSeek-V3.2-MXFP8 --tp 8 --moe-runner-backend flashinfer_trtllm_routed --fp8-gemm-backend flashinfer_trtllm --attention-backend nsa --nsa-decode-backend flashmla_sparse --nsa-prefill-backend flashmla_sparse --page-size 64 --trust-remote-code


time curl -sS http://localhost:30000/update_weights_from_disk \
  -H 'Content-Type: application/json' \
  -d '{
    "model_path": "/data/models/ziangli_v32/DeepSeek-V3.2-MXFP8",
    "flush_cache": true,
    "abort_all_requests": false
  }'
# before
{"success":true,"message":"Succeeded to update model weights.","num_paused_requests":0}
real    0m19.853s
user    0m0.003s
sys     0m0.002s
# after
{"success":true,"message":"Succeeded to update model weights.","num_paused_requests":0}
real    0m19.200s
user    0m0.004s
sys     0m0.002s

@xiuhu17

xiuhu17 commented Jun 30, 2026

Copy link
Copy Markdown
Contributor Author

--attention-backend nsa --nsa-decode-backend flashmla_sparse --nsa-prefill-backend flashmla_sparse --page-size 64

Thanks for the benchmark!

@Fridge003

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/rl/test_update_weights_from_disk_blackwell.py

@github-actions

github-actions Bot commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/rl/test_update_weights_from_disk_blackwell.py:

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

cd test/ && python3 registered/rl/test_update_weights_from_disk_blackwell.py

@Fridge003
Fridge003 merged commit 779ea4a into sgl-project:main Jul 1, 2026
90 of 104 checks passed
vstone-w pushed a commit to ClownBin/sglang that referenced this pull request Jul 2, 2026
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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants