From 6fe2b52b0f9f2fa07be5cab456939cf2180f4f30 Mon Sep 17 00:00:00 2001 From: Andrey Churkin Date: Mon, 4 Sep 2023 11:06:41 +0100 Subject: [PATCH] Reworking the placement of metatype groups (#2094) ### Changes Create a separate module to store metatype groups in one location. ### Reason for changes Ref: 113580 ### Related tickets Ref: 113580 ### Tests N/A --- nncf/onnx/graph/metatypes/groups.py | 122 ++++++++++++++++++ nncf/onnx/graph/metatypes/onnx_metatypes.py | 55 -------- nncf/onnx/graph/nncf_graph_builder.py | 47 ++++++- nncf/onnx/hardware/fused_patterns.py | 20 +-- nncf/onnx/hardware/pattern_operations.py | 77 ----------- .../onnx/quantization/default_quantization.py | 53 +------- nncf/onnx/quantization/ignored_patterns.py | 2 +- .../graph/metatypes/{common.py => groups.py} | 119 +++++++++++++++-- .../graph/metatypes/openvino_metatypes.py | 22 ---- nncf/openvino/graph/model_utils.py | 2 +- nncf/openvino/graph/nncf_graph_builder.py | 4 +- nncf/openvino/graph/node_utils.py | 8 +- nncf/openvino/hardware/fused_patterns.py | 40 +++--- nncf/openvino/hardware/pattern_operations.py | 77 ----------- .../quantization/default_quantization.py | 39 +----- .../accuracy_control/openvino_backend.py | 16 +-- .../bias_correction/openvino_backend.py | 2 +- .../fast_bias_correction/openvino_backend.py | 2 +- .../algorithms/min_max/onnx_backend.py | 5 +- .../algorithms/min_max/openvino_backend.py | 4 +- tests/onnx/test_layer_attributes.py | 4 +- tests/openvino/native/test_metatypes.py | 3 +- 22 files changed, 342 insertions(+), 381 deletions(-) create mode 100644 nncf/onnx/graph/metatypes/groups.py delete mode 100644 nncf/onnx/hardware/pattern_operations.py rename nncf/openvino/graph/metatypes/{common.py => groups.py} (53%) delete mode 100644 nncf/openvino/hardware/pattern_operations.py diff --git a/nncf/onnx/graph/metatypes/groups.py b/nncf/onnx/graph/metatypes/groups.py new file mode 100644 index 00000000000..bac0669ebfa --- /dev/null +++ b/nncf/onnx/graph/metatypes/groups.py @@ -0,0 +1,122 @@ +# Copyright (c) 2023 Intel Corporation +# 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. + +from nncf.onnx.graph.metatypes import onnx_metatypes + +QUANTIZE_AGNOSTIC_OPERATIONS = [ + onnx_metatypes.ONNXMaxPoolMetatype, + onnx_metatypes.ONNXReduceMaxMetatype, + onnx_metatypes.ONNXReshapeMetatype, + onnx_metatypes.ONNXTransposeMetatype, + onnx_metatypes.ONNXSqueezeMetatype, + onnx_metatypes.ONNXUnsqueezeMetatype, + onnx_metatypes.ONNXSplitMetatype, + onnx_metatypes.ONNXTileMetatype, + onnx_metatypes.ONNXCenterCropPadMetatype, + onnx_metatypes.ONNXSliceMetatype, + onnx_metatypes.ONNXPadMetatype, + onnx_metatypes.ONNXGatherMetatype, + onnx_metatypes.ONNXGatherNDMetatype, + onnx_metatypes.ONNXGatherElementsMetatype, + onnx_metatypes.ONNXDepthToSpaceMetatype, + onnx_metatypes.ONNXSpaceToDepthMetatype, + onnx_metatypes.ONNXScatterElementsMetatype, + onnx_metatypes.ONNXScatterNDMetatype, + onnx_metatypes.ONNXScatterMetatype, + onnx_metatypes.ONNXCastLikeMetatype, + onnx_metatypes.ONNXDropoutMetatype, + onnx_metatypes.ONNXFlattenMetatype, + onnx_metatypes.ONNXExpandMetatype, + onnx_metatypes.ONNXIdentityMetatype, + # ONNXReluMetatype is not considered to be QUANTIZATION_AGNOSTIC, because: + # 1. Runtime doesn't provide performance benefits by quantizing the stand-alone RELU's (ticket: 59548) + # 2. It's frequently better for the end accuracy to have quantizers set up after the RELU + # so that the input distribution to the quantizer is non-negative + # and we can therefore have better quantization resolution while preserving the original dynamic range +] + + +MATMUL_METATYPES = [onnx_metatypes.ONNXGemmMetatype, onnx_metatypes.ONNXMatMulMetatype] + + +INPUTS_QUANTIZABLE_OPERATIONS = [ + onnx_metatypes.ONNXConvolutionMetatype, + onnx_metatypes.ONNXDepthwiseConvolutionMetatype, + onnx_metatypes.ONNXConvolutionTransposeMetatype, + *MATMUL_METATYPES, + onnx_metatypes.ONNXAveragePoolMetatype, + onnx_metatypes.ONNXGlobalAveragePoolMetatype, + onnx_metatypes.ONNXAddLayerMetatype, + onnx_metatypes.ONNXSubMetatype, + onnx_metatypes.ONNXMulLayerMetatype, + onnx_metatypes.ONNXBatchNormMetatype, + onnx_metatypes.ONNXHardSigmoidMetatype, + onnx_metatypes.ONNXResizeMetatype, + onnx_metatypes.ONNXPowMetatype, + onnx_metatypes.ONNXReciprocalMetatype, + onnx_metatypes.ONNXMaximumMetatype, + onnx_metatypes.ONNXMinimumMetatype, +] + + +CONSTANT_WEIGHT_LAYER_METATYPES = [ + onnx_metatypes.ONNXConvolutionMetatype, + onnx_metatypes.ONNXDepthwiseConvolutionMetatype, + onnx_metatypes.ONNXConvolutionTransposeMetatype, + onnx_metatypes.ONNXEmbeddingMetatype, +] + + +LINEAR_OPERATIONS = [ + onnx_metatypes.ONNXConvolutionMetatype, + onnx_metatypes.ONNXDepthwiseConvolutionMetatype, + onnx_metatypes.ONNXConvolutionTransposeMetatype, + onnx_metatypes.ONNXDeformableConvolutionMetatype, + *MATMUL_METATYPES, +] + + +ATOMIC_ACTIVATIONS_OPERATIONS = [ + onnx_metatypes.ONNXReluMetatype, + onnx_metatypes.ONNXLeakyReluMetatype, + onnx_metatypes.ONNXThresholdedReluMetatype, + onnx_metatypes.ONNXEluMetatype, + onnx_metatypes.ONNXPReluMetatype, + onnx_metatypes.ONNXSigmoidMetatype, + onnx_metatypes.ONNXHardSigmoidMetatype, + onnx_metatypes.ONNXHardSwishMetatype, +] + + +ARITHMETIC_OPERATIONS = [ + onnx_metatypes.ONNXAddLayerMetatype, + onnx_metatypes.ONNXSubMetatype, + onnx_metatypes.ONNXMulLayerMetatype, + onnx_metatypes.ONNXDivLayerMetatype, +] + + +OPERATIONS_WITH_WEIGHTS = [ + *CONSTANT_WEIGHT_LAYER_METATYPES, + *MATMUL_METATYPES, +] + + +BATCH_NORMALIZATION_OPERATIONS = [ + onnx_metatypes.ONNXBatchNormMetatype, +] + + +# Contains the operation metatypes for which bias can be applied. +OPERATIONS_WITH_BIAS = [ + onnx_metatypes.ONNXConvolutionMetatype, + onnx_metatypes.ONNXDepthwiseConvolutionMetatype, +] diff --git a/nncf/onnx/graph/metatypes/onnx_metatypes.py b/nncf/onnx/graph/metatypes/onnx_metatypes.py index f3c44a0b9ad..8dec26aa857 100644 --- a/nncf/onnx/graph/metatypes/onnx_metatypes.py +++ b/nncf/onnx/graph/metatypes/onnx_metatypes.py @@ -610,24 +610,6 @@ class ONNXDeformableConvolutionMetatype(ONNXOpMetatype): op_names = ["DeformConv"] -CONSTANT_WEIGHT_LAYER_METATYPES = [ - ONNXConvolutionMetatype, - ONNXDepthwiseConvolutionMetatype, - ONNXConvolutionTransposeMetatype, - ONNXEmbeddingMetatype, -] - -MATMUL_METATYPES = [ONNXGemmMetatype, ONNXMatMulMetatype] - -GENERAL_WEIGHT_LAYER_METATYPES = CONSTANT_WEIGHT_LAYER_METATYPES + MATMUL_METATYPES - -# Contains the operation metatypes for which bias can be applied. -OPERATIONS_WITH_BIAS_METATYPES = [ - ONNXConvolutionMetatype, - ONNXDepthwiseConvolutionMetatype, -] - - def get_operator_metatypes() -> List[Type[OperatorMetatype]]: """ Returns a list of the operator metatypes. @@ -653,43 +635,6 @@ def get_metatype(model: onnx.ModelProto, node: onnx.NodeProto) -> ONNXOpMetatype return metatype -def get_constant_weight_port_ids(metatype: ONNXOpMetatype) -> List[int]: - """ - Returns port ids on which metatype must have a weight based on Operation definition. - - :param metatype: Metatype. - :return: Port ids. - """ - if metatype in CONSTANT_WEIGHT_LAYER_METATYPES: - return metatype.weight_port_ids - return [] - - -def get_possible_weight_port_ids(metatype: ONNXOpMetatype) -> List[int]: - """ - Returns weight port ids on which metatype could have a weight. - Example: ONNXMatMulMetatype could have activations or weights on input port ids: 0, 1 - - :param metatype: Metatype. - :return: Port ids. - """ - if metatype in MATMUL_METATYPES: - return metatype.possible_weight_ports - return [] - - -def get_bias_tensor_port_id(metatype: ONNXOpWithWeightsMetatype) -> Optional[int]: - """ - Returns input port id, where a bias tensor should output. - - :param node: Node, for which input port id is returned, - :return: Input port id, where a weight bias should output or None if node can not have bias. - """ - if metatype in OPERATIONS_WITH_BIAS_METATYPES: - return metatype.bias_port_id - return None - - def get_tensor_edge_name(onnx_graph: ONNXGraph, node: onnx.NodeProto, port_id: int) -> Optional[str]: """ Returns an edge name associated with a weight of a node laying on an input port_id. diff --git a/nncf/onnx/graph/nncf_graph_builder.py b/nncf/onnx/graph/nncf_graph_builder.py index ebf438bdcbc..b728b4020a8 100644 --- a/nncf/onnx/graph/nncf_graph_builder.py +++ b/nncf/onnx/graph/nncf_graph_builder.py @@ -9,7 +9,7 @@ # See the License for the specific language governing permissions and # limitations under the License. from collections import Counter -from typing import Any, Dict, Optional, Set +from typing import Any, Dict, List, Optional, Set import onnx @@ -21,11 +21,13 @@ from nncf.common.graph.layer_attributes import Dtype from nncf.common.graph.operator_metatypes import InputNoopMetatype from nncf.common.graph.operator_metatypes import OutputNoopMetatype +from nncf.onnx.graph.metatypes.groups import CONSTANT_WEIGHT_LAYER_METATYPES +from nncf.onnx.graph.metatypes.groups import MATMUL_METATYPES +from nncf.onnx.graph.metatypes.groups import OPERATIONS_WITH_BIAS from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXGemmMetatype -from nncf.onnx.graph.metatypes.onnx_metatypes import get_bias_tensor_port_id -from nncf.onnx.graph.metatypes.onnx_metatypes import get_constant_weight_port_ids +from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXOpMetatype +from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXOpWithWeightsMetatype from nncf.onnx.graph.metatypes.onnx_metatypes import get_metatype -from nncf.onnx.graph.metatypes.onnx_metatypes import get_possible_weight_port_ids from nncf.onnx.graph.metatypes.onnx_metatypes import get_tensor_edge_name from nncf.onnx.graph.onnx_graph import ONNXGraph @@ -64,6 +66,43 @@ def has_node_attrs(self) -> bool: return bool(self.node_attrs) +def get_constant_weight_port_ids(metatype: ONNXOpMetatype) -> List[int]: + """ + Returns port ids on which metatype must have a weight based on Operation definition. + + :param metatype: Metatype. + :return: Port ids. + """ + if metatype in CONSTANT_WEIGHT_LAYER_METATYPES: + return metatype.weight_port_ids + return [] + + +def get_possible_weight_port_ids(metatype: ONNXOpMetatype) -> List[int]: + """ + Returns weight port ids on which metatype could have a weight. + Example: ONNXMatMulMetatype could have activations or weights on input port ids: 0, 1 + + :param metatype: Metatype. + :return: Port ids. + """ + if metatype in MATMUL_METATYPES: + return metatype.possible_weight_ports + return [] + + +def get_bias_tensor_port_id(metatype: ONNXOpWithWeightsMetatype) -> Optional[int]: + """ + Returns input port id, where a bias tensor should output. + + :param node: Node, for which input port id is returned, + :return: Input port id, where a weight bias should output or None if node can not have bias. + """ + if metatype in OPERATIONS_WITH_BIAS: + return metatype.bias_port_id + return None + + def _get_weight_port_ids(node: onnx.NodeProto, onnx_graph: ONNXGraph) -> Set[int]: """ Returns all weight input ports. diff --git a/nncf/onnx/hardware/fused_patterns.py b/nncf/onnx/hardware/fused_patterns.py index 9572fd42970..1c9afad8e0e 100644 --- a/nncf/onnx/hardware/fused_patterns.py +++ b/nncf/onnx/hardware/fused_patterns.py @@ -14,10 +14,10 @@ from nncf.common.graph.patterns import HWFusedPatternNames from nncf.common.utils.registry import Registry from nncf.onnx.graph.metatypes import onnx_metatypes as om -from nncf.onnx.hardware.pattern_operations import ARITHMETIC_OPERATIONS -from nncf.onnx.hardware.pattern_operations import ATOMIC_ACTIVATIONS_OPERATIONS -from nncf.onnx.hardware.pattern_operations import BATCH_NORMALIZATION_OPERATIONS -from nncf.onnx.hardware.pattern_operations import LINEAR_OPERATIONS +from nncf.onnx.graph.metatypes.groups import ARITHMETIC_OPERATIONS +from nncf.onnx.graph.metatypes.groups import ATOMIC_ACTIVATIONS_OPERATIONS +from nncf.onnx.graph.metatypes.groups import BATCH_NORMALIZATION_OPERATIONS +from nncf.onnx.graph.metatypes.groups import LINEAR_OPERATIONS ONNX_HW_FUSED_PATTERNS = Registry("onnx") @@ -383,19 +383,23 @@ def create_linear_scale_shift() -> GraphPattern: def linear_operations() -> GraphPattern: pattern = GraphPattern() - pattern.add_node(**LINEAR_OPERATIONS) + pattern.add_node(**{GraphPattern.METATYPE_ATTR: LINEAR_OPERATIONS, GraphPattern.LABEL_ATTR: "LINEAR"}) return pattern def batch_normalization_operations() -> GraphPattern: pattern = GraphPattern() - pattern.add_node(**BATCH_NORMALIZATION_OPERATIONS) + pattern.add_node( + **{GraphPattern.METATYPE_ATTR: BATCH_NORMALIZATION_OPERATIONS, GraphPattern.LABEL_ATTR: "BATCH_NORMALIZATION"} + ) return pattern def atomic_activations_operations() -> GraphPattern: pattern = GraphPattern() - pattern.add_node(**ATOMIC_ACTIVATIONS_OPERATIONS) + pattern.add_node( + **{GraphPattern.METATYPE_ATTR: ATOMIC_ACTIVATIONS_OPERATIONS, GraphPattern.LABEL_ATTR: "ATOMIC_ACTIVATIONS"} + ) swish_sigmoid = create_swish_with_sigmoid() pattern.add_pattern_alternative(swish_sigmoid) @@ -413,7 +417,7 @@ def atomic_activations_operations() -> GraphPattern: def arithmetic_operations() -> GraphPattern: pattern = GraphPattern() - pattern.add_node(**ARITHMETIC_OPERATIONS) + pattern.add_node(**{GraphPattern.METATYPE_ATTR: ARITHMETIC_OPERATIONS, GraphPattern.LABEL_ATTR: "ARITHMETIC"}) return pattern diff --git a/nncf/onnx/hardware/pattern_operations.py b/nncf/onnx/hardware/pattern_operations.py deleted file mode 100644 index 39c871a38b6..00000000000 --- a/nncf/onnx/hardware/pattern_operations.py +++ /dev/null @@ -1,77 +0,0 @@ -# Copyright (c) 2023 Intel Corporation -# 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. - -from nncf.common.graph.patterns import GraphPattern -from nncf.common.graph.patterns import merge_two_types_of_operations -from nncf.onnx.graph.metatypes.onnx_metatypes import MATMUL_METATYPES -from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXAddLayerMetatype -from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXBatchNormMetatype -from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXConvolutionMetatype -from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXConvolutionTransposeMetatype -from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXDeformableConvolutionMetatype -from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXDepthwiseConvolutionMetatype -from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXDivLayerMetatype -from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXEluMetatype -from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXHardSigmoidMetatype -from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXHardSwishMetatype -from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXLeakyReluMetatype -from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXMulLayerMetatype -from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXPReluMetatype -from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXReluMetatype -from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXSigmoidMetatype -from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXSubMetatype -from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXThresholdedReluMetatype - -LINEAR_OPERATIONS = { - GraphPattern.METATYPE_ATTR: [ - ONNXConvolutionMetatype, - ONNXDepthwiseConvolutionMetatype, - ONNXConvolutionTransposeMetatype, - ONNXDeformableConvolutionMetatype, - *MATMUL_METATYPES, - ], - GraphPattern.LABEL_ATTR: "LINEAR", -} - -BATCH_NORMALIZATION_OPERATIONS = { - GraphPattern.METATYPE_ATTR: [ONNXBatchNormMetatype], - GraphPattern.LABEL_ATTR: "BATCH_NORMALIZATION", -} - -RELU_OPERATIONS = { - GraphPattern.METATYPE_ATTR: [ONNXReluMetatype, ONNXLeakyReluMetatype, ONNXThresholdedReluMetatype], - GraphPattern.LABEL_ATTR: "RELU", -} - -NON_RELU_ACTIVATIONS_OPERATIONS = { - GraphPattern.METATYPE_ATTR: [ - ONNXEluMetatype, - ONNXPReluMetatype, - ONNXSigmoidMetatype, - ONNXHardSigmoidMetatype, - ONNXHardSwishMetatype, - ], - GraphPattern.LABEL_ATTR: "NON_RELU_ACTIVATIONS", -} - -ATOMIC_ACTIVATIONS_OPERATIONS = merge_two_types_of_operations( - RELU_OPERATIONS, NON_RELU_ACTIVATIONS_OPERATIONS, "ATOMIC_ACTIVATIONS" -) - -ARITHMETIC_OPERATIONS = { - GraphPattern.METATYPE_ATTR: [ - ONNXAddLayerMetatype, - ONNXSubMetatype, - ONNXMulLayerMetatype, - ONNXDivLayerMetatype, - ], - GraphPattern.LABEL_ATTR: "ARITHMETIC", -} diff --git a/nncf/onnx/quantization/default_quantization.py b/nncf/onnx/quantization/default_quantization.py index 0be2240f46f..9fbf8aff31d 100644 --- a/nncf/onnx/quantization/default_quantization.py +++ b/nncf/onnx/quantization/default_quantization.py @@ -11,59 +11,14 @@ from nncf.common.quantization.quantizer_propagation.structs import QuantizationTrait from nncf.onnx.graph.metatypes import onnx_metatypes +from nncf.onnx.graph.metatypes.groups import INPUTS_QUANTIZABLE_OPERATIONS +from nncf.onnx.graph.metatypes.groups import QUANTIZE_AGNOSTIC_OPERATIONS # If a metatype is not in this list, then it is considered to be QuantizationTrait.NON_QUANTIZABLE. DEFAULT_ONNX_QUANT_TRAIT_TO_OP_DICT = { - QuantizationTrait.INPUTS_QUANTIZABLE: [ - onnx_metatypes.ONNXConvolutionMetatype, - onnx_metatypes.ONNXDepthwiseConvolutionMetatype, - onnx_metatypes.ONNXConvolutionTransposeMetatype, - *onnx_metatypes.MATMUL_METATYPES, - onnx_metatypes.ONNXAveragePoolMetatype, - onnx_metatypes.ONNXGlobalAveragePoolMetatype, - onnx_metatypes.ONNXAddLayerMetatype, - onnx_metatypes.ONNXSubMetatype, - onnx_metatypes.ONNXMulLayerMetatype, - onnx_metatypes.ONNXBatchNormMetatype, - onnx_metatypes.ONNXHardSigmoidMetatype, - onnx_metatypes.ONNXResizeMetatype, - onnx_metatypes.ONNXPowMetatype, - onnx_metatypes.ONNXReciprocalMetatype, - onnx_metatypes.ONNXMaximumMetatype, - onnx_metatypes.ONNXMinimumMetatype, - ], - QuantizationTrait.QUANTIZATION_AGNOSTIC: [ - onnx_metatypes.ONNXMaxPoolMetatype, - onnx_metatypes.ONNXReduceMaxMetatype, - onnx_metatypes.ONNXReshapeMetatype, - onnx_metatypes.ONNXTransposeMetatype, - onnx_metatypes.ONNXSqueezeMetatype, - onnx_metatypes.ONNXUnsqueezeMetatype, - onnx_metatypes.ONNXSplitMetatype, - onnx_metatypes.ONNXTileMetatype, - onnx_metatypes.ONNXCenterCropPadMetatype, - onnx_metatypes.ONNXSliceMetatype, - onnx_metatypes.ONNXPadMetatype, - onnx_metatypes.ONNXGatherMetatype, - onnx_metatypes.ONNXGatherNDMetatype, - onnx_metatypes.ONNXGatherElementsMetatype, - onnx_metatypes.ONNXDepthToSpaceMetatype, - onnx_metatypes.ONNXSpaceToDepthMetatype, - onnx_metatypes.ONNXScatterElementsMetatype, - onnx_metatypes.ONNXScatterNDMetatype, - onnx_metatypes.ONNXScatterMetatype, - onnx_metatypes.ONNXCastLikeMetatype, - onnx_metatypes.ONNXDropoutMetatype, - onnx_metatypes.ONNXFlattenMetatype, - onnx_metatypes.ONNXExpandMetatype, - onnx_metatypes.ONNXIdentityMetatype, - # ONNXReluMetatype is not considered to be QUANTIZATION_AGNOSTIC, because: - # 1. Runtime doesn't provide performance benefits by quantizing the stand-alone RELU's (ticket: 59548) - # 2. It's frequently better for the end accuracy to have quantizers set up after the RELU - # so that the input distribution to the quantizer is non-negative - # and we can therefore have better quantization resolution while preserving the original dynamic range - ], + QuantizationTrait.INPUTS_QUANTIZABLE: INPUTS_QUANTIZABLE_OPERATIONS, + QuantizationTrait.QUANTIZATION_AGNOSTIC: QUANTIZE_AGNOSTIC_OPERATIONS, QuantizationTrait.CONCAT: [onnx_metatypes.ONNXConcatMetatype], QuantizationTrait.OUTPUT_QUANTIZATION_AS_WEIGHTS: [onnx_metatypes.ONNXEmbeddingMetatype], } diff --git a/nncf/onnx/quantization/ignored_patterns.py b/nncf/onnx/quantization/ignored_patterns.py index 7c0226d590e..2daf69f7898 100644 --- a/nncf/onnx/quantization/ignored_patterns.py +++ b/nncf/onnx/quantization/ignored_patterns.py @@ -12,7 +12,7 @@ from nncf.common.graph.patterns.patterns import IgnoredPatternNames from nncf.common.utils.registry import Registry from nncf.onnx.graph.metatypes import onnx_metatypes as om -from nncf.onnx.graph.metatypes.onnx_metatypes import MATMUL_METATYPES +from nncf.onnx.graph.metatypes.groups import MATMUL_METATYPES ONNX_IGNORED_PATTERNS = Registry("IGNORED_PATTERNS") diff --git a/nncf/openvino/graph/metatypes/common.py b/nncf/openvino/graph/metatypes/groups.py similarity index 53% rename from nncf/openvino/graph/metatypes/common.py rename to nncf/openvino/graph/metatypes/groups.py index 1269b24708a..84ad2acf3c1 100644 --- a/nncf/openvino/graph/metatypes/common.py +++ b/nncf/openvino/graph/metatypes/groups.py @@ -46,34 +46,38 @@ ] -QUANTIZABLE_OPERATIONS = [ +INPUTS_QUANTIZABLE_OPERATIONS = [ ov_metatypes.OVConvolutionMetatype, ov_metatypes.OVGroupConvolutionMetatype, ov_metatypes.OVDepthwiseConvolutionMetatype, ov_metatypes.OVConvolutionBackpropDataMetatype, ov_metatypes.OVGroupConvolutionBackpropDataMetatype, ov_metatypes.OVMatMulMetatype, + ov_metatypes.OVBatchNormMetatype, ov_metatypes.OVAddMetatype, + ov_metatypes.OVSubtractMetatype, ov_metatypes.OVMultiplyMetatype, + ov_metatypes.OVDivideMetatype, + ov_metatypes.OVMaximumMetatype, + ov_metatypes.OVMinimumMetatype, + ov_metatypes.OVAvgPoolMetatype, + ov_metatypes.OVReduceMeanMetatype, + ov_metatypes.OVMVNMetatype, + ov_metatypes.OVNormalizeL2Metatype, + ov_metatypes.OVInterpolateMetatype, + ov_metatypes.OVPowerMetatype, + ov_metatypes.OVFloorModMetatype, ov_metatypes.OVLessMetatype, ov_metatypes.OVLessEqualMetatype, ov_metatypes.OVGreaterMetatype, ov_metatypes.OVGreaterEqualMetatype, - ov_metatypes.OVDivideMetatype, ov_metatypes.OVEqualMetatype, - ov_metatypes.OVSubtractMetatype, ov_metatypes.OVNotEqualMetatype, - ov_metatypes.OVFloorModMetatype, + ov_metatypes.OVLogicalNotMetatype, + ov_metatypes.OVLogicalAndMetatype, ov_metatypes.OVLogicalOrMetatype, ov_metatypes.OVLogicalXorMetatype, - ov_metatypes.OVLogicalAndMetatype, - ov_metatypes.OVLogicalNotMetatype, - ov_metatypes.OVPowerMetatype, - ov_metatypes.OVAvgPoolMetatype, - ov_metatypes.OVNormalizeL2Metatype, - ov_metatypes.OVReduceMeanMetatype, - ov_metatypes.OVInterpolateMetatype, - ov_metatypes.OVMVNMetatype, + ov_metatypes.OVSquaredDifferenceMetatype, ov_metatypes.OVLSTMSequenceMetatype, ov_metatypes.OVGRUSequenceMetatype, ] @@ -92,3 +96,94 @@ SHAPEOF_OPERATIONS = [ ov_metatypes.OVShapeOfMetatype, ] + + +# TODO(andrey-churkin): Can we provide a more suitable name for this variable? +LINEAR_OPERATIONS = [ + ov_metatypes.OVConvolutionMetatype, + ov_metatypes.OVGroupConvolutionMetatype, + ov_metatypes.OVDepthwiseConvolutionMetatype, + ov_metatypes.OVConvolutionBackpropDataMetatype, + ov_metatypes.OVGroupConvolutionBackpropDataMetatype, + ov_metatypes.OVDeformableConvolutionMetatype, + ov_metatypes.OVMatMulMetatype, +] + + +ATOMIC_ACTIVATIONS_OPERATIONS = [ + ov_metatypes.OVReluMetatype, + ov_metatypes.OVClampMetatype, + ov_metatypes.OVEluMetatype, + ov_metatypes.OVPReluMetatype, + ov_metatypes.OVSigmoidMetatype, + ov_metatypes.OVHardSigmoidMetatype, + ov_metatypes.OVSwishMetatype, + ov_metatypes.OVHSwishMetatype, +] + + +ARITHMETIC_OPERATIONS = [ + ov_metatypes.OVAddMetatype, + ov_metatypes.OVSubtractMetatype, + ov_metatypes.OVMultiplyMetatype, + ov_metatypes.OVDivideMetatype, +] + + +ELEMENTWISE_OPERATIONS = [ + ov_metatypes.OVAddMetatype, + ov_metatypes.OVMultiplyMetatype, + ov_metatypes.OVSubtractMetatype, + ov_metatypes.OVDivideMetatype, + ov_metatypes.OVLessMetatype, + ov_metatypes.OVLessEqualMetatype, + ov_metatypes.OVGreaterMetatype, + ov_metatypes.OVGreaterEqualMetatype, + ov_metatypes.OVEqualMetatype, + ov_metatypes.OVNotEqualMetatype, + ov_metatypes.OVFloorModMetatype, + ov_metatypes.OVLogicalOrMetatype, + ov_metatypes.OVLogicalXorMetatype, + ov_metatypes.OVLogicalAndMetatype, + ov_metatypes.OVMaximumMetatype, + ov_metatypes.OVMinimumMetatype, +] + + +BATCH_NORMALIZATION_OPERATIONS = [ + ov_metatypes.OVBatchNormMetatype, +] + + +# Keep in mind that having a metatype in this list is necessary for +# the operation to be considered as an "operation with weights", but +# it's not sufficient. For example, the MatMul operation generally has +# weights when it represents a fully connected layer. However, it can +# also be without weights. Therefore, an additional condition is needed +# to check for the existence of weights. +OPERATIONS_WITH_WEIGHTS = [ + ov_metatypes.OVConvolutionMetatype, + ov_metatypes.OVGroupConvolutionMetatype, + ov_metatypes.OVDepthwiseConvolutionMetatype, + ov_metatypes.OVConvolutionBackpropDataMetatype, + ov_metatypes.OVGroupConvolutionBackpropDataMetatype, + ov_metatypes.OVMatMulMetatype, + ov_metatypes.OVLSTMSequenceMetatype, + ov_metatypes.OVGRUSequenceMetatype, + ov_metatypes.OVEmbeddingMetatype, +] + + +# The same comment as mentioned above makes sense. +OPERATIONS_WITH_CONST_PORT_ID = [ + *OPERATIONS_WITH_WEIGHTS, + ov_metatypes.OVAddMetatype, +] + + +# Contains the operation metatypes for which bias can be applied. +OPERATIONS_WITH_BIAS = [ + ov_metatypes.OVConvolutionMetatype, + # TODO: add all metatypes with bias + ov_metatypes.OVMatMulMetatype, +] diff --git a/nncf/openvino/graph/metatypes/openvino_metatypes.py b/nncf/openvino/graph/metatypes/openvino_metatypes.py index f3e64e70fb0..e6e42f50cd0 100644 --- a/nncf/openvino/graph/metatypes/openvino_metatypes.py +++ b/nncf/openvino/graph/metatypes/openvino_metatypes.py @@ -673,28 +673,6 @@ class OVAbsMetatype(OVOpMetatype): op_names = ["Abs"] -GENERAL_WEIGHT_LAYER_METATYPES = [ - OVConvolutionMetatype, - OVGroupConvolutionMetatype, - OVDepthwiseConvolutionMetatype, - OVConvolutionBackpropDataMetatype, - OVGroupConvolutionBackpropDataMetatype, - OVMatMulMetatype, - OVLSTMSequenceMetatype, - OVGRUSequenceMetatype, - OVEmbeddingMetatype, -] - -METATYPES_WITH_CONST_PORT_ID = GENERAL_WEIGHT_LAYER_METATYPES + [OVAddMetatype] - -# Contains the operation metatypes for which bias can be applied. -OPERATIONS_WITH_BIAS_METATYPES = [ - OVConvolutionMetatype, - # TODO: add all metatypes with bias - OVMatMulMetatype, -] - - def get_operator_metatypes() -> List[Type[OperatorMetatype]]: """ Returns a list of the operator metatypes. diff --git a/nncf/openvino/graph/model_utils.py b/nncf/openvino/graph/model_utils.py index c5e66c99e8a..ec00ee00c72 100644 --- a/nncf/openvino/graph/model_utils.py +++ b/nncf/openvino/graph/model_utils.py @@ -15,7 +15,7 @@ from nncf.common.factory import ModelTransformerFactory from nncf.common.graph.graph import NNCFGraph from nncf.common.graph.transformations.layout import TransformationLayout -from nncf.openvino.graph.metatypes.common import FAKE_QUANTIZE_OPERATIONS +from nncf.openvino.graph.metatypes.groups import FAKE_QUANTIZE_OPERATIONS from nncf.openvino.graph.metatypes.openvino_metatypes import OVConvolutionBackpropDataMetatype from nncf.openvino.graph.metatypes.openvino_metatypes import OVConvolutionMetatype from nncf.openvino.graph.metatypes.openvino_metatypes import OVDepthwiseConvolutionMetatype diff --git a/nncf/openvino/graph/nncf_graph_builder.py b/nncf/openvino/graph/nncf_graph_builder.py index c3f1cae03a2..8fd92378c51 100644 --- a/nncf/openvino/graph/nncf_graph_builder.py +++ b/nncf/openvino/graph/nncf_graph_builder.py @@ -18,7 +18,7 @@ from nncf.common.graph.operator_metatypes import OperatorMetatype from nncf.openvino.graph.layer_attributes import OVLayerAttributes from nncf.openvino.graph.layer_attributes import get_weighted_layer_attributes -from nncf.openvino.graph.metatypes.openvino_metatypes import METATYPES_WITH_CONST_PORT_ID +from nncf.openvino.graph.metatypes.groups import OPERATIONS_WITH_CONST_PORT_ID from nncf.openvino.graph.metatypes.openvino_metatypes import OVConvolutionBackpropDataMetatype from nncf.openvino.graph.metatypes.openvino_metatypes import OVGroupConvolutionBackpropDataMetatype from nncf.openvino.graph.metatypes.openvino_metatypes import OVGRUSequenceMetatype @@ -147,7 +147,7 @@ def create_nncf_graph(model: ov.Model) -> NNCFGraph: if node_name not in visited: GraphConverter._add_nncf_node(node, nncf_graph) # Set const port id - elif metatype in METATYPES_WITH_CONST_PORT_ID: + elif metatype in OPERATIONS_WITH_CONST_PORT_ID: const_attrs, act_attrs = {}, {} for inp in GraphConverter._filter_weight_input_ports(node.inputs(), metatype): inp_name = inp.get_source_output().get_node().get_friendly_name() diff --git a/nncf/openvino/graph/node_utils.py b/nncf/openvino/graph/node_utils.py index ad9bde476b8..0a97c826376 100644 --- a/nncf/openvino/graph/node_utils.py +++ b/nncf/openvino/graph/node_utils.py @@ -18,8 +18,8 @@ from nncf.common.graph.graph import NNCFGraph from nncf.common.graph.graph import NNCFNode from nncf.openvino.graph.layer_attributes import OVLayerAttributes -from nncf.openvino.graph.metatypes.openvino_metatypes import GENERAL_WEIGHT_LAYER_METATYPES -from nncf.openvino.graph.metatypes.openvino_metatypes import OPERATIONS_WITH_BIAS_METATYPES +from nncf.openvino.graph.metatypes.groups import OPERATIONS_WITH_BIAS +from nncf.openvino.graph.metatypes.groups import OPERATIONS_WITH_WEIGHTS from nncf.openvino.graph.metatypes.openvino_metatypes import OVAddMetatype from nncf.openvino.graph.metatypes.openvino_metatypes import OVConstantMetatype from nncf.openvino.graph.metatypes.openvino_metatypes import OVConvertMetatype @@ -38,7 +38,7 @@ def is_node_with_bias(node: NNCFNode, nncf_graph: NNCFGraph) -> bool: with bias (bias is added to the output tensor of that operation), `False` otherwise. """ - if node.metatype not in OPERATIONS_WITH_BIAS_METATYPES: + if node.metatype not in OPERATIONS_WITH_BIAS: return False add_node = nncf_graph.get_next_nodes(node)[0] @@ -318,7 +318,7 @@ def get_weight_channel_axes(node: NNCFNode, weights_port_id: int) -> List[int]: :param weights_port_id: Weight port id of the target node. :return: Axes numbers of the weight tensor which correspond to its channels. """ - if node.metatype not in GENERAL_WEIGHT_LAYER_METATYPES: + if node.metatype not in OPERATIONS_WITH_WEIGHTS: raise ValueError("Channel axis cannot be defined for operation without weights.") channel_axes = node.metatype.const_channel_axis diff --git a/nncf/openvino/hardware/fused_patterns.py b/nncf/openvino/hardware/fused_patterns.py index b6c08d719a8..7e47cb8fd18 100644 --- a/nncf/openvino/hardware/fused_patterns.py +++ b/nncf/openvino/hardware/fused_patterns.py @@ -13,11 +13,11 @@ from nncf.common.graph.patterns import HWFusedPatternNames from nncf.common.utils.registry import Registry from nncf.openvino.graph.metatypes import openvino_metatypes as om -from nncf.openvino.hardware.pattern_operations import ARITHMETIC_OPERATIONS -from nncf.openvino.hardware.pattern_operations import ATOMIC_ACTIVATIONS_OPERATIONS -from nncf.openvino.hardware.pattern_operations import BATCH_NORMALIZATION_OPERATIONS -from nncf.openvino.hardware.pattern_operations import ELEMENTWISE_OPERATIONS -from nncf.openvino.hardware.pattern_operations import LINEAR_OPERATIONS +from nncf.openvino.graph.metatypes.groups import ARITHMETIC_OPERATIONS +from nncf.openvino.graph.metatypes.groups import ATOMIC_ACTIVATIONS_OPERATIONS +from nncf.openvino.graph.metatypes.groups import BATCH_NORMALIZATION_OPERATIONS +from nncf.openvino.graph.metatypes.groups import ELEMENTWISE_OPERATIONS +from nncf.openvino.graph.metatypes.groups import LINEAR_OPERATIONS OPENVINO_HW_FUSED_PATTERNS = Registry("openvino") @@ -110,7 +110,7 @@ def create_normalize() -> GraphPattern: @OPENVINO_HW_FUSED_PATTERNS.register(HWFusedPatternNames.LINEAR_WITH_BIAS) def create_biased_op() -> GraphPattern: pattern = GraphPattern() - linear_node = pattern.add_node(**LINEAR_OPERATIONS) + linear_node = pattern.add_node(**{GraphPattern.METATYPE_ATTR: LINEAR_OPERATIONS, GraphPattern.LABEL_ATTR: "LINEAR"}) add_node = pattern.add_node(**{GraphPattern.LABEL_ATTR: "ADD_BIAS", GraphPattern.METATYPE_ATTR: om.OVAddMetatype}) pattern.add_edge(linear_node, add_node) @@ -156,7 +156,9 @@ def create_se_block() -> GraphPattern: reduce_mean_node = pattern.add_node( **{GraphPattern.LABEL_ATTR: "REDUCE_MEAN", GraphPattern.METATYPE_ATTR: om.OVReduceMeanMetatype} ) - linear_node_1 = pattern.add_node(**LINEAR_OPERATIONS) + linear_node_1 = pattern.add_node( + **{GraphPattern.METATYPE_ATTR: LINEAR_OPERATIONS, GraphPattern.LABEL_ATTR: "LINEAR"} + ) add_node_1 = pattern.add_node(**{GraphPattern.LABEL_ATTR: "ADD_BIAS", GraphPattern.METATYPE_ATTR: om.OVAddMetatype}) activation_node_1 = pattern.add_node( **{ @@ -164,7 +166,9 @@ def create_se_block() -> GraphPattern: GraphPattern.METATYPE_ATTR: [om.OVReluMetatype, om.OVPReluMetatype, om.OVSwishMetatype], } ) - linear_node_2 = pattern.add_node(**LINEAR_OPERATIONS) + linear_node_2 = pattern.add_node( + **{GraphPattern.METATYPE_ATTR: LINEAR_OPERATIONS, GraphPattern.LABEL_ATTR: "LINEAR"} + ) add_node_2 = pattern.add_node(**{GraphPattern.LABEL_ATTR: "ADD_BIAS", GraphPattern.METATYPE_ATTR: om.OVAddMetatype}) activation_node_2 = pattern.add_node( **{GraphPattern.LABEL_ATTR: "SIGMOID", GraphPattern.METATYPE_ATTR: om.OVSigmoidMetatype} @@ -206,7 +210,7 @@ def create_softmax_div() -> GraphPattern: @OPENVINO_HW_FUSED_PATTERNS.register(HWFusedPatternNames.HSWISH_ACTIVATION) def create_hswish() -> GraphPattern: pattern = GraphPattern() - linear_node = pattern.add_node(**LINEAR_OPERATIONS) + linear_node = pattern.add_node(**{GraphPattern.METATYPE_ATTR: LINEAR_OPERATIONS, GraphPattern.LABEL_ATTR: "LINEAR"}) add_node_1 = pattern.add_node(**{GraphPattern.LABEL_ATTR: "ADD_BIAS", GraphPattern.METATYPE_ATTR: om.OVAddMetatype}) add_node_2 = pattern.add_node(**{GraphPattern.LABEL_ATTR: "ADD", GraphPattern.METATYPE_ATTR: om.OVAddMetatype}) clamp_node = pattern.add_node(**{GraphPattern.LABEL_ATTR: "CLAMP", GraphPattern.METATYPE_ATTR: om.OVClampMetatype}) @@ -260,7 +264,7 @@ def create_hswish_pattern_2() -> GraphPattern: @OPENVINO_HW_FUSED_PATTERNS.register(HWFusedPatternNames.HSWISH_ACTIVATION_WITHOUT_DENOMINATOR) def create_hswish_without_denominator() -> GraphPattern: pattern = GraphPattern() - linear_node = pattern.add_node(**LINEAR_OPERATIONS) + linear_node = pattern.add_node(**{GraphPattern.METATYPE_ATTR: LINEAR_OPERATIONS, GraphPattern.LABEL_ATTR: "LINEAR"}) add_node_1 = pattern.add_node(**{GraphPattern.LABEL_ATTR: "ADD_BIAS", GraphPattern.METATYPE_ATTR: om.OVAddMetatype}) add_node_2 = pattern.add_node(**{GraphPattern.LABEL_ATTR: "ADD", GraphPattern.METATYPE_ATTR: om.OVAddMetatype}) clamp_node = pattern.add_node(**{GraphPattern.LABEL_ATTR: "CLAMP", GraphPattern.METATYPE_ATTR: om.OVClampMetatype}) @@ -279,7 +283,7 @@ def create_hswish_without_denominator() -> GraphPattern: @OPENVINO_HW_FUSED_PATTERNS.register(HWFusedPatternNames.SWISH_WITH_HARD_SIGMOID) def create_swish_with_hardsigmoid() -> GraphPattern: pattern = GraphPattern() - linear_node = pattern.add_node(**LINEAR_OPERATIONS) + linear_node = pattern.add_node(**{GraphPattern.METATYPE_ATTR: LINEAR_OPERATIONS, GraphPattern.LABEL_ATTR: "LINEAR"}) add_node = pattern.add_node(**{GraphPattern.LABEL_ATTR: "ADD_BIAS", GraphPattern.METATYPE_ATTR: om.OVAddMetatype}) hard_sigmoid_node = pattern.add_node( **{GraphPattern.LABEL_ATTR: "HARDSIGMOID", GraphPattern.METATYPE_ATTR: om.OVHardSigmoidMetatype} @@ -715,31 +719,35 @@ def create_linear_biased_activation_elementwise() -> GraphPattern: def elementwise_operations() -> GraphPattern: pattern = GraphPattern() - pattern.add_node(**ELEMENTWISE_OPERATIONS) + pattern.add_node(**{GraphPattern.METATYPE_ATTR: ELEMENTWISE_OPERATIONS, GraphPattern.LABEL_ATTR: "ELEMENTWISE"}) return pattern def linear_operations() -> GraphPattern: pattern = GraphPattern() - pattern.add_node(**LINEAR_OPERATIONS) + pattern.add_node(**{GraphPattern.METATYPE_ATTR: LINEAR_OPERATIONS, GraphPattern.LABEL_ATTR: "LINEAR"}) return pattern def batch_normalization_operations() -> GraphPattern: pattern = GraphPattern() - pattern.add_node(**BATCH_NORMALIZATION_OPERATIONS) + pattern.add_node( + **{GraphPattern.METATYPE_ATTR: BATCH_NORMALIZATION_OPERATIONS, GraphPattern.LABEL_ATTR: "BATCH_NORMALIZATION"} + ) return pattern def atomic_activations_operations() -> GraphPattern: pattern = GraphPattern() - pattern.add_node(**ATOMIC_ACTIVATIONS_OPERATIONS) + pattern.add_node( + **{GraphPattern.METATYPE_ATTR: ATOMIC_ACTIVATIONS_OPERATIONS, GraphPattern.LABEL_ATTR: "ATOMIC_ACTIVATIONS"} + ) return pattern def arithmetic_operations() -> GraphPattern: pattern = GraphPattern() - pattern.add_node(**ARITHMETIC_OPERATIONS) + pattern.add_node(**{GraphPattern.METATYPE_ATTR: ARITHMETIC_OPERATIONS, GraphPattern.LABEL_ATTR: "ARITHMETIC"}) return pattern diff --git a/nncf/openvino/hardware/pattern_operations.py b/nncf/openvino/hardware/pattern_operations.py deleted file mode 100644 index 1e05f577f92..00000000000 --- a/nncf/openvino/hardware/pattern_operations.py +++ /dev/null @@ -1,77 +0,0 @@ -# Copyright (c) 2023 Intel Corporation -# 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. - -from nncf.common.graph.patterns import GraphPattern -from nncf.openvino.graph.metatypes import openvino_metatypes as ov_metatypes - -LINEAR_OPERATIONS = { - GraphPattern.METATYPE_ATTR: [ - ov_metatypes.OVConvolutionMetatype, - ov_metatypes.OVGroupConvolutionMetatype, - ov_metatypes.OVDepthwiseConvolutionMetatype, - ov_metatypes.OVConvolutionBackpropDataMetatype, - ov_metatypes.OVGroupConvolutionBackpropDataMetatype, - ov_metatypes.OVDeformableConvolutionMetatype, - ov_metatypes.OVMatMulMetatype, - ], - GraphPattern.LABEL_ATTR: "LINEAR", -} - -BATCH_NORMALIZATION_OPERATIONS = { - GraphPattern.METATYPE_ATTR: [ov_metatypes.OVBatchNormMetatype], - GraphPattern.LABEL_ATTR: "BATCH_NORMALIZATION", -} - -ATOMIC_ACTIVATIONS_OPERATIONS = { - GraphPattern.METATYPE_ATTR: [ - ov_metatypes.OVReluMetatype, - ov_metatypes.OVClampMetatype, - ov_metatypes.OVEluMetatype, - ov_metatypes.OVPReluMetatype, - ov_metatypes.OVSigmoidMetatype, - ov_metatypes.OVHardSigmoidMetatype, - ov_metatypes.OVSwishMetatype, - ov_metatypes.OVHSwishMetatype, - ], - GraphPattern.LABEL_ATTR: "ATOMIC_ACTIVATIONS", -} - -ARITHMETIC_OPERATIONS = { - GraphPattern.METATYPE_ATTR: [ - ov_metatypes.OVAddMetatype, - ov_metatypes.OVSubtractMetatype, - ov_metatypes.OVMultiplyMetatype, - ov_metatypes.OVDivideMetatype, - ], - GraphPattern.LABEL_ATTR: "ARITHMETIC", -} - -ELEMENTWISE_OPERATIONS = { - GraphPattern.METATYPE_ATTR: [ - ov_metatypes.OVAddMetatype, - ov_metatypes.OVMultiplyMetatype, - ov_metatypes.OVSubtractMetatype, - ov_metatypes.OVDivideMetatype, - ov_metatypes.OVLessMetatype, - ov_metatypes.OVLessEqualMetatype, - ov_metatypes.OVGreaterMetatype, - ov_metatypes.OVGreaterEqualMetatype, - ov_metatypes.OVEqualMetatype, - ov_metatypes.OVNotEqualMetatype, - ov_metatypes.OVFloorModMetatype, - ov_metatypes.OVLogicalOrMetatype, - ov_metatypes.OVLogicalXorMetatype, - ov_metatypes.OVLogicalAndMetatype, - ov_metatypes.OVMaximumMetatype, - ov_metatypes.OVMinimumMetatype, - ], - GraphPattern.LABEL_ATTR: "ELEMENTWISE", -} diff --git a/nncf/openvino/quantization/default_quantization.py b/nncf/openvino/quantization/default_quantization.py index d7a30a5b2df..d60358e09fc 100644 --- a/nncf/openvino/quantization/default_quantization.py +++ b/nncf/openvino/quantization/default_quantization.py @@ -11,46 +11,13 @@ from nncf.common.quantization.quantizer_propagation.structs import QuantizationTrait from nncf.openvino.graph.metatypes import openvino_metatypes as ov_metatypes -from nncf.openvino.graph.metatypes.common import QUANTIZE_AGNOSTIC_OPERATIONS +from nncf.openvino.graph.metatypes.groups import INPUTS_QUANTIZABLE_OPERATIONS +from nncf.openvino.graph.metatypes.groups import QUANTIZE_AGNOSTIC_OPERATIONS # If a metatype is not in this list, then it is considered to be QuantizationTrait.NON_QUANTIZABLE. DEFAULT_OV_QUANT_TRAIT_TO_OP_DICT = { - QuantizationTrait.INPUTS_QUANTIZABLE: [ - ov_metatypes.OVConvolutionMetatype, - ov_metatypes.OVGroupConvolutionMetatype, - ov_metatypes.OVDepthwiseConvolutionMetatype, - ov_metatypes.OVConvolutionBackpropDataMetatype, - ov_metatypes.OVGroupConvolutionBackpropDataMetatype, - ov_metatypes.OVMatMulMetatype, - ov_metatypes.OVBatchNormMetatype, - ov_metatypes.OVAddMetatype, - ov_metatypes.OVSubtractMetatype, - ov_metatypes.OVMultiplyMetatype, - ov_metatypes.OVDivideMetatype, - ov_metatypes.OVMaximumMetatype, - ov_metatypes.OVMinimumMetatype, - ov_metatypes.OVAvgPoolMetatype, - ov_metatypes.OVReduceMeanMetatype, - ov_metatypes.OVMVNMetatype, - ov_metatypes.OVNormalizeL2Metatype, - ov_metatypes.OVInterpolateMetatype, - ov_metatypes.OVPowerMetatype, - ov_metatypes.OVFloorModMetatype, - ov_metatypes.OVLessMetatype, - ov_metatypes.OVLessEqualMetatype, - ov_metatypes.OVGreaterMetatype, - ov_metatypes.OVGreaterEqualMetatype, - ov_metatypes.OVEqualMetatype, - ov_metatypes.OVNotEqualMetatype, - ov_metatypes.OVLogicalNotMetatype, - ov_metatypes.OVLogicalAndMetatype, - ov_metatypes.OVLogicalOrMetatype, - ov_metatypes.OVLogicalXorMetatype, - ov_metatypes.OVSquaredDifferenceMetatype, - ov_metatypes.OVLSTMSequenceMetatype, - ov_metatypes.OVGRUSequenceMetatype, - ], + QuantizationTrait.INPUTS_QUANTIZABLE: INPUTS_QUANTIZABLE_OPERATIONS, QuantizationTrait.QUANTIZATION_AGNOSTIC: QUANTIZE_AGNOSTIC_OPERATIONS, QuantizationTrait.CONCAT: [ov_metatypes.OVConcatMetatype], QuantizationTrait.OUTPUT_QUANTIZATION_AS_WEIGHTS: [ov_metatypes.OVEmbeddingMetatype], diff --git a/nncf/quantization/algorithms/accuracy_control/openvino_backend.py b/nncf/quantization/algorithms/accuracy_control/openvino_backend.py index 29f99bdf992..c5c3190d4b4 100644 --- a/nncf/quantization/algorithms/accuracy_control/openvino_backend.py +++ b/nncf/quantization/algorithms/accuracy_control/openvino_backend.py @@ -18,12 +18,12 @@ from nncf.common.graph import NNCFGraph from nncf.common.graph import NNCFNode from nncf.openvino.graph.layer_attributes import OVLayerAttributes -from nncf.openvino.graph.metatypes.common import CONSTANT_OPERATIONS -from nncf.openvino.graph.metatypes.common import FAKE_QUANTIZE_OPERATIONS -from nncf.openvino.graph.metatypes.common import QUANTIZABLE_OPERATIONS -from nncf.openvino.graph.metatypes.common import QUANTIZE_AGNOSTIC_OPERATIONS -from nncf.openvino.graph.metatypes.common import SHAPEOF_OPERATIONS -from nncf.openvino.graph.metatypes.openvino_metatypes import GENERAL_WEIGHT_LAYER_METATYPES +from nncf.openvino.graph.metatypes.groups import CONSTANT_OPERATIONS +from nncf.openvino.graph.metatypes.groups import FAKE_QUANTIZE_OPERATIONS +from nncf.openvino.graph.metatypes.groups import INPUTS_QUANTIZABLE_OPERATIONS +from nncf.openvino.graph.metatypes.groups import OPERATIONS_WITH_WEIGHTS +from nncf.openvino.graph.metatypes.groups import QUANTIZE_AGNOSTIC_OPERATIONS +from nncf.openvino.graph.metatypes.groups import SHAPEOF_OPERATIONS from nncf.openvino.graph.metatypes.openvino_metatypes import OVConcatMetatype from nncf.openvino.graph.metatypes.openvino_metatypes import OVOpMetatype from nncf.openvino.graph.node_utils import get_bias_value @@ -69,7 +69,7 @@ def get_const_metatypes() -> List[OVOpMetatype]: @staticmethod def get_quantizable_metatypes() -> List[OVOpMetatype]: - return QUANTIZABLE_OPERATIONS + return INPUTS_QUANTIZABLE_OPERATIONS @staticmethod def get_quantize_agnostic_metatypes() -> List[OVOpMetatype]: @@ -87,7 +87,7 @@ def is_node_with_bias(node: NNCFNode, nncf_graph: NNCFGraph) -> bool: @staticmethod def is_node_with_weight(node: NNCFNode) -> bool: - return node.metatype in GENERAL_WEIGHT_LAYER_METATYPES and isinstance(node.layer_attributes, OVLayerAttributes) + return node.metatype in OPERATIONS_WITH_WEIGHTS and isinstance(node.layer_attributes, OVLayerAttributes) @staticmethod def get_bias_value(node_with_bias: NNCFNode, nncf_graph: NNCFGraph, model: ov.Model) -> np.ndarray: diff --git a/nncf/quantization/algorithms/bias_correction/openvino_backend.py b/nncf/quantization/algorithms/bias_correction/openvino_backend.py index 6b77d0260ea..c57f9df6a20 100644 --- a/nncf/quantization/algorithms/bias_correction/openvino_backend.py +++ b/nncf/quantization/algorithms/bias_correction/openvino_backend.py @@ -20,7 +20,7 @@ from nncf.common.tensor_statistics.collectors import ReductionShape from nncf.common.utils.backend import BackendType from nncf.experimental.common.tensor_statistics.collectors import TensorCollector -from nncf.openvino.graph.metatypes.common import FAKE_QUANTIZE_OPERATIONS +from nncf.openvino.graph.metatypes.groups import FAKE_QUANTIZE_OPERATIONS from nncf.openvino.graph.model_utils import insert_null_biases from nncf.openvino.graph.model_utils import remove_fq_from_inputs from nncf.openvino.graph.node_utils import get_bias_value diff --git a/nncf/quantization/algorithms/fast_bias_correction/openvino_backend.py b/nncf/quantization/algorithms/fast_bias_correction/openvino_backend.py index f76ef9a0c72..9436a7544f1 100644 --- a/nncf/quantization/algorithms/fast_bias_correction/openvino_backend.py +++ b/nncf/quantization/algorithms/fast_bias_correction/openvino_backend.py @@ -20,7 +20,7 @@ from nncf.common.tensor_statistics.collectors import ReductionShape from nncf.common.utils.backend import BackendType from nncf.experimental.common.tensor_statistics.collectors import TensorCollector -from nncf.openvino.graph.metatypes.common import FAKE_QUANTIZE_OPERATIONS +from nncf.openvino.graph.metatypes.groups import FAKE_QUANTIZE_OPERATIONS from nncf.openvino.graph.node_utils import get_bias_value from nncf.openvino.graph.node_utils import is_node_with_bias from nncf.openvino.graph.transformations.command_creation import OVCommandCreator diff --git a/nncf/quantization/algorithms/min_max/onnx_backend.py b/nncf/quantization/algorithms/min_max/onnx_backend.py index 73a504cf9e6..7f2a717164b 100644 --- a/nncf/quantization/algorithms/min_max/onnx_backend.py +++ b/nncf/quantization/algorithms/min_max/onnx_backend.py @@ -22,6 +22,7 @@ from nncf.common.quantization.structs import QuantizerConfig from nncf.common.utils.backend import BackendType from nncf.onnx.graph.metatypes import onnx_metatypes as om +from nncf.onnx.graph.metatypes.groups import MATMUL_METATYPES from nncf.onnx.graph.node_utils import get_input_edges_mapping from nncf.onnx.graph.node_utils import get_quantization_axis from nncf.onnx.graph.node_utils import get_quantized_tensor_shape @@ -49,7 +50,7 @@ class ONNXMinMaxAlgoBackend(MinMaxAlgoBackend): @property def mat_mul_metatypes(self) -> List[OperatorMetatype]: - return om.MATMUL_METATYPES + return MATMUL_METATYPES @property def post_processing_metatypes(self) -> List[OperatorMetatype]: @@ -65,7 +66,7 @@ def conv_metatypes(self) -> List[OperatorMetatype]: @property def overflow_fix_metatypes(self) -> List[OperatorMetatype]: - return [om.ONNXConvolutionMetatype, om.ONNXConvolutionTransposeMetatype, *om.MATMUL_METATYPES] + return [om.ONNXConvolutionMetatype, om.ONNXConvolutionTransposeMetatype, *MATMUL_METATYPES] @property def read_variable_metatypes(self) -> List[OperatorMetatype]: diff --git a/nncf/quantization/algorithms/min_max/openvino_backend.py b/nncf/quantization/algorithms/min_max/openvino_backend.py index d1be5753024..b5b974cfe33 100644 --- a/nncf/quantization/algorithms/min_max/openvino_backend.py +++ b/nncf/quantization/algorithms/min_max/openvino_backend.py @@ -26,7 +26,7 @@ from nncf.experimental.common.tensor_statistics.collectors import TensorCollector from nncf.openvino.graph.layer_attributes import OVLayerAttributes from nncf.openvino.graph.metatypes import openvino_metatypes as om -from nncf.openvino.graph.metatypes.openvino_metatypes import GENERAL_WEIGHT_LAYER_METATYPES +from nncf.openvino.graph.metatypes.groups import OPERATIONS_WITH_WEIGHTS from nncf.openvino.graph.node_utils import get_channel_agnostic_reduction_shape from nncf.openvino.graph.node_utils import get_weight_channel_axes from nncf.openvino.graph.transformations.commands import OVQuantizerInsertionCommand @@ -231,7 +231,7 @@ def get_weight_nodes(nncf_graph: NNCFGraph) -> List[NNCFNode]: return [ node for node in nncf_graph.get_all_nodes() - if isinstance(node.layer_attributes, OVLayerAttributes) and node.metatype in GENERAL_WEIGHT_LAYER_METATYPES + if isinstance(node.layer_attributes, OVLayerAttributes) and node.metatype in OPERATIONS_WITH_WEIGHTS ] @staticmethod diff --git a/tests/onnx/test_layer_attributes.py b/tests/onnx/test_layer_attributes.py index 685507c7a18..3b793256136 100644 --- a/tests/onnx/test_layer_attributes.py +++ b/tests/onnx/test_layer_attributes.py @@ -13,7 +13,7 @@ import onnx import pytest -from nncf.onnx.graph.metatypes.onnx_metatypes import GENERAL_WEIGHT_LAYER_METATYPES +from nncf.onnx.graph.metatypes.groups import OPERATIONS_WITH_WEIGHTS from nncf.onnx.graph.nncf_graph_builder import GraphConverter from nncf.onnx.graph.nncf_graph_builder import ONNXLayerAttributes from tests.onnx.models import OPSET_VERSION @@ -111,7 +111,7 @@ def test_layer_attributes(node_creator, ref_layer_attrs): onnx_model = get_one_layer_model(op_name, node_creator, input_shape) nncf_graph = GraphConverter.create_nncf_graph(onnx_model) node = nncf_graph.get_node_by_name(op_name) - if node.metatype in GENERAL_WEIGHT_LAYER_METATYPES: + if node.metatype in OPERATIONS_WITH_WEIGHTS: assert node.layer_attributes.__dict__ == ref_layer_attrs.__dict__ else: assert node.layer_attributes.__dict__ == ONNXLayerAttributes().__dict__ diff --git a/tests/openvino/native/test_metatypes.py b/tests/openvino/native/test_metatypes.py index 761c22c1145..e1a703b0f51 100644 --- a/tests/openvino/native/test_metatypes.py +++ b/tests/openvino/native/test_metatypes.py @@ -14,6 +14,7 @@ import pytest import nncf.openvino.graph.metatypes.openvino_metatypes as ovm +from nncf.openvino.graph.metatypes.groups import OPERATIONS_WITH_WEIGHTS from nncf.openvino.graph.nncf_graph_builder import GraphConverter from tests.openvino.native.models import ConvModel from tests.openvino.native.models import DepthwiseConv4DModel @@ -85,7 +86,7 @@ def test_determining_weights_port(): nncf_graph = GraphConverter.create_nncf_graph(model) counter = 0 for node in nncf_graph.get_all_nodes(): - if node.metatype not in ovm.GENERAL_WEIGHT_LAYER_METATYPES: + if node.metatype not in OPERATIONS_WITH_WEIGHTS: continue if node.layer_attributes and node.layer_attributes.constant_attributes: counter += 1