okami.mom/strata

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

brenoperucchibench: community report, RTX 5090 + Ryzen 9 5950X (AVX2), UD-Q4_K_XL / IQ3_S / Swift IQ3_XXS on engines 0.1.31-0.1.39 (#433)2a7db0422

main
9.0 KiB242 linesraw
1Review the following C++ source file from the Strata engine (v0.1.32, src/core/expert_source.cpp, first 9,000 bytes). List the three most important correctness risks you see, with line references.
2
3// src/core/expert_source.cpp - the adapter.  See the header for the three clauses of the contract.
4#include "strata/core/expert_source.hpp"
5#include "strata/core/remote_experts.hpp"
6#include "strata/kernels/cpu/expert_layout.hpp"
7#include "strata/artifact/gguf_reader.hpp"
8
9#include "strata/core/pinned.hpp"
10#include "strata/platform/memory.hpp"
11#include "strata/kernels/elementwise.hpp"
12#include "strata/kernels/quantize_act.hpp"
13#include "strata/kernels/s2_expert_grouped.hpp"
14#include "strata/kernels/cpu/kq_avx2.hpp"
15
16#include <cuda_runtime.h>
17
18#include <algorithm>
19#include <atomic>
20#include <chrono>
21#include <cstdio>
22#include <cstdlib>
23#include <cstring>
24#include <fstream>
25#include <filesystem>
26#include <sstream>
27#include <limits>
28#include <map>
29#include <memory>
30#include <mutex>
31#include <thread>
32#include <utility>
33#include <vector>
34
35#if defined(_WIN32)
36#define WIN32_LEAN_AND_MEAN
37#ifndef NOMINMAX
38#define NOMINMAX   // std::numeric_limits<T>::max() below
39#endif
40#include <windows.h>
41#else
42#include <fcntl.h>
43#include <sys/mman.h>
44#include <sys/stat.h>
45#include <unistd.h>
46#endif
47
48// a 64-bit seek (as in pinned.cu): the 32-bit `fseek` wraps past 4 GiB, and the spelling differs per platform
49#ifndef STRATA_FSEEK64
50#ifdef _WIN32
51#define STRATA_FSEEK64(f, o) _fseeki64((f), (long long) (o), SEEK_SET)
52#else
53#define STRATA_FSEEK64(f, o) fseeko((f), (off_t) (o), SEEK_SET)
54#endif
55#endif
56
57namespace strata::core {
58
59namespace detail {
60
61bool cgroup_available_bytes(uint64_t limit, const CgroupMemoryStat& stat, uint64_t& bytes) {
62    bytes = 0;
63    if (!stat.valid) return false;
64
65    // memory.stat's inactive_file can race memory.current, so bound it to charged usage first.
66    uint64_t reclaimable = std::min(stat.inactive_file, stat.current);
67    reclaimable = stat.file_dirty >= reclaimable ? 0 : reclaimable - stat.file_dirty;
68    reclaimable = stat.file_writeback >= reclaimable ? 0 : reclaimable - stat.file_writeback;
69
70    // Reclaiming clean file pages reduces usage; saturating subtraction also handles a transient over-limit read.
71    const uint64_t usage_after_reclaim = stat.current - reclaimable;
72    bytes = usage_after_reclaim < limit ? limit - usage_after_reclaim : 0;
73    return true;
74}
75
76bool make_cache_complement_plan(
77    int64_t n_layers, int64_t n_expert, const std::vector<uint64_t>& layer_blob_bytes,
78    const std::vector<std::pair<int32_t, int32_t>>& primary_gpu_pairs,
79    const std::vector<std::pair<int32_t, int32_t>>& additional_gpu_pairs,
80    std::vector<uint64_t>& offsets, uint64_t& bytes, std::string& err) {
81    offsets.clear();
82    bytes = 0;
83    err.clear();
84    if (n_layers <= 0 || n_expert <= 0 || layer_blob_bytes.size() != (size_t) n_layers) {
85        err = "FileExpertSource: invalid geometry for the cache complement plan";
86        return false;
87    }
88    if ((uint64_t) n_layers > (uint64_t) std::numeric_limits<size_t>::max() / (uint64_t) n_expert) {
89        err = "FileExpertSource: cache complement index table is too large";
90        return false;
91    }
92    const size_t count = (size_t) n_layers * (size_t) n_expert;
93    std::vector<uint8_t> omitted(count, 0);
94    auto mark_pairs = [&](const std::vector<std::pair<int32_t, int32_t>>& pairs, uint8_t bit,
95                          const char* label) -> bool {
96        for (const auto& pair : pairs) {
97            if (pair.first < 0 || pair.second < 0 || pair.first >= n_layers || pair.second >= n_expert) {
98                err = std::string("FileExpertSource: ") + label + " pair is outside the expert geometry";
99                return false;
100            }
101            const size_t index = (size_t) pair.first * (size_t) n_expert + (size_t) pair.second;
102            if ((omitted[index] & bit) != 0) {
103                err = std::string("FileExpertSource: duplicate ") + label + " pair in the cache complement plan";
104                return false;
105            }
106            if (bit == 2 && (omitted[index] & 1) != 0) {
107                err = "FileExpertSource: the primary and additional GPU expert tiers overlap";
108                return false;
109            }
110            omitted[index] |= bit;
111        }
112        return true;
113    };
114    if (!mark_pairs(primary_gpu_pairs, 1, "primary GPU") ||
115        !mark_pairs(additional_gpu_pairs, 2, "additional GPU")) return false;
116    for (uint64_t blob_bytes : layer_blob_bytes) {
117        if (blob_bytes == 0) {
118            err = "FileExpertSource: cache complement layer has zero-sized expert blobs";
119            return false;
120        }
121    }
122
123    offsets.assign(count, kNoCacheComplement);
124    for (int64_t layer = 0; layer < n_layers; ++layer) {
125        const uint64_t blob_bytes = layer_blob_bytes[(size_t) layer];
126        for (int64_t expert = 0; expert < n_expert; ++expert) {
127            const size_t index = (size_t) layer * (size_t) n_expert + (size_t) expert;
128            if (omitted[index] != 0) continue;
129            if (bytes > std::numeric_limits<uint64_t>::max() - blob_bytes) {
130                offsets.clear();
131                bytes = 0;
132                err = "FileExpertSource: cache complement size overflows";
133                return false;
134            }
135            offsets[index] = bytes;
136            bytes += blob_bytes;
137        }
138    }
139    if (bytes > (uint64_t) std::numeric_limits<size_t>::max()) {
140        offsets.clear();
141        bytes = 0;
142        err = "FileExpertSource: cache complement exceeds the host address space";
143        return false;
144    }
145    return true;
146}
147
148const uint8_t* cache_complement_blob_or_fallback(
149    size_t index, const std::vector<uint64_t>& offsets, const uint8_t* complement_host,
150    const uint8_t* mapped_fallback) {
151    if (complement_host != nullptr && index < offsets.size() && offsets[index] != kNoCacheComplement)
152        return complement_host + (size_t) offsets[index];
153    return mapped_fallback;
154}
155
156int64_t choose_resident_keep_from(const std::vector<uint64_t>& slot_bytes, uint64_t base_bytes, uint64_t budget,
157                                  int64_t lend_from) {
158    if (base_bytes > budget) return -1;
159    const int64_t slots = (int64_t) slot_bytes.size();
160    if (lend_from < 0 || lend_from > slots) lend_from = slots;   // no lend region: only the experts no slot holds
161    int64_t keep = slots;
162    uint64_t bytes = base_bytes;
163    while (keep > lend_from) {
164        const uint64_t b = slot_bytes[(size_t) keep - 1];
165        if (b > budget - bytes) break;
166        bytes += b;
167        --keep;
168    }
169    return keep;
170}
171
172bool exchange_cache_complement(std::vector<uint64_t>& offsets, size_t in, size_t out) {
173    if (in == out || in >= offsets.size() || out >= offsets.size() || offsets[in] == kNoCacheComplement ||
174        offsets[out] != kNoCacheComplement) return false;
175    offsets[out] = offsets[in];
176    offsets[in] = kNoCacheComplement;
177    return true;
178}
179
180}  // namespace detail
181
182namespace {
183
184#if defined(__linux__)
185bool read_cgroup_memory_stat(const std::filesystem::path& path, uint64_t current,
186                             detail::CgroupMemoryStat& stat) {
187    std::ifstream input(path / "memory.stat");
188    if (!input) return false;
189
190    bool inactive_file = false, file_dirty = false, file_writeback = false;
191    std::string line;
192    while (std::getline(input, line)) {
193        std::istringstream fields(line);
194        std::string key;
195        uint64_t value = 0;
196        if (!(fields >> key >> value)) return false;
197        fields >> std::ws;
198        if (!fields.eof()) return false;
199
200        if (key == "inactive_file") {
201            if (inactive_file) return false;
202            inactive_file = true;
203            stat.inactive_file = value;
204        } else if (key == "file_dirty") {
205            if (file_dirty) return false;
206            file_dirty = true;
207            stat.file_dirty = value;
208        } else if (key == "file_writeback") {
209            if (file_writeback) return false;
210            file_writeback = true;
211            stat.file_writeback = value;
212        }
213    }
214    if (!input.eof() || !inactive_file || !file_dirty || !file_writeback) return false;
215    stat.current = current;
216    stat.valid = true;
217    return true;
218}
219#endif
220
221bool available_memory_bytes(uint64_t& bytes) {
222#if defined(_WIN32)
223    MEMORYSTATUSEX status{};
224    status.dwLength = sizeof(status);
225    if (!GlobalMemoryStatusEx(&status)) return false;
226    bytes = (uint64_t) status.ullAvailPhys;
227    return bytes > 0;
228#elif defined(__linux__)
229    // MemAvailable includes reclaimable page cache, unlike _SC_AVPHYS_PAGES.
230    std::ifstream info("/proc/meminfo");
231    std::string line;
232    bytes = 0;
233    while (std::getline(info, line)) {
234        std::istringstream fields(line);
235        std::string key, unit;
236        uint64_t value = 0;
237        if (fields >> key >> value >> unit && key == "MemAvailable:" && unit == "kB" &&
238            value <= std::numeric_limits<uint64_t>::max() / 1024) bytes = value * 1024;
239    }
240    if (bytes == 0) return false;
241    // Account for the tightest cgroup-v2 ancestor limit when its normal mount is visible.
242    // This is a point-in-time guard, not