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

96 lines · 5803 bytes Raw Blame History
1/* omega_gpu_gemm.h -- matrix multiply of any shape on the GB10 tensor cores (row C3 of docs/OMEGA.md).
2 *
3 * C = A * B A is M x K, B is K x N, C is M x N f32 (row-major), every M, N, K from 1 to OMEGA_GPU_GEMM_MAX_DIM
4 * BF16: A and B hold bf16 values, the product runs on HMMA.16816.F32.BF16
5 * E4M3: A and B hold FP8 e4m3 bytes, the product runs on QMMA.16832.F32.E4M3.E4M3
6 * Layouts: A row-major (M rows of K) or column-major (K rows of M); B row-major (K rows of N) or column-major (N rows of K).
7 *
8 * The arithmetic is fixed by the shape alone: every C[m][n] starts at +0 and takes one tensor-core step per 16 (BF16) or
9 * 32 (E4M3) values of k, in increasing k, k past K counting as zero. The rounding of one step is the chip-measured model
10 * (tests/bw_hmma_model.h { 16, 2, RZ, 2, 0 }, tests/bw_qmma_model.h { 32, 26, RZ, 2, 0 }), so a host model of the same
11 * steps reproduces the chip bit for bit (tests/gpu_gemm_test.c).
12 *
13 * Kernels, all from Omega's own code generator (no CUDA):
14 * PACK_A, PACK_B copy A and B, whatever their layout, into the tensor cores' fragment order (each lane's operand
15 * registers are 16 contiguous bytes; a warp's fragment is 512 contiguous bytes), zero past the edges.
16 * MAIN each warp computes a 16 rt x 8 ct block of C from the packed operands (LDG.128 straight into the
17 * operand registers, two register sets so the next loads fly while the tensor cores work), a CTA holds
18 * wm x wn warps; edge elements past M or N are not stored.
19 * Packed operand layout (bytes): block (row group, k pair) at ((group * KS2 + pair) * F + f) * 512, lane L at + 16 L, where
20 * a k pair is two tensor-core steps, KS2 = Kp / (2 * step), F = 2 rt (A: f = u * rt + i, step u of the pair, row tile i)
21 * or ct (B: f = column tile j; registers 0, 1 step 0 and 2, 3 step 1). */
22#ifndef OMEGA_GPU_GEMM_H
23#define OMEGA_GPU_GEMM_H
24
25#include <stddef.h>
26#include <stdint.h>
27#include "omega_blackwell_codegen.h"
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33#define OMEGA_GPU_GEMM_MAX_DIM 16384u
34
35enum {
36 OMEGA_GPU_GEMM_OK = 0,
37 OMEGA_GPU_GEMM_BAD_ARGS = -1, /* NULL pointer, a zero dimension, unknown type or layout bits, a bad configuration */
38 OMEGA_GPU_GEMM_TOO_LARGE = -2, /* a dimension past OMEGA_GPU_GEMM_MAX_DIM */
39 OMEGA_GPU_GEMM_CODEGEN_FAIL = -3, /* our codegen refused a kernel */
40 OMEGA_GPU_GEMM_CHIP_FAIL = -4 /* device, allocation, launch, or an output word the chip never wrote */
41};
42
43typedef enum { OMEGA_GPU_GEMM_BF16 = 0, OMEGA_GPU_GEMM_E4M3 = 1 } OmegaGpuGemmType;
44#define OMEGA_GPU_GEMM_A_COL 1u /* A is column-major: K rows of M */
45#define OMEGA_GPU_GEMM_B_COL 2u /* B is column-major: N rows of K */
46
47typedef enum { OMEGA_GEMM_K_PACK_A = 0, OMEGA_GEMM_K_PACK_B, OMEGA_GEMM_K_MAIN, OMEGA_GEMM_K_COUNT } OmegaGemmKind;
48
49/* rt row tiles of 16 and ct column tiles of 8 per warp (1, 2 or 4 each), wm x wn warps per CTA (1, 2 or 4 each),
50 * with 4 (4 rt + 2 ct + rt ct) <= 100 (the registers of two operand sets and the accumulators; rt 2 ct 4 is the largest),
51 * stall: issue cycles after each tensor-core op (1 .. 15).
52 * mutant (red checks): 1 MAIN drops the second step of every k pair, 2 the packs let element 0 in past the edges (instead of zero),
53 * 3 MAIN stores each column pair swapped. */
54typedef struct { OmegaGpuGemmType type; uint32_t rt, ct, wm, wn, stall; int mutant; } OmegaGemmCfg;
55void omega_gpu_gemm_default_cfg(OmegaGpuGemmType type, OmegaGemmCfg *cfg);
56int omega_gpu_gemm_cfg_ok(const OmegaGemmCfg *cfg);
57
58/* Argument words (constant bank 0x380). Packs: src u64, dst u64, rows, kdim, row stride, k stride (elements), KS2.
59 * MAIN: packed A u64, packed B u64, C u64, M, N, KS2. */
60#define OMEGA_GPU_GEMM_ARGS_WORDS 9u
61int omega_gpu_gemm_codegen(const OmegaGemmCfg *cfg, OmegaGemmKind kind, BlackwellIRProgram *prog);
62
63typedef struct {
64 uint32_t Mp, Np, Kp, KS2; /* padded sizes; KS2 k pairs (even) */
65 uint64_t a_bytes, b_bytes; /* the inputs as given */
66 uint64_t ap_bytes, bp_bytes; /* the packed operands, slack for the last prefetch included */
67 uint32_t grid[OMEGA_GEMM_K_COUNT][2];
68 uint32_t threads[OMEGA_GEMM_K_COUNT];
69} OmegaGemmPlan;
70int omega_gpu_gemm_plan(const OmegaGemmCfg *cfg, uint32_t M, uint32_t N, uint32_t K, OmegaGemmPlan *plan);
71
72/* Runs the three kernels through a launch callback (the chip, or the warp simulator in the test). Device addresses
73 * are plain numbers. */
74typedef int (*OmegaGemmLaunchFn)(void *ctx, OmegaGemmKind kind, const uint32_t args[OMEGA_GPU_GEMM_ARGS_WORDS],
75 uint32_t grid_x, uint32_t grid_y, uint32_t threads);
76int omega_gpu_gemm_drive(const OmegaGemmCfg *cfg, unsigned layout, uint32_t M, uint32_t N, uint32_t K, uint64_t a_va,
77 uint64_t b_va, uint64_t ap_va, uint64_t bp_va, uint64_t c_va, OmegaGemmLaunchFn launch, void *ctx);
78
79typedef struct {
80 uint64_t pack_ns; /* the two pack kernels, launch to completion */
81 uint64_t main_ns; /* the multiply kernel */
82 uint64_t call_ns; /* the whole call, uploads and readback included */
83 uint32_t gpr[OMEGA_GEMM_K_COUNT];
84} OmegaGpuGemmInfo;
85
86/* On the chip, with the configuration set by omega_gpu_gemm_test_set_cfg (default: omega_gpu_gemm_default_cfg). */
87int omega_gpu_gemm(OmegaGpuGemmType type, unsigned layout, uint32_t M, uint32_t N, uint32_t K, const void *A, const void *B,
88 float *C, OmegaGpuGemmInfo *info);
89/* Tests and tuning: later calls of that type use cfg (NULL: back to the default); clears the kernel cache. */
90int omega_gpu_gemm_test_set_cfg(OmegaGpuGemmType type, const OmegaGemmCfg *cfg);
91const char *omega_gpu_gemm_rc_name(int rc);
92
93#ifdef __cplusplus
94}
95#endif
96#endif /* OMEGA_GPU_GEMM_H */