diff --git a/include/strata/prefill/gdn_probe.hpp b/include/strata/prefill/gdn_probe.hpp index 905bdf54..2c58143a 100644 --- a/include/strata/prefill/gdn_probe.hpp +++ b/include/strata/prefill/gdn_probe.hpp @@ -21,7 +21,9 @@ enum GdnPath : int { kGdnChunked = 4, // STRATA_GDN_CHUNKED: FP32 chunked prep + scan (CUDA, C = 32) kGdnTc = 5, // STRATA_GDN_TC=1: chunked, scan on the tensor cores (CUDA sm_80+, 125 KB of shared memory a block) kGdnTcSplit = 6, // the same scan with v / T / P single-buffered: 98 KB, fits the 99-KB cards (sm_86, sm_89, sm_120) - kGdnPathCount = 7 + kGdnTcS2 = 7, // experiment: the split scan with 2 passes a product (lo.hi + hi.hi), not for production + kGdnTcS1 = 8, // experiment: the split scan with plain f16 operands (1 pass), not for production + kGdnPathCount = 9 }; struct GdnProbe { @@ -48,6 +50,8 @@ inline const char* gdn_path_name(int p) { case kGdnChunked: return "chunked"; case kGdnTc: return "tc"; case kGdnTcSplit: return "tcs"; + case kGdnTcS2: return "tcs2"; + case kGdnTcS1: return "tcs1"; case kGdnAuto: return "auto"; default: return "none"; } diff --git a/src/prefill/gdn_tc.cuh b/src/prefill/gdn_tc.cuh index 54fb7ecf..eda8c354 100644 --- a/src/prefill/gdn_tc.cuh +++ b/src/prefill/gdn_tc.cuh @@ -51,6 +51,9 @@ static_assert(kTcScanSmem == 124944 && kTcScanSmem == 160784, "gdn_ // 97,808 B against the 101,376 B opt-in of those cards (the full plan needs 124,944) constexpr size_t kTcScanSmemSplit = 2 * (4 * GCH * GTC_KP * 2 + GCH * 16) + GCH * GTC_VP * 4 + 4 * GCH * GTC_PP * 2 + 16; static_assert(kTcScanSmemSplit == 97808, "gdn_tc.cuh: the split shared-memory plan"); +// + the fused output norm's partial sums of squares [GTC_CW warps][GCH tokens] (SPLIT only): 98,832 B +constexpr size_t kTcScanSmemSplitNorm = kTcScanSmemSplit + GTC_CW * GCH * 4; +static_assert(kTcScanSmemSplitNorm == 98832, "gdn_tc.cuh: the split + norm shared-memory plan"); // the prep's scratch for a super-block (gdn_tc_scratch, one a device, 60.3 MB): the T images x 2^et as // [chunk][vh][2: hi, lo][GCH][GCH] f16 and their scales 2^-et as [chunk][vh], the token scalars as float4 // [chunk][vh][GCH], the k and q images x 2^8 as [chunk][qh][4: k hi, k lo, q hi, q lo][GCH][S] and the P images x 2^8 @@ -151,6 +154,19 @@ __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. +template +__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) { + if constexpr (NP == 3) { + gtc_mma_f16(c, al, bh0, bh1); + gtc_mma_f16(c, ah, bl0, bl1); + } else if constexpr (NP == 2) { + gtc_mma_f16(c, al, bh0, bh1); + } + gtc_mma_f16(c, ah, bh0, bh1); +} // x0, x1 -> packed f16 hi (x0 in the low half) and lo = f16(x - hi): x - hi is exact in f32 (hi carries x's exponent) __device__ __forceinline__ void gtc_split_f16x2(float x0, float x1, uint32_t& hi, uint32_t& lo) { union { __half2 h; uint32_t u; } cv; @@ -186,6 +202,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 __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; @@ -200,15 +217,15 @@ __device__ __forceinline__ void gtc_prod_state(const float (&s)[16][4], float sA #pragma unroll for (int hf = 0; hf < 2; ++hf) { const int o = off + 16 * hf * GTC_KP + 16 * kk; - uint32_t bh[4], bl[4]; + uint32_t bh[4], bl[4] = {0u, 0u, 0u, 0u}; gtc_ldsm_x4(bh, kh + o); - gtc_ldsm_x4(bl, kl + o); - gtc_mma3_f16(xt[2 * hf], ah, al, bh[0], bh[1], bl[0], bl[1]); - gtc_mma3_f16(xt[2 * hf + 1], ah, al, bh[2], bh[3], bl[2], bl[3]); + if constexpr (NP == 3) gtc_ldsm_x4(bl, kl + o); + gtc_mmaN(xt[2 * hf], ah, al, bh[0], bh[1], bl[0], bl[1]); + gtc_mmaN(xt[2 * hf + 1], ah, al, bh[2], bh[3], bl[2], bl[3]); gtc_ldsm_x4(bh, qh + o); - gtc_ldsm_x4(bl, ql + o); - gtc_mma3_f16(qs[2 * hf], ah, al, bh[0], bh[1], bl[0], bl[1]); - gtc_mma3_f16(qs[2 * hf + 1], ah, al, bh[2], bh[3], bl[2], bl[3]); + if constexpr (NP == 3) gtc_ldsm_x4(bl, ql + o); + gtc_mmaN(qs[2 * hf], ah, al, bh[0], bh[1], bl[0], bl[1]); + gtc_mmaN(qs[2 * hf + 1], ah, al, bh[2], bh[3], bl[2], bl[3]); } } } @@ -216,7 +233,7 @@ __device__ __forceinline__ void gtc_prod_state(const float (&s)[16][4], float sA // of P x 2^8 ([GCH][GTC_PP], rows = tokens t) by ldmatrix as in gtc_prod_state. SKIP leaves out the tiles above P's // diagonal: tokens t < 16 (n-tiles 0, 1) see no i >= 16 (k-step 1). 18 (SKIP) or 24 mma. D^T += (sY Y^T) (2^et T^T) // is the same product on the T images (T is zero above its diagonal too), the accumulator x 2^(ey + et). -template +template __device__ __forceinline__ void gtc_prod_dt(const float (&dt)[4][4], float sD, const uint16_t* ph, const uint16_t* pl, float (&ot)[4][4]) { const int lane = threadIdx.x & 31; @@ -232,17 +249,18 @@ __device__ __forceinline__ void gtc_prod_dt(const float (&dt)[4][4], float sD, c for (int hf = 0; hf < 2; ++hf) { if (SKIP && ks == 1 && hf == 0) continue; const int o = off + 16 * hf * GTC_PP + 16 * ks; - uint32_t bh[4], bl[4]; + uint32_t bh[4], bl[4] = {0u, 0u, 0u, 0u}; gtc_ldsm_x4(bh, ph + o); - gtc_ldsm_x4(bl, pl + o); - gtc_mma3_f16(ot[2 * hf], ah, al, bh[0], bh[1], bl[0], bl[1]); - gtc_mma3_f16(ot[2 * hf + 1], ah, al, bh[2], bh[3], bl[2], bl[3]); + if constexpr (NP == 3) gtc_ldsm_x4(bl, pl + o); + gtc_mmaN(ot[2 * hf], ah, al, bh[0], bh[1], bl[0], bl[1]); + gtc_mmaN(ot[2 * hf + 1], ah, al, bh[2], bh[3], bl[2], bl[3]); } } } // S^T += Dd^T (2^8 K) on f16x3: A = the (already scaled) Dd^T tiles split per k16-step, B = the same k images read the // other way by ldmatrix.trans: matrices (n-tile 2hf + (lane >> 4), k-half (lane >> 3) & 1), a lane's row is token // 16 ks + 8 k-half + (lane & 7), its 16-B piece state rows 8 n-tile.. 96 mma. +template __device__ __forceinline__ void gtc_update_state(float (&s)[16][4], const float (&dd)[4][4], const uint16_t* kh, const uint16_t* kl) { const int lane = threadIdx.x & 31; @@ -257,11 +275,11 @@ __device__ __forceinline__ void gtc_update_state(float (&s)[16][4], const float #pragma unroll for (int hf = 0; hf < S / 16; ++hf) { const int o = off + 16 * ks * GTC_KP + 16 * hf; - uint32_t bh[4], bl[4]; + uint32_t bh[4], bl[4] = {0u, 0u, 0u, 0u}; gtc_ldsm_x4_trans(bh, kh + o); - gtc_ldsm_x4_trans(bl, kl + o); - gtc_mma3_f16(s[2 * hf], ah, al, bh[0], bh[1], bl[0], bl[1]); - gtc_mma3_f16(s[2 * hf + 1], ah, al, bh[2], bh[3], bl[2], bl[3]); + if constexpr (NP == 3) gtc_ldsm_x4_trans(bl, kl + o); + gtc_mmaN(s[2 * hf], ah, al, bh[0], bh[1], bl[0], bl[1]); + gtc_mmaN(s[2 * hf + 1], ah, al, bh[2], bh[3], bl[2], bl[3]); } } } @@ -419,12 +437,17 @@ __global__ void __launch_bounds__(256) gdn_tc_prep_kernel(const float* __restric // 8 warps, 2 a sub-partition, may have 255). // NORM (step 5: the output norm fused, the FP16 output written here) is not implemented yet; oc_out takes the FP32 // output as gdn_chunk_scan_kernel writes it. -template +template __global__ void __launch_bounds__(GTC_CW * 32, 1) gdn_tc_scan_kernel(float* __restrict__ state, const float* __restrict__ h, const uint16_t* __restrict__ tig_, const float* __restrict__ teg, const float4* __restrict__ scg, const uint16_t* __restrict__ kqg, - const uint16_t* __restrict__ pig, float* __restrict__ oc_out, int64_t t0, int nch, int64_t T) { - static_assert(!NORM, "gdn_tc_scan_kernel: the fused output norm is not implemented yet"); + const uint16_t* __restrict__ pig, float* __restrict__ oc_out, int64_t t0, int nch, int64_t T, + const float* __restrict__ zg = nullptr, const float* __restrict__ gamma = nullptr, float eps = 0.0f, + uint16_t* __restrict__ y16 = nullptr, int64_t ld16 = 0) { + // NORM (SPLIT only): the output norm of gdn_out_norm_kernel fused: the scan writes the FP16 y16 = o rsqrt(mean(o^2) + eps) gamma + // sigmoid(z) itself (the sums of squares across the block's 8 warps through shared memory), and not the FP32 o: one pass less + // over the 24 KB-a-token output. Another summation order than the kernel's 4 partials (not bitwise). + static_assert(!NORM || SPLIT, "gdn_tc_scan_kernel: the fused output norm is implemented for the split plan"); constexpr int NT = GTC_CW * 32; extern __shared__ __align__(16) unsigned char gtc_sm[]; uint16_t* kh = reinterpret_cast(gtc_sm); // [2][GCH][GTC_KP] f16 of k x 2^8 (hi) @@ -439,6 +462,7 @@ gdn_tc_scan_kernel(float* __restrict__ state, const float* __restrict__ h, const uint16_t* pl = ph + NV * GCH * GTC_PP; float4* sc = reinterpret_cast(pl + NV * GCH * GTC_PP); // [2][GCH] (beta, exp(gamma), exp(gL - gamma), gamma) float* ste = reinterpret_cast(sc + 2 * GCH); // [2] 2^-et + float* rss = ste + 2; // NORM: [GTC_CW][GCH] partial sums of squares const int vh = blockIdx.x, qhd = vh % HK; const int tid = threadIdx.x, warp = tid >> 5, lane = tid & 31, gid = lane >> 2, tig = lane & 3; const size_t rs = (size_t) HV * S; @@ -516,7 +540,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(s, gtc_pow2(es), bkh, bkl, bqh, bql, xt, qs); + gtc_prod_state(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)); @@ -540,7 +564,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(yt, gtc_pow2(ey), bth, btl, dt); + gtc_prod_dt(yt, gtc_pow2(ey), bth, btl, dt); const float sYT = gtc_pow2(-ey) * ste[b]; #pragma unroll for (int nt = 0; nt < 4; ++nt) @@ -561,18 +585,66 @@ 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(dt, sD, bph, bpl, ot); + gtc_prod_dt(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 #pragma unroll - for (int nt = 0; nt < 4; ++nt) + for (int nt = 0; nt < 4; ++nt) #pragma unroll - for (int q = 0; q < 4; ++q) { - const int t = 8 * nt + 2 * tig + (q & 1); - if (t < n) oc_out[(c_t0 + t) * rs + (size_t) vh * S + c0 + gid + 8 * (q >> 1)] = (ot[nt][q] * sOi) * out_scale; - } + for (int q = 0; q < 4; ++q) { + const int t = 8 * nt + 2 * tig + (q & 1); + if (t < n) zr[nt][q] = zg[(c_t0 + t) * rs + (size_t) vh * S + c0 + gid + 8 * (q >> 1)]; + } + gm2[0] = gamma[c0 + gid]; gm2[1] = gamma[c0 + gid + 8]; + // this lane's two columns of each of its 8 tokens -> partial sums over the warp's 16 columns (the 8 gid lanes), 8 warps in rss +#pragma unroll + for (int nt = 0; nt < 4; ++nt) +#pragma unroll + for (int q = 0; q < 4; ++q) ot[nt][q] = (ot[nt][q] * sOi) * out_scale; +#pragma unroll + for (int nt = 0; nt < 4; ++nt) +#pragma unroll + for (int e = 0; e < 2; ++e) { + float ps = ot[nt][e] * ot[nt][e] + ot[nt][e + 2] * ot[nt][e + 2]; + ps += __shfl_xor_sync(0xffffffffu, ps, 4); + ps += __shfl_xor_sync(0xffffffffu, ps, 8); + ps += __shfl_xor_sync(0xffffffffu, ps, 16); + if (gid == 0) rss[warp * GCH + 8 * nt + 2 * tig + e] = ps; + } + } else { +#pragma unroll + for (int nt = 0; nt < 4; ++nt) +#pragma unroll + for (int q = 0; q < 4; ++q) { + const int t = 8 * nt + 2 * tig + (q & 1); + if (t < n) oc_out[(c_t0 + t) * rs + (size_t) vh * S + c0 + gid + 8 * (q >> 1)] = (ot[nt][q] * sOi) * out_scale; + } + } if (SPLIT) { // v, T and P of this chunk are dead: every warp is past (d); the next chunk's land during (e) __syncthreads(); if (ch + 1 < nch) stage(ch + 1, 0, 2); } + if constexpr (NORM) { +#pragma unroll + for (int nt = 0; nt < 4; ++nt) +#pragma unroll + for (int e = 0; e < 2; ++e) { + const int t = 8 * nt + 2 * tig + e; + if (t >= n) continue; + float ss = 0.0f; +#pragma unroll + for (int w = 0; w < GTC_CW; ++w) ss += rss[w * GCH + t]; + const float rn = rsqrtf(ss / (float) S + eps); +#pragma unroll + for (int hc = 0; hc < 2; ++hc) { + const int col = c0 + gid + 8 * hc; + const float zz = zr[nt][e + 2 * hc]; + const float v = ot[nt][e + 2 * hc] * rn * gm2[hc] * (1.0f / (1.0f + __expf(-zz))); + y16[(c_t0 + t) * ld16 + (size_t) vh * S + col] = __half_as_ushort(__float2half_rn(v)); + } + } + } // (e) S^T = eL S0^T + Dd^T K, Dd^T = f_i D^T: the accumulator x (eL 2^(ed + 8)) before (one rounding, as the // s *= gL of gdn_chunk_scan_kernel), Dd^T in x 2^ed, K x 2^8, the accumulator x 2^-(ed + 8) after const float sPre = gtc_lds128(bsc + GCH - 1).y * sO; // exp(gL) @@ -586,7 +658,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(s, dd, bkh, bkl); + gtc_update_state(s, dd, bkh, bkl); #pragma unroll for (int j = 0; j < 16; ++j) #pragma unroll diff --git a/src/prefill/kernels.cu b/src/prefill/kernels.cu index 7c10bdbf..5eb8d6a1 100644 --- a/src/prefill/kernels.cu +++ b/src/prefill/kernels.cu @@ -1227,8 +1227,16 @@ cudaError_t gdn_rec_chunked(float* state, const float* h, const float* gate, con // STRATA_GDN_TC=1: gdn_rec_chunked with the scan on the tensor cores (gdn_tc_scan_kernel, gdn_tc.cuh) and the prep // that also writes its f16 images (gdn_tc_prep_kernel, scratch gdn_tc_scratch), 48 blocks of 256 threads in one wave. An error return means nothing was launched: the card is below // sm_80, has fewer than 48 SMs, or less than 122 KB of shared memory a block. +// the output norm's operands, when the split scan fuses it (see gdn_tc_scan_kernel NORM) +struct GdnTcNorm { + const float* z; + const float* gamma; + float eps; + uint16_t* y16; + int64_t ld16; +}; cudaError_t gdn_rec_tc(float* state, const float* h, const float* gate, const float* beta, float* y, int64_t T, - cudaStream_t s, bool split = false) { + cudaStream_t s, bool split = false, int np = 3, const GdnTcNorm* nm = nullptr) { if (T <= 0) return cudaSuccess; { int dev = 0, major = 0, sms = 0, smem = 0; @@ -1242,25 +1250,44 @@ cudaError_t gdn_rec_tc(float* state, const float* h, const float* gate, const fl const GdnProbe* pr = gdn_probe_get(); // tests only: fewer SMs than value heads run the 48 blocks in several waves const bool any_sms = pr != nullptr && pr->any_sms; if (strata::cc_major_of(major) < 8 || (!any_sms && sms < HV) || - (size_t) strata::smem_optin_of(smem) < (split ? kTcScanSmemSplit : kTcScanSmem)) + (size_t) strata::smem_optin_of(smem) < (split ? (nm ? kTcScanSmemSplitNorm : kTcScanSmemSplit) : kTcScanSmem)) return cudaErrorNotSupported; } const GtcScratch sc = gdn_tc_scratch(); if (sc.ti == nullptr) return cudaErrorMemoryAllocation; { // once a device - static bool attrs[2][64] = {}; + static bool attrs[3][64] = {}; + const int ai = nm && split ? 2 : (split ? 1 : 0); int dev = 0; cudaGetDevice(&dev); - if (dev >= 0 && dev < 64 && !attrs[split][dev]) { - if (split) cudaFuncSetAttribute(gdn_tc_scan_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, (int) kTcScanSmemSplit); + if (dev >= 0 && dev < 64 && !attrs[ai][dev]) { + if (ai == 2) { + cudaFuncSetAttribute(gdn_tc_scan_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, (int) kTcScanSmemSplitNorm); + cudaFuncSetAttribute(gdn_tc_scan_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, (int) kTcScanSmemSplitNorm); + cudaFuncSetAttribute(gdn_tc_scan_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, (int) kTcScanSmemSplitNorm); + } else if (split) { + cudaFuncSetAttribute(gdn_tc_scan_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, (int) kTcScanSmemSplit); + cudaFuncSetAttribute(gdn_tc_scan_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, (int) kTcScanSmemSplit); + cudaFuncSetAttribute(gdn_tc_scan_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, (int) kTcScanSmemSplit); + } else cudaFuncSetAttribute(gdn_tc_scan_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, (int) kTcScanSmem); - attrs[split][dev] = true; + attrs[ai][dev] = true; } } for (int64_t t0 = 0; t0 < T; t0 += GSB) { const int nch = (int) ((std::min(GSB, T - t0) + GCH - 1) / GCH); gdn_tc_prep_kernel<<>>(h, gate, beta, sc.ti, sc.te, sc.sc, sc.kq, sc.pi, t0, T); - if (split) + if (nm && split) { +#define STRATA_TCN(NPV) gdn_tc_scan_kernel<<>>(state, h, sc.ti, sc.te, sc.sc, sc.kq, sc.pi, y, t0, nch, T, nm->z, nm->gamma, nm->eps, nm->y16, nm->ld16) + if (np == 2) STRATA_TCN(2); + else if (np == 1) STRATA_TCN(1); + else STRATA_TCN(3); +#undef STRATA_TCN + } else if (split && np == 2) + gdn_tc_scan_kernel<<>>(state, h, sc.ti, sc.te, sc.sc, sc.kq, sc.pi, y, t0, nch, T); + else if (split && np == 1) + gdn_tc_scan_kernel<<>>(state, h, sc.ti, sc.te, sc.sc, sc.kq, sc.pi, y, t0, nch, T); + else if (split) gdn_tc_scan_kernel<<>>(state, h, sc.ti, sc.te, sc.sc, sc.kq, sc.pi, y, t0, nch, T); else gdn_tc_scan_kernel<<, s>>>(state, h, sc.ti, sc.te, sc.sc, sc.kq, sc.pi, y, t0, nch, T); @@ -2808,6 +2835,7 @@ static void gdn_recurrence_impl(int variant, float* state, const float* h, const gdn_rec_kernel<<>>(state, h, gate, beta, z, gamma, eps, y, y16, T, ld16); } else { static const bool pipe = [] { const char* v = std::getenv("STRATA_GDN_PIPELINE"); return v == nullptr || std::atoi(v) != 0; }(); + bool normed_any = false; // the tensor-core scan wrote y16 (the norm kernel below is skipped) #if !defined(__HIPCC__) // STRATA_GDN_CHUNKED=1 (opt-in): the recurrence in chunks for prompt chunks of kGdnChunkedMin+ tokens (other // bits; a shorter chunk, a card it does not fit or a failed scratch allocation takes the kernels below) @@ -2821,13 +2849,19 @@ static void gdn_recurrence_impl(int variant, float* state, const float* h, const GdnProbe* pr = gdn_probe_get(); const int force = pr ? pr->force : (int) kGdnAuto; int sel = kGdnNone, wanted = force; + bool normed = false; // the scan wrote y16 itself const char* why = ""; const cudaStream_t cs = (cudaStream_t) stream; + // the split tensor-core scan can write y16 itself (STRATA_GDN_TC_FUSE=0 off): then the norm kernel below is skipped + static const bool tc_fuse = [] { const char* v = std::getenv("STRATA_GDN_TC_FUSE"); return v == nullptr || std::atoi(v) != 0; }(); + const GdnTcNorm tcn = {z, gamma, eps, y16, ld16}; + const GdnTcNorm* tcnp = with_out && tc_fuse ? &tcn : nullptr; if (force != kGdnAuto) { - if (force == kGdnTc || force == kGdnTcSplit) { - const bool sp = force == kGdnTcSplit; - cudaError_t e = T >= kGdnTcMin ? gdn_rec_tc(state, h, gate, beta, y, T, cs, sp) : cudaErrorInvalidValue; - if (e == cudaSuccess) sel = force; + if (force == kGdnTc || force == kGdnTcSplit || force == kGdnTcS2 || force == kGdnTcS1) { + const bool sp = force != kGdnTc; + const int np = force == kGdnTcS2 ? 2 : force == kGdnTcS1 ? 1 : 3; + cudaError_t e = T >= kGdnTcMin ? gdn_rec_tc(state, h, gate, beta, y, T, cs, sp, np, sp ? tcnp : nullptr) : cudaErrorInvalidValue; + if (e == cudaSuccess) { sel = force; normed = sp && tcnp != nullptr; } else why = T < kGdnTcMin ? "T below the tc minimum (128)" : gdn_chunk_why(true, e, false, sp); } else if (force == kGdnChunked) { cudaError_t e = T >= kGdnChunkedMin ? gdn_rec_chunked(state, h, gate, beta, y, T, cs) : cudaErrorInvalidValue; @@ -2852,8 +2886,10 @@ static void gdn_recurrence_impl(int variant, float* state, const float* h, const if (e == cudaSuccess) sel = kGdnTc; else { // the 99-KB cards (sm_86 / 89 / 120): the single-buffered v / T / P plan if (pr) why = gdn_chunk_why(true, e, false); - e = gdn_rec_tc(state, h, gate, beta, y, T, cs, true); - if (e == cudaSuccess) { sel = kGdnTcSplit; why = ""; } + // STRATA_GDN_TC_NP=1|2 (experiment, not a default): fewer split passes a product on this plan, other bits + static const int tc_np = [] { const char* v = std::getenv("STRATA_GDN_TC_NP"); const int x = v ? std::atoi(v) : 3; return x == 1 || x == 2 ? x : 3; }(); + e = gdn_rec_tc(state, h, gate, beta, y, T, cs, true, tc_np, tcnp); + if (e == cudaSuccess) { sel = kGdnTcSplit; why = ""; normed = tcnp != nullptr; } } } if (sel == kGdnNone && chunked && T >= kGdnChunkedMin) { @@ -2877,6 +2913,7 @@ static void gdn_recurrence_impl(int variant, float* state, const float* h, const } } if (pr) { pr->selected = sel; pr->wanted = wanted == kGdnAuto ? sel : wanted; pr->reason = why; ++pr->calls; } + normed_any = normed; if (sel == kGdnNone) { check("gdn_recurrence (forced path not eligible: nothing launched)"); return; } #else if (pipe) // the software-pipelined loads (same bits) @@ -2884,7 +2921,7 @@ static void gdn_recurrence_impl(int variant, float* state, const float* h, const else gdn_rec_cols_kernel<<>>(state, h, gate, beta, y, T); #endif - if (with_out) + if (with_out && !normed_any) gdn_out_norm_kernel<<>>(z, gamma, eps, y, y16, ld16); } check("gdn_recurrence");