Skip to content

Commit 3412fa2

Browse files
authored
Add functional test of ONNX PTQ of segmentation models (#1160)
### Changes Add graph test for unet, icnet. ### Reason for changes Extend support of ONNX PTQ models ### Related tickets 83446 ### Tests As stated in the title
1 parent da72001 commit 3412fa2

21 files changed

+2860
-1234
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*.png filter=lfs diff=lfs merge=lfs -text
22
*.jpg filter=lfs diff=lfs merge=lfs -text
33
*.pb filter=lfs diff=lfs merge=lfs -text
4+
*.onnx filter=lfs diff=lfs merge=lfs -text
45

56
* text=auto eol=lf
67
*.{tfrecord,h5} binary

nncf/experimental/onnx/algorithms/quantization/default_quantization.py

+24-18
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,36 @@
1212
"""
1313

1414
from nncf.common.quantization.quantizer_propagation.structs import QuantizationTrait
15-
from nncf.experimental.onnx.graph.metatypes.onnx_ops import ConvolutionMetatype
16-
from nncf.experimental.onnx.graph.metatypes.onnx_ops import LinearMetatype
17-
from nncf.experimental.onnx.graph.metatypes.onnx_ops import SigmoidMetatype
18-
from nncf.experimental.onnx.graph.metatypes.onnx_ops import GlobalAveragePoolMetatype
19-
from nncf.experimental.onnx.graph.metatypes.onnx_ops import AddLayerMetatype
20-
from nncf.experimental.onnx.graph.metatypes.onnx_ops import MulLayerMetatype
21-
from nncf.experimental.onnx.graph.metatypes.onnx_ops import ConcatLayerMetatype
22-
from nncf.experimental.onnx.graph.metatypes.onnx_ops import BatchNormMetatype
23-
from nncf.experimental.onnx.graph.metatypes.onnx_ops import ResizeMetatype
15+
from nncf.experimental.onnx.graph.metatypes.onnx_ops import ONNXConvolutionMetatype
16+
from nncf.experimental.onnx.graph.metatypes.onnx_ops import ONNXLinearMetatype
17+
from nncf.experimental.onnx.graph.metatypes.onnx_ops import ONNXSigmoidMetatype
18+
from nncf.experimental.onnx.graph.metatypes.onnx_ops import ONNXHardSigmoidMetatype
19+
from nncf.experimental.onnx.graph.metatypes.onnx_ops import ONNXAveragePoolMetatype
20+
from nncf.experimental.onnx.graph.metatypes.onnx_ops import ONNXGlobalAveragePoolMetatype
21+
from nncf.experimental.onnx.graph.metatypes.onnx_ops import ONNXAddLayerMetatype
22+
from nncf.experimental.onnx.graph.metatypes.onnx_ops import ONNXMulLayerMetatype
23+
from nncf.experimental.onnx.graph.metatypes.onnx_ops import ONNXConcatLayerMetatype
24+
from nncf.experimental.onnx.graph.metatypes.onnx_ops import ONNXBatchNormMetatype
25+
from nncf.experimental.onnx.graph.metatypes.onnx_ops import ONNXResizeMetatype
26+
from nncf.experimental.onnx.graph.metatypes.onnx_ops import ONNXSoftmaxMetatype
2427

2528
from nncf.common.graph.operator_metatypes import UnknownMetatype
2629

2730
DEFAULT_ONNX_QUANT_TRAIT_TO_OP_DICT = {
2831
QuantizationTrait.INPUTS_QUANTIZABLE: [
29-
ConvolutionMetatype,
30-
LinearMetatype,
31-
GlobalAveragePoolMetatype,
32-
AddLayerMetatype,
33-
MulLayerMetatype,
34-
BatchNormMetatype,
35-
ResizeMetatype,
32+
ONNXConvolutionMetatype,
33+
ONNXLinearMetatype,
34+
ONNXAveragePoolMetatype,
35+
ONNXGlobalAveragePoolMetatype,
36+
ONNXAddLayerMetatype,
37+
ONNXMulLayerMetatype,
38+
ONNXBatchNormMetatype,
39+
ONNXHardSigmoidMetatype,
40+
ONNXResizeMetatype,
3641
],
37-
QuantizationTrait.NON_QUANTIZABLE: [SigmoidMetatype,
42+
QuantizationTrait.NON_QUANTIZABLE: [ONNXSigmoidMetatype,
43+
ONNXSoftmaxMetatype,
3844
UnknownMetatype],
39-
QuantizationTrait.CONCAT: [ConcatLayerMetatype],
45+
QuantizationTrait.CONCAT: [ONNXConcatLayerMetatype],
4046
QuantizationTrait.OUTPUT_QUANTIZATION_AS_WEIGHTS: []
4147
}

nncf/experimental/onnx/graph/metatypes/onnx_ops.py

+38-18
Original file line numberDiff line numberDiff line change
@@ -30,114 +30,134 @@ def get_all_aliases(cls) -> List[str]:
3030

3131

3232
@ONNX_OPERATION_METATYPES.register()
33-
class ConvolutionMetatype(ONNXOpMetatype):
33+
class ONNXConvolutionMetatype(ONNXOpMetatype):
3434
name = 'ConvOp'
3535
op_names = ['Conv']
3636
hw_config_names = [HWConfigOpName.CONVOLUTION]
3737

3838

3939
@ONNX_OPERATION_METATYPES.register()
40-
class LinearMetatype(ONNXOpMetatype):
40+
class ONNXLinearMetatype(ONNXOpMetatype):
4141
name = 'LinearOp'
4242
op_names = ['Gemm']
4343
hw_config_names = [HWConfigOpName.MATMUL]
4444

4545

4646
@ONNX_OPERATION_METATYPES.register()
47-
class ReluMetatype(ONNXOpMetatype):
47+
class ONNXReluMetatype(ONNXOpMetatype):
4848
name = 'ReluOp'
4949
op_names = ['Relu', 'Clip']
5050

5151

5252
@ONNX_OPERATION_METATYPES.register()
53-
class SigmoidMetatype(ONNXOpMetatype):
53+
class ONNXSigmoidMetatype(ONNXOpMetatype):
5454
name = 'SigmoidOp'
5555
op_names = ['Sigmoid']
5656

5757

5858
@ONNX_OPERATION_METATYPES.register()
59-
class GlobalAveragePoolMetatype(ONNXOpMetatype):
59+
class ONNXHardSigmoidMetatype(ONNXOpMetatype):
60+
name = 'HardSigmoidOp'
61+
op_names = ['HardSigmoid']
62+
63+
64+
@ONNX_OPERATION_METATYPES.register()
65+
class ONNXGlobalAveragePoolMetatype(ONNXOpMetatype):
6066
name = 'GlobalAveragePoolOp'
6167
op_names = ['GlobalAveragePool']
6268
hw_config_names = [HWConfigOpName.AVGPOOL]
6369

6470

6571
@ONNX_OPERATION_METATYPES.register()
66-
class MaxPoolMetatype(ONNXOpMetatype):
72+
class ONNXAveragePoolMetatype(ONNXOpMetatype):
73+
name = 'AveragePoolOp'
74+
op_names = ['AveragePool']
75+
hw_config_names = [HWConfigOpName.AVGPOOL]
76+
77+
78+
@ONNX_OPERATION_METATYPES.register()
79+
class ONNXMaxPoolMetatype(ONNXOpMetatype):
6780
name = 'MaxPoolOp'
6881
op_names = ['MaxPool']
6982
hw_config_names = [HWConfigOpName.MAXPOOL]
7083

7184

7285
@ONNX_OPERATION_METATYPES.register()
73-
class ConstantMetatype(ONNXOpMetatype):
86+
class ONNXConstantMetatype(ONNXOpMetatype):
7487
name = 'ConstantOp'
7588
op_names = ['Constant']
7689

7790

7891
@ONNX_OPERATION_METATYPES.register()
79-
class AddLayerMetatype(ONNXOpMetatype):
92+
class ONNXAddLayerMetatype(ONNXOpMetatype):
8093
name = 'AddOp'
8194
op_names = ['Add']
8295
hw_config_names = [HWConfigOpName.ADD]
8396

8497

8598
@ONNX_OPERATION_METATYPES.register()
86-
class MulLayerMetatype(ONNXOpMetatype):
99+
class ONNXMulLayerMetatype(ONNXOpMetatype):
87100
name = 'MulOp'
88101
op_names = ['Mul']
89102
hw_config_names = [HWConfigOpName.MULTIPLY]
90103

91104

92105
@ONNX_OPERATION_METATYPES.register()
93-
class SumMetatype(ONNXOpMetatype):
106+
class ONNXSumMetatype(ONNXOpMetatype):
94107
name = 'SumOp'
95108
op_names = ['Sum']
96109
hw_config_names = [HWConfigOpName.REDUCESUM]
97110

98111

99112
@ONNX_OPERATION_METATYPES.register()
100-
class ConcatLayerMetatype(ONNXOpMetatype):
113+
class ONNXConcatLayerMetatype(ONNXOpMetatype):
101114
name = 'ConcatOp'
102115
op_names = ['Concat']
103116
hw_config_names = [HWConfigOpName.CONCAT]
104117

105118

106119
@ONNX_OPERATION_METATYPES.register()
107-
class BatchNormMetatype(ONNXOpMetatype):
120+
class ONNXBatchNormMetatype(ONNXOpMetatype):
108121
name = 'BatchNormalizationOp'
109122
op_names = ['BatchNormalization']
110123

111124

112125
@ONNX_OPERATION_METATYPES.register()
113-
class ResizeMetatype(ONNXOpMetatype):
126+
class ONNXResizeMetatype(ONNXOpMetatype):
114127
name = 'ResizeOp'
115128
op_names = ['Resize']
129+
hw_config_names = [HWConfigOpName.INTERPOLATE]
116130

117131

118132
@ONNX_OPERATION_METATYPES.register()
119-
class ReshapeMetatype(ONNXOpMetatype):
133+
class ONNXReshapeMetatype(ONNXOpMetatype):
120134
name = 'ReshapeOp'
121135
op_names = ['Reshape']
122136
hw_config_names = [HWConfigOpName.RESHAPE]
123137

124138

125139
@ONNX_OPERATION_METATYPES.register()
126-
class TransposeMetatype(ONNXOpMetatype):
140+
class ONNXTransposeMetatype(ONNXOpMetatype):
127141
name = 'TransposeOp'
128142
op_names = ['Transpose']
129143
hw_config_names = [HWConfigOpName.TRANSPOSE]
130144

131145

132146
@ONNX_OPERATION_METATYPES.register()
133-
class FlattenMetatype(ONNXOpMetatype):
147+
class ONNXFlattenMetatype(ONNXOpMetatype):
134148
name = 'FlattenOp'
135149
op_names = ['Flatten']
136150
hw_config_names = [HWConfigOpName.FLATTEN]
137151

138152

139-
GENERAL_WEIGHT_LAYER_METATYPES = [ConvolutionMetatype,
140-
LinearMetatype]
153+
@ONNX_OPERATION_METATYPES.register()
154+
class ONNXSoftmaxMetatype(ONNXOpMetatype):
155+
name = 'SoftmaxOp'
156+
op_names = ['Softmax']
157+
158+
159+
GENERAL_WEIGHT_LAYER_METATYPES = [ONNXConvolutionMetatype,
160+
ONNXLinearMetatype]
141161

142162

143163
def get_operator_metatypes() -> List[Type[OperatorMetatype]]:

nncf/experimental/onnx/graph/nncf_graph_builder.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
from nncf.experimental.onnx.graph.onnx_graph import ONNXGraph
2828
from nncf.experimental.onnx.graph.metatypes.onnx_ops import ONNX_OPERATION_METATYPES
29-
from nncf.experimental.onnx.graph.metatypes.onnx_ops import ConstantMetatype
29+
from nncf.experimental.onnx.graph.metatypes.onnx_ops import ONNXConstantMetatype
3030

3131

3232
class GraphConverter:
@@ -48,7 +48,7 @@ def create_nncf_graph(onnx_model: ModelProto) -> NNCFGraph:
4848
node_name = node.name
4949
node_type = node.op_type
5050
metatype = ONNX_OPERATION_METATYPES.get_operator_metatype_by_op_name(node_type)
51-
if metatype == ConstantMetatype: # We don't need to quantize Constants
51+
if metatype == ONNXConstantMetatype: # We don't need to quantize Constants
5252
continue
5353
nncf_graph.add_nncf_node(node_name=node_name,
5454
node_type=node_type,

nncf/experimental/onnx/hardware/fused_patterns.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from nncf.experimental.onnx.hardware.pattern_operations import ARITHMETIC_OPERATIONS
2121
from nncf.experimental.onnx.hardware.pattern_operations import MATMUL_OPERATIONS
2222

23-
from nncf.experimental.onnx.hardware.patterns import create_h_sigmoid_act
23+
from nncf.experimental.onnx.hardware.patterns import create_swish_activation
2424
from nncf.experimental.onnx.hardware.patterns import create_input_preprocessing_pattern
2525

2626

@@ -41,8 +41,8 @@ def _get_onnx_hw_fused_patterns() -> HWFusedPatterns:
4141

4242
atomic_activations = GraphPattern()
4343
atomic_activations.add_node(**ATOMIC_ACTIVATIONS_OPERATIONS)
44-
h_sigmoid = create_h_sigmoid_act()
45-
activations = atomic_activations | h_sigmoid
44+
swish = create_swish_activation()
45+
activations = atomic_activations | swish
4646
hw_fused_patterns.register(activations, 'ACTIVATIONS', match=False)
4747

4848
arithmetic_ops = GraphPattern()

nncf/experimental/onnx/hardware/pattern_operations.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
from nncf.common.graph.patterns import merge_two_types_of_operations
1515

1616
LINEAR_OPERATIONS = {'type': ['Conv',
17-
'ConvTranspose'
17+
'ConvTranspose',
18+
'Gemm'
1819
],
1920
'label': 'LINEAR'}
2021

nncf/experimental/onnx/hardware/patterns.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,25 @@
1515
from nncf.common.graph.patterns import GraphPattern
1616

1717

18-
def create_h_sigmoid_act() -> GraphPattern:
18+
def create_swish_activation() -> GraphPattern:
1919
pattern = GraphPattern()
2020

21-
input_pattern_node = pattern.add_node(label='*INPUT_NODE*', type=GraphPattern.NON_PATTERN_NODE_TYPE)
22-
sigmoid_node = pattern.add_node(label='SIGMOID', type='Sigmoid')
23-
mul_node = pattern.add_node(label='MUL', type='Mul')
21+
input_pattern_node_1 = pattern.add_node(label='*INPUT_NODE*', type=GraphPattern.NON_PATTERN_NODE_TYPE)
22+
sigmoid_node_1 = pattern.add_node(label='SIGMOID', type='Sigmoid')
23+
mul_node_1 = pattern.add_node(label='MUL', type='Mul')
24+
25+
pattern.add_edge(input_pattern_node_1, sigmoid_node_1)
26+
pattern.add_edge(input_pattern_node_1, mul_node_1)
27+
pattern.add_edge(sigmoid_node_1, mul_node_1)
28+
29+
input_pattern_node_2 = pattern.add_node(label='*INPUT_NODE*', type=GraphPattern.NON_PATTERN_NODE_TYPE)
30+
sigmoid_node_2 = pattern.add_node(label='HARDSIGMOID', type='HardSigmoid')
31+
mul_node_2 = pattern.add_node(label='MUL', type='Mul')
32+
33+
pattern.add_edge(input_pattern_node_2, sigmoid_node_2)
34+
pattern.add_edge(input_pattern_node_2, mul_node_2)
35+
pattern.add_edge(sigmoid_node_2, mul_node_2)
2436

25-
pattern.add_edge(input_pattern_node, sigmoid_node)
26-
pattern.add_edge(input_pattern_node, mul_node)
27-
pattern.add_edge(sigmoid_node, mul_node)
2837
return pattern
2938

3039

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:850a301f8a10fdf8518901771c4046b7338b50f267cbcb08d44f2bbd29634671
3+
size 26922489
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:87befe217358b6beda0b496536b17216ebddef8f70e8d86fe34ed089bb577289
3+
size 63480982
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:defa8b5aebf1056dbfed1defbb1f02e9db578ee3d331c3a1a74dc3bda079d742
3+
size 124241889
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:a2c44ecf4860acdf03193d41b7d2957637d0b14b8a9e339463b892b0acb9a12f
3+
size 203948401

0 commit comments

Comments
 (0)