From 12085164431de65415cdaf5cdeffe3ff7272fc5f Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Wed, 25 Nov 2026 07:56:59 +0900 Subject: [PATCH 1/2] poly: map the expert cache in equal pieces on native ROCm too, so STRATA_GPU_EXPERTS=N is accepted Native Linux ROCm 7.2.4 (RX 9070 XT, /dev/kfd) refuses the shorter last piece of the expert cache after the 64 MiB ones, as WSL2 does, so STRATA_GPU_EXPERTS=N was refused for every N and an uncapped load silently lost its last piece. The expert cache's pieces are now all of one size on every Linux HIP build (STRATA_ARENA_EQUAL_PIECES=0 turns that off); the other reservations (attention caches, the staging buffer, a prompt's work memory) map as before on native, where every reservation in equal pieces cost 0.5% of 8K prompt speed. The cache is sized to whole pieces that fit under free VRAM minus the reserve: rounded down when the room limits it (64, 32 or 16 MiB pieces, whichever holds the most), and a cap is accepted when its size rounded up to a piece fits. A refused cap prints the rounded room and need. CUDA, Metal and Windows are unchanged. Co-Authored-By: Claude Sonnet 5.5 --- docs/AMD_HIP.md | 4 ++ engine/include/strata/hip_compat/cuda.h | 4 +- engine/src/poly/engine.cpp | 64 ++++++++++++++++++++++--- engine/src/poly/engine.hpp | 2 + 4 files changed, 66 insertions(+), 8 deletions(-) diff --git a/docs/AMD_HIP.md b/docs/AMD_HIP.md index eaa664dd95..e3d970300c 100644 --- a/docs/AMD_HIP.md +++ b/docs/AMD_HIP.md @@ -528,6 +528,10 @@ before it installs a model. mapped after a larger one in the same reservation, so every piece of a reservation is made one size there (the expert cache had lost its last 64 MiB piece, and an attention cache grown to its end failed). `STRATA_VMM_EQUAL_PIECES=1` or `=0` turns that on or off on any Linux HIP build. +- On native Linux ROCm 7.2.4 (`/dev/kfd`, RX 9070 XT) the expert cache's shorter last piece is refused the same way, so + `STRATA_GPU_EXPERTS=N` was refused for every N. The expert cache's pieces are now all one size on every Linux HIP + build (64, 32 or 16 MiB, whichever holds the most under free VRAM minus the reserve); the other reservations map as + before there. `STRATA_ARENA_EQUAL_PIECES=0` turns that off on native Linux (WSL2 is as above). - hipBLASLt loads a product type's kernels at its first call. On Windows (RX 9070 XT, ROCm 10.2.0a20260930) the first single-precision product of a prompt took 4 s on one CPU thread with the card idle, inside the first prompt of every strata-poly process. strata-poly now makes one small product of each kind the prompt uses (halves into diff --git a/engine/include/strata/hip_compat/cuda.h b/engine/include/strata/hip_compat/cuda.h index 0aefa8eff4..87f9ca769f 100644 --- a/engine/include/strata/hip_compat/cuda.h +++ b/engine/include/strata/hip_compat/cuda.h @@ -21,7 +21,9 @@ // ROCm 7.2.4 under WSL2 (librocdxg 1.2.2, an RX 9070 XT) refuses hipMemSetAccess (hipErrorInvalidValue) on a piece // mapped after a larger one in the same reservation - 64 MiB then 1 MiB, 2 then 1, 64 then 4 - while pieces of one // size always pass (64 + 64, 8 + 8, 1 + 1 MiB): the expert cache lost its shorter last piece, and an attention cache -// grown to its reservation's end failed. Under WSL (Engine::vmm_map()) every piece of a reservation is one size. +// grown to its reservation's end failed. Native ROCm 7.2.4 (/dev/kfd) refuses the shorter last piece of the expert cache +// the same way. Under WSL (Engine::vmm_map()) every piece of every reservation is one size; on native Linux only the +// expert cache's. #if !defined(_WIN32) #define STRATA_VMM_EQUAL_PIECES 1 #endif diff --git a/engine/src/poly/engine.cpp b/engine/src/poly/engine.cpp index 35005b37ed..245b7330ad 100644 --- a/engine/src/poly/engine.cpp +++ b/engine/src/poly/engine.cpp @@ -1016,7 +1016,7 @@ void Engine::mind_memory() { } #ifdef STRATA_VMM_EQUAL_PIECES -// Whether a reservation's pieces must all be of one size (hip_compat/cuda.h): under WSL2, where ROCm reaches the card +// Whether every reservation's pieces must all be of one size (hip_compat/cuda.h): under WSL2, where ROCm reaches the card // through librocdxg (/dev/dxg, and no /dev/kfd). STRATA_VMM_EQUAL_PIECES=1 or 0 says it for any Linux HIP build. static bool vmm_equal_pieces() { static const bool on = [] { @@ -1027,6 +1027,17 @@ static bool vmm_equal_pieces() { return on; } +// Whether the expert cache's pieces must all be of one size: on every Linux HIP build. Native ROCm 7.2.4 (/dev/kfd, +// an RX 9070 XT) refuses the shorter last piece of that reservation, as WSL2 does; the other reservations (attention +// caches, the staging buffer, a prompt's work memory) map as before there. STRATA_ARENA_EQUAL_PIECES=0 turns it off. +static bool arena_equal_pieces() { + static const bool on = [] { + if (const char* v = std::getenv("STRATA_ARENA_EQUAL_PIECES"); v && v[0]) return v[0] != '0'; + return true; + }(); + return on; +} + // The unit such pieces are made of, 2 MiB like the pool's handles: vmm_reserve() reserves a whole number of them. static size_t vmm_unit(size_t grain) { return up_to(size_t(2) << 20, grain); } @@ -1061,7 +1072,7 @@ void Engine::vmm_reserve(Mapped& m, size_t bytes) { CUdeviceptr p = 0; #ifdef STRATA_VMM_EQUAL_PIECES // with equal pieces, a whole number of their units - m.reserved = up_to(std::max(bytes, 1), pool_on_ ? pg_ : vmm_equal_pieces() ? vmm_unit(grain_) : grain_); + m.reserved = up_to(std::max(bytes, 1), pool_on_ ? pg_ : (m.equal || vmm_equal_pieces()) ? vmm_unit(grain_) : grain_); #else m.reserved = up_to(std::max(bytes, 1), pool_on_ ? pg_ : grain_); // a whole number of pool handles #endif @@ -1163,8 +1174,13 @@ void Engine::vmm_map(Mapped& m, size_t bytes, size_t piece, const char* what) { // throws as a card that has no more VRAM does if (pool_on_) piece = pg_; #ifdef STRATA_VMM_EQUAL_PIECES - if (!pool_on_ && vmm_equal_pieces()) { // whole pieces of the reservation's one size - piece = vmm_piece(m.reserved, m.pieces.empty() ? 0 : m.pieces.front().second, bytes, piece, vmm_unit(grain_)); + if (!pool_on_ && (m.equal || vmm_equal_pieces())) { // whole pieces of the reservation's one size + // an expert cache that was cut to nothing keeps its piece size for the next map (native only: under WSL + // vmm_equal_pieces() chooses it again, as before) + const bool keep = m.equal && !vmm_equal_pieces(); + piece = vmm_piece(m.reserved, m.pieces.empty() ? (keep ? m.piece0 : 0) : m.pieces.front().second, bytes, piece, + vmm_unit(grain_)); + if (keep && m.pieces.empty()) m.piece0 = piece; bytes = std::min(up_to(bytes, piece), m.reserved); } else { bytes = std::min(up_to(bytes, pool_on_ ? pg_ : grain_), m.reserved); @@ -2416,13 +2432,47 @@ void Engine::fill_expert_cache() { if (vmm_) { // in pieces, so that it can give some up to the attention caches (shrink_cache()) and grow #ifdef STRATA_VMM_EQUAL_PIECES // with equal pieces, a whole number of kArenaPiece, so that its pieces can all be of that size (vmm_map()) - if (!arena_.base) - vmm_reserve(arena_, pool_on_ || !vmm_equal_pieces() ? most + 256 : up_to(most + 256, kArenaPiece)); + if (!arena_.base) { + arena_.equal = arena_equal_pieces(); + vmm_reserve(arena_, pool_on_ || !(arena_.equal || vmm_equal_pieces()) ? most + 256 : up_to(most + 256, kArenaPiece)); + } #else if (!arena_.base) vmm_reserve(arena_, most + 256); // addresses only: grow_cache() maps more of them +#endif + size_t arena_piece = kArenaPiece; +#ifdef STRATA_VMM_EQUAL_PIECES + if (!pool_on_ && arena_.equal && !vmm_equal_pieces() && free_b > reserve) { // native; WSL sizes as before + arena_.piece0 = 0; // chosen again for this load + // With equal pieces a map covers whole pieces, so it must be sized to the pieces that fit under free + // minus the reserve, never past it: a cap that needs the room rounded up to a piece is tried with 32 and + // 16 MiB pieces too, and a cache the room limits is rounded down to whole pieces of the size that holds the + // most. A piece the cache already has stays the size. + const size_t unit = vmm_unit(grain_), room = free_b - reserve; + const size_t front = arena_.pieces.empty() ? 0 : arena_.pieces.front().second; + const auto mapped = [&](size_t want, size_t p, bool down) { + const size_t pc = vmm_piece(arena_.reserved, front, want, p, unit); + return std::min(down ? want / pc * pc : up_to(want, pc), arena_.reserved); + }; + const size_t sizes[3] = {kArenaPiece, kArenaPiece / 2, kArenaPiece / 4}; + const size_t n = front ? 1 : 3, want = arena + 256; + bool fits = false; + if (arena >= all) + for (size_t i = 0; i < n && !fits; ++i) + if (mapped(want, front ? kArenaPiece : sizes[i], false) <= room) fits = true, arena_piece = sizes[i]; + if (!fits) { + size_t best = 0; + for (size_t i = 0; i < n; ++i) { + const size_t b = mapped(std::min(want, room), front ? kArenaPiece : sizes[i], true); + if (b > best) best = b, arena_piece = sizes[i]; + } + arena = best > 256 ? best - 256 : 0; + if (expert_cap_ > 0 && arena < all) short_of(arena, all); + if (arena == 0) return; + } + } #endif try { - vmm_map(arena_, arena + 256, kArenaPiece, "the expert cache"); + vmm_map(arena_, arena + 256, arena_piece, "the expert cache"); } catch (const std::runtime_error&) { // the card may not have it all (grow_cache() is just as tolerant) arena = arena_.mapped > 256 ? arena_.mapped - 256 : 0; if (expert_cap_ > 0 && arena < all) short_of(arena, all); diff --git a/engine/src/poly/engine.hpp b/engine/src/poly/engine.hpp index ab0e274541..1b89cb5801 100644 --- a/engine/src/poly/engine.hpp +++ b/engine/src/poly/engine.hpp @@ -803,6 +803,8 @@ class Engine { struct Mapped { unsigned long long base = 0; ///< a CUdeviceptr size_t reserved = 0, mapped = 0; + size_t piece0 = 0; ///< the piece size an equal-piece cache kept when it was cut to nothing + bool equal = false; ///< its pieces are all of one size even where the other reservations' are not (the expert cache) std::vector> pieces; ///< allocation handles and their bytes, in order }; struct Kv : Mapped { From c1ae8dec06b9d36d9b22fa7f86889bc8ea8f7cb6 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_VMM_EQUAL_PIECES=arena|all|0 in place of STRATA_ARENA_EQUAL_PIECES One variable for which VRAM reservations map in pieces of one size: all (or 1) every reservation, as on main with =1; arena the expert cache's only, the default on native Linux HIP; 0 none. Unset: all under WSL2, arena on native Linux, as before. STRATA_ARENA_EQUAL_PIECES is no longer read; the register note of STRATA_VMM_EQUAL_PIECES says what it does. Co-Authored-By: Claude Sonnet 5.5 --- docs/AMD_HIP.md | 5 +++-- engine/src/poly/engine.cpp | 31 ++++++++++++---------------- terminal/data/settings-register.json | 2 +- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/docs/AMD_HIP.md b/docs/AMD_HIP.md index e3d970300c..e4c98ecb72 100644 --- a/docs/AMD_HIP.md +++ b/docs/AMD_HIP.md @@ -527,11 +527,12 @@ before it installs a model. - Under WSL2 (ROCm 7.2.4 with librocdxg: `/dev/dxg` and no `/dev/kfd`) ROCm refuses `hipMemSetAccess` on a piece mapped after a larger one in the same reservation, so every piece of a reservation is made one size there (the expert cache had lost its last 64 MiB piece, and an attention cache grown to its end failed). - `STRATA_VMM_EQUAL_PIECES=1` or `=0` turns that on or off on any Linux HIP build. + `STRATA_VMM_EQUAL_PIECES=all` (or `1`) or `=0` turns that on or off on any Linux HIP build. - On native Linux ROCm 7.2.4 (`/dev/kfd`, RX 9070 XT) the expert cache's shorter last piece is refused the same way, so `STRATA_GPU_EXPERTS=N` was refused for every N. The expert cache's pieces are now all one size on every Linux HIP build (64, 32 or 16 MiB, whichever holds the most under free VRAM minus the reserve); the other reservations map as - before there. `STRATA_ARENA_EQUAL_PIECES=0` turns that off on native Linux (WSL2 is as above). + before there. `STRATA_VMM_EQUAL_PIECES=0` turns that off on native Linux (WSL2 is as above); + `=arena` asks for only that on any Linux HIP build. - hipBLASLt loads a product type's kernels at its first call. On Windows (RX 9070 XT, ROCm 10.2.0a20260930) the first single-precision product of a prompt took 4 s on one CPU thread with the card idle, inside the first prompt of every strata-poly process. strata-poly now makes one small product of each kind the prompt uses (halves into diff --git a/engine/src/poly/engine.cpp b/engine/src/poly/engine.cpp index 245b7330ad..5c4e1243e5 100644 --- a/engine/src/poly/engine.cpp +++ b/engine/src/poly/engine.cpp @@ -1016,27 +1016,22 @@ void Engine::mind_memory() { } #ifdef STRATA_VMM_EQUAL_PIECES -// Whether every reservation's pieces must all be of one size (hip_compat/cuda.h): under WSL2, where ROCm reaches the card -// through librocdxg (/dev/dxg, and no /dev/kfd). STRATA_VMM_EQUAL_PIECES=1 or 0 says it for any Linux HIP build. -static bool vmm_equal_pieces() { - static const bool on = [] { - if (const char* v = std::getenv("STRATA_VMM_EQUAL_PIECES"); v && v[0]) return v[0] != '0'; +// Which reservations' pieces must all be of one size (hip_compat/cuda.h): 2 every one, under WSL2, where ROCm reaches the +// card through librocdxg (/dev/dxg, and no /dev/kfd); 1 the expert cache's only, on native ROCm 7.2.4 (/dev/kfd, an +// RX 9070 XT), which refuses its shorter last piece as WSL2 does, while the other reservations (attention caches, the +// staging buffer, a prompt's work memory) map as before there; 0 none. STRATA_VMM_EQUAL_PIECES=all (or 1), =arena +// and =0 say it for any Linux HIP build. +static int vmm_equal_mode() { + static const int mode = [] { + if (const char* v = std::getenv("STRATA_VMM_EQUAL_PIECES"); v && v[0]) + return v[0] == '0' ? 0 : std::strcmp(v, "arena") == 0 ? 1 : 2; std::error_code ec; - return std::filesystem::exists("/dev/dxg", ec) && !std::filesystem::exists("/dev/kfd", ec); + return std::filesystem::exists("/dev/dxg", ec) && !std::filesystem::exists("/dev/kfd", ec) ? 2 : 1; }(); - return on; -} - -// Whether the expert cache's pieces must all be of one size: on every Linux HIP build. Native ROCm 7.2.4 (/dev/kfd, -// an RX 9070 XT) refuses the shorter last piece of that reservation, as WSL2 does; the other reservations (attention -// caches, the staging buffer, a prompt's work memory) map as before there. STRATA_ARENA_EQUAL_PIECES=0 turns it off. -static bool arena_equal_pieces() { - static const bool on = [] { - if (const char* v = std::getenv("STRATA_ARENA_EQUAL_PIECES"); v && v[0]) return v[0] != '0'; - return true; - }(); - return on; + return mode; } +static bool vmm_equal_pieces() { return vmm_equal_mode() == 2; } +static bool arena_equal_pieces() { return vmm_equal_mode() >= 1; } // The unit such pieces are made of, 2 MiB like the pool's handles: vmm_reserve() reserves a whole number of them. static size_t vmm_unit(size_t grain) { return up_to(size_t(2) << 20, grain); } diff --git a/terminal/data/settings-register.json b/terminal/data/settings-register.json index 1b115a1705..80b46d9037 100644 --- a/terminal/data/settings-register.json +++ b/terminal/data/settings-register.json @@ -51808,7 +51808,7 @@ "last_date": null, "status": "research", "owner": "poly", - "note": "[reviewed] HIP on Linux (STRATA_VMM_EQUAL_PIECES builds): every piece of a VRAM reservation is one size; on by default under WSL2 (/dev/dxg without /dev/kfd), where ROCm 7.2.4 with librocdxg refuses hipMemSetAccess on a piece mapped after a larger one; =1 or =0 forces it on or off (engine/src/poly/engine.cpp:948, docs/AMD_HIP.md:518)", + "note": "HIP on Linux (STRATA_VMM_EQUAL_PIECES builds): which VRAM reservations map in pieces of one size. WSL2 (/dev/dxg without /dev/kfd): every one, where ROCm 7.2.4 with librocdxg refuses hipMemSetAccess on a piece mapped after a larger one. Native Linux: the expert cache's only (=arena), which native ROCm 7.2.4 refuses a shorter last piece of. =all (or =1) every reservation, =arena the expert cache's only, =0 none, on any Linux HIP build.", "retire_after": "", "retired_in": "" },