Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
 
"""
Tests quantization of InstanceNormalization operator.
"""
 
import unittest
 
import numpy as np
import onnx
from onnx import TensorProto, parser
from op_test_utils import TestDataFeeds, check_model_correctness, check_op_type_count, check_qtype_by_node_type
 
from onnxruntime.quantization import QuantFormat, QuantType, quantize_static
 
 
class TestOpInstanceNormalization(unittest.TestCase):
    """
    Class with test_* methods that test quantization of the InstanceNormalization operator.
    """
 
    def input_feeds(self, num_test_inputs, name2shape):
        """
        Returns a data reader of input test data.
 
        :param num_test_inputs: The number of testing inputs to generate.
        :param name2shape: A dictionary mapping the name of an input to its shape.
 
        :return: A data reader.
        """
        input_data_list = []
        for _ in range(num_test_inputs):
            inputs = {}
            for name, shape in name2shape.items():
                inputs.update({name: np.random.randint(-1, 2, shape).astype(np.float32)})
            input_data_list.extend([inputs])
        data_reader = TestDataFeeds(input_data_list)
        return data_reader
 
    def construct_model(self, output_model_path):
        """
        Constructs an ONNX model containing a single InstanceNormalization node, and saves
        the model to the specified output path.
 
        :param output_model_path: The output filepath in which to save the model.
        """
 
        model_description = """
        <
          ir_version: 8,
          opset_import: ["" : 13]
        >
        agraph (float[1, 3, 224, 224] input) => (float[1, 3, 224, 224] output)
        <float[3] scale = {1.0, 1.0, 1.0}, float[3] B = {0.0, 0.5, 1.0}>
        {
          output = InstanceNormalization<epsilon : float = 0.009999999776482582>(input, scale, B)
        }
        """
 
        model = parser.parse_model(model_description)
        onnx.save(model, output_model_path)
 
    def static_quant_test_qdq(
        self,
        model_fp32_path,
        data_reader,
        activation_type,
        weight_type,
        extra_options=None,
    ):
        """
        Quantizes an FP32 model and checks various properties about the generated quantized model.
 
        :param model_fp32_path: The path to the FP32 ONNX model to quantize and test.
        :param data_reader: Data reader that generates input data.
        :param activation_type: The quantized type for activations. One of QuantType.QUInt8 or QuantType.QInt8.
        :param weight_type: The quantized type for weights. One of QuantType.QUInt8 or QuantType.QInt8.
        :param extra_options: Dictionary of extra options for the quantize_static() function.
        """
 
        activation_proto_qtype = TensorProto.UINT8 if activation_type == QuantType.QUInt8 else TensorProto.INT8
        activation_type_str = "u8" if (activation_type == QuantType.QUInt8) else "s8"
        weight_type_str = "u8" if (weight_type == QuantType.QUInt8) else "s8"
        model_int8_path = f"instance_normalization_fp32.quant_dqd_{activation_type_str}{weight_type_str}.onnx"
 
        data_reader.rewind()
        quantize_static(
            model_fp32_path,
            model_int8_path,
            data_reader,
            quant_format=QuantFormat.QDQ,
            activation_type=activation_type,
            weight_type=weight_type,
            extra_options=extra_options,
        )
 
        # Check node counts in quantized model.
        quant_node_counts = {"InstanceNormalization": 1, "QuantizeLinear": 2, "DequantizeLinear": 4}
        check_op_type_count(self, model_int8_path, **quant_node_counts)
 
        # Check input/output types for QuantizeLinear nodes.
        qnode_io_qtypes = {
            "QuantizeLinear": [
                ["i", 2, activation_proto_qtype],
                ["o", 0, activation_proto_qtype],
            ]
        }
        check_qtype_by_node_type(self, model_int8_path, qnode_io_qtypes)
 
        # Check model correctness.
        data_reader.rewind()
        check_model_correctness(self, model_fp32_path, model_int8_path, data_reader.get_next())
 
    def test_quantize_instance_normalization(self):
        """
        Unit test that quantizes (uint8) an ONNX model containing an InstanceNormalization operator.
        """
 
        np.random.seed(1)
        model_fp32_path = "instance_normalization_fp32.onnx"
        self.construct_model(model_fp32_path)
        data_reader = self.input_feeds(1, {"input": [1, 3, 224, 224]})
 
        self.static_quant_test_qdq(
            model_fp32_path,
            data_reader,
            activation_type=QuantType.QUInt8,
            weight_type=QuantType.QUInt8,
        )
 
    def test_quantize_instance_normalization_s8s8(self):
        """
        Unit test that quantizes (int8) an ONNX model containing an InstanceNormalization operator.
        """
 
        np.random.seed(1)
        model_fp32_path = "instance_normalization_fp32.onnx"
        self.construct_model(model_fp32_path)
        data_reader = self.input_feeds(1, {"input": [1, 3, 224, 224]})
 
        self.static_quant_test_qdq(
            model_fp32_path,
            data_reader,
            activation_type=QuantType.QInt8,
            weight_type=QuantType.QInt8,
            extra_options={"ActivationSymmetric": True},
        )
 
 
if __name__ == "__main__":
    unittest.main()