okami.mom/strata

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

Niko1221Merge f-own-3c 0991006d2 into f-own-intd39d20f52

main
8.8 KiB174 linesraw
1// tools/vision/own/cpu_kernels_avx.cpp - the AVX tier without FMA, F16C or AVX2 (6 x 16 register tile: 12 ymm accumulators), for Sandy Bridge / Ivy
2// Bridge and AMD's AVX CPUs before Excavator.  Compiled with -mavx (/arch:AVX on MSVC); only called when quant::isa_ok(Avx).
3//
4// As the SSE4.1 tier: a product term is a multiply then an add in the same k order as every tier (no FMA), F16 weights are converted in integer code
5// (bit exact, no F16C: Sandy Bridge has none), exp is the Cephes polynomial with separate multiply and add.  AVX has no 256-bit integer operations, so the
6// integer steps (exponent bits, conversions, rounding) run on 128-bit halves.
7#include "cpu_tier.h"      // only this and <immintrin.h>: no standard-library code may be compiled with this file's ISA flags (cpu_tier.h)
8
9#if defined(__x86_64__) || defined(_M_X64)
10#include <immintrin.h>
11
12namespace strata_vit {
13namespace {
14namespace kn {
15
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
21#define STRATA_KN_RAW 1
22constexpr int VW = 8;
23typedef __m256 VR;
24inline VR vr_load(const float* p) { return _mm256_loadu_ps(p); }
25inline void vr_store(float* p, VR v) { _mm256_storeu_ps(p, v); }
26inline VR vr_set1(float f) { return _mm256_set1_ps(f); }
27inline VR vr_zero() { return _mm256_setzero_ps(); }
28inline VR vr_add(VR a, VR b) { return _mm256_add_ps(a, b); }
29inline VR vr_sub(VR a, VR b) { return _mm256_sub_ps(a, b); }
30inline VR vr_mul(VR a, VR b) { return _mm256_mul_ps(a, b); }
31inline VR vr_div(VR a, VR b) { return _mm256_div_ps(a, b); }
32inline VR vr_fma(VR a, VR b, VR c) { return _mm256_add_ps(_mm256_mul_ps(a, b), c); }      // two roundings (no FMA here)
33inline VR vr_max(VR a, VR b) { return _mm256_max_ps(a, b); }
34inline float vr_sqrt1(float x) { return _mm_cvtss_f32(_mm_sqrt_ss(_mm_set_ss(x))); }
35inline float vr_hadd(VR a) {      // the AVX2 tier's order
36    __m128 s = _mm_add_ps(_mm256_castps256_ps128(a), _mm256_extractf128_ps(a, 1));
37    s = _mm_add_ps(s, _mm_movehl_ps(s, s));
38    s = _mm_add_ss(s, _mm_shuffle_ps(s, s, 1));
39    return _mm_cvtss_f32(s);
40}
41inline float vr_hmax(VR a) {
42    __m128 s = _mm_max_ps(_mm256_castps256_ps128(a), _mm256_extractf128_ps(a, 1));
43    s = _mm_max_ps(s, _mm_movehl_ps(s, s));
44    s = _mm_max_ss(s, _mm_shuffle_ps(s, s, 1));
45    return _mm_cvtss_f32(s);
46}
47
48#define STRATA_KN_HAS_MICRO 1
49template <int MR>
50inline 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) {
51    __m256 acc[MR][2];
52    for (int r = 0; r < MR; ++r)
53        for (int c = 0; c < 2; ++c) {
54            __m256 v = _mm256_setzero_ps();
55            if (init != 0) v = _mm256_loadu_ps(C + (size_t) r * ldc + c * 8);
56            if (init != 1 && bias) v = _mm256_add_ps(v, _mm256_loadu_ps(bias + c * 8));
57            acc[r][c] = v;
58        }
59    for (int k = 0; k < K; ++k) {
60        const __m256 b0 = _mm256_loadu_ps(B + (size_t) k * ldb), b1 = _mm256_loadu_ps(B + (size_t) k * ldb + 8);
61        for (int r = 0; r < MR; ++r) {
62            const __m256 a = _mm256_set1_ps(A[(size_t) r * lda + k]);
63            acc[r][0] = _mm256_add_ps(_mm256_mul_ps(a, b0), acc[r][0]);
64            acc[r][1] = _mm256_add_ps(_mm256_mul_ps(a, b1), acc[r][1]);
65        }
66    }
67    for (int r = 0; r < MR; ++r)
68        for (int c = 0; c < 2; ++c) _mm256_storeu_ps(C + (size_t) r * ldc + c * 8, acc[r][c]);
69}
70
71inline __m256 combine(__m128 lo, __m128 hi) { return _mm256_insertf128_ps(_mm256_castps128_ps256(lo), hi, 1); }
72
73// expf (Cephes, the AVX2 tier's constants), multiply and add separately; 2^n built on the two 128-bit halves
74inline VR vr_exp(VR xv) {
75    __m256 x = _mm256_min_ps(_mm256_max_ps(xv, _mm256_set1_ps(-87.3f)), _mm256_set1_ps(88.3f));
76    const __m256 fx = _mm256_round_ps(_mm256_mul_ps(x, _mm256_set1_ps(1.44269504088896341f)), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
77    x = _mm256_sub_ps(x, _mm256_mul_ps(fx, _mm256_set1_ps(0.693359375f)));
78    x = _mm256_sub_ps(x, _mm256_mul_ps(fx, _mm256_set1_ps(-2.12194440e-4f)));
79    __m256 y = _mm256_set1_ps(1.9875691500e-4f);
80    y = _mm256_add_ps(_mm256_mul_ps(y, x), _mm256_set1_ps(1.3981999507e-3f));
81    y = _mm256_add_ps(_mm256_mul_ps(y, x), _mm256_set1_ps(8.3334519073e-3f));
82    y = _mm256_add_ps(_mm256_mul_ps(y, x), _mm256_set1_ps(4.1665795894e-2f));
83    y = _mm256_add_ps(_mm256_mul_ps(y, x), _mm256_set1_ps(1.6666665459e-1f));
84    y = _mm256_add_ps(_mm256_mul_ps(y, x), _mm256_set1_ps(5.0000001201e-1f));
85    y = _mm256_add_ps(_mm256_mul_ps(y, _mm256_mul_ps(x, x)), _mm256_add_ps(x, _mm256_set1_ps(1.0f)));
86    const __m256i n = _mm256_cvtps_epi32(fx);
87    const __m128i b = _mm_set1_epi32(127);
88    const __m128i elo = _mm_slli_epi32(_mm_add_epi32(_mm256_castsi256_si128(n), b), 23);
89    const __m128i ehi = _mm_slli_epi32(_mm_add_epi32(_mm256_extractf128_si256(n, 1), b), 23);
90    return _mm256_mul_ps(y, combine(_mm_castsi128_ps(elo), _mm_castsi128_ps(ehi)));
91}
92
93// rounding to 16 bits, bit for bit as the scalar tier (see cpu_kernels_sse41.cpp for the method), on 128-bit halves
94inline __m128 round4(__m128 x, int kind) {
95    const __m128i u = _mm_castps_si128(x);
96    const __m128i absu = _mm_and_si128(u, _mm_set1_epi32(0x7fffffff));
97    if (kind == 1) {
98        const __m128i lsb = _mm_and_si128(_mm_srli_epi32(u, 16), _mm_set1_epi32(1));
99        const __m128i r = _mm_and_si128(_mm_add_epi32(u, _mm_add_epi32(_mm_set1_epi32(0x7fff), lsb)), _mm_set1_epi32((int) 0xffff0000u));
100        const __m128i nan = _mm_cmpgt_epi32(absu, _mm_set1_epi32(0x7f800000));
101        return _mm_castsi128_ps(_mm_blendv_epi8(r, u, nan));
102    }
103    const __m128 a = _mm_castsi128_ps(absu);
104    const __m128i sign = _mm_and_si128(u, _mm_set1_epi32((int) 0x80000000u));
105    const __m128i lsb = _mm_and_si128(_mm_srli_epi32(absu, 13), _mm_set1_epi32(1));
106    const __m128i nrm = _mm_and_si128(_mm_add_epi32(absu, _mm_add_epi32(_mm_set1_epi32(0xfff), lsb)), _mm_set1_epi32((int) 0xffffe000u));
107    const __m128 sub = _mm_sub_ps(_mm_add_ps(a, _mm_set1_ps(0.5f)), _mm_set1_ps(0.5f));
108    const __m128 is_sub = _mm_cmplt_ps(a, _mm_set1_ps(6.103515625e-05f));
109    __m128 q = _mm_blendv_ps(_mm_castsi128_ps(nrm), sub, is_sub);
110    q = _mm_or_ps(q, _mm_castsi128_ps(sign));
111    const __m128 keep = _mm_cmplt_ps(a, _mm_set1_ps(65520.0f));
112    return _mm_blendv_ps(x, q, keep);
113}
114inline void round_buf(float* p, size_t n, int kind) {
115    size_t i = 0;
116    for (; i + 4 <= n; i += 4) _mm_storeu_ps(p + i, round4(_mm_loadu_ps(p + i), kind));
117    if (i < n) {
118        float t[4] = {0};
119        for (size_t j = i; j < n; ++j) t[j - i] = p[j];
120        _mm_storeu_ps(t, round4(_mm_loadu_ps(t), kind));
121        for (size_t j = i; j < n; ++j) p[j] = t[j - i];
122    }
123}
124
125inline __m128 h2f4(__m128i h) {
126    const __m128i em = _mm_and_si128(h, _mm_set1_epi32(0x7fff));
127    const __m128i sign = _mm_slli_epi32(_mm_and_si128(h, _mm_set1_epi32(0x8000)), 16);
128    const __m128i sh = _mm_slli_epi32(em, 13);
129    const __m128 fin = _mm_mul_ps(_mm_castsi128_ps(sh), _mm_castsi128_ps(_mm_set1_epi32(0x77800000)));
130    const __m128i inf = _mm_or_si128(sh, _mm_set1_epi32(0x7f800000));
131    const __m128i big = _mm_cmpgt_epi32(em, _mm_set1_epi32(0x7bff));
132    const __m128i r = _mm_blendv_epi8(_mm_castps_si128(fin), inf, big);
133    return _mm_castsi128_ps(_mm_or_si128(r, sign));
134}
135inline float f16_one(uint16_t h) { return _mm_cvtss_f32(h2f4(_mm_cvtsi32_si128((int) h))); }
136inline float bf16_one(uint16_t h) { return _mm_cvtss_f32(_mm_castsi128_ps(_mm_cvtsi32_si128((int) ((uint32_t) h << 16)))); }
137
138inline void conv16(const void* Wm, int kind, size_t off, int n, float* out) {
139    const uint16_t* p = (const uint16_t*) Wm + off;
140    int i = 0;
141    if (kind == 2) {
142        for (; i + 8 <= n; i += 8) {
143            const __m128i w = _mm_loadu_si128((const __m128i*) (p + i));
144            _mm256_storeu_ps(out + i, combine(h2f4(_mm_cvtepu16_epi32(w)), h2f4(_mm_cvtepu16_epi32(_mm_srli_si128(w, 8)))));
145        }
146        for (; i < n; ++i) out[i] = f16_one(p[i]);
147    } else {
148        const __m128i z = _mm_setzero_si128();
149        for (; i + 8 <= n; i += 8) {
150            const __m128i w = _mm_loadu_si128((const __m128i*) (p + i));
151            _mm256_storeu_ps(out + i, combine(_mm_castsi128_ps(_mm_unpacklo_epi16(z, w)), _mm_castsi128_ps(_mm_unpackhi_epi16(z, w))));
152        }
153        for (; i < n; ++i) out[i] = bf16_one(p[i]);
154    }
155}
156
157}  // namespace kn
158}  // namespace
159}  // namespace strata_vit
160
161#define STRATA_KN_MR 6
162#define KN_MIN(a, b) kmin(a, b)
163#define KN_MAX(a, b) kmax(a, b)
164#define KN_COPY(d, s, n) kcopy((d), (s), (n))
165#define KN_ZERO(d, n) kzero((d), (n))
166#define STRATA_KN_NAME "avx"
167#define STRATA_KN_GETTER cpu_ops_avx
168#include "cpu_kernels_impl.h"
169
170#else
171namespace strata_vit {
172const CpuOps& cpu_ops_avx() { return cpu_ops_scalar(); }
173}
174#endif