-
Notifications
You must be signed in to change notification settings - Fork 788
[PTQ] Store FP32 global scaling factors (absmax or scale_inv) for all quantized activations and weights. #3296
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
cspades
wants to merge
5
commits into
NVIDIA:main
Choose a base branch
from
cspades:cye/scale_factor_buffering
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
8869e46
Add optional tooling to accumulate quantization scaling factors for i…
cspades 3058e65
Add tests.
cspades ad4df80
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 5049111
Fix minor bugs.
cspades b830ba6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| # Copyright (c) 2022-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # | ||
| # See LICENSE for license information. | ||
|
|
||
| from types import SimpleNamespace | ||
|
|
||
| import pytest | ||
| import torch | ||
|
|
||
| from transformer_engine.pytorch.module import _common | ||
| from transformer_engine.pytorch.module import grouped_linear | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("recipe", "metadata_name", "expected_value"), | ||
| ( | ||
| ("fp8_current_scaling", "scale_inv", 0.25), | ||
| ("fp8_delayed_scaling", "amax", 448.0), | ||
| ("nvfp4", "amax", 2688.0), | ||
| ("nvfp4_rowwise", "amax_rowwise", 1344.0), | ||
| ), | ||
| ) | ||
| def test_scale_buffer_info_selects_recipe_metadata( | ||
| monkeypatch, recipe, metadata_name, expected_value | ||
| ): | ||
| monkeypatch.setattr(_common, "get_quantization_recipe_name", lambda _: recipe) | ||
| tensor = SimpleNamespace( | ||
| _scale_inv=torch.tensor([0.25], dtype=torch.float32), | ||
| _amax_rowwise=torch.tensor([2688.0 if recipe == "nvfp4" else 1344.0], dtype=torch.float32), | ||
| ) | ||
| quantizer = SimpleNamespace(amax=torch.tensor([448.0], dtype=torch.float32)) | ||
|
|
||
| buffer_name, value = _common._get_scale_buffer_info("input", tensor, quantizer) | ||
|
|
||
| assert buffer_name == f"input_tensor_{metadata_name}_{recipe}_te_ptq_calibrated" | ||
| torch.testing.assert_close(value, torch.tensor([expected_value])) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("recipe", ("mxfp8", "fp8_block_scaling")) | ||
| def test_scale_buffer_info_skips_non_global_scaling_recipes(monkeypatch, recipe): | ||
| monkeypatch.setattr(_common, "get_quantization_recipe_name", lambda _: recipe) | ||
| tensor = SimpleNamespace(_rowwise_scale_inv=torch.ones(2, 2)) | ||
|
|
||
| assert _common._get_scale_buffer_info("input", tensor, object()) is None | ||
|
|
||
|
|
||
| def test_grouped_scale_buffers_are_per_gemm(monkeypatch): | ||
| monkeypatch.setattr(_common, "get_quantization_recipe_name", lambda _: "fp8_current_scaling") | ||
| inputs = [ | ||
| SimpleNamespace(_scale_inv=torch.tensor([0.25])), | ||
| SimpleNamespace(_scale_inv=torch.tensor([0.5])), | ||
| ] | ||
| weights = [ | ||
| SimpleNamespace(_scale_inv=torch.tensor([0.75])), | ||
| SimpleNamespace(_scale_inv=torch.tensor([1.0])), | ||
| ] | ||
| scale_buffers = {} | ||
|
|
||
| grouped_linear._update_grouped_scale_buffers( | ||
| scale_buffers, | ||
| inputs, | ||
| weights, | ||
| object(), | ||
| object(), | ||
| activation_scale_decay=0.0, | ||
| ) | ||
|
|
||
| assert set(scale_buffers) == { | ||
| "input_gemm0_tensor_scale_inv_fp8_current_scaling_te_ptq_calibrated", | ||
| "input_gemm1_tensor_scale_inv_fp8_current_scaling_te_ptq_calibrated", | ||
| "weight_gemm0_tensor_scale_inv_fp8_current_scaling_te_ptq_calibrated", | ||
| "weight_gemm1_tensor_scale_inv_fp8_current_scaling_te_ptq_calibrated", | ||
| } | ||
| torch.testing.assert_close( | ||
| scale_buffers["input_gemm1_tensor_scale_inv_fp8_current_scaling_te_ptq_calibrated"], | ||
| torch.tensor([0.5]), | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("observed_scale", "expected_scale"), | ||
| ( | ||
| # Decayed max is greater than the observed. | ||
| (1.0, 2.0), | ||
| # Decayed max is less than the observed. | ||
| (3.0, 3.0), | ||
| ), | ||
| ) | ||
| def test_activation_scale_buffer_uses_decaying_maximum(observed_scale, expected_scale): | ||
| name = "fc1_input_tensor_scale_inv_fp8_current_scaling_te_ptq_calibrated" | ||
| scale_buffers = {name: torch.tensor([4.0])} | ||
|
|
||
| _common._update_scale_buffers( | ||
| scale_buffers, | ||
| {name: torch.tensor([observed_scale])}, | ||
| activation_scale_decay=0.5, | ||
| ) | ||
| torch.testing.assert_close(scale_buffers[name], torch.tensor([expected_scale])) |
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
Oops, something went wrong.
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.