Исходный код СУБД 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
/*++
 
Copyright (c) Microsoft Corporation. All rights reserved.
 
Licensed under the MIT License.
 
Module Name:
 
    rotary_embedding_kernel_neon.cpp
 
Abstract:
 
    This module implements the rotary embedding kernels for ARM NEON.
 
--*/
 
#include <cassert>
 
#include "rotary_embedding.h"
#include "rotary_embedding_kernel_neon.h"
 
namespace {
 
template <bool interleaved>
void
RopeKernel_Fp32_Impl(
    const float* input,
    const float* sin,
    const float* cos,
    size_t dim,
    float* output
);
 
template <>
void
RopeKernel_Fp32_Impl<false>(
    const float* input,
    const float* sin,
    const float* cos,
    size_t dim,
    float* output
) {
    const size_t half_dim = dim >> 1;
    size_t i = 0, j = half_dim;
 
    for (; i + 3 < half_dim; i += 4, j += 4) {
        float32x4_t real = vld1q_f32(input + i);
        float32x4_t imag = vld1q_f32(input + j);
        float32x4_t sin_val = vld1q_f32(sin + i);
        float32x4_t cos_val = vld1q_f32(cos + i);
 
        float32x4_t real_out = vfmsq_f32(vmulq_f32(real, cos_val), imag, sin_val);
        float32x4_t imag_out = vfmaq_f32(vmulq_f32(real, sin_val), imag, cos_val);
 
        vst1q_f32(output + i, real_out);
        vst1q_f32(output + j, imag_out);
    }
 
    for (; i < half_dim; i++, j++) {
        float real = input[i];
        float imag = input[j];
        float sin_val = sin[i];
        float cos_val = cos[i];
        output[i] = real * cos_val - imag * sin_val;
        output[j] = real * sin_val + imag * cos_val;
    }
}
 
template <>
void
RopeKernel_Fp32_Impl<true>(
    const float* input,
    const float* sin,
    const float* cos,
    size_t dim,
    float* output
) {
    size_t i = 0;
 
    for (; i + 7 < dim; i += 8) {
        float32x4x2_t v = vld2q_f32(input + i);
        float32x4_t real = v.val[0];
        float32x4_t imag = v.val[1];
 
        float32x4_t sin_val = vld1q_f32(sin + i / 2);
        float32x4_t cos_val = vld1q_f32(cos + i / 2);
 
        float32x4_t real_out = vfmsq_f32(vmulq_f32(real, cos_val), imag, sin_val);
        float32x4_t imag_out = vfmaq_f32(vmulq_f32(real, sin_val), imag, cos_val);
 
        float32x4x2_t out;
        out.val[0] = real_out;
        out.val[1] = imag_out;
        vst2q_f32(output + i, out);
    }
 
    for (; i + 1 < dim; i += 2) {
        size_t cache_idx = i / 2;
        float in0 = input[i];
        float in1 = input[i + 1];
        float sin_val = sin[cache_idx];
        float cos_val = cos[cache_idx];
        output[i] = in0 * cos_val - in1 * sin_val;
        output[i + 1] = in0 * sin_val + in1 * cos_val;
    }
}
 
}  // namespace
 
namespace rope_neon {
 
void
RopeKernel_Fp32(
    const float* input,
    const float* sin,
    const float* cos,
    size_t dim,
    bool interleaved,
    float* output
) {
    assert(dim % 2 == 0);
    if (interleaved) {
        RopeKernel_Fp32_Impl<true>(input, sin, cos, dim, output);
    } else {
        RopeKernel_Fp32_Impl<false>(input, sin, cos, dim, output);
    }
}
 
}  // namespace rope_neon
 
//
// Kernel dispatch structure definition.
//
const MLAS_ROPE_DISPATCH MlasRopeDispatchNeon = []() {
    MLAS_ROPE_DISPATCH d;
 
    d.SRope = rope_neon::RopeKernel_Fp32;
 
#if defined(MLAS_F16VEC_INTRINSICS_SUPPORTED) && defined(MLAS_TARGET_ARM64)
    if (MlasFp16AccelerationSupported()) {
        d.HRope = rope_neon::RopeKernel_Fp16;
    }
#endif
    return d;
}();