Исходный код СУБД 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/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 for BertOnnxModel.clean_graph() optimizations."""
 
import unittest
 
import numpy as np
import onnx
from onnx import TensorProto, helper, numpy_helper
from parity_utilities import find_transformers_source
 
if find_transformers_source():
    from onnx_model_bert import BertOnnxModel
else:
    from onnxruntime.transformers.onnx_model_bert import BertOnnxModel
 
 
def _make_constantofshape_cast_model():
    """Create a model with ConstantOfShape → Cast pattern that clean_graph should merge.
 
    Graph structure (matching the pattern in clean_graph):
      input_ids → Shape → Gather(indices=0) → Unsqueeze ─────────┐
                    │                                              v
                    └→ Shape → Gather(indices=1) → Unsqueeze → Concat → ConstantOfShape → Cast(to=int64) → ReduceSum → output
 
    After clean_graph:
      input_ids → Shape → ConstantOfShape(value=int64(1)) → ReduceSum → output
    """
    batch_size = 2
    seq_len = 8
 
    # Graph inputs/outputs
    input_ids = helper.make_tensor_value_info("input_ids", TensorProto.INT64, [batch_size, seq_len])
    output = helper.make_tensor_value_info("output", TensorProto.INT64, [1])
 
    # Shape node
    shape_node = helper.make_node("Shape", inputs=["input_ids"], outputs=["shape_out"], name="shape_0")
 
    # Gather indices=0 (batch dim)
    gather_0_idx = numpy_helper.from_array(np.array(0, dtype=np.int64), name="gather_0_idx")
    gather_0 = helper.make_node(
        "Gather", inputs=["shape_out", "gather_0_idx"], outputs=["gather_0_out"], name="gather_0", axis=0
    )
 
    # Gather indices=1 (seq dim)
    gather_1_idx = numpy_helper.from_array(np.array(1, dtype=np.int64), name="gather_1_idx")
    gather_1 = helper.make_node(
        "Gather", inputs=["shape_out", "gather_1_idx"], outputs=["gather_1_out"], name="gather_1", axis=0
    )
 
    # Unsqueeze both
    unsqueeze_0_axes = numpy_helper.from_array(np.array([0], dtype=np.int64), name="unsqueeze_0_axes")
    unsqueeze_0 = helper.make_node(
        "Unsqueeze", inputs=["gather_0_out", "unsqueeze_0_axes"], outputs=["unsqueeze_0_out"], name="unsqueeze_0"
    )
 
    unsqueeze_1_axes = numpy_helper.from_array(np.array([0], dtype=np.int64), name="unsqueeze_1_axes")
    unsqueeze_1 = helper.make_node(
        "Unsqueeze", inputs=["gather_1_out", "unsqueeze_1_axes"], outputs=["unsqueeze_1_out"], name="unsqueeze_1"
    )
 
    # Concat
    concat = helper.make_node(
        "Concat", inputs=["unsqueeze_0_out", "unsqueeze_1_out"], outputs=["concat_out"], name="concat_0", axis=0
    )
 
    # ConstantOfShape with float value
    cos_value = numpy_helper.from_array(np.array([1.0], dtype=np.float32))
    constant_of_shape = helper.make_node(
        "ConstantOfShape",
        inputs=["concat_out"],
        outputs=["cos_out"],
        name="constant_of_shape_0",
        value=cos_value,
    )
 
    # Cast to int64
    cast = helper.make_node("Cast", inputs=["cos_out"], outputs=["cast_out"], name="cast_0", to=TensorProto.INT64)
 
    # ReduceSum as consumer (index 0 matches op_input_id)
    reduce_sum = helper.make_node("ReduceSum", inputs=["cast_out"], outputs=["output"], name="reduce_sum_0", keepdims=0)
 
    nodes = [shape_node, gather_0, gather_1, unsqueeze_0, unsqueeze_1, concat, constant_of_shape, cast, reduce_sum]
    initializers = [gather_0_idx, gather_1_idx, unsqueeze_0_axes, unsqueeze_1_axes]
 
    graph = helper.make_graph(nodes, "cos_cast_test", [input_ids], [output], initializer=initializers)
    model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", 16)])
    model = onnx.shape_inference.infer_shapes(model)
    return model
 
 
class TestCleanGraph(unittest.TestCase):
    """Tests for BertOnnxModel.clean_graph() optimizations."""
 
    def _get_node_by_op(self, model, op_type):
        """Find all nodes of a given op type."""
        return [n for n in model.graph.node if n.op_type == op_type]
 
    def test_constantofshape_cast_merge(self):
        """ConstantOfShape → Cast should be merged: Cast removed, ConstantOfShape produces target type."""
        model = _make_constantofshape_cast_model()
 
        # Verify precondition: Cast node exists
        cast_nodes = self._get_node_by_op(model, "Cast")
        self.assertEqual(len(cast_nodes), 1, "Expected 1 Cast node before clean_graph")
 
        cos_nodes = self._get_node_by_op(model, "ConstantOfShape")
        self.assertEqual(len(cos_nodes), 1)
        cos_value = numpy_helper.to_array(cos_nodes[0].attribute[0].t)
        self.assertEqual(cos_value.dtype, np.float32, "ConstantOfShape should start as float32")
 
        # Run clean_graph
        bert_model = BertOnnxModel(model)
        bert_model.clean_graph()
        bert_model.prune_graph()
        cleaned = bert_model.model
 
        # Verify: Cast node should be removed
        cast_nodes_after = self._get_node_by_op(cleaned, "Cast")
        self.assertEqual(len(cast_nodes_after), 0, "Cast node should be removed after merge")
 
        # Verify: ConstantOfShape now produces int64
        cos_nodes_after = self._get_node_by_op(cleaned, "ConstantOfShape")
        self.assertEqual(len(cos_nodes_after), 1, "ConstantOfShape should still exist")
        cos_value_after = numpy_helper.to_array(cos_nodes_after[0].attribute[0].t)
        self.assertEqual(cos_value_after.dtype, np.int64, "ConstantOfShape should produce int64 after merge")
        self.assertEqual(cos_value_after.flat[0], 1, "Fill value should be preserved as 1")
 
    def test_constantofshape_cast_merge_preserves_fill_value(self):
        """The fill value should be preserved across the type change."""
        model = _make_constantofshape_cast_model()
 
        # Change the fill value to something other than 1.0
        for node in model.graph.node:
            if node.op_type == "ConstantOfShape":
                new_val = numpy_helper.from_array(np.array([0.0], dtype=np.float32))
                node.attribute[0].t.CopyFrom(new_val)
 
        bert_model = BertOnnxModel(model)
        bert_model.clean_graph()
        bert_model.prune_graph()
        cleaned = bert_model.model
 
        cos_nodes = self._get_node_by_op(cleaned, "ConstantOfShape")
        self.assertEqual(len(cos_nodes), 1)
        cos_value = numpy_helper.to_array(cos_nodes[0].attribute[0].t)
        self.assertEqual(cos_value.flat[0], 0, "Fill value 0 should be preserved")
        self.assertEqual(cos_value.dtype, np.int64)
 
    def test_concat_path_simplified(self):
        """The Concat → Unsqueeze → Gather path should be simplified to just Shape."""
        model = _make_constantofshape_cast_model()
 
        bert_model = BertOnnxModel(model)
        bert_model.clean_graph()
        bert_model.prune_graph()
        cleaned = bert_model.model
 
        # Concat, Unsqueeze, and Gather should be pruned
        concat_nodes = self._get_node_by_op(cleaned, "Concat")
        self.assertEqual(len(concat_nodes), 0, "Concat should be pruned")
 
        gather_nodes = self._get_node_by_op(cleaned, "Gather")
        self.assertEqual(len(gather_nodes), 0, "Gather should be pruned")
 
        # Shape and ConstantOfShape should remain
        shape_nodes = self._get_node_by_op(cleaned, "Shape")
        self.assertGreaterEqual(len(shape_nodes), 1, "Shape node should remain")
 
        cos_nodes = self._get_node_by_op(cleaned, "ConstantOfShape")
        self.assertEqual(len(cos_nodes), 1, "ConstantOfShape should remain")
 
    def test_attention_mask_cleanup_after_cast_merge(self):
        """Attention mask_index path should be removed even after Cast is merged.
 
        When an Attention node consumes ReduceSum → Cast → ConstantOfShape → Shape
        on input[3], the Cast merge fires first (folding Cast into ConstantOfShape).
        The Attention cleanup must still match the post-merge pattern
        (ReduceSum → ConstantOfShape → Shape) and remove the mask_index input.
        """
        batch_size = 2
        seq_len = 8
        hidden_size = 16
 
        input_ids = helper.make_tensor_value_info("input_ids", TensorProto.INT64, [batch_size, seq_len])
        attn_output = helper.make_tensor_value_info("attn_output", TensorProto.FLOAT, None)
 
        # Shape → Gather → Unsqueeze → Concat → ConstantOfShape → Cast → ReduceSum
        # (same mask path as _make_constantofshape_cast_model)
        shape_node = helper.make_node("Shape", inputs=["input_ids"], outputs=["shape_out"], name="shape_0")
 
        gather_0_idx = numpy_helper.from_array(np.array(0, dtype=np.int64), name="gather_0_idx")
        gather_0 = helper.make_node(
            "Gather", inputs=["shape_out", "gather_0_idx"], outputs=["gather_0_out"], name="gather_0", axis=0
        )
        gather_1_idx = numpy_helper.from_array(np.array(1, dtype=np.int64), name="gather_1_idx")
        gather_1 = helper.make_node(
            "Gather", inputs=["shape_out", "gather_1_idx"], outputs=["gather_1_out"], name="gather_1", axis=0
        )
 
        unsqueeze_0_axes = numpy_helper.from_array(np.array([0], dtype=np.int64), name="unsqueeze_0_axes")
        unsqueeze_0 = helper.make_node(
            "Unsqueeze", inputs=["gather_0_out", "unsqueeze_0_axes"], outputs=["unsqueeze_0_out"], name="unsqueeze_0"
        )
        unsqueeze_1_axes = numpy_helper.from_array(np.array([0], dtype=np.int64), name="unsqueeze_1_axes")
        unsqueeze_1 = helper.make_node(
            "Unsqueeze", inputs=["gather_1_out", "unsqueeze_1_axes"], outputs=["unsqueeze_1_out"], name="unsqueeze_1"
        )
 
        concat = helper.make_node(
            "Concat", inputs=["unsqueeze_0_out", "unsqueeze_1_out"], outputs=["concat_out"], name="concat_0", axis=0
        )
 
        cos_value = numpy_helper.from_array(np.array([1.0], dtype=np.float32))
        constant_of_shape = helper.make_node(
            "ConstantOfShape", inputs=["concat_out"], outputs=["cos_out"], name="cos_0", value=cos_value
        )
        cast = helper.make_node("Cast", inputs=["cos_out"], outputs=["cast_out"], name="cast_0", to=TensorProto.INT64)
        reduce_sum = helper.make_node(
            "ReduceSum", inputs=["cast_out"], outputs=["reduce_sum_out"], name="reduce_sum_0", keepdims=0
        )
 
        # Dummy QKV initializers for Attention inputs[0:3]
        qkv_data = numpy_helper.from_array(
            np.zeros([batch_size, seq_len, hidden_size], dtype=np.float32), name="qkv_input"
        )
        weight_data = numpy_helper.from_array(np.zeros([hidden_size, hidden_size], dtype=np.float32), name="weight")
        bias_data = numpy_helper.from_array(np.zeros([hidden_size], dtype=np.float32), name="bias")
 
        # Attention node with mask_index at input[3]
        attention = helper.make_node(
            "Attention",
            inputs=["qkv_input", "weight", "bias", "reduce_sum_out"],
            outputs=["attn_output"],
            name="attention_0",
            num_heads=2,
        )
        attention.domain = "com.microsoft"
 
        nodes = [
            shape_node,
            gather_0,
            gather_1,
            unsqueeze_0,
            unsqueeze_1,
            concat,
            constant_of_shape,
            cast,
            reduce_sum,
            attention,
        ]
        initializers = [
            gather_0_idx,
            gather_1_idx,
            unsqueeze_0_axes,
            unsqueeze_1_axes,
            qkv_data,
            weight_data,
            bias_data,
        ]
 
        graph = helper.make_graph(nodes, "attention_mask_test", [input_ids], [attn_output], initializer=initializers)
        model = helper.make_model(
            graph,
            opset_imports=[helper.make_opsetid("", 16), helper.make_opsetid("com.microsoft", 1)],
        )
 
        bert_model = BertOnnxModel(model, num_heads=2, hidden_size=hidden_size)
        bert_model.clean_graph()
        bert_model.prune_graph()
        cleaned = bert_model.model
 
        # Cast should be merged
        cast_nodes = self._get_node_by_op(cleaned, "Cast")
        self.assertEqual(len(cast_nodes), 0, "Cast node should be removed after merge")
 
        # Attention node should have mask_index removed (3 inputs instead of 4)
        attn_nodes = self._get_node_by_op(cleaned, "Attention")
        self.assertEqual(len(attn_nodes), 1, "Should have exactly 1 Attention node")
        self.assertEqual(
            len(attn_nodes[0].input),
            3,
            f"Attention should have 3 inputs (mask removed), got {len(attn_nodes[0].input)}",
        )
 
 
if __name__ == "__main__":
    unittest.main()