interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / src / omega_blackwell_codegen_moe.h

143 lines · 9469 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_MOE_H
18#define OMEGA_BLACKWELL_CODEGEN_MOE_H
19/*
20 * omega_blackwell_codegen_moe: FP8 (e4m3) GEMV kernel family for the OWN-30B MoE lane
21 * (Cut 6 + Cut 5, 2026-10-13). New file: nothing in omega_blackwell_codegen.{c,h} changes.
22 *
23 * One kernel: y[g][r] = sum_k W_g[r][k] * x[g or 0][k] * S_g[r/128][k/128], m = 1 (decode), for
24 * G groups (experts) in one launch. Weights are e4m3 bytes in the checkpoint's row-major
25 * [n][k] order (n output rows, k input columns), scales are the checkpoint's 128 x 128
26 * `weight_scale_inv` grid, stored on the device as f32 (times 2^OMEGA_FP8_SCALE_EXP, see below).
27 *
28 * Numeric scheme (hand-checked bit arithmetic, proven exhaustively in tests/gpu_fp8_api_test.c):
29 * f32 bits ((b & 0x7F) << 20) | ((b & 0x80) << 24) equal e4m3(b) * 2^-120 exactly, for normals,
30 * subnormals and both zeros (bf16 field exponent 0 with the 3 mantissa bits shifted reproduces
31 * 2^-6 * 0.M). The e4m3 NaN codes 0x7F / 0xFF decode wrong (a finite number): the upload refuses
32 * them. The 2^120 is folded on the host: x' = x * 2^OMEGA_FP8_X_EXP and S' = S * 2^OMEGA_FP8_SCALE_EXP
33 * (64 + 56 = 120), chosen so that every product w' * x' is a normal f32 for any practical x.
34 * The kernel accumulates in f32: per lane, a dot of lane_bytes weights (FFMA chain), then
35 * acc_r = fma(dot, S', acc_r) once per 128-block, then a warp reduction (SHFL.DOWN) per row.
36 *
37 * Thread mapping: a CTA has `warps` warps (threads_x = 32 * warps, threads_y = 1); warp w of CTA
38 * (cx, cy) handles group cy and row groups rg = cx * warps + w, rg + stride, ... < RG (RG = n / rows,
39 * stride = grid_x * warps, both launch arguments). A row group is `rows` consecutive rows, inside
40 * one 128-row scale block (rows divides 128). Per K step a warp reads 32 * lane_bytes contiguous
41 * bytes of each of its rows (lane L: bytes [L * lane_bytes, +lane_bytes)), the same lane_bytes
42 * floats of x', and one scale word per lane (the block of its chunk; 128 / lane_bytes lanes share
43 * it). Step count KS = k / (32 * lane_bytes) is baked into the kernel; k must be a multiple of
44 * 32 * lane_bytes and of 128.
45 *
46 * Constant bank 0 argument words (cbank + 0x380, as the other APIs):
47 * 0x380 tab u64 group table: entry g at tab + 16 g = { w_va u64, s_va u64 }
48 * 0x388 x u64 x' rows (f32, k each; row g at x + g * x_stride)
49 * 0x390 y u64 y rows (f32, n each; row g at y + g * y_stride)
50 * 0x398 RG u32 row groups per group (n / rows)
51 * 0x39c x_stride u32 bytes between x rows (0: one x row shared by every group)
52 * 0x3a0 y_stride u32 bytes between y rows
53 * 0x3a4 stride u32 row groups per warp iteration (grid_x * warps)
54 * Grid: grid_x CTAs per group, grid_y = groups. The kernel does not depend on n, RG, the strides
55 * or the grid, so one kernel per (k, lane_bytes, rows, warps, mutant) serves every n and G.
56 *
57 * Only lane 0 of a warp stores (R words per row group) inside a BSSY/BSYNC region; the warp is
58 * whole again for the next row group's SHFLs.
59 *
60 * mutant (tests only, never production): 1 drops the block scale (acc += dot), 2 never advances the
61 * scale pointer (every K step uses the step-0 block: wrong whenever KS >= 2), 3 reads the group
62 * table entry of group cy ^ 1 (swapped bases). Each must be caught by the parity test.
63 *
64 * Instruction encodings: every IR op used here already exists in omega_blackwell_codegen.h; no new
65 * encodings were added. ISETP_GE (immediate) and IADD3 carry fixed control words in the encoder,
66 * so this generator uses ISETP_GE_U32 (register) and IMAD for adds instead.
67 */
68#include <stdint.h>
69#include "omega_blackwell_codegen.h"
70
71#ifdef __cplusplus
72extern "C" {
73#endif
74
75#define OMEGA_FP8_X_EXP 64 /* x' = x * 2^64 (host, exact) */
76#define OMEGA_FP8_SCALE_EXP 56 /* S' = S * 2^56 (host, exact for every finite bf16 scale below 2^72) */
77#define OMEGA_FP8_BLOCK 128u /* scale block (rows and columns) */
78#define OMEGA_FP8_MAX_K 32768u /* the step kernel and the tensor-core kernel loop over k; 17408 is the Qwen3.8-27B down_proj (omega#541) */
79#define OMEGA_FP8_MAX_GROUPS 64u /* group table entries per launch buffer (8 experts today) */
80
81/* argument word offsets */
82#define OMEGA_FP8_ARG_TAB 0x380u
83#define OMEGA_FP8_ARG_X 0x388u
84#define OMEGA_FP8_ARG_Y 0x390u
85#define OMEGA_FP8_ARG_RG 0x398u
86#define OMEGA_FP8_ARG_XSTRIDE 0x39cu
87#define OMEGA_FP8_ARG_YSTRIDE 0x3a0u
88#define OMEGA_FP8_ARG_STRIDE 0x3a4u
89#define OMEGA_FP8_ARGS_WORDS 10u
90
91typedef struct {
92 uint32_t k; /* input columns: multiple of 128 and of 32 * lane_bytes, <= OMEGA_FP8_MAX_K */
93 uint32_t lane_bytes; /* 8 or 16 weight bytes per lane per step (LDG.E.64 / LDG.E.128); k must be a multiple of 256 */
94 uint32_t rows; /* rows per warp: 1, 2, 4 or 8 */
95 uint32_t warps; /* warps per CTA: 1 .. 32 (the host simulator runs up to 8) */
96 uint32_t depth; /* 0: one K step per loop iteration, x read from global memory each step.
97 * D >= 1: "deep" kernel: the CTA stages its group's x' row in shared memory once
98 * (k * 4 bytes, BAR.SYNC), then every loop iteration issues the weight and scale
99 * loads of D consecutive K steps before using any of them (D x more bytes in
100 * flight per warp); x comes from shared memory (LDS.128). Needs KS % D == 0,
101 * rows * D * lane_bytes <= 128, k % (32 * warps) == 0 and k <= 512 * warps (staging rounds). */
102 int mutant; /* 0 production; 1, 2, 3 as above; 4, 5 with coal = 1 (below) */
103 uint32_t sched; /* 0 (default, chip-proven): every fixed-latency instruction stalls 6, one byte decoded at a time.
104 * 1 (CUT5, measurement variant): the bytes of a step are decoded in a 3-deep software pipeline with
105 * their own temporaries, and a pass after register allocation (omega_fp8_schedule_stalls) gives each
106 * instruction the smallest stall that keeps every user >= OMEGA_FP8_FIXED_LAT cycles behind each
107 * fixed-latency write (the repo rule, omega_gpu_elementwise_api.c:13). The FFMA order is unchanged,
108 * so y is bit-identical to sched 0. Appended last: earlier fields keep their offsets. */
109 uint32_t coal; /* 0 (default): lane 0 of each warp stores its R row results (4-byte STG each) to y.
110 * 1 (CUT5 variant A, after omega#490 KT1): the CTA works in rounds (row groups uc .. uc + W - 1,
111 * uc uniform); lane 0 of each warp parks its R results in shared slots (two slot sets alternating by
112 * round, one BAR.SYNC per round) and warp 0 stores the round's W * R consecutive rows with one
113 * coalesced STG. A warp past the last row group recomputes row group RG - 1 and its slot is never
114 * stored. Needs warps * rows <= 32. Test mutants: 4 = one slot set (the round race), 5 = no clamp. */
115 uint32_t wfirst; /* 0 (default): each K step issues the x' loads, then the scale address IMADs, then the weight and
116 * scale loads. The IMADs wait on the x' loads' scoreboard (every fixed-latency op waits on the pending
117 * loads, emit()), so each step pays the x' round trip before any weight load goes out.
118 * 1 (CUT5 variant B): scale addresses first, then weight, scale and x' loads in one batch with no wait
119 * between them. Ignored by deep kernels (depth >= 1), which read x' from shared memory. */
120} OmegaFp8GemvSpec;
121
122#define OMEGA_FP8_FIXED_LAT 6u /* cycles a fixed-latency result needs before a user (the repo keeps 6; hardware minimum 5) */
123
124/* 0 on success (prog solved and register-allocated), -1 on a refused spec or an allocator failure. */
125int omega_blackwell_codegen_fp8_gemv_prog(const OmegaFp8GemvSpec *spec, BlackwellIRProgram *prog);
126
127/* Hosted build: allocate and encode (omega_blackwell_kernel_free releases). */
128int omega_blackwell_codegen_fp8_gemv(const OmegaFp8GemvSpec *spec, OmegaBlackwellKernel *kernel);
129
130/* K steps the kernel runs: k / (32 * lane_bytes). 0 when the spec is refused. */
131uint32_t omega_fp8_gemv_steps(const OmegaFp8GemvSpec *spec);
132
133/* Shared memory bytes the launch must declare: k * 4 + 16 for a deep kernel, 0 otherwise, plus 2 * warps * rows * 4
134 * (the two output slot sets) when coal = 1. */
135uint32_t omega_fp8_gemv_shared_bytes(const OmegaFp8GemvSpec *spec);
136
137/* The IR instruction count and peak GPR count of the solved program (for budgets in tests). */
138int omega_fp8_gemv_prog_stats(const OmegaFp8GemvSpec *spec, uint32_t *insns, uint32_t *gprs);
139
140#ifdef __cplusplus
141}
142#endif
143#endif