Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 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
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation.  All rights reserved.
# Licensed under the MIT License.  See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
 
import os
import unittest
 
import numpy as np
import onnx
from onnx import TensorProto, helper
from parity_utilities import find_transformers_source
 
if find_transformers_source():
    from fusion_options import FusionOptions
    from onnx_model import OnnxModel
    from optimizer import optimize_model
else:
    from onnxruntime.transformers.fusion_options import FusionOptions
    from onnxruntime.transformers.onnx_model import OnnxModel
    from onnxruntime.transformers.optimizer import optimize_model
 
 
onnxdomain = onnx.OperatorSetIdProto()
onnxdomain.version = 12
# The empty string ("") or absence of this field implies the operator set that is defined as part of the ONNX specification.
onnxdomain.domain = ""
msdomain = onnx.OperatorSetIdProto()
msdomain.version = 1
msdomain.domain = "com.microsoft"
opsets = [onnxdomain, msdomain]
 
 
def float_tensor(name: str, shape: list[int], random=False):
    low = 0.0
    high = 1.0
    total_elements = 1
    for x in shape:
        total_elements *= x
    weights = [np.random.uniform(low, high) for _ in range(total_elements)] if random else [1.0] * total_elements
    return helper.make_tensor(name, TensorProto.FLOAT, shape, weights)
 
 
def create_mat_mul_fast_gelu_without_bias(batch_size, m, n, k):
    # MatMul + FastGelu
    nodes = [
        helper.make_node("MatMul", ["input", "matmul_weight"], ["fastgelu_input"], "matmul"),
    ]
    fastgelu_node = helper.make_node("FastGelu", ["fastgelu_input"], ["output"], "fastgelu")
    fastgelu_node.domain = "com.microsoft"
    nodes.append(fastgelu_node)
 
    initializers = [float_tensor("matmul_weight", [k, n])]  # initializers
 
    graph = helper.make_graph(
        [node for node in nodes if node],
        "GemmFastGeluNoBiasModel",  # name
        [  # inputs
            helper.make_tensor_value_info(
                "input",
                TensorProto.FLOAT,
                [batch_size, m, k],
            )
        ],
        [  # outputs
            helper.make_tensor_value_info(
                "output",
                TensorProto.FLOAT,
                [batch_size, m, n],
            ),
        ],
        initializers,
    )
 
    return helper.make_model(graph)
 
 
def create_mat_mul_fast_gelu_with_bias(batch_size, m, n, k):
    # MatMul + FastGelu
    nodes = [
        helper.make_node("MatMul", ["input", "matmul_weight"], ["fastgelu_input"], "matmul"),
    ]
    fastgelu_node = helper.make_node("FastGelu", ["fastgelu_input", "fastgelu_bias"], ["output"], "fastgelu")
    fastgelu_node.domain = "com.microsoft"
    nodes.append(fastgelu_node)
 
    initializers = [float_tensor("matmul_weight", [k, n]), float_tensor("fastgelu_bias", [n])]  # initializers
 
    graph = helper.make_graph(
        [node for node in nodes if node],
        "GemmFastGeluWithBiasModel",  # name
        [  # inputs
            helper.make_tensor_value_info(
                "input",
                TensorProto.FLOAT,
                [batch_size, m, k],
            )
        ],
        [  # outputs
            helper.make_tensor_value_info(
                "output",
                TensorProto.FLOAT,
                [batch_size, m, n],
            ),
        ],
        initializers,
    )
 
    return helper.make_model(graph, opset_imports=opsets)
 
 
class TestFusion(unittest.TestCase):
    def verify_fusion(self, optimized_model, expected_model_filename):
        optimized_model.topological_sort()
 
        expected_model_path = os.path.join(os.path.dirname(__file__), "test_data", "models", expected_model_filename)
        expected_model = OnnxModel(onnx.load(expected_model_path))
        expected_model.topological_sort()
 
        self.assertEqual(str(optimized_model.model.graph), str(expected_model.model.graph))
 
    def test_gemmfastgelu_fusion_withoutbias(self):
        model = create_mat_mul_fast_gelu_without_bias(32, 128, 64, 1024)
        dir = "."
        model_path = os.path.join(dir, "gemmfastgelu_nobias.onnx")
        onnx.save(model, model_path)
 
        fusion_opt = FusionOptions("bert")
        fusion_opt.enable_gemm_fast_gelu = True
        optimized_model = optimize_model(input=model_path, optimization_options=fusion_opt)
        os.remove(model_path)
 
        self.verify_fusion(optimized_model, "gemmfastgelu_nobias_opt.onnx")
 
    def test_gemmfastgelu_fusion_withbias(self):
        model = create_mat_mul_fast_gelu_with_bias(32, 128, 64, 1024)
        dir = "."
        model_path = os.path.join(dir, "gemmfastgelu_withbias.onnx")
        onnx.save(model, model_path)
 
        fusion_opt = FusionOptions("bert")
        fusion_opt.enable_gemm_fast_gelu = True
        optimized_model = optimize_model(input=model_path, optimization_options=fusion_opt)
 
        os.remove(model_path)
 
        self.verify_fusion(optimized_model, "gemmfastgelu_withbias_opt.onnx")
 
 
if __name__ == "__main__":
    unittest.main()