okami.mom/strata

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

waveringanaadd staged DS4 master relocation and row exactness tests90a1a9e1c

main
3.5 KiB83 linesraw
1#pragma once
2#include <algorithm>
3#include <cmath>
4#include <cstdint>
5#include <cstring>
6#include <stdexcept>
7#include <vector>
8
9namespace strata::ds4::scr_cpu {
10inline float e4value(unsigned q) {
11    unsigned e = q >> 3, m = q & 7;
12    return e == 0 ? m * 0.001953125f : std::ldexp(1.0f + m / 8.0f, (int) e - 7);
13}
14inline uint8_t e4code(float x) {
15    unsigned best = 0;
16    float dist = std::fabs(x);
17    for (unsigned q = 1; q < 127; ++q) {
18        float d = std::fabs(e4value(q) - x);
19        if (d < dist || (d == dist && (q & 1) == 0)) { best = q; dist = d; }
20    }
21    return (uint8_t) best;
22}
23inline uint8_t e2code(float x) {
24    const float a = std::fabs(x);
25    const unsigned m = a <= .25f ? 0 : a < .75f ? 1 : a <= 1.25f ? 2 : a < 1.75f ? 3 :
26                       a <= 2.5f ? 4 : a < 3.5f ? 5 : a <= 5.f ? 6 : 7;
27    return (uint8_t) (m | (std::signbit(x) ? 8 : 0));
28}
29inline size_t row_bytes(int fmt, int len) {
30    return ((size_t) len / 2 + len / (fmt == 2 ? 16 : 32) + 15) / 16 * 16;
31}
32inline void geometry(int fmt, int len, int rd) {
33    if ((fmt != 2 && fmt != 3) || rd <= 0 || rd > len || len % 32 || rd % 32)
34        throw std::invalid_argument("SCR requires whole RoPE quantization groups");
35}
36// Fresh finish without normalization: the input is the F32 activation captured after norm.
37inline std::vector<uint8_t> fresh(std::vector<float> x, int rd, int fmt, int pos, const std::vector<float>& freqs) {
38    const int len = (int) x.size(), group = fmt == 2 ? 16 : 32;
39    geometry(fmt, len, rd);
40    for (int i = 0; i < rd / 2; ++i) {
41        const float angle = (float) pos * freqs.at((size_t) i), c = std::cos(angle), s = std::sin(angle);
42        const size_t k = (size_t) (len - rd + 2 * i);
43        const float a = x[k], b = x[k + 1];
44        x[k] = std::fma(-b, s, a * c);
45        x[k + 1] = std::fma(a, s, b * c);
46    }
47    std::vector<uint8_t> out(row_bytes(fmt, len), 0);
48    for (int k = 0; k < len; k += group) {
49        float a = 0;
50        for (int j = 0; j < group; ++j) a = std::max(a, std::fabs(x[(size_t) k + j]));
51        float scale;
52        uint8_t code;
53        if (fmt == 2) {
54            code = e4code(std::max(a, 6.f * .001953125f) / 6.f);
55            scale = e4value(code);
56        } else {
57            const float v = std::max(a, 6.f * 1.1754943508222875e-38f) * (1.f / 6.f);
58            int e;
59            const float m = std::frexp(v, &e);
60            scale = std::ldexp(1.f, e - (m == .5f ? 1 : 0));
61            uint32_t bits; std::memcpy(&bits, &scale, 4);
62            code = (uint8_t) (bits >> 23);
63        }
64        out[(size_t) len / 2 + k / group] = code;
65        for (int j = 0; j < group; j += 2) {
66            const uint8_t lo = e2code(std::clamp(x[(size_t) k + j] / scale, -6.f, 6.f));
67            const uint8_t hi = e2code(std::clamp(x[(size_t) k + j + 1] / scale, -6.f, 6.f));
68            out[((size_t) k + j) / 2] = (uint8_t) (lo | hi << 4);
69        }
70    }
71    return out;
72}
73inline std::vector<uint8_t> relocate(std::vector<uint8_t> packed, const std::vector<float>& master,
74                                     int len, int fmt, int pos, const std::vector<float>& freqs) {
75    const int rd = (int) master.size(), group = fmt == 2 ? 16 : 32;
76    geometry(fmt, len, rd);
77    if (packed.size() != row_bytes(fmt, len)) throw std::invalid_argument("SCR packed row size");
78    auto tail = fresh(master, rd, fmt, pos, freqs);
79    std::copy_n(tail.begin(), rd / 2, packed.begin() + (len - rd) / 2);
80    std::copy_n(tail.begin() + rd / 2, rd / group, packed.begin() + len / 2 + (len - rd) / group);
81    return packed;
82}
83} // namespace strata::ds4::scr_cpu