-
Notifications
You must be signed in to change notification settings - Fork 2.6k
[None][fix] Fix Qwen3 w4a8 model execution failure and add unit test (re-open of #14527) #17196
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
rosenrodt
wants to merge
5
commits into
NVIDIA:main
Choose a base branch
from
rosenrodt:mixed-w4a8-quant-fix
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.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e18606d
Fix the runtime error of w4a8 model with ConfiguragleMoE
leo0519 cf6765f
Add unit test for w4a8 model
leo0519 bbb4e6d
Add QA test for Qwen3-30B-A3B w4a8 model
leo0519 04c9586
Fix mixed W4A8 MMLU accuracy reference
rosenrodt 342b7a0
[None][fix] preserve final quant config in ConfigurableMoE
rosenrodt 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
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
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
122 changes: 122 additions & 0 deletions
122
tests/unittest/_torch/modules/fused_moe/test_configurable_moe.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,122 @@ | ||
| # 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. | ||
|
|
||
| from unittest.mock import Mock, patch | ||
|
|
||
| import torch | ||
|
|
||
| from tensorrt_llm._torch.model_config import ModelConfig | ||
| from tensorrt_llm._torch.models.modeling_utils import DecoderModelForCausalLM | ||
| from tensorrt_llm._torch.modules.fused_moe.configurable_moe import ( | ||
| _BACKEND_SYNC_ATTRS, | ||
| ConfigurableMoE, | ||
| ) | ||
| from tensorrt_llm.models.modeling_utils import QuantAlgo, QuantConfig | ||
|
|
||
|
|
||
| def _wrapper() -> ConfigurableMoE: | ||
| wrapper = ConfigurableMoE.__new__(ConfigurableMoE) | ||
| torch.nn.Module.__init__(wrapper) | ||
| wrapper.num_experts = 8 | ||
| wrapper.hidden_size = 16 | ||
| wrapper.intermediate_size = 32 | ||
| wrapper.dtype = torch.bfloat16 | ||
| wrapper.reduce_results = False | ||
| wrapper.aux_stream_dict = None | ||
| wrapper.weight_loading_mode = None | ||
| wrapper.apply_router_weight_on_input = False | ||
| wrapper.activation_type = None | ||
| wrapper._override_quant_config = None | ||
| for attr in _BACKEND_SYNC_ATTRS: | ||
| setattr(wrapper, attr, None) | ||
| return wrapper | ||
|
|
||
|
|
||
| def _create_backend( | ||
| wrapper: ConfigurableMoE, | ||
| model_config: ModelConfig, | ||
| override_quant_config: QuantConfig | None = None, | ||
| ) -> Mock: | ||
| backend = Mock() | ||
| with ( | ||
| patch( | ||
| "tensorrt_llm._torch.modules.fused_moe.create_moe.resolve_moe_cls", | ||
| return_value=Mock(), | ||
| ), | ||
| patch( | ||
| "tensorrt_llm._torch.modules.fused_moe.create_moe.create_moe_backend", | ||
| return_value=backend, | ||
| ), | ||
| ): | ||
| wrapper._create_and_sync_backend( | ||
| model_config=model_config, | ||
| routing_method=Mock(), | ||
| override_quant_config=override_quant_config, | ||
| ) | ||
| return backend | ||
|
|
||
|
|
||
| def test_layerwise_quant_config_is_applied_before_weight_creation() -> None: | ||
| global_config = QuantConfig() | ||
| layer_config = QuantConfig() | ||
| model_config = ModelConfig( | ||
| quant_config=global_config, | ||
| quant_config_dict={"model.layers.0.mlp.experts": layer_config}, | ||
| ) | ||
| wrapper = _wrapper() | ||
| wrapper.quant_config = global_config | ||
|
|
||
| backend = _create_backend(wrapper, model_config) | ||
|
|
||
| backend.create_weights.assert_not_called() | ||
| wrapper.quant_config = layer_config | ||
| wrapper.create_weights() | ||
|
|
||
| assert backend.quant_config is layer_config | ||
| backend.create_weights.assert_called_once_with() | ||
|
|
||
|
|
||
| def test_exclusions_only_recreate_matching_moe_weights() -> None: | ||
| quant_config = QuantConfig( | ||
| quant_algo=QuantAlgo.FP8, | ||
| exclude_modules=["*kv_b_proj*", "*k_b_proj*", "*eh_proj"], | ||
| ) | ||
| model_config = ModelConfig(quant_config=quant_config) | ||
| wrapper = _wrapper() | ||
| wrapper.quant_config = quant_config | ||
|
|
||
| backend = _create_backend(wrapper, model_config) | ||
|
|
||
| assert backend.quant_config is quant_config | ||
| backend.create_weights.assert_called_once_with() | ||
| backend.create_weights.reset_mock() | ||
| backend._weights_created = True | ||
|
|
||
| root = torch.nn.Module() | ||
| root.model_config = ModelConfig( | ||
| quant_config=QuantConfig( | ||
| quant_algo=QuantAlgo.FP8, | ||
| exclude_modules=["experts"], | ||
| ) | ||
| ) | ||
| root.experts = wrapper | ||
|
|
||
| DecoderModelForCausalLM.apply_quant_config_exclude_modules(root) | ||
|
|
||
| assert not backend._weights_created | ||
| wrapper.create_weights() | ||
| assert wrapper.quant_config.quant_algo is None | ||
| assert backend.quant_config.quant_algo is None | ||
| backend.create_weights.assert_called_once_with() | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Add coverage for the other quantization lifecycle branches.
This test covers
quant_config_dictdeferral. It does not cover a non-emptyexclude_modulesvalue at Line 354. It also does not verify that_override_quant_configremains authoritative at Lines 664-668.Add one focused case for exclusions and one for explicit override precedence.
Test coverage summary: insufficient. The added unit test covers layerwise configuration propagation. It does not cover all changed allocation branches. Integration test-list registration does not apply to this unit-test module.
As per path instructions, changed test code requires a coverage verdict.
🤖 Prompt for AI Agents
Source: Path instructions