Исходный код СУБД 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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*++
 
Copyright (c) Microsoft Corporation. All rights reserved.
 
Licensed under the MIT License.
 
Module Name:
    pooling_fp16.cpp
 
Abstract:
    This module implements the pooling operation for fp16
    tensors in NHWC format.
--*/
 
#include "mlasi.h"
 
#include "fp16_common.h"
 
#ifdef MLAS_F16VEC_INTRINSICS_SUPPORTED
 
 
template <typename AggregationType>
typename AggregationType::CtxType
PoolCreateContext(size_t KernelSize);
 
template <typename AggregationType>
MLAS_FLOAT16X8
PoolInit16x8();
 
template <typename AggregationType>
MLAS_FLOAT16X4
PoolInit16x4();
 
template <typename AggregationType>
MLAS_FLOAT16X8
PoolAggregate16x8(MLAS_FLOAT16X8 agg, MLAS_FLOAT16X8 element);
 
template <typename AggregationType>
MLAS_FLOAT16X4
PoolAggregate16x4(MLAS_FLOAT16X4 agg, MLAS_FLOAT16X4 element);
 
template <typename AggregationType>
MLAS_FLOAT16X8
PoolSummary16x8(MLAS_FLOAT16X8 agg, typename AggregationType::CtxType context);
 
template <typename AggregationType>
MLAS_FLOAT16X4
PoolSummary16x4(MLAS_FLOAT16X4 agg, typename AggregationType::CtxType context);
 
 
struct MaxPoolAggregation {
    typedef size_t CtxType; // useless type to satisfy compilers
};
 
template <>
MLAS_FORCEINLINE
size_t
PoolCreateContext<MaxPoolAggregation>(size_t KernelSize)
{
    MLAS_UNREFERENCED_PARAMETER(KernelSize);
    return 0;
}
 
template<>
MLAS_FORCEINLINE
MLAS_FLOAT16X8
PoolInit16x8<MaxPoolAggregation>()
{
    // lowest fp16 -65504.0f
    return MlasBroadcastFloat16x8(0xfbff);
}
 
template <>
MLAS_FORCEINLINE
MLAS_FLOAT16X4
PoolInit16x4<MaxPoolAggregation>()
{
    // lowest fp16 -65504.0f
    return MlasBroadcastFloat16x4(0xfbff);
}
 
template<>
MLAS_FORCEINLINE
MLAS_FLOAT16X8
PoolAggregate16x8<MaxPoolAggregation>(MLAS_FLOAT16X8 agg, MLAS_FLOAT16X8 element)
{
    return MlasMaximumFloat16(agg, element);
}
 
template<>
MLAS_FORCEINLINE
MLAS_FLOAT16X4
PoolAggregate16x4<MaxPoolAggregation>(MLAS_FLOAT16X4 agg, MLAS_FLOAT16X4 element)
{
    return MlasMaximumFloat16(agg, element);
}
 
template<>
MLAS_FORCEINLINE
MLAS_FLOAT16X8
PoolSummary16x8<MaxPoolAggregation>(MLAS_FLOAT16X8 agg, size_t size)
{
    MLAS_UNREFERENCED_PARAMETER(size);
    return agg;
}
 
template<>
MLAS_FORCEINLINE
MLAS_FLOAT16X4
PoolSummary16x4<MaxPoolAggregation>(MLAS_FLOAT16X4 agg, size_t size)
{
    MLAS_UNREFERENCED_PARAMETER(size);
    return agg;
}
 
struct AveragePoolAggregation {
    typedef MLAS_FLOAT16X8 CtxType;
};
 
template<>
MLAS_FLOAT16X8
PoolCreateContext<AveragePoolAggregation>(size_t KernelSize)
{
    return MlasBroadcastFloat16x8(MLAS_Float2Half(float(KernelSize)));
}
 
 
template <>
MLAS_FORCEINLINE MLAS_FLOAT16X8
PoolInit16x8<AveragePoolAggregation>()
{
    return MlasZeroFloat16x8();
}
 
template <>
MLAS_FORCEINLINE MLAS_FLOAT16X4
PoolInit16x4<AveragePoolAggregation>()
{
    return MlasZeroFloat16x4();
}
 
 
template <>
MLAS_FORCEINLINE MLAS_FLOAT16X8
PoolAggregate16x8<AveragePoolAggregation>(MLAS_FLOAT16X8 agg, MLAS_FLOAT16X8 element)
{
    return MlasAddFloat16(agg, element);
}
 
