Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions engine/src/poly/inkling2_test.cu
Original file line number Diff line number Diff line change
Expand Up @@ -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<Dev<float>> 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);
Expand Down
18 changes: 13 additions & 5 deletions engine/src/poly/kernels.cu
Original file line number Diff line number Diff line change
Expand Up @@ -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 <class T>
__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);
Expand Down
Loading