From 90dc04e14e47de05847b2be57f796598a1ffc8df Mon Sep 17 00:00:00 2001 From: Nikita Malinin Date: Tue, 17 Oct 2023 18:56:03 +0200 Subject: [PATCH] Replace method --- .../algorithms/post_training/algorithm.py | 2 +- .../algorithms/smooth_quant/algorithm.py | 37 ++++++++++++------- .../algorithms/smooth_quant/backend.py | 15 -------- tests/post_training/model_scope.py | 9 ++++- 4 files changed, 33 insertions(+), 30 deletions(-) diff --git a/nncf/quantization/algorithms/post_training/algorithm.py b/nncf/quantization/algorithms/post_training/algorithm.py index 25d22fca99f..21a5dd7caf1 100644 --- a/nncf/quantization/algorithms/post_training/algorithm.py +++ b/nncf/quantization/algorithms/post_training/algorithm.py @@ -90,7 +90,7 @@ def __init__( sq_params = advanced_parameters.smooth_quant_params - if model_type == ModelType.TRANSFORMER and sq_params.convolution >= 0 and sq_params.matmul >= 0: + if model_type == ModelType.TRANSFORMER and (sq_params.convolution >= 0 or sq_params.matmul >= 0): alpha_map = {"convolution": sq_params.convolution, "matmul": sq_params.matmul} smooth_quant_algorithm = SmoothQuant( subset_size=subset_size, inplace_statistics=advanced_parameters.inplace_statistics, alpha_map=alpha_map diff --git a/nncf/quantization/algorithms/smooth_quant/algorithm.py b/nncf/quantization/algorithms/smooth_quant/algorithm.py index 786110e078e..115c91fe0ec 100644 --- a/nncf/quantization/algorithms/smooth_quant/algorithm.py +++ b/nncf/quantization/algorithms/smooth_quant/algorithm.py @@ -74,7 +74,6 @@ def __init__( self._backend_entity = None self._algorithm_key = f"SQ_{hash(self)}" self._cached_multiply_names = Counter() - self._validate_alpha_map(alpha_map) self._alpha_map = alpha_map @property @@ -97,16 +96,6 @@ def _set_backend_entity(self, model: TModel) -> None: "Cannot return backend-specific entity because {} is not supported!".format(model_backend.value) ) - def _validate_alpha_map(self, alpha_map: Dict[str, float]) -> None: - """ - Validates the alpha map for case of the negative value. - - :param alpha_map: Map for the validation. - """ - for layer_type, alpha_value in alpha_map.items(): - if alpha_value < 0: - raise RuntimeError(f"Smooth Quant algorithm does not support negative parameter for {layer_type}!") - def apply( self, model: TModel, @@ -115,7 +104,7 @@ def apply( dataset: Optional[Dataset] = None, ) -> TModel: self._set_backend_entity(model) - alpha_map = self._backend_entity.get_alpha_map(self._alpha_map) + alpha_map = self._get_alpha_map() nodes_to_smooth_data = self._get_nodes_to_smooth_data(graph, alpha_map.keys()) model_transformer = ModelTransformerFactory.create(model) @@ -238,7 +227,7 @@ def get_statistic_points(self, model: TModel, graph: NNCFGraph) -> StatisticPoin statistic_container = StatisticPointsContainer() self._set_backend_entity(model) - alpha_map = self._backend_entity.get_alpha_map(self._alpha_map) + alpha_map = self._get_alpha_map() nodes_to_smooth_data = self._get_nodes_to_smooth_data(graph, alpha_map.keys()) @@ -377,3 +366,25 @@ def _create_scale_node_name(self, source_name: str, source_port_id: int) -> str: unique_index = self._cached_multiply_names[scale_node_name] self._cached_multiply_names[scale_node_name] += 1 return f"{scale_node_name}_{unique_index}/sq_multiply" + + def _get_alpha_map(self) -> Dict[OperatorMetatype, float]: + """ + Returns alpha map by metatypes. + + :return: Alpha map by metatypes. + """ + alpha_by_metatype_map = {} + name_to_metatype = { + "convolution": self._backend_entity.convolution_metatype, + "matmul": self._backend_entity.matmul_metatype, + } + for type_name, alpha_value in self._alpha_map.items(): + if alpha_value < 0: + nncf_logger.debug( + f"Smooth Quant algorithm does not support negative parameter for {type_name}! " + "Skipping these layers." + ) + continue + metatype = name_to_metatype[type_name] + alpha_by_metatype_map[metatype] = alpha_value + return alpha_by_metatype_map diff --git a/nncf/quantization/algorithms/smooth_quant/backend.py b/nncf/quantization/algorithms/smooth_quant/backend.py index e59a2a3a178..094e307c786 100644 --- a/nncf/quantization/algorithms/smooth_quant/backend.py +++ b/nncf/quantization/algorithms/smooth_quant/backend.py @@ -28,21 +28,6 @@ class SmoothQuantAlgoBackend(ABC): - def get_alpha_map(self, alpha_map: Dict[str, float]) -> Dict[OperatorMetatype, float]: - """ - Returns alpha map by metatypes. - - :param convolution_alpha: Alpha value for Convolution layers. - :param matmul_alpha: Alpha value for MatMul layers. - :return: Alpha map by metatypes. - """ - alpha_by_metatype_map = {} - name_to_metatype = {"convolution": self.convolution_metatype, "matmul": self.matmul_metatype} - for type_name, alpha_value in alpha_map.items(): - metatype = name_to_metatype[type_name] - alpha_by_metatype_map[metatype] = alpha_value - return alpha_by_metatype_map - @property @abstractmethod def convolution_metatype(self) -> OperatorMetatype: diff --git a/tests/post_training/model_scope.py b/tests/post_training/model_scope.py index fa0c33d35b2..f0410d9a163 100644 --- a/tests/post_training/model_scope.py +++ b/tests/post_training/model_scope.py @@ -13,6 +13,8 @@ from nncf import ModelType from nncf import QuantizationPreset +from nncf.quantization.advanced_parameters import AdvancedQuantizationParameters +from nncf.quantization.advanced_parameters import AdvancedSmoothQuantParameters from tests.post_training.pipelines.base import ALL_PTQ_BACKENDS from tests.post_training.pipelines.base import NNCF_PTQ_BACKENDS from tests.post_training.pipelines.base import BackendType @@ -52,6 +54,9 @@ "ptq_params": { "preset": QuantizationPreset.MIXED, "model_type": ModelType.TRANSFORMER, + "advanced_parameters": AdvancedQuantizationParameters( + smooth_quant_params=AdvancedSmoothQuantParameters(matmul=-1.0) + ), }, "backends": [BackendType.TORCH, BackendType.ONNX, BackendType.OV, BackendType.POT], }, @@ -71,6 +76,9 @@ "ptq_params": { "preset": QuantizationPreset.MIXED, "model_type": ModelType.TRANSFORMER, + "advanced_parameters": AdvancedQuantizationParameters( + smooth_quant_params=AdvancedSmoothQuantParameters(matmul=-1.0) + ), }, "backends": ALL_PTQ_BACKENDS, }, @@ -143,7 +151,6 @@ "ptq_params": { "preset": QuantizationPreset.MIXED, "model_type": ModelType.TRANSFORMER, - # "advanced_parameters": AdvancedQuantizationParameters(smooth_quant_alpha=0.05), }, "backends": [BackendType.TORCH, BackendType.ONNX, BackendType.OV], },