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

94 lines · 5556 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_GEMM_H
18#define OMEGA_BLACKWELL_CODEGEN_GEMM_H
19/*
20 * omega_blackwell_codegen_gemm: FP8 e4m3 block-128-scaled grouped GEMM for PREFILL (OWN-30B, 2026-10-13, builder F
21 * item 26). New file; it mirrors omega_blackwell_codegen_moe.c (the FP8 GEMV) so that its result is bit-identical to
22 * that GEMV on the same row.
23 *
24 * for every segment g (one expert, `cnt` rows of x'): y[row0 + i][r] = sum_c W_g[r][c] * x'[row0 + i][c] * S_g[r/128][c/128]
25 *
26 * Each distinct expert's weights are streamed once per pass of MT rows (one pass for the typical expert: 4 rows at 64
27 * tokens), not once per token. A warp owns one weight row r and keeps MT f32 accumulators; per K step the lane decodes
28 * its LB weight bytes once (the GEMV's exact bit trick) and FFMAs them into the MT rows' dot products, then
29 * acc[m] = fma(dot[m], S', acc[m]) once per 128-block exactly as the GEMV, then the same SHFL.DOWN tree.
30 *
31 * x' rows arrive PRE-PERMUTED by the host (omega_gpu_fp8gemm_api): for a row, column c = s*32*LB + L*LB + w*4 + e
32 * (K step s, lane L, word w, element e) sits at index s*32*LB + (w*32 + L)*4 + e, scaled by 2^64 as the GEMV. The CTA
33 * stages one K step of MT rows (MT*32*LB*4 <= 16384 bytes: the QMD shared-memory limit of omega_blackwell_qmd.h) in
34 * shared memory, so every lane reads its 4 columns of every row with one conflict-free LDS.128. Per step: stage,
35 * BAR.SYNC, compute, BAR.SYNC. The weights and scales of step s+1 are loaded before step s computes (two register
36 * sets, scoreboards 3 and 4 held until the step that uses them).
37 *
38 * Segment table (device, 32 bytes per segment, ctaid.y = segment): { w_va u64, s_va u64, row0 u32, cnt u32, pad[8] }.
39 * Constant bank 0 argument words (cbank + 0x380):
40 * 0x380 seg u64 segment table
41 * 0x388 xp u64 permuted x' rows, f32, k per row; MT padding rows must follow the last segment
42 * 0x390 y u64 y rows (f32, n each), row i = sorted row i
43 * 0x398 n u32 output rows per segment (weight rows)
44 * 0x39c stride u32 weight rows per warp iteration (grid_x * warps)
45 * A segment with cnt 0 is skipped (its CTAs exit at once): the stroke's expert table has one slot per (head, expert pick) and only
46 * the distinct experts are filled, so grid_y can be the slot count at record time.
47 * Grid: grid_x CTAs per segment (each CTA: `warps` weight rows per iteration), grid_y = segments. All warps of a CTA
48 * execute the same trip counts (rows past n are clamped to n-1 and not stored; rows past cnt in the last pass read
49 * padding and are not stored), so the CTA barriers are uniform.
50 *
51 * mutant (tests only): 1 drops the block scale (acc += dot), 2 never advances the scale pointer (wrong when KS >= 2),
52 * 3 reads the neighbouring row's x (m ^ 1) for every row, 4 drops the empty-segment guard (a cnt 0 segment then runs 2^32 passes: the
53 * warp simulator's step budget catches it). Each must be caught.
54 */
55#include <stdint.h>
56#include "omega_blackwell_codegen.h"
57
58#ifdef __cplusplus
59extern "C" {
60#endif
61
62#define OMEGA_GEMM_ARG_SEG 0x380u
63#define OMEGA_GEMM_ARG_X 0x388u
64#define OMEGA_GEMM_ARG_Y 0x390u
65#define OMEGA_GEMM_ARG_N 0x398u
66#define OMEGA_GEMM_ARG_STRIDE 0x39cu
67#define OMEGA_GEMM_ARG_XMAP 0x3a0u /* rowmap: u32 [rows + mt] x' row of sorted row p (padding entries 0) */
68#define OMEGA_GEMM_ARG_YMAP 0x3a8u /* rowmap: u32 [rows + mt] y row of sorted row p */
69#define OMEGA_GEMM_ARGS_WORDS 8u
70#define OMEGA_GEMM_ARGS_WORDS_MAP 12u
71#define OMEGA_GEMM_SEG_BYTES 32u
72#define OMEGA_GEMM_MAX_SHARED 16384u
73
74typedef struct {
75 uint32_t k; /* multiple of 128 and of 32 * lane_bytes; <= 32768 (17408 is Qwen3.8-27B down_proj) */
76 uint32_t lane_bytes; /* 8 or 16 (the GEMV's choice: 16 when k % 512 == 0, else 8) */
77 uint32_t mt; /* rows per pass: a power of two, mt * 128 * lane_bytes <= 16384 */
78 uint32_t warps; /* warps % lane_bytes == 0, (mt * lane_bytes) % warps == 0, 1 .. 32 (host simulator: build with -DWS_MAX_THREADS=512) */
79 int mutant;
80 uint32_t rowmap; /* 1: x' rows and y rows are addressed through XMAP / YMAP (the decode stroke: N heads' rows grouped by
81 * expert on the chip; the x' buffer holds one permuted row per head, the outputs land in head order). 0: the
82 * prefill form, x' row i and y row i are sorted row i. mt <= 4 (registers); the dense trunk of a stroke (one
83 * segment, the heads' rows in head order) needs no map and runs at mt 8. */
84} OmegaFp8GemmSpec;
85
86int omega_blackwell_codegen_fp8gemm_prog(const OmegaFp8GemmSpec *spec, BlackwellIRProgram *prog);
87uint32_t omega_fp8gemm_shared_bytes(const OmegaFp8GemmSpec *spec); /* 0 when refused */
88uint32_t omega_fp8gemm_steps(const OmegaFp8GemmSpec *spec);
89int omega_fp8gemm_prog_stats(const OmegaFp8GemmSpec *spec, uint32_t *insns, uint32_t *gprs);
90
91#ifdef __cplusplus
92}
93#endif
94#endif