okami.mom/strata

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

waveringanaedge0-strata: add the engine for Edge0 145B and Edge0 120Bfd19246dc

main
7.3 KiB162 linesraw
1#include "strata/core/latent_dense.hpp"
2
3#include <cerrno>
4#include <cstdio>
5#include <cstring>
6
7#if defined(_WIN32)
8#include <windows.h>
9#else
10#include <fcntl.h>
11#include <sys/mman.h>
12#include <sys/stat.h>
13#include <unistd.h>
14#endif
15
16namespace strata::core {
17
18#if defined(_WIN32)
19// Windows: CreateFileA + CreateFileMappingA + MapViewOfFile.  Same contract as the POSIX path:
20// strict size check, read-only mapping, unmap on close.  The mapping handle keeps the section
21// alive after the file handle closes.
22static void win_err(std::string& err, const std::string& path, const char* what) {
23    char buf[64];
24    std::snprintf(buf, sizeof(buf), " (win32 error %lu)", (unsigned long) GetLastError());
25    err = path + ": " + what + buf;
26}
27#endif
28
29LatentDense::~LatentDense() { close(); }
30
31void LatentDense::close() {
32#if defined(_WIN32)
33    if (base_) UnmapViewOfFile(base_);
34    if (map_) CloseHandle(map_);
35#else
36    if (base_) munmap(base_, size_);
37#endif
38    base_ = nullptr; map_ = nullptr;
39    size_ = 0; bytes_ = 0;
40}
41
42bool LatentDense::open(const std::string& path, int64_t n_layers, int64_t n_groups, int64_t n_embd,
43                       int64_t latent_dim, std::string& err) {
44    close();
45    n_layers_ = n_layers; n_groups_ = n_groups; n_embd_ = n_embd; latent_dim_ = latent_dim;
46    // Q4DU: STRATA_LATENT_DENSE_FMT=4 selects the Q4_0 pack (18 B/block), 8 or unset the
47    // historical Q8_0 one (34 B/block).  The file's size must then match the derived layout -
48    // a mismatched file/env pair fails the open with both candidate sizes spelled out.
49    fmt_ = 8;
50    if (const char* f = ::getenv("STRATA_LATENT_DENSE_FMT")) {
51        if (f[0] == '4' && f[1] == 0) fmt_ = 4;
52        else if (f[0] == '8' && f[1] == 0) fmt_ = 8;
53        else { err = path + ": STRATA_LATENT_DENSE_FMT must be 4 or 8 (got '" + std::string(f) + "')"; return false; }
54    }
55    const size_t db = d_bytes(), ub = u_bytes();
56    const uint64_t want = (uint64_t) n_layers * (db + ub);
57#if defined(_WIN32)
58    HANDLE fh = CreateFileA(path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING,
59                            FILE_ATTRIBUTE_NORMAL, nullptr);
60    if (fh == INVALID_HANDLE_VALUE) { win_err(err, path, "CreateFile"); return false; }
61    LARGE_INTEGER fsz;
62    if (!GetFileSizeEx(fh, &fsz)) { win_err(err, path, "GetFileSizeEx"); CloseHandle(fh); return false; }
63    if ((uint64_t) fsz.QuadPart != want) {
64        const uint64_t alt = (uint64_t) n_layers *
65            ((size_t) n_groups_ * (size_t) latent_dim_ * (size_t) (n_embd_ / 32) * (fmt_ == 4 ? 34 : 18) +
66             (size_t) n_groups_ * (size_t) n_embd_ * (size_t) (latent_dim_ / 32) * (fmt_ == 4 ? 34 : 18));
67        err = path + ": size " + std::to_string((long long) fsz.QuadPart) + " != " + std::to_string(want) +
68              " (fmt " + std::to_string(fmt_) + " wants " + std::to_string(want) +
69              "; the other fmt wants " + std::to_string((long long) alt) + ")";
70        CloseHandle(fh); return false;
71    }
72    HANDLE mh = CreateFileMappingA(fh, nullptr, PAGE_READONLY, 0, 0, nullptr);
73    if (!mh) { win_err(err, path, "CreateFileMapping"); CloseHandle(fh); return false; }
74    CloseHandle(fh);   // the mapping object holds the section
75    void* p = MapViewOfFile(mh, FILE_MAP_READ, 0, 0, 0);
76    if (!p) { win_err(err, path, "MapViewOfFile"); CloseHandle(mh); return false; }
77    map_ = mh;
78#else
79    int fd = ::open(path.c_str(), O_RDONLY);
80    if (fd < 0) { err = path + ": " + std::strerror(errno); return false; }
81    struct stat st;
82    if (fstat(fd, &st) != 0) { err = path + ": fstat: " + std::strerror(errno); ::close(fd); return false; }
83    if ((uint64_t) st.st_size != want) {   // strict: the env's format and the file's size disagree.
84        // NO silent fallback - a misread layout would produce garbage answers.  Name both
85        // candidate sizes so the operator sees exactly which format the file actually is.
86        const uint64_t alt = (uint64_t) n_layers *
87            ((size_t) n_groups_ * (size_t) latent_dim_ * (size_t) (n_embd_ / 32) * (fmt_ == 4 ? 34 : 18) +
88             (size_t) n_groups_ * (size_t) n_embd_ * (size_t) (latent_dim_ / 32) * (fmt_ == 4 ? 34 : 18));
89        err = path + ": size " + std::to_string((long long) st.st_size) + " != " + std::to_string(want) +
90              " (fmt " + std::to_string(fmt_) + " wants " + std::to_string(want) +
91              "; the other fmt wants " + std::to_string((long long) alt) + ")";
92        ::close(fd); return false;
93    }
94    void* p = mmap(nullptr, (size_t) want, PROT_READ, MAP_PRIVATE, fd, 0);
95    ::close(fd);
96    if (p == MAP_FAILED) { err = path + ": mmap: " + std::strerror(errno); return false; }
97#endif
98    base_ = (uint8_t*) p; size_ = (size_t) want; bytes_ = want;
99    return true;
100}
101
102const uint8_t* LatentDense::d(int64_t layer) const {
103    if (!base_ || layer < 0 || layer >= n_layers_) return nullptr;
104    return base_ + (size_t) layer * (d_bytes() + u_bytes());
105}
106
107const uint8_t* LatentDense::u(int64_t layer) const {
108    if (!base_ || layer < 0 || layer >= n_layers_) return nullptr;
109    return d(layer) + d_bytes();
110}
111
112LatentA::~LatentA() { close(); }
113void LatentA::close() {
114#if defined(_WIN32)
115    if (base_) UnmapViewOfFile(base_);
116    if (map_) CloseHandle(map_);
117#else
118    if (base_) munmap(base_, size_);
119#endif
120    base_ = nullptr; map_ = nullptr; size_ = 0;
121}
122bool LatentA::open(const std::string& path, int64_t n_layers, int64_t n_expert, int64_t blob_bytes, std::string& err) {
123    close();
124    n_layers_ = n_layers; n_expert_ = n_expert; blob_bytes_ = blob_bytes;
125    const uint64_t want = (uint64_t) n_layers * (uint64_t) n_expert * (uint64_t) blob_bytes;
126#if defined(_WIN32)
127    HANDLE fh = CreateFileA(path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING,
128                            FILE_ATTRIBUTE_NORMAL, nullptr);
129    if (fh == INVALID_HANDLE_VALUE) { win_err(err, path, "CreateFile"); return false; }
130    LARGE_INTEGER fsz;
131    if (!GetFileSizeEx(fh, &fsz)) { win_err(err, path, "GetFileSizeEx"); CloseHandle(fh); return false; }
132    if ((uint64_t) fsz.QuadPart != want) {
133        err = path + ": size " + std::to_string((long long) fsz.QuadPart) + " != " + std::to_string(want);
134        CloseHandle(fh); return false;
135    }
136    HANDLE mh = CreateFileMappingA(fh, nullptr, PAGE_READONLY, 0, 0, nullptr);
137    if (!mh) { win_err(err, path, "CreateFileMapping"); CloseHandle(fh); return false; }
138    CloseHandle(fh);
139    void* p = MapViewOfFile(mh, FILE_MAP_READ, 0, 0, 0);
140    if (!p) { win_err(err, path, "MapViewOfFile"); CloseHandle(mh); return false; }
141    map_ = mh;
142#else
143    int fd = ::open(path.c_str(), O_RDONLY);
144    if (fd < 0) { err = path + ": " + std::strerror(errno); return false; }
145    struct stat st;
146    if (fstat(fd, &st) != 0 || (uint64_t) st.st_size != want) {
147        err = path + ": size " + std::to_string((long long) st.st_size) + " != " + std::to_string(want);
148        ::close(fd); return false;
149    }
150    void* p = mmap(nullptr, (size_t) want, PROT_READ, MAP_PRIVATE, fd, 0);
151    ::close(fd);
152    if (p == MAP_FAILED) { err = path + ": mmap: " + std::strerror(errno); return false; }
153#endif
154    base_ = (uint8_t*) p; size_ = (size_t) want;
155    return true;
156}
157const uint8_t* LatentA::blob(int64_t layer, int64_t expert) const {
158    if (!base_ || layer < 0 || layer >= n_layers_ || expert < 0 || expert >= n_expert_) return nullptr;
159    return base_ + ((size_t) layer * (size_t) n_expert_ + (size_t) expert) * (size_t) blob_bytes_;
160}
161
162}  // namespace strata::core