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_dec.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_DEC_H |
| 18 | #define OMEGA_BLACKWELL_CODEGEN_DEC_H |
| 19 | /* |
| 20 | * omega_blackwell_codegen_dec: the small glue kernels that keep a Qwen3-30B decode token on the chip (OWN-30B W-L2, |
| 21 | * 2026-10-13). Before them the host did the residual adds, the input RMSNorm, SwiGLU, the expert combine and the |
| 22 | * argmax between launches; with them a whole token is one command list (omega_gpu_cmdlist.h). |
| 23 | * |
| 24 | * AXPY x[i] += a[i] in place over iters * stride elements (grid-stride, uniform trip count). Chip test only: a |
| 25 | * chain of these in one list catches overlapping grids (a lost increment). |
| 26 | * RES one CTA of 256 threads over the residual row (2048 wide by default, spec.width), width / 256 values per |
| 27 | * thread. Mode bits: |
| 28 | * ADD x += a (a = o_proj output) |
| 29 | * COMBINE x += sum_g w[g] * d[g] (g = 0..7, as the host: m = w0*d0, then m = m + w_g*d_g, x = x + m) |
| 30 | * NORM y = x * RSQ(mean(x^2) + eps) * nw * scale (scale 2^64 = the FP8 GEMV x' row, 1 = plain) |
| 31 | * x is written back when ADD or COMBINE is set. |
| 32 | * SWIGLU act[g][c] = silu(gu[g][c]) * gu[g][I + c] * scale, g = ctaid.y (8 experts, or 1 dense row), c = ctaid.x * 256 |
| 33 | * + tid, I the intermediate size (768 by default, spec.width; grid I / 256 x groups). |
| 34 | * silu(v) = v * RCP(1 + EX2(-v * log2 e)). |
| 35 | * GATHER out[r][n] = in[r * stride + tab[n]] for n < width (spec.width, spec.stride: floats), r = ctaid.y. Qwen3.8-27B |
| 36 | * attention: the fused q|gate|k|v GEMV row to the attention kernel's permuted q|k|v plus the gate (sc #642; |
| 37 | * the permutation crosses the FP8 weights' 128-row scale blocks, so it cannot be folded into the weights). |
| 38 | * GATE out[n] = a[n] * sigmoid(g[n]) for n < count, sigmoid(v) = RCP(1 + EX2(-v * log2 e)): the attention output gate. |
| 39 | * XBOUND (HUM L314 exact two-pass head) every CTA computes t = sum h^2 + 2^-100 over the 2048 plain hidden values and |
| 40 | * hn = t * RSQ(t) * (1 + 2^-10) >= |h| (the widening covers the f32 sum, RSQ and the products); then row r |
| 41 | * (r = ctaid * 256 + tid < count) writes B = c[r] * hn + |s[r]| * 2^-20 + 2^-100, lo = s + (-B), hi = s + B, so |
| 42 | * lo <= s - c|h| and hi >= s + c|h| exactly despite rounding. No FSUB / FSETP (negation by the sign bit). |
| 43 | * Mutant 1 drops the c[r] * hn term (the bound is then far too small: the identity gate must catch it). |
| 44 | * Mode 1 (SCALE): lo[n] = s[n] * 2^64, n < count, grid ceil(count / 256): the FP8 screen's x' row made from the |
| 45 | * plain hidden, exactly h * 2^64, so the screen and the recheck see the same h. |
| 46 | * GROUP (L312 v2, the stroke) one CTA turns the expert picks of N conversations into one grouped-GEMM segment table. |
| 47 | * Row t = h * S + j (head h, slot j; S = spec.groups slots per head, 9 for the 35B) carries expert id |
| 48 | * IDS[h * STRIDE + j * 4]. Rows are sorted by (id, t): sorted row p of row t is its rank. XMAP[p] = h (the x' |
| 49 | * row the gate|up GEMM reads), YMAP[p] = t (its y row, head order). Distinct ids in ascending order get |
| 50 | * segments s = 0, 1, ...: SEG_GU[s] = {TGU[h][j].w_va, .s_va, row0 = rank of its first row, cnt}, SEG_DN[s] |
| 51 | * the same from TDN; segments D..R-1 (D distinct of R = N * S rows) are written as zeros (cnt 0: the GEMM |
| 52 | * CTA leaves); map entries R..R+7 are 0 (the GEMM's padding rows). N * S <= 128, N = HEADS at run time. |
| 53 | * ARGMAX CTA c reads 2048 values v[c*2048 + 8*tid + j] (and indices, or the identity index), writes the max and the |
| 54 | * lowest index holding it to out_v[c], out_i[c]. Two launches give the greedy token of 151936 logits |
| 55 | * (75 CTAs, then 1 CTA over the 75 partials padded with -1e30). The lowest-index rule is the host's |
| 56 | * sample_argmax (strict >). Max by FMNMX, the index by the router's trick (float 2^23 + idx, lifted when the |
| 57 | * value differs from the max, FMNMX.MIN). No FSETP / FSEL. |
| 58 | * |
| 59 | * Constant bank: addresses at 0x380 + 8n, then scalars (offsets below). Every load carries the read barrier, as in the |
| 60 | * other OWN-30B emitters. |
| 61 | */ |
| 62 | #include "omega_blackwell_codegen.h" |
| 63 | #include <stdint.h> |
| 64 | |
| 65 | typedef enum { OMEGA_DEC_AXPY = 1, OMEGA_DEC_RES = 2, OMEGA_DEC_SWIGLU = 3, OMEGA_DEC_ARGMAX = 4, OMEGA_DEC_GATHER = 5, OMEGA_DEC_GATE = 6, OMEGA_DEC_GROUP = 7, OMEGA_DEC_XBOUND = 8 } OmegaDecKind; |
| 66 | |
| 67 | #define OMEGA_DEC_RES_ADD 1u |
| 68 | #define OMEGA_DEC_RES_COMBINE 2u |
| 69 | #define OMEGA_DEC_RES_NORM 4u |
| 70 | #define OMEGA_DEC_RES_ROWS 8u /* row ctaid.y of a [rows][width] block: x, a and y move by width per row, nw does not; with COMBINE |
| 71 | * (L356) a moves by groups * 2048 and the weights by stride bytes per row */ |
| 72 | |
| 73 | #define OMEGA_DEC_HIDDEN 2048u |
| 74 | #define OMEGA_DEC_THREADS 256u |
| 75 | #define OMEGA_DEC_TOPK 8u |
| 76 | #define OMEGA_DEC_INTER 768u |
| 77 | #define OMEGA_DEC_ARGMAX_SPAN 2048u /* values per ARGMAX CTA */ |
| 78 | |
| 79 | /* AXPY args */ |
| 80 | #define OMEGA_DEC_AXPY_X 0x380u |
| 81 | #define OMEGA_DEC_AXPY_A 0x388u |
| 82 | #define OMEGA_DEC_AXPY_ITERS 0x390u |
| 83 | #define OMEGA_DEC_AXPY_WORDS 5u |
| 84 | /* RES args */ |
| 85 | #define OMEGA_DEC_RES_X 0x380u /* f32 [2048] residual, read and (ADD/COMBINE) written */ |
| 86 | #define OMEGA_DEC_RES_A 0x388u /* ADD: f32 [2048]; COMBINE: f32 [8][2048] down outputs */ |
| 87 | #define OMEGA_DEC_RES_W 0x390u /* COMBINE: f32 [8] routing weights */ |
| 88 | #define OMEGA_DEC_RES_NW 0x398u /* NORM: f32 [2048] norm weight */ |
| 89 | #define OMEGA_DEC_RES_Y 0x3a0u /* NORM: f32 [2048] output */ |
| 90 | #define OMEGA_DEC_RES_EPS 0x3a8u |
| 91 | #define OMEGA_DEC_RES_SCALE 0x3acu |
| 92 | #define OMEGA_DEC_RES_WORDS 12u |
| 93 | /* SWIGLU args */ |
| 94 | #define OMEGA_DEC_SW_GU 0x380u /* f32 [8][1536] gate|up */ |
| 95 | #define OMEGA_DEC_SW_ACT 0x388u /* f32 [8][768] */ |
| 96 | #define OMEGA_DEC_SW_SCALE 0x390u |
| 97 | #define OMEGA_DEC_SW_WORDS 5u |
| 98 | /* ARGMAX args */ |
| 99 | #define OMEGA_DEC_AM_V 0x380u /* f32 values, CTA c reads [c*2048, +2048) */ |
| 100 | #define OMEGA_DEC_AM_I 0x388u /* u32 indices (indexed spec), same layout */ |
| 101 | #define OMEGA_DEC_AM_OV 0x390u /* f32 [ctas] */ |
| 102 | #define OMEGA_DEC_AM_OI 0x398u /* u32 [ctas] */ |
| 103 | #define OMEGA_DEC_AM_WORDS 8u |
| 104 | /* GATHER args (out[r][n] = in[r * stride + tab[n]], n < width; grid ceil(width / 256) x rows) */ |
| 105 | #define OMEGA_DEC_GA_IN 0x380u |
| 106 | #define OMEGA_DEC_GA_TAB 0x388u /* u32 [width] source index within an input row */ |
| 107 | #define OMEGA_DEC_GA_OUT 0x390u |
| 108 | #define OMEGA_DEC_GA_WORDS 6u |
| 109 | /* GATE args (out[n] = a[n] * sigmoid(g[n]), n < count; grid ceil(count / 256); out may be a); mode 1 (L369, rows): row ctaid.y, |
| 110 | * a and out at + row * width floats, g at + row * stride floats, count per row; every element the one-row arithmetic */ |
| 111 | #define OMEGA_DEC_GT_A 0x380u |
| 112 | #define OMEGA_DEC_GT_G 0x388u |
| 113 | #define OMEGA_DEC_GT_OUT 0x390u |
| 114 | #define OMEGA_DEC_GT_N 0x398u /* u32 count */ |
| 115 | #define OMEGA_DEC_GT_WORDS 7u |
| 116 | /* XBOUND args (HUM L314 exact head; lo[r] = s[r] - B[r], hi[r] = s[r] + B[r], r < count; grid ceil(count / 256)) */ |
| 117 | #define OMEGA_DEC_XB_S 0x380u /* f32 [count] screen logits */ |
| 118 | #define OMEGA_DEC_XB_C 0x388u /* f32 [count] Row bound coefficients */ |
| 119 | #define OMEGA_DEC_XB_H 0x390u /* f32 [2048] the plain hidden (the recheck x) */ |
| 120 | #define OMEGA_DEC_XB_LO 0x398u |
| 121 | #define OMEGA_DEC_XB_HI 0x3a0u |
| 122 | #define OMEGA_DEC_XB_N 0x3a8u /* u32 count */ |
| 123 | #define OMEGA_DEC_XB_WORDS 11u |
| 124 | /* GROUP args (one CTA of 256 threads; shared: ids at 0, first-of-id flags at 512) */ |
| 125 | #define OMEGA_DEC_GR_IDS 0x380u /* u32 [S] of head 0; head h at + h * STRIDE */ |
| 126 | #define OMEGA_DEC_GR_TGU 0x388u /* {w_va, s_va} [S] of head 0 (gate|up directory entries), head h at + h * STRIDE */ |
| 127 | #define OMEGA_DEC_GR_TDN 0x390u /* the same for down */ |
| 128 | #define OMEGA_DEC_GR_SGU 0x398u /* out: OMEGA_GEMM_SEG_BYTES [R] segment table for gate|up */ |
| 129 | #define OMEGA_DEC_GR_SDN 0x3a0u /* out: [R] for down */ |
| 130 | #define OMEGA_DEC_GR_XMAP 0x3a8u /* out: u32 [R + 8] */ |
| 131 | #define OMEGA_DEC_GR_YMAP 0x3b0u /* out: u32 [R + 8] */ |
| 132 | #define OMEGA_DEC_GR_STRIDE 0x3b8u /* u32 bytes between one head's router buffer and the next */ |
| 133 | #define OMEGA_DEC_GR_HEADS 0x3bcu /* u32 N */ |
| 134 | #define OMEGA_DEC_GR_WORDS 16u |
| 135 | #define OMEGA_DEC_GR_PAD 8u /* map padding entries (the GEMM's largest rows-per-pass) */ |
| 136 | #define OMEGA_DEC_GR_MAX_ROWS 128u |
| 137 | #define OMEGA_DEC_GR_SHARED_BYTES 1024u |
| 138 | #define OMEGA_DEC_GR_SEG_BYTES 32u |
| 139 | |
| 140 | #define OMEGA_DEC_SM_RED 0u /* 8 per-warp partials */ |
| 141 | #define OMEGA_DEC_SM_RED2 64u |
| 142 | #define OMEGA_DEC_SHARED_BYTES 128u |
| 143 | |
| 144 | typedef struct { |
| 145 | OmegaDecKind kind; |
| 146 | uint32_t mode; /* RES mode bits; ARGMAX: 1 = indexed (indices loaded), 0 = identity */ |
| 147 | uint32_t stride; /* AXPY: elements per iteration (grid_x * 256). GATHER: the input row stride (floats). RES COMBINE|ROWS: |
| 148 | the routing weights row stride (bytes, a multiple of 16) */ |
| 149 | int mutant; /* TEST ONLY. RES: 1 = norm without the weight; 2 = COMBINE skips expert 7. |
| 150 | SWIGLU: 1 = up half read at +767. ARGMAX: 1 = last equal index instead of first. |
| 151 | GATHER: 1 = source index + 1. GATE: 1 = the gate dropped (out = a). |
| 152 | GROUP: 1 = sort key without the row tie-break; 2 = segment cnt one short; 3 = the trailing |
| 153 | empty segments left unwritten. 0 none */ |
| 154 | uint32_t width; /* RES: the row width (0 = OMEGA_DEC_HIDDEN; a multiple of 1024 up to 8192, COMBINE only at 2048). |
| 155 | SWIGLU: the intermediate size (0 = OMEGA_DEC_INTER; a multiple of 256). Qwen3.8-27B: 5120 and |
| 156 | 17408 (sc #642). The default widths emit the same program as before the field existed. */ |
| 157 | uint32_t groups; /* RES COMBINE: 0 or 8 routed experts, 9 = 8 + the shared expert (Qwen3.6-35B-A3B; the weights and down rows |
| 158 | have 9 entries, slot 8 the shared expert). Mutant 3 (9 only): the shared slot is skipped. |
| 159 | GROUP: slots per head S (0 = 8; 9 for the 35B), 1..16. */ |
| 160 | } OmegaDecSpec; |
| 161 | |
| 162 | int omega_blackwell_codegen_dec_prog(const OmegaDecSpec *spec, BlackwellIRProgram *prog); |
| 163 | int omega_dec_prog_stats(const OmegaDecSpec *spec, uint32_t *insns, uint32_t *gprs); |
| 164 | |
| 165 | #endif |