feat(cuda-driver): sealed prefill_stage PTX (chunk-wide PLE decode, embedding broadcast, GR/GDN kernels with folded guards, element-parallel dequant)

New precise-recipe asset (extra-seal-plan group prefill-stage, nvcc 13.2.86), sealed with compile_assets.py/seal_pins.py, with its sm_86 companion.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Pavlo Ratushnyi
2026-10-17 02:04:42 +02:00
co-authored by Claude Sonnet 5.5
parent 499faec1ae
commit c0190bdb6d
7 changed files with 7899 additions and 0 deletions
+20
View File
@@ -2142,6 +2142,26 @@
"sm86_bytes": 5198,
"sm86_sha256": "bfcd7c884462c1e1e28e3f73c7250fbba51bc0c95f037dba184f3224758faa33"
},
{
"entries": 17,
"isa": "9.2",
"method": "retarget",
"name": "prefill_stage.ptx",
"ptxas": "ok",
"rules": [
{
"count": 1,
"rule": "target"
}
],
"sm120_bytes": 112485,
"sm120_nvcc": "13.2.86",
"sm120_path": "crates/cuda-driver/src/prefill_stage.ptx",
"sm120_sha256": "3aacbef45fed661823f9500a568b0d8964ef6a206664ba6a42d374b3e2bb0106",
"sm120_stored_nul": false,
"sm86_bytes": 112484,
"sm86_sha256": "0cd1835dc83d3a3186f220439c6777a3a7e7048d8d09dffdbe011b1672d9553d"
},
{
"entries": 2,
"isa": "9.0",
File diff suppressed because it is too large Load Diff
+505
View File
@@ -0,0 +1,505 @@
// Offline compilation source; not a runtime/toolkit dependency.
// Batched Original-prompt input staging: the whole chunk's KAR PLE rows decoded by one launch, and the token
// embedding rows broadcast to the four residual streams on the device. Replaces per-token round trips
// (16-row scatter + 16-row decode + synchronous readback per token) and the host assembly/upload of the
// 10240-float residual row of every token.
//
// Arithmetic is exactly that of leleka_kar_ple_decode (offline_kar_ple_kernel.cu): the F16 book entry times the F16
// scale, both converted exactly, one binary32 multiply. The broadcast is a copy. Nothing here changes a value.
//
// Intended offline compile (the recipe of the other precise assets): pinned NVCC 13.2.86, -std=c++17 -O3 -ptx
// -arch=sm_120 --ftz=false --prec-div=true --prec-sqrt=true --fmad=true.
#include <cuda_fp16.h>
#include <stdint.h>
static __device__ __forceinline__ uint16_t u16le(const uint8_t* p) {
return uint16_t(p[0]) | (uint16_t(p[1]) << 8);
}
static __device__ __forceinline__ uint32_t u32le(const uint8_t* p) {
return uint32_t(p[0]) | (uint32_t(p[1]) << 8) | (uint32_t(p[2]) << 16) | (uint32_t(p[3]) << 24);
}
// Cache slot layout (64 bytes): 5 f16 scales at 0, two 5-word planes at 10 and 30, u16 partition at 50.
// Update record (56 bytes): u32 slot, 50 payload bytes, u16 partition. `count` records; every slot is distinct.
extern "C" __global__ __launch_bounds__(256) void leleka_prefill_ple_scatter(uint8_t* cache, const uint8_t* updates,
uint32_t count) {
const uint64_t i = uint64_t(blockIdx.x) * blockDim.x + threadIdx.x;
const uint64_t row = i / 64;
const uint32_t byte = uint32_t(i % 64);
if (row >= count) return;
const uint8_t* update = updates + row * 56;
const uint32_t slot = u32le(update);
if (slot >= 1048576u) return;
if (byte < 52) cache[uint64_t(slot) * 64 + byte] = update[4 + byte];
}
// `rows` PLE rows of 160 values: row r reads cache slot slots[r]. out = [rows][160] f32. A bad slot or partition, or a
// non-finite book/scale, writes NaN and raises flags[row * flag_stride + flag_word]=1 (the host refuses the chunk).
extern "C" __global__ __launch_bounds__(256) void leleka_prefill_ple_decode(
const uint8_t* cache, const uint32_t* slots, const uint16_t* books, float* out, uint32_t partition_count,
uint64_t rows, uint32_t* flags, uint32_t flag_stride, uint32_t flag_word) {
const uint64_t at = uint64_t(blockIdx.x) * blockDim.x + threadIdx.x;
if (at >= rows * 160) return;
const uint64_t row = at / 160;
const uint32_t value = uint32_t(at % 160);
const uint32_t slot = slots[row];
if (slot >= 1048576u || partition_count == 0 || partition_count > 65535) {
out[at] = __int_as_float(0x7fc00000);
flags[row * flag_stride + flag_word] = 1;
return;
}
const uint8_t* bytes = cache + uint64_t(slot) * 64;
const uint32_t part = u16le(bytes + 50);
if (part >= partition_count) {
out[at] = __int_as_float(0x7fc00000);
flags[row * flag_stride + flag_word] = 1;
return;
}
const uint32_t word = value / 32, bit = value % 32;
const uint32_t code = (((u32le(bytes + 10 + word * 4) >> bit) & 1) << 1) | ((u32le(bytes + 30 + word * 4) >> bit) & 1);
const uint16_t scale = u16le(bytes + word * 2), book = books[part * 4 + code];
if ((scale & 0x7c00) == 0x7c00 || (book & 0x7c00) == 0x7c00) {
out[at] = __int_as_float(0x7fc00000);
flags[row * flag_stride + flag_word] = 1;
return;
}
out[at] = __half2float(__ushort_as_half(book)) * __half2float(__ushort_as_half(scale));
}
// R[row] = four copies of the 2560-float embedding row uniq[index[row]] (the Original's gr_broadcast_kernel with the
// per-token row selected by an index, so a chunk uploads each distinct token's row once). An index outside the table
// writes NaN and raises flags[row * flag_stride + flag_word].
extern "C" __global__ __launch_bounds__(256) void leleka_prefill_embed_broadcast(
const float* uniq, const uint32_t* index, float* r, uint64_t rows, uint32_t unique_count, uint32_t* flags,
uint32_t flag_stride, uint32_t flag_word) {
const uint64_t at = uint64_t(blockIdx.x) * blockDim.x + threadIdx.x;
if (at >= rows * 2560) return;
const uint64_t row = at / 2560;
const uint32_t i = uint32_t(at % 2560);
const uint32_t u = index[row];
float v;
if (u < unique_count) {
v = uniq[uint64_t(u) * 2560 + i];
} else {
v = __int_as_float(0x7fc00000);
flags[row * flag_stride + flag_word] = 1;
}
float* dst = r + row * 10240 + i;
dst[0] = v;
dst[2560] = v;
dst[5120] = v;
dst[7680] = v;
}
// ====================================================================================================
// GR read / inject with the finite guards folded into the producing kernels.
//
// The originals (gr_prompt_device.cu, copied here arithmetic-for-arithmetic) are followed by separate guard
// kernels that re-read the same tensors (gr_norm_scan: R; gr_product_scan: R and the gated tensor; gr_scan of the
// gated tensor, of the mixed row and of R after a write). Each guard recomputes the value its producer just made.
// These kernels set the same per-row flag words from the producer's own registers, so a clean chunk is
// bit-identical to the original sequence (the produced values are the very same expressions) and the guards no
// longer cost a second pass over 335 MB tensors. Flag words: [row * 8 + stage] as gr_scan.
// ====================================================================================================
namespace {
constexpr int N = 2560, HC = 4, D = N * HC;
__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)); }
__device__ __forceinline__ bool nonfinite(float x) { return (__float_as_uint(x) & 0x7f800000u) == 0x7f800000u; }
} // namespace
// gr_norm_rs_kernel + gr_norm_scan (flag stage 0). `flags` null: no guard.
extern "C" __global__ void leleka_prefill_gr_norm_rs(const float* __restrict__ R, const float* __restrict__ w, float eps,
float* __restrict__ rs_out, uint16_t* __restrict__ xn16,
uint16_t* __restrict__ xn16_lo, uint32_t* __restrict__ flags) {
__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;
bool bad = false;
for (int d = threadIdx.x; d < N; d += blockDim.x) {
const float v = r[d] * rs * w[c * N + d];
bad |= nonfinite(v);
const uint16_t h = bf(v);
xn16[row * N + d] = h;
if (xn16_lo) xn16_lo[row * N + d] = bf_lo(v, h);
}
if (bad && flags) atomicOr(flags + (row / HC) * 8, 1u);
}
// gr_mix_r_kernel + gr_product_scan (stage 4) + gr_scan of the gated tensor (stage 3) + gr_scan of `mixed` (stage 5).
extern "C" __global__ void leleka_prefill_gr_mix_r(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, uint32_t* __restrict__ flags) {
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;
bool bad_gate = false, bad_product = false;
#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
const float gj = g[j];
const float sg = sigm(gj);
bad_gate |= nonfinite(gj);
bad_product |= nonfinite(x * sg); // gr_product_scan's `product`
s = fmaf(x, sg, 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);
if (flags) {
if (bad_gate) atomicOr(flags + t * 8 + 3, 1u);
if (bad_product) atomicOr(flags + t * 8 + 4, 1u);
if (nonfinite(s)) atomicOr(flags + t * 8 + 5, 1u);
}
}
// gr_write_norm_rs_kernel + gr_scan of R after the write (stage 7) + gr_norm_scan of the next half's norm (stage 0).
extern "C" __global__ void __launch_bounds__(256) leleka_prefill_gr_write_norm_rs(
float* __restrict__ R, const float* __restrict__ bo, const float* __restrict__ inj, int64_t inj_ld,
const float* __restrict__ w, float eps, float* __restrict__ rs_out, uint16_t* __restrict__ xn16,
uint16_t* __restrict__ xn16_lo, uint32_t* __restrict__ flags) {
constexpr int GRW_PER = (N + 255) / 256;
__shared__ float sh[32];
const int64_t row = blockIdx.x; // t * 4 + c
const int64_t t = row / HC;
const int c = (int) (row % HC);
float* r = R + row * N;
const float sc = 2.0f * sigm(inj[t * inj_ld + c] / (float) HC);
float v[GRW_PER];
float ss = 0.0f;
bool bad_r = false, bad_norm = false;
int k = 0;
#pragma unroll
for (int d = threadIdx.x; d < N; d += 256, ++k) {
const float x = fmaf(bo[t * N + d], sc, r[d]);
r[d] = x;
v[k] = x;
bad_r |= nonfinite(x);
ss += x * x;
}
const float rs = rsqrtf(block_sum(ss, sh) / (float) N + eps);
if (threadIdx.x == 0) rs_out[row] = rs;
k = 0;
#pragma unroll
for (int d = threadIdx.x; d < N; d += 256, ++k) {
const float x = v[k] * rs * w[c * N + d];
bad_norm |= nonfinite(x);
const uint16_t h = bf(x);
xn16[row * N + d] = h;
if (xn16_lo) xn16_lo[row * N + d] = bf_lo(x, h);
}
if (flags) {
if (bad_r) atomicOr(flags + t * 8 + 7, 1u);
if (bad_norm) atomicOr(flags + t * 8, 1u);
}
}
// gr_write_kernel + gr_scan of R after the write (stage 7).
extern "C" __global__ void leleka_prefill_gr_write(float* __restrict__ R, const float* __restrict__ bo,
const float* __restrict__ inj, int64_t inj_ld, int64_t T,
uint32_t* __restrict__ flags) {
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 = fmaf(bo[t * N + d], 2.0f * sigm(inj[t * inj_ld + c] / (float) HC), R[i]);
R[i] = x;
if (flags && nonfinite(x)) atomicOr(flags + t * 8 + 7, 1u);
}
// ====================================================================================================
// GDN prompt kernels (offline_gdn_prefill_kernel.cu / offline_native_dequant_f16_kernel.cu, the Original's
// kernels.cu / dequant_bf16.cu) with two changes that touch no value:
// * the dequantization runs one thread per ELEMENT instead of one thread per 32-element group (the original reads
// and writes 34/18/144... bytes per thread at a stride, ~6 GB/s on a laptop part); every element's arithmetic is the
// original `group32` expression for that element, so the FP16 weights are bit-identical;
// * the finite guards of the tensors a kernel reads or writes anyway (qkv, the convolution output, the normalised
// q/k, z, the readout, y and its FP16 image) are raised from the kernel's own registers instead of by one extra
// pass per tensor. The status words are the ones the separate guard kernels use.
// All launches are 1-D grids of 256 threads (KernelModule's contract); the 2-D / 128-thread geometries of the
// originals are folded into the block index and into two 128-thread groups per block, with the same per-group
// reduction order.
// ====================================================================================================
namespace {
constexpr int GS = 128, GHK = 16, GHV = 48, GC = 10240;
constexpr int CONV_TILE = 64;
__device__ __forceinline__ float h2f(const uint8_t* p) {
return __half2float(__ushort_as_half((uint16_t) (p[0] | (p[1] << 8))));
}
__constant__ int8_t kv_iq4nl[16] = {-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113};
__device__ __forceinline__ void scale_min_k4(int j, const uint8_t* q, int& d, int& m) {
if (j < 4) { d = q[j] & 63; m = q[j + 4] & 63; }
else { d = (q[j + 4] & 0xF) | ((q[j - 4] >> 6) << 4); m = (q[j + 4] >> 4) | ((q[j] >> 6) << 4); }
}
__device__ __forceinline__ uint16_t h16(float v) { return __half_as_ushort(__float2half_rn(v)); }
// Element `l` (0..31) of group `gi_in_row` of a row of blocks; the original's group32 body for that element.
template <int TYPE>
__device__ __forceinline__ uint16_t dq_element(const uint8_t* row_blocks, int gi_in_row, int l) {
if constexpr (TYPE == 8) { // Q8_0
const uint8_t* b = row_blocks + (size_t) gi_in_row * 34;
const float d = h2f(b);
return h16((float) (int8_t) b[2 + l] * d);
} else if constexpr (TYPE == 20) { // IQ4_NL
const uint8_t* b = row_blocks + (size_t) gi_in_row * 18;
const float d = h2f(b);
return h16(l < 16 ? d * (float) kv_iq4nl[b[2 + l] & 0xf] : d * (float) kv_iq4nl[b[2 + (l - 16)] >> 4]);
} else if constexpr (TYPE == 12) { // Q4_K
const uint8_t* b = row_blocks + (size_t) (gi_in_row / 8) * 144;
const int gi = gi_in_row % 8, j64 = gi / 2, hi = gi % 2;
const float d = h2f(b), dmin = h2f(b + 2);
int sc, m;
scale_min_k4(gi, b + 4, sc, m);
const float d1 = d * (float) sc, m1 = dmin * (float) m;
const uint8_t* q = b + 16 + 32 * j64;
return h16(d1 * (float) (hi ? (q[l] >> 4) : (q[l] & 0xF)) - m1);
} else if constexpr (TYPE == 13) { // Q5_K
const uint8_t* b = row_blocks + (size_t) (gi_in_row / 8) * 176;
const int gi = gi_in_row % 8, j64 = gi / 2, hi = gi % 2;
const float d = h2f(b), dmin = h2f(b + 2);
int sc, m;
scale_min_k4(gi, b + 4, sc, m);
const float d1 = d * (float) sc, m1 = dmin * (float) m;
const uint8_t* qh = b + 16;
const uint8_t* ql = b + 48 + 32 * j64;
const uint8_t u = (uint8_t) (1u << (2 * j64 + hi));
const int nib = hi ? (ql[l] >> 4) : (ql[l] & 0xF);
return h16(d1 * (float) (nib + ((qh[l] & u) ? 16 : 0)) - m1);
} else if constexpr (TYPE == 14) { // Q6_K
const uint8_t* b = row_blocks + (size_t) (gi_in_row / 8) * 210;
const int gi = gi_in_row % 8, n = gi / 4, qu = gi % 4;
const uint8_t* ql = b + 64 * n;
const uint8_t* qh = b + 128 + 32 * n;
const int8_t* sc = reinterpret_cast<const int8_t*>(b + 192) + 8 * n;
const float d = h2f(b + 208);
const int is = l / 16;
int q;
if (qu == 0) q = (ql[l] & 0xF) | (((qh[l] >> 0) & 3) << 4);
else if (qu == 1) q = (ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4);
else if (qu == 2) q = (ql[l] >> 4) | (((qh[l] >> 4) & 3) << 4);
else q = (ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4);
return h16(d * (float) sc[is + 2 * qu] * (float) (q - 32));
} else { // IQ4_XS (23)
const uint8_t* b = row_blocks + (size_t) (gi_in_row / 8) * 136;
const int ib = gi_in_row % 8;
const float d = h2f(b);
const uint16_t scales_h = (uint16_t) (b[2] | (b[3] << 8));
const int ls = ((b[4 + ib / 2] >> (4 * (ib % 2))) & 0xf) | (((scales_h >> (2 * ib)) & 3) << 4);
const float dl = d * (float) (ls - 32);
const uint8_t* qs = b + 8 + 16 * ib;
return h16(l < 16 ? dl * (float) kv_iq4nl[qs[l] & 0xf] : dl * (float) kv_iq4nl[qs[l - 16] >> 4]);
}
}
template <int TYPE>
__device__ __forceinline__ void dq_body(const uint8_t* __restrict__ blocks, int64_t row_bytes, int64_t total,
int64_t groups_per_row, uint16_t* __restrict__ out) {
const int64_t e = (int64_t) blockIdx.x * blockDim.x + threadIdx.x;
if (e >= total) return;
const int64_t row_elems = groups_per_row * 32;
const int64_t r = e / row_elems, in_row = e % row_elems;
out[e] = dq_element<TYPE>(blocks + r * row_bytes, (int) (in_row / 32), (int) (in_row % 32));
}
} // namespace
#define LELEKA_PREFILL_DEQUANT(NAME, TYPE) \
extern "C" __global__ __launch_bounds__(256) void NAME(const uint8_t* __restrict__ blocks, int64_t row_bytes, \
int64_t total, int64_t groups_per_row, \
uint16_t* __restrict__ out) { \
dq_body<TYPE>(blocks, row_bytes, total, groups_per_row, out); \
}
LELEKA_PREFILL_DEQUANT(leleka_prefill_dequant_q8_0, 8)
LELEKA_PREFILL_DEQUANT(leleka_prefill_dequant_q4_k, 12)
LELEKA_PREFILL_DEQUANT(leleka_prefill_dequant_q5_k, 13)
LELEKA_PREFILL_DEQUANT(leleka_prefill_dequant_q6_k, 14)
LELEKA_PREFILL_DEQUANT(leleka_prefill_dequant_iq4_nl, 20)
LELEKA_PREFILL_DEQUANT(leleka_prefill_dequant_iq4_xs, 23)
// gdn_conv_tiled_kernel + the guards of its input (qkv) and output (h). Grid: (GC / 256) column blocks x tiles.
extern "C" __global__ __launch_bounds__(256) void leleka_prefill_gdn_conv_tiled(
const float* __restrict__ hist, const float* __restrict__ qkv, const float* __restrict__ w,
float* __restrict__ h, int64_t T, unsigned* __restrict__ status_qkv, unsigned* __restrict__ status_conv) {
constexpr int COLUMN_BLOCKS = GC / 256;
const int c = (blockIdx.x % COLUMN_BLOCKS) * 256 + threadIdx.x;
const int64_t t0 = (int64_t) (blockIdx.x / COLUMN_BLOCKS) * CONV_TILE;
if (t0 >= T) return;
const int64_t t1 = t0 + CONV_TILE < T ? t0 + CONV_TILE : T;
auto input = [&](int64_t t) -> float { return t >= 0 ? qkv[t * GC + c] : hist[c * 3 + (int) (t + 3)]; };
float v0 = input(t0 - 3), v1 = input(t0 - 2), v2 = input(t0 - 1);
const float w0 = w[c * 4], w1 = w[c * 4 + 1], w2 = w[c * 4 + 2], w3 = w[c * 4 + 3];
bool bad_in = false, bad_out = false;
for (int64_t t = t0; t < t1; ++t) {
const float x = qkv[t * GC + c];
const float s = v0 * w0 + v1 * w1 + v2 * w2 + x * w3;
const float y = s / (1.0f + __expf(-s));
h[t * GC + c] = y;
bad_in |= nonfinite(x);
bad_out |= nonfinite(y);
v0 = v1; v1 = v2; v2 = x;
}
if (bad_in) atomicOr(status_qkv, 1u);
if (bad_out) atomicOr(status_conv, 1u);
}
// gdn_l2_kernel for two (t, head) items per 256-thread block (128 threads each, the original's 4 warp partials added
// part[0] + part[1] + part[2] + part[3]) + the guard of the normalised values. Grid: ceil(32 * T / 2).
extern "C" __global__ __launch_bounds__(256) void leleka_prefill_gdn_l2(float* __restrict__ h, float eps, int64_t T,
unsigned* __restrict__ status_qk) {
__shared__ float part[8];
const int half = threadIdx.x >> 7, lane = threadIdx.x & 127;
const int64_t item = (int64_t) blockIdx.x * 2 + half;
const bool valid = item < (int64_t) 2 * GHK * T;
const int64_t t = item / (2 * GHK);
const int head = (int) (item % (2 * GHK));
float* x = h + t * GC + head * GS;
const float v = valid ? x[lane] : 0.0f;
const float sq = warp_sum(v * v);
if ((threadIdx.x & 31) == 0) part[threadIdx.x >> 5] = sq;
__syncthreads();
if (valid) {
const float ss = part[half * 4] + part[half * 4 + 1] + part[half * 4 + 2] + part[half * 4 + 3];
const float y = v * rsqrtf(ss + eps);
x[lane] = y;
if (nonfinite(y)) atomicOr(status_qk, 1u);
}
}
// gdn_out_norm_kernel for two (t, head) items per block + the guards of z (read), the readout oc (read), y and its FP16
// image (written). Grid: ceil(T * 48 / 2).
extern "C" __global__ __launch_bounds__(256) void leleka_prefill_gdn_out_norm(
const float* __restrict__ z, const float* __restrict__ gamma, float eps, float* __restrict__ y,
uint16_t* __restrict__ y16, int64_t T, unsigned* __restrict__ status_z, unsigned* __restrict__ status_readout,
unsigned* __restrict__ status_y, unsigned* __restrict__ status_y16) {
__shared__ float wsum[8];
const int half = threadIdx.x >> 7, col = threadIdx.x & 127;
const int64_t item = (int64_t) blockIdx.x * 2 + half;
const bool valid = item < (int64_t) GHV * T;
const int64_t t = item / GHV;
const int head = (int) (item % GHV);
const size_t at = (size_t) t * GHV * GS + (size_t) head * GS + col;
const float oc = valid ? y[at] : 0.0f;
const float sp = warp_sum(oc * oc);
if ((threadIdx.x & 31) == 0) wsum[threadIdx.x >> 5] = sp;
__syncthreads();
if (valid) {
const float ss = wsum[half * 4] + wsum[half * 4 + 1] + wsum[half * 4 + 2] + wsum[half * 4 + 3];
const float zz = z[t * GHV * GS + head * GS + col];
const float v = oc * rsqrtf(ss / (float) GS + eps) * gamma[col] * sigm(zz);
y[at] = v;
const uint16_t hv = hf(v);
y16[at] = hv;
if (nonfinite(zz)) atomicOr(status_z, 1u);
if (nonfinite(oc)) atomicOr(status_readout, 1u);
if (nonfinite(v)) atomicOr(status_y, 1u);
if ((hv & 0x7c00u) == 0x7c00u) atomicOr(status_y16, 1u);
}
}
// gdn_rec_cols_pipe_kernel (the Original's prompt recurrence, interface edits of offline_gdn_prefill_kernel.cu:
// state_in -> state_out, the state witness) with the same arithmetic in the same order, restructured to wait less per
// token: a warp reads only its own 32-row slice of q and k, so the token no longer starts with a block barrier and a
// shared q/k copy; the two row-group reductions use ONE barrier each (the first reduction buffer is rewritten only after
// the second barrier of the same token, the second only after the first barrier of the next, so single buffers
// suffice). Per column: kv = fma chain over the warp's 32 rows (rg-th group), kv_col = ((r0 + r1) + r2) + r3,
// delta = (v - g * kv_col) * beta, s = fma(g, s, k * delta), o = fma chain of s * q, oc = ((o0 + o1) + o2) + o3 * rsqrt(S).
// Launch: grid 192 (48 heads x 4 column blocks of 32), block 128 (4 warps = the 4 row groups).
extern "C" __global__ void __launch_bounds__(128)
leleka_prefill_gdn_rec(const float* __restrict__ state_in, float* __restrict__ state_out, const float* __restrict__ h,
const float* __restrict__ gate, const float* __restrict__ beta, float* __restrict__ oc_out,
int64_t T, unsigned* __restrict__ witness) {
constexpr int RG = 4, RPG = GS / RG, CB = 32, NCB = GS / CB;
__shared__ float sk[2][RG][RPG], sq[2][RG][RPG];
__shared__ float red1[RG][CB], red2[RG][CB];
const int head = blockIdx.x / NCB, cb = blockIdx.x % NCB;
const int c = threadIdx.x & 31, rg = threadIdx.x >> 5, col = cb * CB + c;
const int qh = head % GHK;
float s[RPG];
const size_t at = ((size_t) (rg * RPG) * GHV + head) * GS + col;
const float* base_in = state_in + at;
float* base_out = state_out + at;
const size_t rs = (size_t) GHV * GS;
#pragma unroll
for (int r = 0; r < RPG; ++r) s[r] = base_in[r * rs];
unsigned bad = 0;
#pragma unroll
for (int r = 0; r < RPG; ++r) bad |= (__float_as_uint(s[r]) & 0x7f800000u) == 0x7f800000u;
float nq = 0.0f, nk = 0.0f, nv = 0.0f, ng = 0.0f, nb = 0.0f;
auto fetch = [&](int64_t t) {
const float* ht = h + t * GC;
nq = ht[qh * GS + rg * RPG + c];
nk = ht[GHK * GS + qh * GS + rg * RPG + c];
nv = ht[2 * GHK * GS + head * GS + col];
ng = gate[t * GHV + head];
nb = beta[t * GHV + head];
};
if (T > 0) fetch(0);
for (int64_t t = 0; t < T; ++t) {
const int par = (int) (t & 1);
const float cq = nq, ck = nk, cv = nv, cg = ng, cbt = nb;
sq[par][rg][c] = cq;
sk[par][rg][c] = ck;
__syncwarp();
if (t + 1 < T) fetch(t + 1);
const float g = __expf(cg);
float kv = 0.0f;
#pragma unroll
for (int r = 0; r < RPG; ++r) kv = fmaf(s[r], sk[par][rg][r], kv);
red1[rg][c] = kv;
__syncthreads();
const float kv_col = red1[0][c] + red1[1][c] + red1[2][c] + red1[3][c];
const float delta = (cv - g * kv_col) * cbt;
float o = 0.0f;
#pragma unroll
for (int r = 0; r < RPG; ++r) {
s[r] = fmaf(g, s[r], sk[par][rg][r] * delta);
o = fmaf(s[r], sq[par][rg][r], o);
}
#pragma unroll
for (int r = 0; r < RPG; ++r) bad |= (__float_as_uint(s[r]) & 0x7f800000u) == 0x7f800000u;
red2[rg][c] = o;
__syncthreads();
if (rg == 0) oc_out[t * GHV * GS + head * GS + col] = (red2[0][c] + red2[1][c] + red2[2][c] + red2[3][c]) * rsqrtf((float) GS);
}
#pragma unroll
for (int r = 0; r < RPG; ++r) base_out[r * rs] = s[r];
if (bad) atomicOr(witness, 1u);
}
File diff suppressed because it is too large Load Diff
+72
View File
@@ -0,0 +1,72 @@
//! Fixed asset of the batched prefill input stage (`prefill_stage.cu`): chunk-wide KAR PLE scatter/decode and the
//! embedding broadcast of `OriginalPromptDevice::initialize_staged`. Pure data: the PTX bytes, the entry names and
//! the pin that `admit` checks before the module is ever handed to the driver.
use crate::kar_decode_levels::{KarDecodeError, PtxPin, admit_ptx};
use std::ffi::CStr;
/// The PTX plus the single ABI NUL, like every other fixed asset.
pub(crate) static PTX: &[u8] = concat!(include_str!("prefill_stage.ptx"), "\0").as_bytes();
/// Module entry order: the index passed to `KernelModule::launch`.
pub(crate) const ENTRIES: &[&CStr] = &[
c"leleka_prefill_ple_scatter",
c"leleka_prefill_ple_decode",
c"leleka_prefill_embed_broadcast",
c"leleka_prefill_gr_norm_rs",
c"leleka_prefill_gr_mix_r",
c"leleka_prefill_gr_write_norm_rs",
c"leleka_prefill_gr_write",
c"leleka_prefill_dequant_q8_0",
c"leleka_prefill_dequant_q4_k",
c"leleka_prefill_dequant_q5_k",
c"leleka_prefill_dequant_q6_k",
c"leleka_prefill_dequant_iq4_nl",
c"leleka_prefill_dequant_iq4_xs",
c"leleka_prefill_gdn_conv_tiled",
c"leleka_prefill_gdn_l2",
c"leleka_prefill_gdn_out_norm",
c"leleka_prefill_gdn_rec",
];
/// Entry indices (positions in `ENTRIES`).
pub(crate) const GR_NORM_RS: usize = 3;
pub(crate) const GR_MIX_R: usize = 4;
pub(crate) const GR_WRITE_NORM_RS: usize = 5;
pub(crate) const GR_WRITE: usize = 6;
pub(crate) const DEQUANT_FIRST: usize = 7;
pub(crate) const GDN_CONV_TILED: usize = 13;
pub(crate) const GDN_L2: usize = 14;
pub(crate) const GDN_OUT_NORM: usize = 15;
pub(crate) const GDN_REC: usize = 16;
fn switch(name: &'static str, cell: &'static std::sync::OnceLock<bool>) -> bool {
*cell.get_or_init(|| std::env::var(name).map_or(true, |v| v != "0"))
}
/// `LELEKA_PREFILL_FUSED_SCANS=0`: the separate finite-guard kernels of the GR read/inject and the GDN chain (A/B arm).
pub(crate) fn fused_scans() -> bool {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
switch("LELEKA_PREFILL_FUSED_SCANS", &ON)
}
/// `LELEKA_PREFILL_FAST_REC=1` (EXPERIMENTAL, off by default): the two-barrier recurrence kernel. Bit-identical to the
/// original on the 3080 probe but not faster there, so it is not the default until a 5090 A/B shows a gain.
pub(crate) fn fast_rec() -> bool {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*ON.get_or_init(|| std::env::var("LELEKA_PREFILL_FAST_REC").is_ok_and(|v| v == "1"))
}
/// `LELEKA_PREFILL_FAST_DEQUANT=0`: the original one-thread-per-32-values GDN weight dequantization (A/B arm).
pub(crate) fn fast_dequant() -> bool {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
switch("LELEKA_PREFILL_FAST_DEQUANT", &ON)
}
/// Frozen by `scripts/gpu-bringup/seal_pins.py` after `compile_assets.py` (extra-seal-plan.json, group prefill-stage).
pub(crate) const PIN: PtxPin = PtxPin {
source_sha256: [
62, 152, 220, 4, 61, 27, 47, 181, 125, 201, 35, 79, 65, 232, 25, 204, 213, 251, 26, 196,
227, 189, 129, 148, 111, 236, 159, 49, 220, 12, 209, 126,
],
ptx_sha256: [
58, 172, 190, 244, 95, 237, 102, 24, 35, 249, 80, 10, 86, 139, 13, 137, 100, 239, 106, 32,
102, 100, 186, 106, 66, 211, 116, 179, 226, 187, 1, 6,
],
};
pub(crate) fn admit() -> Result<(), KarDecodeError> {
admit_ptx(PTX, &PIN)
}
+7
View File
@@ -244,6 +244,13 @@ pub(crate) static COMPANIONS: &[Companion] = &[
sha: [0x1c, 0xcf, 0xf9, 0xa5, 0xce, 0x9f, 0x1a, 0xb7, 0x9c, 0xf8, 0xf5, 0xc8, 0x1d, 0xfd, 0x2a, 0x77, 0x5a, 0xd8, 0x38, 0x11, 0x5c, 0x09, 0x41, 0x4e, 0x0e, 0x1c, 0xdb, 0x3f, 0x34, 0x7f, 0x35, 0x6b],
ptx: concat!(include_str!("../ptx-sm86/gr_prompt_device.ptx"), "\0").as_bytes(),
},
Companion {
name: "prefill_stage.ptx",
key: [0x3a, 0xac, 0xbe, 0xf4, 0x5f, 0xed, 0x66, 0x18, 0x23, 0xf9, 0x50, 0x0a, 0x56, 0x8b, 0x0d, 0x89, 0x64, 0xef, 0x6a, 0x20, 0x66, 0x64, 0xba, 0x6a, 0x42, 0xd3, 0x74, 0xb3, 0xe2, 0xbb, 0x01, 0x06],
len: 112485,
sha: [0x0c, 0xd1, 0x83, 0x5d, 0xc8, 0x3d, 0x3a, 0x31, 0x86, 0xf2, 0x20, 0x43, 0x9c, 0x67, 0x77, 0xa3, 0xa7, 0xe7, 0x04, 0x8d, 0x8d, 0x09, 0xdf, 0xfd, 0xbe, 0x01, 0x1b, 0x16, 0x72, 0xd9, 0x55, 0x3d],
ptx: concat!(include_str!("../ptx-sm86/prefill_stage.ptx"), "\0").as_bytes(),
},
Companion {
name: "s_gemv_legacy.ptx",
key: [0x3a, 0xb4, 0xd1, 0x2a, 0x5e, 0x97, 0xc4, 0xe2, 0xf4, 0x60, 0x2c, 0x6c, 0x87, 0x23, 0x72, 0xc5, 0xc0, 0x68, 0x5d, 0x8c, 0xf1, 0xa2, 0x02, 0x60, 0xaf, 0x6e, 0x0f, 0x52, 0x05, 0xac, 0x76, 0xd5],
+43
View File
@@ -683,6 +683,49 @@
"group": "serve-assets",
"optional": false,
"include_profile": "standard"
},
{
"cu": "crates/cuda-driver/src/prefill_stage.cu",
"ptx": "crates/cuda-driver/src/prefill_stage.ptx",
"flags": [
"-ptx",
"-O3",
"-arch=sm_120",
"-std=c++17",
"--ftz=false",
"--prec-div=true",
"--prec-sqrt=true",
"--fmad=true"
],
"policy": "precise (the recipe of kar_decode_levels and the fixed KAR PLE kernels it replaces for chunk-wide use; no fast-math)",
"entries": [
"leleka_prefill_ple_scatter",
"leleka_prefill_ple_decode",
"leleka_prefill_embed_broadcast",
"leleka_prefill_gr_norm_rs",
"leleka_prefill_gr_mix_r",
"leleka_prefill_gr_write_norm_rs",
"leleka_prefill_gr_write",
"leleka_prefill_dequant_q8_0",
"leleka_prefill_dequant_q4_k",
"leleka_prefill_dequant_q5_k",
"leleka_prefill_dequant_q6_k",
"leleka_prefill_dequant_iq4_nl",
"leleka_prefill_dequant_iq4_xs",
"leleka_prefill_gdn_conv_tiled",
"leleka_prefill_gdn_l2",
"leleka_prefill_gdn_out_norm",
"leleka_prefill_gdn_rec"
],
"encoding": "concat_include_str",
"rust_fields_to_fill": {
"prefill_stage.rs": [
"PIN.source_sha256",
"PIN.ptx_sha256"
]
},
"group": "prefill-stage",
"optional": false
}
]
}