From aaa323fec51a9389731cc5c9d2671d6daeeea4c2 Mon Sep 17 00:00:00 2001 From: NoxestNoz Date: Wed, 7 Oct 2026 16:46:39 +0000 Subject: [PATCH] fused_gr: Volta runs gfx906's latency-hidden norm/up (STRATA_GR_FAST), which now also writes the QFUSE q8_1 tail The fast kernels build for CUDA; sm_70 defaults to them, other CUDA cards keep the plain ones. gr_up_fast_kernel ends with gr_q8_tail like gr_up_multi_kernel, so STRATA_QFUSE gets its q8_1 image from it too. In the engine (staged read) only the up projection changes: hc-read0's up 1.08 -> 0.96 ms per window on a V100-SXM2. fused_gr_bench bitwise, gr_multi_parity OK. --- src/kernels/cuda/fused_gr.cu | 51 ++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/src/kernels/cuda/fused_gr.cu b/src/kernels/cuda/fused_gr.cu index 210cf228..0085be18 100644 --- a/src/kernels/cuda/fused_gr.cu +++ b/src/kernels/cuda/fused_gr.cu @@ -23,6 +23,12 @@ constexpr int N = 2560; // n_embd constexpr int HC = 4; // streams constexpr int D = N * HC; // 10240 constexpr int LR = 320; // hc_lr +// the latency-hidden norm/up (gr_norm_fast_kernel / gr_up_fast_kernel, STRATA_GR_FAST): gfx906's, built for CUDA too +#if defined(STRATA_HIP_GFX906) || !defined(__HIPCC__) +#define STRATA_GR_FAST_BUILD 1 +#else +#define STRATA_GR_FAST_BUILD 0 +#endif constexpr int THREADS = 256; constexpr int WARPS = THREADS / 32; constexpr int DOWN_BLOCKS = LR / WARPS; // 40 blocks of 8 rows; one more for the inject rows @@ -741,6 +747,24 @@ __global__ void __launch_bounds__(THREADS) gr_down_staged_kernel(GrMulti m) { } } +// the current device is Volta (sm_70): the fast norm/up is its default +bool cur_dev_volta() { +#if defined(__HIPCC__) + return false; +#else + static int per_dev[64]; // 0 unknown, 1 no, 2 yes + int dev = 0; + if (cudaGetDevice(&dev) != cudaSuccess || dev < 0 || dev >= 64) return false; + if (!per_dev[dev]) { + int major = 0, minor = 0; + cudaDeviceGetAttribute(&major, cudaDevAttrComputeCapabilityMajor, dev); + cudaDeviceGetAttribute(&minor, cudaDevAttrComputeCapabilityMinor, dev); + per_dev[dev] = major == 7 && minor == 0 ? 2 : 1; + } + return per_dev[dev] == 2; +#endif +} + // The tokens a down kernel may carry in one launch on the current card, and the plain read's tile: the plain read // stages n_tok * TILEV floats (1280 on sm_75, 2560 elsewhere), staged two tiles of n_tok * H_TILE. The shared-memory // opt-in is set here once per device (a per-DEVICE setting: a layer split runs these kernels on two cards). @@ -829,7 +853,7 @@ int down_chunk(bool staged, int* tile_out) { // many tokens as fit the card, the up projection; the profile's stamps after the norm and after the down projection. // kHcPlain is the default read exactly as before (never main's opt-in STRATA_GR_V3 path, which fused_gr_read_multi // takes first). -#if defined(STRATA_HIP_GFX906) +#if STRATA_GR_FAST_BUILD __global__ void __launch_bounds__(THREADS) gr_norm_fast_kernel(GrMulti m); // below, with the AMD fast path __global__ void __launch_bounds__(THREADS) gr_up_fast_kernel(GrMulti m); } // namespace @@ -849,7 +873,7 @@ bool gr_down_max4() { void launch_multi(const GrMulti& m, int variant, cudaStream_t st, unsigned long long* stamp_buf, int stamp_i0) { const int n_tok = m.T; -#if defined(STRATA_HIP_GFX906) +#if STRATA_GR_FAST_BUILD // gfx906: the latency-hidden norm/up (STRATA_GR_FAST=0: off) - the same sums in the same order as the kernels // they replace (gr_parity checks the multi-token read against single-token calls bitwise) const bool fast = gr_fast(); @@ -905,7 +929,7 @@ void launch_multi(const GrMulti& m, int variant, cudaStream_t st, unsigned long } } if (stamp_buf) gpu_stamp(stamp_buf, stamp_i0 + 1, (void*) st); -#if defined(STRATA_HIP_GFX906) +#if STRATA_GR_FAST_BUILD if (fast) gr_up_fast_kernel<<>>(m); else #endif @@ -933,7 +957,7 @@ int env_variant() { std::atomic g_variant[64]; -#if defined(STRATA_HIP_GFX906) +#if STRATA_GR_FAST_BUILD // ---- AMD fast path (STRATA_GR_FAST, default 1; 0 = the old kernels): the same arithmetic per value in the same order, latency // hidden. gr_norm: one block per token (as before) but a thread's 10 float4 of R / bo / w_norm are loaded // before any is used, and xn stays in registers until rs is known (was: store, then re-read the 40 KB). @@ -1001,7 +1025,11 @@ __global__ void __launch_bounds__(THREADS) gr_norm_fast_kernel(GrMulti m) { // idle lanes on the tail chunks. A block's 64 rows are 2 passes of 32 groups, their weights loaded up front. __device__ __forceinline__ float xor8(float v) { #pragma unroll +#if defined(__HIPCC__) for (int o = 4; o > 0; o >>= 1) v += __shfl_xor(v, o, 64); +#else + for (int o = 4; o > 0; o >>= 1) v += __shfl_xor_sync(0xffffffffu, v, o); +#endif return v; } __global__ void __launch_bounds__(THREADS) gr_up_fast_kernel(GrMulti m) { @@ -1064,6 +1092,7 @@ __global__ void __launch_bounds__(THREADS) gr_up_fast_kernel(GrMulti m) { for (int c = 0; c < HC; ++c) s += g[k][c][col]; m.a[k].mixed[d0 + col] = s / (float) HC; } + if (m.a[0].q8_mixed != nullptr) gr_q8_tail(m, d0); // S26 STRATA_QFUSE, as gr_up_multi_kernel } #endif @@ -1607,11 +1636,17 @@ void launch_q8(const GrMulti& m, float* scratch, cudaStream_t st, unsigned long } // namespace -#if defined(STRATA_HIP_GFX906) -int g_gr_fast = -1; // fused_gr_set_fast (the bench); -1 = STRATA_GR_FAST +#if STRATA_GR_FAST_BUILD +int g_gr_fast = -1; // fused_gr_set_fast (the bench); -1 = STRATA_GR_FAST, else the card's default static bool gr_fast() { - static const bool env = [] { const char* v = std::getenv("STRATA_GR_FAST"); return v ? std::atoi(v) != 0 : true; }(); - return g_gr_fast >= 0 ? g_gr_fast != 0 : env; + static const int env = [] { const char* v = std::getenv("STRATA_GR_FAST"); return v ? (std::atoi(v) != 0 ? 1 : 0) : -1; }(); + if (g_gr_fast >= 0) return g_gr_fast != 0; + if (env >= 0) return env != 0; +#if defined(__HIPCC__) + return true; +#else + return cur_dev_volta(); // CUDA: on Volta (V100-SXM2: 50.9 -> 46.7 us a read at T 1, bitwise); elsewhere opt-in +#endif } #else int g_gr_fast = -1;