-
Notifications
You must be signed in to change notification settings - Fork 247
Feat (utils): replace weights with quantized ones #1505
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
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5b9965c
Feat (utils): replace weights with quantized ones
Giuseppe5 2fb94d0
cleanup
Giuseppe5 40ef943
Fixed tests
Giuseppe5 580abc7
Review
Giuseppe5 a4fa28a
update tests
Giuseppe5 7ca7fa4
Update utils.py
Giuseppe5 bad3853
use pre-existing function
Giuseppe5 5ad1862
Merge branch 'merge_ln' of https://github.com/Giuseppe5/brevitas into…
Giuseppe5 633f38f
precommit
Giuseppe5 95befc6
Update utils.py
Giuseppe5 b8a45e1
Update utils.py
Giuseppe5 7eb0b1c
Update utils.py
Giuseppe5 69d07e1
Update utils.py
Giuseppe5 6a24032
Update utils.py
Giuseppe5 850a8c9
pre-=commit
Giuseppe5 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| # Copyright (C) 2026, Advanced Micro Devices, Inc. All rights reserved. | ||
| # SPDX-License-Identifier: BSD-3-Clause | ||
|
|
||
| import pytest | ||
| import torch | ||
|
|
||
| from brevitas.core.function_wrapper.learned_round import LearnedRoundSte | ||
| from brevitas.core.scaling import ParameterFromStatsFromParameterScaling | ||
| from brevitas.inject.enum import FloatToIntImplType | ||
| from brevitas.inject.enum import LearnedRoundImplType | ||
| from brevitas.nn import QuantLinear | ||
| from brevitas.nn.utils import merge_quant_weights | ||
| from brevitas.quant_tensor import QuantTensor | ||
| from brevitas_examples.common.learned_round.learned_round_method import \ | ||
| insert_learned_round_quantizers | ||
| from tests.conftest import SEED | ||
|
|
||
| IN_FEATURES = 8 | ||
| OUT_FEATURES = 16 | ||
|
|
||
| LEARNED_ROUND_OPTIONS = [ | ||
| LearnedRoundImplType.HARD_SIGMOID, LearnedRoundImplType.SIGMOID, LearnedRoundImplType.IDENTITY] | ||
|
|
||
|
|
||
| def _get_quant_weights(model): | ||
| """Get the quantised weight outputs for all QuantLinear layers in the model.""" | ||
| results = {} | ||
| for name, module in model.named_modules(): | ||
| if isinstance(module, QuantLinear): | ||
| quant_weight = module.quant_weight() | ||
| if isinstance(quant_weight, QuantTensor): | ||
| quant_weight = quant_weight.value | ||
| results[name] = quant_weight.detach().clone() | ||
| return results | ||
|
|
||
|
|
||
| def _randomise_learned_round(model): | ||
| """Randomise learned round values to simulate training.""" | ||
| for module in model.modules(): | ||
| if isinstance(module, LearnedRoundSte): | ||
| module.value.data = torch.randn_like(module.value.data) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("learned_round_param", LEARNED_ROUND_OPTIONS) | ||
| def test_merge_quant_weights_preserves_quantised_weights(learned_round_param): | ||
| """After merging, standard round should preserve the quantised weights, remove learned | ||
| round and its forward hooks, and reset the rounding mode to ROUND.""" | ||
| torch.manual_seed(SEED) | ||
| model = QuantLinear(in_features=IN_FEATURES, out_features=OUT_FEATURES, bias=False) | ||
| model.eval() | ||
| insert_learned_round_quantizers(model, learned_round_param) | ||
| assert model.weight_quant.rounding_mode == "LEARNED_ROUND" | ||
|
|
||
| _randomise_learned_round(model) | ||
| model.eval() | ||
|
|
||
| # Get quantised weights with learned round active | ||
| quant_before = _get_quant_weights(model) | ||
| hooks_before = len(model._forward_hooks) | ||
|
|
||
| # Merge learned round into weights | ||
| x = torch.randn(4, IN_FEATURES) | ||
| merge_quant_weights(model, x) | ||
|
|
||
| # Verify that learned round has been removed | ||
| for module in model.modules(): | ||
| assert not isinstance(module, LearnedRoundSte), \ | ||
| "LearnedRoundSte should be removed after merge" | ||
|
|
||
| # Verify that the merge's forward hooks were cleaned up | ||
| hooks_after = len(model._forward_hooks) | ||
| assert hooks_after == hooks_before, "Forward hooks were not cleaned up after merge" | ||
|
|
||
| # Verify that the rounding mode has been reset to standard round | ||
| assert isinstance( | ||
| model.weight_quant.tensor_quant.scaling_impl, ParameterFromStatsFromParameterScaling) | ||
| assert model.weight_quant.rounding_mode == "ROUND" | ||
|
|
||
| # The quantised outputs should match | ||
| quant_after = _get_quant_weights(model) | ||
| for name in quant_before: | ||
| assert torch.allclose(quant_before[name], quant_after[name], atol=1e-6), \ | ||
| f"Quantised weights differ for {name} after merge" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("learned_round_param", LEARNED_ROUND_OPTIONS) | ||
| def test_merge_quant_weights_forward_equivalence(learned_round_param): | ||
|
Giuseppe5 marked this conversation as resolved.
|
||
| """The model forward output should be identical before and after merging.""" | ||
| torch.manual_seed(SEED) | ||
| model = QuantLinear(in_features=IN_FEATURES, out_features=OUT_FEATURES, bias=True) | ||
| model.eval() | ||
|
|
||
| insert_learned_round_quantizers(model, learned_round_param) | ||
| _randomise_learned_round(model) | ||
|
|
||
| model.eval() | ||
| x = torch.randn(4, IN_FEATURES) | ||
|
|
||
| with torch.no_grad(): | ||
| out_before = model(x).clone() | ||
|
|
||
| merge_quant_weights(model, x) | ||
|
|
||
| with torch.no_grad(): | ||
| out_after = model(x) | ||
|
|
||
| assert torch.allclose(out_before, out_after, atol=1e-5), \ | ||
| "Model outputs differ after merge" | ||
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.