perf(gdn): exact-T gdn_ab_multi, double-buffered gdn_step_norm_multi with an all-outputs path, and the gdn_parity T=1..8 memcmp check (#783 PR-b)
gdn_ab_multi is instantiated per T=1..6 (generic above) with the weight unpack hoisted out of the token loop; gdn_step_norm_multi prefetches the next token's q/k while the current one runs, and when no state is written and every token is an output (the verify half) it skips the per-token bookkeeping. gdn_step_norm_kernel splits its reduction buffer so one barrier goes. gdn_parity gains section 5: for every T = 1..8, gdn_ab_multi against fused_gdn_ab, and gdn_step_norm_multi (verify half, commit half, t_out_begin, state-only commit) against the single-token kernel chained, compared with memcmp. Ported from #783 (stuchapin909, head 4327807), split per piece. Co-authored-by: stuchapin909 <178119976+stuchapin909@users.noreply.github.com>
This commit is contained in:
1 parent
1690a4a38b
commit
ae3b249fd2
3 files changed
+202
-59
No files matched your search
@@ -21,7 +21,8 @@ __global__ void __launch_bounds__(S * RG) gdn_step_norm_kernel(float* __restrict
|
||||
const float* __restrict__ gamma, float eps,
|
||||
float* __restrict__ y, int h_k, int h_v) {
|
||||
__shared__ float sk[S], sq[S];
|
||||
__shared__ float red[RG][S];
|
||||
__shared__ float red_kv[RG][S];
|
||||
__shared__ float red_o[RG][S];
|
||||
__shared__ float wsum[S * RG / 32];
|
||||
const int head = blockIdx.x;
|
||||
const int col = threadIdx.x; // 0..127
|
||||
@@ -39,9 +40,9 @@ __global__ void __launch_bounds__(S * RG) gdn_step_norm_kernel(float* __restrict
|
||||
float kv = 0.0f;
|
||||
#pragma unroll
|
||||
for (int r = 0; r < RPG; ++r) kv = fmaf(s[r], sk[rg * RPG + r], kv);
|
||||
red[rg][col] = kv;
|
||||
red_kv[rg][col] = kv;
|
||||
__syncthreads();
|
||||
const float kv_col = red[0][col] + red[1][col] + red[2][col] + red[3][col];
|
||||
const float kv_col = red_kv[0][col] + red_kv[1][col] + red_kv[2][col] + red_kv[3][col];
|
||||
const float delta = (v[head * S + col] - g * kv_col) * beta[head];
|
||||
float o = 0.0f;
|
||||
#pragma unroll
|
||||
@@ -50,12 +51,11 @@ __global__ void __launch_bounds__(S * RG) gdn_step_norm_kernel(float* __restrict
|
||||
o = fmaf(s[r], sq[rg * RPG + r], o);
|
||||
base[r * row_stride] = s[r];
|
||||
}
|
||||
__syncthreads(); // every thread has read red[] for kv_col
|
||||
red[rg][col] = o;
|
||||
red_o[rg][col] = o;
|
||||
__syncthreads();
|
||||
float oc = 0.0f, sq_part = 0.0f;
|
||||
if (rg == 0) {
|
||||
oc = (red[0][col] + red[1][col] + red[2][col] + red[3][col]) * rsqrtf((float) S);
|
||||
oc = (red_o[0][col] + red_o[1][col] + red_o[2][col] + red_o[3][col]) * rsqrtf((float) S);
|
||||
sq_part = oc * oc;
|
||||
}
|
||||
// RMS over the head's 128 outputs: warps of row group 0 are threads 0..127.
|
||||
|
||||
@@ -74,7 +74,7 @@ __global__ void gdn_conv_commit_kernel(float* __restrict__ hist, const float* __
|
||||
hist[c * 3 + 2] = seq[2];
|
||||
}
|
||||
|
||||
template <int MAX_T = kVerifyMaxT>
|
||||
template <int MAX_T = kVerifyMaxT, bool EXACT_T = false>
|
||||
__global__ void __launch_bounds__(64) gdn_ab_multi_kernel(const float* __restrict__ x, const uint16_t* __restrict__ wa,
|
||||
const uint16_t* __restrict__ wb,
|
||||
const float* __restrict__ dt,
|
||||
@@ -90,24 +90,29 @@ __global__ void __launch_bounds__(64) gdn_ab_multi_kernel(const float* __restric
|
||||
for (int t = 0; t < MAX_T; ++t) acc[t] = 0.0f;
|
||||
for (int j = lane; j < n / 8; j += 32) {
|
||||
const uint4 wv = __ldg(w4 + j);
|
||||
const float w0 = __uint_as_float(wv.x << 16), w1 = __uint_as_float(wv.x & 0xffff0000u);
|
||||
const float w2 = __uint_as_float(wv.y << 16), w3 = __uint_as_float(wv.y & 0xffff0000u);
|
||||
const float w4f = __uint_as_float(wv.z << 16), w5 = __uint_as_float(wv.z & 0xffff0000u);
|
||||
const float w6 = __uint_as_float(wv.w << 16), w7 = __uint_as_float(wv.w & 0xffff0000u);
|
||||
#pragma unroll
|
||||
for (int t = 0; t < MAX_T; ++t) {
|
||||
if (t >= T) break;
|
||||
if (!EXACT_T && t >= T) break;
|
||||
const float* xt = x + (size_t) t * n;
|
||||
const float4 xa = *reinterpret_cast<const float4*>(xt + j * 8);
|
||||
const float4 xb = *reinterpret_cast<const float4*>(xt + j * 8 + 4);
|
||||
const float4 xa = __ldg(reinterpret_cast<const float4*>(xt + j * 8));
|
||||
const float4 xb = __ldg(reinterpret_cast<const float4*>(xt + j * 8 + 4));
|
||||
float a = acc[t];
|
||||
a = fmaf(__uint_as_float(wv.x << 16), xa.x, a); a = fmaf(__uint_as_float(wv.x & 0xffff0000u), xa.y, a);
|
||||
a = fmaf(__uint_as_float(wv.y << 16), xa.z, a); a = fmaf(__uint_as_float(wv.y & 0xffff0000u), xa.w, a);
|
||||
a = fmaf(__uint_as_float(wv.z << 16), xb.x, a); a = fmaf(__uint_as_float(wv.z & 0xffff0000u), xb.y, a);
|
||||
a = fmaf(__uint_as_float(wv.w << 16), xb.z, a); a = fmaf(__uint_as_float(wv.w & 0xffff0000u), xb.w, a);
|
||||
a = fmaf(w0, xa.x, a); a = fmaf(w1, xa.y, a);
|
||||
a = fmaf(w2, xa.z, a); a = fmaf(w3, xa.w, a);
|
||||
a = fmaf(w4f, xb.x, a); a = fmaf(w5, xb.y, a);
|
||||
a = fmaf(w6, xb.z, a); a = fmaf(w7, xb.w, a);
|
||||
acc[t] = a;
|
||||
}
|
||||
}
|
||||
#pragma unroll
|
||||
for (int t = 0; t < MAX_T; ++t) {
|
||||
if (t >= T) break;
|
||||
if (!EXACT_T && t >= T) break;
|
||||
float a = acc[t];
|
||||
#pragma unroll
|
||||
for (int o = 16; o > 0; o >>= 1) a += __shfl_xor_sync(0xffffffffu, a, o);
|
||||
if (lane != 0) continue;
|
||||
if (is_beta) {
|
||||
@@ -182,7 +187,7 @@ __device__ __forceinline__ void gdn_q8_1_store(GdnQ81* __restrict__ xq, size_t i
|
||||
if (idx % 32 == 0) xq[idx / 32].ds = make_half2(d, sum);
|
||||
}
|
||||
|
||||
template<bool Q>
|
||||
template <bool ALL_OUT, bool Q>
|
||||
__global__ void __launch_bounds__(S * RG) gdn_step_norm_multi_kernel(float* __restrict__ state,
|
||||
const float* __restrict__ hbuf, int C,
|
||||
const float* __restrict__ gate,
|
||||
@@ -192,75 +197,78 @@ __global__ void __launch_bounds__(S * RG) gdn_step_norm_multi_kernel(float* __re
|
||||
float* __restrict__ y, int h_k, int h_v, int T,
|
||||
const int32_t* __restrict__ n_keep, int t_out_begin,
|
||||
GdnQ81* __restrict__ xq) {
|
||||
__shared__ float sk[S], sq[S];
|
||||
__shared__ float red[RG][S];
|
||||
__shared__ float wsum[S * RG / 32];
|
||||
__shared__ float sk[2][S], sq[2][S];
|
||||
__shared__ float red_kv[RG][S];
|
||||
__shared__ float red_o[RG][S];
|
||||
__shared__ float wsum[S / 32];
|
||||
const int head = blockIdx.x;
|
||||
const int col = threadIdx.x;
|
||||
const int rg = threadIdx.y;
|
||||
const int tid = rg * S + col;
|
||||
const int qh = head % h_k;
|
||||
const int qk = S * h_k; // q at [0, qk), k at [qk, 2qk), v at [2qk, ...)
|
||||
const int value_dim = S * h_v;
|
||||
const int n = n_keep ? *n_keep : T;
|
||||
const int n = ALL_OUT ? T : (n_keep ? *n_keep : T);
|
||||
if (n > 0) {
|
||||
if (rg == 0) sk[0][col] = hbuf[qk + qh * S + col];
|
||||
else if (rg == 1 && (ALL_OUT || 0 >= t_out_begin)) sq[0][col] = hbuf[qh * S + col];
|
||||
}
|
||||
const float gam = (rg == 0) ? gamma[col] : 0.0f;
|
||||
float s[RPG];
|
||||
float* base = state + ((size_t) (rg * RPG) * h_v + head) * S + col;
|
||||
const size_t row_stride = (size_t) h_v * S;
|
||||
#pragma unroll
|
||||
for (int r = 0; r < RPG; ++r) s[r] = base[r * row_stride];
|
||||
for (int r = 0; r < RPG; ++r) s[r] = ALL_OUT ? __ldg(&base[r * row_stride]) : base[r * row_stride];
|
||||
for (int t = 0; t < n; ++t) {
|
||||
const int cur = t & 1, nxt = (t + 1) & 1;
|
||||
const float* ht = hbuf + (size_t) t * C;
|
||||
const bool need_out = (t >= t_out_begin);
|
||||
__syncthreads(); // the previous token is done with sk/sq/red/wsum
|
||||
if (tid < S) {
|
||||
sk[tid] = ht[qk + qh * S + tid];
|
||||
if (need_out) sq[tid] = ht[qh * S + tid];
|
||||
}
|
||||
const bool need_out = ALL_OUT || (t >= t_out_begin);
|
||||
__syncthreads();
|
||||
if (t + 1 < n) {
|
||||
const float* ht_next = ht + C;
|
||||
const bool need_next = ALL_OUT || (t + 1 >= t_out_begin);
|
||||
if (rg == 0) sk[nxt][col] = ht_next[qk + qh * S + col];
|
||||
else if (rg == 1 && need_next) sq[nxt][col] = ht_next[qh * S + col];
|
||||
}
|
||||
const float g = __expf(gate[(size_t) t * h_v + head]);
|
||||
float kv = 0.0f;
|
||||
#pragma unroll
|
||||
for (int r = 0; r < RPG; ++r) kv = fmaf(s[r], sk[rg * RPG + r], kv);
|
||||
red[rg][col] = kv;
|
||||
for (int r = 0; r < RPG; ++r) kv = fmaf(s[r], sk[cur][rg * RPG + r], kv);
|
||||
red_kv[rg][col] = kv;
|
||||
__syncthreads();
|
||||
const float kv_col = red[0][col] + red[1][col] + red[2][col] + red[3][col];
|
||||
const float kv_col = red_kv[0][col] + red_kv[1][col] + red_kv[2][col] + red_kv[3][col];
|
||||
const float delta = (ht[2 * qk + head * S + col] - g * kv_col) * beta[(size_t) t * h_v + head];
|
||||
if (!need_out) {
|
||||
#pragma unroll
|
||||
for (int r = 0; r < RPG; ++r) s[r] = fmaf(g, s[r], sk[rg * RPG + r] * delta);
|
||||
for (int r = 0; r < RPG; ++r) s[r] = fmaf(g, s[r], sk[cur][rg * RPG + r] * delta);
|
||||
continue;
|
||||
}
|
||||
float o = 0.0f;
|
||||
#pragma unroll
|
||||
for (int r = 0; r < RPG; ++r) {
|
||||
s[r] = fmaf(g, s[r], sk[rg * RPG + r] * delta);
|
||||
o = fmaf(s[r], sq[rg * RPG + r], o);
|
||||
s[r] = fmaf(g, s[r], sk[cur][rg * RPG + r] * delta);
|
||||
o = fmaf(s[r], sq[cur][rg * RPG + r], o);
|
||||
}
|
||||
red_o[rg][col] = o;
|
||||
__syncthreads();
|
||||
red[rg][col] = o;
|
||||
__syncthreads();
|
||||
float oc = 0.0f, sq_part = 0.0f;
|
||||
float oc = 0.0f;
|
||||
if (rg == 0) {
|
||||
oc = (red[0][col] + red[1][col] + red[2][col] + red[3][col]) * rsqrtf((float) S);
|
||||
sq_part = oc * oc;
|
||||
oc = (red_o[0][col] + red_o[1][col] + red_o[2][col] + red_o[3][col]) * rsqrtf((float) S);
|
||||
float sq_part = oc * oc;
|
||||
#pragma unroll
|
||||
for (int o2 = 16; o2 > 0; o2 >>= 1) sq_part += __shfl_xor_sync(0xffffffffu, sq_part, o2);
|
||||
if ((col & 31) == 0) wsum[col >> 5] = sq_part;
|
||||
}
|
||||
for (int o2 = 16; o2 > 0; o2 >>= 1) sq_part += __shfl_xor_sync(0xffffffffu, sq_part, o2);
|
||||
if ((tid & 31) == 0) wsum[tid >> 5] = sq_part;
|
||||
__syncthreads();
|
||||
if (rg == 0) {
|
||||
const float ss = wsum[0] + wsum[1] + wsum[2] + wsum[3];
|
||||
const float scale = rsqrtf(ss / (float) S + eps);
|
||||
const float zz = z[(size_t) t * value_dim + head * S + col];
|
||||
if constexpr (Q) {
|
||||
const float yv = oc * scale * gamma[col] * (1.0f / (1.0f + __expf(-zz)));
|
||||
y[(size_t) t * value_dim + head * S + col] = yv;
|
||||
gdn_q8_1_store(xq, (size_t) (t - t_out_begin) * value_dim + head * S + col, yv);
|
||||
} else {
|
||||
y[(size_t) t * value_dim + head * S + col] = oc * scale * gamma[col] * (1.0f / (1.0f + __expf(-zz)));
|
||||
}
|
||||
const float yv = oc * scale * gam * (1.0f / (1.0f + __expf(-zz)));
|
||||
y[(size_t) t * value_dim + head * S + col] = yv;
|
||||
if constexpr (Q) gdn_q8_1_store(xq, (size_t) (t - t_out_begin) * value_dim + head * S + col, yv);
|
||||
}
|
||||
}
|
||||
if (n_keep != nullptr && n > 0) {
|
||||
if (!ALL_OUT && n_keep != nullptr && n > 0) {
|
||||
#pragma unroll
|
||||
for (int r = 0; r < RPG; ++r) base[r * row_stride] = s[r];
|
||||
}
|
||||
@@ -871,12 +879,15 @@ void gdn_ab_multi(const float* x, const uint16_t* w_alpha, const uint16_t* w_bet
|
||||
std::exit(1);
|
||||
}
|
||||
const unsigned blocks = (unsigned) ((2 * h_v + 1) / 2);
|
||||
if (n_tok <= 4) {
|
||||
gdn_ab_multi_kernel<4><<<blocks, 64, 0, (cudaStream_t) stream>>>(
|
||||
x, w_alpha, w_beta, dt, ssm_a, gate, beta, n_embd, h_v, n_tok);
|
||||
} else {
|
||||
gdn_ab_multi_kernel<kVerifyMaxT><<<blocks, 64, 0, (cudaStream_t) stream>>>(
|
||||
x, w_alpha, w_beta, dt, ssm_a, gate, beta, n_embd, h_v, n_tok);
|
||||
cudaStream_t st = (cudaStream_t) stream;
|
||||
switch (n_tok) {
|
||||
case 1: gdn_ab_multi_kernel<1, true><<<blocks, 64, 0, st>>>(x, w_alpha, w_beta, dt, ssm_a, gate, beta, n_embd, h_v, n_tok); break;
|
||||
case 2: gdn_ab_multi_kernel<2, true><<<blocks, 64, 0, st>>>(x, w_alpha, w_beta, dt, ssm_a, gate, beta, n_embd, h_v, n_tok); break;
|
||||
case 3: gdn_ab_multi_kernel<3, true><<<blocks, 64, 0, st>>>(x, w_alpha, w_beta, dt, ssm_a, gate, beta, n_embd, h_v, n_tok); break;
|
||||
case 4: gdn_ab_multi_kernel<4, true><<<blocks, 64, 0, st>>>(x, w_alpha, w_beta, dt, ssm_a, gate, beta, n_embd, h_v, n_tok); break;
|
||||
case 5: gdn_ab_multi_kernel<5, true><<<blocks, 64, 0, st>>>(x, w_alpha, w_beta, dt, ssm_a, gate, beta, n_embd, h_v, n_tok); break;
|
||||
case 6: gdn_ab_multi_kernel<6, true><<<blocks, 64, 0, st>>>(x, w_alpha, w_beta, dt, ssm_a, gate, beta, n_embd, h_v, n_tok); break;
|
||||
default: gdn_ab_multi_kernel<kVerifyMaxT, false><<<blocks, 64, 0, st>>>(x, w_alpha, w_beta, dt, ssm_a, gate, beta, n_embd, h_v, n_tok); break;
|
||||
}
|
||||
check("gdn_ab_multi");
|
||||
}
|
||||
@@ -918,12 +929,20 @@ void gdn_step_norm_multi(float* state, const float* h, int conv_channels, const
|
||||
check("gdn_step_commit");
|
||||
return;
|
||||
}
|
||||
if (xq && t_out_begin < n_tok)
|
||||
gdn_step_norm_multi_kernel<true><<<(unsigned) h_v, dim3(S, RG), 0, (cudaStream_t) stream>>>(
|
||||
const dim3 g1((unsigned) h_v), b1(S, RG);
|
||||
const bool all_out = n_keep == nullptr && t_out_begin <= 0;
|
||||
if (xq && t_out_begin < n_tok) {
|
||||
if (all_out) gdn_step_norm_multi_kernel<true, true><<<g1, b1, 0, (cudaStream_t) stream>>>(
|
||||
state, h, conv_channels, gate, beta, z, gamma, eps, y, h_k, h_v, n_tok, nullptr, 0, xq);
|
||||
else gdn_step_norm_multi_kernel<false, true><<<g1, b1, 0, (cudaStream_t) stream>>>(
|
||||
state, h, conv_channels, gate, beta, z, gamma, eps, y, h_k, h_v, n_tok, n_keep, t_out_begin, xq);
|
||||
else
|
||||
gdn_step_norm_multi_kernel<false><<<(unsigned) h_v, dim3(S, RG), 0, (cudaStream_t) stream>>>(
|
||||
} else if (all_out) {
|
||||
gdn_step_norm_multi_kernel<true, false><<<g1, b1, 0, (cudaStream_t) stream>>>(
|
||||
state, h, conv_channels, gate, beta, z, gamma, eps, y, h_k, h_v, n_tok, nullptr, 0, nullptr);
|
||||
} else {
|
||||
gdn_step_norm_multi_kernel<false, false><<<g1, b1, 0, (cudaStream_t) stream>>>(
|
||||
state, h, conv_channels, gate, beta, z, gamma, eps, y, h_k, h_v, n_tok, n_keep, t_out_begin, nullptr);
|
||||
}
|
||||
check("gdn_step_norm_multi");
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,8 @@
|
||||
// reference: (S, h_v, S) with j fastest instead of (S, S, h_v). A layout mix-up is silent, so the state is
|
||||
// filled with a value that encodes its own coordinates.
|
||||
#include "strata/kernels/gdn.hpp"
|
||||
#include "strata/kernels/fused_gdn.hpp"
|
||||
#include "strata/kernels/verify_kernels.hpp"
|
||||
|
||||
#include <cuda_runtime.h>
|
||||
|
||||
@@ -390,6 +392,128 @@ int main(int argc, char** argv) {
|
||||
cudaFree(d_o); cudaFree(d_z); cudaFree(d_sn); cudaFree(d_y);
|
||||
}
|
||||
|
||||
// ================= 5. the verify-window kernels, T = 1..8, bitwise against the single-token kernels =================
|
||||
// gdn_ab_multi and gdn_step_norm_multi are documented as bitwise `fused_gdn_ab` / `fused_gdn_step_norm` per
|
||||
// token (verify_kernels.hpp). Every T from 1 to kVerifyMaxT is its own exact-T instantiation, so each is run,
|
||||
// and every output and the state are compared with memcmp (not a tolerance).
|
||||
{
|
||||
const int n_embd = 2560, qk = S * h_k, C = 2 * qk + S * h_v, vd = S * h_v;
|
||||
const float eps = 1e-6f;
|
||||
auto bf16_of = [](float f) {
|
||||
uint32_t u;
|
||||
std::memcpy(&u, &f, 4);
|
||||
return (uint16_t) (u >> 16);
|
||||
};
|
||||
std::vector<uint16_t> wa((size_t) h_v * n_embd), wb((size_t) h_v * n_embd);
|
||||
for (auto& w : wa) w = bf16_of(gauss(rng) * 0.05f);
|
||||
for (auto& w : wb) w = bf16_of(gauss(rng) * 0.05f);
|
||||
std::vector<float> dt((size_t) h_v), ssm_a((size_t) h_v), gamma((size_t) S);
|
||||
for (auto& x : dt) x = gauss(rng);
|
||||
for (auto& x : ssm_a) x = -(0.1f + (float) (rng() % 100) / 50.0f);
|
||||
for (auto& x : gamma) x = 1.0f + 0.1f * gauss(rng);
|
||||
std::vector<float> st0((size_t) S * h_v * S);
|
||||
for (auto& x : st0) x = gauss(rng) * 0.1f;
|
||||
|
||||
auto dev = [&](size_t bytes) {
|
||||
void* p = nullptr;
|
||||
check(cudaMalloc(&p, bytes), "m5");
|
||||
return p;
|
||||
};
|
||||
auto up = [&](const void* h, size_t bytes) {
|
||||
void* p = dev(bytes);
|
||||
check(cudaMemcpy(p, h, bytes, cudaMemcpyHostToDevice), "u5");
|
||||
return p;
|
||||
};
|
||||
auto down = [&](std::vector<float>& h, const void* d) {
|
||||
check(cudaMemcpy(h.data(), d, h.size() * 4, cudaMemcpyDeviceToHost), "d5");
|
||||
};
|
||||
auto d_wa = (uint16_t*) up(wa.data(), wa.size() * 2);
|
||||
auto d_wb = (uint16_t*) up(wb.data(), wb.size() * 2);
|
||||
auto d_dt = (float*) up(dt.data(), dt.size() * 4);
|
||||
auto d_sa = (float*) up(ssm_a.data(), ssm_a.size() * 4);
|
||||
auto d_gm = (float*) up(gamma.data(), gamma.size() * 4);
|
||||
int ab_bad = 0, sn_bad = 0, ab_n = 0, sn_n = 0;
|
||||
for (int T = 1; T <= strata::kernels::kVerifyMaxT; ++T) {
|
||||
// --- alpha/beta
|
||||
std::vector<float> x((size_t) T * n_embd);
|
||||
for (auto& v : x) v = gauss(rng);
|
||||
auto d_x = (float*) up(x.data(), x.size() * 4);
|
||||
auto d_g = (float*) dev((size_t) T * h_v * 4), d_b = (float*) dev((size_t) T * h_v * 4);
|
||||
auto d_gr = (float*) dev((size_t) T * h_v * 4), d_br = (float*) dev((size_t) T * h_v * 4);
|
||||
strata::kernels::gdn_ab_multi(d_x, d_wa, d_wb, d_dt, d_sa, d_g, d_b, n_embd, h_v, T, nullptr);
|
||||
for (int t = 0; t < T; ++t)
|
||||
strata::kernels::fused_gdn_ab(d_x + (size_t) t * n_embd, d_wa, d_wb, d_dt, d_sa, d_gr + (size_t) t * h_v,
|
||||
d_br + (size_t) t * h_v, n_embd, h_v, nullptr);
|
||||
std::vector<float> g((size_t) T * h_v), b(g.size()), gr(g.size()), br(g.size());
|
||||
down(g, d_g); down(b, d_b); down(gr, d_gr); down(br, d_br);
|
||||
++ab_n;
|
||||
if (std::memcmp(g.data(), gr.data(), g.size() * 4) != 0 || std::memcmp(b.data(), br.data(), b.size() * 4) != 0) {
|
||||
std::printf(" *** gdn_ab_multi T=%d differs from fused_gdn_ab (memcmp) ***\n", T);
|
||||
++ab_bad;
|
||||
}
|
||||
cudaFree(d_x); cudaFree(d_g); cudaFree(d_b); cudaFree(d_gr); cudaFree(d_br);
|
||||
|
||||
// --- the recurrence + output norm
|
||||
std::vector<float> h((size_t) T * C), gate((size_t) T * h_v), beta((size_t) T * h_v), z((size_t) T * vd);
|
||||
for (auto& v : h) v = gauss(rng) * 0.1f;
|
||||
for (auto& v : gate) v = -(0.5f + 3.0f * (float) (rng() % 100) / 100.0f);
|
||||
for (auto& v : beta) v = (float) (rng() % 100) / 100.0f;
|
||||
for (auto& v : z) v = gauss(rng);
|
||||
auto d_h = (float*) up(h.data(), h.size() * 4);
|
||||
auto d_gate = (float*) up(gate.data(), gate.size() * 4);
|
||||
auto d_beta = (float*) up(beta.data(), beta.size() * 4);
|
||||
auto d_z = (float*) up(z.data(), z.size() * 4);
|
||||
auto d_y = (float*) dev((size_t) T * vd * 4);
|
||||
auto d_yr = (float*) dev((size_t) T * vd * 4);
|
||||
auto d_st = (float*) dev(st0.size() * 4);
|
||||
auto d_sr = (float*) dev(st0.size() * 4);
|
||||
int32_t* d_nk = (int32_t*) dev(4);
|
||||
// the reference chain (the single-token kernel), n tokens, into d_sr / d_yr
|
||||
auto run_ref = [&](int n) {
|
||||
check(cudaMemcpy(d_sr, st0.data(), st0.size() * 4, cudaMemcpyHostToDevice), "r0");
|
||||
for (int t = 0; t < n; ++t)
|
||||
strata::kernels::fused_gdn_step_norm(d_sr, d_h + (size_t) t * C, d_h + (size_t) t * C + qk,
|
||||
d_h + (size_t) t * C + 2 * qk, d_gate + (size_t) t * h_v,
|
||||
d_beta + (size_t) t * h_v, d_z + (size_t) t * vd, d_gm, eps,
|
||||
d_yr + (size_t) t * vd, h_k, h_v, nullptr);
|
||||
};
|
||||
std::vector<float> y((size_t) T * vd), yr(y.size()), st(st0.size()), sr(st0.size());
|
||||
// (name, n_keep tokens or -1 = verify half, t_out_begin)
|
||||
struct Case { const char* name; int n_keep; int tb; };
|
||||
std::vector<Case> cases = {{"verify", -1, 0}, {"verify tb", -1, T > 1 ? T / 2 : 0}, {"commit", T, 0},
|
||||
{"commit tb", T, T / 2}, {"commit state only", T, T}};
|
||||
if (T > 1) cases.push_back({"commit n-1", T - 1, T - 1});
|
||||
for (const Case& c : cases) {
|
||||
check(cudaMemcpy(d_st, st0.data(), st0.size() * 4, cudaMemcpyHostToDevice), "s0");
|
||||
check(cudaMemset(d_y, 0, (size_t) T * vd * 4), "y0");
|
||||
check(cudaMemset(d_yr, 0, (size_t) T * vd * 4), "yr0");
|
||||
const int n = c.n_keep < 0 ? T : c.n_keep;
|
||||
if (c.n_keep >= 0) check(cudaMemcpy(d_nk, &c.n_keep, 4, cudaMemcpyHostToDevice), "nk");
|
||||
strata::kernels::gdn_step_norm_multi(d_st, d_h, C, d_gate, d_beta, d_z, d_gm, eps, d_y, h_k, h_v, T,
|
||||
c.n_keep >= 0 ? d_nk : nullptr, nullptr, c.tb);
|
||||
run_ref(n);
|
||||
down(y, d_y); down(yr, d_yr); down(st, d_st); down(sr, d_sr);
|
||||
bool ok = true;
|
||||
for (int t = c.tb; t < n; ++t)
|
||||
if (std::memcmp(y.data() + (size_t) t * vd, yr.data() + (size_t) t * vd, (size_t) vd * 4) != 0) ok = false;
|
||||
// the verify half leaves the state untouched; the commit half writes the state after n tokens
|
||||
const std::vector<float>& want_st = c.n_keep < 0 ? st0 : sr;
|
||||
if (std::memcmp(st.data(), want_st.data(), st.size() * 4) != 0) ok = false;
|
||||
++sn_n;
|
||||
if (!ok) {
|
||||
std::printf(" *** gdn_step_norm_multi T=%d '%s' differs from fused_gdn_step_norm (memcmp) ***\n", T, c.name);
|
||||
++sn_bad;
|
||||
}
|
||||
}
|
||||
cudaFree(d_h); cudaFree(d_gate); cudaFree(d_beta); cudaFree(d_z); cudaFree(d_y); cudaFree(d_yr); cudaFree(d_st);
|
||||
cudaFree(d_sr); cudaFree(d_nk);
|
||||
}
|
||||
cudaFree(d_wa); cudaFree(d_wb); cudaFree(d_dt); cudaFree(d_sa); cudaFree(d_gm);
|
||||
std::printf("\n %-42s %s (%d cases)\n", "gdn_ab_multi T=1..8 == fused_gdn_ab", ab_bad ? "*** NO ***" : "bitwise", ab_n);
|
||||
std::printf(" %-42s %s (%d cases)\n", "gdn_step_norm_multi T=1..8 == single-token", sn_bad ? "*** NO ***" : "bitwise", sn_n);
|
||||
bad += ab_bad + sn_bad;
|
||||
}
|
||||
|
||||
std::printf("\ngdn: %d failures\n", bad);
|
||||
if (bad) return 1;
|
||||
if (selftest) std::printf("gdn_parity OK\n");
|
||||
|
||||
Reference in new issue
Block a user