From b681c695ff07b77582a321c6fb9f19a2de4e904c Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Mon, 23 Nov 2026 04:48:27 +0900 Subject: [PATCH 1/8] poly: build Inkling 2's DSpark screen on HIP The HIP build of strata-poly stopped at inkling2.cu since 8ddd41667: the screen kernels (scr_base_kernel, scr_step_kernel, scr_final_kernel) call __ldcs and __ldcg on uint4, uint32_t, float and float2, and HIP has those two only for halves ("no matching function for call to '__ldcs'", ten errors, ROCm 7.2.4, gfx1201). On HIP they are now plain loads: the screen uses them as cache hints, on a copy's words read once and on candidates that earlier launches wrote. CUDA builds compile the same code as before (the definitions are under __HIPCC__). Class: same bits on CUDA; HIP did not build before. Inkling 2 is VecSzn's engine. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Erqa3PhC87SPTfVSKZ3Ri5 --- engine/src/poly/inkling2.cu | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/engine/src/poly/inkling2.cu b/engine/src/poly/inkling2.cu index 5808f15c2..308e6eff1 100644 --- a/engine/src/poly/inkling2.cu +++ b/engine/src/poly/inkling2.cu @@ -39,6 +39,13 @@ void launched(const char* what) { throw std::runtime_error(std::string(what) + ": " + cudaGetErrorString(e)); } +#if defined(__HIPCC__) +// HIP has __ldcs and __ldcg for halves only. The DSpark screen's are cache hints (a copy's words read once, the +// tiles' candidates from earlier launches), so on HIP they are plain loads. +template __device__ __forceinline__ T __ldcs(const T* p) { return *p; } +template __device__ __forceinline__ T __ldcg(const T* p) { return *p; } +#endif + constexpr int WARP = 32; // the lanes a shuffle spans: a warp, or half of a 64-lane wavefront // The warp's sum of x, in every lane. From c03a797820ba6390afa418e82cf189b2ee2a939f Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Mon, 23 Nov 2026 06:39:30 +0900 Subject: [PATCH 2/8] docs, setup: Inkling 2 Mini on AMD is experimental and not ready docs/models/inkling.md said "Not run yet: an AMD card". It now says what was seen on the first AMD runs of this engine: it builds for gfx1100, gfx1200 and gfx1201, main's HIP tests pass 15 of 16 on an RX 7900 XT and 11 of 16 on an RX 9070 XT (ROCm 7.2.4, a container that reaches the card through librocdxg), a prompt with experts in RAM can hang, and on the RX 9070 XT the prompt products' library gave a NaN and run-to-run differences in its test. Setup's family line (and the MCP server's copy of it) says AMD cards are not ready. No behaviour change. Co-Authored-By: Claude Opus 5.5 --- docs/models/inkling.md | 8 +++++++- terminal/setup.py | 4 ++-- terminal/tools/strata_mcp.py | 4 ++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/docs/models/inkling.md b/docs/models/inkling.md index e2d87f09e..462733428 100644 --- a/docs/models/inkling.md +++ b/docs/models/inkling.md @@ -94,7 +94,13 @@ rows took 3.4 s. | **Disk** | 9.1 to 17.6 GB for the model file. | | **System** | Linux or Windows 10 / 11, and a current NVIDIA driver; or macOS 15 or newer. | -Not run yet: an AMD card, two cards (`--expert-gpu`, built but not run), UD-Q4_K_XL on the 8 GB card and UD-Q4_K_XL +**AMD (HIP), experimental, not ready:** the engine builds for RDNA3 and RDNA4 (gfx1100, gfx1200, gfx1201), and main's +HIP tests pass on an RX 7900 XT (15 of 16) and on an RX 9070 XT (11 of 16), both under ROCm 7.2.4 in a container that +reaches the card through librocdxg. Known problems there: a prompt with some experts in RAM can hang (the engine waits +forever, at full CPU load); on the RX 9070 XT the prompt products' library gave a NaN and results that differ run to run +in its test. Use an NVIDIA card or a Mac for now. + +Not run yet: a desktop AMD card on native Linux or Windows, two cards (`--expert-gpu`, built but not run), UD-Q4_K_XL on the 8 GB card and UD-Q4_K_XL on a Mac. Pictures go through Unsloth's mmproj-F16.gguf, read on the CPU (`--vision cpu`) or the card; on a Mac, on the CPU. The draft file is an option (`--draft-model`). The engine takes one request at a time diff --git a/terminal/setup.py b/terminal/setup.py index bbdfcf1be..681b25357 100644 --- a/terminal/setup.py +++ b/terminal/setup.py @@ -932,8 +932,8 @@ def ms_meta(repo: str, path: str): # request at a time ("one_request": setup writes "parallel": 1). "vram_beside_gb" is the peak VRAM measured on an RTX 5090 with no expert on the card, a 10,081-token # prompt read 1,024 tokens at a time at a 32K context (2,916 MiB), plus the rows of the context's other tokens. "inkling": {"title": "Inkling 2 Mini", "by": "Thinking Machines; quantized by Unsloth", - "about": "experimental, on the strata-poly engine: one NVIDIA card, Linux or Windows, or a Mac (beta), " - "reads pictures; a 17.6 GB file, or 13.6 or 9.1 GB", + "about": "experimental, on the strata-poly engine: one NVIDIA card, Linux or Windows, or a Mac (beta); " + "AMD cards are not ready (docs/models/inkling.md); reads pictures; a 17.6 GB file, or 13.6 or 9.1 GB", "hf": hf("unsloth/Inkling-2-Mini-GGUF") + "Mainline_Support/", "file": "Inkling-2-Mini-{q}.gguf", "remote_suffix": "_file", "shards": 1, "tag": "inkling-", "name": "inkling-2-mini", "engine": "poly", "vision": True, "experimental": True, diff --git a/terminal/tools/strata_mcp.py b/terminal/tools/strata_mcp.py index 04d358a83..f251d6e7c 100644 --- a/terminal/tools/strata_mcp.py +++ b/terminal/tools/strata_mcp.py @@ -165,8 +165,8 @@ "Q8_0": {"about": "8-bit throughout: the closest to the model as trained", "download_gb": 25.5, "ram_gb": 31, "arena_gb": 24.4}}}, "inkling": {"title": "Inkling 2 Mini", - "about": "experimental, on the strata-poly engine: one NVIDIA card, Linux or Windows, or a Mac (beta), " - "reads pictures; a 17.6 GB file, or 13.6 or 9.1 GB", + "about": "experimental, on the strata-poly engine: one NVIDIA card, Linux or Windows, or a Mac (beta); " + "AMD cards are not ready (docs/models/inkling.md); reads pictures; a 17.6 GB file, or 13.6 or 9.1 GB", "tag": "inkling-", "engine": "poly", "vision": True, "experimental": True, "models": {"UD-Q4_K_XL": {"about": "4-bit experts (NVFP4, Unsloth Dynamic), 8-bit where they write", "download_gb": 17.6, "ram_gb": 21, "arena_gb": 15.797846016}, From 26a31e0bf08d924a68a970ba66727060da694ba1 Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Mon, 23 Nov 2026 06:12:00 +0900 Subject: [PATCH 3/8] poly: VRAM pieces of one size in each reservation under WSL2 on HIP 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, 8 then 4, 64 then 4 all fail, while pieces of one size always pass (64 + 64, 8 + 8, 1 + 1). vmm_map() maps a reservation in pieces of the size it is asked for and a shorter last one, so the expert cache lost its last piece (3,058 of Inkling 2 Mini UD-Q2_K_XL's 3,072 experts in VRAM, whatever the VRAM, reserve or context), and an attention cache grown to the end of its reservation failed ("no VRAM for the attention cache (1 MiB more)": an 8,192-token prompt at --max-context 8704). On a Linux HIP build under WSL2 (/dev/dxg and no /dev/kfd: the test of setup's and the launcher's WSL note, wsl_amd_note()) every piece of a reservation is the size of its first: a reservation is a whole number of 2 MiB units, a first map's piece the most units that divide it and are no more than the piece asked for and the map's bytes, and a map covers whole pieces. The expert cache reserves a whole number of its 64 MiB pieces and an attention cache a whole number of cover()'s, so they keep those sizes; cover() counts the rows' VRAM as the pieces vmm_map() maps. STRATA_VMM_EQUAL_PIECES=1/0 turns it on or off on any Linux HIP build. Native Linux and Windows HIP, the pool (--vram-pool, already one size) and CUDA are unchanged. Co-Authored-By: Claude Opus 5.5 --- engine/include/strata/hip_compat/cuda.h | 8 +++ engine/src/poly/engine.cpp | 69 +++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/engine/include/strata/hip_compat/cuda.h b/engine/include/strata/hip_compat/cuda.h index 7e28df9aa..0aefa8eff 100644 --- a/engine/include/strata/hip_compat/cuda.h +++ b/engine/include/strata/hip_compat/cuda.h @@ -18,6 +18,14 @@ #define STRATA_VMM_MAP_SETTLES 1 #endif +// 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. +#if !defined(_WIN32) +#define STRATA_VMM_EQUAL_PIECES 1 +#endif + typedef hipError_t CUresult; typedef int CUdevice; // HIP's own CUdeviceptr/handle equivalents are pointer-typed; CUDA's are integers, and strata-poly's Mapped diff --git a/engine/src/poly/engine.cpp b/engine/src/poly/engine.cpp index 92dfa1bab..2aaaa974b 100644 --- a/engine/src/poly/engine.cpp +++ b/engine/src/poly/engine.cpp @@ -942,9 +942,41 @@ void Engine::mind_memory() { #endif } +#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 +// 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'; + std::error_code ec; + return std::filesystem::exists("/dev/dxg", ec) && !std::filesystem::exists("/dev/kfd", ec); + }(); + 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); } + +// The size of every piece of a reservation of `reserved` bytes: `first`, the size of the piece it has, or for its +// first map the most units that divide the reservation and are no more than the piece asked for and the map's bytes. +// A map then covers a whole number of pieces, and the last one is no shorter than the others. +static size_t vmm_piece(size_t reserved, size_t first, size_t bytes, size_t piece, size_t unit) { + if (first) return first; + const size_t most = std::min(up_to(std::min(piece, reserved), unit), up_to(bytes, unit)) / unit; + size_t k = std::max(most, 1); + while (k > 1 && (reserved / unit) % k != 0) --k; + return k * unit; +} +#endif + 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_); +#else m.reserved = up_to(std::max(bytes, 1), pool_on_ ? pg_ : grain_); // a whole number of pool handles +#endif cu_ck(cuMemAddressReserve(&p, m.reserved, 0, 0, 0), "reserving addresses for VRAM"); m.base = p; } @@ -1042,7 +1074,16 @@ void Engine::vmm_map(Mapped& m, size_t bytes, size_t piece, const char* what) { // With the pool a piece is one of its handles and comes from it, never from the card; a map it cannot serve // 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_)); + bytes = std::min(up_to(bytes, piece), m.reserved); + } else { + bytes = std::min(up_to(bytes, pool_on_ ? pg_ : grain_), m.reserved); + } +#else bytes = std::min(up_to(bytes, pool_on_ ? pg_ : grain_), m.reserved); +#endif CUmemAllocationProp prop = {}; prop.type = CU_MEM_ALLOCATION_TYPE_PINNED; prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE; @@ -1367,7 +1408,19 @@ void* Engine::kv_alloc(size_t bytes, int per) { Kv k; k.bytes = bytes; k.per = per; +#ifdef STRATA_VMM_EQUAL_PIECES + // with equal pieces, a whole number of cover()'s (addresses only; at most a piece more is mapped at the whole + // context): vmm_piece() takes the most units that divide the reservation, which for one of another size could + // be one unit, every piece 2 MiB and a file descriptor on ROCm + if (!pool_on_ && vmm_equal_pieces()) { + const size_t unit = vmm_unit(grain_), piece = (size_t) ((kv_grow() + per - 1) / per) * bytes; + vmm_reserve(k, up_to(all, up_to(std::min(piece, up_to(all, unit)), unit))); + } else { + vmm_reserve(k, all); + } +#else vmm_reserve(k, all); +#endif kv_.push_back(std::move(k)); p = (void*) kv_.back().base; } @@ -1502,7 +1555,17 @@ void Engine::cover(int64_t end) { const auto size = [rows](const Kv& k) { return (size_t) ((rows + k.per - 1) / k.per) * k.bytes; }; size_t more = 0, held = arena_.mapped + other_rows(); for (const Kv& k : kv_) { +#ifdef STRATA_VMM_EQUAL_PIECES + // what vmm_map() below maps: with equal pieces, whole pieces + const size_t piece = pool_on_ ? pg_ + : !vmm_equal_pieces() + ? grain_ + : vmm_piece(k.reserved, k.pieces.empty() ? 0 : k.pieces.front().second, size(k), + (size_t) ((kv_grow() + k.per - 1) / k.per) * k.bytes, vmm_unit(grain_)); + const size_t want = std::min(up_to(size(k), piece), k.reserved); +#else const size_t want = std::min(up_to(size(k), pool_on_ ? pg_ : grain_), k.reserved); +#endif if (want > k.mapped) more += want - k.mapped; // trim_card_kv() leaves a piece it cuts through held += k.mapped; } @@ -2211,7 +2274,13 @@ void Engine::fill_expert_cache() { #endif if (arena == 0) return; 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)); +#else if (!arena_.base) vmm_reserve(arena_, most + 256); // addresses only: grow_cache() maps more of them +#endif try { vmm_map(arena_, arena + 256, kArenaPiece, "the expert cache"); } catch (const std::runtime_error&) { // the card may not have it all (grow_cache() is just as tolerant) From c4771fcfdc1981ac3b7f1e150ac37b8dc7351311 Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Mon, 23 Nov 2026 06:12:01 +0900 Subject: [PATCH 4/8] poly: the soft limit of open files up to the hard one on HIP ROCm keeps a file descriptor open for each VMM handle (its dmabuf; librocdxg under WSL2), so the usual soft limit of 1,024 caps the handles a process can map: with --vram-pool's 2 MiB handles an RX 9070 XT stopped at 774 of Inkling 2 Mini UD-Q2_K_XL's experts ("fail to import fd, ret 11", and the map fails as a card without VRAM does), where a raised limit maps 6,000 handles (12 GB). A HIP build on Linux now raises its soft RLIMIT_NOFILE towards the hard limit (at most 2^20) at load, as any process may, and never lowers it. CUDA builds are unchanged. Co-Authored-By: Claude Opus 5.5 --- engine/src/poly/engine.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/engine/src/poly/engine.cpp b/engine/src/poly/engine.cpp index 2aaaa974b..a102b1922 100644 --- a/engine/src/poly/engine.cpp +++ b/engine/src/poly/engine.cpp @@ -3192,6 +3192,16 @@ void Engine::load(const Options& o) { // allocation each (kv_alloc()) and the expert cache still gives VRAM to other programs and takes it back // (mind_vram()): refilled in one allocation instead, it only ever got smaller. ck(cudaGetDevice(&device_), "cudaGetDevice"); +#if defined(STRATA_USE_HIP) && defined(__linux__) + // ROCm keeps a file descriptor open for each VMM handle (its dmabuf; librocdxg under WSL2): with --vram-pool's + // 2 MiB handles the usual soft limit of 1,024 descriptors held an RX 9070 XT's pool to 2 GiB ("fail to import fd", + // and the map fails as a card without VRAM does). The soft limit goes up to the hard one (at most 2^20), as a + // process may, and never down. + if (rlimit r{}; getrlimit(RLIMIT_NOFILE, &r) == 0 && r.rlim_cur < r.rlim_max) { + r.rlim_cur = std::max(r.rlim_cur, std::min(r.rlim_max, rlim_t(1) << 20)); + (void) setrlimit(RLIMIT_NOFILE, &r); + } +#endif CUdevice dev = 0; int vmm = 0; if (cuDeviceGet(&dev, device_) == CUDA_SUCCESS && From 4ceaa09744378caaea777f47467384a900cdbd13 Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Mon, 23 Nov 2026 06:29:37 +0900 Subject: [PATCH 5/8] poly: mirror poly_mmq_tiles_test's bounds on the host Mmq::mul()'s BLAS path reads a launch's bounds on the host, where Mmq::mirror() says they are, as Engine::alloc() sets it up for tab_. poly_mmq_tiles_test never called mirror(), so wherever the BLAS path takes its matrices (every HIP build: gemm_iq_f16_grouped and the int8 tiles are CUDA-only) it aborted at its first case with "the matrices' bounds are not where mirror() says" (RX 7900 XT gfx1100 and RX 9070 XT gfx1201, ROCm 7.2.4). The test now mirrors its reference bounds and its class bounds before the launches that read them, as the engine does. Only the test changes. With it the test passes (48 cases) on the RX 7900 XT (Salad container, ROCm 7.2.4). Co-Authored-By: Claude Opus 5.5 --- engine/src/poly/mmq_tiles_test.cu | 3 +++ 1 file changed, 3 insertions(+) diff --git a/engine/src/poly/mmq_tiles_test.cu b/engine/src/poly/mmq_tiles_test.cu index 9fbbe8953..5f69c749a 100644 --- a/engine/src/poly/mmq_tiles_test.cu +++ b/engine/src/poly/mmq_tiles_test.cu @@ -102,8 +102,11 @@ int run_case(const Quant& q, const std::vector& rows, const std::vector 0) From 7ce1fa21bac111312a8528aa164411dae6842641 Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Mon, 23 Nov 2026 08:04:45 +0900 Subject: [PATCH 6/8] poly: Inkling 2's clips and one-token q/k/v as separate launches on HIP ink_clips_kernel and ink_qkv_kernel never finished on AMD cards: an RX 9070 XT (gfx1201) under WSL2 and RX 7900 XT / RX 9070 XT containers, ROCm 7.2.4. The first prompt of Inkling 2 Mini stopped in the first layer's ink_clips launch (with AMD_SERIALIZE_KERNEL=3 the launch never returned; on Windows the driver reset the card), with or without experts on the card, and poly_inkling2_test hung in its clips and qkv cases while clip and head_rms passed. The same source built with any change to ink_clips_kernel finished, so it is the compiler's code for these two kernels, not their arithmetic. ink_clips() is ink_clip() and ink_head_rms() of its four jobs with the same bits, and ink_qkv() ink_conv() of K and V and then the same, by their own contract. On HIP they are now those launches, with the K and V rows stored by store_f16(), store_f16_at() (a recorded launch's positions) or ink_store_ring() (a window's ring), the cells the fused kernels wrote. More launches a layer on AMD; CUDA builds compile the same code as before (the change is under __HIPCC__). Class: same bits by the kernels' contract (poly_inkling2_test's clips and qkv cases, #222, compare them); HIP did not run before. The kernels are VecSzn's. Co-Authored-By: Claude Opus 5.5 --- engine/src/poly/inkling2.cu | 55 +++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/engine/src/poly/inkling2.cu b/engine/src/poly/inkling2.cu index 308e6eff1..ddb0f8be6 100644 --- a/engine/src/poly/inkling2.cu +++ b/engine/src/poly/inkling2.cu @@ -1638,9 +1638,61 @@ void ink_moe_conv_add(const float* x, const float* w, const float* scale, const launched("ink_moe_conv_add"); } +#if defined(__HIPCC__) +// HIP: ink_clips() as the launches its contract names (ink_clip(), ink_head_rms() and fp16 stores of its jobs), and +// ink_qkv() as ink_conv() of K and V and then those. ink_clips_kernel and ink_qkv_kernel never finished on an +// RX 7900 XT (gfx1100) or an RX 9070 XT (gfx1201) under ROCm 7.2.4, the first prompt or token of Inkling 2 Mini +// with them, and the same code built with any change to ink_clips_kernel finished: the compiler's code for these +// two kernels, not their arithmetic. The separate kernels give the same bits (poly_inkling2_test's clips and qkv +// cases compare them). +static void clips_apart(const InkClips& c, void* stream) { + if (c.kv8) throw std::logic_error("ink_clips: no FP8 caches under HIP"); + for (int j = 0; j < 4; ++j) { + if (c.heads[j] <= 0) continue; + if (c.rms[j]) + ink_head_rms(c.x[j], c.dim[j], c.heads[j], c.rows, c.eps, stream); + else + ink_clip(c.x[j], c.scale[j], c.thr[j], c.dim[j], c.heads[j], c.rows, c.pos, c.alpha[j], c.floor[j], stream, + c.at); + if (!c.cache[j]) continue; + const int n = c.heads[j] * c.dim[j]; + if (c.at) store_f16_at(c.x[j], c.cache[j], n, c.at, c.rows, c.ring, stream); + else if (c.ring > 0) ink_store_ring(c.x[j], c.cache[j], n, c.pos, c.rows, c.ring, stream); + else store_f16(c.x[j], c.cache[j], n, c.pos, c.rows, stream); + launched("ink_clips' stores"); + } +} +#endif + void ink_qkv(const InkQkv& a, void* stream) { if (a.dim > 128 || a.n_rel > 128 || a.taps < 2 || a.taps > 8 || a.n_kv < 1) throw std::invalid_argument("ink_qkv: heads of up to 128 values, 2 to 8 taps"); +#if defined(__HIPCC__) + const int kv = a.n_kv * a.dim; + ink_conv(a.kraw, a.kstate, a.kw, a.kdyn, a.k, kv, a.n_kv, a.taps, 1, stream); + ink_conv(a.vraw, a.vstate, a.vw, a.vdyn, a.v, kv, a.n_kv, a.taps, 1, stream); + InkClips c; + float* xs[4] = {a.q, a.k, a.v, a.r}; + const float* sc[4] = {a.q_scale, nullptr, nullptr, a.r_scale}; + const float thr[4] = {a.qk_thr, a.qk_thr, 0.f, a.rel_thr}, al[4] = {a.alpha, 0.f, 0.f, a.alpha}; + const float fl[4] = {a.floor, 1.f, 1.f, a.floor}; + const int dims[4] = {a.dim, a.dim, a.dim, a.n_rel}, hs[4] = {a.n_head, a.n_kv, a.n_kv, a.n_head}; + for (int j = 0; j < 4; ++j) { + c.x[j] = xs[j], c.scale[j] = sc[j], c.thr[j] = thr[j], c.alpha[j] = al[j], c.floor[j] = fl[j]; + c.dim[j] = dims[j], c.heads[j] = hs[j]; + } + c.rms[2] = true; + c.eps = a.eps; + c.pos = a.pos; + c.at = a.at; + c.rows = 1; + c.cache[1] = a.keys; + c.cache[2] = a.vals; + c.ring = a.ring; + c.kv8 = a.kv8; + clips_apart(c, stream); + return; +#endif QkvArgs g{a, 1.0f / std::sqrt((float) ((a.kdyn ? 2 : 1) * a.taps + 1)), 1.0f / std::sqrt((float) ((a.vdyn ? 2 : 1) * a.taps + 1))}; launch_pdl(a.kv8 ? ink_qkv_kernel : ink_qkv_kernel, dim3(2 * a.n_head + 2 * a.n_kv), dim3(128), 0, @@ -1658,6 +1710,9 @@ void ink_clip(float* x, const float* scale, float thr, int dim, int heads, int r void ink_clips(const InkClips& c, void* stream) { if (c.rows <= 0) return; +#if defined(__HIPCC__) + return clips_apart(c, stream); +#endif auto* k = c.kv8 ? ink_clips_kernel : ink_clips_kernel; k<<>>(c); launched("ink_clips"); From cfcb0b9a1730d5dfccddf2defe7bdab869c0591b Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Mon, 23 Nov 2026 08:38:08 +0900 Subject: [PATCH 7/8] poly: no lending of a prompt's buffers to the expert cache on ROCm (Linux) Between prompts the expert cache takes the VRAM of the buffers only a prompt batch uses (lend_prompt()), and a prompt maps them again. ROCm on Linux gives VRAM back only with its addresses (vmm_unmap() frees and reserves them again), which the staging buffer's widening already stays away from (Engine::alloc(), `lend`). With the buffers lent strata-poly crashed in 15 of 16 runs of Inkling 2 Mini IQ4_NL on an RX 9070 XT with 2,443 of its 3,072 experts in VRAM (ROCm 7.2.4 container): SIGSEGV inside hipLaunchKernel, the dequantize launch of a prompt's dense product (Mmq::mul's BLAS path) into the lent work memory. With every expert in VRAM nothing is lent, and it ran. With this change 8 of 8 runs on that card finished (2,424 of 3,072 experts in VRAM, 16 tokens written, the same first tokens as on an RX 7900 XT). Where STRATA_VMM_RELEASE_WITH_ADDRESSES is defined (HIP on Linux) the buffers are now allocated as they are without VMM (our engine in #200, which ran this case, never lent them). CUDA and Windows HIP are unchanged (the preprocessed CUDA engine.cpp is identical). Class: placement only (the buffers' VRAM stays theirs; the expert cache does not grow into it between prompts). Co-Authored-By: Claude Opus 5.5 --- engine/src/poly/engine.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/engine/src/poly/engine.cpp b/engine/src/poly/engine.cpp index a102b1922..c2619d1d5 100644 --- a/engine/src/poly/engine.cpp +++ b/engine/src/poly/engine.cpp @@ -3750,8 +3750,16 @@ void Engine::alloc() { work = floats_bytes + (need.halves * 2 + 255) / 256 * 256; // The buffers only a call of kStageRows rows or more uses (moe_rows(), lin_raw()'s products), side by side: // where they can be mapped apart (VMM) and the answer's calls never have that many rows, the expert cache - // has their VRAM between prompts (lend_prompt()), 300 MB of Qwen3.6's at a prompt batch of 2,048. + // has their VRAM between prompts (lend_prompt()), 300 MB of Qwen3.6's at a prompt batch of 2,048. Not on ROCm + // on Linux, which gives VRAM back only with its addresses (vmm_unmap()), as the staging buffer's widening is + // not: with the buffers lent, strata-poly crashed (SIGSEGV inside hipLaunchKernel, the dequantize launch of a + // prompt's dense product into the lent work memory) in 15 of 16 runs of Inkling 2 Mini IQ4_NL on an RX 9070 XT + // with 2,443 of its 3,072 experts in VRAM, ROCm 7.2.4. +#ifdef STRATA_VMM_RELEASE_WITH_ADDRESSES + prompt_lends_ = false; +#else prompt_lends_ = vmm_ && !split_ && std::max(opt_.window, most_step) < kStageRows; +#endif const size_t sizes[] = {rr * 2 * n_ff_exp_ * sizeof(float), rr * n_ff_exp_ * sizeof(float), rr * n_embd_ * sizeof(float), rr * sizeof(float), Mmq::q8_bytes((int64_t) rr, n_embd_), Mmq::q8_bytes((int64_t) rr, n_ff_exp_), From 521da1618b4046c5d8fc0262fa9166569c8de306 Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Mon, 23 Nov 2026 04:50:36 +0900 Subject: [PATCH 8/8] kernels: the 16-value codebook lookups through v_perm_b32 on every HIP card table16_lookup() (IQ4_NL, IQ4_XS, MXFP4, the formats of 16 values) took llama.cpp's v_perm_b32 sequence on gfx906 only, and the __byte_perm one elsewhere. Both give the same bytes; on RDNA4 the v_perm_b32 one is faster. In our first Inkling engine (PR #200) on an RX 9070 XT (gfx1201, ROCm 7.2.4), 8 cached IQ4_NL experts' products read at 337 -> 411 GB/s (2,304 values) and 244 -> 349 (1,152), and Inkling 2 Mini IQ4_NL wrote 256 tokens after 302 at 43.2 -> 45.6 tok/s, medians of 3. So every HIP build takes it. CUDA builds compile the same code. Co-Authored-By: Claude Opus 5.5 --- engine/src/kernels/cuda/native_mmvq.cu | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/engine/src/kernels/cuda/native_mmvq.cu b/engine/src/kernels/cuda/native_mmvq.cu index e21ec5e68..385087746 100644 --- a/engine/src/kernels/cuda/native_mmvq.cu +++ b/engine/src/kernels/cuda/native_mmvq.cu @@ -605,8 +605,9 @@ __device__ __align__(4) int8_t mxfp4_values[16] = {0, 1, 2, 3, 4, 6, 8, 12, 0, - // Eight four-bit codes of q4 looked up in a codebook of 16: the even nibbles' values in .x, the odd ones' in .y. __device__ __forceinline__ int2 table16_lookup(const int8_t* table, int q4) { -#if defined(STRATA_HIP_GFX906) - // AMD: llama.cpp's HIP lookup (see iq_kernels.cu get_int_from_table_16): 4 v_perm_b32 per 8 values +#if defined(__HIPCC__) + // AMD: llama.cpp's HIP lookup (see iq_kernels.cu get_int_from_table_16): 4 v_perm_b32 per 8 values, and 2 to pick + // the halves - the same bytes as the __byte_perm sequence below, which was slower on RDNA4 const uint32_t* v32 = reinterpret_cast(table); const uint32_t q_even = (uint32_t) q4, q_odd = (uint32_t) q4 >> 4; const uint32_t el = __builtin_amdgcn_perm(v32[1], v32[0], q_even & 0x07070707u);