Skip to content

[AMD] Fix DeepSeek-V4 FP4 MoE expert memory bloat - #31450

Merged
HaiShaw merged 1 commit into
sgl-project:mainfrom
1am9trash:v4-mlp-memory-issue
Aug 2, 2026
Merged

HaiShaw merged 1 commit into
sgl-project:mainfrom
1am9trash:v4-mlp-memory-issue

Conversation

@1am9trash

@1am9trash 1am9trash commented Jul 16, 2026 •

Copy link
Copy Markdown
Collaborator

Need aiter version bump before merge.
Needed aiter: PR#4130

Motivation

On AMD M355 each layer's MoE weights take 2.105 GB, while the same layer on NVIDIA B200 is 1.582 GB (~25% gap).
The cause is that on AMD we pad the w13 expert weight up to a multiple of 256.
For DeepSeek-V4-Pro on 8 GPUs (TP8) the per-partition intermediate size is 3072 / 8 = 384, which gets rounded up to 512, inflating the routed-expert weights by ~1/3.

After this change, per-layer per-rank MoE drops to 1.592 GiB (close to NV's 1.582 GiB), reducing total model weights by ~40 GiB.

Modifications

Change fp4_k_align from 256 to 128 in process_weights_after_loading_block_quant().
DeepSeek-V4 MoE runs on the FlyDSL kernel, which supports tile_k=128, so K only needs to be padded to a multiple of 128. 384 stays 384 instead of 512.

This cannot affect other models: the code path is guarded by

# python/sglang/srt/layers/quantization/fp8.py
if _use_aiter and self.is_fp4_expert:

and is_fp4_expert is only set for DeepSeek-V4:

# python/sglang/srt/configs/model_config.py
if is_deepseek_v4(self.hf_config):
    self.is_fp4_experts = envs.SGLANG_DSV4_FP4_EXPERTS.get()

This change also requires cherry-picking aiter PR#4130 (merged): the shuffle_scale in the currently pinned aiter only supports 256-padded shapes; non-256 shapes are supported starting from that PR.

Accuracy Tests

GSM8k:

  • tp8:
    100%|██████████| 1319/1319 [00:52<00:00, 25.03it/s]
    Accuracy: 0.940
    Invalid: 0.000
    Latency: 52.696 s
    Output throughput: 2204.778 token/s
    
  • tp8+dp8:
    100%|██████████| 1319/1319 [00:58<00:00, 22.64it/s]
    Accuracy: 0.942
    Invalid: 0.000
    Latency: 58.267 s
    Output throughput: 2021.708 token/s
    

Speed Tests and Profiling

Client Cmd
python3 -m sglang.bench_serving \
  --backend sglang-oai --base-url http://localhost:8000 \
  --model DeepSeek-V4-Pro \
  --dataset-name random --random-input-len 8192 --random-output-len 1024 \
  --random-range-ratio 0.8 \
  --num-prompts 64 --max-concurrency 8 \
  --warmup-requests 16 --request-rate inf

8k1k, cc8, random=0.8 (no regression)

config mem after loading weight (GB) ITL TTFT TTT
before PR tp8 159.07 15.84 315.41 3860.72
after PR 112.36 15.81 319.31 3863.6

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 #29491528112
Latest PR Test (Extra): ✅ Run #29491540358

@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 FP4 MoE alignment parameter fp4_k_align from 256 to 128 in fp8.py. This change leverages the FlyDSL kernel's support for tile_k=128 to avoid unnecessary padding (e.g., skipping padding from 384 to 512 for DeepSeek-V4-Pro at TP8), thereby reducing routed-expert memory usage by approximately 25%. There are no review comments, so I have no feedback to provide.

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.

@kkHuang-amd kkHuang-amd 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.

LGTM

@HaiShaw

HaiShaw commented Jul 16, 2026

Copy link
Copy Markdown
Collaborator

@bingxche please check on aiter upgrade.
@1am9trash for speed/perf difference before and after?

@1am9trash

Copy link
Copy Markdown
Collaborator Author

@bingxche please check on aiter upgrade. @1am9trash for speed/perf difference before and after?

I will do more perf check on it.
Update soon.

@1am9trash

1am9trash commented Jul 17, 2026 •

Copy link
Copy Markdown
Collaborator Author

Hi, @HaiShaw

w/ vs. w/o the moe intermediate padding (inter_dim padded to 512 vs kept at 384):

  • send_one test: no noticeable difference.
    • w/ padding: 78.93 tokens/s
    • w/o padding: 79.95 tokens/s
  • Small concurrency (e.g. <cc16): no noticeable difference.
  • Larger concurrency (e.g. cc16): ~1% perf drop.
image

Capture the torch kernel tracing. The difference comes mainly from moe1 (stage-1):

padding kernel avg time
w/ padding (inter=512) mfma_moe1_silu_mul_afp8_wfp4_fp8_t32x128x256_pm1_fp8q_sort_async_gui_v32 35.15 us
w/ padding (inter=512) mfma_moe2_afp8_wfp4_bf16_cshuffle_t32x256x256_vscale_fix3_fp4opt_v1_pm1_acc0 21.95 us
w/o padding (inter=384) mfma_moe1_silu_mul_afp8_wfp4_fp8_t32x64x256_pm1_fp8q_sort_async_kw2_gui_v32 39.31 us
w/o padding (inter=384) mfma_moe2_afp8_wfp4_bf16_cshuffle_t32x256x128_vscale_fix3_fp4opt_v1_pm1_acc0 20.79 us

@1am9trash

Copy link
Copy Markdown
Collaborator Author

Also try to tuning the moe for new shape (dim=384), and found it has been tuned and added in aiter/configs/model_configs/dsv4_fp8fp4_tuned_fmoe.csv.

image

@HaiShaw

HaiShaw commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator

@bingxche would you please help aiter scout to land aiter commit #id after PR#4130. Urgent.

@bingxche

bingxche commented Jul 22, 2026 •

Copy link
Copy Markdown
Collaborator

@bingxche would you please help aiter scout to land aiter commit #id after PR#4130. Urgent.

Sure, I found a suspect AITER commit that regress our DSv32 and DSv4-pro in the latest AITER scout, will upgrade AITER once resolve this.

@HaiShaw

HaiShaw commented Jul 29, 2026 •

Copy link
Copy Markdown
Collaborator

@bingxche aiter scout green?

@HaiShaw
HaiShaw merged commit 7e509f6 into sgl-project:main Aug 2, 2026
113 of 147 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants