From 3a4af428d5d9c53c59626bbe6f15d9f3d6b8c54a Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Mon, 23 Nov 2026 07:48:36 +0900 Subject: [PATCH 1/4] poly: Inkling 2's prompt scratch lent to the expert cache between prompts Inkling 2 sized every per-token buffer of its scratch for the prompt batch (rows_) and kept it in VRAM while an answer was written, though a decode call uses a few rows of it: 430 MiB at the default prompt batch of 1,024 on an RTX 2070 Super. It now has the engine's two scratch layouts (scratch_layouts(), use_scratch()), as Qwen3.6 does: a call of fewer than kStageRows tokens has a small layout of its own, and a prompt batch's is part of the prompt's buffers that lend_prompt() gives the expert cache between prompts. assign_scratch() sizes by buf_rows_. Not with the DSpark drafter, whose features are strided by a whole call's rows. Placement only: the calls and their arithmetic are the same, so a capped and frozen expert cache gives the same greedy ids and --top-probs 8 lines before and after (IQ4_NL 302, 1,000 and 2,048 tokens; AD-IQ3_S 302 and 2,048; RTX 2070 Super). What changes is the VRAM the expert cache has while an answer is written. The two layouts and the lending are VecSzn's; a small resident scratch on 8 GB cards was also merbanan's idea in his Strata inkling2-turing branch (a 512-row work chunk). Co-Authored-By: Claude Opus 5.5 --- engine/src/poly/inkling2.cpp | 8 ++++---- engine/src/poly/inkling2.hpp | 3 +++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/engine/src/poly/inkling2.cpp b/engine/src/poly/inkling2.cpp index e0a04ec81d..3381bd2319 100644 --- a/engine/src/poly/inkling2.cpp +++ b/engine/src/poly/inkling2.cpp @@ -580,9 +580,9 @@ void Inkling2::alloc_caches() { } } -// Per-token buffers hold rows_ tokens, row after row. +// Per-token buffers hold buf_rows_ tokens, row after row. void Inkling2::assign_scratch() { - const size_t n = (size_t) rows_, e = n * n_embd_, heads = n * n_head_ * head_dim_, kv = n * n_kv_ * head_dim_; + const size_t n = (size_t) buf_rows_, e = n * n_embd_, heads = n * n_head_ * head_dim_, kv = n * n_kv_ * head_dim_; for (float*& b : bl_) b = fbuf(e); bank_ = fbuf(e); bank_out_ = fbuf(e); @@ -598,7 +598,7 @@ void Inkling2::assign_scratch() { ch_ = fbuf(n * rank_); gain_ = fbuf(e); forget_ = fbuf(e); - const size_t few = (size_t) std::min(rows_, kStageRows); + const size_t few = (size_t) std::min(buf_rows_, kStageRows); for (int j = 0; j < 3; ++j) { cat3_[j] = fbuf(2 * few * n_embd_); ch3_[j] = fbuf(few * rank_); @@ -625,7 +625,7 @@ void Inkling2::assign_scratch() { score_ = fbuf(score_n_); zeros_ = fbuf((size_t) n_head_); xqo_ = fbuf((kernels::native_q8_1_bytes(n_head_ * head_dim_) + sizeof(float) - 1) / sizeof(float)); - if (rows_ >= kStageRows) awork_ = fbuf(heads + heads / 2 + 1 + 3 * n * n_head_); // a prompt batch's attention + if (buf_rows_ >= kStageRows) awork_ = fbuf(heads + heads / 2 + 1 + 3 * n * n_head_); // a prompt batch's attention if (ds_file_) { // the drafter's: a call's features and mixes, and a block's rows const size_t E = (size_t) n_embd_, hq = (size_t) n_head_ * head_dim_, kv = (size_t) n_kv_ * head_dim_, B = (size_t) ds_block_, V = (size_t) n_vocab_, R = (size_t) ds_rank_; diff --git a/engine/src/poly/inkling2.hpp b/engine/src/poly/inkling2.hpp index 0be39f2abd..cd90ac2bd9 100644 --- a/engine/src/poly/inkling2.hpp +++ b/engine/src/poly/inkling2.hpp @@ -38,6 +38,9 @@ class Inkling2 final : public Engine { void load_extra(const uint8_t* src, int64_t pos) override; void alloc_caches() override; void assign_scratch() override; + // A call of a few tokens keeps a scratch of its own, and a prompt batch's is lent to the expert cache between + // prompts (Engine::use_scratch()). Not with the drafter, whose features are a whole call's rows. + bool scratch_layouts() const override { return !ds_file_; } int widest() const override; int streams() const override { return 1; } void layer_head(Layer& L, int il, int n) override; From bea895b99e38bf7fd98c4661575f691eab64a2b2 Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Mon, 23 Nov 2026 09:14:42 +0900 Subject: [PATCH 2/4] poly: Inkling 2's scratch in one layout on ROCm (Linux) On ROCm on Linux, VRAM goes back only with its addresses (vmm_unmap() frees them and reserves them again). Lending a prompt's buffers to the expert cache crashed strata-poly there: Inkling 2 Mini IQ4_NL on an RX 9070 XT with some experts in RAM, SIGSEGV inside hipLaunchKernel. Where STRATA_VMM_RELEASE_WITH_ADDRESSES is defined (HIP on Linux), Inkling2::scratch_layouts() is now false, so the prompt batch's scratch is not put into those lent buffers and stays in one layout, as before the previous commit. The override moves to inkling2.cpp, which includes (on HIP, hip_compat's), where the macro is defined. CUDA and Windows HIP are unchanged. Class: placement only. Co-Authored-By: Claude Opus 5.5 --- engine/src/poly/inkling2.cpp | 13 +++++++++++++ engine/src/poly/inkling2.hpp | 5 +++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/engine/src/poly/inkling2.cpp b/engine/src/poly/inkling2.cpp index 3381bd2319..489a6e0152 100644 --- a/engine/src/poly/inkling2.cpp +++ b/engine/src/poly/inkling2.cpp @@ -9,6 +9,8 @@ #include "strata/kernels/iq_kernels.hpp" #endif +#include + #include #include #include @@ -557,6 +559,17 @@ void Inkling2::load_layer(int il) { size_t Inkling2::recurrent_floats() const { return slots_.size() * past_floats(); } +// ROCm on Linux gives VRAM back only with its addresses (Engine::vmm_unmap()), and a prompt's lent buffers crashed +// strata-poly there (an RX 9070 XT, Inkling 2 Mini IQ4_NL with some experts in RAM): the prompt batch's scratch stays +// out of them, in one layout as before. +bool Inkling2::scratch_layouts() const { +#ifdef STRATA_VMM_RELEASE_WITH_ADDRESSES + return false; +#else + return !ds_file_; +#endif +} + int Inkling2::widest() const { return std::max(n_head_ * head_dim_, 2 * n_embd_); } // Every application of a layer has caches of its own: a full layer's keep a row a position, a window layer's a diff --git a/engine/src/poly/inkling2.hpp b/engine/src/poly/inkling2.hpp index cd90ac2bd9..6475ff39ce 100644 --- a/engine/src/poly/inkling2.hpp +++ b/engine/src/poly/inkling2.hpp @@ -39,8 +39,9 @@ class Inkling2 final : public Engine { void alloc_caches() override; void assign_scratch() override; // A call of a few tokens keeps a scratch of its own, and a prompt batch's is lent to the expert cache between - // prompts (Engine::use_scratch()). Not with the drafter, whose features are a whole call's rows. - bool scratch_layouts() const override { return !ds_file_; } + // prompts (Engine::use_scratch()). Not with the drafter, whose features are a whole call's rows, nor on ROCm on + // Linux (inkling2.cpp). + bool scratch_layouts() const override; int widest() const override; int streams() const override { return 1; } void layer_head(Layer& L, int il, int n) override; From 9a76cb08604ff1aa0ea2e7ce9f9c36eb2cd19b5f Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Mon, 23 Nov 2026 11:02:44 +0900 Subject: [PATCH 3/4] poly: Inkling 2's attention scores shared by both scratch layouts The 64 MiB of attention scores (score_, sized for the context) were in each of the two scratch layouts, so the few tokens' layout cost about 70 MiB beside the prompt batch's: 15-22 fewer experts in VRAM at load on an 8 GB card. They are now one buffer apart from the scratch that both layouts share, as VecSzn's Qwen3.6 already does (qwen35.cpp, alloc_caches()). Their size is the same, so the attention's chunks and the output bits are too. The layouts are also off where nothing is ever lent (lend_prompt() returns early): with the experts frozen (STRATA_FREEZE_EXPERTS, --check-batch), and where the free VRAM at load holds every expert, the batch's scratch and 2 GiB more. There the VRAM is main's. Co-Authored-By: Claude Opus 5.5 --- engine/src/poly/inkling2.cpp | 33 +++++++++++++++++++++++++++++---- engine/src/poly/inkling2.hpp | 5 ++++- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/engine/src/poly/inkling2.cpp b/engine/src/poly/inkling2.cpp index 489a6e0152..287008b6ee 100644 --- a/engine/src/poly/inkling2.cpp +++ b/engine/src/poly/inkling2.cpp @@ -561,15 +561,35 @@ size_t Inkling2::recurrent_floats() const { return slots_.size() * past_floats() // ROCm on Linux gives VRAM back only with its addresses (Engine::vmm_unmap()), and a prompt's lent buffers crashed // strata-poly there (an RX 9070 XT, Inkling 2 Mini IQ4_NL with some experts in RAM): the prompt batch's scratch stays -// out of them, in one layout as before. -bool Inkling2::scratch_layouts() const { +// out of them, in one layout as before. With the experts frozen (STRATA_FREEZE_EXPERTS, --check-batch) lend_prompt() +// lends nothing, and the small layout would only take VRAM from the expert cache for the whole run. +bool Inkling2::scores_apart() const { #ifdef STRATA_VMM_RELEASE_WITH_ADDRESSES return false; #else - return !ds_file_; + return !ds_file_ && !freeze_experts_asked_ && mmq_ && vmm_ && !split_ && rows_ >= kStageRows; #endif } +// A card that holds every expert lends nothing either (lend_prompt() with all_cached()). The cache is not sized yet +// when the engine asks, so this is from the free VRAM: every expert's bytes, the prompt batch's scratch (scratch_at_, +// just counted) and 2 GiB for the reserve, the prompt's other buffers and the staging buffer. Where it is wrong the +// card either has the small layout it does not use (a few MiB) or no layouts, as main. +bool Inkling2::every_expert_fits() const { + size_t all = 0; + for (const Layer& L : layers_) { + if (!L.moe) continue; + if (!kernels::native_mmvq_supported((int) L.gu_type) || !kernels::native_mmvq_supported((int) L.u_type) || + !kernels::native_mmvq_supported((int) L.d_type)) + return false; // fill_expert_cache() keeps such a layer's experts in RAM + all += (size_t) n_expert_ * (L.gu2() + L.d_bytes); + } + const size_t need = all + scratch_at_ * sizeof(float) + (size_t(2) << 30); + return (size_t) vram_free_mib() << 20 >= need; +} + +bool Inkling2::scratch_layouts() const { return scores_apart() && !every_expert_fits(); } + int Inkling2::widest() const { return std::max(n_head_ * head_dim_, 2 * n_embd_); } // Every application of a layer has caches of its own: a full layer's keep a row a position, a window layer's a @@ -635,7 +655,12 @@ void Inkling2::assign_scratch() { fc_ = fbuf(e); // a token's scores of the whole context, and what ink_attend() keeps beside them score_n_ = std::max(kScoreFloats, ink_attend_floats(n_head_, n_kv_, head_dim_, opt_.max_context)); - score_ = fbuf(score_n_); + if (scores_apart()) { // sized for the context, not for a call's tokens: 64 MiB that both layouts share + if (!scores_) scores_ = (float*) dmalloc(score_n_ * sizeof(float), "the attention's scores"); + score_ = scores_; + } else { + score_ = fbuf(score_n_); + } zeros_ = fbuf((size_t) n_head_); xqo_ = fbuf((kernels::native_q8_1_bytes(n_head_ * head_dim_) + sizeof(float) - 1) / sizeof(float)); if (buf_rows_ >= kStageRows) awork_ = fbuf(heads + heads / 2 + 1 + 3 * n * n_head_); // a prompt batch's attention diff --git a/engine/src/poly/inkling2.hpp b/engine/src/poly/inkling2.hpp index 6475ff39ce..c2f96fcc77 100644 --- a/engine/src/poly/inkling2.hpp +++ b/engine/src/poly/inkling2.hpp @@ -40,8 +40,10 @@ class Inkling2 final : public Engine { void assign_scratch() override; // A call of a few tokens keeps a scratch of its own, and a prompt batch's is lent to the expert cache between // prompts (Engine::use_scratch()). Not with the drafter, whose features are a whole call's rows, nor on ROCm on - // Linux (inkling2.cpp). + // Linux, nor where nothing would be lent: the experts frozen, or every one of them in VRAM (inkling2.cpp). bool scratch_layouts() const override; + bool scores_apart() const; ///< the attention's scores outside the scratch, for its two layouts to share + bool every_expert_fits() const; int widest() const override; int streams() const override { return 1; } void layer_head(Layer& L, int il, int n) override; @@ -219,6 +221,7 @@ class Inkling2 final : public Engine { *sink_ = nullptr, *gate_ = nullptr, *kdyn_ = nullptr, *vdyn_ = nullptr, *att_ = nullptr, *aoraw_ = nullptr, *fc_ = nullptr, *score_ = nullptr, *zeros_ = nullptr, *awork_ = nullptr; size_t score_n_ = 0; + float* scores_ = nullptr; ///< score_ where it is apart from the scratch (scores_apart()) }; } // namespace strata::poly From 869a4535f2ff03e4db6f2960bd846c7a109fff01 Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Mon, 23 Nov 2026 11:02:50 +0900 Subject: [PATCH 4/4] poly: no attention work buffer in Inkling 2's few-token scratch layout In the few tokens' layout awork_ kept the prompt batch layout's address, which is unmapped while that layout is lent. Nothing reads it there (only calls of 32 rows or more, which take the batch layout first), but a null pointer makes any later misuse fault plainly. Co-Authored-By: Claude Opus 5.5 --- engine/src/poly/inkling2.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/engine/src/poly/inkling2.cpp b/engine/src/poly/inkling2.cpp index 287008b6ee..6a4dbe107a 100644 --- a/engine/src/poly/inkling2.cpp +++ b/engine/src/poly/inkling2.cpp @@ -663,7 +663,8 @@ void Inkling2::assign_scratch() { } zeros_ = fbuf((size_t) n_head_); xqo_ = fbuf((kernels::native_q8_1_bytes(n_head_ * head_dim_) + sizeof(float) - 1) / sizeof(float)); - if (buf_rows_ >= kStageRows) awork_ = fbuf(heads + heads / 2 + 1 + 3 * n * n_head_); // a prompt batch's attention + // a prompt batch's attention; none in the few tokens' layout, where the batch layout's may be lent + awork_ = buf_rows_ >= kStageRows ? fbuf(heads + heads / 2 + 1 + 3 * n * n_head_) : nullptr; if (ds_file_) { // the drafter's: a call's features and mixes, and a block's rows const size_t E = (size_t) n_embd_, hq = (size_t) n_head_ * head_dim_, kv = (size_t) n_kv_ * head_dim_, B = (size_t) ds_block_, V = (size_t) n_vocab_, R = (size_t) ds_rank_;