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_matmul_api.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | #ifndef OMEGA_GPU_MATMUL_API_H |
| 3 | #define OMEGA_GPU_MATMUL_API_H |
| 4 | /* |
| 5 | * omega_gpu_matmul_api: the native (no CUDA) matrix-multiply entry point for |
| 6 | * the AIEN inference stack (FB-1 cut 1, 2026-10-03; cut 1b speed, 2026-10-04). |
| 7 | * |
| 8 | * Inputs are row-major bf16 (A: m x k, B: k x n), output is row-major f32 |
| 9 | * (C: m x n). Kernels are generated by our own codegen and cached in-process. |
| 10 | * |
| 11 | * Cut 1b: |
| 12 | * - the whole K dimension is accumulated on the chip (looped HMMA kernel, |
| 13 | * omega_blackwell_codegen_matmul_tensor_loop); no host K slicing; |
| 14 | * - the device and its channel are opened once per process and reused; |
| 15 | * - weights are uploaded once (omega_gpu_tensor_upload_*) and stay resident; |
| 16 | * omega_gpu_matmul_resident_* then moves only the activation rows and the |
| 17 | * result per call. The original omega_gpu_matmul_* calls keep working (they |
| 18 | * upload B, run, free) and keep running the host oracle per call as cut 1 |
| 19 | * promised; the resident calls never run an oracle (speed), the caller or |
| 20 | * the test holds the reference. |
| 21 | * - every call poisons the output rows on the device before the launch and |
| 22 | * refuses a result that still holds poison (OUTPUT_UNCHANGED -> CHIP_FAIL). |
| 23 | * - an uncertain completion (marker wait timed out) latches the process: |
| 24 | * nothing is freed or closed (the GPU may still be running) and every later |
| 25 | * call returns CHIP_FAIL, as the engine does (omega_gpu_engine.h). |
| 26 | */ |
| 27 | #include <stdbool.h> |
| 28 | #include <stddef.h> |
| 29 | #include <stdint.h> |
| 30 | |
| 31 | #ifdef __cplusplus |
| 32 | extern "C" { |
| 33 | #endif |
| 34 | |
| 35 | enum { |
| 36 | OMEGA_GPU_MATMUL_OK = 0, |
| 37 | OMEGA_GPU_MATMUL_BAD_ARGS = -1, /* NULL pointer, zero dimension, shape mismatch, out of memory */ |
| 38 | OMEGA_GPU_MATMUL_TOO_LARGE = -2, /* a dimension exceeds OMEGA_BW_MATMUL_MAX_* */ |
| 39 | OMEGA_GPU_MATMUL_CODEGEN_FAIL = -3, /* our codegen refused the shape */ |
| 40 | OMEGA_GPU_MATMUL_CHIP_FAIL = -4, /* device open, alloc, submit, marker, release or output-written check failed */ |
| 41 | OMEGA_GPU_MATMUL_PARITY_FAIL = -5 /* chip result disagrees with the host oracle (non-resident calls only) */ |
| 42 | }; |
| 43 | |
| 44 | /* Kernel tile rule (omega_blackwell_codegen_matmul_tensor_loop): M multiple of |
| 45 | * 16, N multiple of 8, K multiple of 16. The API zero-pads: A rows/columns on |
| 46 | * the way in per call, B at upload, and reads back only the real m x n block. */ |
| 47 | #define OMEGA_GPU_MATMUL_TILE_M 16 |
| 48 | #define OMEGA_GPU_MATMUL_TILE_N 8 |
| 49 | #define OMEGA_GPU_MATMUL_TILE_K 16 |
| 50 | /* Launch envelope. Cut 1 (2026-10-03 sweep, evidence GPU-MATMUL-API-2ea8e67, |
| 51 | * investigation I42) saw grids of 128 CTAs and above lose output with the |
| 52 | * single-tile kernel. The looped kernel does not need big grids: each CTA walks |
| 53 | * column tiles x, x + grid_x, ... so any shape is one launch inside the budget |
| 54 | * (rows are split across launches only when m / 16 alone exceeds it). The |
| 55 | * budget is a tunable (omega_gpu_matmul_set_cta_budget) so the chip test can |
| 56 | * measure larger grids. |
| 57 | * Default 1024 since omega #473 R4 (sealed receipt evidence/R4-MATMUL-CTA-ffc3176a, |
| 58 | * tests/gpu_matmul_r4_receipt.c): every output element of every call compared |
| 59 | * with a double reference at 8192x768x{2304,768,3072}, 8192x3072x768, |
| 60 | * 1024x768x50304 and two single-tile-kernel shapes (1x2048x8192, 112x768x3072), |
| 61 | * budgets 64..1024, 3 repeats per sign, bit-identical repeats and runs, no |
| 62 | * stale (I42) element at any grid of 128 CTAs and up, mutant caught. 1024 has |
| 63 | * the least chip time summed over all seven shapes; it was 64 before. The |
| 64 | * speedup is for the five training shapes only (1.3x-1.7x less chip time than |
| 65 | * at 64). The small shape 112x768x3072 is 1.1x-2x SLOWER at 1024 than at 64 |
| 66 | * (0.32 ms at 64, 0.62 ms at 1024 in run 2); callers dominated by small |
| 67 | * shapes can lower the budget with omega_gpu_matmul_set_cta_budget. */ |
| 68 | #define OMEGA_GPU_MATMUL_MAX_CTAS 1024 |
| 69 | |
| 70 | typedef struct { |
| 71 | uint32_t m, k, n; |
| 72 | uint32_t padded_m, padded_n;/* shape handed to the kernel */ |
| 73 | uint32_t k_slices; /* always 1 since cut 1b (whole K on the chip) */ |
| 74 | uint32_t chip_calls; /* launches made (row blocks) */ |
| 75 | uint32_t rows_per_call; /* row block height chosen for the CTA envelope */ |
| 76 | uint64_t elapsed_ns; /* sum of chip launch-to-completion over the launches */ |
| 77 | float max_abs_err; /* vs host oracle (non-resident calls only, else 0) */ |
| 78 | float max_rel_err; /* vs host oracle, relative to the accumulation scale */ |
| 79 | size_t mismatch_count; /* host oracle elements beyond tolerance (non-resident calls only) */ |
| 80 | bool parity_verified; /* oracle ran and every element agreed (always false for resident calls) */ |
| 81 | uint32_t completion_marker; /* observed first marker word of the last launch */ |
| 82 | bool kernel_cache_hit; /* every launch found its kernel already generated */ |
| 83 | char target_chip[64]; |
| 84 | uint32_t sm_architecture; |
| 85 | /* cut 1b additions (appended; earlier fields keep their offsets) */ |
| 86 | uint32_t grid_x, grid_y; /* grid of the last launch */ |
| 87 | uint32_t padded_k; |
| 88 | bool resident; /* B came from a resident tensor handle */ |
| 89 | bool oracle_ran; /* the host oracle was computed for this call */ |
| 90 | uint64_t call_ns; /* wall time of the whole API call (copy-in, launch, wait, copy-out) */ |
| 91 | } OmegaGpuMatmulInfo; |
| 92 | |
| 93 | /* Resident weight tensor: k x n bf16, zero-padded to the tile rule, kept in |
| 94 | * GPU-uncached unified memory until freed. Opaque. */ |
| 95 | typedef struct OmegaGpuTensor OmegaGpuTensor; |
| 96 | |
| 97 | /* Upload a k x n row-major matrix once. f32 input is rounded to bf16 on the |
| 98 | * host (round-to-nearest-even). Opens the device on first use. */ |
| 99 | int omega_gpu_tensor_upload_bf16(uint32_t k, uint32_t n, const uint16_t *b, OmegaGpuTensor **out); |
| 100 | int omega_gpu_tensor_upload_f32(uint32_t k, uint32_t n, const float *b, OmegaGpuTensor **out); |
| 101 | void omega_gpu_tensor_free(OmegaGpuTensor *t); |
| 102 | void omega_gpu_tensor_shape(const OmegaGpuTensor *t, uint32_t *k, uint32_t *n); |
| 103 | |
| 104 | /* Replace a resident tensor's values in place (a training step's new weights): b is k x n row-major and |
| 105 | * k, n must equal the tensor's shape (BAD_ARGS otherwise, nothing written); f32 is rounded to bf16 as at |
| 106 | * upload. The new values are packed straight into the tensor's existing GPU-uncached device buffer: no |
| 107 | * driver allocation or free and no host allocation, so it is allowed while the API is sealed and has no |
| 108 | * out-of-memory path. Refused (CHIP_FAIL, nothing written) when the session is latched or the handle's |
| 109 | * device was closed; BAD_ARGS for NULL arguments. |
| 110 | * |
| 111 | * Thread safety of the resident calls: omega_gpu_matmul_resident_* and omega_gpu_tensor_update_* |
| 112 | * each hold the session lock for the whole call, and a launch has completed before its call |
| 113 | * returns, so a resident matmul reads B entirely from before or entirely from after an update, |
| 114 | * never a mix, and an update never writes while the chip reads. upload builds a new buffer and |
| 115 | * needs no ordering. The caller must not free a tensor while another thread uses it. */ |
| 116 | int omega_gpu_tensor_update_bf16(OmegaGpuTensor *t, uint32_t k, uint32_t n, const uint16_t *b); |
| 117 | int omega_gpu_tensor_update_f32(OmegaGpuTensor *t, uint32_t k, uint32_t n, const float *b); |
| 118 | |
| 119 | /* C[m x n] = A[m x k] * B, B resident (k and n come from the handle). No host |
| 120 | * oracle: info->parity_verified is false, mismatch_count 0. */ |
| 121 | int omega_gpu_matmul_resident_bf16(uint32_t m, const uint16_t *a, const OmegaGpuTensor *b, float *c, |
| 122 | OmegaGpuMatmulInfo *info); |
| 123 | int omega_gpu_matmul_resident_f32(uint32_t m, const float *a, const OmegaGpuTensor *b, float *c, |
| 124 | OmegaGpuMatmulInfo *info); |
| 125 | |
| 126 | /* C[m x n] = A[m x k] * B[k x n], bf16 inputs, f32 output. info may be NULL. |
| 127 | * Uploads B for the call, runs, frees it, and checks the result against the |
| 128 | * host oracle (double accumulation, tolerance 1e-5 of the accumulation scale). */ |
| 129 | int omega_gpu_matmul_bf16(uint32_t m, uint32_t k, uint32_t n, |
| 130 | const uint16_t *a, const uint16_t *b, float *c, |
| 131 | OmegaGpuMatmulInfo *info); |
| 132 | |
| 133 | /* Same, taking f32 inputs: they are rounded to bf16 on the host first |
| 134 | * (round-to-nearest-even), so the result carries bf16 input precision. */ |
| 135 | int omega_gpu_matmul_f32(uint32_t m, uint32_t k, uint32_t n, |
| 136 | const float *a, const float *b, float *c, |
| 137 | OmegaGpuMatmulInfo *info); |
| 138 | |
| 139 | /* Name of a return code, for logs and receipts. */ |
| 140 | const char *omega_gpu_matmul_rc_name(int rc); |
| 141 | |
| 142 | /* Drop every cached kernel (host and device copies). */ |
| 143 | void omega_gpu_matmul_cache_clear(void); |
| 144 | |
| 145 | /* Close the device and free every API-owned buffer (not resident tensors the |
| 146 | * caller still holds; free those first). Refused (no-op) after an uncertain |
| 147 | * completion. The next call reopens. */ |
| 148 | void omega_gpu_device_close(void); |
| 149 | |
| 150 | /* 1 once an uncertain completion has latched this process. */ |
| 151 | int omega_gpu_matmul_is_blocked(void); |
| 152 | |
| 153 | /* Stage name of the most recent CHIP_FAIL ("" if none), for logs and receipts. */ |
| 154 | const char *omega_gpu_matmul_last_error(void); |
| 155 | |
| 156 | /* Where the time of one matmul goes (omega #473). A separate struct, not new OmegaGpuMatmulInfo fields: |
| 157 | * Rust callers mirror OmegaGpuMatmulInfo by size (no size assert), and the API memsets the caller's |
| 158 | * struct, so growing it would overrun every mirror that has not been updated. The stages tile the |
| 159 | * call's work in order; core_ns is their span, so core_ns - (the six stages) is clock overhead only. |
| 160 | * setup_ns lock wait, device open, launch shape, staging buffer growth |
| 161 | * pack_ns activation rows packed into the GPU-visible staging buffer (f32 -> bf16 on the way), |
| 162 | * plus the result sentinels |
| 163 | * upload_ns CPU-to-GPU visibility of both staging buffers (platform sync) |
| 164 | * launch_ns kernel cache lookup (or build), submit and completion wait, every launch; contains chip_ns |
| 165 | * chip_ns = OmegaGpuMatmulInfo.elapsed_ns (launch to completion marker, summed over launches) |
| 166 | * sync_ns GPU-to-CPU visibility of the result buffer |
| 167 | * copy_out_ns sentinel scan and copy of the real m x n block into the caller's array |
| 168 | * The non-resident calls add the B upload before and the host oracle after (in OmegaGpuMatmulInfo.call_ns, |
| 169 | * not here). */ |
| 170 | typedef struct { |
| 171 | uint64_t setup_ns, pack_ns, upload_ns, launch_ns, chip_ns, sync_ns, copy_out_ns; |
| 172 | uint64_t core_ns; /* entry to exit of the matmul body; a resident call's call_ns adds argument checks only */ |
| 173 | } OmegaGpuMatmulStages; |
| 174 | /* Stages of the most recent matmul in this process. The record is ONE per process, shared by every thread: |
| 175 | * another thread's matmul can overwrite it between your call and this read, so read it from the thread that |
| 176 | * made the call, with no other matmul in between. Calls are serialized by the session lock, so a read never sees |
| 177 | * a half-written record. All zero before the first call; a call that fails records the stages it reached. */ |
| 178 | void omega_gpu_matmul_last_stages(OmegaGpuMatmulStages *out); |
| 179 | |
| 180 | /* CTA budget per launch (default OMEGA_GPU_MATMUL_MAX_CTAS; 0 restores it). |
| 181 | * Clears the kernel cache (kernels bake in grid_x). Test and measurement knob. */ |
| 182 | void omega_gpu_matmul_set_cta_budget(uint32_t ctas); |
| 183 | uint32_t omega_gpu_matmul_cta_budget(void); |
| 184 | |
| 185 | /* OPT-IN serving reservation (omega_gpu_reserve_serving in omega_gpu_serving.h calls this). Allocates the |
| 186 | * activation and result staging buffers once for calls of up to max_rows rows over weights up to max_k x max_n |
| 187 | * (max_n_one_row, 0 = none: a wider n allowed for calls of at most 16 rows, e.g. lm_head), and makes the kernel |
| 188 | * cache kernel_slots deep (0 or <= 8 = unchanged; at most 128). From then on a call that fits never allocates |
| 189 | * staging; a call that does not fit returns OMEGA_GPU_MATMUL_TOO_LARGE with omega_gpu_matmul_last_error() saying |
| 190 | * "serving reservation exceeded", and asks the driver for nothing. Never called = behaviour unchanged. The kernel |
| 191 | * cache still builds each NEW shape once (one small code buffer); a slot count at least the number of distinct |
| 192 | * shapes keeps them for good. 0 on success, else an OMEGA_GPU_MATMUL_* code. */ |
| 193 | int omega_gpu_matmul_reserve(uint32_t max_rows, uint32_t max_k, uint32_t max_n, uint32_t max_n_one_row, uint32_t kernel_slots); |
| 194 | |
| 195 | /* OPT-IN, after omega_gpu_matmul_reserve: build now the kernel a call of m rows over a k x n weight will use, and |
| 196 | * pin it (a prepared kernel is never evicted). The kernel cache key bakes in grid_x, which depends on the row |
| 197 | * count, so a server prepares every (row count, weight shape) it will run; repeated keys are cache hits. Refused |
| 198 | * with TOO_LARGE, before any build or driver call, when every slot already holds a prepared kernel. */ |
| 199 | int omega_gpu_matmul_prepare(uint32_t m, uint32_t k, uint32_t n); |
| 200 | /* on = 1: from now on a call whose kernel is not cached is refused with OMEGA_GPU_MATMUL_TOO_LARGE ("serving |
| 201 | * reservation exceeded: matmul kernel ... was not prepared") and asks the driver for nothing. 0 lifts it; closing |
| 202 | * the device also ends it. */ |
| 203 | void omega_gpu_matmul_seal(int on); |
| 204 | /* Drop the reservation flag (buffers stay allocated, growth is allowed again). */ |
| 205 | void omega_gpu_matmul_unreserve(void); |
| 206 | |
| 207 | /* Host oracle on the non-resident calls (default on). */ |
| 208 | void omega_gpu_matmul_set_oracle(int on); |
| 209 | |
| 210 | /* TEST ONLY. Non-zero generates deliberately wrong kernels (last K step |
| 211 | * dropped) so the parity test can prove it catches a wrong kernel. Clears the |
| 212 | * kernel cache. Production code must never call this. */ |
| 213 | void omega_gpu_matmul_test_set_mutant(int on); |
| 214 | /* Test only: force the fragment kernel unroll (1, 2, 4 or 8; it must divide kp / 16 or the call |
| 215 | * fails CODEGEN_FAIL); 0 restores the automatic choice. Clears the kernel cache. */ |
| 216 | void omega_gpu_matmul_test_set_unroll(uint32_t unroll); |
| 217 | /* Test hook: one-row calls so far that wrote only row 0 into a buffer already zero in rows 1..15. */ |
| 218 | uint64_t omega_gpu_matmul_test_a_row0_hits(void); |
| 219 | |
| 220 | #ifdef __cplusplus |
| 221 | } |
| 222 | #endif |
| 223 | #endif |