From e3bdb83f54ed7064e5a3d4115eed563586ba11da Mon Sep 17 00:00:00 2001 From: Harshita Mangal Date: Wed, 1 Nov 2023 14:37:43 -0700 Subject: [PATCH 1/3] Fixes for failing models Signed-off-by: Harshita Mangal --- .../src/python/aimet_onnx/adaround/adaround_weight.py | 3 +++ .../onnx/src/python/aimet_onnx/adaround/utils.py | 9 ++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/TrainingExtensions/onnx/src/python/aimet_onnx/adaround/adaround_weight.py b/TrainingExtensions/onnx/src/python/aimet_onnx/adaround/adaround_weight.py index 161ea81837d..32f1c6c4b3f 100644 --- a/TrainingExtensions/onnx/src/python/aimet_onnx/adaround/adaround_weight.py +++ b/TrainingExtensions/onnx/src/python/aimet_onnx/adaround/adaround_weight.py @@ -43,6 +43,7 @@ import json from typing import Tuple, Dict, List, Callable from onnx import onnx_pb +from onnxruntime.quantization.onnx_quantizer import ONNXModel from tqdm import tqdm # Import AIMET specific modules @@ -139,6 +140,8 @@ def apply_adaround(cls, model: onnx_pb.ModelProto, params: AdaroundParameters, """ # pylint: disable=too-many-arguments # Create Quant sim with given parameters + if not isinstance(model, ONNXModel): + model = ONNXModel(model) quant_sim = QuantizationSimModel(copy.deepcopy(model), quant_scheme=default_quant_scheme, default_param_bw=default_param_bw, config_file=default_config_file) diff --git a/TrainingExtensions/onnx/src/python/aimet_onnx/adaround/utils.py b/TrainingExtensions/onnx/src/python/aimet_onnx/adaround/utils.py index 318b5d10444..64a9bea564e 100644 --- a/TrainingExtensions/onnx/src/python/aimet_onnx/adaround/utils.py +++ b/TrainingExtensions/onnx/src/python/aimet_onnx/adaround/utils.py @@ -105,7 +105,14 @@ def read_attributes_for_op(module_info: ModuleInfo) -> Dict: if attribute.name == 'dilations': attributes['dilations'] = list(attribute.ints) elif attribute.name == 'pads': - attributes['pads'] = list(attribute.ints) + padding = list(attribute.ints) + unique_vals = set() + new_padding = [] + for val in padding: + if val not in unique_vals: + unique_vals.add(val) + new_padding.append(val) + attributes['pads'] = new_padding elif attribute.name == 'strides': attributes['strides'] = list(attribute.ints) elif attribute.name == 'group': From 32e68dbdf76997b66d278241a8531c3034f861d2 Mon Sep 17 00:00:00 2001 From: Harshita Mangal Date: Wed, 1 Nov 2023 14:42:39 -0700 Subject: [PATCH 2/3] Fixes for failing models Signed-off-by: Harshita Mangal --- .../onnx/src/python/aimet_onnx/adaround/adaround_optimizer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TrainingExtensions/onnx/src/python/aimet_onnx/adaround/adaround_optimizer.py b/TrainingExtensions/onnx/src/python/aimet_onnx/adaround/adaround_optimizer.py index b4babef579a..0ea8589c2de 100644 --- a/TrainingExtensions/onnx/src/python/aimet_onnx/adaround/adaround_optimizer.py +++ b/TrainingExtensions/onnx/src/python/aimet_onnx/adaround/adaround_optimizer.py @@ -274,7 +274,7 @@ def _compute_output_with_adarounded_weights(weights: torch.Tensor, quant_module, if 'bias' in quant_module.params: bias = torch.from_numpy(numpy_helper.to_array(quant_module.params['bias'].tensor)).to(device) out_data = functional.conv_transpose2d(inp_data, adarounded_weights, bias=bias, stride=attributes['strides'], - dilation=attributes['dilations'], padding=attributes['pads'][0], + dilation=attributes['dilations'], padding=attributes['pads'], groups=attributes['group']) elif quant_module.type in ['Gemm', 'MatMul']: bias = torch.from_numpy(numpy_helper.to_array(quant_module.params['bias'].tensor)).to(device) From 3605db8b5c991e43e1ba11abd2cea8df6e5369bd Mon Sep 17 00:00:00 2001 From: Harshita Mangal Date: Wed, 1 Nov 2023 15:01:07 -0700 Subject: [PATCH 3/3] Add exit message Signed-off-by: Harshita Mangal --- .../src/python/aimet_onnx/adaround/adaround_optimizer.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/TrainingExtensions/onnx/src/python/aimet_onnx/adaround/adaround_optimizer.py b/TrainingExtensions/onnx/src/python/aimet_onnx/adaround/adaround_optimizer.py index 0ea8589c2de..26dee94deef 100644 --- a/TrainingExtensions/onnx/src/python/aimet_onnx/adaround/adaround_optimizer.py +++ b/TrainingExtensions/onnx/src/python/aimet_onnx/adaround/adaround_optimizer.py @@ -149,6 +149,11 @@ def _optimize_rounding(cls, module: ModuleInfo, quantized_input_name, use_cache_acts_data = TorchAdaroundOptimizer._can_cache_acts_data(len(cached_dataset), inp_data_torch.shape, out_data_torch.shape) + attributes = read_attributes_for_op(module) + if len(attributes['pad']) > 2: + logger.info("Skipping the Convolution layer because padding size of 4 is not supported for optimization") + return + if use_cache_acts_data and AdaroundOptimizer.enable_caching_acts_data(): logger.debug("Caching intermediate activations data for optimization.") all_inp_data, all_orig_out_data = act_sampler.sample_and_place_all_acts_on_cpu(cached_dataset) @@ -266,7 +271,7 @@ def _compute_output_with_adarounded_weights(weights: torch.Tensor, quant_module, if 'bias' in quant_module.params: bias = torch.from_numpy(numpy_helper.to_array(quant_module.params['bias'].tensor)).to(device) out_data = functional.conv2d(inp_data, adarounded_weights, bias=bias, stride=attributes['strides'], - dilation=attributes['dilations'], padding=attributes['pads'][0], + dilation=attributes['dilations'], padding=attributes['pads'], groups=attributes['group']) elif quant_module.type == 'ConvTranspose': attributes = read_attributes_for_op(quant_module)