Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/src/omega_blackwell_codegen_qg.h # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / omega_blackwell_codegen_qg.h` 86 lines ยท 5410 bytes Raw Blame History 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_QG_H 18 #define OMEGA_BLACKWELL_CODEGEN_QG_H 19 /* 20 * omega_blackwell_codegen_qg: native FP8 prefill kernels (OWN-30B L1 GEMM, W-L1G, 2026-10-14). New file. 21 * 22 * ---- activation quantiser (pq) ---- 23 * Per row r and 128-wide group b of k: amax = max(max_j |x[r][128b + j]|, 2^-100) 24 * E = biased f32 exponent of amax, M = its 23 mantissa bits, c = (M + 0x1fffff) >> 23 (1 when amax / 2^(E-127) > 1.75) 25 * scale = 2^(E - 135 + c) (the smallest power of two with amax / scale <= 448), inv = 2^(135 - E - c) 26 * q[r][128b + j] = e4m3_rne_satfinite(x * inv) (FMUL by a power of two: exact for every result that can be nonzero) 27 * s[r][b] = scale (f32) 28 * Power-of-two scales instead of amax / 448 (stated deviation from the usual dynamic scheme): the GEMM's scale products 29 * stay exact and no reciprocal is needed; e4m3 precision is uniform over its normal range, so at most the subnormal tail 30 * of a group loses one bit. 31 * Layout: x f32 or bf16 rows of k (row stride k elements); q e4m3 rows of k bytes; s f32 [rows][k / 128]. 32 * One CTA per row (ctaid.x = row), 32 * (k / 128) threads: warp b = group b, lane l = elements 4l .. 4l+3 (k <= 4096). 33 * 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; 34 * CTA y covers groups yG .. yG+G-1 (thread index offset by 32 G y). groups 0 is the one-CTA form (programs unchanged). 35 * Arguments (cbank 0x380): x u64, q u64, s u64. 36 * mutant (tests only): 1 skips the butterfly (lane-local amax), 2 uses c = 0, 3 drops the ctaid.y offset (groups form). 37 */ 38 #include 39 #include "omega_blackwell_codegen.h" 40 41 #ifdef __cplusplus 42 extern "C" { 43 #endif 44 enum { OMEGA_QG_IN_F32 = 0, OMEGA_QG_IN_BF16 = 1 }; 45 typedef struct { uint32_t k, in_type; int mutant; uint32_t groups; } OmegaPqSpec; 46 #define OMEGA_PQ_ARGS_WORDS 6u 47 int omega_pq_spec_ok(const OmegaPqSpec *s); 48 int omega_blackwell_codegen_pq_prog(const OmegaPqSpec *s, BlackwellIRProgram *prog); 49 50 /* ---- grouped GEMM on QMMA (qgemm) ---- 51 * y[row][c] = sum_kb sum_{h in 0,1} fma-promoted( T(row, c, kb, h) * (sa[src(row)][kb] * sw[c][kb]) ) 52 * 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 53 * 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 54 * 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 55 * bytes 8v+4 .. 8v+7 logical k 16+4t .. 16+4t+3 (A and B alike, so no K permutation is stored anywhere). 56 * Weights: read IN PLACE from an omega_gpu_fp8_api tensor (no second copy): e4m3 rows of k in checkpoint order, scales 57 * [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 58 * [rows][KB] (xs), the quantiser's output. Output F32: y f32 rows of n (warp columns n0w .. n0w+15). SWIGLU (n = 2I, gate rows 59 * 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 60 * (the cap keeps the reciprocal normal: for g < -86.6 the result is g * 2^-125 * u instead of a flushed or subnormal value). 61 * 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. 62 * Constant bank 0x380: tiles, rowidx (ignored when direct), xq, y, xs (u64 each), n u32 at 0x3a8. 63 * Grid: n / (warps * 16) x tiles; 32 * warps threads; shared 2 * (16 rt * 64 + 16 rt * 4) bytes. 64 * 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 65 * of every odd chunk. */ 66 enum { OMEGA_QG_EPI_F32 = 0, OMEGA_QG_EPI_SWIGLU = 1 }; 67 typedef struct { 68 uint32_t k; /* multiple of 128, 128 .. 32768 */ 69 uint32_t rt; /* row tiles of 16: 4 (8 exceeds 128 registers and the 16384-byte QMD shared cap) */ 70 uint32_t warps; /* 8 or 16 */ 71 uint32_t epi; /* OMEGA_QG_EPI_* */ 72 uint32_t direct; /* 1: no row index */ 73 uint32_t n; /* output width (weight rows), multiple of warps * 16 */ 74 uint32_t wait; /* stall cycles between a row tile's last QMMA and the first FFMA reading its result */ 75 int mutant; 76 } OmegaQgSpec; 77 #define OMEGA_QG_ARGS_WORDS 11u 78 #define OMEGA_QG_TILE_BYTES 32u 79 int omega_qg_spec_ok(const OmegaQgSpec *s); 80 uint32_t omega_qg_shared_bytes(const OmegaQgSpec *s); 81 uint32_t omega_qg_span(const OmegaQgSpec *s); 82 int omega_blackwell_codegen_qgemm_prog(const OmegaQgSpec *s, BlackwellIRProgram *prog); 83 #ifdef __cplusplus 84 } 85 #endif 86 #endif