Skip to content

Add MXFP4 (including Quark W4A4) quantization support for DeepSeek-architecture on ROCm - #21529

Closed
JohnQinAMD wants to merge 1 commit into
sgl-project:mainfrom
JohnQinAMD:fix/mxfp4-deepseek-support
Closed

JohnQinAMD wants to merge 1 commit into
sgl-project:mainfrom
JohnQinAMD:fix/mxfp4-deepseek-support

Conversation

@JohnQinAMD

Copy link
Copy Markdown
Contributor

Enable MXFP4 (including Quark W4A4) quantized models (e.g. GLM-5-MXFP4-Q8, Kimi-K2.5-MXFP4) to load and run on AMD MI355X with ROCm. These models use Quark's W4A4 MXFP4 scheme with block-scaled FP4 for MoE expert weights and BF16 for attention.

Key changes:

mxfp4.py:

  • Return None instead of raising NotImplementedError for attention layers (attention uses BF16 weights, not MXFP4)

fused_moe_triton/layer.py:

  • Skip weight transpose for packed uint8 (MXFP4) tensors during loading
  • Pre-compute expert_mask_gpu in init for CUDA graph compatibility
  • Fix expert_id None check (was not expert_id which fails for id=0)

quark/:

  • Fix Quark MXFP4 MoE scheme weight creation and loading for block-scaled format with proper shard handling
  • Add per-expert weight processing support

deepseek_v2.py:

  • Set packed_modules_mapping for Quark weight loader compatibility
  • Disable shared expert fusion for MXFP4 serialized models (routed experts are quantized but shared experts remain BF16)
  • Disable shared expert fusion for Quark models (mixed precision)

deepseek_weight_loader.py:

  • Handle fused_qkv_a_proj_with_mqa weight mapping for Quark checkpoints

Motivation

Modifications

Accuracy Tests

Benchmarking and Profiling

Checklist

Review Process

  1. Ping Merge Oncalls to start the PR flow. 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.
    • /tag-run-ci-label, /rerun-failed-ci, /tag-and-rerun-ci
  4. After green CI and required approvals, ask Merge Oncalls to merge.

… models

Enable MXFP4 quantized models (e.g. GLM-5-MXFP4-Q8, Kimi-K2.5-MXFP4) to
load and run on AMD MI355X with ROCm. These models use Quark's W4A4 MXFP4
scheme with block-scaled FP4 for MoE expert weights and BF16 for attention.

Key changes:

mxfp4.py:
- Return None instead of raising NotImplementedError for attention layers
  (attention uses BF16 weights, not MXFP4)

fused_moe_triton/layer.py:
- Skip weight transpose for packed uint8 (MXFP4) tensors during loading
- Pre-compute expert_mask_gpu in __init__ for CUDA graph compatibility
- Fix expert_id None check (was `not expert_id` which fails for id=0)

quark/:
- Fix Quark MXFP4 MoE scheme weight creation and loading for block-scaled
  format with proper shard handling
- Add per-expert weight processing support

deepseek_v2.py:
- Set packed_modules_mapping for Quark weight loader compatibility
- Disable shared expert fusion for MXFP4 serialized models (routed experts
  are quantized but shared experts remain BF16)
- Disable shared expert fusion for Quark models (mixed precision)

deepseek_weight_loader.py:
- Handle fused_qkv_a_proj_with_mqa weight mapping for Quark checkpoints

Made-with: Cursor
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

Gemini is experiencing higher than usual traffic and was unable to create the review. Please try again in a few hours by commenting /gemini review.

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

The deepseek mxfp4 models, such as https://huggingface.co/amd/DeepSeek-R1-MXFP4 and https://huggingface.co/amd/DeepSeek-R1-0528-MXFP4, are well supported by SGLang. I'm not sure what additional functionality this PR is intended to provide.

else ActivationType.Gelu
),
doweight_stage1=False,
activation=ActivationType.Swiglu,

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.

is this correct to fix as swiglu?

h, b, d = x.shape
x, x_scales = dynamic_mxfp4_quant(x.reshape(-1, d))
return x.view(h, b, d // 2), x_scales.view(h, b, d // 32)
x_out = x.view(h, b, d // 2).contiguous()

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.

are these necessary changes?

layer.register_parameter("w2_weight_scale", w2_weight_scale)

# WEIGHT_BIAS (zeros, matching standard mxfp4 path for CUDA graph compat)
w13_weight_bias = torch.nn.Parameter(

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.

not all models have biases, could we make this conditional?

self.intermediate_pad = 0

def process_weights_after_loading(self, layer: torch.nn.Module) -> None:
float_dtype = torch.get_default_dtype()

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.

I'm not sure about the shuffling as well as activation changes.

@github-actions

Copy link
Copy Markdown
Contributor

Thanks @JohnQinAMD. Closing this because it has had no updates in 151 days.

Reopen it if the work is still relevant.

Some directories moved recently, so an older branch may need retargeting:
sgl-kernel/ -> python/sglang/kernels/aot/, python/sglang/jit_kernel/
-> python/sglang/kernels/jit/, docs/ -> docs/docs/ (.mdx),
bench_serving.py -> benchmark/serving.py, test/srt/ -> test/registered/.

@github-actions github-actions Bot closed this Aug 26, 2026
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