-
Notifications
You must be signed in to change notification settings - Fork 21
Add quantization options #102
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
mht-sharma
wants to merge
27
commits into
main
Choose a base branch
from
add_quantization_options
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
27 commits
Select commit
Hold shift + click to select a range
afd8367
add pipeline
mht-sharma 3afe2bc
add options
mht-sharma ceef198
added diff dict
mht-sharma 51334f6
added diff dict
mht-sharma ea88c53
Merge branch 'main' into add_quantization_options
mht-sharma 591ee62
removed fpn
mht-sharma f044274
removed fpn
mht-sharma 44891a6
add docsttring
mht-sharma 5bf1818
updated init models
mht-sharma be8c954
updated init models
mht-sharma 6174113
updated docs
mht-sharma 3568ca7
Merge branch 'main' into add_quantization_options
mht-sharma 86d038f
Merge branch 'main' into add_quantization_options
mht-sharma acf90b0
fix config
mht-sharma 1721308
update quantization configurations for ryzenai (vai_q_onnx) (#117)
ChaoLi-AMD 1c5cd94
Merge branch 'main' into add_quantization_options
mht-sharma db3ca00
fix style
mht-sharma 0d8b1a3
fix options
mht-sharma 4385b77
fix options
mht-sharma 94b3f80
add tests
mht-sharma 4684dea
add config options
mht-sharma 8cc538f
fix style
mht-sharma 7faa79e
Merge branch 'main' into add_quantization_options
mht-sharma 41143d9
fix token
mht-sharma 409d43c
addressed comments
mht-sharma c3e2554
addressed comments
mht-sharma e67ed2f
fix docstring
mht-sharma 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,169 @@ | ||
| import unittest | ||
|
|
||
| import vai_q_onnx | ||
| from parameterized import parameterized | ||
|
|
||
| from optimum.amd.ryzenai import AutoQuantizationConfig, ExtraOptions, QuantizationConfig | ||
|
|
||
|
|
||
| class TestExtraOptions(unittest.TestCase): | ||
| def test_default_values(self): | ||
| options = ExtraOptions() | ||
| self.assertEqual(options.activation_symmetric, False) | ||
| self.assertEqual(options.weight_symmetric, True) | ||
| self.assertEqual(options.use_unsigned_relu, False) | ||
| self.assertEqual(options.quantize_bias, True) | ||
| self.assertEqual(options.remove_input_init, True) | ||
| self.assertEqual(options.enable_subgraph, False) | ||
| self.assertEqual(options.force_quantize_no_input_check, False) | ||
| self.assertEqual(options.matmul_const_b_only, False) | ||
| self.assertEqual(options.add_qdq_pair_to_weight, False) | ||
| self.assertEqual(options.op_types_to_exclude_output_quantization, []) | ||
| self.assertEqual(options.dedicated_qdq_pair, False) | ||
| self.assertEqual(options.qdq_op_type_per_channel_support_to_axis, {}) | ||
| self.assertEqual(options.use_qdq_vitis_custom_ops, True) | ||
| self.assertEqual(options.calib_tensor_range_symmetric, False) | ||
| self.assertEqual(options.calib_moving_average, False) | ||
| self.assertEqual(options.calib_moving_average_constant, 0.01) | ||
| self.assertEqual(options.random_data_reader_input_data_range, None) | ||
| self.assertEqual(options.int16_scale, False) | ||
| self.assertEqual(options.min_mse_mode, "All") | ||
| self.assertEqual(options.convert_bn_to_conv, True) | ||
| self.assertEqual(options.convert_reduce_mean_to_global_avg_pool, True) | ||
| self.assertEqual(options.split_large_kernel_pool, True) | ||
| self.assertEqual(options.convert_split_to_slice, True) | ||
| self.assertEqual(options.fuse_instance_norm, False) | ||
| self.assertEqual(options.fuse_l2_norm, False) | ||
| self.assertEqual(options.convert_clip_to_relu, False) | ||
| self.assertEqual(options.simulate_dpu, True) | ||
| self.assertEqual(options.convert_leaky_relu_to_dpu_version, True) | ||
| self.assertEqual(options.convert_sigmoid_to_hard_sigmoid, True) | ||
| self.assertEqual(options.convert_hard_sigmoid_to_dpu_version, True) | ||
| self.assertEqual(options.convert_avg_pool_to_dpu_version, True) | ||
| self.assertEqual(options.convert_reduce_mean_to_dpu_version, True) | ||
| self.assertEqual(options.convert_softmax_to_dpu_version, False) | ||
| self.assertEqual(options.ipu_limitation_check, True) | ||
| self.assertEqual(options.adjust_shift_cut, True) | ||
| self.assertEqual(options.adjust_shift_bias, True) | ||
| self.assertEqual(options.adjust_shift_read, True) | ||
| self.assertEqual(options.adjust_shift_write, True) | ||
| self.assertEqual(options.adjust_hard_sigmoid, True) | ||
| self.assertEqual(options.adjust_shift_swish, True) | ||
| self.assertEqual(options.align_concat, True) | ||
| self.assertEqual(options.align_pool, True) | ||
| self.assertEqual(options.replace_clip6_relu, False) | ||
| self.assertEqual(options.cle_steps, 1) | ||
| self.assertEqual(options.cle_total_layer_diff_threshold, 2e-7) | ||
| self.assertEqual(options.cle_scale_append_bias, True) | ||
| self.assertEqual(options.remove_qdq_conv_leaky_relu, False) | ||
| self.assertEqual(options.remove_qdq_conv_prelu, False) | ||
|
|
||
| def test_snake_to_camel(self): | ||
| options = ExtraOptions() | ||
| camel_case_dict = options.snake_to_camel | ||
| self.assertEqual( | ||
| camel_case_dict["qdq_op_type_per_channel_support_to_axis"], "QDQOpTypePerChannelSupportToAxis" | ||
| ) | ||
| self.assertEqual(camel_case_dict["ipu_limitation_check"], "IPULimitationCheck") | ||
| self.assertEqual(camel_case_dict["cle_steps"], "CLESteps") | ||
| self.assertEqual(camel_case_dict["cle_total_layer_diff_threshold"], "CLETotalLayerDiffThreshold") | ||
| self.assertEqual(camel_case_dict["cle_scale_append_bias"], "CLEScaleAppendBias") | ||
|
|
||
| def test_camel_to_snake_setattr(self): | ||
| options = ExtraOptions() | ||
| options.QDQOpTypePerChannelSupportToAxis = "some_value" | ||
| options.IPULimitationCheck = False | ||
| options.CLESteps = 5 | ||
| options.CLETotalLayerDiffThreshold = 1e-7 | ||
| options.CLEScaleAppendBias = False | ||
| self.assertEqual(options.qdq_op_type_per_channel_support_to_axis, "some_value") | ||
| self.assertFalse(options.ipu_limitation_check) | ||
| self.assertEqual(options.cle_steps, 5) | ||
| self.assertEqual(options.cle_total_layer_diff_threshold, 1e-7) | ||
| self.assertFalse(options.cle_scale_append_bias) | ||
|
|
||
| def test_to_diff_dict(self): | ||
| options = ExtraOptions(activation_symmetric=True) | ||
| diff_dict = options.to_diff_dict() | ||
| self.assertEqual(diff_dict, {"activation_symmetric": True}) | ||
|
|
||
| @parameterized.expand( | ||
| [ | ||
| ("activation_symmetric", False, True), | ||
| ("weight_symmetric", True, False), | ||
| ("use_unsigned_relu", False, True), | ||
| ] | ||
| ) | ||
| def test_parametric_setting_attributes(self, attribute, default_value, new_value): | ||
| options = ExtraOptions() | ||
| self.assertEqual(getattr(options, attribute), default_value) | ||
| setattr(options, attribute, new_value) | ||
| self.assertEqual(getattr(options, attribute), new_value) | ||
|
|
||
|
|
||
| class TestQuantizationConfig(unittest.TestCase): | ||
| def test_default_values(self): | ||
| config = QuantizationConfig() | ||
| self.assertEqual(config.format, vai_q_onnx.QuantFormat.QDQ) | ||
| self.assertEqual(config.calibration_method, vai_q_onnx.PowerOfTwoMethod.MinMSE) | ||
| self.assertEqual(config.input_nodes, []) | ||
| self.assertEqual(config.output_nodes, []) | ||
| self.assertEqual(config.op_types_to_quantize, []) | ||
| self.assertEqual(config.random_data_reader_input_shape, []) | ||
| self.assertFalse(config.per_channel) | ||
| self.assertFalse(config.reduce_range) | ||
| self.assertEqual(config.activations_dtype, vai_q_onnx.QuantType.QUInt8) | ||
| self.assertEqual(config.weights_dtype, vai_q_onnx.QuantType.QInt8) | ||
| self.assertEqual(config.nodes_to_quantize, []) | ||
| self.assertEqual(config.nodes_to_exclude, []) | ||
| self.assertTrue(config.optimize_model) | ||
| self.assertFalse(config.use_external_data_format) | ||
| self.assertEqual(config.execution_providers, ["CPUExecutionProvider"]) | ||
| self.assertFalse(config.enable_ipu_cnn) | ||
| self.assertFalse(config.convert_fp16_to_fp32) | ||
| self.assertFalse(config.convert_nchw_to_nhwc) | ||
| self.assertFalse(config.include_cle) | ||
| self.assertIsInstance(config.extra_options, ExtraOptions) | ||
|
|
||
| def test_extra_options_initialization(self): | ||
| extra_options = ExtraOptions(activation_symmetric=True) | ||
| config = QuantizationConfig(extra_options=extra_options) | ||
| self.assertEqual(config.extra_options.activation_symmetric, True) | ||
|
|
||
| def test_use_symmetric_calibration(self): | ||
| config = QuantizationConfig(extra_options=ExtraOptions(activation_symmetric=True, weight_symmetric=True)) | ||
| self.assertTrue(config.use_symmetric_calibration) | ||
|
|
||
| @parameterized.expand( | ||
| [ | ||
| ("format", vai_q_onnx.QuantFormat.QDQ, vai_q_onnx.QuantFormat.QOperator), | ||
| ("calibration_method", vai_q_onnx.PowerOfTwoMethod.MinMSE, vai_q_onnx.CalibrationMethod.Entropy), | ||
| ("activations_dtype", vai_q_onnx.QuantType.QUInt8, vai_q_onnx.QuantType.QInt8), | ||
| ("weights_dtype", vai_q_onnx.QuantType.QInt8, vai_q_onnx.QuantType.QUInt8), | ||
| ] | ||
| ) | ||
| def test_parametric_setting_attributes(self, attribute, default_value, new_value): | ||
| config = QuantizationConfig() | ||
| self.assertEqual(getattr(config, attribute), default_value) | ||
| setattr(config, attribute, new_value) | ||
| self.assertEqual(getattr(config, attribute), new_value) | ||
|
|
||
|
|
||
| class TestAutoQuantizationConfig(unittest.TestCase): | ||
| def test_ipu_cnn_config(self): | ||
| config = AutoQuantizationConfig.ipu_cnn_config() | ||
| self.assertEqual(config.format, vai_q_onnx.QuantFormat.QDQ) | ||
| self.assertEqual(config.calibration_method, vai_q_onnx.PowerOfTwoMethod.MinMSE) | ||
| self.assertTrue(config.extra_options.activation_symmetric) | ||
|
|
||
| def test_ipu_transformer_config(self): | ||
| config = AutoQuantizationConfig.ipu_transformer_config() | ||
| self.assertEqual(config.format, vai_q_onnx.QuantFormat.QDQ) | ||
| self.assertEqual(config.calibration_method, vai_q_onnx.CalibrationMethod.MinMax) | ||
| self.assertTrue(config.extra_options.activation_symmetric) | ||
|
|
||
| def test_cpu_cnn_config(self): | ||
| config = AutoQuantizationConfig.cpu_cnn_config() | ||
|
|
||
| self.assertEqual(config.format, vai_q_onnx.QuantFormat.QDQ) | ||
| self.assertEqual(config.calibration_method, vai_q_onnx.CalibrationMethod.MinMax) | ||
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.
Are the new quantization parameters tested?
Uh oh!
There was an error while loading. Please reload this page.
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.
In CI? If you mean running running quantization with different params that is not tested for all. Maybe suitable to add in future PRs.