template <>
MLAS_FORCEINLINE MLAS_FLOAT16X4
PoolAggregate16x4<AveragePoolAggregation>(MLAS_FLOAT16X4 agg, MLAS_FLOAT16X4 element)
{
    return MlasAddFloat16(agg, element);
}
 
template <>
MLAS_FORCEINLINE MLAS_FLOAT16X8
PoolSummary16x8<AveragePoolAggregation>(MLAS_FLOAT16X8 agg, MLAS_FLOAT16X8 context)
{
    return MlasDivideFloat16(agg, context);
}
 
template <>
MLAS_FORCEINLINE MLAS_FLOAT16X4
PoolSummary16x4<AveragePoolAggregation>(MLAS_FLOAT16X4 agg, MLAS_FLOAT16X8 context)
{
    return MlasDivideFloat16(agg, MlasToLowHalfFloat16x4(context));
}
 
 
template<typename AggregationType>
MLAS_FORCEINLINE
void
MlasPoolFp16HWC(
    const _mlas_fp16_* const* Input,
    _mlas_fp16_* Output,
    size_t Channels,
    size_t OutputCount,
    size_t KernelSize
    )
{
    while (OutputCount > 0) {
        size_t ChannelOffset = 0;
        size_t c = Channels;
 
        while (c >= 32) {
            MLAS_FLOAT16X8 MaximumVector0 = PoolInit16x8<AggregationType>();
            MLAS_FLOAT16X8 MaximumVector1 = MaximumVector0;
            MLAS_FLOAT16X8 MaximumVector2 = MaximumVector0;
            MLAS_FLOAT16X8 MaximumVector3 = MaximumVector0;
            size_t cnt = 0;
 
            for (size_t k = 0; k < KernelSize; k++) {
                if (Input[k] == nullptr) {
                    continue;
                }
                MLAS_FLOAT16X8 InputVector0 = MlasLoadFloat16x8(&Input[k][ChannelOffset]);
                MLAS_FLOAT16X8 InputVector1 = MlasLoadFloat16x8(&Input[k][ChannelOffset + 8]);
                MLAS_FLOAT16X8 InputVector2 = MlasLoadFloat16x8(&Input[k][ChannelOffset + 16]);
                MLAS_FLOAT16X8 InputVector3 = MlasLoadFloat16x8(&Input[k][ChannelOffset + 24]);
 
                MaximumVector0 = PoolAggregate16x8<AggregationType>(MaximumVector0, InputVector0);
                MaximumVector1 = PoolAggregate16x8<AggregationType>(MaximumVector1, InputVector1);
                MaximumVector2 = PoolAggregate16x8<AggregationType>(MaximumVector2, InputVector2);
                MaximumVector3 = PoolAggregate16x8<AggregationType>(MaximumVector3, InputVector3);
                cnt++;
            }
            typename AggregationType::CtxType context = PoolCreateContext<AggregationType>(cnt);
            MaximumVector0 = PoolSummary16x8<AggregationType>(MaximumVector0, context);
            MaximumVector1 = PoolSummary16x8<AggregationType>(MaximumVector1, context);
            MaximumVector2 = PoolSummary16x8<AggregationType>(MaximumVector2, context);
            MaximumVector3 = PoolSummary16x8<AggregationType>(MaximumVector3, context);
 
            MlasStoreFloat16x8(&Output[0], MaximumVector0);
            MlasStoreFloat16x8(&Output[8], MaximumVector1);
            MlasStoreFloat16x8(&Output[16], MaximumVector2);
            MlasStoreFloat16x8(&Output[24], MaximumVector3);
 
            Output += 32;
            ChannelOffset += 32;
            c -= 32;
        }
 
        if (c >= 16) {
            MLAS_FLOAT16X8 MaximumVector0 = PoolInit16x8<AggregationType>();
            MLAS_FLOAT16X8 MaximumVector1 = MaximumVector0;
            size_t cnt = 0;
 
            for (size_t k = 0; k < KernelSize; k++) {
                if (Input[k] == nullptr) {
                    continue;
                }
                MLAS_FLOAT16X8 InputVector0 = MlasLoadFloat16x8(&Input[k][ChannelOffset]);
                MLAS_FLOAT16X8 InputVector1 = MlasLoadFloat16x8(&Input[k][ChannelOffset + 8]);
 
                MaximumVector0 = PoolAggregate16x8<AggregationType>(MaximumVector0, InputVector0);
                MaximumVector1 = PoolAggregate16x8<AggregationType>(MaximumVector1, InputVector1);
                cnt++;
            }
            typename AggregationType::CtxType context = PoolCreateContext<AggregationType>(cnt);
            MaximumVector0 = PoolSummary16x8<AggregationType>(MaximumVector0, context);
            MaximumVector1 = PoolSummary16x8<AggregationType>(MaximumVector1, context);
 
            MlasStoreFloat16x8(&Output[0], MaximumVector0);
            MlasStoreFloat16x8(&Output[8], MaximumVector1);
 
            Output += 16;
            ChannelOffset += 16;
            c -= 16;
        }
 
        if (c >= 8) {
            MLAS_FLOAT16X8 MaximumVector0 = PoolInit16x8<AggregationType>();
            size_t cnt = 0;
 
            for (size_t k = 0; k < KernelSize; k++) {
                if (Input[k] == nullptr) {
                    continue;
                }
                MLAS_FLOAT16X8 InputVector0 = MlasLoadFloat16x8(&Input[k][ChannelOffset]);
                MaximumVector0 = PoolAggregate16x8<AggregationType>(MaximumVector0, InputVector0);
                cnt++;
            }
            typename AggregationType::CtxType context = PoolCreateContext<AggregationType>(cnt);
            MaximumVector0 = PoolSummary16x8<AggregationType>(MaximumVector0, context);
 
            MlasStoreFloat16x8(&Output[0], MaximumVector0);
 
            Output += 8;
            ChannelOffset += 8;
            c -= 8;
        }
 
        if (c >= 4) {
            MLAS_FLOAT16X4 MaximumVector0 = PoolInit16x4<AggregationType>();
            size_t cnt = 0;
 
            for (size_t k = 0; k < KernelSize; k++) {
                if (Input[k] == nullptr) {
                    continue;
                }
                MLAS_FLOAT16X4 InputVector0 = MlasLoadFloat16x4(&Input[k][ChannelOffset]);
                MaximumVector0 = PoolAggregate16x4<AggregationType>(MaximumVector0, InputVector0);
                cnt++;
            }
            typename AggregationType::CtxType context = PoolCreateContext<AggregationType>(cnt);
            MaximumVector0 = PoolSummary16x4<AggregationType>(MaximumVector0, context);
 
            MlasStoreFloat16x4(&Output[0], MaximumVector0);
 
            Output += 4;
            ChannelOffset += 4;
            c -= 4;
        }
 
        if (c > 0) {
            // possible over read by 7 bytes
            MLAS_FLOAT16X4 MaximumVector0 = PoolInit16x4<AggregationType>();
            size_t cnt = 0;
 
            for (size_t k = 0; k < KernelSize; k++) {
                if (Input[k] == nullptr) {
                    continue;
                }
                MLAS_FLOAT16X4 InputVector0 = MlasLoadFloat16x4(&Input[k][ChannelOffset]);
                MaximumVector0 = PoolAggregate16x4<AggregationType>(MaximumVector0, InputVector0);
                cnt++;
            }
            typename AggregationType::CtxType context = PoolCreateContext<AggregationType>(cnt);
            MaximumVector0 = PoolSummary16x4<AggregationType>(MaximumVector0, context);
 
            MlasStorePartialFloat16x4(&Output[0], MaximumVector0, c);
            Output += c;
        }
 
        Input += KernelSize;
        OutputCount -= 1;
    }
}
 
 
void
MLASCALL
MlasNhwcMaxPool(
    const MLAS_FP16* const* Input,
    MLAS_FP16* Output,
    size_t Channels,
    size_t OutputCount,
    size_t KernelSize
    )
{
    const _mlas_fp16_* const* input_ptr = reinterpret_cast<const _mlas_fp16_* const*>(Input);
    _mlas_fp16_* output_ptr = reinterpret_cast<_mlas_fp16_*>(Output);
    MlasPoolFp16HWC<MaxPoolAggregation>(input_ptr, output_ptr, Channels, OutputCount, KernelSize);
}
 
 
void
MLASCALL
MlasNhwcAvgPool(
    const MLAS_FP16* const* Input,
    MLAS_FP16* Output,
    size_t Channels,
    size_t OutputCount,
    size_t KernelSize
    )
{
    const _mlas_fp16_* const* input_ptr = reinterpret_cast<const _mlas_fp16_* const*>(Input);
    _mlas_fp16_* output_ptr = reinterpret_cast<_mlas_fp16_*>(Output);
    MlasPoolFp16HWC<AveragePoolAggregation>(input_ptr, output_ptr, Channels, OutputCount, KernelSize);
}
 
 
#endif  // MLAS_F16VEC_INTRINSICS_SUPPORTED