From c3f6978b10d434fed2beeae34bee1b2208aebcf0 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Sun, 15 Nov 2026 03:12:43 +0100 Subject: [PATCH] Integrated gfx11 under Linux: the prompt path can keep its own buffers instead of lending cache slots (off by default) What the box showed (p2006, Radeon 780M gfx1103, 11-15): with the arena unregistered, every short prompt took 1.1-1.4 s more (512 tokens 3,517 against 2,620 ms; 84-256 tokens about 2,850-3,050 against 1,500-1,920 ms). That time is the copy-back of the 1,967 cache slots (2.53 GiB) the prompt path borrows, from pageable memory, after every prompt. Without a loan there is no copy-back. This extends the Strix Halo rule of 0.2.8 (Windows gfx1151: no loan) to this class, with two differences. The decision is made after the expert cache is sized as with a loan, so the cache gets the same slots it gets today (--expert-cache N, or the auto size). And the chunk stays the one the loan would have used: no drop to the 2,048 that --no-prefill-borrow alone gives an auto chunk. The prompt path's own buffers must fit in what the driver reports free beside the cache and the reserve, with 3 GiB to spare (the arithmetic of AUTO NO-BORROW); otherwise the loan stays. The chunk check at the prompt path's start then runs as for any path with its own buffers. One GPU only, chunks up to 16,384. Scope: HIP builds under Linux, the kfd_quiet class (every visible card an integrated gfx1103 / gfx1150 / gfx1151), one block after the cache sizing in generate.cpp under STRATA_USE_HIP && __linux__; it reads class_flag(), so nothing changes elsewhere. Default OFF: kNoLendDefaultOn in kfd_quiet.hpp is the one constant to flip once the box test decides. STRATA_KFD_QUIET_LEND=1 turns it on for a test on the same binary; STRATA_KFD_QUIET_LEND=0, STRATA_KFD_QUIET=0 or STRATA_PREFILL_BORROW=1 keep the loan. No new user setting and no new user-visible line (one STRATA_TRACE line). Unit tests: the switch rule and the pick, with the chunk kept. --- include/strata/core/kfd_quiet.hpp | 41 +++++++++++++++++++++++++++++++ src/program/generate.cpp | 36 +++++++++++++++++++++++++++ tests/core/kfd_quiet_test.cpp | 37 ++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+) diff --git a/include/strata/core/kfd_quiet.hpp b/include/strata/core/kfd_quiet.hpp index c759dd53ee..1f82b2d977 100644 --- a/include/strata/core/kfd_quiet.hpp +++ b/include/strata/core/kfd_quiet.hpp @@ -12,6 +12,8 @@ // pinning our pages in place); a value the caller set wins. // NOREG the expert arena is not registered with the driver (the unregistered "UMA" path of pinned.cu), and not mlock'ed; a value // the user set for STRATA_ARENA_PIN_GIB (or STRATA_UMA_REGISTER=1) wins. +// LEND the prompt path keeps its own buffers instead of lending expert cache slots (no refill after every prompt), decided after +// the cache is sized and only where the buffers fit; the chunk stays. Off by default (kNoLendDefaultOn); _LEND=1 turns it on. // None changes a kernel or what is computed. STRATA_KFD_QUIET=0 turns all of them off at run time (A/B with the same binary); // STRATA_KFD_QUIET_LOAD / _EXIT / _XFER / _NOREG = 0 turn one off. STRATA_TRACE prints one line per change that is active. // @@ -109,6 +111,45 @@ inline bool arena_lock_wanted(bool noreg, const char* lock_env) { return !noreg; } +// ---- LEND: no cache slots lent to the prompt path on the class ------------------------------------------------------------------- + +/// The default of LEND. false: written, off until the box test of the no-loan arm decides; true makes it the class default. +inline constexpr bool kNoLendDefaultOn = false; +/// What the prompt path's own buffers must leave free beside the cache and the reserve (AUTO NO-BORROW's kNoBorrowKeepMib). +inline constexpr int64_t kNoLendKeepMib = 3072; +/// The largest chunk taken without a loan: above it a chunk without a loan meets the Strix Halo launch probe +/// (generate.cpp uma_refuse_chunk_if_full), which can end the start; such a chunk keeps its loan. +inline constexpr int64_t kNoLendMaxChunk = 16384; + +/// LEND's switch: the class; STRATA_KFD_QUIET=0 or STRATA_KFD_QUIET_LEND=0 off; STRATA_KFD_QUIET_LEND=1 on whatever the default; +/// STRATA_PREFILL_BORROW=1 (the existing "keep the loan") off; otherwise kNoLendDefaultOn. +inline bool lend_rule_on(bool cls, const char* all, const char* one, const char* prefill_borrow, bool def = kNoLendDefaultOn) { + if (!cls) return false; + if (prefill_borrow != nullptr && prefill_borrow[0] == '1') return false; + if (all != nullptr && all[0] == '0') return false; + if (one != nullptr && one[0] == '0') return false; + if (one != nullptr && one[0] == '1') return true; + return def; +} + +/// The decision, made AFTER the expert cache is sized as with a loan (so the cache gets the same slots as with lending): the loan +/// is dropped only where the prompt path would lend (`lends`), on one GPU, with a chunk of 1 .. kNoLendMaxChunk, and when the path's +/// own buffers for that chunk (`own_mib`) fit in what the driver reports free beside the cache and the reserve (`room_mib`) with +/// kNoLendKeepMib to spare. The chunk is never changed here (no drop to the 2,048 of a bare --no-prefill-borrow). +struct NoLend { + bool no_lend = false; + bool fits = false; + int64_t chunk = 0; +}; +inline NoLend no_lend_pick(bool rule_on, bool lends, bool one_gpu, int64_t chunk, int64_t room_mib, int64_t own_mib) { + NoLend r; + r.chunk = chunk; + if (!rule_on || !lends || !one_gpu || chunk <= 0 || chunk > kNoLendMaxChunk) return r; + r.fits = room_mib >= own_mib + kNoLendKeepMib; + r.no_lend = r.fits; + return r; +} + // ---- XFER: the device class from the KFD topology (before HIP starts) ------------------------------------------------------------ /// One node's properties text: `gfx_target_version` and `simd_count` (0 when absent). A CPU node has simd_count 0. diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 75d39b19f3..2414274177 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -8854,6 +8854,42 @@ int main(int argc, char** argv) { } #endif } +#if defined(STRATA_USE_HIP) && defined(__linux__) + // LEND (kfd_quiet.hpp), integrated gfx11 under Linux: the prompt path keeps its own buffers instead of lending cache slots, the + // rule of the Strix Halo on Windows (0.2.8, above) for this class. Every lent slot is copied back from the host arena after each + // prompt: 1,967 slots (2.53 GiB) on p2006, +1.1-1.4 s on every short prompt with the arena unregistered (11-15). Decided HERE, + // after the cache was sized as with a loan, so the cache keeps the slots it gets today (--expert-cache N or the auto size); the + // own buffers then must fit in what the driver reports free beside it (AUTO NO-BORROW's arithmetic), else the loan stays. The + // chunk is the one the loan would have used (no drop to 2,048). Off by default (kNoLendDefaultOn); STRATA_KFD_QUIET_LEND=1 turns + // it on, =0 or STRATA_PREFILL_BORROW=1 keeps the loan. + if (pf_borrow && o.prefill_chunk > 0 && + strata::core::kfd_quiet::lend_rule_on(strata::core::kfd_quiet::class_flag().load(std::memory_order_relaxed), std::getenv("STRATA_KFD_QUIET"), + std::getenv("STRATA_KFD_QUIET_LEND"), std::getenv("STRATA_PREFILL_BORROW"))) { + const bool one_gpu = !multi_gpu && stages.empty() && o.peer_device < 1 && + std::none_of(o.expert_cache_remote.begin(), o.expert_cache_remote.end(), [](int n) { return n > 0; }); + uint64_t cache_b = 0; + if (!sized_slots.empty()) + for (const int64_t sb : sized_slots) cache_b += (uint64_t) strata::core::ExpertCache::slot_stride(sb, slot_guard); + else + cache_b = (uint64_t) std::max(o.expert_cache, 0) * (uint64_t) nibble_max_slot_bytes(); + const int64_t own_mib = ((int64_t) strata::prefill::Prefill::bytes_needed_owned(g, ss, o.prefill_chunk, srcp != nullptr) >> 20) + 64; + const size_t fb = strata::core::device_free_bytes(); + const int64_t room_mib = fb > cache_b ? (int64_t) ((fb - cache_b) >> 20) - o.vram_reserve_mib - (int64_t) (pipe_first >> 20) : 0; + const strata::core::kfd_quiet::NoLend nl = strata::core::kfd_quiet::no_lend_pick(true, pf_borrow, one_gpu, o.prefill_chunk, room_mib, own_mib); + if (std::getenv("STRATA_TRACE") != nullptr) + std::fprintf(stderr, "strata trace: kfd quiet: prompt path without a loan: %s (own buffers %lld MiB for a %lld-token chunk, %lld MiB free " + "beside the %d-slot cache and the %d MiB reserve, %lld kept)\n", + nl.no_lend ? "yes" : !one_gpu ? "no, not one GPU" : !nl.fits ? "no, they do not fit" : "no", + (long long) own_mib, (long long) o.prefill_chunk, (long long) room_mib, o.expert_cache, o.vram_reserve_mib, + (long long) strata::core::kfd_quiet::kNoLendKeepMib); + if (nl.no_lend) { + o.no_prefill_borrow = true; + pf_borrow = false; + o.prefill_auto = false; + o.prefill_chunk = nl.chunk; + } + } +#endif #if !defined(STRATA_USE_HIP) && !defined(STRATA_GPU_METAL) && !defined(_WIN32) // 0.2.10.1 (round0212 item 7): the FP16 weight cache is on when the engine runs batch slots on Linux CUDA. Bit-exact against the uncached // form (gemm_w16_parity, greedy identity 12/12, first-token KL 0); +3.8% (c=1) .. +6.6% (c=8) total tokens/s on l620. It takes only VRAM beyond diff --git a/tests/core/kfd_quiet_test.cpp b/tests/core/kfd_quiet_test.cpp index b0ecb55422..e270b78907 100644 --- a/tests/core/kfd_quiet_test.cpp +++ b/tests/core/kfd_quiet_test.cpp @@ -219,11 +219,48 @@ void noreg() { CHECK(arena_lock_wanted(false, "")); CHECK(!arena_lock_wanted(false, "0")); } + +void lend() { + CHECK(std::string(fix_env(Fix::Lend)) == "STRATA_KFD_QUIET_LEND"); + // the switch: written off by default; _LEND=1 on, =0 / STRATA_KFD_QUIET=0 / STRATA_PREFILL_BORROW=1 off; never outside the class + CHECK(!kNoLendDefaultOn); + CHECK(lend_rule_on(true, nullptr, nullptr, nullptr) == kNoLendDefaultOn); + CHECK(lend_rule_on(true, nullptr, nullptr, nullptr, true)); // the constant flipped: the class default + CHECK(!lend_rule_on(true, nullptr, nullptr, nullptr, false)); + CHECK(lend_rule_on(true, nullptr, "1", nullptr)); + CHECK(lend_rule_on(true, "1", "1", nullptr)); + CHECK(!lend_rule_on(true, nullptr, "0", nullptr, true)); + CHECK(!lend_rule_on(true, "0", "1", nullptr, true)); + CHECK(!lend_rule_on(true, nullptr, "1", "1", true)); // STRATA_PREFILL_BORROW=1 keeps the loan + CHECK(lend_rule_on(true, nullptr, "1", "0")); + CHECK(!lend_rule_on(false, nullptr, "1", nullptr, true)); // not the class + // the pick: the chunk always comes back as it went in (8192 stays 8192: no drop to 2048) + const int64_t own8k = 2560; // about the 780M's own buffers at 8192 (2.5 GiB) + for (int64_t c : {(int64_t) 512, (int64_t) 2048, (int64_t) 8192, (int64_t) 16384, (int64_t) 32768}) { + CHECK(no_lend_pick(true, true, true, c, 100000, own8k).chunk == c); + CHECK(no_lend_pick(true, true, true, c, 0, own8k).chunk == c); + CHECK(no_lend_pick(false, true, true, c, 100000, own8k).chunk == c); + } + // the fit check decides: room >= own + kNoLendKeepMib + CHECK(no_lend_pick(true, true, true, 8192, own8k + kNoLendKeepMib, own8k).no_lend); + CHECK(!no_lend_pick(true, true, true, 8192, own8k + kNoLendKeepMib - 1, own8k).no_lend); + CHECK(!no_lend_pick(true, true, true, 8192, own8k + kNoLendKeepMib - 1, own8k).fits); + CHECK(no_lend_pick(true, true, true, 8192, 18000, own8k).no_lend); // p2006: ~18 GiB free beside a 7.72 GiB cache (G) + CHECK(!no_lend_pick(true, true, true, 8192, 0, own8k).no_lend); + // only where the path would lend, on one GPU, a chunk of 1 .. kNoLendMaxChunk, with the rule on + CHECK(!no_lend_pick(false, true, true, 8192, 100000, own8k).no_lend); + CHECK(!no_lend_pick(true, false, true, 8192, 100000, own8k).no_lend); + CHECK(!no_lend_pick(true, true, false, 8192, 100000, own8k).no_lend); + CHECK(!no_lend_pick(true, true, true, 0, 100000, own8k).no_lend); + CHECK(no_lend_pick(true, true, true, kNoLendMaxChunk, 100000, own8k).no_lend); + CHECK(!no_lend_pick(true, true, true, kNoLendMaxChunk + 1, 100000, own8k).no_lend); +} } // namespace int main() { switches(); noreg(); + lend(); load_order(); topology(); stop_order();