Files
leleka/tools/chain-probe/gr_prof.cu
T

247 lines
10 KiB
Plaintext

// Instrumented copy of the chain GR down kernel (clock64 per phase of block 3); probe-only, never sealed.
// claude/chain-kernels: latency-hoisted, bit-identical variants of the verify-window kernels that sit on the decode chain
// (docs/chain-kernels.md). Every kernel here computes EVERY output with the same operations in the same order as the
// kernel it replaces (original_verify.cu, mtp_original_full_fused_gr.cu); only the issue order of independent loads, the
// block shape and the number of launches change. Selected at record time by LELEKA_CHAIN_* switches.
//
// Compile flags (same as the sources they derive from): -ptx -O3 -arch=sm_120 --fmad=true --ftz=false -std=c++17
#include <cuda_runtime.h>
#include <stdint.h>
#include <stddef.h>
#include <cuda_fp16.h>
#include <cfloat>
#include "strata/kernels/fused_gr.hpp"
#include "strata/kernels/sampler.hpp"
using strata::kernels::FusedGrArgs;
constexpr int kFusedGrMaxT = strata::kernels::kFusedGrMaxT;
namespace {
__device__ __forceinline__ void strata_spin_pause() {
__nanosleep(100);
}
__device__ __forceinline__ float warp_sum(float v) {
#pragma unroll
for (int o = 16; o > 0; o >>= 1) v += __shfl_xor_sync(0xffffffffu, v, o);
return v;
}
__device__ __forceinline__ float sigmoidf_(float x) { return 1.0f / (1.0f + __expf(-x)); }
// 8 bf16 packed in a uint4 against 8 floats (fused_gr.cu `dot8`).
__device__ __forceinline__ float dot8(const uint4 w, const float* x) {
float acc = 0.0f;
const uint32_t v[4] = {w.x, w.y, w.z, w.w};
#pragma unroll
for (int j = 0; j < 4; ++j) {
acc = fmaf(__uint_as_float(v[j] << 16), x[2 * j], acc);
acc = fmaf(__uint_as_float(v[j] & 0xffff0000u), x[2 * j + 1], acc);
}
return acc;
}
constexpr int N = 2560;
constexpr int HC = 4;
constexpr int D = N * HC;
constexpr int LR = 320;
constexpr int THREADS = 256;
constexpr int WARPS = THREADS / 32;
struct GrMulti {
FusedGrArgs a[kFusedGrMaxT];
float* xn;
int T;
};
} // namespace
static_assert(sizeof(FusedGrArgs) == 112, "Original FusedGrArgs ABI");
static_assert(sizeof(GrMulti) == 912, "Original GrMulti ABI");
namespace {
constexpr int H_TILE = 1280;
constexpr int N_HTILES = D / H_TILE; // 8
constexpr int HQ = H_TILE / 8 / 32; // 5 chunks per lane per tile
constexpr int DOWN_ROWS = LR + HC; // 320 down rows + 4 inject rows
__device__ __forceinline__ void cp_async16(void* smem, const void* gmem) {
const unsigned sa = (unsigned) __cvta_generic_to_shared(smem);
asm volatile("cp.async.cg.shared.global [%0], [%1], 16;\n" ::"r"(sa), "l"(gmem) : "memory");
}
__device__ __forceinline__ void cp_async_commit() { asm volatile("cp.async.commit_group;\n" ::: "memory"); }
template <int N>
__device__ __forceinline__ void cp_async_wait() { asm volatile("cp.async.wait_group %0;\n" ::"n"(N) : "memory"); }
__device__ __forceinline__ float dot8v(const uint4 w, const float4 x0, const float4 x1) {
float acc = 0.0f;
acc = fmaf(__uint_as_float(w.x << 16), x0.x, acc);
acc = fmaf(__uint_as_float(w.x & 0xffff0000u), x0.y, acc);
acc = fmaf(__uint_as_float(w.y << 16), x0.z, acc);
acc = fmaf(__uint_as_float(w.y & 0xffff0000u), x0.w, acc);
acc = fmaf(__uint_as_float(w.z << 16), x1.x, acc);
acc = fmaf(__uint_as_float(w.z & 0xffff0000u), x1.y, acc);
acc = fmaf(__uint_as_float(w.w << 16), x1.z, acc);
acc = fmaf(__uint_as_float(w.w & 0xffff0000u), x1.w, acc);
return acc;
}
template <int W>
__device__ __forceinline__ void stage_tile(const GrMulti& m, int T, int h, float4* buf, int t) {
for (int i = t; i < T * (H_TILE / 4); i += W * 32) {
const int k = i / (H_TILE / 4), s4 = i - k * (H_TILE / 4);
const float* src = m.xn + (size_t) k * D + (size_t) h * H_TILE + (size_t) s4 * 4;
cp_async16(buf + (size_t) k * (H_TILE / 4) + (s4 & 1) * (H_TILE / 8) + (s4 >> 1), src);
}
}
template <int W, int NB>
__device__ __forceinline__ void gr_down_body(const GrMulti& m, unsigned long long* st) {
const long long c0 = clock64();
extern __shared__ __align__(16) float4 hbuf[]; // NB x [T][2][160] float4
const int t = threadIdx.x, lane = t & 31, warp = t >> 5;
const int T = m.T;
const int row = blockIdx.x * W + warp; // 0..319 down rows, 320..323 inject rows
const bool inject = row >= LR;
const bool active = row < DOWN_ROWS && !(inject && m.a[0].w_inject == nullptr);
const uint16_t* wrow = (inject ? m.a[0].w_inject : m.a[0].w_down) + (size_t) (active ? (inject ? row - LR : row) : 0) * D;
const uint4* w4 = reinterpret_cast<const uint4*>(wrow);
const size_t buf_f4 = (size_t) T * (H_TILE / 4);
long long w_wait = 0, w_comp = 0, w_sync = 0;
float acc[kFusedGrMaxT];
#pragma unroll
for (int k = 0; k < kFusedGrMaxT; ++k) acc[k] = 0.0f;
uint4 wv[N_HTILES][HQ];
if (active) {
#pragma unroll
for (int h = 0; h < N_HTILES; ++h)
#pragma unroll
for (int q = 0; q < HQ; ++q) wv[h][q] = __ldg(w4 + h * (H_TILE / 8) + lane + 32 * q);
}
#pragma unroll
for (int b = 0; b < NB - 1; ++b) {
stage_tile<W>(m, T, b, hbuf + b * buf_f4, t);
cp_async_commit();
}
#pragma unroll
for (int h = 0; h < N_HTILES; ++h) {
const long long a0 = clock64();
cp_async_wait<NB - 2>();
__syncthreads();
const long long a1 = clock64(); w_wait += a1 - a0;
const float4* cur = hbuf + (h % NB) * buf_f4;
if (active) {
#pragma unroll
for (int q = 0; q < HQ; ++q) {
const int j = lane + 32 * q;
#pragma unroll
for (int k = 0; k < kFusedGrMaxT; ++k) {
if (k < T) {
const float4* pk = cur + (size_t) k * (H_TILE / 4);
acc[k] += dot8v(wv[h][q], pk[j], pk[H_TILE / 8 + j]);
}
}
}
}
const long long a2 = clock64(); w_comp += a2 - a1;
__syncthreads();
w_sync += clock64() - a2;
if (h + NB - 1 < N_HTILES) stage_tile<W>(m, T, h + NB - 1, hbuf + ((h + NB - 1) % NB) * buf_f4, t);
cp_async_commit();
}
if (blockIdx.x == 3 && threadIdx.x == 0) { st[0] = (unsigned long long)(clock64() - c0); st[1] = (unsigned long long)w_wait; st[2] = (unsigned long long)w_comp; st[3] = (unsigned long long)w_sync; }
if (!active) return;
float s[kFusedGrMaxT];
#pragma unroll
for (int k = 0; k < kFusedGrMaxT; ++k) s[k] = k < T ? warp_sum(acc[k]) : 0.0f;
#pragma unroll
for (int k = 0; k < kFusedGrMaxT; ++k) {
if (k >= T || lane != k) continue;
if (inject) {
m.a[k].inject_out[row - LR] = s[k];
} else {
const float x = s[k] / (float) HC;
m.a[k].lo[row] = x / (1.0f + __expf(-x));
}
}
}
// ---- diagnostics: the same kernel with the weight loads (MODE & 1) and/or the activation staging + shared reads (MODE & 2) removed
template <int W, int NB, int MODE>
__device__ __forceinline__ void gr_down_diag(const GrMulti& m) {
extern __shared__ __align__(16) float4 hbuf[];
const int t = threadIdx.x, lane = t & 31, warp = t >> 5;
const int T = m.T;
const int row = blockIdx.x * W + warp;
const bool inject = row >= LR;
const bool active = row < DOWN_ROWS && !(inject && m.a[0].w_inject == nullptr);
const uint16_t* wrow = (inject ? m.a[0].w_inject : m.a[0].w_down) + (size_t) (active ? (inject ? row - LR : row) : 0) * D;
const uint4* w4 = reinterpret_cast<const uint4*>(wrow);
const size_t buf_f4 = (size_t) T * (H_TILE / 4);
float acc[kFusedGrMaxT];
#pragma unroll
for (int k = 0; k < kFusedGrMaxT; ++k) acc[k] = 0.0f;
uint4 wv[N_HTILES][HQ];
if (active) {
#pragma unroll
for (int h = 0; h < N_HTILES; ++h)
#pragma unroll
for (int q = 0; q < HQ; ++q) {
if (MODE & 1) wv[h][q] = make_uint4(0x3f803f80u + h, 0x3f803f80u + q, 0x3f803f80u + lane, 0x3f803f80u);
else wv[h][q] = __ldg(w4 + h * (H_TILE / 8) + lane + 32 * q);
}
}
if (!(MODE & 2)) {
#pragma unroll
for (int b = 0; b < NB - 1; ++b) {
stage_tile<W>(m, T, b, hbuf + b * buf_f4, t);
cp_async_commit();
}
}
#pragma unroll
for (int h = 0; h < N_HTILES; ++h) {
if (!(MODE & 2)) {
cp_async_wait<NB - 2>();
__syncthreads();
}
const float4* cur = hbuf + (h % NB) * buf_f4;
if (active) {
#pragma unroll
for (int q = 0; q < HQ; ++q) {
const int j = lane + 32 * q;
#pragma unroll
for (int k = 0; k < kFusedGrMaxT; ++k) {
if (k < T) {
float4 x0, x1;
if (MODE & 2) { x0 = make_float4(1.0f + j, 2.0f + k, 3.0f, 4.0f + h); x1 = make_float4(0.5f, 0.25f + j, 0.125f, 1.5f + k); }
else { const float4* pk = cur + (size_t) k * (H_TILE / 4); x0 = pk[j]; x1 = pk[H_TILE / 8 + j]; }
acc[k] += dot8v(wv[h][q], x0, x1);
}
}
}
}
if (!(MODE & 2)) {
__syncthreads();
if (h + NB - 1 < N_HTILES) stage_tile<W>(m, T, h + NB - 1, hbuf + ((h + NB - 1) % NB) * buf_f4, t);
cp_async_commit();
}
}
if (!active) return;
float s[kFusedGrMaxT];
#pragma unroll
for (int k = 0; k < kFusedGrMaxT; ++k) s[k] = k < T ? warp_sum(acc[k]) : 0.0f;
#pragma unroll
for (int k = 0; k < kFusedGrMaxT; ++k) {
if (k >= T || lane != k) continue;
if (inject) m.a[k].inject_out[row - LR] = s[k];
else { const float x = s[k] / (float) HC; m.a[k].lo[row] = x / (1.0f + __expf(-x)); }
}
}
} // namespace
extern "C" __global__ void __launch_bounds__(256) gr_down_prof_w8_nb3(GrMulti m, unsigned long long* st) { gr_down_body<8, 3>(m, st); }
extern "C" __global__ void __launch_bounds__(256) gr_diag_m0(GrMulti m) { gr_down_diag<8, 3, 0>(m); }
extern "C" __global__ void __launch_bounds__(256) gr_diag_m1(GrMulti m) { gr_down_diag<8, 3, 1>(m); }
extern "C" __global__ void __launch_bounds__(256) gr_diag_m2(GrMulti m) { gr_down_diag<8, 3, 2>(m); }
extern "C" __global__ void __launch_bounds__(256) gr_diag_m3(GrMulti m) { gr_down_diag<8, 3, 3>(m); }