[Bugfix][Quantization] Refresh online NVFP4 scales before reload post-processing (#57954)

Signed-off-by: S1ro1 <matej.sirovatka@gmail.com>
Signed-off-by: aoshen02 <aoshen02@users.noreply.github.com>
Co-authored-by: aoshen02 <aoshen02@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: aoshen02 <aoshen@inferact.ai>
This commit is contained in:
Matej Sirovatka
2026-09-24 18:36:08 -07:00
committed by GitHub
co-authored by aoshen02 Claude Opus 5 aoshen02
parent 16070ed7e9
commit 3652f35e7c
4 changed files with 104 additions and 20 deletions
@@ -277,6 +277,60 @@ def test_trtllm_fp4_moe_no_graph(
torch.testing.assert_close(torch_output, trtllm_output, atol=2e-1, rtol=2e-1)
@torch.inference_mode()
def test_trtllm_fp4_moe_reprocess_does_not_refold_swiglu_params():
"""Weight reloads rerun post-processing on the same experts object; the
g1_alphas fold of clamp/beta must start from the unfolded values."""
e, n, k = 8, 256, 256
clamp, beta = 7.0, 1.0
with set_current_vllm_config(
VllmConfig(parallel_config=ParallelConfig(pipeline_parallel_size=1))
):
_, _, quant_config = make_test_quant_config(
e,
n,
k,
in_dtype=torch.bfloat16,
quant_dtype="nvfp4",
block_shape=None,
per_act_token_quant=False,
is_scale_swizzled=False,
)
quant_config.gemm1_clamp_limit = clamp
quant_config.gemm1_beta = beta
moe_config = FusedMoEConfig(
num_experts=e,
experts_per_token=2,
hidden_dim=k,
intermediate_size=n,
num_local_experts=e,
num_logical_experts=e,
activation=MoEActivation.SILU,
device="cuda",
moe_parallel_config=FusedMoEParallelConfig.make_no_parallel(),
in_dtype=torch.bfloat16,
routing_method=RoutingMethodType.TopK,
max_num_tokens=16,
)
experts = TrtLlmNvFp4ExpertsModular(
moe_config=moe_config, quant_config=quant_config
)
g1_alphas = quant_config.g1_alphas
assert g1_alphas is not None
layer = torch.nn.Module()
layer.w13_weight_scale_2 = g1_alphas
layer.w2_weight_scale_2 = quant_config.g2_alphas
layer.w13_input_scale = torch.ones_like(g1_alphas)
layer.w2_input_scale = torch.ones_like(g1_alphas)
for new_alphas in (0.5, 0.25):
g1_alphas.fill_(new_alphas)
experts.process_weights_after_loading(layer)
expected = torch.full_like(g1_alphas, 1.0 / new_alphas)
torch.testing.assert_close(experts.gemm1_clamp_limit, clamp * expected)
torch.testing.assert_close(experts.gemm1_beta, beta * expected)
if __name__ == "__main__":
test_trtllm_fp4_moe_no_graph(
64,
+29 -8
View File
@@ -216,16 +216,29 @@ def test_online_nvfp4_reuses_kernel_when_weights_are_reprocessed(
method.moe_kernel = None
layer = Mock()
converted_weights = tuple(object() for _ in range(8))
convert_weights = Mock(return_value=converted_weights)
process_weights = Mock()
converted_weights = [
tuple(torch.full((2,), value + index) for index in range(8))
for value in (1.0, 2.0, 0.5)
]
convert_weights = Mock(side_effect=converted_weights)
quant_config = SimpleNamespace(
g1_alphas=converted_weights[0][2].clone(),
g2_alphas=converted_weights[0][6].clone(),
)
alpha_ptrs = (quant_config.g1_alphas.data_ptr(), quant_config.g2_alphas.data_ptr())
observed_scales = []
process_weights = Mock(
side_effect=lambda _: observed_scales.append(
(quant_config.g1_alphas.clone(), quant_config.g2_alphas.clone())
)
)
kernel = SimpleNamespace(
fused_experts=SimpleNamespace(
process_weights_after_loading=process_weights,
)
)
make_kernel = Mock(return_value=kernel)
get_quant_config = Mock(return_value=object())
get_quant_config = Mock(return_value=quant_config)
method.get_fused_moe_quant_config = get_quant_config
monkeypatch.setattr(
@@ -242,14 +255,22 @@ def test_online_nvfp4_reuses_kernel_when_weights_are_reprocessed(
make_kernel,
)
method._setup_kernel(layer)
method._setup_kernel(layer)
for weights in converted_weights:
method._setup_kernel(layer)
# Expert post-processing must observe fresh scales, with stable storage.
torch.testing.assert_close(observed_scales[-1][0], weights[2])
torch.testing.assert_close(observed_scales[-1][1], weights[6])
assert method.moe_quant_config is quant_config
assert (
quant_config.g1_alphas.data_ptr(),
quant_config.g2_alphas.data_ptr(),
) == alpha_ptrs
assert method.moe_kernel is kernel
assert convert_weights.call_count == 2
assert convert_weights.call_count == 3
make_kernel.assert_called_once()
get_quant_config.assert_called_once()
assert process_weights.call_count == 2
assert process_weights.call_count == 3
def _fully_quantized_quark_config() -> QuarkConfig: