interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / src / omega_blackwell_codegen_gemm.h

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