From e979e9529274b63cb62300444431036b2d889174 Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Wed, 25 Nov 2026 01:33:00 +0900 Subject: [PATCH 1/2] poly: size the expert staging buffer to the experts a call copies on ROCm on Linux Same bits for a given cached set (placement: more experts cached). ROCm on Linux lends nothing back to the expert cache (vmm_unmap() frees VRAM only with its addresses), so the staging buffer was a whole layer for the whole run: 1,033 MiB for Inkling 2 Mini IQ4_NL, whose Q8_0 layers have 128 experts of 8.1 MiB. Once the cache is filled, load() now cuts it to the most a layer's call copies through it (the experts the layer lacks, plus a margin for what the attention caches may still take from the cache), with room for vram_ahead()'s groups of 4 slots so the router launches stay as they were. The freed VRAM goes to the expert cache. A cache that later loses more than the margin copies a layer in rounds; on RDNA4's BLAS loop that changes the last bits of those calls. Inkling 2 only (trims_stage()); CUDA, Metal and Windows HIP are unchanged. STRATA_STAGE_TRIM=0 keeps the whole layer. Co-Authored-By: Claude Opus 5.5 --- engine/src/poly/engine.cpp | 88 +++++++++++++++++++++++++++++++++++- engine/src/poly/engine.hpp | 6 +++ engine/src/poly/inkling2.hpp | 1 + 3 files changed, 94 insertions(+), 1 deletion(-) diff --git a/engine/src/poly/engine.cpp b/engine/src/poly/engine.cpp index 35005b37e..399baa0d3 100644 --- a/engine/src/poly/engine.cpp +++ b/engine/src/poly/engine.cpp @@ -2535,7 +2535,14 @@ void Engine::ensure_stage() { #if defined(STRATA_GPU_METAL) if (opt_.metal_balanced && all_cached()) return; #endif - if (stage_wide_ > stage_narrow_) { + if (stage_trims_) { + // whole pieces of kArenaPiece, all of one size: native Fedora's ROCm 7.2.4 (an RX 9070 XT) refused the 8 MiB + // piece after the 64 MiB ones ("no VRAM"), as WSL's does (STRATA_VMM_EQUAL_PIECES) + stage_full_ = stage_narrow_; + vmm_reserve(stage_map_, up_to(stage_narrow_ + 256 + kTail, kArenaPiece)); + vmm_map(stage_map_, stage_map_.reserved, kArenaPiece, "the expert staging buffer"); + stage_ = (uint8_t*) stage_map_.base; + } else if (stage_wide_ > stage_narrow_) { vmm_reserve(stage_map_, stage_wide_ + 256 + kTail); // with the pool the buffer comes out of it, and out of the expert cache when the pool lacks the room if (pool_on_ && pool_claimed_ && exp_) { @@ -2626,7 +2633,13 @@ void Engine::resize_card_cache(int mib) { metal_cache_bytes_ = 0; #endif opt_.vram_reserve_mib = mib < 0 ? asked_reserve_mib_ : mib; + if (stage_trims_ && stage_ && stage_full_ > stage_bytes_) { // the whole layer again, cut for the new cache below + if (up_) ck(cudaStreamSynchronize(cs(up_)), "resize"); + vmm_map(stage_map_, stage_map_.reserved, kArenaPiece, "the expert staging buffer"); + stage_bytes_ = stage_narrow_ = stage_wide_ = stage_full_; + } fill_expert_cache(); + trim_stage(); ensure_stage(); start_swaps(); } @@ -2783,6 +2796,68 @@ void Engine::narrow_stage() { stage_cut_ = 0; } +// ROCm on Linux lends nothing (vmm_unmap() gives VRAM back only with its addresses), so the staging buffer is a +// whole layer for the run: 1,033 MiB for Inkling 2 Mini IQ4_NL, whose last four layers are Q8_0 (128 experts of +// 8.1 MiB). A call copies through it only the experts the cache lacks, so once the cache is filled the buffer is +// cut to the most of those a layer has, with a margin for the experts the attention caches may still take from +// the cache as they grow (spread over the layers by bytes) plus kStageSpare, and the VRAM goes to the expert cache. +// It keeps room for vram_ahead()'s groups of 4 slots, so that ahead_slots_ and the router launches that go with it +// stay as they were (HIP builds no mirror, so nothing is uploaded into them). A call that lacks more experts than +// the buffer holds copies them in rounds, as a call of a layer larger than the buffer always did. Where a row's +// product depends on the launch's rows (Mmq::split_exact() false: RDNA4's default BLAS loop, CDNA's stream-k) its last +// bits then differ from one round's; the margin keeps that to a cache that lost more than the attention caches take, +// which a frozen cache never does. STRATA_STAGE_TRIM=0 keeps the whole layer. +void Engine::trim_stage() { +#if defined(STRATA_VMM_RELEASE_WITH_ADDRESSES) && !defined(STRATA_GPU_METAL) + constexpr int kStageSpare = 4, kAheadSlots = 4; + if (!stage_trims_ || !mmq_ || !stage_ || !exp_ || all_cached()) return; + // what the cache may still give up: the attention caches still to map, and the reserve that mind_vram() keeps + // free once the recorded launches have taken theirs + size_t kv_left = size_t(std::max(reserve_mib_, 0)) << 20; + for (const Kv& k : kv_) kv_left += k.reserved - k.mapped; + for (const Seq& q : seqs_) + for (const Kv& k : q.kv) kv_left += k.reserved - k.mapped; + size_t slots = 0; // a slot of each MoE layer + for (const Layer& L : layers_) + if (L.moe) slots += L.slot_bytes ? L.slot_bytes : L.gu_step + L.d_bytes; + const int spare = kStageSpare + (slots ? (int) ((kv_left + slots - 1) / slots) : 0); + size_t miss = 0; + for (const Layer& L : layers_) { + if (!L.moe) continue; + int lacks = 0; + for (int e = 0; e < n_expert_; ++e) lacks += !L.slot[(size_t) e]; + miss = std::max(miss, (size_t) std::min(lacks + spare, n_expert_) * (L.gu_step + L.d_bytes)); + } + const size_t stride = (slot_max_ + 4095) / 4096 * 4096; // ahead_stride_, set later in load() + // vram_ahead()'s groups, and card_rows() slots so that far_plan()'s buffer still fits (STRATA_POLY_CARD_FAR) + const size_t floor = (size_t) std::max((kAheadGroups + 1) * kAheadSlots, card_rows()) * stride; + const size_t want = std::min(stage_bytes_, std::max(miss, floor)); + const size_t piece = stage_map_.pieces.empty() ? kArenaPiece : stage_map_.pieces.front().second; + const size_t had = stage_map_.mapped, now = up_to(want + 256 + kTail, piece); + if (now + kArenaPiece > had) { // less than a piece of the cache to gain + std::fprintf(stderr, "strata-poly: the expert staging buffer stays at %zu MiB: a call may copy %zu MiB " + "(%d experts spare a layer for %zu MiB the cache may still give up)\n", + stage_bytes_ >> 20, want >> 20, spare, kv_left >> 20); + return; + } + if (up_) ck(cudaStreamSynchronize(cs(up_)), "the expert staging buffer"); + ck(cudaStreamSynchronize(cs(st_)), "the expert staging buffer"); + vmm_unmap(stage_map_, want + 256 + kTail); // whole pieces: what it keeps covers `want` + const size_t was = stage_bytes_, freed = had - stage_map_.mapped; + stage_bytes_ = stage_narrow_ = stage_wide_ = std::min(was, stage_map_.mapped - 256 - kTail); // whole pieces' worth + staged_il_ = -1; + const int before = cached_; + // STRATA_GPU_EXPERTS holds the cache to its N: the buffer is cut all the same, and the VRAM stays free. A freeze + // (STRATA_FREEZE_EXPERTS) holds the cache still from the first request on, not during the load. + const bool frozen = std::exchange(freeze_experts_, false); + const int took = expert_cap_ < 0 ? grow_cache(freed) : 0; + freeze_experts_ = frozen; + std::fprintf(stderr, "strata-poly: the expert staging buffer %zu -> %zu MiB (the most a layer's call copies); the " + "expert cache %d -> %d experts\n", + was >> 20, stage_bytes_ >> 20, before, before + took); +#endif +} + // The cache's VRAM and the attention caches' as they are now become the budget cover() keeps them to. void Engine::held_is_budget() { budget_ = arena_.mapped + other_rows(); @@ -3454,6 +3529,7 @@ void Engine::load(const Options& o) { #if !defined(STRATA_GPU_METAL) fill_expert_card(); #endif + trim_stage(); ensure_stage(); if (mmq_ && !all_cached()) stage_pins(); // The weights and the expert cache are written on st_ now (h2d(), zero_vram()): the load is not over until @@ -4067,6 +4143,16 @@ void Engine::alloc() { stage_narrow_ = stage_wide_ = stage_bytes_; if (widens && read_mib > 0) stage_wide_ = std::max(stage_bytes_, std::min((size_t) read_mib << 20, (size_t) n_expert_ * one_expert)); +#if defined(STRATA_VMM_RELEASE_WITH_ADDRESSES) && !defined(STRATA_GPU_METAL) + // trim_stage(): mapped in pieces, which it gives back once the cache is filled. Not a cudaMalloc it frees and + // makes again smaller: on ROCm 7.2.4 (an RX 9070 XT) the expert cache's addresses then could not be reserved + // again where they were the next time it gave VRAM back (vmm_unmap()), and the engine stopped. + { + const char* trim = std::getenv("STRATA_STAGE_TRIM"); + stage_trims_ = trims_stage() && opt_.stage_mib <= 0 && widens && !pool_on_ && stage_wide_ == stage_narrow_ && + !(trim && trim[0] == '0'); + } +#endif #if defined(STRATA_GPU_METAL) if (!opt_.metal_balanced) #endif diff --git a/engine/src/poly/engine.hpp b/engine/src/poly/engine.hpp index ab0e27454..c52ff435d 100644 --- a/engine/src/poly/engine.hpp +++ b/engine/src/poly/engine.hpp @@ -531,6 +531,9 @@ class Engine { /// Whether read() may take a prompt's calls through a layer together before the next layer. Not where a call's /// layers keep rows of their own between them, or do not run in order. virtual bool reads_by_layer() const { return true; } + /// Where the staging buffer cannot be lent (ROCm on Linux), whether load() may size it to what the filled cache + /// leaves out of a layer (trim_stage()) in place of a whole layer. + virtual bool trims_stage() const { return false; } /// Rows a token keeps of its own from one layer run to the next beside its rows of r_ (Inkling 2's residual /// streams). Where there are any, read() takes a group of calls through each layer run, in the order next_layer() /// gives, with the group's rows and these on the card (read_groups()). carry_at() points the architecture's @@ -845,6 +848,7 @@ class Engine { void fill_now(); void widen_stage(); void narrow_stage(); + void trim_stage(); void resize_card_cache(int mib); ///< resize_cache() and mind_vram() for this engine's card bool mind_card_vram(); size_t trim_card_kv(int64_t end); @@ -1221,6 +1225,8 @@ class Engine { Mapped stage_map_; ///< stage_'s VRAM when it can grow for a read (stage_read_mib) size_t stage_narrow_ = 0, stage_wide_ = 0; ///< stage_bytes_ outside a read and inside one bool stage_lent_ = false; ///< the expert cache has lent the buffer VRAM (widen_stage()) + bool stage_trims_ = false; ///< stage_map_ in pieces that trim_stage() cuts after the fill + size_t stage_full_ = 0; ///< its size before the cut, mapped again for resize_cache() size_t stage_cut_ = 0; ///< what the cache gave up for it, which it takes back int stage_turn_ = 0; std::vector ids_, count_, first_, group_, round_, round_row_; diff --git a/engine/src/poly/inkling2.hpp b/engine/src/poly/inkling2.hpp index 9b3b44293..1ff6bbeb9 100644 --- a/engine/src/poly/inkling2.hpp +++ b/engine/src/poly/inkling2.hpp @@ -64,6 +64,7 @@ class Inkling2 final : public Engine { bool batch_heads() const override { return true; } bool tail_by_rows() const override { return true; } bool reads_by_layer() const override { return false; } + bool trims_stage() const override { return true; } // A call's residual streams and the core's bank from one layer run to the next: read() takes the calls through // the runs a group at a time. Not with the drafter, whose taps of three runs are a call's too. int carry_rows() const override { return ds_file_ ? 0 : kBlocks + 2; } From 5ac5aa61499e31606b8ebd991a41bfdb43b49bc4 Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Wed, 25 Nov 2026 12:45:21 +0900 Subject: [PATCH 2/2] poly: STRATA_POLY_STAGE_FIT=0 keeps the staging buffer whole (not STRATA_STAGE_TRIM) STRATA_STAGE_TRIM is the program engine's split-stack weight trim; strata-poly no longer reads it, so setting it for a split config cannot change poly's staging buffer. The new STRATA_POLY_STAGE_FIT=0 keeps the whole layer, as STRATA_STAGE_TRIM=0 did, and is registered. Co-Authored-By: Claude Sonnet 5.5 --- engine/src/poly/engine.cpp | 4 ++-- terminal/data/settings-register.json | 33 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/engine/src/poly/engine.cpp b/engine/src/poly/engine.cpp index 399baa0d3..605bfdce4 100644 --- a/engine/src/poly/engine.cpp +++ b/engine/src/poly/engine.cpp @@ -2806,7 +2806,7 @@ void Engine::narrow_stage() { // the buffer holds copies them in rounds, as a call of a layer larger than the buffer always did. Where a row's // product depends on the launch's rows (Mmq::split_exact() false: RDNA4's default BLAS loop, CDNA's stream-k) its last // bits then differ from one round's; the margin keeps that to a cache that lost more than the attention caches take, -// which a frozen cache never does. STRATA_STAGE_TRIM=0 keeps the whole layer. +// which a frozen cache never does. STRATA_POLY_STAGE_FIT=0 keeps the whole layer. void Engine::trim_stage() { #if defined(STRATA_VMM_RELEASE_WITH_ADDRESSES) && !defined(STRATA_GPU_METAL) constexpr int kStageSpare = 4, kAheadSlots = 4; @@ -4148,7 +4148,7 @@ void Engine::alloc() { // makes again smaller: on ROCm 7.2.4 (an RX 9070 XT) the expert cache's addresses then could not be reserved // again where they were the next time it gave VRAM back (vmm_unmap()), and the engine stopped. { - const char* trim = std::getenv("STRATA_STAGE_TRIM"); + const char* trim = std::getenv("STRATA_POLY_STAGE_FIT"); stage_trims_ = trims_stage() && opt_.stage_mib <= 0 && widens && !pool_on_ && stage_wide_ == stage_narrow_ && !(trim && trim[0] == '0'); } diff --git a/terminal/data/settings-register.json b/terminal/data/settings-register.json index 1b115a170..9f2b3b994 100644 --- a/terminal/data/settings-register.json +++ b/terminal/data/settings-register.json @@ -34431,6 +34431,39 @@ "retire_after": "", "retired_in": "" }, + { + "name": "STRATA_POLY_STAGE_FIT", + "backends": [ + "cuda-hip" + ], + "reads": [ + { + "file": "engine/src/poly/engine.cpp", + "line": 4151, + "backend": "cuda-hip", + "fn": "getenv" + } + ], + "parse": "flag", + "default_state": "on", + "code_default": null, + "engine_sets": [], + "engine_defaults": [], + "launch_files": [], + "launch_sets": [], + "launch_reads": [], + "doc_mentions": [], + "first_commit": null, + "first_date": null, + "since": null, + "last_commit": null, + "last_date": null, + "status": "rollback", + "owner": "", + "note": "HIP on Linux (VMM builds): 0 keeps strata-poly's expert staging buffer at a whole layer for the run, as before it was cut to the most experts a call copies once the expert cache is filled (Engine::trim_stage()). Not STRATA_STAGE_TRIM, which is the program engine's split-stack weight trim.", + "retire_after": "", + "retired_in": "" + }, { "name": "STRATA_POLY_STEP_GRAPHS", "backends": [