Review 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. // src/core/expert_source.cpp - the adapter. See the header for the three clauses of the contract. #include "strata/core/expert_source.hpp" #include "strata/core/remote_experts.hpp" #include "strata/kernels/cpu/expert_layout.hpp" #include "strata/artifact/gguf_reader.hpp" #include "strata/core/pinned.hpp" #include "strata/platform/memory.hpp" #include "strata/kernels/elementwise.hpp" #include "strata/kernels/quantize_act.hpp" #include "strata/kernels/s2_expert_grouped.hpp" #include "strata/kernels/cpu/kq_avx2.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if defined(_WIN32) #define WIN32_LEAN_AND_MEAN #ifndef NOMINMAX #define NOMINMAX // std::numeric_limits::max() below #endif #include #else #include #include #include #include #endif // a 64-bit seek (as in pinned.cu): the 32-bit `fseek` wraps past 4 GiB, and the spelling differs per platform #ifndef STRATA_FSEEK64 #ifdef _WIN32 #define STRATA_FSEEK64(f, o) _fseeki64((f), (long long) (o), SEEK_SET) #else #define STRATA_FSEEK64(f, o) fseeko((f), (off_t) (o), SEEK_SET) #endif #endif namespace strata::core { namespace detail { bool cgroup_available_bytes(uint64_t limit, const CgroupMemoryStat& stat, uint64_t& bytes) { bytes = 0; if (!stat.valid) return false; // memory.stat's inactive_file can race memory.current, so bound it to charged usage first. uint64_t reclaimable = std::min(stat.inactive_file, stat.current); reclaimable = stat.file_dirty >= reclaimable ? 0 : reclaimable - stat.file_dirty; reclaimable = stat.file_writeback >= reclaimable ? 0 : reclaimable - stat.file_writeback; // Reclaiming clean file pages reduces usage; saturating subtraction also handles a transient over-limit read. const uint64_t usage_after_reclaim = stat.current - reclaimable; bytes = usage_after_reclaim < limit ? limit - usage_after_reclaim : 0; return true; } bool make_cache_complement_plan( int64_t n_layers, int64_t n_expert, const std::vector& layer_blob_bytes, const std::vector>& primary_gpu_pairs, const std::vector>& additional_gpu_pairs, std::vector& offsets, uint64_t& bytes, std::string& err) { offsets.clear(); bytes = 0; err.clear(); if (n_layers <= 0 || n_expert <= 0 || layer_blob_bytes.size() != (size_t) n_layers) { err = "FileExpertSource: invalid geometry for the cache complement plan"; return false; } if ((uint64_t) n_layers > (uint64_t) std::numeric_limits::max() / (uint64_t) n_expert) { err = "FileExpertSource: cache complement index table is too large"; return false; } const size_t count = (size_t) n_layers * (size_t) n_expert; std::vector omitted(count, 0); auto mark_pairs = [&](const std::vector>& pairs, uint8_t bit, const char* label) -> bool { for (const auto& pair : pairs) { if (pair.first < 0 || pair.second < 0 || pair.first >= n_layers || pair.second >= n_expert) { err = std::string("FileExpertSource: ") + label + " pair is outside the expert geometry"; return false; } const size_t index = (size_t) pair.first * (size_t) n_expert + (size_t) pair.second; if ((omitted[index] & bit) != 0) { err = std::string("FileExpertSource: duplicate ") + label + " pair in the cache complement plan"; return false; } if (bit == 2 && (omitted[index] & 1) != 0) { err = "FileExpertSource: the primary and additional GPU expert tiers overlap"; return false; } omitted[index] |= bit; } return true; }; if (!mark_pairs(primary_gpu_pairs, 1, "primary GPU") || !mark_pairs(additional_gpu_pairs, 2, "additional GPU")) return false; for (uint64_t blob_bytes : layer_blob_bytes) { if (blob_bytes == 0) { err = "FileExpertSource: cache complement layer has zero-sized expert blobs"; return false; } } offsets.assign(count, kNoCacheComplement); for (int64_t layer = 0; layer < n_layers; ++layer) { const uint64_t blob_bytes = layer_blob_bytes[(size_t) layer]; for (int64_t expert = 0; expert < n_expert; ++expert) { const size_t index = (size_t) layer * (size_t) n_expert + (size_t) expert; if (omitted[index] != 0) continue; if (bytes > std::numeric_limits::max() - blob_bytes) { offsets.clear(); bytes = 0; err = "FileExpertSource: cache complement size overflows"; return false; } offsets[index] = bytes; bytes += blob_bytes; } } if (bytes > (uint64_t) std::numeric_limits::max()) { offsets.clear(); bytes = 0; err = "FileExpertSource: cache complement exceeds the host address space"; return false; } return true; } const uint8_t* cache_complement_blob_or_fallback( size_t index, const std::vector& offsets, const uint8_t* complement_host, const uint8_t* mapped_fallback) { if (complement_host != nullptr && index < offsets.size() && offsets[index] != kNoCacheComplement) return complement_host + (size_t) offsets[index]; return mapped_fallback; } int64_t choose_resident_keep_from(const std::vector& slot_bytes, uint64_t base_bytes, uint64_t budget, int64_t lend_from) { if (base_bytes > budget) return -1; const int64_t slots = (int64_t) slot_bytes.size(); if (lend_from < 0 || lend_from > slots) lend_from = slots; // no lend region: only the experts no slot holds int64_t keep = slots; uint64_t bytes = base_bytes; while (keep > lend_from) { const uint64_t b = slot_bytes[(size_t) keep - 1]; if (b > budget - bytes) break; bytes += b; --keep; } return keep; } bool exchange_cache_complement(std::vector& offsets, size_t in, size_t out) { if (in == out || in >= offsets.size() || out >= offsets.size() || offsets[in] == kNoCacheComplement || offsets[out] != kNoCacheComplement) return false; offsets[out] = offsets[in]; offsets[in] = kNoCacheComplement; return true; } } // namespace detail namespace { #if defined(__linux__) bool read_cgroup_memory_stat(const std::filesystem::path& path, uint64_t current, detail::CgroupMemoryStat& stat) { std::ifstream input(path / "memory.stat"); if (!input) return false; bool inactive_file = false, file_dirty = false, file_writeback = false; std::string line; while (std::getline(input, line)) { std::istringstream fields(line); std::string key; uint64_t value = 0; if (!(fields >> key >> value)) return false; fields >> std::ws; if (!fields.eof()) return false; if (key == "inactive_file") { if (inactive_file) return false; inactive_file = true; stat.inactive_file = value; } else if (key == "file_dirty") { if (file_dirty) return false; file_dirty = true; stat.file_dirty = value; } else if (key == "file_writeback") { if (file_writeback) return false; file_writeback = true; stat.file_writeback = value; } } if (!input.eof() || !inactive_file || !file_dirty || !file_writeback) return false; stat.current = current; stat.valid = true; return true; } #endif bool available_memory_bytes(uint64_t& bytes) { #if defined(_WIN32) MEMORYSTATUSEX status{}; status.dwLength = sizeof(status); if (!GlobalMemoryStatusEx(&status)) return false; bytes = (uint64_t) status.ullAvailPhys; return bytes > 0; #elif defined(__linux__) // MemAvailable includes reclaimable page cache, unlike _SC_AVPHYS_PAGES. std::ifstream info("/proc/meminfo"); std::string line; bytes = 0; while (std::getline(info, line)) { std::istringstream fields(line); std::string key, unit; uint64_t value = 0; if (fields >> key >> value >> unit && key == "MemAvailable:" && unit == "kB" && value <= std::numeric_limits::max() / 1024) bytes = value * 1024; } if (bytes == 0) return false; // Account for the tightest cgroup-v2 ancestor limit when its normal mount is visible. // This is a point-in-time guard, not