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

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