interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / src / omega_gpu_fp8_api.h

256 lines · 20161 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2#ifndef OMEGA_GPU_FP8_API_H
3#define OMEGA_GPU_FP8_API_H
4/*
5 * omega_gpu_fp8_api: native (no CUDA) FP8 e4m3 block-scaled GEMV for the OWN-30B lane
6 * (Cut 6: m = 1 decode GEMV; Cut 5: grouped experts, one launch for the selected experts).
7 * New file on the shared session (omega_gpu_session.h); no existing API file changes.
8 *
9 * ============ C signatures and semantics (for the Rust binding) ============
10 *
11 * typedef struct OmegaGpuFp8Tensor OmegaGpuFp8Tensor; // opaque, device resident
12 *
13 * int omega_gpu_fp8_tensor_upload(uint32_t experts, uint32_t n, uint32_t k,
14 * const uint8_t *w, const uint16_t *scale_inv_bf16,
15 * OmegaGpuFp8Tensor **out);
16 * w: experts * n * k bytes, e4m3, row-major [expert][n][k] (n output rows, k input columns):
17 * exactly the checkpoint's `weight` for one projection, experts concatenated (the sc
18 * capsule GATE_UP [128][1536][2048] and DOWN [128][2048][768] layouts; attention
19 * projections have experts = 1, e.g. q_proj [4096][2048]).
20 * scale_inv_bf16: experts * (n/128) * (k/128) bf16, [expert][n/128][k/128] (`weight_scale_inv`).
21 * Requires n % 128 == 0, k % 128 == 0, 128 <= k <= 32768 (omega#541), experts >= 1. Refuses any e4m3 NaN
22 * code (0x7F, 0xFF) in w with OMEGA_GPU_FP8_NAN_WEIGHT (nothing uploaded), a NaN/inf scale with
23 * BAD_ARGS. Weights are copied byte for byte (no reorder); scales are stored as f32 * 2^56.
24 * Opens the device on first use. Resident until omega_gpu_fp8_tensor_free.
25 *
26 * int omega_gpu_fp8_gemv_f32 (const OmegaGpuFp8Tensor *t, uint32_t expert, const float *x, float *y, OmegaGpuFp8Info *info);
27 * int omega_gpu_fp8_gemv_bf16(const OmegaGpuFp8Tensor *t, uint32_t expert, const uint16_t *x, float *y, OmegaGpuFp8Info *info);
28 * y[r] = sum_c W[expert][r][c] * x[c] * scale[expert][r/128][c/128], r < n, c < k. f32 accumulate.
29 * x: k values (f32, or bf16 bits), y: n f32. One launch. info may be NULL.
30 *
31 * int omega_gpu_fp8_gemv_grouped_f32(const OmegaGpuFp8Tensor *t, uint32_t groups, const uint32_t *expert_ids,
32 * const float *x, uint32_t x_rows, float *y, OmegaGpuFp8Info *info);
33 * One launch for `groups` experts (1..OMEGA_GPU_FP8_MAX_GROUPS, each id < experts, repeats allowed):
34 * y[g][r] = sum_c W[expert_ids[g]][r][c] * x[g'][c] * scale[...], with g' = 0 when x_rows == 1
35 * (one activation row shared by every group: the gate+up projections of 8 experts) or g' = g when
36 * x_rows == groups (one row per group: the down projections). x: x_rows * k f32, y: groups * n f32.
37 * The m = 1 call above is this call with groups = 1.
38 *
39 * General fallback (GemmGeneral, 2026-10-27): these three host-staged calls accept any groups >= 1 and any k the tensor
40 * holds. A call the tuned kernels refuse (groups past OMEGA_GPU_FP8_MAX_GROUPS; a k the GEMV codegen refuses, e.g.
41 * k % 256 != 0 at the default lane width: k = 128, 384, 640) runs omega_gpu_gemm_ex_fp8_block_rows per group instead
42 * (weights dequantized exactly, fp32-input GEMM via bf16x3; info->general = 1, the kernel-shape fields stay 0). Not
43 * when a test override (lane width, rows, warps, depth) or a kernel mutant is set: those calls keep the refusal. The
44 * device-table and record forms (grouped_dev, grouped_record) keep the kernels' domain, extended by the ragged-k kernel
45 * (below, omega_gpu_fp8_ragged_for): a k that is a multiple of 128 but not of 32 * lane_bytes (k = 640, the Qwen3.8-Flash-Next
46 * expert down_proj) runs the kernel there instead of being refused. The host-staged forms keep the fallback above.
47 *
48 * int omega_gpu_fp8_prepare(uint32_t k); build and pin the kernel for width k now (serving).
49 * void omega_gpu_fp8_tensor_free(OmegaGpuFp8Tensor *t);
50 * void omega_gpu_fp8_tensor_shape(const OmegaGpuFp8Tensor *t, uint32_t *experts, uint32_t *n, uint32_t *k);
51 * const char *omega_gpu_fp8_rc_name(int rc); const char *omega_gpu_fp8_last_error(void);
52 *
53 * Return codes: 0 OK; negative OMEGA_GPU_FP8_* below. An uncertain completion latches the process
54 * (every later call of every API returns CHIP_FAIL), as the other APIs do.
55 *
56 * Every output word is poisoned before the launch and the call refuses a result that still holds
57 * poison (CHIP_FAIL "output poison survived"). No host oracle runs inside the call (speed); the test
58 * holds the reference (omega_fp8_gemv_oracle below).
59 */
60#include <stdbool.h>
61#include <stddef.h>
62#include <stdint.h>
63#include "omega_blackwell_codegen_moe.h" /* OmegaFp8GemvSpec (depth query) */
64
65#ifdef __cplusplus
66extern "C" {
67#endif
68
69enum {
70 OMEGA_GPU_FP8_OK = 0,
71 OMEGA_GPU_FP8_BAD_ARGS = -1, /* NULL, zero or misaligned dimension, bad expert id, out of host memory */
72 OMEGA_GPU_FP8_TOO_LARGE = -2, /* k past OMEGA_FP8_MAX_K, groups past the table, sealed cache miss */
73 OMEGA_GPU_FP8_CODEGEN_FAIL = -3, /* our codegen refused the shape */
74 OMEGA_GPU_FP8_CHIP_FAIL = -4, /* device open, alloc, submit, marker or output-written check failed */
75 OMEGA_GPU_FP8_NAN_WEIGHT = -5 /* an e4m3 NaN code in the weights (refused at upload) */
76};
77
78#define OMEGA_GPU_FP8_MAX_GROUPS 64u
79#define OMEGA_GPU_FP8_MAX_CTAS 256u /* default CTA budget per group for a single projection; the elementwise and attention rows paths proved 1024 */
80#define OMEGA_GPU_FP8_BIGK_CTAS 384u /* big-k rule: CTA budget of a single projection with k > 4096 (Qwen3.8-27B), split evenly (2026-10-19) */
81#define OMEGA_GPU_FP8_GROUPED_CTAS 6u /* CTAs per group for a grouped launch (32 warps each): measured best for 8 experts */
82#define OMEGA_GPU_SM_COUNT 48u /* GB10: 48 SMs (NVIDIA GB10 Superchip Whitepaper WP-12718-001_v02, Table 4); one 32-warp CTA per SM (CC 12.x, 1536 resident threads, CUDA Programming Guide Table 30) */
83
84typedef struct {
85 uint32_t experts, n, k, groups;
86 uint32_t lane_bytes, rows_per_warp, warps_per_cta; /* kernel shape chosen for the call */
87 uint32_t depth; /* 0: step-by-step kernel; D: deep kernel, D K steps of loads in flight, x' in shared memory */
88 uint32_t grid_x, grid_y, steps;
89 uint64_t weight_bytes; /* e4m3 bytes the launch reads (groups * n * k) */
90 uint64_t scale_bytes; /* f32 scale words it reads */
91 uint64_t elapsed_ns; /* chip submit-to-second-marker */
92 uint64_t call_ns; /* whole API call */
93 uint32_t completion_marker;
94 bool kernel_cache_hit;
95 char target_chip[64];
96 uint32_t sm_architecture;
97 uint32_t general; /* 1: the call ran the general fallback (omega_gpu_gemm_ex_fp8_block_rows), not the GEMV kernel */
98} OmegaGpuFp8Info;
99
100typedef struct OmegaGpuFp8Tensor OmegaGpuFp8Tensor;
101
102int omega_gpu_fp8_tensor_upload(uint32_t experts, uint32_t n, uint32_t k, const uint8_t *w,
103 const uint16_t *scale_inv_bf16, OmegaGpuFp8Tensor **out);
104/* Host-only quantiser (qwen38/decode-speed, the FP8 lm_head): bf16 [n][k] -> e4m3 [n][k] + scale_inv bf16 [n/128][k/128], the
105 * upload format above. Per 128 x 128 block: scale = max|w| / 448 rounded up to bf16 (1 for an all-zero block), w8 = e4m3(w / scale),
106 * round to nearest even, saturating at 448 (never the NaN code). n, k multiples of 128. OMEGA_GPU_FP8_BAD_ARGS on an inf/NaN weight. */
107int omega_gpu_fp8_quantize_bf16(uint32_t n, uint32_t k, const uint16_t *w_bf16, uint8_t *w8, uint16_t *scale_inv_bf16);
108uint8_t omega_gpu_fp8_e4m3_rne(float v); /* one value, the rounding the quantiser uses */
109void omega_gpu_fp8_tensor_free(OmegaGpuFp8Tensor *t);
110void omega_gpu_fp8_tensor_shape(const OmegaGpuFp8Tensor *t, uint32_t *experts, uint32_t *n, uint32_t *k);
111
112int omega_gpu_fp8_gemv_f32(const OmegaGpuFp8Tensor *t, uint32_t expert, const float *x, float *y, OmegaGpuFp8Info *info);
113int omega_gpu_fp8_gemv_bf16(const OmegaGpuFp8Tensor *t, uint32_t expert, const uint16_t *x, float *y, OmegaGpuFp8Info *info);
114int omega_gpu_fp8_gemv_grouped_f32(const OmegaGpuFp8Tensor *t, uint32_t groups, const uint32_t *expert_ids,
115 const float *x, uint32_t x_rows, float *y, OmegaGpuFp8Info *info);
116/* Device-resident inputs (OWN-30B router, 2026-10-13): the group table ({w_va, s_va} per group, `groups` entries) and
117 * the single x' row (f32, k values, already * 2^64) are read from tab_va / x_va, which another launch (the router)
118 * wrote; no host copy of either. y and the poison check as above. */
119int omega_gpu_fp8_gemv_grouped_dev(const OmegaGpuFp8Tensor *t, uint32_t groups, uint64_t tab_va, uint64_t x_va, float *y, OmegaGpuFp8Info *info);
120/* Record path (OWN-30B W-L2a, decode graph; omega_gpu_cmdlist.h): the same launch as grouped_dev, appended to cl instead
121 * of submitted. tab_va: `groups` {w_va, s_va} entries (omega_gpu_fp8_tensor_entry, or the router's table); x_va: x_rows
122 * rows of k f32 already * 2^64 (1 row shared, or one per group); y_va: groups * n f32, written when the list runs.
123 * The caller holds omega_gpu_session_lock() (do not call another API's non-record entry point while holding it).
124 * Nothing is staged, poisoned or read back: the list's per-launch semaphore check and the caller's end-of-token checks
125 * replace the poison check. A kernel cache miss that would evict a resident kernel is refused (TOO_LARGE): warm the
126 * shape up with one non-record call before recording. */
127struct OmegaGpuCmdList;
128int omega_gpu_fp8_gemv_grouped_record(struct OmegaGpuCmdList *cl, const OmegaGpuFp8Tensor *t, uint32_t groups, uint64_t tab_va,
129 uint64_t x_va, uint32_t x_rows, uint64_t y_va);
130/* Device addresses of one expert's weights and scales (for a directory the router copies from). */
131int omega_gpu_fp8_tensor_entry(const OmegaGpuFp8Tensor *t, uint32_t expert, uint64_t *w_va, uint64_t *s_va);
132
133/* Serving: build and pin the kernel for width k (the one the automatic shape choice uses for that k). */
134int omega_gpu_fp8_prepare(uint32_t k);
135/* on = 1: a call whose kernel is not cached is refused with TOO_LARGE instead of built. */
136void omega_gpu_fp8_seal(int on);
137
138const char *omega_gpu_fp8_rc_name(int rc);
139const char *omega_gpu_fp8_last_error(void);
140void omega_gpu_fp8_cache_clear(void);
141
142/* ---- measurement knobs (tests; each clears the kernel cache) ---- */
143/* Kernel shape override: lane_bytes 8/16, rows 1/2/4/8, warps 1..32; 0 = automatic. */
144void omega_gpu_fp8_set_shape(uint32_t lane_bytes, uint32_t rows_per_warp, uint32_t warps_per_cta);
145/* Kernel depth override: set = 0 automatic (deep for a single projection of at most 4 MiB whose row fits: rows * k <= 4096), set = 1 forces `depth`
146 * (0 = step-by-step kernel). Each call clears the kernel cache. */
147void omega_gpu_fp8_set_depth(int set, uint32_t depth);
148/* The depth the automatic choice makes for a spec (k, lane_bytes, rows, warps filled in), n rows, `groups` experts:
149 * deep only for a single projection of at most 4 MiB of weights (measured, see omega_gpu_fp8_api.c). */
150uint32_t omega_gpu_fp8_depth_for(const OmegaFp8GemvSpec *spec, uint32_t n, uint32_t groups);
151/* The deep default was measured (SPEED-1B, 2026-10-17) while the launch descriptor allowed 16 KiB of shared memory; the
152 * 32 KiB cap (#559, Qwen3.8 bf16 GEMV) would newly admit the 30B o_proj (16464 bytes), which no run has measured. The
153 * automatic choice keeps the measured bound; set_depth still reaches anything the descriptor allows. */
154#define OMEGA_GPU_FP8_DEEP_MAX_SHARED_BYTES 16384u
155/* Register-file check for a spec (chip-found rule: 32 * warps * declared registers <= 65536, declared = peak + 1 rounded
156 * up to 8). OK, TOO_LARGE (last_error names the counts) or CODEGEN_FAIL. The automatic depth choice and every build apply it. */
157int omega_gpu_fp8_kernel_fits(const OmegaFp8GemvSpec *spec, uint32_t *declared);
158/* CTAs per group per launch (0 restores the automatic choice: MAX_CTAS single, grouped_ctas_auto(groups) grouped). */
159void omega_gpu_fp8_set_cta_budget(uint32_t ctas);
160/* Grouped-launch CTA rule (L310, 2026-10-24). rule(groups, forced, wave): forced in 1..MAX_CTAS wins; else wave != 0 gives
161 * min(GROUPED_CTAS, SM_COUNT / groups) (at least 1), the count that keeps groups x CTAs inside one wave of the 48 SMs;
162 * else the compiled GROUPED_CTAS. auto(groups) applies the rule with the env read once at the first call:
163 * AIEN_OMEGA_FP8_GROUPED_CTAS=n (exact, experiments) and AIEN_OMEGA_FP8_GROUPED_WAVE=0 (the compiled 6 for every group
164 * count). Default: the wave rule (A1, 2026-10-27): 8 groups keep 6; the 35B's 9 groups get 5 instead of 54 CTAs in two
165 * waves (chip A/B, bit-identical outputs: gate_up + down median 164.698 -> 138.776 us per layer,
166 * evidence/DECODE-KERNELS-20261027T0612Z; earlier L310 6.962 -> 5.874 ms per step). Servers log auto(). */
167uint32_t omega_gpu_fp8_grouped_ctas_rule(uint32_t groups, uint32_t forced, int wave);
168uint32_t omega_gpu_fp8_grouped_ctas_auto(uint32_t groups);
169/* 1: tensors uploaded from now on live in GPU-cached memory (read-only weights; omega_gpu_mem_cached.h). Default 0:
170 * GPU-uncached like every session buffer. Tensors already uploaded keep their policy. Never set: the first upload reads env
171 * AIEN_OMEGA_FP8_WEIGHT_CACHE ("1" = cached; W-L2d, so sc can switch it without a code change). */
172void omega_gpu_fp8_set_weight_cache(int on);
173/* GEMV variant (W-L2c, 2026-10-14): 1 = omega_blackwell_codegen_moe.c (the default, unchanged), 2 = omega_blackwell_codegen_moe2.c
174 * (row-pipelined: the whole row group in flight per warp and the next row group's loads in flight while this one is multiplied;
175 * same arithmetic order, y bit-identical). Read once from AIEN_OMEGA_FP8_GEMV ("2") at the first call unless set here first.
176 * Each call clears the kernel cache. Variant 2 defaults: OMEGA_GPU_FP8_V2_WARPS warps per CTA, OMEGA_GPU_FP8_V2_GROUPED_CTAS CTAs
177 * per group for grouped launches (the shape and budget knobs above still override). */
178void omega_gpu_fp8_set_variant(int variant);
179int omega_gpu_fp8_variant(void);
180/* Kernel schedule (CUT5, measurement knob; clears the kernel cache): 0 (default) = the emitted stalls, 1 = the bytes of a step
181 * decoded in a software pipeline plus the dependency-driven stall pass (OmegaFp8GemvSpec.sched; y bit-identical to 0). Variant 1
182 * only (variant 2 ignores it). Never set: the first kernel build reads env AIEN_OMEGA_FP8_SCHED ("1" = 1). */
183void omega_gpu_fp8_set_sched(int sched);
184int omega_gpu_fp8_sched(void);
185/* CUT5 variants (variant 1 only; each setter clears the kernel cache; never set: the env is read once at the first build).
186 * A, coalesced output (OmegaFp8GemvSpec.coal; applied where warps * rows <= 32): ON by default since SPEED-A (2026-10-17,
187 * OWN-30B decode graph 24.49 -> 21.34 ms/token, tokens identical to golden); AIEN_OMEGA_FP8_COAL=0 or set_coal(0) restores
188 * the earlier kernel (one 4-byte STG per row, no shared slots).
189 * B, weight loads before the x loads in each K step (OmegaFp8GemvSpec.wfirst): OFF by default; AIEN_OMEGA_FP8_WFIRST=1 turns it on. */
190void omega_gpu_fp8_set_coal(int on);
191int omega_gpu_fp8_coal(void);
192void omega_gpu_fp8_set_wfirst(int on);
193int omega_gpu_fp8_wfirst(void);
194/* SPEED-1B (2026-10-17): the automatic deep kernel (x staged in shared memory, every load of the row in flight) for single
195 * projections whenever it fits the register file and the launch descriptor's shared-memory limit; ON by default (qkv 5120 x 2048
196 * cold 0.732 -> 0.853 of line). AIEN_OMEGA_FP8_DEEP=0 or set_deep(0) keeps the step-by-step kernel everywhere. */
197void omega_gpu_fp8_set_deep(int on);
198int omega_gpu_fp8_deep(void);
199/* Big-k rule (qwen38/decode-speed): single projections with k > 4096 get 16 warps per CTA and, when x fits the launch
200 * descriptor's shared memory, the deep kernel at the largest step count that divides the row within the lane budget
201 * (k 5120: 5 steps, k 6144: 6). 1 (default since 2026-10-19: Qwen3.8-27B whole model 151.6 -> 142-143 ms per token,
202 * golden 24/24) or 0; env AIEN_OMEGA_FP8_BIGK when never set. Shapes with k <= 4096 (the 30B lane) are unchanged. */
203void omega_gpu_fp8_set_bigk(int on);
204int omega_gpu_fp8_bigk(void);
205/* Host-only: the spec the automatic choice makes for a call of k columns, n rows, `groups` experts under the current knobs
206 * (shape, depth, sched, coal, wfirst, mutant). No chip, no kernel build. */
207void omega_gpu_fp8_spec_for(uint32_t k, uint32_t n, uint32_t groups, OmegaFp8GemvSpec *spec);
208/* Ragged k (Qwen3.8-Flash-Next expert down_proj, k = 640): a k that is a multiple of 128 but not of 32 * lane_bytes has no plain kernel.
209 * omega_gpu_fp8_ragged_for(spec, device_form) says whether a call of that form runs the ragged-k kernel
210 * (omega_blackwell_codegen_fp8_gemv_ragged_prog: floor(k / (32 * lane_bytes)) full steps plus one lane-masked tail step;
211 * step-by-step kernel, variant 1). The device-table and record forms (grouped_dev, grouped_record, prepare: device_form != 0) do, where
212 * the alternative was a refusal. The host-staged forms keep the general fallback by default (their results do not change). */
213int omega_gpu_fp8_ragged_for(const OmegaFp8GemvSpec *spec, int device_form);
214/* mode -1 automatic (the rule above), 0 never (a ragged k is refused in the device and record forms), 1 every form takes the ragged kernel
215 * (a host-staged k = 640 call then runs the kernel, not the general fallback). Each call clears the kernel cache. */
216void omega_gpu_fp8_set_ragged(int mode);
217#define OMEGA_GPU_FP8_V2_WARPS 16u
218#define OMEGA_GPU_FP8_V2_GROUPED_CTAS 12u
219int omega_gpu_fp8_weight_cache(void);
220/* Diagnosis: declare `pad` registers beyond the kernel's peak in the QMD (0 = exact peak). */
221void omega_gpu_fp8_set_gpr_pad(uint32_t pad);
222/* TEST ONLY: deliberately wrong kernels (1 scale dropped, 2 scale block never advances, 3 group table entry
223 * of cy ^ 1). The parity test must catch each. Production never calls this. */
224void omega_gpu_fp8_test_set_mutant(int mutant);
225/* The general fallback for `m` host rows of x (x32 or x16, m x k) through expert `expert`: y (m x n) as the GEMV defines it,
226 * computed by omega_gpu_gemm_ex_fp8_block_rows. `refused` is returned unchanged if that answers UNSUPPORTED. The fp8
227 * grouped GEMM (omega_gpu_fp8gemm_api.h) uses it for the shapes its kernel refuses. */
228int omega_gpu_fp8_general_rows(const OmegaGpuFp8Tensor *t, uint32_t expert, const float *x32, const uint16_t *x16, uint32_t m,
229 float *y, int refused);
230
231/* TEST ONLY: upload scales without the 2^56 fold (host-side mutant; the parity test must catch it). */
232void omega_gpu_fp8_test_set_no_fold(int on);
233/* The spec the API would pick for (k, n): for tests and the bandwidth table. */
234void omega_gpu_fp8_shape_for(uint32_t k, uint32_t n, uint32_t *lane_bytes, uint32_t *rows_per_warp, uint32_t *warps_per_cta);
235
236/* ---- host helpers (omega_gpu_fp8_pack.c), no device ---- */
237/* Reference e4m3 decode (port of sc fp8_e4m3fn_to_f32: bias 7, subnormal m * 2^-9, NaN for 0x7f / 0xff). */
238float omega_fp8_e4m3_ref(uint8_t b);
239/* The kernel's conversion: f32 bits equal to e4m3(b) * 2^-120 (wrong for the NaN codes, which are refused). */
240uint32_t omega_fp8_e4m3_kernel_bits(uint8_t b);
241int omega_fp8_e4m3_is_nan(uint8_t b);
242/* NaN codes in w; first = index of the first one. */
243size_t omega_fp8_pack_scan_nan(const uint8_t *w, size_t count, size_t *first);
244/* bf16 scales -> f32 * 2^56 (mutant_no_fold: * 1). -1 on a NaN/inf scale or fold overflow. */
245int omega_fp8_pack_scales(const uint16_t *bf16, size_t count, float *out, int mutant_no_fold);
246void omega_fp8_pack_x_f32(const float *x, uint32_t k, float *out);
247void omega_fp8_pack_x_bf16(const uint16_t *x, uint32_t k, float *out);
248/* Host oracle in double: y[r] = sum_c ref(w[r][c]) * x[c] * bf16(scale[r/128][c/128]); yscale[r] = sum |terms|
249 * (the accumulation scale a relative tolerance is measured against). w: n x k, scales (n/128) x (k/128). */
250void omega_fp8_gemv_oracle(const uint8_t *w, const uint16_t *scale_bf16, uint32_t n, uint32_t k,
251 const float *x, double *y, double *yscale);
252
253#ifdef __cplusplus
254}
255#endif
256#endif