okami.mom/strata

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

Niko1221Merge f-own-3v (aa2a3f956) into f-own-int: --tower auto keeps mtmd on Pascal and on scalar-only CPUs, per-ISA CPU files without standard-library code, the SYCL tower76c1a1744

main
3.4 KiB77 linesraw
1// tools/vision/own/cpu_kernels_scalar.cpp - the portable tier of the CPU tower's kernels: plain C++ over a 4-float vector type (the compiler turns it into
2// whatever vector unit the target has, NEON included).  Compiled without any ISA flags.
3#include "cpu_kernels.h"
4
5#include <algorithm>
6#include <cmath>
7#include <cstring>
8
9namespace strata_vit {
10namespace {
11namespace kn {
12
13struct V {
14    static constexpr int W = 4;
15    float x[4];
16    static V load(const float* p) { V v; std::memcpy(v.x, p, sizeof v.x); return v; }
17    void store(float* p) const { std::memcpy(p, x, sizeof x); }
18    static V set1(float f) { V v; for (int i = 0; i < 4; ++i) v.x[i] = f; return v; }
19    static V zero() { return set1(0.0f); }
20    static float sqrt1(float x) { return std::sqrt(x); }      // this tier is compiled at the baseline: the standard library is safe here
21    static V fma(V a, V b, V c) { V v; for (int i = 0; i < 4; ++i) v.x[i] = a.x[i] * b.x[i] + c.x[i]; return v; }
22    static float reduce_add(V a) { return (a.x[0] + a.x[2]) + (a.x[1] + a.x[3]); }
23    static float reduce_max(V a) { return std::fmax(std::fmax(a.x[0], a.x[1]), std::fmax(a.x[2], a.x[3])); }
24    friend V operator+(V a, V b) { V v; for (int i = 0; i < 4; ++i) v.x[i] = a.x[i] + b.x[i]; return v; }
25    friend V operator-(V a, V b) { V v; for (int i = 0; i < 4; ++i) v.x[i] = a.x[i] - b.x[i]; return v; }
26    friend V operator*(V a, V b) { V v; for (int i = 0; i < 4; ++i) v.x[i] = a.x[i] * b.x[i]; return v; }
27    friend V operator/(V a, V b) { V v; for (int i = 0; i < 4; ++i) v.x[i] = a.x[i] / b.x[i]; return v; }
28};
29inline V vmax(V a, V b) { V v; for (int i = 0; i < 4; ++i) v.x[i] = a.x[i] > b.x[i] ? a.x[i] : b.x[i]; return v; }
30inline V vexp(V a) {
31    V v;
32    for (int i = 0; i < 4; ++i) v.x[i] = std::exp(a.x[i] < -87.0f ? -87.0f : a.x[i] > 88.0f ? 88.0f : a.x[i]);
33    return v;
34}
35inline float round_one(float f, int kind) {
36    uint32_t u;
37    std::memcpy(&u, &f, 4);
38    if (kind == 1) {
39        if ((u & 0x7fffffffu) > 0x7f800000u) return f;
40        u += 0x7fffu + ((u >> 16) & 1u);
41        u &= 0xffff0000u;
42        std::memcpy(&f, &u, 4);
43        return f;
44    }
45    const float a = std::fabs(f);
46    if (!(a < 65520.0f)) return f;
47    if (a < 6.103515625e-05f) {
48        const float q = std::nearbyint(a * 16777216.0f) / 16777216.0f;
49        return (u & 0x80000000u) ? -q : q;
50    }
51    int e;
52    std::frexp(a, &e);
53    const float ulp = std::ldexp(1.0f, e - 11);
54    const float q = std::nearbyint(a / ulp) * ulp;
55    return (u & 0x80000000u) ? -q : q;
56}
57inline void round_buf(float* p, size_t n, int kind) {
58    for (size_t i = 0; i < n; ++i) p[i] = round_one(p[i], kind);
59}
60inline void conv16(const void* Wm, int kind, size_t off, int n, float* out) {
61    const uint16_t* p = (const uint16_t*) Wm + off;
62    for (int i = 0; i < n; ++i) out[i] = kind == 1 ? quant::bf16_to_f32(p[i]) : quant::f16_to_f32(p[i]);
63}
64
65}  // namespace kn
66}  // namespace
67}  // namespace strata_vit
68
69#define STRATA_KN_MR 4
70// this tier is compiled at the baseline: the standard library is safe here (see cpu_kernels_impl.h for why these and not look-alike helpers)
71#define KN_MIN(a, b) std::min(a, b)
72#define KN_MAX(a, b) std::max(a, b)
73#define KN_COPY(d, s, n) std::memcpy((d), (s), (size_t) (n) * sizeof(float))
74#define KN_ZERO(d, n) std::memset((d), 0, (size_t) (n) * sizeof(float))
75#define STRATA_KN_NAME "scalar"
76#define STRATA_KN_GETTER cpu_ops_scalar
77#include "cpu_kernels_impl.h"