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
CUDApp / src / omega_gpu_gemm.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* omega_gpu_gemm.h -- matrix multiply of any shape on the GB10 tensor cores (row C3 of docs/OMEGA.md). |
| 3 | * |
| 4 | * 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 |
| 5 | * BF16: A and B hold bf16 values, the product runs on HMMA.16816.F32.BF16 |
| 6 | * F16: A and B hold IEEE fp16 values, the product runs on HMMA.16816.F32 (GemmGeneral, 2026-10-27) |
| 7 | * E4M3: A and B hold FP8 e4m3 bytes, the product runs on QMMA.16832.F32.E4M3.E4M3 |
| 8 | * F32_FFMA: A and B hold f32, one thread per C element runs IEEE FFMA in k order from +0 (bit-exact with a host fmaf |
| 9 | * loop); no tensor cores, no packs, dense row-major A and B only (layout 0). The independent comparator for the |
| 10 | * fp32-input bf16x3 path of omega_gpu_gemm_ex (GemmGeneral, 2026-10-27); slow by design. |
| 11 | * 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). |
| 12 | * Leading dimensions (omega_gpu_gemm_ld, _drive_ld): the distance in elements between consecutive stored rows, at least the |
| 13 | * stored row length (0: dense); the span an operand covers must stay below 2^32 elements (the packs index in 32 bits). |
| 14 | * |
| 15 | * 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 |
| 16 | * 32 (E4M3) values of k, in increasing k, k past K counting as zero. The rounding of one step is the chip-measured model |
| 17 | * (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 |
| 18 | * steps reproduces the chip bit for bit (tests/gpu_gemm_test.c). |
| 19 | * |
| 20 | * Kernels, all from Omega's own code generator (no CUDA): |
| 21 | * PACK_A, PACK_B copy A and B, whatever their layout, into the tensor cores' fragment order (each lane's operand |
| 22 | * registers are 16 contiguous bytes; a warp's fragment is 512 contiguous bytes), zero past the edges. |
| 23 | * MAIN each warp computes a 16 rt x 8 ct block of C from the packed operands (LDG.128 straight into the |
| 24 | * operand registers, two register sets so the next loads fly while the tensor cores work), a CTA holds |
| 25 | * wm x wn warps; edge elements past M or N are not stored. |
| 26 | * Packed operand layout (bytes): block (row group, k pair) at ((group * KS2 + pair) * F + f) * 512, lane L at + 16 L, where |
| 27 | * 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) |
| 28 | * or ct (B: f = column tile j; registers 0, 1 step 0 and 2, 3 step 1). */ |
| 29 | #ifndef OMEGA_GPU_GEMM_H |
| 30 | #define OMEGA_GPU_GEMM_H |
| 31 | |
| 32 | #include <stddef.h> |
| 33 | #include <stdint.h> |
| 34 | #include "omega_blackwell_codegen.h" |
| 35 | |
| 36 | #ifdef __cplusplus |
| 37 | extern "C" { |
| 38 | #endif |
| 39 | |
| 40 | #define OMEGA_GPU_GEMM_MAX_DIM 16384u |
| 41 | |
| 42 | enum { |
| 43 | OMEGA_GPU_GEMM_OK = 0, |
| 44 | OMEGA_GPU_GEMM_BAD_ARGS = -1, /* NULL pointer, a zero dimension, unknown type or layout bits, a bad configuration, |
| 45 | a leading dimension below the stored row length or a span past 2^32 elements */ |
| 46 | OMEGA_GPU_GEMM_TOO_LARGE = -2, /* a dimension past OMEGA_GPU_GEMM_MAX_DIM */ |
| 47 | OMEGA_GPU_GEMM_CODEGEN_FAIL = -3, /* our codegen refused a kernel */ |
| 48 | OMEGA_GPU_GEMM_CHIP_FAIL = -4, /* device, allocation, launch, or an output word the chip never wrote */ |
| 49 | OMEGA_GPU_GEMM_UNSUPPORTED = -5 /* omega_gpu_gemm_ex: a well-formed request outside the domain (e.g. fp8 output) */ |
| 50 | }; |
| 51 | |
| 52 | typedef enum { OMEGA_GPU_GEMM_BF16 = 0, OMEGA_GPU_GEMM_E4M3 = 1, OMEGA_GPU_GEMM_F16 = 2, OMEGA_GPU_GEMM_F32_FFMA = 3 } OmegaGpuGemmType; |
| 53 | #define OMEGA_GPU_GEMM_A_COL 1u /* A is column-major: K rows of M */ |
| 54 | #define OMEGA_GPU_GEMM_B_COL 2u /* B is column-major: N rows of K */ |
| 55 | |
| 56 | typedef enum { OMEGA_GEMM_K_PACK_A = 0, OMEGA_GEMM_K_PACK_B, OMEGA_GEMM_K_MAIN, OMEGA_GEMM_K_COUNT } OmegaGemmKind; |
| 57 | |
| 58 | /* 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), |
| 59 | * 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), |
| 60 | * stall: issue cycles after each tensor-core op (1 .. 15). |
| 61 | * 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), |
| 62 | * 3 MAIN stores each column pair swapped. */ |
| 63 | /* band: MAIN visits its tiles in column bands of `band` CTA tiles (power of two up to 32; 0 or 1: plain row-major grid). |
| 64 | * Consecutive CTAs then share one band of packed B while the rows of A stream past it, so the band stays in L2. */ |
| 65 | typedef struct { OmegaGpuGemmType type; uint32_t rt, ct, wm, wn, stall; int mutant; uint32_t band; } OmegaGemmCfg; |
| 66 | void omega_gpu_gemm_default_cfg(OmegaGpuGemmType type, OmegaGemmCfg *cfg); |
| 67 | int omega_gpu_gemm_cfg_ok(const OmegaGemmCfg *cfg); |
| 68 | |
| 69 | /* Argument words (constant bank 0x380). Packs: src u64, dst u64, rows, kdim, row stride, k stride (elements), KS2. |
| 70 | * MAIN: packed A u64, packed B u64, C u64, M, N, KS2, tile columns (Np / (8 ct wn); the banded grid's bound). */ |
| 71 | #define OMEGA_GPU_GEMM_ARGS_WORDS 10u |
| 72 | int omega_gpu_gemm_codegen(const OmegaGemmCfg *cfg, OmegaGemmKind kind, BlackwellIRProgram *prog); |
| 73 | |
| 74 | typedef struct { |
| 75 | uint32_t Mp, Np, Kp, KS2; /* padded sizes; KS2 k pairs (even) */ |
| 76 | uint64_t a_bytes, b_bytes; /* the inputs as given */ |
| 77 | uint64_t ap_bytes, bp_bytes; /* the packed operands, slack for the last prefetch included */ |
| 78 | uint32_t grid[OMEGA_GEMM_K_COUNT][2]; |
| 79 | uint32_t threads[OMEGA_GEMM_K_COUNT]; |
| 80 | } OmegaGemmPlan; |
| 81 | int omega_gpu_gemm_plan(const OmegaGemmCfg *cfg, uint32_t M, uint32_t N, uint32_t K, OmegaGemmPlan *plan); |
| 82 | |
| 83 | /* Runs the three kernels through a launch callback (the chip, or the warp simulator in the test). Device addresses |
| 84 | * are plain numbers. */ |
| 85 | typedef int (*OmegaGemmLaunchFn)(void *ctx, OmegaGemmKind kind, const uint32_t args[OMEGA_GPU_GEMM_ARGS_WORDS], |
| 86 | uint32_t grid_x, uint32_t grid_y, uint32_t threads); |
| 87 | int omega_gpu_gemm_drive(const OmegaGemmCfg *cfg, unsigned layout, uint32_t M, uint32_t N, uint32_t K, uint64_t a_va, |
| 88 | uint64_t b_va, uint64_t ap_va, uint64_t bp_va, uint64_t c_va, OmegaGemmLaunchFn launch, void *ctx); |
| 89 | /* the same with leading dimensions lda, ldb in elements (0: dense) */ |
| 90 | int omega_gpu_gemm_drive_ld(const OmegaGemmCfg *cfg, unsigned layout, uint32_t M, uint32_t N, uint32_t K, uint64_t a_va, uint64_t lda, |
| 91 | uint64_t b_va, uint64_t ldb, uint64_t ap_va, uint64_t bp_va, uint64_t c_va, OmegaGemmLaunchFn launch, void *ctx); |
| 92 | /* elements A and B span with those leading dimensions; OMEGA_GPU_GEMM_BAD_ARGS when a leading dimension is short or a span |
| 93 | * passes 2^32 elements */ |
| 94 | int omega_gpu_gemm_span(unsigned layout, uint32_t M, uint32_t N, uint32_t K, uint64_t lda, uint64_t ldb, uint64_t *a_elems, uint64_t *b_elems); |
| 95 | |
| 96 | typedef struct { |
| 97 | uint64_t pack_ns; /* the two pack kernels, launch to completion */ |
| 98 | uint64_t main_ns; /* the multiply kernel */ |
| 99 | uint64_t call_ns; /* the whole call, uploads and readback included */ |
| 100 | uint32_t gpr[OMEGA_GEMM_K_COUNT]; |
| 101 | } OmegaGpuGemmInfo; |
| 102 | |
| 103 | /* On the chip, with the configuration set by omega_gpu_gemm_test_set_cfg (default: omega_gpu_gemm_default_cfg). */ |
| 104 | int omega_gpu_gemm(OmegaGpuGemmType type, unsigned layout, uint32_t M, uint32_t N, uint32_t K, const void *A, const void *B, |
| 105 | float *C, OmegaGpuGemmInfo *info); |
| 106 | /* the same with leading dimensions (elements, 0: dense); C stays dense M x N */ |
| 107 | int omega_gpu_gemm_ld(OmegaGpuGemmType type, unsigned layout, uint32_t M, uint32_t N, uint32_t K, const void *A, uint64_t lda, |
| 108 | const void *B, uint64_t ldb, float *C, OmegaGpuGemmInfo *info); |
| 109 | /* Tests and tuning: later calls of that type use cfg (NULL: back to the default); clears the kernel cache. */ |
| 110 | int omega_gpu_gemm_test_set_cfg(OmegaGpuGemmType type, const OmegaGemmCfg *cfg); |
| 111 | const char *omega_gpu_gemm_rc_name(int rc); |
| 112 | |
| 113 | #ifdef __cplusplus |
| 114 | } |
| 115 | #endif |
| 116 | #endif /* OMEGA_GPU_GEMM_H */ |