-
Notifications
You must be signed in to change notification settings - Fork 0
Apply activation quantization parameters selection(3rd PR internal review) #19
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
gouda-youichi
merged 13 commits into
apply_actq_params_selection_for_node_inside_fln
from
dev2_apply_actq_params_selection_for_node_inside_fln
Jul 7, 2025
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
fac4119
modify for PR review comments.
gouda-youichi a15644b
fix https://github.com/kkawa14/model_optimization/pull/19#discussion_…
gouda-youichi e41edc9
node Mock化途中
gouda-youichi 0821958
nodeのMock化一旦完了
gouda-youichi ac8afee
modify a bit.
gouda-youichi 326d11b
Mocklize of _collect_nodes_for_hmse.
gouda-youichi e7eeda5
delete torch dependency.
gouda-youichi 784ad69
delete unnecessary code and improved readability.
gouda-youichi f4cdb41
move test script.
gouda-youichi 5cf8cdb
corrections to PR comments.
gouda-youichi 67a6d72
corrections to PR comments(2).
gouda-youichi d633a81
chenge file mode (-x).
gouda-youichi d12e6b1
change if-statement for q_mode.
gouda-youichi 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
124 changes: 124 additions & 0 deletions
124
...ts/core/quantization/quantization_params_generation/test_calculate_quantization_params.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,124 @@ | ||
| # Copyright 2025 Sony Semiconductor Israel, Inc. All rights reserved. | ||
| # | ||
| # 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. | ||
| # ============================================================================== | ||
| import pytest | ||
| import numpy as np | ||
|
|
||
| from typing import Generator | ||
| from unittest.mock import Mock | ||
| from model_compression_toolkit.core.common import Graph, BaseNode | ||
| from model_compression_toolkit.core.common.quantization.quantization_params_generation.qparams_computation import \ | ||
| calculate_quantization_params | ||
| from model_compression_toolkit.core.common.quantization.candidate_node_quantization_config import \ | ||
| CandidateNodeQuantizationConfig | ||
| from model_compression_toolkit.core.common.quantization.node_quantization_config import \ | ||
| ActivationQuantizationMode, NodeActivationQuantizationConfig, NodeWeightsQuantizationConfig | ||
| from model_compression_toolkit.target_platform_capabilities import OpQuantizationConfig | ||
| from model_compression_toolkit.core import QuantizationConfig | ||
| from model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema import Signedness | ||
| from model_compression_toolkit.core.common.collectors.statistics_collector import StatsCollector | ||
| from mct_quantizers import QuantizationMethod | ||
| from model_compression_toolkit.core.common.node_prior_info import NodePriorInfo | ||
| from model_compression_toolkit.core.common.framework_implementation import FrameworkImplementation | ||
|
|
||
|
|
||
| class TestCalculateQuantizationParams: | ||
| def build_op_cfg(self): | ||
| op_cfg = Mock(spec=OpQuantizationConfig) | ||
| op_cfg.activation_quantization_method = QuantizationMethod.POWER_OF_TWO | ||
| op_cfg.activation_n_bits = 16 | ||
| op_cfg.enable_activation_quantization = True | ||
| op_cfg.quantization_preserving = False | ||
| op_cfg.signedness = Signedness.AUTO | ||
|
|
||
| return op_cfg | ||
|
|
||
| def build_node(self, name='node', q_mode=ActivationQuantizationMode.QUANT): | ||
| node = Mock(spec=BaseNode) | ||
| node.name = name | ||
| node.get_node_weights_attributes.return_value = [] | ||
|
|
||
| if q_mode == ActivationQuantizationMode.QUANT: | ||
| node.is_activation_quantization_enabled.return_value = True | ||
| node.is_fln_quantization.return_value = False | ||
| elif q_mode == ActivationQuantizationMode.FLN_QUANT: | ||
| node.is_activation_quantization_enabled.return_value = False | ||
| node.is_fln_quantization.return_value = True | ||
| else: | ||
| node.is_activation_quantization_enabled.return_value = False | ||
| node.is_fln_quantization.return_value = False | ||
|
|
||
| activation_quantization_cfg = NodeActivationQuantizationConfig(op_cfg=self.build_op_cfg()) | ||
| activation_quantization_cfg.set_qc(QuantizationConfig()) | ||
| activation_quantization_cfg.quant_mode = q_mode | ||
|
|
||
| candidate_quantization_config = Mock(spec=CandidateNodeQuantizationConfig) | ||
| candidate_quantization_config.activation_quantization_cfg = activation_quantization_cfg | ||
| candidate_quantization_config.weights_quantization_cfg = Mock(spec=NodeWeightsQuantizationConfig) | ||
|
|
||
| node.candidates_quantization_cfg = [candidate_quantization_config] | ||
|
|
||
| return node | ||
|
|
||
| def get_test_graph(self, node_name, q_mode, data): | ||
| node = self.build_node(node_name, q_mode=q_mode) | ||
| graph = Graph('graph_name', input_nodes=[node], nodes=[node], output_nodes=[node], edge_list=[]) | ||
|
|
||
| graph.node_to_out_stats_collector = dict() | ||
| for n in graph.nodes(): | ||
| n.prior_info = NodePriorInfo() | ||
|
|
||
| graph.node_to_out_stats_collector[n] = StatsCollector(init_min_value=0.0, init_max_value=1.0, out_channel_axis=0) | ||
| graph.node_to_out_stats_collector[n].hc._n_bins = 3 | ||
| graph.node_to_out_stats_collector[n].hc._bins = np.array(data) | ||
| graph.node_to_out_stats_collector[n].hc._counts = np.array([1, 1]) | ||
|
|
||
| return graph | ||
|
|
||
| ### test pattern for ActivationQuantizationMode | ||
| @pytest.mark.parametrize(["node_name", "q_mode", "input_data", "expects"], [ | ||
| # node_name, q_mode, input data, expected value | ||
| ['node_quant', ActivationQuantizationMode.QUANT, [0.4, 0.8, 1.2], [1.0, False]], | ||
| ['node_fln_quant', ActivationQuantizationMode.FLN_QUANT, [0.7, 1.4, 2.1], [2.0, False]], | ||
| ['node_fln_no_quant', ActivationQuantizationMode.FLN_NO_QUANT, [0.7, 1.4, 2.1], [None, None]], | ||
| ['node_no_quant', ActivationQuantizationMode.NO_QUANT, [0.7, 1.4, 2.1], [None, None]], | ||
| ['node_preserve_quant', ActivationQuantizationMode.PRESERVE_QUANT, [0.7, 1.4, 2.1], [None, None]], | ||
| ]) | ||
| def test_calculate_quantization_params_for_activation(self, node_name, q_mode, input_data, expects, mocker): | ||
| """ | ||
| Tests that calculate quantization params for activation quantization method. | ||
| """ | ||
| graph = self.get_test_graph(node_name, q_mode, input_data) | ||
|
|
||
| mocker.patch( | ||
| 'model_compression_toolkit.core.common.quantization.quantization_params_generation.qparams_computation._collect_nodes_for_hmse', | ||
| return_value=[]) | ||
|
|
||
| calculate_quantization_params(graph, Mock(spec=FrameworkImplementation), Mock(spec=Generator)) | ||
|
|
||
| node = list(graph.nodes)[0] | ||
| for candidate_qc in node.candidates_quantization_cfg: | ||
| assert type(candidate_qc.activation_quantization_cfg.activation_quantization_params) == dict | ||
| if expects[0] is not None: | ||
| ### QUANT or FLN_QUANT | ||
| assert 'threshold' in candidate_qc.activation_quantization_cfg.activation_quantization_params.keys() | ||
| assert 'is_signed' in candidate_qc.activation_quantization_cfg.activation_quantization_params.keys() | ||
|
|
||
| threshold = candidate_qc.activation_quantization_cfg.activation_quantization_params['threshold'] | ||
| is_signed = candidate_qc.activation_quantization_cfg.activation_quantization_params['is_signed'] | ||
| assert threshold == expects[0] | ||
| assert is_signed == expects[1] | ||
| else: | ||
| assert 'threshold' not in candidate_qc.activation_quantization_cfg.activation_quantization_params.keys() | ||
| assert 'is_signed' not in candidate_qc.activation_quantization_cfg.activation_quantization_params.keys() | ||
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.