-
Notifications
You must be signed in to change notification settings - Fork 123
ESM2 NVFP4 and MXFP8 support and documentation update. #1484
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
jomitchellnv
wants to merge
10
commits into
main
Choose a base branch
from
jm/mxfp8-nvfp4-mr-02272026
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.
+2,583
−153
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
da59e86
NVFP4 and MXFP8 integrations
734a25a
nothing
a75b5b4
adds changes to modeling - untested
e45116a
uses local file instead of automodel
jomitchellnv 5454f67
more lints
jomitchellnv 89e7d7f
adds errors instead of warnings
jomitchellnv 86a8b00
[cc] Creates layer precision map inside modeling_te file
jomitchellnv 8c53527
linting test fix
jomitchellnv c73a2a5
removes tokenizer revision
jomitchellnv 645da2b
adds to docs
jomitchellnv 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
242 changes: 242 additions & 0 deletions
242
bionemo-recipes/models/esm2/tests/test_layer_quantization.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,242 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: LicenseRef-Apache2 | ||
| # | ||
| # 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. | ||
|
|
||
| """Unit tests for NVEsmEncoder.initialize_quantization and get_layer_autocast.""" | ||
|
|
||
| from contextlib import nullcontext | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
| import transformer_engine.common.recipe | ||
| import transformer_engine.pytorch | ||
|
|
||
| from modeling_esm_te import NVEsmConfig, NVEsmEncoder | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def encoder(): | ||
| """Create a small NVEsmEncoder on CUDA for testing.""" | ||
| config = NVEsmConfig( | ||
| hidden_size=320, | ||
| intermediate_size=1280, | ||
| num_hidden_layers=6, | ||
| num_attention_heads=20, | ||
| max_position_embeddings=1026, | ||
| ) | ||
| return NVEsmEncoder(config) | ||
|
|
||
|
|
||
| class TestInitializeQuantization: | ||
| """Tests for NVEsmEncoder.initialize_quantization.""" | ||
|
|
||
| def test_all_fp8(self, encoder): | ||
| fp8_recipe = transformer_engine.common.recipe.DelayedScaling() | ||
| encoder.initialize_quantization( | ||
| fp8_layers=[0, 1, 2, 3, 4, 5], | ||
| fp4_layers=None, | ||
| fp8_recipe=fp8_recipe, | ||
| fp4_recipe=None, | ||
| ) | ||
| assert encoder._fp8_recipe is fp8_recipe | ||
| assert encoder._fp4_recipe is None | ||
| assert all(encoder._layer_precision[i] == "fp8" for i in range(6)) | ||
|
|
||
| def test_all_fp4(self, encoder): | ||
| fp4_recipe = transformer_engine.common.recipe.NVFP4BlockScaling() | ||
| encoder.initialize_quantization( | ||
| fp8_layers=None, | ||
| fp4_layers=[0, 1, 2, 3, 4, 5], | ||
| fp8_recipe=None, | ||
| fp4_recipe=fp4_recipe, | ||
| ) | ||
| assert encoder._fp8_recipe is None | ||
| assert encoder._fp4_recipe is fp4_recipe | ||
| assert all(encoder._layer_precision[i] == "fp4" for i in range(6)) | ||
|
|
||
| def test_all_bf16(self, encoder): | ||
| encoder.initialize_quantization( | ||
| fp8_layers=None, | ||
| fp4_layers=None, | ||
| fp8_recipe=None, | ||
| fp4_recipe=None, | ||
| ) | ||
| assert all(encoder._layer_precision[i] is None for i in range(6)) | ||
|
|
||
| def test_mixed_fp8_fp4(self, encoder): | ||
| fp8_recipe = transformer_engine.common.recipe.DelayedScaling() | ||
| fp4_recipe = transformer_engine.common.recipe.NVFP4BlockScaling() | ||
| encoder.initialize_quantization( | ||
| fp8_layers=[0, 1, 2], | ||
| fp4_layers=[3, 4, 5], | ||
| fp8_recipe=fp8_recipe, | ||
| fp4_recipe=fp4_recipe, | ||
| ) | ||
| for i in range(3): | ||
| assert encoder._layer_precision[i] == "fp8" | ||
| for i in range(3, 6): | ||
| assert encoder._layer_precision[i] == "fp4" | ||
|
|
||
| def test_mixed_fp8_bf16(self, encoder): | ||
| fp8_recipe = transformer_engine.common.recipe.DelayedScaling() | ||
| encoder.initialize_quantization( | ||
| fp8_layers=[0, 2, 4], | ||
| fp4_layers=None, | ||
| fp8_recipe=fp8_recipe, | ||
| fp4_recipe=None, | ||
| ) | ||
| assert encoder._layer_precision[0] == "fp8" | ||
| assert encoder._layer_precision[1] is None | ||
| assert encoder._layer_precision[2] == "fp8" | ||
| assert encoder._layer_precision[3] is None | ||
| assert encoder._layer_precision[4] == "fp8" | ||
| assert encoder._layer_precision[5] is None | ||
|
|
||
| def test_mixed_all_three(self, encoder): | ||
| fp8_recipe = transformer_engine.common.recipe.DelayedScaling() | ||
| fp4_recipe = transformer_engine.common.recipe.NVFP4BlockScaling() | ||
| encoder.initialize_quantization( | ||
| fp8_layers=[0, 1], | ||
| fp4_layers=[4, 5], | ||
| fp8_recipe=fp8_recipe, | ||
| fp4_recipe=fp4_recipe, | ||
| ) | ||
| assert encoder._layer_precision[0] == "fp8" | ||
| assert encoder._layer_precision[1] == "fp8" | ||
| assert encoder._layer_precision[2] is None # BF16 | ||
| assert encoder._layer_precision[3] is None # BF16 | ||
| assert encoder._layer_precision[4] == "fp4" | ||
| assert encoder._layer_precision[5] == "fp4" | ||
|
|
||
| def test_empty_lists_treated_as_none(self, encoder): | ||
| encoder.initialize_quantization( | ||
| fp8_layers=[], | ||
| fp4_layers=[], | ||
| fp8_recipe=None, | ||
| fp4_recipe=None, | ||
| ) | ||
| assert all(encoder._layer_precision[i] is None for i in range(6)) | ||
|
|
||
| def test_covers_all_layers(self, encoder): | ||
| encoder.initialize_quantization( | ||
| fp8_layers=[0], | ||
| fp4_layers=None, | ||
| fp8_recipe=transformer_engine.common.recipe.DelayedScaling(), | ||
| fp4_recipe=None, | ||
| ) | ||
| assert len(encoder._layer_precision) == 6 | ||
|
|
||
| def test_recipes_stored_as_attributes(self, encoder): | ||
| fp8_recipe = transformer_engine.common.recipe.DelayedScaling() | ||
| fp4_recipe = transformer_engine.common.recipe.NVFP4BlockScaling() | ||
| encoder.initialize_quantization( | ||
| fp8_layers=[0], | ||
| fp4_layers=[1], | ||
| fp8_recipe=fp8_recipe, | ||
| fp4_recipe=fp4_recipe, | ||
| ) | ||
| # Recipes are stored once, not duplicated per-layer in the map. | ||
| assert encoder._fp8_recipe is fp8_recipe | ||
| assert encoder._fp4_recipe is fp4_recipe | ||
| # The map only contains strings, not recipe objects. | ||
| for v in encoder._layer_precision.values(): | ||
| assert v is None or isinstance(v, str) | ||
|
|
||
|
|
||
| class TestGetLayerAutocast: | ||
| """Tests for NVEsmEncoder.get_layer_autocast.""" | ||
|
|
||
| def test_fp8_layer_returns_nullcontext(self, encoder): | ||
| encoder.initialize_quantization( | ||
| fp8_layers=[0], | ||
| fp4_layers=None, | ||
| fp8_recipe=transformer_engine.common.recipe.DelayedScaling(), | ||
| fp4_recipe=None, | ||
| ) | ||
| ctx = encoder.get_layer_autocast(0) | ||
| assert isinstance(ctx, nullcontext) | ||
|
|
||
| def test_fp4_layer_returns_te_autocast(self, encoder): | ||
| fp4_recipe = transformer_engine.common.recipe.NVFP4BlockScaling() | ||
| encoder.initialize_quantization( | ||
| fp8_layers=None, | ||
| fp4_layers=[0], | ||
| fp8_recipe=None, | ||
| fp4_recipe=fp4_recipe, | ||
| ) | ||
| with patch.object(transformer_engine.pytorch, "autocast") as mock_autocast: | ||
| mock_autocast.return_value = "fp4_context" | ||
| ctx = encoder.get_layer_autocast(0) | ||
| mock_autocast.assert_called_once_with(enabled=True, recipe=fp4_recipe) | ||
| assert ctx == "fp4_context" | ||
|
|
||
| def test_bf16_layer_returns_te_autocast_disabled(self, encoder): | ||
| encoder.initialize_quantization( | ||
| fp8_layers=None, | ||
| fp4_layers=None, | ||
| fp8_recipe=None, | ||
| fp4_recipe=None, | ||
| ) | ||
| with patch.object(transformer_engine.pytorch, "autocast") as mock_autocast: | ||
| mock_autocast.return_value = "bf16_context" | ||
| ctx = encoder.get_layer_autocast(0) | ||
| mock_autocast.assert_called_once_with(enabled=False) | ||
| assert ctx == "bf16_context" | ||
|
|
||
| def test_uninitialized_defaults_to_bf16(self, encoder): | ||
| """When initialize_quantization was never called, all layers default to BF16.""" | ||
| with patch.object(transformer_engine.pytorch, "autocast") as mock_autocast: | ||
| mock_autocast.return_value = "bf16_context" | ||
| ctx = encoder.get_layer_autocast(0) | ||
| mock_autocast.assert_called_once_with(enabled=False) | ||
| assert ctx == "bf16_context" | ||
|
|
||
| def test_mixed_layers_return_correct_contexts(self, encoder): | ||
| fp8_recipe = transformer_engine.common.recipe.DelayedScaling() | ||
| fp4_recipe = transformer_engine.common.recipe.NVFP4BlockScaling() | ||
| encoder.initialize_quantization( | ||
| fp8_layers=[0, 1], | ||
| fp4_layers=[2, 3], | ||
| fp8_recipe=fp8_recipe, | ||
| fp4_recipe=fp4_recipe, | ||
| ) | ||
| # FP8 layers -> nullcontext | ||
| assert isinstance(encoder.get_layer_autocast(0), nullcontext) | ||
| assert isinstance(encoder.get_layer_autocast(1), nullcontext) | ||
|
|
||
| # FP4 and BF16 layers -> te.pytorch.autocast (not nullcontext) | ||
| with patch.object(transformer_engine.pytorch, "autocast") as mock_autocast: | ||
| mock_autocast.return_value = "fp4_context" | ||
| encoder.get_layer_autocast(2) | ||
| mock_autocast.assert_called_with(enabled=True, recipe=fp4_recipe) | ||
|
|
||
| with patch.object(transformer_engine.pytorch, "autocast") as mock_autocast: | ||
| mock_autocast.return_value = "bf16_context" | ||
| encoder.get_layer_autocast(4) | ||
| mock_autocast.assert_called_with(enabled=False) | ||
|
|
||
| def test_layer_precision_map_is_pickleable(self, encoder): | ||
| """The _layer_precision map should be trivially pickleable (only strings/None).""" | ||
| import pickle | ||
|
|
||
| fp8_recipe = transformer_engine.common.recipe.DelayedScaling() | ||
| fp4_recipe = transformer_engine.common.recipe.NVFP4BlockScaling() | ||
| encoder.initialize_quantization( | ||
| fp8_layers=[0, 1], | ||
| fp4_layers=[2, 3], | ||
| fp8_recipe=fp8_recipe, | ||
| fp4_recipe=fp4_recipe, | ||
| ) | ||
| roundtripped = pickle.loads(pickle.dumps(encoder._layer_precision)) | ||
| assert roundtripped == encoder._layer_precision | ||
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.
🛠️ Refactor suggestion | 🟠 Major
Add at least one TE-vs-reference golden-value parity test in this module.
These tests cover routing/context behavior, but they do not assert numerical parity between the TE model and the reference ESM model for a fixed input/seed.
As per coding guidelines: "In bionemo-recipes/models/, create golden value tests proving that the TransformerEngine model matches the reference model".
🤖 Prompt for AI Agents