okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-3v (9e5f5a5ed) into f-own-int: larger attention chunk above 16,384 patches, the NEON CPU tier, the C4556 fix, auto picks our CPU tower on Armb361f8245

main
7.1 KiB146 linesraw
1// tools/vision/own/cpu_kernels_neon.cpp - the Arm NEON tier (8 x 8 register tile: 16 accumulators and 8 A vectors of the 32 vector registers).  Baseline AArch64 only (Advanced
2// SIMD and the F16 conversions are in every ARMv8-A CPU), so no ISA flags; it keeps the contract of the x86 tiers anyway (cpu_tier.h: no standard-library code).
3#include "cpu_tier.h"      // only this and <arm_neon.h> (cpu_tier.h)
4
5#if defined(__aarch64__) || defined(_M_ARM64)
6#include <arm_neon.h>
7
8namespace strata_vit {
9namespace {
10namespace kn {
11
12// one 16-bit value to float, bit exact
13inline float f16_one(uint16_t h) { return vgetq_lane_f32(vcvt_f32_f16(vreinterpret_f16_u16(vdup_n_u16(h))), 0); }
14inline float bf16_one(uint16_t h) { return vgetq_lane_f32(vreinterpretq_f32_u32(vdupq_n_u32((uint32_t) h << 16)), 0); }
15// the helpers cpu_kernels_impl.h asks for, internal to this file
16template <class T> inline T kmin(T a, T b) { return b < a ? b : a; }
17template <class T> inline T kmax(T a, T b) { return a < b ? b : a; }
18inline void kcopy(float* d, const float* s, int n) { for (int i = 0; i < n; ++i) d[i] = s[i]; }
19inline void kzero(float* d, int n) { for (int i = 0; i < n; ++i) d[i] = 0.0f; }
20
21struct V {
22    static constexpr int W = 4;
23    float32x4_t v;
24    V() : v(vdupq_n_f32(0.0f)) {}
25    V(float32x4_t x) : v(x) {}
26    static V load(const float* p) { return vld1q_f32(p); }
27    void store(float* p) const { vst1q_f32(p, v); }
28    static V set1(float f) { return vdupq_n_f32(f); }
29    static V zero() { return vdupq_n_f32(0.0f); }
30    static float sqrt1(float x) { return vgetq_lane_f32(vsqrtq_f32(vdupq_n_f32(x)), 0); }      // IEEE square root, correctly rounded
31    static V fma(V a, V b, V c) { return vfmaq_f32(c.v, a.v, b.v); }
32    static float reduce_add(V a) { return vaddvq_f32(a.v); }
33    static float reduce_max(V a) { return vmaxvq_f32(a.v); }
34    friend V operator+(V a, V b) { return vaddq_f32(a.v, b.v); }
35    friend V operator-(V a, V b) { return vsubq_f32(a.v, b.v); }
36    friend V operator*(V a, V b) { return vmulq_f32(a.v, b.v); }
37    friend V operator/(V a, V b) { return vdivq_f32(a.v, b.v); }
38};
39inline V vmax(V a, V b) { return vmaxq_f32(a.v, b.v); }
40
41// The product's micro-kernel with lane-indexed FMAs: four k steps take one vector load of each A row instead of four scalar broadcasts.  The same
42// summation order as the generic micro of cpu_kernels_impl.h (each accumulator gets a[r][k] * b[k] in k order, fused), so the results are the same.
43#define STRATA_KN_HAS_MICRO 1
44#define KN_STEP(L)                                                                                     \
45    {                                                                                                  \
46        const float32x4_t b0 = vld1q_f32(B + (size_t) (k + L) * ldb), b1 = vld1q_f32(B + (size_t) (k + L) * ldb + 4); \
47        for (int r = 0; r < MR; ++r) {                                                                 \
48            acc[r][0] = vfmaq_laneq_f32(acc[r][0], b0, a[r], L);                                       \
49            acc[r][1] = vfmaq_laneq_f32(acc[r][1], b1, a[r], L);                                       \
50        }                                                                                              \
51    }
52template <int MR>
53inline void micro(const float* A, size_t lda, const float* B, size_t ldb, int K, float* C, size_t ldc, int init, const float* bias) {
54    float32x4_t acc[MR][2];
55    for (int r = 0; r < MR; ++r)
56        for (int c = 0; c < 2; ++c) {
57            float32x4_t v = vdupq_n_f32(0.0f);
58            if (init != 0) v = vld1q_f32(C + (size_t) r * ldc + c * 4);
59            if (init != 1 && bias) v = vaddq_f32(v, vld1q_f32(bias + c * 4));
60            acc[r][c] = v;
61        }
62    int k = 0;
63    for (; k + 4 <= K; k += 4) {
64        float32x4_t a[MR];
65        for (int r = 0; r < MR; ++r) a[r] = vld1q_f32(A + (size_t) r * lda + k);
66        KN_STEP(0) KN_STEP(1) KN_STEP(2) KN_STEP(3)
67    }
68    for (; k < K; ++k) {
69        const float32x4_t b0 = vld1q_f32(B + (size_t) k * ldb), b1 = vld1q_f32(B + (size_t) k * ldb + 4);
70        for (int r = 0; r < MR; ++r) {
71            const float32x4_t a = vdupq_n_f32(A[(size_t) r * lda + k]);
72            acc[r][0] = vfmaq_f32(acc[r][0], a, b0);
73            acc[r][1] = vfmaq_f32(acc[r][1], a, b1);
74        }
75    }
76    for (int r = 0; r < MR; ++r)
77        for (int c = 0; c < 2; ++c) vst1q_f32(C + (size_t) r * ldc + c * 4, acc[r][c]);
78}
79#undef KN_STEP
80
81// expf (Cephes), the same steps and constants as the x86 tiers: x = n ln2 + r, exp(x) = 2^n * p(r)
82inline V vexp(V xv) {
83    float32x4_t x = vminq_f32(vmaxq_f32(xv.v, vdupq_n_f32(-87.3f)), vdupq_n_f32(88.3f));
84    const float32x4_t fx = vrndnq_f32(vmulq_f32(x, vdupq_n_f32(1.44269504088896341f)));
85    x = vfmsq_f32(x, fx, vdupq_n_f32(0.693359375f));
86    x = vfmsq_f32(x, fx, vdupq_n_f32(-2.12194440e-4f));
87    float32x4_t y = vdupq_n_f32(1.9875691500e-4f);
88    y = vfmaq_f32(vdupq_n_f32(1.3981999507e-3f), y, x);
89    y = vfmaq_f32(vdupq_n_f32(8.3334519073e-3f), y, x);
90    y = vfmaq_f32(vdupq_n_f32(4.1665795894e-2f), y, x);
91    y = vfmaq_f32(vdupq_n_f32(1.6666665459e-1f), y, x);
92    y = vfmaq_f32(vdupq_n_f32(5.0000001201e-1f), y, x);
93    y = vfmaq_f32(vaddq_f32(x, vdupq_n_f32(1.0f)), y, vmulq_f32(x, x));
94    const int32x4_t e = vshlq_n_s32(vaddq_s32(vcvtnq_s32_f32(fx), vdupq_n_s32(127)), 23);
95    return vmulq_f32(y, vreinterpretq_f32_s32(e));
96}
97
98// 4 floats rounded to the 16-bit type and back (BF16: integer round to nearest even as the x86 tiers; F16: the hardware conversion pair, nearest even)
99inline float32x4_t round4(float32x4_t x, int kind) {
100    if (kind == 2) return vcvt_f32_f16(vcvt_f16_f32(x));
101    uint32x4_t u = vreinterpretq_u32_f32(x);
102    const uint32x4_t lsb = vandq_u32(vshrq_n_u32(u, 16), vdupq_n_u32(1));
103    u = vaddq_u32(u, vaddq_u32(vdupq_n_u32(0x7fff), lsb));
104    return vreinterpretq_f32_u32(vandq_u32(u, vdupq_n_u32(0xffff0000u)));
105}
106inline void round_buf(float* p, size_t n, int kind) {
107    size_t i = 0;
108    for (; i + 4 <= n; i += 4) vst1q_f32(p + i, round4(vld1q_f32(p + i), kind));
109    if (i < n) {
110        float t[4] = {0, 0, 0, 0};
111        for (size_t j = i; j < n; ++j) t[j - i] = p[j];
112        vst1q_f32(t, round4(vld1q_f32(t), kind));
113        for (size_t j = i; j < n; ++j) p[j] = t[j - i];
114    }
115}
116
117inline void conv16(const void* Wm, int kind, size_t off, int n, float* out) {
118    const uint16_t* p = (const uint16_t*) Wm + off;
119    int i = 0;
120    if (kind == 2) {
121        for (; i + 4 <= n; i += 4) vst1q_f32(out + i, vcvt_f32_f16(vreinterpret_f16_u16(vld1_u16(p + i))));
122        for (; i < n; ++i) out[i] = f16_one(p[i]);
123    } else {
124        for (; i + 4 <= n; i += 4) vst1q_f32(out + i, vreinterpretq_f32_u32(vshll_n_u16(vld1_u16(p + i), 16)));
125        for (; i < n; ++i) out[i] = bf16_one(p[i]);
126    }
127}
128
129}  // namespace kn
130}  // namespace
131}  // namespace strata_vit
132
133#define STRATA_KN_MR 8
134#define KN_MIN(a, b) kmin(a, b)
135#define KN_MAX(a, b) kmax(a, b)
136#define KN_COPY(d, s, n) kcopy((d), (s), (n))
137#define KN_ZERO(d, n) kzero((d), (n))
138#define STRATA_KN_NAME "neon"
139#define STRATA_KN_GETTER cpu_ops_neon
140#include "cpu_kernels_impl.h"
141
142#else   // not AArch64: the tier does not exist, the dispatcher never asks for it
143namespace strata_vit {
144const CpuOps& cpu_ops_neon() { return cpu_ops_scalar(); }
145}
146#endif