Исходный код СУБД 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
#!/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 unittest
 
import numpy as np
import onnx
from onnx import TensorProto, helper
from op_test_utils import TestDataFeeds, check_model_correctness, check_op_type_count
 
from onnxruntime.quantization import quantize_dynamic
 
 
class TestOpAttention(unittest.TestCase):
    def input_feeds(self, n, name2shape):
        input_data_list = []
        for _i in range(n):
            inputs = {}
            for name, shape in name2shape.items():
                inputs.update({name: np.random.randint(-1, 2, shape).astype(np.float32)})
            input_data_list.extend([inputs])
        dr = TestDataFeeds(input_data_list)
        return dr
 
    def construct_model_attention_and_matmul(self, output_model_path):
        #      (input)
        #         |
        #     Attention
        #         |
        #       MatMul
        #         |
        #      (output)
        input_name = "input"
        output_name = "output"
        initializers = []
 
        def make_attention_node(input_name, weight_shape, weight_name, bias_shape, bias_name, output_name):
            weight_data = np.random.normal(0, 0.1, weight_shape).astype(np.float32)
            initializers.append(onnx.numpy_helper.from_array(weight_data, name=weight_name))
 
            bias_data = np.random.normal(0, 0.1, bias_shape).astype(np.float32)
            initializers.append(onnx.numpy_helper.from_array(bias_data, name=bias_name))
 
            return onnx.helper.make_node("Attention", [input_name, weight_name, bias_name], [output_name])
 
        def make_matmul_node(input_name, weight_shape, weight_name, output_name):
            weight_data = np.random.normal(0, 0.1, weight_shape).astype(np.float32)
            initializers.append(onnx.numpy_helper.from_array(weight_data, name=weight_name))
 
            return onnx.helper.make_node("MatMul", [input_name, weight_name], [output_name])
 
        # make attention node
        attention_output_name = "attention_output"
        attention_node = make_attention_node(
            input_name, [10, 30], "qkv.weight", [30], "qkv.bias", attention_output_name
        )
        attention_node.domain = "com.microsoft"
        attention_node.attribute.extend([helper.make_attribute("num_heads", 5)])
 
        # make matmul node
        matmul_node = make_matmul_node(attention_output_name, [10, 10], "matmul.weight", output_name)
 
        # make graph
        input_tensor = helper.make_tensor_value_info(input_name, TensorProto.FLOAT, [1, -1, 10])
        output_tensor = helper.make_tensor_value_info(output_name, TensorProto.FLOAT, [1, -1, 10])
        graph_name = "attention_test"
        graph = helper.make_graph(
            [attention_node, matmul_node],
            graph_name,
            [input_tensor],
            [output_tensor],
            initializer=initializers,
        )
        model = helper.make_model(
            graph, opset_imports=[helper.make_opsetid("", 13), helper.make_opsetid("com.microsoft", 1)]
        )
        model.ir_version = onnx.IR_VERSION
 
        onnx.save(model, output_model_path)
 
    def dynamic_attention_quant_test(self, model_fp32_path, model_int8_path, per_channel, reduce_range):
        quantize_dynamic(
            model_fp32_path,
            model_int8_path,
            per_channel=per_channel,
            reduce_range=reduce_range,
        )
        quant_nodes = {"QAttention": 1, "MatMulInteger": 1}
        check_op_type_count(self, model_int8_path, **quant_nodes)
        check_model_correctness(
            self,
            model_fp32_path,
            model_int8_path,
            {"input": np.random.rand(1, 5, 10).astype(np.float32)},
        )
 
    def test_quantize_attention(self):
        np.random.seed(1)
        model_fp32_path = "attention_fp32.onnx"
        model_int8_path = "attention_fp32.quant.onnx"
        self.construct_model_attention_and_matmul(model_fp32_path)
 
        self.dynamic_attention_quant_test(model_fp32_path, model_int8_path, True, True)
        self.dynamic_attention_quant_test(model_fp32_path, model_int8_path, True, False)
        self.dynamic_attention_quant_test(model_fp32_path, model_int8_path, False, True)
        self.dynamic_attention_quant_test(model_fp32_path, model_int8_path, False, False)
 
 
if __name__ == "__main__":
    unittest.main()