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_ex.h
| 1 | /* omega_gpu_gemm_ex.h -- the general matrix multiply (GemmGeneral, 2026-10-27): one entry with the shape contract of |
| 2 | * cblas_?gemm (RowMajor) plus strided batches and fp8 scales, built on the C3 kernels of omega_gpu_gemm.h. |
| 3 | * |
| 4 | * C_b = alpha * s * op(A_b) op(B_b) + beta * C_b, b = 0 .. batch-1 (row-major, cblas RowMajor semantics) |
| 5 | * |
| 6 | * Shapes: any m, n, k >= 1 (up to OMEGA_GPU_GEMM_EX_MAX_DIM), any batch >= 1. op(A) is m x k: stored m rows of k (OP_N, |
| 7 | * lda >= k) or k rows of m (OP_T, lda >= m). op(B) is k x n: stored k rows of n (OP_N, ldb >= n) or n rows of k (OP_T, |
| 8 | * ldb >= k). C is m rows of n, ldc >= n. A leading dimension of 0 means dense. Batch b reads A + b stride_a, B + b stride_b, |
| 9 | * C + b stride_c (elements); stride 0 for A or B reuses one matrix in every batch; C batches must not overlap each other and |
| 10 | * C must not overlap A or B (BAD_ARGS). beta == 0: C is written without being read. alpha == 0: A and B are not read. |
| 11 | * |
| 12 | * Types: A and B each f32, bf16, f16, e4m3 or e5m2; C f32, bf16 or f16 (round to nearest even; fp8 output: UNSUPPORTED). |
| 13 | * How the product runs (all accumulation is fp32 on the tensor cores; more than one k part: the parts summed on the host in |
| 14 | * double; epilogue alpha * s * acc + beta * C in double, then one rounding to the C type): |
| 15 | * bf16 x bf16, f16 x f16, e4m3 x e4m3 native: HMMA.16816.F32.BF16, HMMA.16816.F32, QMMA.16832.F32.E4M3. With a single k |
| 16 | * part, alpha s == 1, beta 0, no block scales and C f32 the result is bit for bit |
| 17 | * the C3 kernel's (tests/bw_hmma_model.h, bw_qmma_model.h). When also ldc == n, |
| 18 | * n <= OMEGA_GPU_GEMM_MAX_DIM and no input is non-finite, the C3 kernel writes C |
| 19 | * itself, in parts of at most OMEGA_GPU_GEMM_MAX_DIM rows (no host tile). |
| 20 | * pairs within {bf16, e4m3, e5m2} both widened exactly to bf16, HMMA.BF16 |
| 21 | * pairs within {f16, e4m3, e5m2} both widened exactly to f16, HMMA (f16) |
| 22 | * anything with f32, and bf16 x f16 fp32-input GEMM via bf16x3: each value split exactly (truncation) into three bf16 |
| 23 | * planes, the nine plane products concatenated along k on HMMA.BF16, smallest first. |
| 24 | * Not exact fp32: accuracy is stated against a double reference in |
| 25 | * tests/gpu_gemm_ex_test.c. Parts of a value below 2^-133 are dropped. |
| 26 | * compute OMEGA_GEMM_EX_F32_FAST_BF16: one bf16 plane (round to nearest even). |
| 27 | * compute OMEGA_GEMM_EX_F32_FFMA_REF the reference comparator for any input types: every input widened to f32, one |
| 28 | * FFMA per k in increasing k from +0 (C3 type F32_FFMA); slow by design. |
| 29 | * Non-finite inputs (inf, NaN; e4m3 0x7f/0xff): the outputs in their row of op(A) or column of op(B) are recomputed on the |
| 30 | * host in double over the whole k, so inf and NaN propagate as IEEE arithmetic says. |
| 31 | * |
| 32 | * Scales: SCALE_TENSOR multiplies by scale_a[0] scale_b[0] (per batch at + b scale_stride_a / _b). SCALE_BLOCK: op(A) is cut |
| 33 | * into block_m x block_k blocks, scale_a row-major [ceil(m/block_m)][ceil(k/block_k)]; op(B) into block_k x block_n blocks, |
| 34 | * scale_b [ceil(k/block_k)][ceil(n/block_n)]; the k parts break at block_k. Scales must be finite. Any input type may be scaled. |
| 35 | * |
| 36 | * The decisions and arithmetic are seeded Omega (om/gemmex, CUDApp#144); src/omega_gpu_gemm_ex.c is their memory shell. |
| 37 | * Migration-pending C, kept as the differential oracle: build switch CUDAPP_MIGRATION_PENDING_GEMM_EX (mk/gemm-ex.mk, |
| 38 | * default 0: the Omega path; 1: the old C path; CUDAPP_MIGRATION_PENDING_GEMM_EX_DIFF=1 runs both and aborts on a difference). */ |
| 39 | #ifndef OMEGA_GPU_GEMM_EX_H |
| 40 | #define OMEGA_GPU_GEMM_EX_H |
| 41 | |
| 42 | #include <stdint.h> |
| 43 | #include "omega_gpu_gemm.h" |
| 44 | |
| 45 | #ifdef __cplusplus |
| 46 | extern "C" { |
| 47 | #endif |
| 48 | |
| 49 | #define OMEGA_GPU_GEMM_EX_MAX_DIM 0x7fffffffull |
| 50 | #define OMEGA_GPU_GEMM_EX_MAX_BATCH 0x7fffffffull |
| 51 | |
| 52 | typedef enum { OMEGA_GEMM_OP_N = 0, OMEGA_GEMM_OP_T = 1 } OmegaGemmOp; |
| 53 | typedef enum { |
| 54 | OMEGA_GEMM_DT_F32 = 0, OMEGA_GEMM_DT_BF16 = 1, OMEGA_GEMM_DT_F16 = 2, OMEGA_GEMM_DT_E4M3 = 3, OMEGA_GEMM_DT_E5M2 = 4 |
| 55 | } OmegaGemmDtype; |
| 56 | typedef enum { OMEGA_GEMM_EX_DEFAULT = 0, OMEGA_GEMM_EX_F32_FAST_BF16 = 1, OMEGA_GEMM_EX_F32_FFMA_REF = 2 } OmegaGemmExCompute; |
| 57 | typedef enum { OMEGA_GEMM_SCALE_NONE = 0, OMEGA_GEMM_SCALE_TENSOR = 1, OMEGA_GEMM_SCALE_BLOCK = 2 } OmegaGemmScale; |
| 58 | /* the route a call took (OmegaGemmExInfo.route) */ |
| 59 | typedef enum { |
| 60 | OMEGA_GEMM_ROUTE_NONE = 0, OMEGA_GEMM_ROUTE_NATIVE, OMEGA_GEMM_ROUTE_WIDEN, OMEGA_GEMM_ROUTE_BF16X3, |
| 61 | OMEGA_GEMM_ROUTE_FAST_BF16, OMEGA_GEMM_ROUTE_FFMA |
| 62 | } OmegaGemmRoute; |
| 63 | |
| 64 | typedef struct { |
| 65 | uint32_t route; /* OmegaGemmRoute of the product (NONE when alpha == 0) */ |
| 66 | uint32_t core_type; /* OmegaGpuGemmType the kernels ran */ |
| 67 | uint64_t core_calls; /* C3 calls made */ |
| 68 | uint64_t host_fixed; /* outputs recomputed on the host for non-finite inputs */ |
| 69 | } OmegaGemmExInfo; |
| 70 | |
| 71 | typedef struct { |
| 72 | OmegaGemmOp op_a, op_b; |
| 73 | uint64_t m, n, k, batch; /* batch 0 counts as 1 */ |
| 74 | OmegaGemmDtype a_type, b_type, c_type; |
| 75 | const void *a; uint64_t lda, stride_a; |
| 76 | const void *b; uint64_t ldb, stride_b; |
| 77 | void *c; uint64_t ldc, stride_c; |
| 78 | float alpha, beta; |
| 79 | OmegaGemmExCompute compute; |
| 80 | OmegaGemmScale scale; |
| 81 | const float *scale_a, *scale_b; |
| 82 | uint64_t scale_stride_a, scale_stride_b; /* per batch, elements (0: shared) */ |
| 83 | uint32_t block_m, block_n, block_k; /* SCALE_BLOCK only */ |
| 84 | OmegaGemmExInfo *info; /* optional */ |
| 85 | } OmegaGemmExArgs; |
| 86 | |
| 87 | /* OMEGA_GPU_GEMM_OK, _BAD_ARGS (malformed), _TOO_LARGE (a dimension or batch past the maxima), _UNSUPPORTED (fp8 C), |
| 88 | * _CODEGEN_FAIL (an Omega unit refused its window: a shell defect, never the caller's), _CHIP_FAIL (from the kernels or a |
| 89 | * host allocation) */ |
| 90 | int omega_gpu_gemm_ex(const OmegaGemmExArgs *args); |
| 91 | |
| 92 | /* The block-scaled fp8 weight product the fp8 GEMV and grouped GEMM fall back to for shapes their tuned kernels refuse |
| 93 | * (omega_gpu_fp8_api.h, omega_gpu_fp8gemm_api.h): |
| 94 | * y[r][j] = sum_c x[r][c] * e4m3(w[j][c]) * scale[j / block][c / block] * scale_mul, r < m, j < n, c < k |
| 95 | * w: n rows of k e4m3 bytes; scale: ceil(n/block) rows of ceil(k/block) f32; x: m rows of k, f32 (x32) or bf16 (x16), exactly |
| 96 | * one non-NULL; y: m rows of n f32 at ldy (0: n). Any m, n, k, block >= 1. Each weight is dequantized to f32 with one rounding |
| 97 | * (exact when the scale has <= 20 significant bits, as the fp8 tensors' bf16 scales do), then the product runs as |
| 98 | * omega_gpu_gemm_ex f32 x f32 (the bf16x3 route, its stated accuracy). Every output element's sum is independent of m, so |
| 99 | * a row computed in a batch of m equals the same row computed alone. Returns the omega_gpu_gemm_ex codes. */ |
| 100 | int omega_gpu_gemm_ex_fp8_block_rows(const uint8_t *w, const float *scale, double scale_mul, uint32_t n, uint32_t k, uint32_t block, |
| 101 | const float *x32, const uint16_t *x16, uint32_t m, float *y, uint64_t ldy, OmegaGemmExInfo *info); |
| 102 | |
| 103 | /* The kernels the product runs on: default the chip (omega_gpu_gemm_ld). Tests install a host model or the warp simulator; |
| 104 | * fn NULL restores the chip. C is dense M x N f32. */ |
| 105 | typedef int (*OmegaGemmExCoreFn)(void *ctx, OmegaGpuGemmType type, unsigned layout, uint32_t M, uint32_t N, uint32_t K, |
| 106 | const void *A, uint64_t lda, const void *B, uint64_t ldb, float *C); |
| 107 | void omega_gpu_gemm_ex_set_backend(OmegaGemmExCoreFn fn, void *ctx); |
| 108 | /* Red checks: 1 the last k element of every product is dropped, 2 op_b is read as OP_N, 3 the block scale of the next k |
| 109 | * block is used, 4 beta is ignored, 5 omega_gpu_gemm_ex_fp8_block_rows dequantizes with the next k block's scale; 0 off. */ |
| 110 | void omega_gpu_gemm_ex_test_set_mutant(int mutant); |
| 111 | |
| 112 | #ifdef __cplusplus |
| 113 | } |
| 114 | #endif |
| 115 | #endif /* OMEGA_GPU_GEMM_EX_H */ |