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