diff --git a/engine/src/poly/inkling2_test.cu b/engine/src/poly/inkling2_test.cu index 7c26b0cf6..622e80bb0 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 256f269c1..215e1bebc 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);