From 631a7b8f2990a80b9b9473f999651b1808a7e7d4 Mon Sep 17 00:00:00 2001 From: Alexander Suslov Date: Mon, 18 Sep 2023 19:26:16 +0400 Subject: [PATCH] linear_squeeze_arithmetical_activations pattern (#2142) ### Changes - Added LINEAR_SQUEEZE_ARITHMETIC_ACTIVATIONS pattern - Added ONNXGlobalMaxPoolMetatype metatype for ONNX backend ### Reason for changes Fixed FakeQuantize operations placement ### Related tickets ref: 113396 ### Tests N/A --- nncf/common/graph/patterns/patterns.py | 1 + nncf/onnx/graph/metatypes/groups.py | 1 + nncf/onnx/graph/metatypes/onnx_metatypes.py | 7 +++++++ nncf/onnx/hardware/fused_patterns.py | 11 +++++++++++ nncf/openvino/hardware/fused_patterns.py | 11 +++++++++++ tests/torch/test_pattern_manager.py | 1 + 6 files changed, 32 insertions(+) diff --git a/nncf/common/graph/patterns/patterns.py b/nncf/common/graph/patterns/patterns.py index ae39925ad8d..4dce2ac8748 100644 --- a/nncf/common/graph/patterns/patterns.py +++ b/nncf/common/graph/patterns/patterns.py @@ -347,6 +347,7 @@ class HWFusedPatternNames(Enum): LINEAR_SCALE_SHIFT_ACTIVATIONS = PatternDesc("linear_scale_shift_activations") LINEAR_CONST_MULTIPLY = PatternDesc("linear_const_multiply") LINEAR_SQUEEZE_ACTIVATIONS = PatternDesc("linear_squeeze_activations") + LINEAR_SQUEEZE_ARITHMETIC_ACTIVATIONS = PatternDesc("linear_squeeze_arithmetic_activations") LINEAR_ACTIVATIONS_UNSQUEEZE_BN_SQUEEZE = PatternDesc("linear_activations_unsqueeze_bn_squeeze") SCALE_SHIFT_ACTIVATIONS = PatternDesc("scale_shift_activations") MVN_SCALE_SHIFT_ACTIVATIONS = PatternDesc("mvn_scale_shift_activations") diff --git a/nncf/onnx/graph/metatypes/groups.py b/nncf/onnx/graph/metatypes/groups.py index bac0669ebfa..404b65cf8a5 100644 --- a/nncf/onnx/graph/metatypes/groups.py +++ b/nncf/onnx/graph/metatypes/groups.py @@ -12,6 +12,7 @@ from nncf.onnx.graph.metatypes import onnx_metatypes QUANTIZE_AGNOSTIC_OPERATIONS = [ + onnx_metatypes.ONNXGlobalMaxPoolMetatype, onnx_metatypes.ONNXMaxPoolMetatype, onnx_metatypes.ONNXReduceMaxMetatype, onnx_metatypes.ONNXReshapeMetatype, diff --git a/nncf/onnx/graph/metatypes/onnx_metatypes.py b/nncf/onnx/graph/metatypes/onnx_metatypes.py index 8dec26aa857..b998b889dda 100644 --- a/nncf/onnx/graph/metatypes/onnx_metatypes.py +++ b/nncf/onnx/graph/metatypes/onnx_metatypes.py @@ -190,6 +190,13 @@ class ONNXAveragePoolMetatype(ONNXOpMetatype): hw_config_names = [HWConfigOpName.AVGPOOL] +@ONNX_OPERATION_METATYPES.register() +class ONNXGlobalMaxPoolMetatype(ONNXOpMetatype): + name = "GlobalMaxPoolOp" + op_names = ["GlobalMaxPool"] + hw_config_names = [HWConfigOpName.MAXPOOL] + + @ONNX_OPERATION_METATYPES.register() class ONNXMaxPoolMetatype(ONNXOpMetatype): name = "MaxPoolOp" diff --git a/nncf/onnx/hardware/fused_patterns.py b/nncf/onnx/hardware/fused_patterns.py index 1c9afad8e0e..c496a94bee3 100644 --- a/nncf/onnx/hardware/fused_patterns.py +++ b/nncf/onnx/hardware/fused_patterns.py @@ -339,6 +339,17 @@ def create_linear_squeeze_activation() -> GraphPattern: return linear +@ONNX_HW_FUSED_PATTERNS.register(HWFusedPatternNames.LINEAR_SQUEEZE_ARITHMETIC_ACTIVATIONS) +def create_linear_squeeze_arithmetic_activation() -> GraphPattern: + linear = linear_operations() + squeeze = squeeze_operation() + arithmetic_activations = create_arithmetic_activations() + + linear.join_patterns(squeeze) + linear.join_patterns(arithmetic_activations) + return linear + + @ONNX_HW_FUSED_PATTERNS.register(HWFusedPatternNames.BATCH_NORM_SCALE_SHIFT_ACTIVATIONS) def create_bn_scale_shift_activation() -> GraphPattern: batch_norm = batch_normalization_operations() diff --git a/nncf/openvino/hardware/fused_patterns.py b/nncf/openvino/hardware/fused_patterns.py index 7e47cb8fd18..dcf29333d28 100644 --- a/nncf/openvino/hardware/fused_patterns.py +++ b/nncf/openvino/hardware/fused_patterns.py @@ -595,6 +595,17 @@ def create_linear_squeeze_activation() -> GraphPattern: return linear +@OPENVINO_HW_FUSED_PATTERNS.register(HWFusedPatternNames.LINEAR_SQUEEZE_ARITHMETIC_ACTIVATIONS) +def create_linear_squeeze_arithmetic_activation() -> GraphPattern: + linear = linear_operations() + squeeze = squeeze_operation() + arithmetic_activations = create_arithmetic_activations() + + linear.join_patterns(squeeze) + linear.join_patterns(arithmetic_activations) + return linear + + @OPENVINO_HW_FUSED_PATTERNS.register(HWFusedPatternNames.MVN_SCALE_SHIFT_ACTIVATIONS) def create_mvn_scale_shift_activations() -> GraphPattern: pattern = GraphPattern() diff --git a/tests/torch/test_pattern_manager.py b/tests/torch/test_pattern_manager.py index c44ad738080..167a673b2fa 100644 --- a/tests/torch/test_pattern_manager.py +++ b/tests/torch/test_pattern_manager.py @@ -63,6 +63,7 @@ HWFusedPatternNames.LINEAR_BIASED_ACTIVATION_ELEMENTWISE: "Not relevant for Torch.", HWFusedPatternNames.MVN_SCALE_SHIFT_ACTIVATIONS: "Not relevant for Torch.", HWFusedPatternNames.LINEAR_SQUEEZE_ACTIVATIONS: "Not relevant for Torch.", + HWFusedPatternNames.LINEAR_SQUEEZE_ARITHMETIC_ACTIVATIONS: "Not relevant for Torch.", HWFusedPatternNames.LINEAR_ACTIVATIONS_UNSQUEEZE_BN_SQUEEZE: "Not relevant for Torch.", }