GDN: single-pass conv+SiLU+L2 (STRATA_GDN_CONVL2=2), output-only reduced-precision experiment mode (STRATA_GDN_TC_NP=2)

This commit is contained in:
Niko1221 committed 2026-10-30 05:12:40 +01:00
1 parent 1a865fb3db
commit d3af2d42ad
2 files changed
+75 -14

No files matched your search

+14 -13
View File
@@ -154,8 +154,9 @@ __device__ __forceinline__ void gtc_mma3_f16(float (&c)[4], const uint32_t (&ah)
gtc_mma_f16(c, ah, bl0, bl1);
gtc_mma_f16(c, ah, bh0, bh1);
}
// NP passes of a split product (experiment, STRATA_GDN_TC split kernels only): 3 = the three above (the default); 2 = lo.hi and
// hi.hi (the A operand split, B in f16 only); 1 = hi.hi (plain f16 operands, as the gfx11 WMMA path). B's lo image is not read for NP < 3.
// NP (experiment, STRATA_GDN_TC split kernels only): 3 = every product on the three passes above (the default); 2 = only the products
// that reach the OUTPUT alone (q.S and D.P) on one pass, the state's products (k.S, Y.T, the update) on three; 1 = every product on one pass
// (hi.hi, plain f16 operands as the gfx11 WMMA path). A single-pass product does not read its B operand's lo image.
template <int NP>
__device__ __forceinline__ void gtc_mmaN(float (&c)[4], const uint32_t (&ah)[4], const uint32_t (&al)[4], uint32_t bh0,
uint32_t bh1, uint32_t bl0, uint32_t bl1) {
@@ -202,7 +203,7 @@ __device__ __forceinline__ float4 gtc_lds128(const float4* p) {
// (tiles 2kk and 2kk + 1 of S^T are that step's A fragment), B = the f16 hi / lo images of k and q ([GCH][GTC_KP] in
// shared memory, rows = tokens) by ldmatrix: matrices (n-tile 2hf + (lane >> 4), k-half (lane >> 3) & 1), a lane's
// row is token 8 n-tile + (lane & 7). The accumulators come out as 2^(8 + es) times the products. 2 x 96 mma.
template <int NP = 3>
template <int NPK = 3, int NPQ = 3>
__device__ __forceinline__ void gtc_prod_state(const float (&s)[16][4], float sA, const uint16_t* kh, const uint16_t* kl,
const uint16_t* qh, const uint16_t* ql, float (&xt)[4][4], float (&qs)[4][4]) {
const int lane = threadIdx.x & 31;
@@ -219,13 +220,13 @@ __device__ __forceinline__ void gtc_prod_state(const float (&s)[16][4], float sA
const int o = off + 16 * hf * GTC_KP + 16 * kk;
uint32_t bh[4], bl[4] = {0u, 0u, 0u, 0u};
gtc_ldsm_x4(bh, kh + o);
if constexpr (NP == 3) gtc_ldsm_x4(bl, kl + o);
gtc_mmaN<NP>(xt[2 * hf], ah, al, bh[0], bh[1], bl[0], bl[1]);
gtc_mmaN<NP>(xt[2 * hf + 1], ah, al, bh[2], bh[3], bl[2], bl[3]);
if constexpr (NPK == 3) gtc_ldsm_x4(bl, kl + o);
gtc_mmaN<NPK>(xt[2 * hf], ah, al, bh[0], bh[1], bl[0], bl[1]);
gtc_mmaN<NPK>(xt[2 * hf + 1], ah, al, bh[2], bh[3], bl[2], bl[3]);
gtc_ldsm_x4(bh, qh + o);
if constexpr (NP == 3) gtc_ldsm_x4(bl, ql + o);
gtc_mmaN<NP>(qs[2 * hf], ah, al, bh[0], bh[1], bl[0], bl[1]);
gtc_mmaN<NP>(qs[2 * hf + 1], ah, al, bh[2], bh[3], bl[2], bl[3]);
if constexpr (NPQ == 3) gtc_ldsm_x4(bl, ql + o);
gtc_mmaN<NPQ>(qs[2 * hf], ah, al, bh[0], bh[1], bl[0], bl[1]);
gtc_mmaN<NPQ>(qs[2 * hf + 1], ah, al, bh[2], bh[3], bl[2], bl[3]);
}
}
}
@@ -540,7 +541,7 @@ gdn_tc_scan_kernel(float* __restrict__ state, const float* __restrict__ h, const
const float m = fmaxf(fmaxf(fmaxf(m8[0], m8[1]), fmaxf(m8[2], m8[3])), fmaxf(fmaxf(m8[4], m8[5]), fmaxf(m8[6], m8[7])));
const int es = gtc_scale_exp(gtc_warp_max(m));
float xt[4][4] = {}, qs[4][4] = {};
gtc_prod_state<NP>(s, gtc_pow2(es), bkh, bkl, bqh, bql, xt, qs);
gtc_prod_state<(NP == 1 ? 1 : 3), (NP == 3 ? 3 : 1)>(s, gtc_pow2(es), bkh, bkl, bqh, bql, xt, qs);
// (b) Y^T = beta_i (V^T - e'_i X^T), e'_i = exp(gamma_i) 2^-(8 + es): lane (gid, tig) holds tokens
// 8nt + 2tig and + 1, columns c0 + gid and + 8
const float eS = gtc_pow2(-(8 + es));
@@ -564,7 +565,7 @@ gdn_tc_scan_kernel(float* __restrict__ state, const float* __restrict__ h, const
for (int q = 0; q < 4; ++q) my4[q] = fmaxf(my4[q], fabsf(yt[nt][q]));
const int ey = gtc_scale_exp(gtc_warp_max(fmaxf(fmaxf(my4[0], my4[1]), fmaxf(my4[2], my4[3]))));
float dt[4][4] = {};
gtc_prod_dt<true, NP>(yt, gtc_pow2(ey), bth, btl, dt);
gtc_prod_dt<true, (NP == 1 ? 1 : 3)>(yt, gtc_pow2(ey), bth, btl, dt);
const float sYT = gtc_pow2(-ey) * ste[b];
#pragma unroll
for (int nt = 0; nt < 4; ++nt)
@@ -585,7 +586,7 @@ gdn_tc_scan_kernel(float* __restrict__ state, const float* __restrict__ h, const
const float e0 = gtc_lds128(bsc + 8 * nt + 2 * tig).y * eO, e1 = gtc_lds128(bsc + 8 * nt + 2 * tig + 1).y * eO;
ot[nt][0] = qs[nt][0] * e0; ot[nt][1] = qs[nt][1] * e1; ot[nt][2] = qs[nt][2] * e0; ot[nt][3] = qs[nt][3] * e1;
}
gtc_prod_dt<true, NP>(dt, sD, bph, bpl, ot);
gtc_prod_dt<true, (NP == 3 ? 3 : 1)>(dt, sD, bph, bpl, ot);
float zr[4][4] = {}, gm2[2] = {};
if constexpr (NORM) {
// the z values and gamma of this lane's elements are loaded now, so that their latency is spent in the barrier below
@@ -658,7 +659,7 @@ gdn_tc_scan_kernel(float* __restrict__ state, const float* __restrict__ h, const
const float f0 = gtc_lds128(bsc + 8 * nt + 2 * tig).z * sD, f1 = gtc_lds128(bsc + 8 * nt + 2 * tig + 1).z * sD;
dd[nt][0] = dt[nt][0] * f0; dd[nt][1] = dt[nt][1] * f1; dd[nt][2] = dt[nt][2] * f0; dd[nt][3] = dt[nt][3] * f1;
}
gtc_update_state<NP>(s, dd, bkh, bkl);
gtc_update_state<(NP == 1 ? 1 : 3)>(s, dd, bkh, bkl);
#pragma unroll
for (int j = 0; j < 16; ++j)
#pragma unroll
+61 -1
View File
@@ -398,6 +398,58 @@ __global__ void gdn_conv_l2_kernel(const float* __restrict__ hist, const float*
__syncthreads();
}
}
// STRATA_GDN_CONVL2=2: the conv, SiLU and q / k L2 norm in ONE pass, a warp per (head, run of CONVW_TOK tokens): a lane owns
// four consecutive channels of the head's 128 (a float4 each way), walks the tokens keeping the three previous inputs in
// registers, and the head's sum of squares is one 5-step warp reduction a token - no shared memory, no barrier, h written
// once, normalised. The conv + SiLU value is the expression of gdn_conv_tiled_kernel (same bits); the sum of squares is
// added in another order than gdn_l2_kernel's (FP32-level difference). The first three inputs of a run are re-read from
// qkv (L2 hits; 3 / CONVW_TOK extra reads). Bytes a token: 41 KB read, 41 KB written, against 114 KB for conv + l2.
constexpr int CONVW_TOK = 32;
__global__ void __launch_bounds__(128) gdn_conv_w_kernel(const float* __restrict__ hist, const float* __restrict__ qkv,
const float* __restrict__ w, float* __restrict__ h, int64_t T,
float eps) {
const int warp = threadIdx.x >> 5, lane = threadIdx.x & 31;
const int64_t tb = ((int64_t) blockIdx.y * 4 + warp) * CONVW_TOK;
if (tb >= T) return;
const int64_t te = tb + CONVW_TOK < T ? tb + CONVW_TOK : T;
const int c0 = blockIdx.x * 128 + lane * 4;
const bool norm = blockIdx.x < 2 * HK; // q / k heads
float wr[4][4];
#pragma unroll
for (int i = 0; i < 4; ++i)
#pragma unroll
for (int k = 0; k < 4; ++k) wr[i][k] = w[(c0 + i) * 4 + k];
float4 p0, p1, p2; // inputs t - 3, t - 2, t - 1
auto input = [&](int64_t t) -> float4 {
if (t >= 0) return *reinterpret_cast<const float4*>(qkv + t * C + c0);
return make_float4(hist[(c0 + 0) * 3 + (int) (t + 3)], hist[(c0 + 1) * 3 + (int) (t + 3)],
hist[(c0 + 2) * 3 + (int) (t + 3)], hist[(c0 + 3) * 3 + (int) (t + 3)]);
};
p0 = input(tb - 3); p1 = input(tb - 2); p2 = input(tb - 1);
#pragma unroll 4
for (int64_t t = tb; t < te; ++t) {
const float4 x = *reinterpret_cast<const float4*>(qkv + t * C + c0);
const float xv[4] = {x.x, x.y, x.z, x.w}, a0[4] = {p0.x, p0.y, p0.z, p0.w}, a1[4] = {p1.x, p1.y, p1.z, p1.w},
a2[4] = {p2.x, p2.y, p2.z, p2.w};
float o[4];
float sq = 0.0f;
#pragma unroll
for (int i = 0; i < 4; ++i) {
const float s = a0[i] * wr[i][0] + a1[i] * wr[i][1] + a2[i] * wr[i][2] + xv[i] * wr[i][3];
o[i] = s / (1.0f + __expf(-s));
sq = fmaf(o[i], o[i], sq);
}
if (norm) {
#pragma unroll
for (int m = 16; m > 0; m >>= 1) sq += __shfl_xor_sync(0xffffffffu, sq, m);
const float rn = rsqrtf(sq + eps);
#pragma unroll
for (int i = 0; i < 4; ++i) o[i] *= rn;
}
*reinterpret_cast<float4*>(h + t * C + c0) = make_float4(o[0], o[1], o[2], o[3]);
p0 = p1; p1 = p2; p2 = x;
}
}
// the history after the chunk: its last three inputs (the older history where the chunk is shorter than 3)
__global__ void gdn_conv_hist_kernel(float* __restrict__ hist, const float* __restrict__ qkv, int64_t T) {
const int c = blockIdx.x * blockDim.x + threadIdx.x;
@@ -2715,7 +2767,15 @@ void gdn_conv(float* history, const float* qkv, const float* conv_w, float* h, i
if (serial || T <= CONV_TILE) {
gdn_conv_kernel<<<C / 128, 128, 0, (cudaStream_t) stream>>>(history, qkv, conv_w, h, T);
} else {
static const bool fused = [] { const char* v = std::getenv("STRATA_GDN_CONVL2"); return v && std::atoi(v) != 0; }();
static const int fused_mode = [] { const char* v = std::getenv("STRATA_GDN_CONVL2"); return v ? std::atoi(v) : 0; }();
const bool fused = fused_mode != 0;
if (fused_mode >= 2) {
gdn_conv_w_kernel<<<dim3(C / 128, (unsigned) ((T + 4 * CONVW_TOK - 1) / (4 * CONVW_TOK))), 128, 0,
(cudaStream_t) stream>>>(history, qkv, conv_w, h, T, eps);
gdn_conv_hist_kernel<<<C / 128, 128, 0, (cudaStream_t) stream>>>(history, qkv, T);
check("gdn_conv");
return;
}
if (fused) {
gdn_conv_l2_kernel<<<dim3(C / 128, (unsigned) ((T + CONV_TILE - 1) / CONV_TILE)), 128, 0,
(cudaStream_t) stream>>>(history, qkv, conv_w, h, T, eps);