Исходный код СУБД 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/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.
# --------------------------------------------------------------------------
 
import sys
import tempfile
import unittest
from pathlib import Path
 
import numpy as np
import onnx
 
from onnxruntime.quantization.shape_inference import quant_pre_process
 
 
class TestUpsample(unittest.TestCase):
    def setUp(self):
        self.temp_dir = tempfile.TemporaryDirectory(prefix="ort.quant_preprocess_")
        self.temp_path = Path(self.temp_dir.name)
 
    def tearDown(self):
        self.temp_dir.cleanup()
 
    def build_upsample_model(self, input_shape=(1, 3, 32, 32)):
        """
        Build a model with deprecated Upsample op (opset <= 10) for testing version conversion.
        """
        input_tensor = onnx.helper.make_tensor_value_info("input", onnx.TensorProto.FLOAT, input_shape)
        output_shape = (input_shape[0], input_shape[1], input_shape[2] * 2, input_shape[3] * 2)
        output_tensor = onnx.helper.make_tensor_value_info("output", onnx.TensorProto.FLOAT, output_shape)
 
        # Create scales for upsample
        scales = np.array([1.0, 1.0, 2.0, 2.0], dtype=np.float32)
        scales_initializer = onnx.numpy_helper.from_array(scales, "scales")
 
        upsample_node = onnx.helper.make_node(
            "Upsample",
            ["input", "scales"],
            ["output"],
            name="upsample_node",
            mode="nearest",
        )
 
        graph = onnx.helper.make_graph(
            [upsample_node],
            "upsample_graph",
            [input_tensor],
            [output_tensor],
            initializer=[scales_initializer],
        )
        # Use opset 10 to trigger Upsample -> Resize conversion
        opset_imports = [onnx.helper.make_opsetid("", 10)]
        model = onnx.helper.make_model(graph, opset_imports=opset_imports)
        return model
 
    def test_upsample_to_resize_conversion(self):
        """Test that deprecated Upsample ops are converted to Resize ops."""
        model = self.build_upsample_model()
        input_path = self.temp_path / "input_model.onnx"
        onnx.save_model(model, input_path)
 
        self.assertEqual(model.graph.node[0].op_type, "Upsample")
        self.assertEqual(model.opset_import[0].version, 10)
 
        for skip_optimization in (True, False):
            with self.subTest(skip_optimization=skip_optimization):
                output_path = self.temp_path / f"preprocessed_model_{skip_optimization}.onnx"
                quant_pre_process(
                    input_model=str(input_path),
                    output_model_path=str(output_path),
                    skip_optimization=skip_optimization,
                    skip_onnx_shape=True,
                    skip_symbolic_shape=True,
                )
 
                self.assertTrue(output_path.exists())
                preprocessed_model = onnx.load(str(output_path))
                node_types = [node.op_type for node in preprocessed_model.graph.node]
                self.assertIn("Resize", node_types)
                self.assertNotIn("Upsample", node_types)
                self.assertGreaterEqual(preprocessed_model.opset_import[0].version, 11)
 
 
class TestClip(unittest.TestCase):
    def setUp(self):
        self.temp_dir = tempfile.TemporaryDirectory(prefix="ort.quant_preprocess_")
        self.temp_path = Path(self.temp_dir.name)
 
    def tearDown(self):
        self.temp_dir.cleanup()
 
    def build_clip_model(self, input_shape=(1, 3, 32, 32)):
        """
        Build a model with Clip op using ai.onnx v6 for testing version conversion.
        """
        input_tensor = onnx.helper.make_tensor_value_info("input", onnx.TensorProto.FLOAT, input_shape)
        output_tensor = onnx.helper.make_tensor_value_info("output", onnx.TensorProto.FLOAT, input_shape)
 
        # Create min and max values for clip
        min_val = np.array(0.0, dtype=np.float32)
        max_val = np.array(6.0, dtype=np.float32)
        min_initializer = onnx.numpy_helper.from_array(min_val, "min")
        max_initializer = onnx.numpy_helper.from_array(max_val, "max")
 
        clip_node = onnx.helper.make_node(
            "Clip",
            ["input", "min", "max"],
            ["output"],
            name="clip_node",
        )
 
        graph = onnx.helper.make_graph(
            [clip_node],
            "clip_graph",
            [input_tensor],
            [output_tensor],
            initializer=[min_initializer, max_initializer],
        )
        # Use opset 6 to trigger version conversion
        opset_imports = [onnx.helper.make_opsetid("", 6)]
        model = onnx.helper.make_model(graph, opset_imports=opset_imports)
        return model
 
    def test_clip_version_conversion(self):
        """
        Test that Clip op from ai.onnx v6 is upgraded to v11 after quant_pre_process.
        """
        model = self.build_clip_model()
        input_path = self.temp_path / "input_clip_model.onnx"
        output_path = self.temp_path / "preprocessed_clip_model.onnx"
 
        onnx.save_model(model, input_path)
 
        # Verify original model has Clip op with opset 6
        self.assertEqual(model.graph.node[0].op_type, "Clip")
        self.assertEqual(model.opset_import[0].version, 6)
 
        quant_pre_process(
            input_model=str(input_path),
            output_model_path=str(output_path),
            skip_optimization=True,
            skip_onnx_shape=True,
            skip_symbolic_shape=True,
        )
 
        self.assertTrue(output_path.exists())
        preprocessed_model = onnx.load(str(output_path))
 
        # Verify Clip op is still present and opset was upgraded to v11 or higher
        node_types = [node.op_type for node in preprocessed_model.graph.node]
        assert "Clip" in node_types
        assert preprocessed_model.opset_import[0].version >= 11
 
 
