Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

resident split: a test that the RAM copy keeps each stage's lend region (red: FileExpertSource has no stage_lend_regions, and no append_stage_lend_regions)

Evan7a7c568b9a4d
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6e0ce1c49..b93a5a4ca 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -468,6 +468,21 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP)
target_link_libraries(vmm_test PRIVATE strata_engine)
add_test(NAME vmm_test COMMAND vmm_test)
set_tests_properties(vmm_test PROPERTIES TIMEOUT 60)
+ # The resident RAM mode with a layer split: the copy keeps each stage's lend region, and the experts the
+ # prompt path borrows are served from RAM (the file source reads none of them). The device calls are
+ # wrapped - conversation_transfer_test's pattern - and `pin = false` keeps the copy pageable, so this
+ # runs on a CPU-only PC.
+ if(NOT STRATA_ENABLE_HIP AND UNIX AND NOT APPLE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
+ add_executable(split_lend_complement_test tests/core/split_lend_complement_test.cpp)
+ target_link_libraries(split_lend_complement_test PRIVATE strata_engine)
+ target_link_options(split_lend_complement_test PRIVATE
+ -Wl,--wrap=cudaMalloc -Wl,--wrap=cudaFree -Wl,--wrap=cudaMemset -Wl,--wrap=cudaMemcpy
+ -Wl,--wrap=cudaMemcpyAsync -Wl,--wrap=cudaStreamSynchronize -Wl,--wrap=cudaDeviceSynchronize
+ -Wl,--wrap=cudaGetLastError -Wl,--wrap=cudaGetDevice -Wl,--wrap=cudaSetDevice
+ -Wl,--wrap=cudaMemGetInfo -Wl,--wrap=cudaHostAlloc -Wl,--wrap=cudaFreeHost)
+ add_test(NAME split_lend_complement_test COMMAND split_lend_complement_test)
+ set_tests_properties(split_lend_complement_test PROPERTIES ENVIRONMENT "CUDA_VISIBLE_DEVICES=-1" TIMEOUT 120)
+ endif()
endif()
# #477: the learned expert profile (--expert-profile-save): its order and make_profile.py's file format (CPU only)
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tests/core/expert_profile_save_test.cpp")
diff --git a/tests/core/file_expert_source_test.cpp b/tests/core/file_expert_source_test.cpp
index 975f42faa..321c623ca 100644
--- a/tests/core/file_expert_source_test.cpp
+++ b/tests/core/file_expert_source_test.cpp
@@ -291,6 +291,51 @@ void test_resident_lend_region() {
require(choose_resident_keep_from({}, 0, 0, 0) == 0, "an empty cache");
}
+void test_split_lend_regions() {
+ using namespace strata::core::detail;
+ // two layers, three experts; a layer-0 blob is 5 bytes and a layer-1 blob 3. The plan under
+ // construction already holds the (0,1) expert (an expert no cache holds); stage 0's region lends
+ // (0,2) and (0,1) from its highest slots, stage 1's lends (1,2), (1,0), (1,1) in that order.
+ const std::vector<uint64_t> blobs{5, 3};
+ const std::vector<std::vector<std::pair<int32_t, int32_t>>> regions = {{{0, 2}, {0, 1}}, {{1, 2}, {1, 0}, {1, 1}}};
+ std::string err;
+ std::vector<uint64_t> offsets;
+ uint64_t bytes = 0;
+
+ // room for everything: (0,1) is already in the copy and is passed over, not counted twice
+ offsets = {kNoCacheComplement, 0, kNoCacheComplement, kNoCacheComplement, kNoCacheComplement, kNoCacheComplement};
+ bytes = 5;
+ require(append_stage_lend_regions(2, 3, blobs, offsets, bytes, 100, regions, err) == 4,
+ "both stage lend regions must be kept");
+ require(bytes == 19 &&
+ offsets == std::vector<uint64_t>{kNoCacheComplement, 0, 5, 13, 16, 10},
+ "the stage lend regions got wrong compact offsets");
+
+ // the cap is the whole copy's: the stage-0 walk fits (0,2) and stage 1 stops at the first pair that
+ // does not fit; that stage's rest stays on the file
+ offsets = {kNoCacheComplement, 0, kNoCacheComplement, kNoCacheComplement, kNoCacheComplement, kNoCacheComplement};
+ bytes = 5;
+ require(append_stage_lend_regions(2, 3, blobs, offsets, bytes, 10, regions, err) == 1, "cap 10 keeps only (0,2)");
+ require(bytes == 10 && offsets[2] == 5 && offsets[3] == kNoCacheComplement && offsets[5] == kNoCacheComplement,
+ "the walk placed experts past the cap");
+
+ // (0,2)'s 5 bytes do not fit in the 4 left: stage 0 keeps nothing, and stage 1's own walk still runs -
+ // its (1,2) fits, its (1,0) stops that stage
+ offsets = {kNoCacheComplement, 0, kNoCacheComplement, kNoCacheComplement, kNoCacheComplement, kNoCacheComplement};
+ bytes = 5;
+ require(append_stage_lend_regions(2, 3, blobs, offsets, bytes, 9, regions, err) == 1,
+ "a stage refused at the cap must not stop the next stage's walk");
+ require(bytes == 8 && offsets[2] == kNoCacheComplement && offsets[5] == 5 && offsets[3] == kNoCacheComplement,
+ "the refused stage's experts entered the copy, or the next stage's did not");
+
+ // a pair outside the geometry is an error, and the plan is left untouched: validate before appending
+ offsets = {kNoCacheComplement, 0, kNoCacheComplement, kNoCacheComplement, kNoCacheComplement, kNoCacheComplement};
+ bytes = 5;
+ require(append_stage_lend_regions(2, 3, blobs, offsets, bytes, 100, {{{0, 2}, {2, 0}}}, err) == -1 &&
+ !err.empty() && bytes == 5 && offsets[2] == kNoCacheComplement,
+ "an out-of-range lend-region pair was accepted, or a rejected walk touched the plan");
+}
+
void test_resident_exchange() {
using namespace strata::core;
using namespace strata::core::detail;
@@ -686,6 +731,7 @@ int main(int argc, char** argv) {
try {
test_complement_plan();
test_resident_lend_region();
+ test_split_lend_regions();
test_resident_exchange();
test_resident_memory_budget();
test_pin_pacing();
diff --git a/tests/core/split_lend_complement_test.cpp b/tests/core/split_lend_complement_test.cpp
new file mode 100644
index 000000000..c6d5dd515
--- /dev/null
+++ b/tests/core/split_lend_complement_test.cpp
@@ -0,0 +1,219 @@
+// The resident RAM mode with a layer split: the compact copy holds the experts no stage's cache holds AND each
+// stage's LEND REGION - the experts in the cache slots the prompt path borrows - so a prompt streams the
+// borrowed experts from RAM instead of reading the model files through page faults. Runs on a CPU-only
+// PC: the device calls are wrapped at the link (`--wrap` in CMakeLists.txt, conversation_transfer_test's
+// pattern), the memory is ordinary memory, and `pin = false` keeps the copy pageable.
+#include "strata/core/expert_source.hpp"
+#include "strata/kernels/cpu/expert_layout.hpp"
+
+#include <cuda_runtime.h>
+
+#include <chrono>
+#include <cstdint>
+#include <cstdlib>
+#include <cstring>
+#include <filesystem>
+#include <fstream>
+#include <iostream>
+#include <stdexcept>
+#include <string>
+#include <system_error>
+#include <utility>
+#include <vector>
+
+// ---- the wrapped device calls: memory is ordinary memory and every operation completes at once.
+extern "C" cudaError_t __wrap_cudaMalloc(void** p, size_t n) {
+ *p = std::malloc(n);
+ return *p ? cudaSuccess : cudaErrorMemoryAllocation;
+}
+extern "C" cudaError_t __wrap_cudaFree(void* p) { std::free(p); return cudaSuccess; }
+extern "C" cudaError_t __wrap_cudaMemset(void* d, int v, size_t n) { std::memset(d, v, n); return cudaSuccess; }
+extern "C" cudaError_t __wrap_cudaMemcpy(void* d, const void* s, size_t n, cudaMemcpyKind k) {
+ std::memcpy(d, s, n);
+ return cudaSuccess;
+}
+extern "C" cudaError_t __wrap_cudaMemcpyAsync(void* d, const void* s, size_t n, cudaMemcpyKind k, cudaStream_t st) {
+ std::memcpy(d, s, n);
+ return cudaSuccess;
+}
+extern "C" cudaError_t __wrap_cudaStreamSynchronize(cudaStream_t st) { return cudaSuccess; }
+extern "C" cudaError_t __wrap_cudaDeviceSynchronize(void) { return cudaSuccess; }
+extern "C" cudaError_t __wrap_cudaGetLastError(void) { return cudaSuccess; }
+extern "C" cudaError_t __wrap_cudaGetDevice(int* d) { *d = 0; return cudaSuccess; }
+extern "C" cudaError_t __wrap_cudaSetDevice(int d) { return cudaSuccess; }
+extern "C" cudaError_t __wrap_cudaMemGetInfo(size_t* free_b, size_t* total_b) {
+ *free_b = 64ull << 30;
+ *total_b = 128ull << 30;
+ return cudaSuccess;
+}
+extern "C" cudaError_t __wrap_cudaHostAlloc(void** p, size_t n, unsigned f) {
+ *p = std::malloc(n);
+ return *p ? cudaSuccess : cudaErrorMemoryAllocation;
+}
+extern "C" cudaError_t __wrap_cudaFreeHost(void* p) { std::free(p); return cudaSuccess; }
+
+namespace fs = std::filesystem;
+
+namespace {
+
+constexpr int64_t kLayers = 2; // stage 0 owns layer 0, stage 1 owns layer 1 (the split's `claimed` layers)
+constexpr int64_t kExperts = 4;
+
+void require(bool ok, const std::string& message) {
+ if (!ok) throw std::runtime_error(message);
+}
+
+struct TempDirectory {
+ fs::path path;
+
+ explicit TempDirectory(const fs::path& base = fs::temp_directory_path()) {
+ const auto stamp = std::chrono::steady_clock::now().time_since_epoch().count();
+ path = base / ("strata-split-lend-test-" + std::to_string(stamp));
+ fs::create_directories(path);
+ }
+
+ ~TempDirectory() {
+ std::error_code ignored;
+ fs::remove_all(path, ignored);
+ }
+};
+
+const uint8_t truth_byte(int64_t l, int64_t e, size_t i) {
+ return (uint8_t) ((i * 131u + (size_t) (l * kExperts + e) * 37u) ^ (i >> 8));
+}
+
+// one fully-built fixture: experts.bin written, the source open, both stage caches open with their
+// own layer's experts admitted and filled (highest slot = highest expert, as the
+// cache fills from slot 0 up)
+struct Rig {
+ std::vector<std::vector<uint8_t>> truth; // [layer * kExperts + expert]
+ TempDirectory dir;
+ strata::core::FileExpertSource src;
+ strata::core::ExpertCache stage0, stage1; // the two split stages' caches
+ size_t blob = 0;
+
+ explicit Rig(const char* tag) {
+ std::string err;
+ require(strata::kernels::cpu::expert_layout_load(dir.path.string(), kLayers, kExperts, err), err);
+ blob = (size_t) strata::kernels::cpu::BLOB;
+ truth.resize((size_t) kLayers * kExperts);
+ {
+ std::ofstream out(dir.path / "experts.bin", std::ios::binary);
+ for (int64_t l = 0; l < kLayers; ++l)
+ for (int64_t e = 0; e < kExperts; ++e) {
+ std::vector<uint8_t>& t = truth[(size_t) l * kExperts + e];
+ t.resize(blob);
+ for (size_t i = 0; i < blob; ++i) t[i] = truth_byte(l, e, i);
+ out.write((const char*) t.data(), (std::streamsize) blob);
+ }
+ require((bool) out, std::string(tag) + ": fixture write failed");
+ }
+ require(src.open(dir.path.string(), kLayers, kExperts, err), err);
+ // kExperts slots each: every layer of the stage, and the tail the prompt path borrows
+ require(stage0.open(kExperts, kLayers, kExperts, blob, err), err);
+ require(stage1.open(kExperts, kLayers, kExperts, blob, err), err);
+ for (int64_t e = 0; e < kExperts; ++e) {
+ const int32_t s0 = stage0.admit(0, e);
+ require(s0 >= 0 && stage0.fill_slot_blocking(s0, truth[(size_t) e].data(), err), err);
+ const int32_t s1 = stage1.admit(1, e);
+ require(s1 >= 0 && stage1.fill_slot_blocking(s1, truth[(size_t) kExperts + e].data(), err), err);
+ }
+ }
+
+ // every expert of stage 1's cache, the way generate hands the later stages to pin_cache_complement
+ std::vector<std::pair<int32_t, int32_t>> stage1_pairs() const {
+ std::vector<std::pair<int32_t, int32_t>> pairs;
+ for (int64_t e = 0; e < kExperts; ++e) pairs.emplace_back(1, (int32_t) e);
+ return pairs;
+ }
+};
+
+// both stage caches hold everything of their layer: the complement - the experts NO cache
+// holds - is empty, so before this feature the copy kept no bytes at all and every expert a prompt borrowed
+// came back through page faults from the mapped files.
+void test_split_lend_region_served_from_ram() {
+ Rig rig("split lend region");
+ std::string err;
+
+ // each stage lends its two highest slots to the prompt path: pairs listed highest slot first,
+ // as the borrowing takes them
+ rig.src.stage_lend_regions({{{0, 3}, {0, 2}}, {{1, 3}, {1, 2}}});
+ require(rig.src.pin_cache_complement(rig.stage0, err, false, rig.stage1_pairs(), -1, 0, 0), err);
+
+ require(rig.src.resident_bytes() == 4 * (uint64_t) rig.blob,
+ "the copy is the empty complement plus the two stages' lend regions");
+ require(rig.src.resident_lent_slots() == 4, "the copy reports both stages' lend regions");
+ for (int64_t l = 0; l < kLayers; ++l)
+ for (int64_t e = 0; e < kExperts; ++e) {
+ const bool in_a_cache = true; // every pair is held by some stage's cache: none is in the complement
+ require(rig.src.has_resident(l, e) == (e >= 2), "the copy holds exactly the two stages' lent experts");
+ }
+
+ // the borrowed experts are served from the RAM copy: the lookup resolves into the copy, the bytes match
+ // the file's, and not one read touched the file source
+ std::vector<uint8_t> dst(rig.blob);
+ for (int64_t l = 0; l < kLayers; ++l)
+ for (int64_t e = 2; e < kExperts; ++e) {
+ require(rig.src.blob(l, e) == rig.src.resident_blob(l, e), "a lent expert is not served from the copy");
+ require(rig.src.copy_blob(l, e, dst.data()) &&
+ dst == rig.truth[(size_t) l * kExperts + e],
+ "the copy serves wrong bytes for a lent expert");
+ }
+ require(rig.src.file_reads() == 0 && rig.src.file_read_bytes() == 0,
+ "the prompt path's borrowed experts came from the file source, not the RAM copy");
+}
+
+// a --resident-budget-gib smaller than the regions: the copy keeps the bytes it can - the same budget
+// caps the region walk - and the experts past it keep the file fallback that reads the same bytes
+void test_split_lend_region_budget() {
+ Rig rig("split lend region budget");
+ std::string err;
+
+ rig.src.stage_lend_regions({{{0, 3}, {0, 2}}, {{1, 3}, {1, 2}}});
+ const uint64_t budget = 2 * (uint64_t) rig.blob; // room for one stage's region, not both
+ require(rig.src.pin_cache_complement(rig.stage0, err, false, rig.stage1_pairs(), -1, 0, budget), err);
+
+ require(rig.src.resident_bytes() == budget, "the budget did not cap the stage lend regions");
+ require(rig.src.resident_lent_slots() == 2, "the clamped regions kept the wrong count");
+ require(rig.src.has_resident(0, 3) && rig.src.has_resident(0, 2), "the first stage's region was not kept");
+ require(!rig.src.has_resident(1, 3) && !rig.src.has_resident(1, 2), "bytes past the budget entered the copy");
+
+ std::vector<uint8_t> dst(rig.blob);
+ require(rig.src.copy_blob(0, 3, dst.data()) && dst == rig.truth[3], "wrong bytes from the region inside the cap");
+ require(rig.src.file_reads() == 0, "an expert the copy holds was read from the file");
+ require(rig.src.copy_blob(1, 3, dst.data()) && dst == rig.truth[kExperts + 3],
+ "the expert past the budget lost its file fallback");
+ require(rig.src.file_reads() == 0 && rig.src.file_read_bytes() == (uint64_t) rig.blob,
+ "the expert past the budget was served by the file source, exactly one blob read");
+}
+
+// the #848 shape, with no stage regions set: with the later stage's pairs handed over as
+// `additional_gpu_pairs` the copy keeps NOTHING (the complement is empty and the single lend region is off), so
+// this is the state that measured 3.7 GB of NVMe reads over one 73K-token prompt on 2026-10-06
+void test_split_without_regions_keeps_nothing() {
+ Rig rig("split without regions");
+ std::string err;
+
+ require(rig.src.pin_cache_complement(rig.stage0, err, false, rig.stage1_pairs(), -1, 0, 0), err);
+ require(rig.src.complement_ready(), "the empty complement did not complete");
+ require(rig.src.resident_bytes() == 0 && rig.src.resident_lent_slots() == 0,
+ "without stage regions the copy should hold no expert at all");
+ for (int64_t l = 0; l < kLayers; ++l)
+ for (int64_t e = 0; e < kExperts; ++e)
+ require(!rig.src.has_resident(l, e), "an expert neither in a cache's complement region entered the copy");
+}
+
+} // namespace
+
+int main() {
+ try {
+ test_split_lend_region_served_from_ram();
+ test_split_lend_region_budget();
+ test_split_without_regions_keeps_nothing();
+ std::cout << "split_lend_complement_test: PASS\n";
+ return 0;
+ } catch (const std::exception& error) {
+ std::cerr << "split_lend_complement_test: " << error.what() << '\n';
+ return 1;
+ }
+}