From 5956327e2e3306e957809b4fbfe0271f1dcf02cc Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Tue, 24 Nov 2026 03:43:50 +0900 Subject: [PATCH] poly: rms_norm_many() without its parameters in local memory rms_norm_many_kernel took its input's pointers as m.x[blockIdx.y]: an index into the kernel's parameters by a register, which nvcc compiles as a copy of the whole struct to local memory in every block (nine STL.64 and three LDL.64 on sm_75), and the pointers read back from there made every load a generic one (LD) instead of a read-only global load. The three-input norms of Inkling 2's prompt layers took about 2.8 times as long a row as rms_norm_kernel in an nsys trace on an RTX 2070 Super. The pointers are now picked by comparisons (rms_pick()). The rest of the kernel is unchanged: each row's sum is the same threads' values in the same order, so the same bits. A new case in poly_inkling2_test checks rms_norm_many() of 5 and 300 rows against rms_norm() of each input, bit for bit. Co-Authored-By: Claude Opus 5.5 --- engine/src/poly/inkling2_test.cu | 18 ++++++++++++++++++ engine/src/poly/kernels.cu | 18 +++++++++++++----- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/engine/src/poly/inkling2_test.cu b/engine/src/poly/inkling2_test.cu index 7c26b0cf6c..622e80bb0d 100644 --- a/engine/src/poly/inkling2_test.cu +++ b/engine/src/poly/inkling2_test.cu @@ -893,6 +893,24 @@ void case_norms() { compare(fmt("rms_norm_many input %d against the reference", j), ys2[(size_t) j].get(), h_rms_norm(hx[(size_t) j].data(), hw[(size_t) j].data(), kEmbd, 1, kEps), 1e-5); } + // rms_norm_many() of several rows, each input's = rms_norm()'s of it, bit for bit (the prompt's three norms) + for (const int rows : {5, 300}) { + std::vector> mx, mw, my, my2; + const float* mxp[3]; + const float* mwp[3]; + float* myp[3]; + for (int j = 0; j < 3; ++j) { + mx.emplace_back(randv((size_t) rows * kEmbd, 1.0f + j)), mw.emplace_back(randv(kEmbd, 0.2f, 1.0f)); + my.emplace_back((size_t) rows * kEmbd), my2.emplace_back((size_t) rows * kEmbd); + mxp[j] = mx.back(), mwp[j] = mw.back(), myp[j] = my.back(); + } + rms_norm_many(mxp, mwp, myp, 3, kEmbd, rows, kEps, stream()); + for (int j = 0; j < 3; ++j) { + rms_norm(mx[(size_t) j], mw[(size_t) j], my2[(size_t) j], kEmbd, rows, kEps, stream()); + same_bits(fmt("rms_norm_many %d rows, input %d = rms_norm()", rows, j), my[(size_t) j].get(), + my2[(size_t) j].get()); + } + } // ink_gate_norm_q8() const auto cur = randv(kEmbd, 1.0f), msg = randv(kEmbd, 1.0f), g = randv(kEmbd, 1.0f), f = randv(kEmbd, 1.0f), w = randv(kEmbd, 0.2f, 1.0f); diff --git a/engine/src/poly/kernels.cu b/engine/src/poly/kernels.cu index 256f269c10..215e1bebc9 100644 --- a/engine/src/poly/kernels.cu +++ b/engine/src/poly/kernels.cu @@ -413,13 +413,21 @@ struct RmsMany { float* y[3]; }; +// The input's pointers are picked by comparisons, not by m.x[blockIdx.y]: indexed by a register, the parameters +// were copied to local memory by every block, and the pointers read from there made the loads generic ones that +// do not go through the read-only cache. That took 236 ns a row on the RTX 2070 Super where rms_norm_kernel took +// 85. The sums are the same. +template +__device__ __forceinline__ T rms_pick(const T (&p)[3], int j) { + return j == 0 ? p[0] : j == 1 ? p[1] : p[2]; +} + __global__ void rms_norm_many_kernel(RmsMany m, int n, float eps) { __shared__ float part[32]; - const float* __restrict__ x = m.x[blockIdx.y]; - const float* __restrict__ w = m.w[blockIdx.y]; - float* y = m.y[blockIdx.y]; - const float* xr = x + size_t(blockIdx.x) * n; - float* yr = y + size_t(blockIdx.x) * n; + const int j = blockIdx.y; + const float* __restrict__ xr = rms_pick(m.x, j) + size_t(blockIdx.x) * n; + const float* __restrict__ w = rms_pick(m.w, j); + float* yr = rms_pick(m.y, j) + size_t(blockIdx.x) * n; float ss = 0.0f; for (int i = threadIdx.x; i < n; i += blockDim.x) ss += xr[i] * xr[i]; ss = block_sum(ss, part);