From 913c000108c81ae3719103f4b29b9be04bb5be1c Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Wed, 25 Nov 2026 10:36:17 +0900 Subject: [PATCH 1/2] poly: Inkling 2's prompt controllers take their gain, forget and gate in one launch on HIP A prompt's loop controller ran its gain and forget products as two hipblasSgemm calls (64 values in, 2,304 out a token) and then ink_gate(): three launches and a round trip of two 2,304-wide buffers, about 290 us of every controller at 1,024 rows on an RX 9070 XT, 96 controllers a call. ink_ctrl_up_gate() does the two products and the gate in one launch, a thread an output with its two weight rows in registers. Each product is one fmaf after the other over the rank from 0, which is what hipBLAS's kernel for this shape does: byte for byte the same on gfx1201 under ROCm 7.2 and 10.2 at every row count tried (32 to 8,192), and poly_inkling2_test's ctrl_up_gate case checks it against gemm_f32() and ink_gate(). CUDA keeps cuBLAS. Co-Authored-By: Claude Opus 5.5 --- engine/src/poly/inkling2.cpp | 19 +++++++++ engine/src/poly/inkling2.cu | 58 ++++++++++++++++++++++++++ engine/src/poly/inkling2.hpp | 3 ++ engine/src/poly/inkling2_kernels.hpp | 6 +++ engine/src/poly/inkling2_test.cu | 62 ++++++++++++++++++++++++++++ 5 files changed, 148 insertions(+) diff --git a/engine/src/poly/inkling2.cpp b/engine/src/poly/inkling2.cpp index c20d374044..01abd14017 100644 --- a/engine/src/poly/inkling2.cpp +++ b/engine/src/poly/inkling2.cpp @@ -940,11 +940,30 @@ void Inkling2::control(const float* cur, const float* msg, const Ctrl& c, float* const int outs[2] = {n_embd_, n_embd_}; gemv_set(w, x, y, outs, 2, rank_, n, st_); } else { +#if defined(STRATA_USE_HIP) + // a prompt's: lin_pair()'s two hipblasSgemm calls and the gate in one launch, the same bits + if (up_gate_ok(c, n) && ink_ctrl_up_gate(cur, msg, ch_, c.gain.p, c.forget.p, out, n_embd_, rank_, n, st_, + c.gain.type == kTypeBF16)) + return; +#endif lin_pair(c.gain, c.forget, ch_, gain_, forget_, n, nullptr); } ink_gate(cur, msg, gain_, forget_, out, (size_t) n * n_embd_, st_); } +#if defined(STRATA_USE_HIP) +// Where lin_pair() takes a controller's gain and forget through gemm_f32()'s hipblasSgemm (a prompt batch of F32, or +// of BF16 widened into wf_; more than the 256 outputs gemm_f32() keeps for its own kernels), whose bits +// ink_ctrl_up_gate() has. +bool Inkling2::up_gate_ok(const Ctrl& c, int n) const { + const auto blas = [&](const Dev& d) { + return d.scale == 1.0f && d.n_in == rank_ && d.n_out == n_embd_ && + (d.type == kTypeF32 || (d.type == kTypeBF16 && wf_ && (size_t) d.n_in * d.n_out <= bf16_max_)); + }; + return n >= kStageRows && n_embd_ > 256 && c.gain.type == c.forget.type && blas(c.gain) && blas(c.forget); +} +#endif + // A later pass's FFN controller for one token (control() on mixf_), its gate in the launch of the experts' input // norm and quantization (native_add_rms_norm_q8_1()'s): the same bytes. False, with nothing launched, where // control() and that norm do it. `row`: the token's row of mixf_, msg, xn_ and xq_ (step()'s rows, one at a time). diff --git a/engine/src/poly/inkling2.cu b/engine/src/poly/inkling2.cu index c5d7266393..74e5c5fa5c 100644 --- a/engine/src/poly/inkling2.cu +++ b/engine/src/poly/inkling2.cu @@ -2941,6 +2941,64 @@ void ink_gate_set(const float* const* cur, const float* const* msg, const float* launched("ink_gate"); } +// ink_ctrl_up_gate(): a prompt's gain and forget products of a controller and its gate in one launch. A thread has +// one output (value o of a row) and its two weight rows in registers, and goes through kUpTokens tokens whose ch rows +// the block holds. Each product is one fmaf after the other over the rank in its order from 0, as hipblasSgemm's +// kernel for these shapes does it (the same bits on gfx1201 under ROCm 7.2 and 10.2; inkling2_test's ctrl_up_gate +// checks it against lin()'s launches), then ink_gate_kernel's arithmetic on them. +constexpr int kUpThreads = 256, kUpTokens = 32, kUpRank = 64; + +template +__global__ void __launch_bounds__(kUpThreads) ink_ctrl_up_gate_kernel(const float* cur, const float* msg, + const float* __restrict__ ch, + const W* __restrict__ gw, + const W* __restrict__ fw, float* out, + int n_embd, int rows) { + __shared__ __align__(16) float sc[kUpTokens][kUpRank]; + const int o = blockIdx.x * kUpThreads + threadIdx.x, t0 = blockIdx.y * kUpTokens; + const int nt = min(kUpTokens, rows - t0); + for (int i = threadIdx.x; i < kUpTokens * kUpRank; i += kUpThreads) { + const int t = i / kUpRank; + sc[t][i % kUpRank] = t < nt ? ch[size_t(t0 + t) * kUpRank + i % kUpRank] : 0.0f; + } + __syncthreads(); + if (o >= n_embd) return; + float g[kUpRank], f[kUpRank]; +#pragma unroll + for (int k = 0; k < kUpRank; ++k) { + g[k] = wval(gw, size_t(o) * kUpRank + k); + f[k] = wval(fw, size_t(o) * kUpRank + k); + } + for (int t = 0; t < nt; ++t) { + float a = 0.0f, b = 0.0f; +#pragma unroll + for (int k = 0; k < kUpRank; k += 4) { + const float4 x = *reinterpret_cast(&sc[t][k]); + a = fmaf(x.x, g[k], a), b = fmaf(x.x, f[k], b); + a = fmaf(x.y, g[k + 1], a), b = fmaf(x.y, f[k + 1], b); + a = fmaf(x.z, g[k + 2], a), b = fmaf(x.z, f[k + 2], b); + a = fmaf(x.w, g[k + 3], a), b = fmaf(x.w, f[k + 3], b); + } + const size_t i = size_t(t0 + t) * n_embd + o; + const float c = cur[i]; + out[i] = c - tanhf(b) * c + tanhf(a) * msg[i]; + } +} + +bool ink_ctrl_up_gate(const float* cur, const float* msg, const float* ch, const void* gain, const void* forget, + float* out, int n_embd, int rank, int rows, void* stream, bool bf16) { + if (rank != kUpRank || rows <= 0) return false; + const dim3 g(grid((size_t) n_embd, kUpThreads), grid((size_t) rows, kUpTokens)); + if (bf16) + ink_ctrl_up_gate_kernel<<>>( + cur, msg, ch, (const uint16_t*) gain, (const uint16_t*) forget, out, n_embd, rows); + else + ink_ctrl_up_gate_kernel<<>>(cur, msg, ch, (const float*) gain, + (const float*) forget, out, n_embd, rows); + launched("ink_ctrl_up_gate"); + return true; +} + void ink_gate(const float* cur, const float* msg, const float* gain, const float* forget, float* out, size_t n, void* stream) { ink_gate_set(&cur, &msg, &gain, &forget, &out, 1, n, stream); diff --git a/engine/src/poly/inkling2.hpp b/engine/src/poly/inkling2.hpp index 9b3b442939..1a72f33ecf 100644 --- a/engine/src/poly/inkling2.hpp +++ b/engine/src/poly/inkling2.hpp @@ -135,6 +135,9 @@ class Inkling2 final : public Engine { /// The stream waits for what side_ has. void join(); void control(const float* cur, const float* msg, const Ctrl& c, float* out, int n); +#if defined(STRATA_USE_HIP) + bool up_gate_ok(const Ctrl& c, int n) const; +#endif bool control3(const float* const* cur, float* const* msg, const Ctrl* c, float* const* out, int n); bool project(const Block& B, const float* const* x, int n, bool quantized = false, bool* extras = nullptr); bool control_ffn(const float* msg, const Ctrl& c, const Block& B, int row = 0); diff --git a/engine/src/poly/inkling2_kernels.hpp b/engine/src/poly/inkling2_kernels.hpp index 0026015baa..93e69b12c5 100644 --- a/engine/src/poly/inkling2_kernels.hpp +++ b/engine/src/poly/inkling2_kernels.hpp @@ -64,6 +64,12 @@ void ink_control3_gate(const float* const* cur, const float* const* msg, const f void ink_gate_set(const float* const* cur, const float* const* msg, const float* const* gain, const float* const* forget, float* const* out, int count, size_t n, void* stream); +/// A prompt's controller after its down product: gain = ch gain_w^T and forget = ch forget_w^T (rank values in, n_embd +/// out, F32 or with bf16 BF16 matrices), then ink_gate() of them, in one launch. Each product is one fmaf after the +/// other in the rank's order: the bits of lin_pair()'s hipblasSgemm on gfx1201 (inkling2_test: ctrl_up_gate). False, +/// with nothing launched, for a rank other than 64. out may be cur. +bool ink_ctrl_up_gate(const float* cur, const float* msg, const float* ch, const void* gain_w, const void* forget_w, + float* out, int n_embd, int rank, int rows, void* stream, bool bf16 = false); /// out = cur - tanh(forget) * cur + tanh(gain) * msg, n values. out may be cur. void ink_gate(const float* cur, const float* msg, const float* gain, const float* forget, float* out, size_t n, void* stream); diff --git a/engine/src/poly/inkling2_test.cu b/engine/src/poly/inkling2_test.cu index 7c26b0cf6c..09f9985317 100644 --- a/engine/src/poly/inkling2_test.cu +++ b/engine/src/poly/inkling2_test.cu @@ -1423,6 +1423,67 @@ void case_attend_batch() { // ---- the list ---- + +// A prompt's controller after its down product: ink_ctrl_up_gate() against lin_pair()'s launches for it (gemm_f32()'s +// two products, after bf16_to_f32() for BF16 matrices, then ink_gate()), byte for byte, at row counts around the +// prompt calls' and in place (out = cur, as the FFN's controller writes it); and against the reference. The bits are +// hipblasSgemm's where gemm_f32() calls it (HIP): CUDA's cuBLAS sums in another order, so there it is checked against +// the reference only. +void case_ctrl_up_gate() { + void* blas = blas_create(stream()); + for (const bool bf : {false, true}) + for (const auto [rows, embd] : {std::pair{32, kEmbd}, {33, kEmbd}, {100, kEmbd}, {777, kEmbd}, {1024, kEmbd}, + {2048, kEmbd}, {3001, kEmbd}, {100, kEmbd + 32}}) { + const auto cur = randv((size_t) rows * embd, 1.0f), msg = randv((size_t) rows * embd, 0.5f), + chv = randv((size_t) rows * kRank, 1.0f); + auto gain = randv((size_t) embd * kRank, 0.1f), forget = randv((size_t) embd * kRank, 0.1f); + std::vector gb, fb; + if (bf) bf16_of(std::vector(gain), gb, gain), bf16_of(std::vector(forget), fb, forget); + Dev dcur(cur), dmsg(msg), dch(chv), dg(gain), df(forget), gv((size_t) rows * embd), + fv((size_t) rows * embd), out((size_t) rows * embd), out2((size_t) rows * embd), wide(gain.size()); + Dev dgb(bf ? gb : std::vector(1)), dfb(bf ? fb : std::vector(1)); + if (bf) { + bf16_to_f32(dgb, wide, gain.size(), stream()); + gemm_f32(blas, wide, dch, gv, kRank, embd, embd, rows); + bf16_to_f32(dfb, wide, forget.size(), stream()); + gemm_f32(blas, wide, dch, fv, kRank, embd, embd, rows); + } else { + gemm_f32(blas, dg, dch, gv, kRank, embd, embd, rows); + gemm_f32(blas, df, dch, fv, kRank, embd, embd, rows); + } + ink_gate(dcur, dmsg, gv, fv, out2, (size_t) rows * embd, stream()); + const void* gw = bf ? (const void*) dgb.p : (const void*) dg.p; + const void* fw = bf ? (const void*) dfb.p : (const void*) df.p; + if (!ink_ctrl_up_gate(dcur, dmsg, dch, gw, fw, out, embd, kRank, rows, stream(), bf)) { + ++failures; + std::printf("ink_ctrl_up_gate: refused Inkling 2 Mini's shape FAIL\n"); + continue; + } + const auto got = out.get(); + const std::string what = fmt("ink_ctrl_up_gate %s, %d rows of %d", bf ? "BF16" : "F32", rows, embd); +#if defined(__HIPCC__) + same_bits(what + " = gemm_f32() x2 + ink_gate()", got, out2.get()); +#endif + // in place: the FFN's controller writes its cur + ink_ctrl_up_gate(dcur, dmsg, dch, gw, fw, dcur, embd, kRank, rows, stream(), bf); + same_bits(what + ", out = cur", dcur.get(), got); + if (rows > 100) continue; + std::vector ref((size_t) rows * embd); + for (int t = 0; t < rows; ++t) + for (int i = 0; i < embd; ++i) { + double g = 0, f = 0; + for (int k = 0; k < kRank; ++k) { + g += (double) gain[(size_t) i * kRank + k] * chv[(size_t) t * kRank + k]; + f += (double) forget[(size_t) i * kRank + k] * chv[(size_t) t * kRank + k]; + } + const size_t at = (size_t) t * embd + i; + ref[at] = h_gate(cur[at], msg[at], g, f); + } + compare(what + " against the reference", got, ref, 2e-5); + } + blas_destroy(blas); +} + struct Case { const char* name; void (*run)(); @@ -1439,6 +1500,7 @@ const Case kCases[] = { {"mix", case_mix}, {"norms", case_norms}, {"controllers", case_controllers}, + {"ctrl_up_gate", case_ctrl_up_gate}, {"situ", case_situ}, {"router", case_router}, {"small", case_small}, From f8fd4743dd7d9ece7725d7906003d0765049a928 Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Wed, 25 Nov 2026 13:24:18 +0900 Subject: [PATCH 2/2] poly: gemm_f32()'s small products take RDNA-sized tiles on HIP gemm_f32() gives a prompt's products of up to 256 outputs to gemm_tile_kernel (or gemm_small_kernel), with tiles sized on NVIDIA cards. On an RX 9070 XT they left the 64 multiprocessors few blocks: Inkling 2 Mini's controllers' down products (4,608 values into 64 outputs, 1,024 tokens) took 243 us, its router (2,304 into 128) 146 us. On HIP the tile now follows the product's size: <32, 32, 2, 2> up to 65,536 outputs, <64, 64, 4, 4> up to 262,144 (or at 64 outputs or fewer), <64, 128, 4, 4> past that; 156 and 120 us for the two. Every tile sums an output one fmaf after the other in the inputs' order, so the bits are the same; poly_gemm_small_test checks Inkling's shapes too. Co-Authored-By: Claude Opus 5.5 --- engine/src/poly/gemm_small_test.cu | 4 ++++ engine/src/poly/kernels.cu | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/engine/src/poly/gemm_small_test.cu b/engine/src/poly/gemm_small_test.cu index ca97897ab3..7564ea4873 100644 --- a/engine/src/poly/gemm_small_test.cu +++ b/engine/src/poly/gemm_small_test.cu @@ -56,6 +56,10 @@ int main() { int bad = 0; for (int n_out : {32, 64, 100, 128, 256}) for (int rows : {32, 37, 1024, 2048}) bad += check(h, 2048, n_out, rows, rng); + for (int rows : {37, 1024, 2048, 8192}) { // Inkling 2 Mini's controllers' down matrices and its router + bad += check(h, 4608, 64, rows, rng); + bad += check(h, 2304, 128, rows, rng); + } bad += check(h, 2000, 64, 45, rng); // inputs that end inside a step bad += check(h, 2000, 256, 45, rng); // and of gemm_tile_kernel's cublasDestroy(h); diff --git a/engine/src/poly/kernels.cu b/engine/src/poly/kernels.cu index 256f269c10..1ab008b4ff 100644 --- a/engine/src/poly/kernels.cu +++ b/engine/src/poly/kernels.cu @@ -3529,6 +3529,22 @@ void gemm_f32(void* blas, const float* w, const float* x, float* y, int n_in, in if (groups == 1 && n_out <= 8 * SMALL_OUT) { cudaStream_t stream = nullptr; blas_ck(cublasGetStream(h, &stream), "cublasGetStream"); +#if defined(STRATA_USE_HIP) + // RDNA: the tiles by the size of the product. The shapes above left an RX 9070 XT's 64 multiprocessors few + // blocks: 1,024 tokens of Inkling 2 Mini's 4,608 values into its controllers' 64 outputs took 243 us with + // <16, 64, 2, 4>, 156 with <32, 32, 2, 2>; its router (2,304 into 128) 146 us with <32, 64, 4, 4>, 120 with + // <64, 64, 4, 4>. Every tile sums an output in the same order: the same bits. + if (n_out > 2 * SMALL_OUT || small_waves(rows, n_out) > 2) { + const long long outs = (long long) rows * n_out; + if (outs <= 65536) + gemm_tile<32, 32, 2, 2>(w, x, y, n_in, n_out, rows, stream); + else if (outs <= 262144 || n_out <= 64) + gemm_tile<64, 64, 4, 4>(w, x, y, n_in, n_out, rows, stream); + else + gemm_tile<64, 128, 4, 4>(w, x, y, n_in, n_out, rows, stream); + return; + } +#endif if (n_out > 2 * SMALL_OUT) gemm_tile<32, 64, 4, 4>(w, x, y, n_in, n_out, rows, stream); else if (small_waves(rows, n_out) > 2)