Files
leleka/crates/cuda-driver/src/gr_native_batch.cu
T
Pavlo RatushnyiandClaude Sonnet 5.5 7f896a8bb8 import: Original-literal source-port snapshot (frozen 2026-10-12 06:36, uncompiled together, no GPU run)
Snapshot of target/stra169-direct-port-integration/source-port (without target/), now the tracked base for the full Strata Rust port.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 14:38:46 +02:00

135 lines
5.7 KiB
Plaintext

// Literal imported Strata prefill GR helpers/kernels. Export-only adaptation.
// Root compiles this asset after integrated source port; placeholder PTX refuses.
#include <cuda_runtime.h>
#include <cuda_fp16.h>
#include <cstdint>
#include <cmath>
constexpr int N=2560, HC=4, D=N*HC, LR=320;
__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__ float block_sum(float v, float* sh) {
const int lane = threadIdx.x & 31, w = threadIdx.x >> 5;
v = warp_sum(v);
__syncthreads();
if (lane == 0) sh[w] = v;
__syncthreads();
const int nw = (blockDim.x + 31) >> 5;
float t = (threadIdx.x < nw) ? sh[threadIdx.x] : 0.0f;
if (w == 0) t = warp_sum(t);
if (threadIdx.x == 0) sh[0] = t;
__syncthreads();
return sh[0];
}
__device__ __forceinline__ uint16_t bf(float f) {
uint32_t u = __float_as_uint(f);
u += 0x7fffu + ((u >> 16) & 1u);
return (uint16_t) (u >> 16);
}
__device__ __forceinline__ uint16_t bf_lo(float f, uint16_t hi) { return bf(f - __uint_as_float((uint32_t) hi << 16)); }
__device__ __forceinline__ float sigm(float x) { return 1.0f / (1.0f + __expf(-x)); }
__device__ __forceinline__ uint16_t hf(float f) { return __half_as_ushort(__float2half_rn(f)); }
extern "C" __global__ void gr_norm_rs_kernel(const float* __restrict__ R, const float* __restrict__ w, float eps,
float* __restrict__ rs_out, uint16_t* __restrict__ xn16,
uint16_t* __restrict__ xn16_lo) {
__shared__ float sh[32];
const int64_t row = blockIdx.x; // t * 4 + c
const int c = (int) (row % HC);
const float* r = R + row * N;
float ss = 0.0f;
for (int d = threadIdx.x; d < N; d += blockDim.x) ss += r[d] * r[d];
const float rs = rsqrtf(block_sum(ss, sh) / (float) N + eps);
if (threadIdx.x == 0) rs_out[row] = rs;
for (int d = threadIdx.x; d < N; d += blockDim.x) {
const float v = r[d] * rs * w[c * N + d];
const uint16_t h = bf(v);
xn16[row * N + d] = h;
if (xn16_lo) xn16_lo[row * N + d] = bf_lo(v, h);
}
}
extern "C" __global__ void gr_silu_kernel(const float* __restrict__ lo, uint16_t* __restrict__ lo16, uint16_t* __restrict__ lo16_lo,
int64_t n) {
const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x;
if (i >= n) return;
const float x = lo[i] / (float) HC;
const float v = x / (1.0f + __expf(-x));
const uint16_t h = bf(v);
lo16[i] = h;
if (lo16_lo) lo16_lo[i] = bf_lo(v, h);
}
extern "C" __global__ void gr_mix_r_kernel(const float* __restrict__ R, const float* __restrict__ rs, const float* __restrict__ w,
const float* __restrict__ g, float* __restrict__ mixed, uint16_t* __restrict__ mixed16,
int64_t T, uint16_t* __restrict__ mixed_h, uint16_t* __restrict__ mixed16_lo) {
const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x;
if (i >= T * N) return;
const int64_t t = i / N, d = i % N;
float s = 0.0f;
#pragma unroll
for (int c = 0; c < HC; ++c) {
const int64_t j = t * D + c * N + d;
const float x = R[j] * rs[t * HC + c] * w[c * N + d]; // gr_norm_kernel's value, bit for bit
s = fmaf(x, sigm(g[j]), s);
}
s /= (float) HC;
mixed[i] = s;
if (mixed16) {
const uint16_t h = bf(s);
mixed16[i] = h;
if (mixed16_lo) mixed16_lo[i] = bf_lo(s, h);
}
if (mixed_h) mixed_h[i] = hf(s);
}
extern "C" __global__ void gr_write_kernel(float* __restrict__ R, const float* __restrict__ bo, const float* __restrict__ inj,
int64_t inj_ld, int64_t T) {
const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x;
if (i >= T * D) return;
const int64_t t = i / D, c = (i % D) / N, d = i % N;
R[i] = fmaf(bo[t * N + d], 2.0f * sigm(inj[t * inj_ld + c] / (float) HC), R[i]);
}
// Full ordered numerical guards. Original normalized/product arithmetic is
// recomputed in original order, without fabricating a host normalized bank.
// Integer exponent checking covers every actual F32 word; flags per real row.
extern "C" __global__ void gr_scan(const float* x, uint32_t* flags,
int64_t width, int64_t T, uint32_t stage) {
const int64_t i = (int64_t)blockIdx.x*blockDim.x+threadIdx.x;
if (i >= width*T) return;
if ((__float_as_uint(x[i]) & 0x7f800000u)==0x7f800000u)
atomicOr(flags+(i/width)*8+stage,1u);
}
extern "C" __global__ void gr_norm_scan(const float* R,const float* rs,const float* w,
uint32_t* flags,int64_t T) {
const int64_t i=(int64_t)blockIdx.x*blockDim.x+threadIdx.x;
if (i>=T*D) return;
const int64_t t=i/D,c=(i%D)/N,d=i%N;
const float x=R[i]*rs[t*HC+c]*w[c*N+d];
if ((__float_as_uint(x)&0x7f800000u)==0x7f800000u) atomicOr(flags+t*8,1u);
}
extern "C" __global__ void gr_silu_scan(const float* lo,uint32_t* flags,int64_t T) {
const int64_t i=(int64_t)blockIdx.x*blockDim.x+threadIdx.x;
if(i>=T*LR) return;
const float x=lo[i]/(float)HC;
const float v=x/(1.0f+__expf(-x));
if((__float_as_uint(v)&0x7f800000u)==0x7f800000u) atomicOr(flags+(i/LR)*8+2,1u);
}
extern "C" __global__ void gr_product_scan(const float* R,const float* rs,const float* w,
const float* g,uint32_t* flags,int64_t T) {
const int64_t i=(int64_t)blockIdx.x*blockDim.x+threadIdx.x;
if(i>=T*D) return;
const int64_t t=i/D,c=(i%D)/N,d=i%N;
const float x=R[i]*rs[t*HC+c]*w[c*N+d];
const float product=x*sigm(g[i]);
if((__float_as_uint(product)&0x7f800000u)==0x7f800000u) atomicOr(flags+t*8+4,1u);
}