-
Notifications
You must be signed in to change notification settings - Fork 366
fix hybrid model subblock param counting: all FFN sizes reported identical params #1258
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
Merged
kevalmorabia97
merged 5 commits into
feature/puzzletron
from
jrausch/nemotron-h-fix-pattern-truncation
Apr 15, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cfa2585
truncate hybrid_override_pattern to match subblock layer index; fixes…
j-rausch d66fd6f
simplify pattern truncation mechanism; remove heuristic fallback
j-rausch b279014
update unit + GPU tests for pattern truncation
j-rausch de38282
update tests: type annotations, empty-pattern guard, test ordering fixes
j-rausch 9afd4a7
Fix test assertions
kevalmorabia97 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2024 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. | ||
|
|
||
| """GPU validation for Nemotron-H hybrid model subblock parameter counting. | ||
|
|
||
| Requires HuggingFace Hub access to nvidia/NVIDIA-Nemotron-Nano-12B-v2-Base (config only, | ||
| no weights are downloaded) and mamba_ssm (CUDA). | ||
|
|
||
| Usage: | ||
| pytest -v -s -o addopts= tests/gpu/puzzletron/test_nemotron_h_gpu_validation.py | ||
| """ | ||
|
|
||
| import copy | ||
|
|
||
| import pytest | ||
|
|
||
| import modelopt.torch.puzzletron.anymodel.models.nemotron_h_v2.nemotron_h_v2_model_descriptor # noqa: F401 | ||
| from modelopt.torch.puzzletron.anymodel.model_descriptor import ( | ||
| ModelDescriptor, | ||
| ModelDescriptorFactory, | ||
| ) | ||
| from modelopt.torch.puzzletron.block_config import FFNConfig | ||
| from modelopt.torch.puzzletron.subblock_stats.calc_subblock_params_and_memory import ( | ||
| calculate_subblock_params, | ||
| ) | ||
| from modelopt.torch.puzzletron.tools.checkpoint_utils import load_model_config | ||
|
|
||
| MODEL_ID = "nvidia/NVIDIA-Nemotron-Nano-12B-v2-Base" | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def nemotron_descriptor(): | ||
| return ModelDescriptorFactory.get("nemotron_h_v2") | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def nemotron_config(nemotron_descriptor): | ||
| return load_model_config( | ||
| MODEL_ID, trust_remote_code=nemotron_descriptor.requires_trust_remote_code() | ||
| ) | ||
|
|
||
|
|
||
| def test_ffn_variants_produce_distinct_params(nemotron_config, nemotron_descriptor): | ||
| """FFN subblocks with different intermediate_size must report different param counts. | ||
|
|
||
| On hybrid models, hybrid_override_pattern must be truncated to match the subblock | ||
| type; otherwise a single-layer model always builds layer 0 (Mamba) and every FFN | ||
| variant reports identical param counts. | ||
| """ | ||
| lm_config = nemotron_descriptor.get_language_model_config(nemotron_config) | ||
| pattern = lm_config.hybrid_override_pattern.replace("|", "") | ||
| ffn_indices = [i for i, c in enumerate(pattern) if c in ("-", "E")] | ||
| assert ffn_indices, f"No FFN layers in pattern: {pattern}" | ||
|
|
||
| teacher_size = lm_config.intermediate_size | ||
| sizes = [teacher_size // 4, teacher_size // 2, teacher_size] | ||
|
|
||
| param_counts = {} | ||
| for size in sizes: | ||
| layer_config = copy.deepcopy(nemotron_config) | ||
| ModelDescriptor.truncate_pattern_for_subblock( | ||
| nemotron_descriptor.get_language_model_config(layer_config), ffn_indices[0] | ||
| ) | ||
|
|
||
| params = calculate_subblock_params( | ||
| layer_config, FFNConfig(intermediate_size=size), nemotron_descriptor | ||
| ) | ||
| param_counts[size] = params | ||
| print(f" intermediate_size={size:>8d} -> params={params:>12,}") | ||
|
|
||
| assert len(set(param_counts.values())) == len(sizes), ( | ||
| f"Expected {len(sizes)} distinct param counts, got: {param_counts}" | ||
| ) |
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
115 changes: 115 additions & 0 deletions
115
tests/unit/torch/puzzletron/test_hybrid_pattern_truncation.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,115 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2024 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. | ||
|
|
||
| """Tests for ModelDescriptor.truncate_pattern_for_subblock. | ||
|
|
||
| Validates that the base descriptor method selects the correct pattern | ||
| character when building a 1-layer model for per-subblock param counting. | ||
| """ | ||
|
|
||
| from types import SimpleNamespace | ||
|
|
||
| import pytest | ||
|
|
||
| pytest.importorskip("transformers") | ||
|
|
||
| from modelopt.torch.puzzletron.anymodel.model_descriptor import ModelDescriptor | ||
|
|
||
| NEMOTRON_H_PATTERN = "M-M-M-M*-M-M-M-M-M*-M-M-M-M-M*-M-M-M-M-M*-M-M-M-M-M-" | ||
|
|
||
|
|
||
| class TestTruncatePatternForSubblock: | ||
| """Test ModelDescriptor.truncate_pattern_for_subblock.""" | ||
|
|
||
| @pytest.mark.parametrize( | ||
| ("index", "expected"), | ||
| [ | ||
| (0, "M"), | ||
| (1, "-"), | ||
| (7, "*"), | ||
| ], | ||
| ids=["mamba", "ffn", "attention"], | ||
| ) | ||
| def test_index_selects_correct_layer_type(self, index, expected): | ||
| """Parent layer index selects the matching character from the pattern.""" | ||
| cfg = _make_config() | ||
|
|
||
| ModelDescriptor.truncate_pattern_for_subblock(cfg, parent_layer_index=index) | ||
|
|
||
| assert cfg.hybrid_override_pattern == expected | ||
|
|
||
| @pytest.mark.parametrize( | ||
| ("index", "expected"), | ||
| [ | ||
| (1, "-"), | ||
| (2, "*"), | ||
| ], | ||
| ids=["ffn_after_strip", "attention_after_strip"], | ||
| ) | ||
| def test_pipe_separators_stripped_before_indexing(self, index, expected): | ||
| """Pipe-delimited patterns like 'M|-|*' are normalised to 'M-*' before lookup.""" | ||
| cfg = _make_config("M|-|*") | ||
|
|
||
| ModelDescriptor.truncate_pattern_for_subblock(cfg, parent_layer_index=index) | ||
|
|
||
| assert cfg.hybrid_override_pattern == expected | ||
|
|
||
| def test_missing_attribute_is_noop(self): | ||
| """Config without hybrid_override_pattern is left unchanged.""" | ||
| cfg = SimpleNamespace() | ||
|
|
||
| ModelDescriptor.truncate_pattern_for_subblock(cfg, parent_layer_index=0) | ||
|
|
||
| assert not hasattr(cfg, "hybrid_override_pattern") | ||
|
|
||
| def test_empty_pattern_is_noop(self): | ||
| """Empty pattern string is left unchanged.""" | ||
| cfg = _make_config("") | ||
|
|
||
| ModelDescriptor.truncate_pattern_for_subblock(cfg, parent_layer_index=0) | ||
|
|
||
| assert cfg.hybrid_override_pattern == "" | ||
|
|
||
| def test_pipes_only_pattern_raises(self): | ||
| """Pattern with only pipe separators has no layer-type characters and should error.""" | ||
| cfg = _make_config("|||") | ||
|
|
||
| with pytest.raises(ValueError, match="no layer-type characters"): | ||
| ModelDescriptor.truncate_pattern_for_subblock(cfg, parent_layer_index=0) | ||
|
|
||
| def test_none_index_defaults_to_first_char(self): | ||
| """Without an explicit index, defaults to pattern[0].""" | ||
| cfg = _make_config("*-M") | ||
|
|
||
| ModelDescriptor.truncate_pattern_for_subblock(cfg) | ||
|
|
||
| assert cfg.hybrid_override_pattern == "*" | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "index", | ||
| [999, -1], | ||
| ids=["above_range", "negative"], | ||
| ) | ||
| def test_out_of_range_index_defaults_to_first_char(self, index): | ||
| """Out-of-range index defaults to pattern[0].""" | ||
| cfg = _make_config("*-M") | ||
|
|
||
| ModelDescriptor.truncate_pattern_for_subblock(cfg, parent_layer_index=index) | ||
|
|
||
| assert cfg.hybrid_override_pattern == "*" | ||
|
|
||
|
|
||
| def _make_config(pattern=NEMOTRON_H_PATTERN): | ||
| return SimpleNamespace(hybrid_override_pattern=pattern) |
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.