Support kimi k3 nvfp4 checkpoint (#53132)

Signed-off-by: wzhao18 <wzhao18.sz@gmail.com>
Co-authored-by: OpenAI Codex <codex@openai.com>
This commit is contained in:
Wei Zhao
2026-08-21 05:59:44 +00:00
committed by GitHub
co-authored by OpenAI Codex
parent b389ac2946
commit f8e0602713
6 changed files with 151 additions and 35 deletions
@@ -70,16 +70,9 @@ class TrtLlmNvFp4ExpertsBase:
)
self.local_num_experts = moe_config.num_local_experts
self.ep_rank = moe_config.moe_parallel_config.ep_rank
self.is_situ = moe_config.activation == MoEActivation.SITU
assert self.quant_config.g1_alphas is not None
assert self.quant_config.a2_gscale is not None
if moe_config.is_act_and_mul:
# g1_alpha_s = a13_scale * w13_scale_2
# a2_gscale = (1 / a2_scale)
# g1_scale_c = a13_scale * w13_scale_2 / a2_scale
self.g1_scale_c = self.quant_config.g1_alphas * self.quant_config.a2_gscale
else:
self.g1_scale_c = self.quant_config.a2_gscale.clone()
self.g1_scale_c = self._compute_g1_scale_c()
# Fall back to moe_config.swiglu_* when quant_config doesn't carry them
# (ModelOpt NVFP4 checkpoints store these on moe_config, not quant_config).
@@ -120,7 +113,6 @@ class TrtLlmNvFp4ExpertsBase:
# (gemm1_alpha) and situ linear_beta -> gatedActBeta (gemm1_beta).
# These operate on the dequantized gate/up, so they are NOT folded by
# g1_alphas in process_weights_after_loading.
self.is_situ = moe_config.activation == MoEActivation.SITU
if self.is_situ:
situ_beta = moe_config.activation_situ_beta
situ_linear_beta = moe_config.activation_situ_linear_beta
@@ -143,18 +135,28 @@ class TrtLlmNvFp4ExpertsBase:
clamp,
)
def _compute_g1_scale_c(self) -> torch.Tensor:
assert self.quant_config.g1_alphas is not None
assert self.quant_config.a2_gscale is not None
if not self.moe_config.is_act_and_mul:
return self.quant_config.a2_gscale.clone()
if self.is_situ:
# SITU applies its nonlinear activation after g1_alphas, so only
# the output quantization factor belongs in g1_scale_c.
return self.quant_config.a2_gscale.clone()
# g1_alphas = a13_scale * w13_scale_2
# a2_gscale = 1 / a2_scale
# g1_scale_c = a13_scale * w13_scale_2 / a2_scale
return self.quant_config.g1_alphas * self.quant_config.a2_gscale
def process_weights_after_loading(self, layer: torch.nn.Module) -> None:
layer.w13_weight_scale_2.data.mul_(layer.w13_input_scale)
layer.w2_weight_scale_2.data.mul_(layer.w2_input_scale)
# Recompute g1_scale_c since g1_alphas was just fused in-place.
# Register as a layer parameter so EPLB rearranges it alongside
# other expert weights.
assert self.quant_config.g1_alphas is not None
assert self.quant_config.a2_gscale is not None
if self.moe_config.is_act_and_mul:
g1_scale_c = self.quant_config.g1_alphas * self.quant_config.a2_gscale
else:
g1_scale_c = self.quant_config.a2_gscale.clone()
g1_scale_c = self._compute_g1_scale_c()
layer.register_parameter(
"g1_scale_c",
torch.nn.Parameter(g1_scale_c, requires_grad=False),