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++

CS-T: the partial pin locks only what it does not register and ends at an expert boundary; the Q4_K/Q5_1/Q8_0 multi-token kernels opt-in (STRATA_KQ256=1)

Niko12217d8fc8fd044b
diff --git a/include/strata/core/expert_source.hpp b/include/strata/core/expert_source.hpp
index 485733b20..a604fbb05 100644
--- a/include/strata/core/expert_source.hpp
+++ b/include/strata/core/expert_source.hpp
@@ -496,6 +496,7 @@ private:
bool complement_pinned_ = false;
bool complement_partial_ = false; ///< CS-T: only the first complement_pin_limit_ bytes are registered
uint64_t complement_pin_limit_ = 0;
+ uint64_t complement_lock_off_ = 0; ///< the working-set lock covers [lock_off, lock_off + locked)
bool complement_ready_ = false;
uint64_t complement_locked_ = 0; ///< bytes held in the working set (pin refused)
int64_t complement_lent_slots_ = 0;
diff --git a/src/core/expert_source.cpp b/src/core/expert_source.cpp
index dbb60fd14..e565b854b 100644
--- a/src/core/expert_source.cpp
+++ b/src/core/expert_source.cpp
@@ -451,7 +451,8 @@ void FileExpertSource::close() {
if (complement_pinned_ && !complement_partial_) (void) cudaFreeHost(complement_arena_);
else {
if (complement_partial_) (void) cudaHostUnregister(complement_arena_);
- if (complement_locked_ > 0) strata::platform::unlock_resident(complement_arena_, complement_locked_);
+ if (complement_locked_ > 0)
+ strata::platform::unlock_resident((uint8_t*) complement_arena_ + complement_lock_off_, complement_locked_);
std::free(complement_arena_);
}
}
@@ -475,6 +476,7 @@ void FileExpertSource::close() {
complement_pinned_ = false;
complement_partial_ = false;
complement_pin_limit_ = 0;
+ complement_lock_off_ = 0;
complement_ready_ = false;
complement_locked_ = 0;
complement_lent_slots_ = 0;
@@ -964,13 +966,14 @@ bool FileExpertSource::pin_cache_complement(
bool pinned_ok = false;
uint64_t locked = 0;
uint64_t partial_pin = 0; ///< CS-T: a registered prefix of a locked arena
+ uint64_t lock_off = 0; ///< where the working-set lock starts (after the registered prefix)
std::string note;
auto release = [&]() {
if (arena == nullptr) return;
if (pinned_ok) (void) cudaFreeHost(arena);
else {
if (partial_pin > 0) (void) cudaHostUnregister(arena);
- if (locked > 0) strata::platform::unlock_resident(arena, locked);
+ if (locked > 0) strata::platform::unlock_resident((uint8_t*) arena + lock_off, locked);
std::free(arena);
}
arena = nullptr;
@@ -1010,12 +1013,11 @@ bool FileExpertSource::pin_cache_complement(
return false;
}
if (pin) {
- const strata::platform::LockResult lr = strata::platform::lock_resident(arena, bytes);
- locked = lr.locked_bytes;
- note += (note.empty() ? "" : "; ") + lr.note;
// CS-T, a RAM budget: its bytes are in profile order, hottest first, so the driver is asked to
- // register the largest prefix it takes (from the whole, down in 2 GiB steps). Those experts can be
- // read by the GPU over PCIe (--pcie-frac) and copied by DMA; the rest stay locked-only.
+ // register the largest prefix it takes (from the cap down in 2 GiB steps). Those experts can be
+ // read by the GPU over PCIe (--pcie-frac) and copied by DMA; only the rest is locked in the working
+ // set (the registered prefix is page-locked by the driver already - locking it twice made the next
+ // device allocation fail).
static const bool partial_on = [] {
const char* v = std::getenv("STRATA_PARTIAL_PIN"); // 0: the A/B arm without it
return v == nullptr || std::atoi(v) != 0;
@@ -1029,7 +1031,15 @@ bool FileExpertSource::pin_cache_complement(
if (budget_bytes > 0 && partial_on) {
const uint64_t step = 2ull << 30;
for (uint64_t want = std::min(bytes, pin_cap); want >= step; want = want > step ? want - step : 0) {
- const uint64_t w = want - want % (64ull << 10);
+ // cut at an expert boundary: a blob that started inside the registered range and ran past it
+ // would be taken as page-locked by a cudaMemcpyAsync and refused ("adaptive refill failed")
+ uint64_t w = want;
+ for (size_t i = 0; i < offsets.size(); ++i) {
+ if (offsets[i] == kNoComplement) continue;
+ const uint64_t b = layer_blob_bytes_[i / (size_t) n_expert_];
+ if (offsets[i] < want && offsets[i] + b > want) { w = offsets[i]; break; }
+ }
+ if (w == 0) break;
if (cudaHostRegister(arena, (size_t) w, cudaHostRegisterMapped | cudaHostRegisterPortable) ==
cudaSuccess) {
void* alias = nullptr;
@@ -1050,6 +1060,11 @@ bool FileExpertSource::pin_cache_complement(
(double) partial_pin / 1073741824.0);
note += std::string("; ") + msg;
}
+ lock_off = partial_pin;
+ const strata::platform::LockResult lr =
+ strata::platform::lock_resident((uint8_t*) arena + lock_off, bytes - lock_off);
+ locked = lr.locked_bytes;
+ note += (note.empty() ? "" : "; ") + lr.note;
}
}
host = (const uint8_t*) arena;
@@ -1146,6 +1161,7 @@ bool FileExpertSource::pin_cache_complement(
complement_pin_limit_ = pinned_ok ? bytes : partial_pin;
complement_partial_ = !pinned_ok && partial_pin > 0;
complement_locked_ = locked;
+ complement_lock_off_ = lock_off;
complement_lent_slots_ = lend ? n_slots - keep_from : 0;
complement_ready_ = true;
std::fprintf(stderr, "FileExpertSource: %s cache complement ready: resident %.2f GiB, pinned %.2f GiB%s%s\n",
diff --git a/src/kernels/cpu/native_expert.cpp b/src/kernels/cpu/native_expert.cpp
index 2ae3947bc..2e8d8adb2 100644
--- a/src/kernels/cpu/native_expert.cpp
+++ b/src/kernels/cpu/native_expert.cpp
@@ -90,9 +90,10 @@ void native_gu_rows(const NativeFmt& f, const uint8_t* blob, const void* const*
// can depend on how many drafts a verify window held. STRATA_IQ_MT_MIN=1 (opt-in, 0.1.30) uses the multi-token
// kernels for every group: output independent of the drafting, at a measured -1..-3% decode on IQ3_S (AVX-512).
static const int mt_min = [] { const char* e = std::getenv("STRATA_IQ_MT_MIN"); return e ? std::atoi(e) : 2; }();
- // Unsloth UD-Q4_K_XL's Q4_K gate/up: the multi-token kernel is bit-exact against ggml's per-token dot, so it
- // runs for every group size (no #152 rule). STRATA_NO_KQ256 falls back to ggml's vec_dot.
- static const bool kq = std::getenv("STRATA_NO_KQ256") == nullptr;
+ // Unsloth UD-Q4_K_XL's Q4_K gate/up: the multi-token kernel is bit-exact against ggml's per-token dot (any group
+ // size, no #152 rule). Opt-in, STRATA_KQ256=1: measured no faster in the engine (a window's expert groups hold
+ // ~1.4 tokens and the weights stay in L1 across ggml's per-token calls; 1.01-1.13x in native_expert_parity).
+ static const bool kq = [] { const char* v = std::getenv("STRATA_KQ256"); return v != nullptr && std::atoi(v) != 0; }();
if (kq && f.gu_type == 12 && nt >= 2) { // one token: ggml's own dot below (the same bits, less overhead)
kq256_gu_rows(f.gu_type, blob, f.gu_row, f.up_off, (int) f.n_embd, act, nt, ff, r0, r1);
return;
@@ -127,7 +128,7 @@ void native_down_rows(const NativeFmt& f, const uint8_t* blob, const void* const
// block instead of once per token; ggml-cpu's dot is single-token. STRATA_NO_IQ4NL falls back to it.
static const bool iq4nl_mt = std::getenv("STRATA_NO_IQ4NL") == nullptr;
static const int mt_min = [] { const char* e = std::getenv("STRATA_IQ_MT_MIN"); return e ? std::atoi(e) : 2; }();
- static const bool kq = std::getenv("STRATA_NO_KQ256") == nullptr;
+ static const bool kq = [] { const char* v = std::getenv("STRATA_KQ256"); return v != nullptr && std::atoi(v) != 0; }();
if (kq && nt >= 2 && (f.d_type == 7 || f.d_type == 8)) { // Q5_1 / Q8_0 down: bit-exact, any group size
kq256_rows(f.d_type, blob + f.down_off, f.d_row, (int) f.n_ff, hq, nt, out, r0, r1);
return;
diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp
index 28981d12b..7c36257ce 100644
--- a/src/prefill/prefill.cpp
+++ b/src/prefill/prefill.cpp
@@ -496,8 +496,8 @@ bool Prefill::init(const core::WeightTable& wt, const core::ModelGeometry& g, co
return false;
}
if (m.tok_dev == nullptr) {
- if (cudaMalloc((void**) &m.tok_dev, (size_t) chunk * sizeof(int32_t)) != cudaSuccess) {
- err = "prefill: the token id buffer";
+ if (const cudaError_t e = cudaMalloc((void**) &m.tok_dev, (size_t) chunk * sizeof(int32_t)); e != cudaSuccess) {
+ err = std::string("prefill: the token id buffer (") + cudaGetErrorString(e) + ")";
return false;
}
m.owned.push_back(m.tok_dev);