-
Notifications
You must be signed in to change notification settings - Fork 2.7k
[TRTLLM-10657][fix] Resolve MIXED_PRECISION quant config for DeepSeek W4A8 MoE experts #18393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
brnguyen2
wants to merge
4
commits into
NVIDIA:main
Choose a base branch
from
brnguyen2:fix-deepseek-w4a8-mixed-precision-moe
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+195
−14
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4a80e1e
[TRTLLM-10657][fix] Resolve MIXED_PRECISION quant config for DeepSeek…
brnguyen2 60a15bc
[TRTLLM-10657][test] Cover MIXED_PRECISION expert quant resolution wi…
brnguyen2 68a733d
[TRTLLM-10657][fix] Make expert weight-loading-mode selection None-safe
brnguyen2 11c3b3e
[TRTLLM-10657][test] Relocate DeepSeek-V3 mixed-precision quant unit …
brnguyen2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
tests/unittest/_torch/modeling/test_modeling_deepseekv3.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """Per-module quant config resolution for DeepSeek MIXED_PRECISION checkpoints. | ||
|
|
||
| DeepSeek-R1-W4AFP8 ships an hf_quant_config with the global | ||
| quant_algo=MIXED_PRECISION, which does not map to a single QuantMode. The MoE | ||
| experts must resolve their own per-module config (W4A8_AWQ) instead of using | ||
| the global one. These tests exercise that resolution on CPU without weights. | ||
| """ | ||
|
|
||
| from types import SimpleNamespace | ||
|
|
||
| import pytest | ||
|
|
||
| from tensorrt_llm._torch.models.modeling_deepseekv3 import Deepseekv3MoE | ||
| from tensorrt_llm._torch.moe.fused_moe import MoEWeightLoadingMode | ||
| from tensorrt_llm._torch.moe.fused_moe.configurable_moe import ConfigurableMoE | ||
| from tensorrt_llm.models.modeling_utils import QuantConfig | ||
| from tensorrt_llm.quantization.mode import QuantAlgo | ||
|
|
||
| pytestmark = pytest.mark.cpu_only | ||
|
|
||
| EXPERTS_KEY = "model.layers.{}.mlp.experts" | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mixed_precision_config(): | ||
| return QuantConfig(quant_algo=QuantAlgo.MIXED_PRECISION) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def w4a8_awq_config(): | ||
| return QuantConfig(quant_algo=QuantAlgo.W4A8_AWQ, group_size=128) | ||
|
|
||
|
|
||
| def test_w4a8_awq_config_is_int4_weight_only_per_group(w4a8_awq_config): | ||
| # This predicate is what selects MoEWeightLoadingMode.W4A8_CUSTOM for the | ||
| # experts in Deepseekv3MoE, so pin it down explicitly. | ||
| assert w4a8_awq_config.layer_quant_mode.is_int4_weight_only_per_group() | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def test_experts_quant_config_resolved_from_per_module_dict( | ||
| mixed_precision_config, w4a8_awq_config | ||
| ): | ||
| model_config = SimpleNamespace( | ||
| quant_config=mixed_precision_config, | ||
| quant_config_dict={EXPERTS_KEY.format(0): w4a8_awq_config}, | ||
| ) | ||
|
|
||
| resolved = Deepseekv3MoE._get_experts_quant_config(model_config, 0) | ||
|
|
||
| assert resolved is w4a8_awq_config | ||
| assert resolved.quant_algo == QuantAlgo.W4A8_AWQ | ||
| assert resolved.layer_quant_mode.is_int4_weight_only_per_group() | ||
| assert not mixed_precision_config.layer_quant_mode.is_int4_weight_only_per_group() | ||
|
|
||
|
|
||
| def test_experts_quant_config_falls_back_to_global_for_unlisted_layer( | ||
| mixed_precision_config, w4a8_awq_config | ||
| ): | ||
| model_config = SimpleNamespace( | ||
| quant_config=mixed_precision_config, | ||
| quant_config_dict={EXPERTS_KEY.format(0): w4a8_awq_config}, | ||
| ) | ||
|
|
||
| assert Deepseekv3MoE._get_experts_quant_config(model_config, 1) is mixed_precision_config | ||
|
|
||
|
|
||
| def test_experts_quant_config_falls_back_to_global_without_dict(mixed_precision_config): | ||
| model_config = SimpleNamespace(quant_config=mixed_precision_config, quant_config_dict=None) | ||
|
|
||
| assert Deepseekv3MoE._get_experts_quant_config(model_config, 0) is mixed_precision_config | ||
|
|
||
|
|
||
| def test_expert_weight_loading_mode_w4a8_custom_for_w4a8_awq(w4a8_awq_config): | ||
| # The resolved W4A8_AWQ expert config selects the custom loading mode; this | ||
| # is the assignment the MoE construction makes from the resolved config. | ||
| assert ( | ||
| Deepseekv3MoE._expert_weight_loading_mode(w4a8_awq_config) | ||
| is MoEWeightLoadingMode.W4A8_CUSTOM | ||
| ) | ||
|
|
||
|
|
||
| def test_expert_weight_loading_mode_vanilla_for_non_int4(mixed_precision_config): | ||
| # Neither the ambiguous MIXED_PRECISION global nor a plain FP8 config is | ||
| # int4-weight-per-group, so both fall to VANILLA. | ||
| assert ( | ||
| Deepseekv3MoE._expert_weight_loading_mode(mixed_precision_config) | ||
| is MoEWeightLoadingMode.VANILLA | ||
| ) | ||
| assert ( | ||
| Deepseekv3MoE._expert_weight_loading_mode(QuantConfig(quant_algo=QuantAlgo.FP8)) | ||
| is MoEWeightLoadingMode.VANILLA | ||
| ) | ||
|
|
||
|
|
||
| def test_expert_weight_loading_mode_none_is_vanilla(): | ||
| # override_quant_config is Optional, so the resolved expert config is None on | ||
| # an unquantized layer; the mode selection must not dereference it. | ||
| assert Deepseekv3MoE._expert_weight_loading_mode(None) is MoEWeightLoadingMode.VANILLA | ||
|
|
||
|
|
||
| def _bare_configurable_moe(override_quant_config): | ||
| moe = object.__new__(ConfigurableMoE) | ||
| moe._override_quant_config = override_quant_config | ||
| return moe | ||
|
|
||
|
|
||
| def test_quant_config_dict_prefers_override_over_mixed_precision_global( | ||
| mixed_precision_config, w4a8_awq_config | ||
| ): | ||
| moe = _bare_configurable_moe(w4a8_awq_config) | ||
| model_config = SimpleNamespace(quant_config=mixed_precision_config) | ||
|
|
||
| result = ConfigurableMoE._get_quant_config_dict(moe, model_config) | ||
|
|
||
| assert result == { | ||
| "has_fp8_qdq": False, | ||
| "has_nvfp4": False, | ||
| "has_w4afp8": True, | ||
| "has_fp8_block_scales": False, | ||
| } | ||
| # The global MIXED_PRECISION mode would not have flagged w4afp8. | ||
| assert not mixed_precision_config.layer_quant_mode.is_int4_weight_only_per_group() | ||
|
|
||
|
|
||
| def test_quant_config_dict_falls_back_to_global_without_override(): | ||
| fp8_config = QuantConfig(quant_algo=QuantAlgo.FP8) | ||
| moe = _bare_configurable_moe(None) | ||
| model_config = SimpleNamespace(quant_config=fp8_config) | ||
|
|
||
| result = ConfigurableMoE._get_quant_config_dict(moe, model_config) | ||
|
|
||
| assert result == { | ||
| "has_fp8_qdq": True, | ||
| "has_nvfp4": False, | ||
| "has_w4afp8": False, | ||
| "has_fp8_block_scales": False, | ||
| } | ||
|
|
||
|
|
||
| def test_quant_config_dict_is_none_when_unquantized(): | ||
| moe = _bare_configurable_moe(None) | ||
| model_config = SimpleNamespace(quant_config=None) | ||
|
|
||
| assert ConfigurableMoE._get_quant_config_dict(moe, model_config) is None | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.