olka-fi's picture
MXFP4 (mixed precision) quantization of inclusionAI/Ling-3.0-flash via qstream
3998b42 verified
|
Raw History Blame Contribute Delete
3.34 kB

Local patch: keep the per-layer SwiGLU clamp on the compressed-tensors MXFP4 MoE path

Applied on top of inclusionAI/vllm@ling_3_0 (e0040c3 "[Model] Add Bailing V3 support").

The problem

Ling-3.0-flash declares a per-layer clamped SwiGLU:

"expert_swiglu_limit_list":       [0]*35 + [4]*7,     // layers 35-41
"share_expert_swiglu_limit_list": [0]*34 + [5]*6 + [7]*2

bailing_moe_v3.py reads it correctly (_get_layer_swiglu_limit) and passes swiglu_limit= into FusedMoEFactory, which stores it on RoutedExperts.swiglu_limit. The unquantized path then honours it — unquantized_fused_moe_method.py reads getattr(layer, "swiglu_limit", None).

The compressed-tensors MXFP4 path did not. Three independent gaps, all of which had to be closed for the value to reach the kernel:

  1. CompressedTensorsW4A4Mxfp4MoEMethod.get_fused_moe_quant_config never read layer.swiglu_limit.
  2. mxfp4_moe_quant_config (the W4A4 cutlass builder) had no gemm1_clamp_limit parameter at all, so even a caller that wanted to pass one could not.
  3. run_cutlass_moe_mxfp4 took no clamp argument and called apply_moe_activation(activation, c2, c1) bare. For MoEActivation.SILU it also takes a fused ops.silu_and_mul_mxfp4_experts_quant fast path that has no clamp variant.

Net effect: on an MXFP4 checkpoint, layers 35-41 computed unclamped SwiGLU while BF16 clamped them. The divergence is large, not marginal — for a gate activation of 10.0, silu(10)*2 = 20.0 against silu(4)*2 = 7.84.

This is not specific to our quantization: any MXFP4 compressed-tensors checkpoint of a model with a SwiGLU limit hits it (gpt-oss-style limit=7.0 models included).

The change

File Change
vllm/model_executor/layers/fused_moe/config.py mxfp4_moe_quant_config accepts gemm1_clamp_limit and forwards it to FusedMoEQuantConfig.make (which already supported it).
.../compressed_tensors_moe/compressed_tensors_moe_w4a4_mxfp4.py get_fused_moe_quant_config reads getattr(layer, "swiglu_limit", None) and passes it down both the cutlass (gemm1_clamp_limit=) and Marlin (swiglu_limit=) branches.
vllm/model_executor/layers/fused_moe/experts/cutlass_moe.py run_cutlass_moe_mxfp4 takes swiglu_limit; when set, SILU falls out of the fused fast path into apply_moe_activation(..., clamp_limit=...). CutlassExpertsMxfp4.apply passes self.quant_config.gemm1_clamp_limit.

c2 and workspace2 are allocated unconditionally (workspace2 = (M * topk, N)), so the generic path is safe to take for any activation.

Models that declare no limit are unaffected: swiglu_limit stays None, the quant config field stays None, and SILU keeps the fused fast path.

Verification

mxfp4_moe_quant_config(..., gemm1_clamp_limit=4.0).gemm1_clamp_limit  -> 4.0
mxfp4_moe_quant_config(...)                       .gemm1_clamp_limit  -> None
get_fused_moe_quant_config(layer.swiglu_limit=4.0).gemm1_clamp_limit  -> 4.0
get_fused_moe_quant_config(layer.swiglu_limit=None).gemm1_clamp_limit -> None
apply_moe_activation(SILU, x=[10,-10,2,1])                -> [20.0,   -0.00045]
apply_moe_activation(SILU, x=[10,-10,2,1], clamp_limit=4) -> [ 7.844, -0.00045]

All four edits are pure Python, so an editable install picks them up with no rebuild.