class TestSkipSymbolicShape(unittest.TestCase):
    """Verify that skip_symbolic_shape=True avoids importing sympy."""
 
    def setUp(self):
        self.temp_dir = tempfile.TemporaryDirectory(prefix="ort.quant_preprocess_skip_sympy_")
        self.temp_path = Path(self.temp_dir.name)
 
    def tearDown(self):
        self.temp_dir.cleanup()
 
    def build_simple_model(self):
        """Build a minimal identity model for testing."""
        input_tensor = onnx.helper.make_tensor_value_info("input", onnx.TensorProto.FLOAT, [1, 4])
        output_tensor = onnx.helper.make_tensor_value_info("output", onnx.TensorProto.FLOAT, [1, 4])
        identity_node = onnx.helper.make_node("Identity", ["input"], ["output"])
        graph = onnx.helper.make_graph([identity_node], "simple_graph", [input_tensor], [output_tensor])
        opset_imports = [onnx.helper.make_opsetid("", 13)]
        return onnx.helper.make_model(graph, opset_imports=opset_imports)
 
    def test_skip_symbolic_shape_does_not_require_sympy(self):
        """
        When skip_symbolic_shape=True, quant_pre_process must not attempt to
        import onnxruntime.tools.symbolic_shape_infer (which requires sympy).
        We verify this by installing a meta_path finder that raises
        ModuleNotFoundError for those modules — guaranteeing any fresh import
        attempt fails — and asserting the call succeeds without ever loading
        them.
        """
 
        class _BlockSympyAndSymbolicFinder:
            blocked_prefixes = ("sympy",)
            blocked_substrings = ("symbolic_shape_infer",)
 
            def find_spec(self, fullname, path=None, target=None):
                if fullname == "sympy" or fullname.startswith("sympy."):
                    raise ModuleNotFoundError(f"blocked by test: {fullname}")
                if "symbolic_shape_infer" in fullname:
                    raise ModuleNotFoundError(f"blocked by test: {fullname}")
                return None
 
        model = self.build_simple_model()
        input_path = self.temp_path / "simple_model.onnx"
        output_path = self.temp_path / "out_model.onnx"
        onnx.save_model(model, str(input_path))
 
        saved = {}
        for key in list(sys.modules.keys()):
            if key == "sympy" or key.startswith("sympy.") or "symbolic_shape_infer" in key:
                saved[key] = sys.modules.pop(key)
 
        blocker = _BlockSympyAndSymbolicFinder()
        sys.meta_path.insert(0, blocker)
        try:
            quant_pre_process(
                input_model=str(input_path),
                output_model_path=str(output_path),
                skip_optimization=True,
                skip_onnx_shape=True,
                skip_symbolic_shape=True,
            )
 
            for mod_name in list(sys.modules):
                self.assertFalse(
                    mod_name == "sympy" or mod_name.startswith("sympy."),
                    f"sympy was imported despite skip_symbolic_shape=True: {mod_name}",
                )
                self.assertNotIn(
                    "symbolic_shape_infer",
                    mod_name,
                    f"symbolic_shape_infer was imported despite skip_symbolic_shape=True: {mod_name}",
                )
        finally:
            sys.meta_path.remove(blocker)
            sys.modules.update(saved)
 
        self.assertTrue(output_path.exists(), "Output model should be created even without sympy")
 
 
if __name__ == "__main__":
    unittest.main()