omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / src / omega_blackwell_codegen_bf16.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef OMEGA_BLACKWELL_CODEGEN_BF16_H |
| 18 | #define OMEGA_BLACKWELL_CODEGEN_BF16_H |
| 19 | /* |
| 20 | * omega_blackwell_codegen_bf16: bf16 streaming GEMV (the lm_head: vocab x hidden, bf16 weights, f32 x and y) for the |
| 21 | * OWN-30B lane (2026-10-13). New file; it mirrors the structure of omega_blackwell_codegen_moe.c (the FP8 GEMV) with |
| 22 | * no scales and no group table. |
| 23 | * |
| 24 | * y[r] = sum_c W[r][c] * x[c], W bf16 row-major [n][k], x f32 [k], y f32 [n], f32 accumulate. |
| 25 | * |
| 26 | * bf16 -> f32 is exact and free: element 2i of a 32-bit weight word is (word << 16), element 2i+1 is |
| 27 | * (word & 0xFFFF0000). Thread mapping (as the FP8 kernel): a CTA has `warps` warps; warp w of CTA cx handles row |
| 28 | * groups rg = cx * warps + w, rg + stride, ... < RG (RG = n / rows, a row group is `rows` consecutive rows). Per K |
| 29 | * 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 |
| 30 | * stages the whole x row in shared memory once (k * 4 bytes, one BAR.SYNC) and every loop iteration issues the loads |
| 31 | * 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 |
| 32 | * accumulate in one FFMA chain (element order: step, word, low half, high half), then a SHFL.DOWN tree (16,8,4,2,1) |
| 33 | * and lane 0 stores. |
| 34 | * |
| 35 | * Constant bank 0 argument words (cbank + 0x380): |
| 36 | * 0x380 w u64 weights base (row r at w + r * k * 2) |
| 37 | * 0x388 x u64 x row (f32, k) |
| 38 | * 0x390 y u64 y (f32, n) |
| 39 | * 0x398 RG u32 row groups (n / rows) |
| 40 | * 0x39c stride u32 row groups per warp iteration (grid_x * warps) |
| 41 | * Grid: grid_x CTAs, grid_y = 1. The kernel does not depend on n or the grid: one kernel per (k, rows, warps, depth, |
| 42 | * mutant) serves every n. ROWS form (spec.ystride != 0, sc #642 prefill): grid_y token rows, row cy reads x + cy * k |
| 43 | * floats and writes y + cy * ystride floats (the Qwen3.8-27B DeltaNet a|b projection of a prefill chunk). |
| 44 | * 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 |
| 45 | * hi[r] (f32) and the threshold tau (one f32: the max of the screen's lower bounds, written by ARGMAX); a row with |
| 46 | * hi[r] < tau stores -inf and skips its K loop, every other row runs the unchanged K loop and reduction, so its y[r] |
| 47 | * is bit for bit the plain kernel's. No FSETP (never proven on the chip): h = hi + 0, t = tau + 0 (-0 becomes +0), |
| 48 | * m = FMNMX.MAX(h, t), skip iff (m XOR h) != 0. hi and tau must not be NaN (the screen writes finite values). |
| 49 | * |
| 50 | * 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, |
| 51 | * all staged in shared memory: k * 4 * xbatch bytes) share every weight read; each (weight row, x row) accumulates in the |
| 52 | * 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 |
| 53 | * goes to y + m * ystride floats (ystride >= n, required). grid_y stays 1. |
| 54 | * MASK and XBATCH never mix (spec_ok refuses mask with xbatch > 1). |
| 55 | * |
| 56 | * mutant (tests only): 1 pairs the two halves of every word with the wrong x (low half takes the high half's x and |
| 57 | * back), 2 never advances the x pointer (every step uses step 0's x: wrong whenever KS >= 2), 3 drops the last word |
| 58 | * of each lane chunk. Each must be caught by the parity test. |
| 59 | * 4 (MASK form): inverts the skip decision. |
| 60 | * 4 (XBATCH form): every x row reads row 0's x (rows 1.. wrong). Must be caught by the xbatch parity test. |
| 61 | */ |
| 62 | #include <stdint.h> |
| 63 | #include "omega_blackwell_codegen.h" |
| 64 | |
| 65 | #ifdef __cplusplus |
| 66 | extern "C" { |
| 67 | #endif |
| 68 | |
| 69 | #define OMEGA_BF16_MAX_K 16384u |
| 70 | #define OMEGA_BF16_ARG_W 0x380u |
| 71 | #define OMEGA_BF16_ARG_X 0x388u |
| 72 | #define OMEGA_BF16_ARG_Y 0x390u |
| 73 | #define OMEGA_BF16_ARG_RG 0x398u |
| 74 | #define OMEGA_BF16_ARG_STRIDE 0x39cu |
| 75 | #define OMEGA_BF16_ARGS_WORDS 8u |
| 76 | #define OMEGA_BF16_ARG_HI 0x3a0u /* MASK form: u64 hi (f32, n) */ |
| 77 | #define OMEGA_BF16_ARG_TAU 0x3a8u /* MASK form: u64 tau (one f32) */ |
| 78 | #define OMEGA_BF16_MASK_ARGS_WORDS 12u |
| 79 | #define OMEGA_BF16_STEP_BYTES 512u /* weight bytes per row per K step (32 lanes x 16) */ |
| 80 | #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) */ |
| 81 | |
| 82 | typedef struct { |
| 83 | uint32_t k; /* multiple of 256 (one step) and of 32 * warps (exact staging rounds), <= OMEGA_BF16_MAX_K */ |
| 84 | uint32_t rows; /* rows per warp: 1, 2 or 4 */ |
| 85 | uint32_t warps; /* 1 .. 32 (the host simulator runs up to 8) */ |
| 86 | uint32_t depth; /* K steps per loop iteration: divides k / 256; rows * depth <= 16 */ |
| 87 | int mutant; /* 0 production; 1, 2, 3 as above */ |
| 88 | uint32_t ystride; /* 0: one x row (grid_y 1). Else the ROWS form: token row ctaid.y reads x + cy * k and writes |
| 89 | * y + cy * ystride (floats; ystride >= n). The default program is unchanged. */ |
| 90 | uint32_t xbatch; /* 0 or 1: one x row. 2..OMEGA_BF16_MAX_XBATCH: the XBATCH form (needs ystride >= n): x rows at |
| 91 | * x + m * k floats share every weight read, y row m at y + m * ystride. */ |
| 92 | uint32_t mask; /* 0: every row. 1: the MASK form (rows 1, ystride 0). The default program is unchanged. */ |
| 93 | } OmegaBf16GemvSpec; |
| 94 | |
| 95 | int omega_blackwell_codegen_bf16_gemv_prog(const OmegaBf16GemvSpec *spec, BlackwellIRProgram *prog); |
| 96 | int omega_blackwell_codegen_bf16_gemv(const OmegaBf16GemvSpec *spec, OmegaBlackwellKernel *kernel); |
| 97 | /* K steps: k / 256 (0 when refused). */ |
| 98 | uint32_t omega_bf16_gemv_steps(const OmegaBf16GemvSpec *spec); |
| 99 | /* Shared bytes the launch declares: k * 4. */ |
| 100 | uint32_t omega_bf16_gemv_shared_bytes(const OmegaBf16GemvSpec *spec); |
| 101 | int omega_bf16_gemv_prog_stats(const OmegaBf16GemvSpec *spec, uint32_t *insns, uint32_t *gprs); |
| 102 | |
| 103 | #ifdef __cplusplus |
| 104 | } |
| 105 | #endif |
| 106 | #endif |