Исходный код СУБД 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation.  All rights reserved.
# Licensed under the MIT License.
# --------------------------------------------------------------------------
from argparse import ArgumentParser
from enum import Enum
 
 
class AttentionMaskFormat:
    # Build 1D mask indice (sequence length). It requires right side padding! Recommended for BERT model to get best performance.
    MaskIndexEnd = 0
 
    # For experiment only. Do not use it in production.
    MaskIndexEndAndStart = 1
 
    # Raw attention mask with 0 means padding (or no attention) and 1 otherwise.
    AttentionMask = 2
 
    # No attention mask
    NoMask = 3
 
 
class AttentionOpType(Enum):
    Attention = "Attention"
    MultiHeadAttention = "MultiHeadAttention"
    GroupQueryAttention = "GroupQueryAttention"
    PagedAttention = "PagedAttention"
 
    def __str__(self):
        return self.value
 
    # Override __eq__ to return string comparison
    def __hash__(self):
        return hash(self.value)
 
    def __eq__(self, other):
        return other.value == self.value
 
 
class FusionOptions:
    """Options of fusion in graph optimization"""
 
    def __init__(self, model_type):
        self.enable_gelu = True
        self.enable_layer_norm = True
        self.enable_attention = True
        self.enable_rotary_embeddings = True
 
        # Use MultiHeadAttention instead of Attention operator. The difference:
        # (1) Attention has merged weights for Q/K/V projection, which might be faster in some cases since 3 MatMul is
        #     merged into one.
        # (2) Attention could only handle self attention; MultiHeadAttention could handle both self and cross attention.
        self.use_multi_head_attention = False
        self.disable_multi_head_attention_bias = False
 
        self.enable_skip_layer_norm = True
        self.enable_embed_layer_norm = True
        self.enable_bias_skip_layer_norm = True
        self.enable_bias_gelu = True
        self.enable_gelu_approximation = False
        self.enable_qordered_matmul = True
 
        self.enable_shape_inference = True
        self.enable_gemm_fast_gelu = False
        self.group_norm_channels_last = True
 
        if model_type in ["clip", "qwen3"]:
            self.enable_embed_layer_norm = False
 
        # Set default to sequence length for BERT model to use fused attention to speed up.
        # Note that embed layer normalization will convert 2D mask to 1D when mask type is MaskIndexEnd.
        self.attention_mask_format = AttentionMaskFormat.AttentionMask
        if model_type == "bert":
            self.attention_mask_format = AttentionMaskFormat.MaskIndexEnd
        elif model_type in ["vit", "qwen3"]:
            self.attention_mask_format = AttentionMaskFormat.NoMask
 
        self.attention_op_type = None
 
        # options for stable diffusion
        if model_type in ["unet", "vae", "clip"]:
            self.enable_nhwc_conv = True
            self.enable_group_norm = True
            self.enable_skip_group_norm = True
            self.enable_bias_splitgelu = True
            self.enable_packed_qkv = True
            self.enable_packed_kv = True
            self.enable_bias_add = True
 
    def use_raw_attention_mask(self, use_raw_mask=True):
        if use_raw_mask:
            self.attention_mask_format = AttentionMaskFormat.AttentionMask
        else:
            self.attention_mask_format = AttentionMaskFormat.MaskIndexEnd
 
    def disable_attention_mask(self):
        self.attention_mask_format = AttentionMaskFormat.NoMask
 
    def set_attention_op_type(self, attn_op_type: AttentionOpType):
        self.attention_op_type = attn_op_type
 
    @staticmethod
    def parse(args):
        options = FusionOptions(args.model_type)
        if args.disable_gelu:
            options.enable_gelu = False
        if args.disable_layer_norm:
            options.enable_layer_norm = False
        if args.disable_rotary_embeddings:
            options.enable_rotary_embeddings = False
        if args.disable_attention:
            options.enable_attention = False
        if args.use_multi_head_attention:
            options.use_multi_head_attention = True
        if args.disable_skip_layer_norm:
            options.enable_skip_layer_norm = False
        if args.disable_embed_layer_norm:
            options.enable_embed_layer_norm = False
        if args.disable_bias_skip_layer_norm:
            options.enable_bias_skip_layer_norm = False
        if args.disable_bias_gelu:
            options.enable_bias_gelu = False
        if args.enable_gelu_approximation:
            options.enable_gelu_approximation = True
        if args.disable_shape_inference:
            options.enable_shape_inference = False
        if args.enable_gemm_fast_gelu:
            options.enable_gemm_fast_gelu = True
        if args.use_mask_index:
            options.use_raw_attention_mask(False)
        if args.use_raw_attention_mask:
            options.use_raw_attention_mask(True)
        if args.no_attention_mask:
            options.disable_attention_mask()
 
        if args.model_type in ["unet", "vae", "clip"]:
            if args.use_group_norm_channels_first:
                options.group_norm_channels_last = False
            if args.disable_nhwc_conv:
                options.enable_nhwc_conv = False
            if args.disable_group_norm:
                options.enable_group_norm = False
            if args.disable_skip_group_norm:
                options.enable_skip_group_norm = False
            if args.disable_bias_splitgelu:
                options.enable_bias_splitgelu = False
            if args.disable_packed_qkv:
                options.enable_packed_qkv = False
            if args.disable_packed_kv:
                options.enable_packed_kv = False
            if args.disable_bias_add:
                options.enable_bias_add = False
 
        return options
 
    @staticmethod
    def add_arguments(parser: ArgumentParser):
        parser.add_argument(
            "--disable_attention",
            required=False,
            action="store_true",
            help="disable Attention fusion",
        )
        parser.set_defaults(disable_attention=False)
 
        parser.add_argument(
            "--disable_skip_layer_norm",
            required=False,
            action="store_true",
            help="disable SkipLayerNormalization fusion",
        )
        parser.set_defaults(disable_skip_layer_norm=False)
 
        parser.add_argument(
            "--disable_embed_layer_norm",
            required=False,
            action="store_true",
            help="disable EmbedLayerNormalization fusion",
        )
        parser.set_defaults(disable_embed_layer_norm=False)
 
        parser.add_argument(
            "--disable_bias_skip_layer_norm",
            required=False,
            action="store_true",
            help="disable Add Bias and SkipLayerNormalization fusion",
        )
        parser.set_defaults(disable_bias_skip_layer_norm=False)
 
        parser.add_argument(
            "--disable_bias_gelu",
            required=False,
            action="store_true",
            help="disable Add Bias and Gelu/FastGelu fusion",
        )
        parser.set_defaults(disable_bias_gelu=False)
 
        parser.add_argument(
            "--disable_layer_norm",
            required=False,
            action="store_true",
            help="disable LayerNormalization fusion",
        )
        parser.set_defaults(disable_layer_norm=False)
 
        parser.add_argument(
            "--disable_gelu",
            required=False,
            action="store_true",
            help="disable Gelu fusion",
        )
        parser.set_defaults(disable_gelu=False)
 
        parser.add_argument(
            "--enable_gelu_approximation",
            required=False,
            action="store_true",
            help="enable Gelu/BiasGelu to FastGelu conversion",
        )
        parser.set_defaults(enable_gelu_approximation=False)
 
        parser.add_argument(
            "--disable_shape_inference",
            required=False,
            action="store_true",
            help="disable symbolic shape inference",
        )
        parser.set_defaults(disable_shape_inference=False)
 
        parser.add_argument(
            "--enable_gemm_fast_gelu",
            required=False,
            action="store_true",
            help="enable GemmfastGelu fusion",
        )
        parser.set_defaults(enable_gemm_fast_gelu=False)
 
        parser.add_argument(
            "--use_mask_index",
            required=False,
            action="store_true",
            help="use mask index to activate fused attention to speed up. It requires right-side padding!",
        )
        parser.set_defaults(use_mask_index=False)
 
        parser.add_argument(
            "--use_raw_attention_mask",
            required=False,
            action="store_true",
            help="use raw attention mask. Use this option if your input is not right-side padding. This might deactivate fused attention and get worse performance.",
        )
        parser.set_defaults(use_raw_attention_mask=False)
 
        parser.add_argument(
            "--no_attention_mask",
            required=False,
            action="store_true",
            help="no attention mask. Only works for model_type=bert",
        )
        parser.set_defaults(no_attention_mask=False)
 
        parser.add_argument(
            "--use_multi_head_attention",
            required=False,
            action="store_true",
            help="Use MultiHeadAttention instead of Attention operator for testing purpose. "
            "Note that MultiHeadAttention might be slower than Attention when qkv are not packed. ",
        )
        parser.set_defaults(use_multi_head_attention=False)
 
        parser.add_argument(
            "--disable_group_norm",
            required=False,
            action="store_true",
            help="not fuse GroupNorm. Only works for model_type=unet or vae",
        )
        parser.set_defaults(disable_group_norm=False)
 
        parser.add_argument(
            "--disable_skip_group_norm",
            required=False,
            action="store_true",
            help="not fuse Add + GroupNorm to SkipGroupNorm. Only works for model_type=unet or vae",
        )
        parser.set_defaults(disable_skip_group_norm=False)
 
        parser.add_argument(
            "--disable_packed_kv",
            required=False,
            action="store_true",
            help="not use packed kv for cross attention in MultiHeadAttention. Only works for model_type=unet",
        )
        parser.set_defaults(disable_packed_kv=False)
 
        parser.add_argument(
            "--disable_packed_qkv",
            required=False,
            action="store_true",
            help="not use packed qkv for self attention in MultiHeadAttention. Only works for model_type=unet",
        )
        parser.set_defaults(disable_packed_qkv=False)
 
        parser.add_argument(
            "--disable_bias_add",
            required=False,
            action="store_true",
            help="not fuse BiasAdd. Only works for model_type=unet",
        )
        parser.set_defaults(disable_bias_add=False)
 
        parser.add_argument(
            "--disable_bias_splitgelu",
            required=False,
            action="store_true",
            help="not fuse BiasSplitGelu. Only works for model_type=unet",
        )
        parser.set_defaults(disable_bias_splitgelu=False)
 
        parser.add_argument(
            "--disable_nhwc_conv",
            required=False,
            action="store_true",
            help="Do not use NhwcConv. Only works for model_type=unet or vae",
        )
        parser.set_defaults(disable_nhwc_conv=False)
 
        parser.add_argument(
            "--use_group_norm_channels_first",
            required=False,
            action="store_true",
            help="Use channels_first (NCHW) instead of channels_last (NHWC) for GroupNorm. Only works for model_type=unet or vae",
        )
        parser.set_defaults(use_group_norm_channels_first=False)
 
        parser.add_argument(
            "--disable_rotary_embeddings",
            required=False,
            action="store_true",
            help="Do not fuse rotary embeddings into RotaryEmbedding op",
        )