okami.mom/strata

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

Niko1221merge f-split-gyro (Gyro-S/M trellis port and catalog entries) into f-split-intbcbf2caf3

main
4.3 KiB110 linesraw
1// src/kernels/cpu/hadamard.cpp - see include/strata/kernels/cpu/hadamard.hpp.
2#include "strata/kernels/cpu/hadamard.hpp"
3
4#include <cmath>
5#include <cstdlib>
6#include <fstream>
7#include <sstream>
8
9namespace strata::kernels::cpu {
10namespace {
11HadamardSpec g_spec;
12}
13
14const HadamardSpec& hadamard_spec() { return g_spec; }
15void hadamard_set(HadamardSpec h) { g_spec = std::move(h); }
16
17const float* hadamard_signs(int width) {
18    const auto it = g_spec.signs.find(width);
19    return it == g_spec.signs.end() ? nullptr : it->second.data();
20}
21
22bool hadamard_parse(const std::string& path, int64_t n_layers, HadamardSpec& h, std::string& err) {
23    std::ifstream in(path);
24    if (!in) { err = "cannot read " + path; return false; }
25    h = HadamardSpec{};
26    h.roles.assign((size_t) n_layers, 0);
27    std::vector<uint8_t> seen((size_t) n_layers, 0);
28    std::string line;
29    bool versioned = false;
30    while (std::getline(in, line)) {
31        if (line.empty()) continue;
32        if (line[0] == '#') {
33            static const char tag[] = "# strata hadamard v";
34            if (line.compare(0, sizeof tag - 1, tag) == 0) {
35                if (std::atoi(line.c_str() + sizeof tag - 1) != 1) {
36                    err = path + ": an unknown hadamard.txt version (this engine reads v1)";
37                    return false;
38                }
39                versioned = true;
40            }
41            continue;
42        }
43        std::istringstream ss(line);
44        std::string key;
45        ss >> key;
46        if (key == "block") {
47            long long b = 0;
48            if (!(ss >> b) || b < 2 || b > 4096 || (b & (b - 1)) != 0 || h.block) {
49                err = path + ": a bad or repeated block line: " + line;
50                return false;
51            }
52            h.block = (int) b;
53        } else if (key == "signs") {
54            long long w = 0;
55            if (!(ss >> w) || w <= 0 || w > (1 << 20) || h.signs.count((int) w)) {
56                err = path + ": a bad or repeated signs line";
57                return false;
58            }
59            std::vector<float> v((size_t) w);
60            for (auto& x : v) {
61                int s = 0;
62                if (!(ss >> s) || (s != 1 && s != -1)) { err = path + ": sign values must be +1 or -1"; return false; }
63                x = (float) s;
64            }
65            std::string extra;
66            if (ss >> extra) { err = path + ": a signs line longer than its width"; return false; }
67            h.signs.emplace((int) w, std::move(v));
68        } else if (key == "layer") {
69            long long l = -1;
70            int g = -1, u = -1, d = -1;
71            if (!(ss >> l >> g >> u >> d) || l < 0 || l >= n_layers || (g | u | d) & ~1 || seen[(size_t) l]) {
72                err = path + ": a bad or repeated layer line: " + line;
73                return false;
74            }
75            if (g != u) {   // gate and up read one activation: a transform for one of them only is not runnable
76                err = path + ": layer " + std::to_string(l) + " folds only one of gate/up";
77                return false;
78            }
79            seen[(size_t) l] = 1;
80            h.roles[(size_t) l] = (uint8_t) (g | (u << 1) | (d << 2));
81        } else {
82            err = path + ": an unknown line: " + line;
83            return false;
84        }
85    }
86    if (!versioned || h.block == 0) { err = path + ": no version header or no block line"; return false; }
87    for (const auto& [w, v] : h.signs)
88        if (w % h.block) { err = path + ": sign width " + std::to_string(w) + " is not whole blocks"; return false; }
89    return true;
90}
91
92void hadamard_rotate(float* x, int n, int block, const float* signs) {
93    if (signs)
94        for (int i = 0; i < n; ++i) x[i] *= signs[i];
95    const float scale = 1.0f / std::sqrt((float) block);
96    for (int b0 = 0; b0 < n; b0 += block) {
97        float* v = x + b0;
98        // the in-place fast Walsh-Hadamard transform in natural (Sylvester) order: entry (-1)^popcount(r & c)
99        for (int len = 1; len < block; len <<= 1)
100            for (int i = 0; i < block; i += len << 1)
101                for (int j = i; j < i + len; ++j) {
102                    const float a = v[j], c = v[j + len];
103                    v[j] = a + c;
104                    v[j + len] = a - c;
105                }
106        for (int i = 0; i < block; ++i) v[i] *= scale;
107    }
108}
109
110}  // namespace strata::kernels::cpu