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_tc.h

108 lines · 7034 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_TC_H
18#define OMEGA_BLACKWELL_CODEGEN_TC_H
19/*
20 * omega_blackwell_codegen_tc: tensor-core (HMMA.16816.F32.BF16) kernels for the OWN-30B one-pass prefill (T3, 2026-10-13).
21 * New file. Uses only opcodes the encoder already has and fixture-tests (omega_blackwell_codegen.h).
22 *
23 * HMMA.16816 fragment layout (lane L, g = L / 4, t = L % 4; register q holds element 2q in its low half, 2q+1 high):
24 * A 16x16 (row-major M x K): a0,a1 = A[g][2t, 2t+1] a2,a3 = A[g+8][2t, 2t+1] a4,a5 = A[g][2t+8, 2t+9] a6,a7 = A[g+8][2t+8, 2t+9]
25 * B 16x8 (K x N): b0,b1 = B[2t, 2t+1][g] b2,b3 = B[2t+8, 2t+9][g]
26 * C/D 16x8 f32: c0,c1 = C[g][2t, 2t+1] c2,c3 = C[g+8][2t, 2t+1]
27 * (PTX ISA mma.m16n8k16 fragment figures; the fragment-major matmul of omega_blackwell_codegen.h packs to the same order and
28 * is chip-proven.)
29 *
30 * ---- probe kernel (characterisation of the chip's HMMA arithmetic) ----
31 * One warp per CTA, ctaid.x = case. Per case, at in + case * 1280: A quads [32 lanes][4 u32] (512 B), B pairs [32][2] (256 B),
32 * C quads [32][4] f32 (512 B). Output at out + case * 512: D quads [32][4] f32. Arguments: 0x380 in u64, 0x388 out u64.
33 * D = A * B + C with one HMMA.16816.F32.BF16. mutant 1 (tests only): C is replaced by RZ (must be caught when C != 0).
34 */
35#include <stdint.h>
36#include "omega_blackwell_codegen.h"
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42#define OMEGA_TC_PROBE_IN_BYTES 1280u
43#define OMEGA_TC_PROBE_OUT_BYTES 512u
44int omega_blackwell_codegen_tc_probe_prog(int mutant, BlackwellIRProgram *prog);
45
46/* ---- grouped FP8 GEMM on tensor cores (cut 1: e4m3 weights decoded to bf16 in registers, HMMA.16816.F32.BF16) ----
47 * y[row0 + r][c] = sum_kb S'[c/128][kb] * ( sum_{k in kb} w(W[c][k]) * x(X[rowidx[row0 + r]][k]) )
48 * w(e) = e4m3 value * 2^-120 as bf16 bits ((e & 0x7F) << 4 | (e & 0x80) << 8: exact, subnormal codes become bf16 subnormals,
49 * which the HMMA honours: evidence OWN30B-T3-HMMA-MODEL), x = bf16(x * 2^64), S' = scale * 2^56 (the FP8 GEMV's fold, so
50 * the true value is recovered). Per 128-wide K block: 8 HMMA steps into a zeroed temporary, then acc = fma(tmp, S', acc).
51 *
52 * Physical K order. Within every aligned group of 4 elements (k = 4q .. 4q+3) the activations are STORED as
53 * [x(4q), x(4q+2), x(4q+1), x(4q+3)] (bf16, row-major otherwise): omega_tcgemm_permute_x. Weights stay in checkpoint order.
54 * HMMA step (chunk c of 64 elements, s = 0..3) consumes the 16 elements c*64 + 16t + 4s + j (t = 0..3 lane%4, j = 0..3);
55 * lane t's weight bytes j = 0,2 form B register 0 and j = 1,3 register 1, so one e4m3 word decodes into both B registers
56 * with 8 integer ops, and the matching activation words arrive in order. The fused 16-term HMMA sum does not depend on the
57 * order of its terms (tests/bw_hmma_model.h), so the result depends only on the step grouping above.
58 *
59 * Tile table (device, ctaid.y = tile, 32 bytes each, the FP8 GEMM's segment entry): { w_va u64, s_va u64, row0 u32, cnt u32,
60 * pad[8] } with 1 <= cnt <= 16*rt; rows row0 .. row0+cnt-1 of the sorted row space belong to one expert.
61 * rowidx (device u32): sorted row -> source row of X; entries row0 .. row0 + 16*rt - 1 must be readable for every tile (the
62 * host pads with valid rows; rows past cnt are computed and not stored).
63 * Constant bank 0 (cbank + 0x380): tiles u64, rowidx u64, x u64 (bf16 rows of k), y u64 (f32 rows of n), n u32.
64 * Grid: grid_x = n / (warps * 8 * ct) (exact), grid_y = tiles. CTA: 32 * warps threads; shared 2 * 16 * rt * 128 bytes.
65 * mutant (tests only): 1 drops the block scale, 2 never advances the scale pointer, 3 row tile i reads tile i^1 (rt >= 2),
66 * 4 drops the last 64-element chunk of every 128 block. Each must be caught. */
67#define OMEGA_TCGEMM_MAX_K 32768u /* the K loop runs KB = k / 128 iterations; nothing else grows with k (omega#541) */
68typedef struct {
69 uint32_t k; /* multiple of 128, 128 .. OMEGA_TCGEMM_MAX_K */
70 uint32_t rt; /* row tiles (16 rows) per CTA: 1, 2 or 4 */
71 uint32_t ct; /* column tiles (8 weight rows) per warp: 1, 2 or 4; rt * ct >= 4 */
72 uint32_t warps; /* 1, 2, 4 or 8; (16 * rt * 8) % (32 * warps) == 0 */
73 int mutant;
74} OmegaTcGemmSpec;
75#define OMEGA_TCGEMM_ARG_TILES 0x380u
76#define OMEGA_TCGEMM_ARG_ROWIDX 0x388u
77#define OMEGA_TCGEMM_ARG_X 0x390u
78#define OMEGA_TCGEMM_ARG_Y 0x398u
79#define OMEGA_TCGEMM_ARG_N 0x3a0u
80#define OMEGA_TCGEMM_ARGS_WORDS 9u
81#define OMEGA_TCGEMM_TILE_BYTES 32u
82int omega_tcgemm_spec_ok(const OmegaTcGemmSpec *spec);
83uint32_t omega_tcgemm_shared_bytes(const OmegaTcGemmSpec *spec);
84uint32_t omega_tcgemm_span(const OmegaTcGemmSpec *spec); /* weight rows per CTA */
85int omega_blackwell_codegen_tcgemm_prog(const OmegaTcGemmSpec *spec, BlackwellIRProgram *prog);
86/* x (k values) -> the stored bf16 row: RNE bf16 of x * 2^64 in the (0,2,1,3) group order */
87void omega_tcgemm_permute_x(const float *x, uint32_t k, uint16_t *out);
88/* e4m3 code -> the bf16 bits the kernel feeds the HMMA (value * 2^-120) */
89uint16_t omega_tcgemm_w_bf16(uint8_t code);
90
91/* tensor-core peak microbenchmark: each thread runs `iters` x `chains` HMMA.16816.F32.BF16 (A = B = 1.0, C chained per
92 * accumulator, `stall` cycles between issues), then stores its chains x 4 accumulators at out (arg 0x380) + (ctaid * threads +
93 * tid) * 16 * chains. Every stored value must be 16 * iters (a hazard on a chained accumulator shows as a wrong value). */
94int omega_blackwell_codegen_tc_peak_prog(uint32_t iters, uint32_t chains, uint32_t stall, uint32_t threads, BlackwellIRProgram *prog);
95
96/* read-bandwidth probe: CTA c streams region (c % groups) of iters * threads * 128 bytes (8 LDG.128 per thread per iteration,
97 * XOR-folded) from arg 0x380 and stores one word per thread at arg 0x388 + (c * threads + tid) * 4. groups = grid: every
98 * CTA distinct (DRAM); groups = 1: every CTA the same bytes (shows any cache reuse of these buffers). */
99int omega_blackwell_codegen_tc_bw_prog(uint32_t iters, uint32_t groups, uint32_t threads, BlackwellIRProgram *prog);
100
101/* SHFL.BFLY and F2FP.BF16.F32.PACK_AB were placeholders patched after encoding (omega_tc_patch_code) until training R5;
102 * they are now native encoder entries (BW_IR_SHFL_BFLY, BW_IR_F2FP_BF16_PACK, golden words in
103 * omega_blackwell_verify_codegen_fixtures_bfly_f2fp) and the emitted bytes are unchanged. */
104
105#ifdef __cplusplus
106}
107#endif
108#endif