Исходный код СУБД 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
/*++
 
Copyright (c) Microsoft Corporation. All rights reserved.
 
Licensed under the MIT License.
 
Module Name:
 
    rotary_embedding_kernel_avx2.cpp
 
Abstract:
 
    This module implements the rotary embedding kernels for AVX2 supported h/w.
 
--*/
 
 
#include <cassert>
 
#include "rotary_embedding.h"
#include "rotary_embedding_kernel_avx2.h"
 
namespace rope_avx2 {
 
namespace {
 
typedef __m256 float32x8_t;
 
template <bool interleaved>
void
RopeKernel_Avx2_fp16_Impl(
    const MLAS_FP16* input,
    const MLAS_FP16* sin_data,
    const MLAS_FP16* cos_data,
    size_t dim,
    MLAS_FP16* output
);
 
float32x8_t
load_fp16_and_convert_to_fp32(const MLAS_FP16* input)
{
    __m128i fp16 = _mm_lddqu_si128(reinterpret_cast<const __m128i*>(input));
    return _mm256_cvtph_ps(fp16);
}
 
void
convert_to_fp16_and_store(MLAS_FP16* dst_fp16, const __m256 output)
{
    __m128i fp16_chunk = _mm256_cvtps_ph(output, _MM_FROUND_TO_NEAREST_INT);
    _mm_storeu_si128(reinterpret_cast<__m128i*>(dst_fp16), fp16_chunk);
}
 
template <>
void
RopeKernel_Avx2_fp16_Impl<false>(
    const MLAS_FP16* input,
    const MLAS_FP16* sin_data,
    const MLAS_FP16* cos_data,
    size_t dim,
    MLAS_FP16* output
)
{
  // ?cast input -> const unsigned short*
    const size_t half_dim = dim >> 1;
    size_t i = 0, j = half_dim;
    for (; i + 7 < half_dim; i += 8, j += 8) {
        float32x8_t real = load_fp16_and_convert_to_fp32(input + i);
        float32x8_t imag = load_fp16_and_convert_to_fp32(input + j);
        float32x8_t sin_val = load_fp16_and_convert_to_fp32(sin_data + i);
        float32x8_t cos_val = load_fp16_and_convert_to_fp32(cos_data + i);
        // Compute Real and Imaginary output values
        float32x8_t real_out = _mm256_fmsub_ps(real, cos_val, _mm256_mul_ps(imag, sin_val));
        float32x8_t imag_out = _mm256_fmadd_ps(real, sin_val, _mm256_mul_ps(imag, cos_val));
        // Store back into non interleaved format
        convert_to_fp16_and_store(output + i, real_out);
        convert_to_fp16_and_store(output + j, imag_out);
    }
    for (; i < half_dim; i++, j++) {
        float real = input[i].ToFloat();
        float imag = input[j].ToFloat();
        float sin_val = sin_data[i];
        float cos_val = cos_data[i];
        output[i] = MLAS_FP16(real * cos_val - imag * sin_val);
        output[j] = MLAS_FP16(real * sin_val + imag * cos_val);
    }
}
 
template <>
void
RopeKernel_Avx2_fp16_Impl<true>(
    const MLAS_FP16* input,
    const MLAS_FP16* sin_data,
    const MLAS_FP16* cos_data,
    size_t dim,
    MLAS_FP16* output
)
{
    // ?cast input -> const unsigned short*
    size_t i = 0;
    for (; i + 15 < dim; i += 16) {
        float32x8_t x0 = load_fp16_and_convert_to_fp32(input + i);
        float32x8_t x1 = load_fp16_and_convert_to_fp32(input + i + 8);
        float32x8_t real_s = _mm256_shuffle_ps(x0, x1, 0b10001000);
        float32x8_t imag_s = _mm256_shuffle_ps(x0, x1, 0b11011101);
        __m256i in_mask_vec = _mm256_set_epi32(7, 6, 3, 2, 5, 4, 1, 0);
        float32x8_t real = _mm256_permutevar8x32_ps(real_s, in_mask_vec);
        float32x8_t imag = _mm256_permutevar8x32_ps(imag_s, in_mask_vec);
        float32x8_t sin_val = load_fp16_and_convert_to_fp32(sin_data + i / 2);
        float32x8_t cos_val = load_fp16_and_convert_to_fp32(cos_data + i / 2);
        // Compute Real and Imaginary output values
        float32x8_t real_out = _mm256_fmsub_ps(real, cos_val, _mm256_mul_ps(imag, sin_val));
        float32x8_t imag_out = _mm256_fmadd_ps(real, sin_val, _mm256_mul_ps(imag, cos_val));
        // Store back into interleaved format
        __m256i out_mask_vec = _mm256_set_epi32(7, 6, 3, 2, 5, 4, 1, 0);
        float32x8_t real_out_s = _mm256_permutevar8x32_ps(real_out, out_mask_vec);
        float32x8_t imag_out_s = _mm256_permutevar8x32_ps(imag_out, out_mask_vec);
        float32x8_t y0 = _mm256_unpacklo_ps(real_out_s, imag_out_s);
        float32x8_t y1 = _mm256_unpackhi_ps(real_out_s, imag_out_s);
 
        // Store back into non interleaved format
        convert_to_fp16_and_store(output + i, y0);
        convert_to_fp16_and_store(output + i + 8, y1);
    }
 
    // Scalar remainder loop to safely handle trailing elements in pairs.
    for (; i + 1 < dim; i += 2) {
        size_t cache_idx = i / 2;
        float input0 = input[i].ToFloat();
        float input1 = input[i + 1].ToFloat();
        float sin_val = sin_data[cache_idx].ToFloat();
        float cos_val = cos_data[cache_idx].ToFloat();
        output[i] = MLAS_FP16(input0 * cos_val - input1 * sin_val);
        output[i + 1] = MLAS_FP16(input0 * sin_val + input1 * cos_val);
    }
}
 
template <bool interleaved>
void
RopeKernel_Avx2_fp32_Impl(
    const float* input,
    const float* sin_data,
    const float* cos_data,
    size_t dim,
    float* output
);
 
template <>
void
RopeKernel_Avx2_fp32_Impl<false>(
    const float* input,
    const float* sin_data,
    const float* cos_data,
    size_t dim,
    float* output
) {
    const size_t half_dim = dim >> 1;
    size_t i = 0, j = half_dim;
    for (; i + 7 < half_dim; i += 8, j += 8) {
        float32x8_t real = _mm256_loadu_ps(input + i);
        float32x8_t imag = _mm256_loadu_ps(input + j);
        float32x8_t sin_val = _mm256_loadu_ps(sin_data + i);
        float32x8_t cos_val = _mm256_loadu_ps(cos_data + i);
        //Compute Real and Imaginary output values
        float32x8_t real_out = _mm256_fmsub_ps(real, cos_val, _mm256_mul_ps(imag, sin_val));
        float32x8_t imag_out = _mm256_fmadd_ps(real, sin_val, _mm256_mul_ps(imag, cos_val));
        //Store back into non interleaved format
        _mm256_storeu_ps(output + i, real_out);
        _mm256_storeu_ps(output + j, imag_out);
    }
 
    // Scalar remainder loop to safely handle trailing elements
    for (; i < half_dim; i++, j++) {
        float real = input[i];
        float imag = input[j];
        float sin_val = sin_data[i];
        float cos_val = cos_data[i];
        output[i] = real * cos_val - imag * sin_val;
        output[j] = real * sin_val + imag * cos_val;
    }
}
 
template <>
void
RopeKernel_Avx2_fp32_Impl<true>(
    const float* input,
    const float* sin_data,
    const float* cos_data,
    size_t dim,
    float* output
) {
    size_t i = 0;
    for (; i + 15 < dim; i += 16) {
        float32x8_t x0 = _mm256_loadu_ps(input + i);
        float32x8_t x1 = _mm256_loadu_ps(input + i + 8);
        //Load imaginary and real values to separate non-interleaved vectors
        float32x8_t real_s = _mm256_shuffle_ps(x0, x1, 0b10001000);
        float32x8_t imag_s = _mm256_shuffle_ps(x0, x1, 0b11011101);
        __m256i in_mask_vec = _mm256_set_epi32(7, 6, 3, 2, 5, 4, 1, 0);
        float32x8_t real = _mm256_permutevar8x32_ps(real_s, in_mask_vec);
        float32x8_t imag = _mm256_permutevar8x32_ps(imag_s, in_mask_vec);
        float32x8_t sin_val = _mm256_loadu_ps(sin_data + i / 2);
        float32x8_t cos_val = _mm256_loadu_ps(cos_data + i / 2);
        //Compute Real and Imaginary output values
        float32x8_t real_out = _mm256_fmsub_ps(real, cos_val, _mm256_mul_ps(imag, sin_val));
        float32x8_t imag_out = _mm256_fmadd_ps(real, sin_val, _mm256_mul_ps(imag, cos_val));
        //Store back into interleaved format
        __m256i out_mask_vec = _mm256_set_epi32(7, 6, 3, 2, 5, 4, 1, 0);
        float32x8_t real_out_s = _mm256_permutevar8x32_ps(real_out, out_mask_vec);
        float32x8_t imag_out_s = _mm256_permutevar8x32_ps(imag_out, out_mask_vec);
        float32x8_t y0 = _mm256_unpacklo_ps(real_out_s, imag_out_s);
        float32x8_t y1 = _mm256_unpackhi_ps(real_out_s, imag_out_s);
        _mm256_storeu_ps(output + i, y0);
        _mm256_storeu_ps(output + i + 8, y1);
    }
 
    // Scalar remainder loop to safely handle trailing elements in pairs
    for (; i + 1 < dim; i += 2) {
        size_t cache_idx = i / 2;
        float input0 = input[i];
        float input1 = input[i + 1];
        float sin_val = sin_data[cache_idx];
        float cos_val = cos_data[cache_idx];
        output[i]     = input0 * cos_val - input1 * sin_val;
        output[i + 1] = input0 * sin_val + input1 * cos_val;
    }
}
 
}  // rope_avx2 namespace
 
void
RopeKernel_Avx2_fp32(
    const float* input,
    const float* sin_data,
    const float* cos_data,
    size_t dim,
    bool interleaved,
    float* output
) {
    // real part and imaginary part must be paired
    assert(dim % 2 == 0);
    const auto* input_impl = reinterpret_cast<const float*>(input);
    const auto* sin_impl = reinterpret_cast<const float*>(sin_data);
    const auto* cos_impl = reinterpret_cast<const float*>(cos_data);
    auto* output_impl = reinterpret_cast<float*>(output);
 
    if (interleaved) {
        RopeKernel_Avx2_fp32_Impl<true>(input_impl, sin_impl, cos_impl, dim, output_impl);
    } else {
        RopeKernel_Avx2_fp32_Impl<false>(input_impl, sin_impl, cos_impl, dim, output_impl);
    }
}
 
void
RopeKernel_Avx2_fp16(
    const MLAS_FP16* input,
    const MLAS_FP16* sin_data,
    const MLAS_FP16* cos_data,
    size_t dim,
    bool interleaved,
    MLAS_FP16* output
)
{
    // real part and imaginary part must be paired
    assert(dim % 2 == 0);
 
    if (interleaved) {
        RopeKernel_Avx2_fp16_Impl<true>(input, sin_data, cos_data, dim, output);
    } else {
        RopeKernel_Avx2_fp16_Impl<false>(input, sin_data, cos_data, dim, output);
    }
}
}
 
//
// Kernel dispatch structure definition.
//
const MLAS_ROPE_DISPATCH MlasRopeDispatchAvx2 = []() {
    MLAS_ROPE_DISPATCH d;
    d.SRope = rope_avx2::RopeKernel_Avx2_fp32;
    d.HRope = rope_avx2::RopeKernel_Avx2_fp16;
    return d;
}();