From 950c1199dd0c55a5683e7e78eab761d6f16165d6 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Thu, 1 Oct 2026 20:51:16 +0200 Subject: [PATCH] expert cache: --expert-cache-per-layer starts on native packs (#369, dag08) open_sized seeded every layer's cursor at slot 0, so all layers wrote the same slots and the startup check failed; the profile fill ended at the first full layer. Now each layer starts in its own range, a full layer skips only its own pairs, and per-layer mode keeps slots of the largest blob (its ranges ignore the rank a sized slot was cut for). The default (shared) cache is unchanged. --- CMakeLists.txt | 5 +++ src/core/expert_cache.cpp | 7 ++++ src/program/generate.cpp | 17 +++++++-- tests/core/expert_cache_per_layer_test.cpp | 44 ++++++++++++++++++++++ 4 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 tests/core/expert_cache_per_layer_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 8661ccce..73f687b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -362,6 +362,11 @@ if(STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP) target_link_libraries(native_dense_ple_key_test PRIVATE strata_engine) add_test(NAME native_dense_ple_key_test COMMAND native_dense_ple_key_test) set_tests_properties(native_dense_ple_key_test PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60) + # #369: per-layer admission on a sized (native pack) cache starts each layer in its own range + add_executable(expert_cache_per_layer_test tests/core/expert_cache_per_layer_test.cpp) + target_link_libraries(expert_cache_per_layer_test PRIVATE strata_engine) + add_test(NAME expert_cache_per_layer_test COMMAND expert_cache_per_layer_test) + set_tests_properties(expert_cache_per_layer_test PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60) endif() if(STRATA_BUILD_CONVERSATION_TESTS) add_executable(conversation_snapshot_test src/core/conversation_snapshot_test.cpp) diff --git a/src/core/expert_cache.cpp b/src/core/expert_cache.cpp index 6eb09294..3588cd5d 100644 --- a/src/core/expert_cache.cpp +++ b/src/core/expert_cache.cpp @@ -182,7 +182,14 @@ bool ExpertCache::open_sized(const std::vector& slot_bytes, int64_t n_l return false; } #endif + // #369: each layer's cursor at the bottom of its own range, as open() seeds it - open() above ran on byte-sized + // "slots", so its seeds are not slot indices layer_next_.assign((size_t) (n_layers > 0 ? n_layers : 0), 0); + for (int64_t l = 0; l < n_layers; ++l) { + int64_t lo = 0, hi = 0; + layer_slot_range(l, lo, hi); + layer_next_[(size_t) l] = (int32_t) lo; + } return true; } diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 7f1e3e96..78b5b6d3 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -2690,8 +2690,10 @@ int main(int argc, char** argv) { } // plan v0.3 P6: a native pack's blobs differ per layer, so with a profile its slots are sized per pair: the // same VRAM holds ~30% more IQ3_XXS experts than slots of the largest blob would + // #369: not with --expert-cache-per-layer - its per-layer slot ranges ignore the profile rank a sized slot was cut + // for, so a layer's larger blob could land in a smaller slot: that mode keeps slots of the largest blob std::vector sized_slots; - if (native_pack && o.expert_cache > 0 && !profile.empty()) { + if (native_pack && o.expert_cache > 0 && !profile.empty() && !o.expert_cache_per_layer) { size_t free_b = 0, total_b = 0; cudaMemGetInfo(&free_b, &total_b); const auto& lay = strata::kernels::cpu::expert_layout(); @@ -2830,10 +2832,17 @@ int main(int argc, char** argv) { // the policy rather than a hint. int64_t prefilled = 0; if (!profile.empty() && srcp != nullptr) { - const int64_t want = std::min((int64_t) profile.size(), xcache.slots()); + // #369 (dag08): per layer, a full layer skips only its own pairs - each layer takes its hottest experts until + // its range is full (one full layer used to end the whole fill, leaving most layers empty) + const bool per_layer = xcache.per_layer_admission(); + const int64_t want = per_layer ? (int64_t) profile.size() + : std::min((int64_t) profile.size(), xcache.slots()); for (int64_t i = 0; i < want; ++i) { const int32_t slot = xcache.admit(profile[(size_t) i].first, profile[(size_t) i].second); - if (slot == strata::core::kNotResident) break; + if (slot == strata::core::kNotResident) { + if (per_layer) continue; + break; + } const uint8_t* b = srcp->blob(profile[(size_t) i].first, profile[(size_t) i].second); if (b == nullptr || !xcache.fill_slot_blocking(slot, b, err, (int64_t) strata::kernels::cpu::expert_layout().blob_bytes(profile[(size_t) i].first))) { @@ -2854,7 +2863,7 @@ int main(int argc, char** argv) { } mem_mark("the profile fill"); std::fprintf(stderr, "strata generate: pre-filled %lld of %lld slots from the profile; slot 0 verified\n", - (long long) prefilled, (long long) want); + (long long) prefilled, (long long) (per_layer ? xcache.slots() : want)); } for (auto& stp : stages) { diff --git a/tests/core/expert_cache_per_layer_test.cpp b/tests/core/expert_cache_per_layer_test.cpp new file mode 100644 index 00000000..26bfdd4d --- /dev/null +++ b/tests/core/expert_cache_per_layer_test.cpp @@ -0,0 +1,44 @@ +// #369: per-layer admission on a sized cache (open_sized, every native pack) - each layer admits into its own slot +// range, as on the uniform cache, so no two layers write the same slot. Needs a CUDA device (exits 77 without one). +#include "strata/core/expert_cache.hpp" + +#include + +#include +#include +#include + +int main() { + int n = 0; + if (cudaGetDeviceCount(&n) != cudaSuccess || n == 0) { + std::puts("no CUDA device: skipped"); + return 77; + } + std::string err; + for (int sized = 0; sized < 2; ++sized) { + strata::core::ExpertCache cache; + const bool ok = sized ? cache.open_sized(std::vector(6, 1024), 3, 4, err) + : cache.open(6, 3, 4, 1024, err); + if (!ok) { + std::fprintf(stderr, "open: %s\n", err.c_str()); + return 1; + } + cache.set_per_layer_admission(true); + for (int64_t layer = 0; layer < 3; ++layer) { + int64_t lo = 0, hi = 0; + cache.layer_slot_range(layer, lo, hi); + for (int64_t e = 0; e < 3; ++e) { + const int32_t slot = cache.admit(layer, e); + const bool want_room = lo + e < hi; + if (want_room ? slot != (int32_t) (lo + e) : slot != strata::core::kNotResident) { + std::fprintf(stderr, "%s cache: layer %lld expert %lld got slot %d (range %lld..%lld)\n", + sized ? "sized" : "uniform", (long long) layer, (long long) e, slot, (long long) lo, + (long long) hi - 1); + return 1; + } + } + } + } + std::puts("expert cache per-layer admission: uniform and sized caches passed"); + return 0; +}