interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / src / omega_blackwell_codegen_bf16.h

91 lines · 5755 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2#ifndef OMEGA_BLACKWELL_CODEGEN_BF16_H
3#define OMEGA_BLACKWELL_CODEGEN_BF16_H
4/*
5 * omega_blackwell_codegen_bf16: bf16 streaming GEMV (the lm_head: vocab x hidden, bf16 weights, f32 x and y) for the
6 * OWN-30B lane (2026-10-13). New file; it mirrors the structure of omega_blackwell_codegen_moe.c (the FP8 GEMV) with
7 * no scales and no group table.
8 *
9 * y[r] = sum_c W[r][c] * x[c], W bf16 row-major [n][k], x f32 [k], y f32 [n], f32 accumulate.
10 *
11 * bf16 -> f32 is exact and free: element 2i of a 32-bit weight word is (word << 16), element 2i+1 is
12 * (word & 0xFFFF0000). Thread mapping (as the FP8 kernel): a CTA has `warps` warps; warp w of CTA cx handles row
13 * groups rg = cx * warps + w, rg + stride, ... < RG (RG = n / rows, a row group is `rows` consecutive rows). Per K
14 * step a warp reads 512 contiguous bytes of each of its rows (lane L: one LDG.E.128 = 8 bf16 at byte 16 L); the CTA
15 * stages the whole x row in shared memory once (k * 4 bytes, one BAR.SYNC) and every loop iteration issues the loads
16 * of `depth` K steps of all its rows before using any of them. Per lane the 8 x depth x rows products of a row
17 * accumulate in one FFMA chain (element order: step, word, low half, high half), then a SHFL.DOWN tree (16,8,4,2,1)
18 * and lane 0 stores.
19 *
20 * Constant bank 0 argument words (cbank + 0x380):
21 * 0x380 w u64 weights base (row r at w + r * k * 2)
22 * 0x388 x u64 x row (f32, k)
23 * 0x390 y u64 y (f32, n)
24 * 0x398 RG u32 row groups (n / rows)
25 * 0x39c stride u32 row groups per warp iteration (grid_x * warps)
26 * Grid: grid_x CTAs, grid_y = 1. The kernel does not depend on n or the grid: one kernel per (k, rows, warps, depth,
27 * mutant) serves every n. ROWS form (spec.ystride != 0, sc #642 prefill): grid_y token rows, row cy reads x + cy * k
28 * floats and writes y + cy * ystride floats (the Qwen3.8-27B DeltaNet a|b projection of a prefill chunk).
29 * MASK form (spec.mask = 1, rows 1, one x row; HUM L314 exact two-pass head): before row r's K loop the warp loads
30 * hi[r] (f32) and the threshold tau (one f32: the max of the screen's lower bounds, written by ARGMAX); a row with
31 * hi[r] < tau stores -inf and skips its K loop, every other row runs the unchanged K loop and reduction, so its y[r]
32 * is bit for bit the plain kernel's. No FSETP (never proven on the chip): h = hi + 0, t = tau + 0 (-0 becomes +0),
33 * m = FMNMX.MAX(h, t), skip iff (m XOR h) != 0. hi and tau must not be NaN (the screen writes finite values).
34 *
35 * XBATCH form (spec.xbatch 2..4, L312 stroke: the lm_head once for all heads): xbatch x rows (row m at x + m * k floats,
36 * all staged in shared memory: k * 4 * xbatch bytes) share every weight read; each (weight row, x row) accumulates in the
37 * SAME order as the one-row kernel, so every output row is bit-identical to the one-row GEMV of its own x; y row m
38 * goes to y + m * ystride floats (ystride >= n, required). grid_y stays 1.
39 * MASK and XBATCH never mix (spec_ok refuses mask with xbatch > 1).
40 *
41 * mutant (tests only): 1 pairs the two halves of every word with the wrong x (low half takes the high half's x and
42 * back), 2 never advances the x pointer (every step uses step 0's x: wrong whenever KS >= 2), 3 drops the last word
43 * of each lane chunk. Each must be caught by the parity test.
44 * 4 (MASK form): inverts the skip decision.
45 * 4 (XBATCH form): every x row reads row 0's x (rows 1.. wrong). Must be caught by the xbatch parity test.
46 */
47#include <stdint.h>
48#include "omega_blackwell_codegen.h"
49
50#ifdef __cplusplus
51extern "C" {
52#endif
53
54#define OMEGA_BF16_MAX_K 16384u
55#define OMEGA_BF16_ARG_W 0x380u
56#define OMEGA_BF16_ARG_X 0x388u
57#define OMEGA_BF16_ARG_Y 0x390u
58#define OMEGA_BF16_ARG_RG 0x398u
59#define OMEGA_BF16_ARG_STRIDE 0x39cu
60#define OMEGA_BF16_ARGS_WORDS 8u
61#define OMEGA_BF16_ARG_HI 0x3a0u /* MASK form: u64 hi (f32, n) */
62#define OMEGA_BF16_ARG_TAU 0x3a8u /* MASK form: u64 tau (one f32) */
63#define OMEGA_BF16_MASK_ARGS_WORDS 12u
64#define OMEGA_BF16_STEP_BYTES 512u /* weight bytes per row per K step (32 lanes x 16) */
65#define OMEGA_BF16_MAX_XBATCH 4u /* x rows per CTA in the XBATCH form (f32 rows staged in shared: 4 x 8 KiB at k 2048) */
66
67typedef struct {
68 uint32_t k; /* multiple of 256 (one step) and of 32 * warps (exact staging rounds), <= OMEGA_BF16_MAX_K */
69 uint32_t rows; /* rows per warp: 1, 2 or 4 */
70 uint32_t warps; /* 1 .. 32 (the host simulator runs up to 8) */
71 uint32_t depth; /* K steps per loop iteration: divides k / 256; rows * depth <= 16 */
72 int mutant; /* 0 production; 1, 2, 3 as above */
73 uint32_t ystride; /* 0: one x row (grid_y 1). Else the ROWS form: token row ctaid.y reads x + cy * k and writes
74 * y + cy * ystride (floats; ystride >= n). The default program is unchanged. */
75 uint32_t xbatch; /* 0 or 1: one x row. 2..OMEGA_BF16_MAX_XBATCH: the XBATCH form (needs ystride >= n): x rows at
76 * x + m * k floats share every weight read, y row m at y + m * ystride. */
77 uint32_t mask; /* 0: every row. 1: the MASK form (rows 1, ystride 0). The default program is unchanged. */
78} OmegaBf16GemvSpec;
79
80int omega_blackwell_codegen_bf16_gemv_prog(const OmegaBf16GemvSpec *spec, BlackwellIRProgram *prog);
81int omega_blackwell_codegen_bf16_gemv(const OmegaBf16GemvSpec *spec, OmegaBlackwellKernel *kernel);
82/* K steps: k / 256 (0 when refused). */
83uint32_t omega_bf16_gemv_steps(const OmegaBf16GemvSpec *spec);
84/* Shared bytes the launch declares: k * 4. */
85uint32_t omega_bf16_gemv_shared_bytes(const OmegaBf16GemvSpec *spec);
86int omega_bf16_gemv_prog_stats(const OmegaBf16GemvSpec *spec, uint32_t *insns, uint32_t *gprs);
87
88#ifdef __cplusplus
89}
90#endif
91#endif