CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
CUDApp / src / omega_blackwell_codegen_qg.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | #ifndef OMEGA_BLACKWELL_CODEGEN_QG_H |
| 3 | #define OMEGA_BLACKWELL_CODEGEN_QG_H |
| 4 | /* |
| 5 | * omega_blackwell_codegen_qg: native FP8 prefill kernels (OWN-30B L1 GEMM, W-L1G, 2026-10-14). New file. |
| 6 | * |
| 7 | * ---- activation quantiser (pq) ---- |
| 8 | * Per row r and 128-wide group b of k: amax = max(max_j |x[r][128b + j]|, 2^-100) |
| 9 | * E = biased f32 exponent of amax, M = its 23 mantissa bits, c = (M + 0x1fffff) >> 23 (1 when amax / 2^(E-127) > 1.75) |
| 10 | * scale = 2^(E - 135 + c) (the smallest power of two with amax / scale <= 448), inv = 2^(135 - E - c) |
| 11 | * q[r][128b + j] = e4m3_rne_satfinite(x * inv) (FMUL by a power of two: exact for every result that can be nonzero) |
| 12 | * s[r][b] = scale (f32) |
| 13 | * Power-of-two scales instead of amax / 448 (stated deviation from the usual dynamic scheme): the GEMM's scale products |
| 14 | * stay exact and no reciprocal is needed; e4m3 precision is uniform over its normal range, so at most the subnormal tail |
| 15 | * of a group loses one bit. |
| 16 | * Layout: x f32 or bf16 rows of k (row stride k elements); q e4m3 rows of k bytes; s f32 [rows][k / 128]. |
| 17 | * One CTA per row (ctaid.x = row), 32 * (k / 128) threads: warp b = group b, lane l = elements 4l .. 4l+3 (k <= 4096). |
| 18 | * groups G > 0 (any k <= 32768, G <= 32 dividing k / 128): a row spans k / (128 G) CTAs along ctaid.y, 32 G threads each; |
| 19 | * CTA y covers groups yG .. yG+G-1 (thread index offset by 32 G y). groups 0 is the one-CTA form (programs unchanged). |
| 20 | * Arguments (cbank 0x380): x u64, q u64, s u64. |
| 21 | * mutant (tests only): 1 skips the butterfly (lane-local amax), 2 uses c = 0, 3 drops the ctaid.y offset (groups form). |
| 22 | */ |
| 23 | #include <stdint.h> |
| 24 | #include "omega_blackwell_codegen.h" |
| 25 | |
| 26 | #ifdef __cplusplus |
| 27 | extern "C" { |
| 28 | #endif |
| 29 | enum { OMEGA_QG_IN_F32 = 0, OMEGA_QG_IN_BF16 = 1 }; |
| 30 | typedef struct { uint32_t k, in_type; int mutant; uint32_t groups; } OmegaPqSpec; |
| 31 | #define OMEGA_PQ_ARGS_WORDS 6u |
| 32 | int omega_pq_spec_ok(const OmegaPqSpec *s); |
| 33 | int omega_blackwell_codegen_pq_prog(const OmegaPqSpec *s, BlackwellIRProgram *prog); |
| 34 | |
| 35 | /* ---- grouped GEMM on QMMA (qgemm) ---- |
| 36 | * y[row][c] = sum_kb sum_{h in 0,1} fma-promoted( T(row, c, kb, h) * (sa[src(row)][kb] * sw[c][kb]) ) |
| 37 | * where T is two chained QMMA steps from zero over the 64-byte chunk h of block kb: T = Q(v1, C = Q(v0, C = 0)), and after each |
| 38 | * chunk acc = fma(T, sa * sw, acc) in f32 (sa a power of two, so sa * sw is exact). Step v of chunk h: lane t's 16 bytes |
| 39 | * 64h + 16t .. +15 of both the activation row and the weight row; bytes 8v .. 8v+3 are logical k 4t .. 4t+3 of the QMMA and |
| 40 | * bytes 8v+4 .. 8v+7 logical k 16+4t .. 16+4t+3 (A and B alike, so no K permutation is stored anywhere). |
| 41 | * Weights: read IN PLACE from an omega_gpu_fp8_api tensor (no second copy): e4m3 rows of k in checkpoint order, scales |
| 42 | * [n / 128][KB] f32 * 2^56 per expert; the kernel uses sw = S * 2^-56 (exact: the checkpoint bf16 scale). Activations: e4m3 rows of k (xq) + f32 scales |
| 43 | * [rows][KB] (xs), the quantiser's output. Output F32: y f32 rows of n (warp columns n0w .. n0w+15). SWIGLU (n = 2I, gate rows |
| 44 | * 0 .. I-1 then up rows I .. 2I-1, as the checkpoint): column tile 0 of a warp is gate rows n0w / 2 .. +7, tile 1 the up rows I later; act[row][n0w / 2 + 2t + q] = bf16_rn(g * rcp(2^min(-g log2 e, 125) + 1) * u), no extra scale |
| 45 | * (the cap keeps the reciprocal normal: for g < -86.6 the result is g * 2^-125 * u instead of a flushed or subnormal value). |
| 46 | * Tile entry (32 bytes): { w_va u64, s_va u64, row0 u32, cnt u32 (1 .. 16 rt), pad u64 }; rows past cnt are not stored. |
| 47 | * Constant bank 0x380: tiles, rowidx (ignored when direct), xq, y, xs (u64 each), n u32 at 0x3a8, tile count u32 at 0x3ac. |
| 48 | * Grid: n / (warps * 16) x tiles; 32 * warps threads; shared 2 * (16 rt * 64 + 16 rt * 4) bytes. |
| 49 | * mutant (tests only): 1 drops sa (uses sw only), 2 never advances the sw pointer, 3 row tile i reads tile i^1, 4 drops step 1 |
| 50 | * of every odd chunk. */ |
| 51 | enum { OMEGA_QG_EPI_F32 = 0, OMEGA_QG_EPI_SWIGLU = 1 }; |
| 52 | typedef struct { |
| 53 | uint32_t k; /* multiple of 128, 128 .. 32768 */ |
| 54 | uint32_t rt; /* row tiles of 16, admitted by Omega; 8 opts into the extended register allocator */ |
| 55 | uint32_t warps; /* paired with rt by the checked Omega shape policy */ |
| 56 | uint32_t epi; /* OMEGA_QG_EPI_* */ |
| 57 | uint32_t direct; /* 1: no row index */ |
| 58 | uint32_t n; /* output width (weight rows), multiple of warps * 16 */ |
| 59 | uint32_t wait; /* stall cycles between a row tile's last QMMA and the first FFMA reading its result */ |
| 60 | int mutant; |
| 61 | uint32_t group; /* 0: original order; 1..5: checked Omega weight-panel reuse groups */ |
| 62 | } OmegaQgSpec; |
| 63 | #define OMEGA_QG_ARGS_WORDS 12u |
| 64 | #define OMEGA_QG_TILE_BYTES 32u |
| 65 | int omega_qg_spec_ok(const OmegaQgSpec *s); |
| 66 | uint32_t omega_qg_shared_bytes(const OmegaQgSpec *s); |
| 67 | uint32_t omega_qg_span(const OmegaQgSpec *s); |
| 68 | int omega_blackwell_codegen_qgemm_prog(const OmegaQgSpec *s, BlackwellIRProgram *prog); |
| 69 | #ifdef __cplusplus |
| 70 | } |
| 71 | #endif |
| 72 | #endif |