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_fp8_api.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* omega_gpu_fp8_api.c: FP8 e4m3 GEMV entry points on the shared session (OWN-30B Cut 6 / Cut 5). |
| 3 | * See the header. Submission path and buffer policy are the matmul API's: every buffer both sides |
| 4 | * touch is GPU-uncached (nvos.h NVOS32_ATTR2_GPU_CACHEABLE: "For system memory this will not be |
| 5 | * coherent with direct CPU mappings"), one launch = omega_gpu_session_launch (shader-cache |
| 6 | * invalidate, constant bank, QMD0/QMD1, marker, L2_FLUSH_DIRTY, second marker on uncached memory). */ |
| 7 | #include "omega_gpu_fp8_api.h" |
| 8 | #include "omega_gpu_mem_cached.h" |
| 9 | #include "omega_blackwell_codegen_moe.h" |
| 10 | #include "omega_blackwell_codegen_moe2.h" /* variant 2 (W-L2c) */ |
| 11 | #include "omega_gpu_session.h" |
| 12 | #include "omega_gpu_fmt.h" |
| 13 | #include "omega_gpu_cmdlist.h" |
| 14 | #include "omega_blackwell_qmd.h" /* OMEGA_BW_QMD_MAX_SHARED_BYTES: the deep kernel's x stage must fit the launch descriptor */ |
| 15 | #include "omega_gpu_gemm_ex.h" /* the general fallback for shapes the tuned kernels refuse */ |
| 16 | #include "omega_gpu_fp8_internal.h" |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | |
| 20 | #include "omega_gpu_native_runtime.h" |
| 21 | #include "omega_gpu_apiviews.h" |
| 22 | #include "omega_gpu_api_hostrest.h" |
| 23 | #include "omega_gpu_code_alloc.h" |
| 24 | #define CACHE_SLOTS 16 |
| 25 | #define MARKER_TIMEOUT_MS 20000ull |
| 26 | #define POISON_F32 (-999.0f) |
| 27 | |
| 28 | struct OmegaGpuFp8Tensor { |
| 29 | uint32_t experts, n, k; |
| 30 | uint64_t allocation_epoch, owner_generation; |
| 31 | OmegaGpuMem w; /* experts * n * k e4m3 bytes, row-major */ |
| 32 | OmegaGpuMem s; /* experts * (n/128) * (k/128) f32 (scale * 2^56) */ |
| 33 | double s_mul; /* stored scale * s_mul = the scale (2^-56; 1 under the no-fold test mutant) */ |
| 34 | }; |
| 35 | |
| 36 | /* Tensor identities outlive the device session's resettable cache state. */ |
| 37 | static uint64_t g_tensor_owner_generation; |
| 38 | |
| 39 | typedef struct { |
| 40 | bool valid, pinned; |
| 41 | bool exact; |
| 42 | uint8_t manifest[32]; |
| 43 | uint32_t variant; /* 1 or 2: which generator built the kernel (part of the cache key) */ |
| 44 | uint32_t ragged; /* 1: built by the ragged-k entry point (k not a multiple of 32 * lane_bytes; part of the cache key) */ |
| 45 | OmegaFp8GemvSpec spec; |
| 46 | OmegaBlackwellKernel kernel; |
| 47 | OmegaGpuMem code; |
| 48 | } Slot; |
| 49 | |
| 50 | static struct { |
| 51 | OmegaGpuScratch x_buf, y_buf; |
| 52 | OmegaGpuMem tab; /* group table, OMEGA_GPU_FP8_MAX_GROUPS * 16 bytes */ |
| 53 | uint32_t cta_budget; int cta_budget_set; |
| 54 | uint32_t lane_bytes, rows, warps; /* overrides, 0 = automatic */ |
| 55 | int depth_set; uint32_t depth; /* depth override (depth_set: 0 = automatic) */ |
| 56 | uint32_t gpr_pad; /* registers declared beyond the kernel's peak (diagnosis knob) */ |
| 57 | int mutant, no_fold, sealed; |
| 58 | int variant, variant_set; /* GEMV variant 1 or 2 (omega_gpu_fp8_set_variant / AIEN_OMEGA_FP8_GEMV), read once */ |
| 59 | int weights_cached; /* omega_gpu_fp8_set_weight_cache: uploads after it allocate GPU-cached (read-only weights) */ |
| 60 | int weights_cached_set; /* 0: never set, the first upload reads env AIEN_OMEGA_FP8_WEIGHT_CACHE (W-L2d: "1" = cached) */ |
| 61 | int sched, sched_set; /* CUT5: kernel schedule 0 (default) or 1 (pipelined decode + stall pass), variant 1 only; env AIEN_OMEGA_FP8_SCHED when never set */ |
| 62 | int coal, coal_set, wfirst, wfirst_set; /* CUT5 variants A (coalesced output) and B (weights before x), variant 1 only; env AIEN_OMEGA_FP8_COAL / AIEN_OMEGA_FP8_WFIRST when never set */ |
| 63 | int deep, deep_set; /* SPEED-1B: automatic deep kernel for single projections whenever it fits (1, default) or never (0); env AIEN_OMEGA_FP8_DEEP when never set */ |
| 64 | int bigk, bigk_set; /* qwen38/decode-speed: single projections with k > 4096 (Qwen3.8-27B) get 16 warps and a partial-depth deep kernel when x fits; default on, env AIEN_OMEGA_FP8_BIGK when never set */ |
| 65 | int ragged_mode; /* omega_gpu_fp8_set_ragged: 0 automatic (device and record forms), 1 never, 2 every form */ |
| 66 | bool hooked; |
| 67 | Slot cache[CACHE_SLOTS]; |
| 68 | unsigned next; |
| 69 | OmegaGpuNativeCache native; |
| 70 | } g = { .cta_budget = OMEGA_GPU_FP8_MAX_CTAS }; |
| 71 | static int g_rec; /* 1 while a record call runs (lock held): kernel_for must not evict */ |
| 72 | |
| 73 | const char *omega_gpu_fp8_last_error(void) { return omega_gpu_session_last_error(); } |
| 74 | const char *omega_gpu_fp8_rc_name(int rc) { |
| 75 | switch (rc) { |
| 76 | case OMEGA_GPU_FP8_OK: return "OK"; |
| 77 | case OMEGA_GPU_FP8_BAD_ARGS: return "BAD_ARGS"; |
| 78 | case OMEGA_GPU_FP8_TOO_LARGE: return "TOO_LARGE"; |
| 79 | case OMEGA_GPU_FP8_CODEGEN_FAIL: return "CODEGEN_FAIL"; |
| 80 | case OMEGA_GPU_FP8_CHIP_FAIL: return "CHIP_FAIL"; |
| 81 | case OMEGA_GPU_FP8_NAN_WEIGHT: return "NAN_WEIGHT"; |
| 82 | default: return "UNKNOWN"; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | static void cache_clear_locked(void) { |
| 87 | for (unsigned i = 0; i < CACHE_SLOTS; i++) { |
| 88 | if (!g.cache[i].valid) continue; |
| 89 | if (g.cache[i].pinned) { |
| 90 | OmegaGpuNativeLease lease = omega_native_cache_lease(&g.native, i); |
| 91 | if (omega_native_lease_op(&lease, 5, 0) == 0) g.cache[i].pinned = false; |
| 92 | } |
| 93 | if (omega_native_cache_victim(&g.native, i, 0) != 1) continue; |
| 94 | omega_gpu_session_free(&g.cache[i].code); |
| 95 | (void)omega_native_cache_victim(&g.native, i, 1); |
| 96 | memset(&g.cache[i], 0, sizeof g.cache[i]); |
| 97 | } |
| 98 | g.next = 0; |
| 99 | } |
| 100 | static void on_session_close(void) { |
| 101 | cache_clear_locked(); |
| 102 | omega_gpu_session_scratch_free(&g.x_buf); |
| 103 | omega_gpu_session_scratch_free(&g.y_buf); |
| 104 | omega_gpu_session_free(&g.tab); |
| 105 | g.sealed = 0; |
| 106 | } |
| 107 | static OmegaGpuSession *dev_open_locked(void) { |
| 108 | if (!g.hooked) { (void)omega_gpu_session_on_close(on_session_close); g.hooked = true; } |
| 109 | return omega_gpu_session_open(); |
| 110 | } |
| 111 | void omega_gpu_fp8_cache_clear(void) { omega_gpu_session_lock(); cache_clear_locked(); omega_gpu_session_unlock(); } |
| 112 | void omega_gpu_fp8_set_shape(uint32_t lb, uint32_t rows, uint32_t warps) { |
| 113 | omega_gpu_session_lock(); g.lane_bytes = lb; g.rows = rows; g.warps = warps; cache_clear_locked(); omega_gpu_session_unlock(); |
| 114 | } |
| 115 | void omega_gpu_fp8_set_depth(int set, uint32_t depth) { omega_gpu_session_lock(); g.depth_set = set; g.depth = depth; cache_clear_locked(); omega_gpu_session_unlock(); } |
| 116 | /* mode -1: automatic (ragged kernels for the device-table and record forms only), 0: never, 1: every form. Clears the kernel cache. */ |
| 117 | void omega_gpu_fp8_set_ragged(int mode) { omega_gpu_session_lock(); g.ragged_mode = mode < 0 ? 0 : mode == 0 ? 1 : 2; cache_clear_locked(); omega_gpu_session_unlock(); } |
| 118 | void omega_gpu_fp8_set_gpr_pad(uint32_t pad) { omega_gpu_session_lock(); g.gpr_pad = pad; omega_gpu_session_unlock(); } |
| 119 | void omega_gpu_fp8_set_cta_budget(uint32_t ctas) { omega_gpu_session_lock(); g.cta_budget_set = ctas != 0; g.cta_budget = ctas ? ctas : OMEGA_GPU_FP8_MAX_CTAS; cache_clear_locked(); omega_gpu_session_unlock(); } |
| 120 | void omega_gpu_fp8_test_set_mutant(int m) { omega_gpu_session_lock(); g.mutant = m; cache_clear_locked(); omega_gpu_session_unlock(); } |
| 121 | void omega_gpu_fp8_test_set_no_fold(int on) { omega_gpu_session_lock(); g.no_fold = on ? 1 : 0; omega_gpu_session_unlock(); } |
| 122 | void omega_gpu_fp8_seal(int on) { omega_gpu_session_lock(); g.sealed = on ? 1 : 0; omega_gpu_session_unlock(); } |
| 123 | |
| 124 | /* ---- GEMV variant (W-L2c): 1 = omega_blackwell_codegen_moe.c (default), 2 = omega_blackwell_codegen_moe2.c (row-pipelined) ---- */ |
| 125 | static int variant_now(void) { |
| 126 | if (!g.variant_set) { |
| 127 | const char *v = getenv("AIEN_OMEGA_FP8_GEMV"); |
| 128 | g.variant = (int)OMEGA_AH(1, v ? (unsigned char)v[0] : 0, v && v[0] ? (unsigned char)v[1] : 1); |
| 129 | g.variant_set = 1; |
| 130 | } |
| 131 | return g.variant; |
| 132 | } |
| 133 | void omega_gpu_fp8_set_variant(int variant) { |
| 134 | omega_gpu_session_lock(); |
| 135 | g.variant = variant == 2 ? 2 : 1; g.variant_set = 1; |
| 136 | cache_clear_locked(); |
| 137 | omega_gpu_session_unlock(); |
| 138 | } |
| 139 | int omega_gpu_fp8_variant(void) { return variant_now(); } |
| 140 | |
| 141 | /* ---- kernel schedule (CUT5): 0 = emitted stalls (default), 1 = pipelined decode + dependency stall pass (variant 1 only) ---- */ |
| 142 | static int sched_now(void) { |
| 143 | if (!g.sched_set) { |
| 144 | const char *v = getenv("AIEN_OMEGA_FP8_SCHED"); |
| 145 | g.sched = (int)omega_ah_flag(v, 0); |
| 146 | g.sched_set = 1; |
| 147 | } |
| 148 | return g.sched; |
| 149 | } |
| 150 | void omega_gpu_fp8_set_sched(int sched) { |
| 151 | omega_gpu_session_lock(); |
| 152 | g.sched = sched == 1 ? 1 : 0; g.sched_set = 1; |
| 153 | cache_clear_locked(); |
| 154 | omega_gpu_session_unlock(); |
| 155 | } |
| 156 | int omega_gpu_fp8_sched(void) { return sched_now(); } |
| 157 | /* ---- CUT5 variants A and B (variant 1 only): 0 = off, 1 = on. Never set: the env decides once; A (coal) defaults ON (env |
| 158 | * value "0" turns it off; SPEED-A 2026-10-17: OWN-30B decode graph 24.49 -> 21.34 ms/token, tokens identical), B (wfirst) |
| 159 | * defaults OFF (env value "1" turns it on; cold bench: not distinguishable from noise) ---- */ |
| 160 | static int flag_now(int *v, int *set, const char *env, int dflt) { |
| 161 | if (!*set) { |
| 162 | const char *s = getenv(env); |
| 163 | *v = (int)omega_ah_flag(s, (unsigned)dflt); |
| 164 | *set = 1; |
| 165 | } |
| 166 | return *v; |
| 167 | } |
| 168 | static void flag_set(int *v, int *set, int on) { omega_gpu_session_lock(); *v = on == 1 ? 1 : 0; *set = 1; cache_clear_locked(); omega_gpu_session_unlock(); } |
| 169 | void omega_gpu_fp8_set_coal(int on) { flag_set(&g.coal, &g.coal_set, on); } |
| 170 | int omega_gpu_fp8_coal(void) { return flag_now(&g.coal, &g.coal_set, "AIEN_OMEGA_FP8_COAL", 1); } |
| 171 | void omega_gpu_fp8_set_wfirst(int on) { flag_set(&g.wfirst, &g.wfirst_set, on); } |
| 172 | int omega_gpu_fp8_wfirst(void) { return flag_now(&g.wfirst, &g.wfirst_set, "AIEN_OMEGA_FP8_WFIRST", 0); } |
| 173 | void omega_gpu_fp8_set_deep(int on) { flag_set(&g.deep, &g.deep_set, on); } |
| 174 | int omega_gpu_fp8_deep(void) { return flag_now(&g.deep, &g.deep_set, "AIEN_OMEGA_FP8_DEEP", 1); } |
| 175 | void omega_gpu_fp8_set_bigk(int on) { flag_set(&g.bigk, &g.bigk_set, on); } |
| 176 | int omega_gpu_fp8_bigk(void) { return flag_now(&g.bigk, &g.bigk_set, "AIEN_OMEGA_FP8_BIGK", 1); } |
| 177 | /* codegen by variant */ |
| 178 | static int prog_for(int variant, int ragged, const OmegaFp8GemvSpec *spec, BlackwellIRProgram *prog) { |
| 179 | return variant == 2 ? omega_blackwell_codegen_fp8_gemv2_prog(spec, prog) |
| 180 | : ragged ? omega_blackwell_codegen_fp8_gemv_ragged_prog(spec, prog) : omega_blackwell_codegen_fp8_gemv_prog(spec, prog); |
| 181 | } |
| 182 | static int stats_for(int variant, int ragged, const OmegaFp8GemvSpec *spec, uint32_t *insns, uint32_t *gprs) { |
| 183 | return variant == 2 ? omega_fp8_gemv2_prog_stats(spec, insns, gprs) |
| 184 | : ragged ? omega_fp8_gemv_ragged_prog_stats(spec, insns, gprs) : omega_fp8_gemv_prog_stats(spec, insns, gprs); |
| 185 | } |
| 186 | static uint32_t shared_for(int variant, int ragged, const OmegaFp8GemvSpec *spec) { |
| 187 | return variant == 2 ? omega_fp8_gemv2_shared_bytes(spec) |
| 188 | : ragged ? omega_fp8_gemv_ragged_shared_bytes(spec) : omega_fp8_gemv_shared_bytes(spec); |
| 189 | } |
| 190 | |
| 191 | /* ---- shape choice ---- */ |
| 192 | |
| 193 | /* Largest lane width dividing k (16 for k % 512 == 0: one LDG.E.128 per row per step); one row per warp: |
| 194 | * the chip sweep (evidence/OWN30B-FP8-GEMV-7ce5dd4b-*, rows 1/2/4/8 one variable at a time) measured rows = 1 |
| 195 | * fastest at every shape (more warps and CTAs in flight on the same bytes beats fewer, wider warps). */ |
| 196 | void omega_gpu_fp8_shape_for(uint32_t k, uint32_t n, uint32_t *lane_bytes, uint32_t *rows, uint32_t *warps) { |
| 197 | (void)n; |
| 198 | if (lane_bytes) *lane_bytes = (uint32_t)OMEGA_AH(2, k, g.lane_bytes, 0); |
| 199 | if (rows) *rows = (uint32_t)OMEGA_AH(2, k, g.rows, 1); |
| 200 | if (warps) *warps = (uint32_t)OMEGA_AH(2, k, g.warps, 2); |
| 201 | } |
| 202 | |
| 203 | /* ---- kernel cache (lock held, device open) ---- */ |
| 204 | |
| 205 | /* Registers declared in the QMD: the peak plus one, rounded up to a multiple of 8. Measured on the chip |
| 206 | * (evidence OWN30B-FP8-GEMV-c4006391, kernel log Xid 13 "Out Of Range Register"): a kernel whose peak is |
| 207 | * exactly 64 (top register R63) and declares 64 faults; declaring 65 runs. Every earlier kernel had a peak |
| 208 | * that was not a multiple of 8, so the hardware's rounding hid the rule. */ |
| 209 | static uint32_t gpr_declared(uint32_t peak) { return omega_bw_gpr_declared(peak); } /* peak + 2 rounded up to 8: the chip hung at peak + 1 (omega_blackwell_codegen.h) */ |
| 210 | |
| 211 | /* Register file: a CTA of 32 * warps threads must fit the SM's 65536 registers at the declared count. Measured |
| 212 | * (chip, 2026-10-13, kernel log Xid 13): the deep k=2048 kernel (72 declared) at 32 warps faults, 73728 > 65536. */ |
| 213 | static bool register_file_fits(uint32_t warps, uint32_t peak) { |
| 214 | uint32_t decl = gpr_declared(peak); |
| 215 | if (OMEGA_AH(3, (uint64_t)32u * warps, decl)) return true; |
| 216 | char e[160]; omega_gpu_fmt(e, sizeof e, "fp8 kernel: %u warps x %u declared registers exceed the 65536-register file", warps, decl); |
| 217 | omega_gpu_session_set_error(e); |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | int omega_gpu_fp8_kernel_fits(const OmegaFp8GemvSpec *spec, uint32_t *declared) { |
| 222 | uint32_t peak = 0; |
| 223 | if (!spec || stats_for(variant_now(), 1, spec, NULL, &peak) != 0) { omega_gpu_session_set_error("fp8 codegen refused the shape"); return OMEGA_GPU_FP8_CODEGEN_FAIL; } |
| 224 | if (declared) *declared = gpr_declared(peak); |
| 225 | return register_file_fits(spec->warps, peak) ? OMEGA_GPU_FP8_OK : OMEGA_GPU_FP8_TOO_LARGE; |
| 226 | } |
| 227 | |
| 228 | static int kernel_for(const OmegaFp8GemvSpec *spec, int ragged, Slot **out, bool *hit) { |
| 229 | const uint32_t variant = (uint32_t)variant_now(); |
| 230 | int fresh = omega_native_cache_refresh(&g.native, CACHE_SLOTS); |
| 231 | if (fresh < 0) return OMEGA_GPU_FP8_CODEGEN_FAIL; |
| 232 | if (fresh) memset(g.cache, 0, sizeof g.cache); |
| 233 | for (unsigned i = 0; i < CACHE_SLOTS; i++) { |
| 234 | Slot *s = &g.cache[i]; |
| 235 | if (s->valid && !s->exact && s->variant == variant && s->ragged == (uint32_t)ragged && memcmp(&s->spec, spec, sizeof *spec) == 0) { *out = s; *hit = true; return OMEGA_GPU_FP8_OK; } |
| 236 | } |
| 237 | if (g.sealed) { |
| 238 | char e[160]; |
| 239 | omega_gpu_fmt(e, sizeof e, "serving reservation exceeded: fp8 kernel k=%u lb=%u rows=%u warps=%u was not prepared", |
| 240 | spec->k, spec->lane_bytes, spec->rows, spec->warps); |
| 241 | omega_gpu_session_set_error(e); |
| 242 | return OMEGA_GPU_FP8_TOO_LARGE; |
| 243 | } |
| 244 | BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); |
| 245 | if (!prog) { omega_gpu_session_set_error("fp8 codegen: host memory"); return OMEGA_GPU_FP8_CODEGEN_FAIL; } |
| 246 | int cg = prog_for((int)variant, ragged, spec, prog); |
| 247 | size_t need = cg == 0 ? omega_bw_program_code_bytes(prog) : 0; |
| 248 | if (cg != 0 || need == 0) { omega_gpu_host_free(prog); omega_gpu_session_set_error("fp8 codegen refused the shape"); return OMEGA_GPU_FP8_CODEGEN_FAIL; } |
| 249 | if (!register_file_fits(spec->warps, prog->regalloc.peak_gpr_usage)) { omega_gpu_host_free(prog); return OMEGA_GPU_FP8_TOO_LARGE; } |
| 250 | struct { OmegaFp8GemvSpec spec; uint32_t variant, ragged; } identity; |
| 251 | memset(&identity, 0, sizeof identity); identity.spec = *spec; identity.variant = variant; identity.ragged = (uint32_t)ragged; |
| 252 | uint8_t key[32]; uint64_t request[16]; |
| 253 | int index = omega_native_shape_key(&identity, sizeof identity, key) == 0 ? |
| 254 | omega_native_cache_reserve(&g.native, key, omega_gpu_code_alloc_bytes(need), request) : -1; |
| 255 | if (index < 0) { omega_gpu_host_free(prog); return OMEGA_GPU_FP8_TOO_LARGE; } |
| 256 | Slot *s = &g.cache[index]; |
| 257 | if (g_rec && s->valid) { |
| 258 | (void)omega_native_cache_finish(&g.native, (unsigned)index, request, 0); |
| 259 | omega_gpu_host_free(prog); return OMEGA_GPU_FP8_TOO_LARGE; |
| 260 | } |
| 261 | OmegaGpuMem code = {0}; OmegaBlackwellKernel kernel; |
| 262 | if (omega_gpu_session_alloc_code(need, &code) != 0) cg = -1; |
| 263 | else cg = omega_blackwell_kernel_encode_into(prog, code.cpu, need, &kernel); |
| 264 | omega_gpu_host_free(prog); |
| 265 | if (cg != 0 || !kernel.code || kernel.code_size != need || |
| 266 | omega_gpu_session_sync_to_gpu(&code) != 0 || |
| 267 | omega_native_cache_finish(&g.native, (unsigned)index, request, 1) != 0) { |
| 268 | omega_gpu_session_free(&code); |
| 269 | (void)omega_native_cache_finish(&g.native, (unsigned)index, request, 0); |
| 270 | return OMEGA_GPU_FP8_CHIP_FAIL; |
| 271 | } |
| 272 | if (s->valid) omega_gpu_session_free(&s->code); |
| 273 | memset(s, 0, sizeof *s); |
| 274 | s->valid = true; s->variant = variant; s->ragged = (uint32_t)ragged; s->spec = *spec; s->kernel = kernel; s->code = code; |
| 275 | *out = s; *hit = false; |
| 276 | return OMEGA_GPU_FP8_OK; |
| 277 | } |
| 278 | |
| 279 | /* Deep kernel (x' staged in shared memory, every load of the row issued at once) whenever the whole row fits |
| 280 | * the budgets (rows * KS * lane_bytes <= 128, i.e. rows * k <= 4096) and the staging rounds are exact; else the step-by-step kernel. */ |
| 281 | uint32_t omega_gpu_fp8_depth_for(const OmegaFp8GemvSpec *spec, uint32_t n, uint32_t groups) { |
| 282 | if (g.depth_set) return g.depth; |
| 283 | const uint64_t choice = OMEGA_AH(8, spec->k, spec->lane_bytes, spec->rows, spec->warps, groups, OMEGA_AH(24, spec->k, spec->lane_bytes, spec->rows, groups) ? omega_gpu_fp8_bigk() : 0); |
| 284 | const uint32_t ks = (uint32_t)choice; |
| 285 | const int partial = (int)(choice >> 32); |
| 286 | if (!ks) return 0; |
| 287 | /* Measured (evidence OWN30B-FP8-GEMV-fc05176a-*, main vs depth0 in one run, before variant A): the deep kernel won on |
| 288 | * single projections up to a few MB (gate+up 1536x2048: 30 us vs 51; k_proj: 30 vs 44) and lost on the grouped |
| 289 | * 8-expert launches (its per-CTA x staging re-reads 8 KB per CTA: 188 us vs 150) and on o_proj (79 vs 64), so the rule |
| 290 | * was "classic above 4 MB". Re-measured under A (SPEED-1B, 2026-10-17, cold GPU-stamped, 5/5 interleaved, |
| 291 | * ~/workspace/investigations/2026-10-17-SPEED-1B): deep wins on qkv 5120 x 2048 (10.5 MB) 51.8 us vs 60.4 (0.853 vs 0.732 |
| 292 | * of the 237.2 GB/s line, floor spread 0.078), so the size clause is gone. o_proj 2048 x 4096 is not a candidate: its deep |
| 293 | * kernel's x stage exceeds the launch descriptor's shared-memory limit (qmd build (shared_bytes), 5/5), which is the bound |
| 294 | * checked below. AIEN_OMEGA_FP8_DEEP=0 or set_deep(0) restores the classic kernel everywhere. */ |
| 295 | (void)n; |
| 296 | if (!OMEGA_AH(20, groups, groups == 1u ? omega_gpu_fp8_deep() : 0, 0, 0)) return 0; |
| 297 | OmegaFp8GemvSpec deep = *spec; deep.depth = ks; deep.mutant = 0; |
| 298 | if (!OMEGA_AH(20, groups, omega_gpu_fp8_deep(), omega_fp8_gemv_shared_bytes(&deep), partial ? OMEGA_BW_QMD_MAX_SHARED_BYTES : OMEGA_GPU_FP8_DEEP_MAX_SHARED_BYTES)) return 0; |
| 299 | /* The register check builds the whole kernel (codegen + register allocation). spec_for runs on every launch and every |
| 300 | * record, so the answer is remembered per spec: SPEED-1B graph A/B (2026-10-17) measured the unremembered check at |
| 301 | * 853..1172 us of host record time per token (48 qkv records) against 45..113 us without the deep default. The answer is |
| 302 | * a pure function of the spec (deterministic codegen), so the memo never needs clearing; spec_for zeroes the struct, |
| 303 | * so memcmp over the padding is exact. */ |
| 304 | static struct { OmegaFp8GemvSpec spec; uint32_t fits; } memo[8]; |
| 305 | static unsigned memo_n, memo_next; |
| 306 | for (unsigned i = 0; i < memo_n; i++) |
| 307 | if (memcmp(&memo[i].spec, &deep, sizeof deep) == 0) return memo[i].fits ? ks : 0; |
| 308 | uint32_t peak = 0; |
| 309 | const uint32_t fits = omega_fp8_gemv_prog_stats(&deep, NULL, &peak) == 0 && OMEGA_AH(3, (uint64_t)32u * spec->warps, gpr_declared(peak)); |
| 310 | const unsigned at = memo_n < 8u ? memo_n++ : (memo_next++ % 8u); |
| 311 | memo[at].spec = deep; memo[at].fits = fits; |
| 312 | return fits ? ks : 0; |
| 313 | } |
| 314 | |
| 315 | static void spec_for(uint32_t k, uint32_t n, uint32_t groups, OmegaFp8GemvSpec *spec) { |
| 316 | memset(spec, 0, sizeof *spec); |
| 317 | spec->k = k; |
| 318 | omega_gpu_fp8_shape_for(k, n, &spec->lane_bytes, &spec->rows, &spec->warps); |
| 319 | /* Grouped launches (8 experts): 32 warps per CTA and few CTAs per group (OMEGA_GPU_FP8_GROUPED_CTAS, one per SM |
| 320 | * for 8 groups) measured fastest: down 2048x768 x 8 went from 181 us (256 x 8 CTAs of 8 warps) to 90 us |
| 321 | * (4..6 x 8 CTAs of 32 warps); gate+up 146 -> 140 us (probe logs listed in ~/handoffs/own30b-K-report.md). */ |
| 322 | spec->warps = (uint32_t)OMEGA_AH(6, k, groups, g.warps, !g.warps && OMEGA_AH(19, 0, groups, k, 1) ? omega_gpu_fp8_bigk() : 0, variant_now()); |
| 323 | if (variant_now() == 2) { |
| 324 | /* variant 2 (W-L2c): 16 warps per CTA (two CTAs per SM at its register count) for every launch unless overridden; |
| 325 | * the whole row group is always in flight (depth = KS, informational: the generator ignores the field). */ |
| 326 | /* ah_warps already applies the variant-2 default. */ |
| 327 | spec->depth = spec->k / (32u * spec->lane_bytes); |
| 328 | } else { |
| 329 | spec->sched = (uint32_t)sched_now(); |
| 330 | spec->coal = (uint32_t)OMEGA_AH(7, omega_gpu_fp8_coal(), spec->warps, spec->rows); |
| 331 | spec->wfirst = (uint32_t)omega_gpu_fp8_wfirst(); /* B: deep kernels ignore it */ |
| 332 | spec->depth = omega_gpu_fp8_depth_for(spec, n, groups); /* after coal: the deep kernel's shared bytes include A's slots */ |
| 333 | } |
| 334 | spec->mutant = g.mutant; |
| 335 | } |
| 336 | /* Host-only: the spec the automatic choice makes for a call (no chip, no cache; tests check the defaults with it). */ |
| 337 | void omega_gpu_fp8_spec_for(uint32_t k, uint32_t n, uint32_t groups, OmegaFp8GemvSpec *spec) { spec_for(k, n, groups, spec); } |
| 338 | /* Whether a call of that form runs the ragged-k kernel for spec (a k that is a multiple of 128 but not of 32 * lane_bytes). The device-table |
| 339 | * and record forms (device_form != 0) do, where the alternative was a refusal; a host-staged call keeps its general fallback, so its results do |
| 340 | * not change. omega_gpu_fp8_set_ragged overrides both. The rule itself is the seeded Omega host-policy unit (op 25, om/apiviews/hostrest.om). */ |
| 341 | int omega_gpu_fp8_ragged_for(const OmegaFp8GemvSpec *spec, int device_form) { |
| 342 | const int ok = g.ragged_mode == 2 || (g.ragged_mode == 0 && device_form); |
| 343 | return spec && OMEGA_AH(25, spec->k, spec->lane_bytes, ok, (uint64_t)variant_now(), spec->depth) ? 1 : 0; |
| 344 | } |
| 345 | |
| 346 | OmegaGpuNativeCache *omega_gpu_fp8_native_cache(void) { return &g.native; } |
| 347 | |
| 348 | /* Private immutable payload upload. Qualification/selection belongs to the graph |
| 349 | * resolver; this preparation alone does not authorize recording or activation. */ |
| 350 | int omega_gpu_fp8_prepare_native(const OmegaGpuNativeArtifact *a, OmegaGpuNativeLease *lease) { |
| 351 | uint8_t key[32]; uint64_t req[16], tok[16] = {0}; |
| 352 | if (!lease || omega_native_admitted(a, key) != 0) return -1; |
| 353 | if (!dev_open_locked()) return -1; |
| 354 | int fresh = omega_native_cache_refresh(&g.native, CACHE_SLOTS); |
| 355 | if (fresh < 0) return -1; |
| 356 | if (fresh) memset(g.cache, 0, sizeof g.cache); |
| 357 | omega_native_context(req); req[2] = omega_gpu_code_alloc_bytes((size_t)a->sizes[1]); |
| 358 | memcpy(req + 3, key, 32); |
| 359 | int64_t found = omega_native_cache_call(&g.native, 12, req, tok); |
| 360 | if (found < 0) return -1; |
| 361 | int index = found ? (int)(found - 1) : omega_native_cache_reserve(&g.native, key, (size_t)req[2], req); |
| 362 | if (index < 0) return -1; |
| 363 | Slot *s = &g.cache[index]; |
| 364 | if (found) { |
| 365 | if (!s->valid || !s->exact || memcmp(s->manifest, a->manifest, 32)) return -1; |
| 366 | } else { |
| 367 | OmegaGpuMem code = {0}; |
| 368 | if (omega_gpu_session_alloc_code((size_t)a->sizes[1], &code) != 0) goto abort; |
| 369 | memcpy(code.cpu, a->code, (size_t)a->sizes[1]); |
| 370 | if (omega_gpu_session_sync_to_gpu(&code) != 0 || |
| 371 | omega_native_cache_finish(&g.native, (unsigned)index, req, 1) != 0) { |
| 372 | omega_gpu_session_free(&code); goto abort; |
| 373 | } |
| 374 | if (s->valid) omega_gpu_session_free(&s->code); |
| 375 | memset(s, 0, sizeof *s); |
| 376 | s->valid = s->exact = true; s->code = code; |
| 377 | s->kernel.code = code.cpu; s->kernel.code_size = (size_t)a->sizes[1]; |
| 378 | memcpy(s->manifest, a->manifest, 32); |
| 379 | } |
| 380 | *lease = omega_native_cache_lease(&g.native, (unsigned)index); |
| 381 | return omega_native_lease_op(lease, 5, 1); |
| 382 | abort: |
| 383 | (void)omega_native_cache_finish(&g.native, (unsigned)index, req, 0); |
| 384 | return -1; |
| 385 | } |
| 386 | |
| 387 | int omega_gpu_fp8_record_native(OmegaGpuCmdList *cl, const OmegaGpuFp8Tensor *t, |
| 388 | const OmegaGpuNativeArtifact *a, const OmegaGpuNativeLease *lease, |
| 389 | uint32_t groups, uint64_t table, uint64_t xp, uint32_t xrows, uint64_t y) { |
| 390 | if (!cl || !t || !groups || !table || !xp || !y || |
| 391 | !lease || lease->cache != &g.native || omega_native_bound(a, lease) != 0) return -1; |
| 392 | uint64_t call[12] = {1, 1, t->n, t->k, groups, 10, 0, xrows, |
| 393 | t->allocation_epoch, omega_gpu_session_epoch(), t->owner_generation, 0}, launch[32]; |
| 394 | if (omega_native_launch(a, call, launch) != 0) return -1; |
| 395 | Slot *s = &g.cache[lease->slot]; |
| 396 | if (!s->exact || memcmp(s->manifest, a->manifest, 32)) return -1; |
| 397 | uint32_t args[10] = {(uint32_t)table, (uint32_t)(table >> 32), |
| 398 | (uint32_t)xp, (uint32_t)(xp >> 32), (uint32_t)y, (uint32_t)(y >> 32), |
| 399 | (uint32_t)launch[28], xrows == 1 ? 0 : t->k * 4u, t->n * 4u, (uint32_t)launch[29]}; |
| 400 | OmegaGpuLaunch L = {.code_va = s->code.va, .gpr_count = (uint32_t)launch[8], |
| 401 | .threads_x = (uint32_t)launch[4], .threads_y = (uint32_t)launch[5], |
| 402 | .grid_x = (uint32_t)launch[1], .grid_y = (uint32_t)launch[2], |
| 403 | .num_elements = (size_t)groups * t->n, .args = args, .n_args = 10, |
| 404 | .timeout_ms = MARKER_TIMEOUT_MS, .shared_bytes = (uint32_t)launch[7]}; |
| 405 | return omega_gpu_cmdlist_add_native(cl, &L, lease); |
| 406 | } |
| 407 | |
| 408 | int omega_gpu_fp8_prepare(uint32_t k) { |
| 409 | OmegaFp8GemvSpec spec; spec_for(k, 1u << 20, 1, &spec); |
| 410 | const int ragged = omega_gpu_fp8_ragged_for(&spec, 1); /* serving prepares the device and record forms */ |
| 411 | omega_gpu_session_lock(); |
| 412 | int rc = OMEGA_GPU_FP8_OK; |
| 413 | if (!dev_open_locked()) rc = OMEGA_GPU_FP8_CHIP_FAIL; |
| 414 | else { |
| 415 | Slot *s = NULL; bool hit = false; rc = kernel_for(&spec, ragged, &s, &hit); |
| 416 | if (rc == OMEGA_GPU_FP8_OK && !s->pinned) { |
| 417 | OmegaGpuNativeLease lease = omega_native_cache_lease(&g.native, (unsigned)(s - g.cache)); |
| 418 | if (omega_native_lease_op(&lease, 5, 1) != 0) rc = OMEGA_GPU_FP8_TOO_LARGE; |
| 419 | else s->pinned = true; |
| 420 | } |
| 421 | } |
| 422 | omega_gpu_session_unlock(); |
| 423 | return rc; |
| 424 | } |
| 425 | |
| 426 | /* ---- tensors ---- */ |
| 427 | |
| 428 | void omega_gpu_fp8_tensor_shape(const OmegaGpuFp8Tensor *t, uint32_t *experts, uint32_t *n, uint32_t *k) { |
| 429 | if (experts) *experts = t ? t->experts : 0; |
| 430 | if (n) *n = t ? t->n : 0; |
| 431 | if (k) *k = t ? t->k : 0; |
| 432 | } |
| 433 | |
| 434 | int omega_gpu_fp8_native_observe(const OmegaGpuFp8Tensor *t, OmegaGpuNativeTensorObservation *out) { |
| 435 | if (!t || !out) return -1; |
| 436 | *out = (OmegaGpuNativeTensorObservation){ |
| 437 | .experts = t->experts, .n = t->n, .k = t->k, |
| 438 | .allocation_epoch = t->allocation_epoch, .owner_generation = t->owner_generation, |
| 439 | .weights = t->w, .scales = t->s |
| 440 | }; |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | int omega_gpu_fp8_tensor_upload(uint32_t experts, uint32_t n, uint32_t k, const uint8_t *w, const uint16_t *scale, OmegaGpuFp8Tensor **out) { |
| 445 | if (out) *out = NULL; |
| 446 | if (!w || !scale || !out || experts == 0 || n == 0 || k == 0 || n % OMEGA_FP8_BLOCK || k % OMEGA_FP8_BLOCK) return OMEGA_GPU_FP8_BAD_ARGS; |
| 447 | if (k > OMEGA_FP8_MAX_K) return OMEGA_GPU_FP8_TOO_LARGE; |
| 448 | const size_t wbytes = (size_t)experts * n * k, scount = (size_t)experts * (n / OMEGA_FP8_BLOCK) * (k / OMEGA_FP8_BLOCK); |
| 449 | size_t first = 0; |
| 450 | if (omega_fp8_pack_scan_nan(w, wbytes, &first)) { |
| 451 | char e[120]; omega_gpu_fmt(e, sizeof e, "fp8 upload: e4m3 NaN code at byte %zu", first); |
| 452 | omega_gpu_session_set_error(e); |
| 453 | return OMEGA_GPU_FP8_NAN_WEIGHT; |
| 454 | } |
| 455 | OmegaGpuFp8Tensor *t = omega_gpu_host_calloc(1, sizeof *t); |
| 456 | if (!t) return OMEGA_GPU_FP8_BAD_ARGS; |
| 457 | t->experts = experts; t->n = n; t->k = k; t->s_mul = g.no_fold ? 1.0 : 1.0 / (double)(1ull << OMEGA_FP8_SCALE_EXP); |
| 458 | int rc = OMEGA_GPU_FP8_OK; |
| 459 | omega_gpu_session_lock(); |
| 460 | if (!g.weights_cached_set) { const char *v = getenv("AIEN_OMEGA_FP8_WEIGHT_CACHE"); g.weights_cached = (v && v[0] == '1' && v[1] == 0) ? 1 : 0; g.weights_cached_set = 1; } |
| 461 | int (*const alloc)(size_t, OmegaGpuMem *) = g.weights_cached ? omega_gpu_session_alloc_cached : omega_gpu_session_alloc; |
| 462 | uint64_t next_owner = 0; |
| 463 | if (omega_native_revision(&next_owner, g_tensor_owner_generation) != OMEGA_OK || !next_owner) |
| 464 | rc = OMEGA_GPU_FP8_CHIP_FAIL; |
| 465 | else if (!dev_open_locked() || alloc(wbytes, &t->w) != 0 || alloc(scount * 4u, &t->s) != 0) |
| 466 | rc = OMEGA_GPU_FP8_CHIP_FAIL; |
| 467 | if (rc == OMEGA_GPU_FP8_OK) { |
| 468 | g_tensor_owner_generation = next_owner; |
| 469 | t->owner_generation = next_owner; |
| 470 | t->allocation_epoch = omega_gpu_session_epoch(); |
| 471 | } |
| 472 | /* Fill and sync under the lock (omega#456): t is not shared yet, but its CPU views belong to the open device, and a |
| 473 | * device close on another thread between unlock and memcpy would unmap them; sync_to_gpu also reads the open state. |
| 474 | * g.no_fold is read under the lock, as its setter now writes it. The cost is holding the lock for one weight copy. */ |
| 475 | if (rc == OMEGA_GPU_FP8_OK) { |
| 476 | memcpy(t->w.cpu, w, wbytes); |
| 477 | if (omega_fp8_pack_scales(scale, scount, t->s.cpu, g.no_fold) != 0) { omega_gpu_session_set_error("fp8 upload: NaN, inf or overflowing scale"); rc = OMEGA_GPU_FP8_BAD_ARGS; } |
| 478 | else if (omega_gpu_session_sync_to_gpu(&t->w) != 0 || omega_gpu_session_sync_to_gpu(&t->s) != 0) rc = OMEGA_GPU_FP8_CHIP_FAIL; |
| 479 | } |
| 480 | omega_gpu_session_unlock(); |
| 481 | if (rc != OMEGA_GPU_FP8_OK) { omega_gpu_fp8_tensor_free(t); return rc; } |
| 482 | *out = t; |
| 483 | return OMEGA_GPU_FP8_OK; |
| 484 | } |
| 485 | |
| 486 | void omega_gpu_fp8_tensor_free(OmegaGpuFp8Tensor *t) { |
| 487 | if (!t) return; |
| 488 | omega_gpu_session_lock(); |
| 489 | if (t->w.cpu) omega_gpu_session_free(&t->w); |
| 490 | if (t->s.cpu) omega_gpu_session_free(&t->s); |
| 491 | omega_gpu_session_unlock(); |
| 492 | omega_gpu_host_free(t); |
| 493 | } |
| 494 | |
| 495 | /* ---- the GEMV (lock held inside) ---- */ |
| 496 | |
| 497 | /* L310 (2026-10-24): CTAs per group of a grouped launch. A grouped CTA is 32 warps = 1024 threads; CC 12.x holds 1536 |
| 498 | * resident threads per SM (CUDA Programming Guide, Compute Capabilities, Table 30), so one such CTA fits per SM and the |
| 499 | * GB10's 48 SMs (GB10 Superchip Whitepaper WP-12718-001_v02, Table 4) hold one wave of 48 CTAs. The compiled 6 was |
| 500 | * measured on 8 groups (48 CTAs = one wave). The 35B launches 9 groups: 54 CTAs run as two waves, the second 6 CTAs wide; |
| 501 | * 5 per group (45 = one wave) measured gate_up + down 6.962 -> 5.874 ms per 1-row step |
| 502 | * (~/workspace/investigations/2026-10-24-l310-grouped-ctas). Env, read once like the other switches: |
| 503 | * AIEN_OMEGA_FP8_GROUPED_CTAS=1..256 exact CTAs per group (experiments; wins over the wave rule); |
| 504 | * AIEN_OMEGA_FP8_GROUPED_WAVE=0 the compiled GROUPED_CTAS for every group count (the pre-A1 default). |
| 505 | * Neither set: the wave rule, min(GROUPED_CTAS, SM_COUNT / groups), at least 1: 8 groups keep 6, the 35B's 9 groups get |
| 506 | * 5 (A1, DecodeKernels 2026-10-27: gate_up + down median 164.698 -> 138.776 us per layer, bit-identical outputs, |
| 507 | * evidence/DECODE-KERNELS-20261027T0612Z). */ |
| 508 | uint32_t omega_gpu_fp8_grouped_ctas_rule(uint32_t groups, uint32_t forced, int wave) { |
| 509 | return (uint32_t)OMEGA_AH(4, groups, forced, wave != 0); |
| 510 | } |
| 511 | static uint32_t env_u32(const char *name) { /* 0 when unset, empty or not a plain number */ |
| 512 | const char *e = getenv(name); uint32_t n = 0; |
| 513 | if (!e || !*e) return 0; |
| 514 | for (; *e; e++) { |
| 515 | const uint64_t next = OMEGA_AH(5, n, (unsigned char)*e); |
| 516 | if (next > UINT32_MAX) return 0; |
| 517 | n = (uint32_t)next; |
| 518 | } |
| 519 | return n; |
| 520 | } |
| 521 | uint32_t omega_gpu_fp8_grouped_ctas_auto(uint32_t groups) { |
| 522 | static uint32_t forced; static int wave, set; |
| 523 | if (!set) { forced = env_u32("AIEN_OMEGA_FP8_GROUPED_CTAS"); wave = (int)omega_ah_flag(getenv("AIEN_OMEGA_FP8_GROUPED_WAVE"), 1); set = 1; } |
| 524 | return omega_gpu_fp8_grouped_ctas_rule(groups, forced, wave); |
| 525 | } |
| 526 | |
| 527 | /* General fallback (GemmGeneral, 2026-10-27): a host-staged call (x and the expert ids on the host, y returned) whose shape |
| 528 | * the tuned kernels refuse runs through omega_gpu_gemm_ex_fp8_block_rows instead (dequantize, fp32-input GEMM via bf16x3). |
| 529 | * `refused` is what the call returned before the fallback existed; it comes back unchanged if the fallback answers |
| 530 | * UNSUPPORTED. */ |
| 531 | int omega_gpu_fp8_general_rows(const OmegaGpuFp8Tensor *t, uint32_t expert, const float *x32, const uint16_t *x16, uint32_t m, |
| 532 | float *y, int refused) { |
| 533 | if (!t || !t->w.cpu || !t->s.cpu || expert >= t->experts) return OMEGA_GPU_FP8_BAD_ARGS; |
| 534 | const size_t sc = (size_t)(t->n / OMEGA_FP8_BLOCK) * (t->k / OMEGA_FP8_BLOCK); |
| 535 | const int rc = omega_gpu_gemm_ex_fp8_block_rows((const uint8_t *)t->w.cpu + (size_t)expert * t->n * t->k, (const float *)t->s.cpu + expert * sc, |
| 536 | t->s_mul, t->n, t->k, OMEGA_FP8_BLOCK, x32, x16, m, y, 0, NULL); |
| 537 | switch (rc) { |
| 538 | case OMEGA_GPU_GEMM_OK: return OMEGA_GPU_FP8_OK; |
| 539 | case OMEGA_GPU_GEMM_UNSUPPORTED: return refused; |
| 540 | case OMEGA_GPU_GEMM_BAD_ARGS: return OMEGA_GPU_FP8_BAD_ARGS; |
| 541 | case OMEGA_GPU_GEMM_TOO_LARGE: return OMEGA_GPU_FP8_TOO_LARGE; |
| 542 | case OMEGA_GPU_GEMM_CODEGEN_FAIL: return OMEGA_GPU_FP8_CODEGEN_FAIL; |
| 543 | default: return OMEGA_GPU_FP8_CHIP_FAIL; |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | /* the GEMV's own refusal of a spec, decided on the host without building the kernel */ |
| 548 | static int spec_refused(const OmegaFp8GemvSpec *spec, uint32_t n, int ragged) { |
| 549 | const uint32_t support = variant_now() == 2 ? omega_fp8_gemv2_shared_bytes(spec) : ragged ? omega_fp8_gemv_ragged_steps(spec) : omega_fp8_gemv_steps(spec); |
| 550 | return (int)OMEGA_AH(9, n, spec->rows, support); |
| 551 | } |
| 552 | |
| 553 | static int general_gemv(const OmegaGpuFp8Tensor *t, uint32_t groups, const uint32_t *ids, const float *x32, const uint16_t *x16, |
| 554 | uint32_t x_rows, float *y, OmegaGpuFp8Info *info, int refused, uint64_t t0) { |
| 555 | int rc = OMEGA_GPU_FP8_OK; |
| 556 | for (uint32_t gi = 0; gi < groups && rc == OMEGA_GPU_FP8_OK; gi++) { |
| 557 | const uint32_t r = x_rows == 1 ? 0u : gi; |
| 558 | rc = omega_gpu_fp8_general_rows(t, ids[gi], x32 ? x32 + (size_t)r * t->k : NULL, x16 ? x16 + (size_t)r * t->k : NULL, 1, |
| 559 | y + (size_t)gi * t->n, refused); |
| 560 | } |
| 561 | if (info) { |
| 562 | info->experts = t->experts; info->n = t->n; info->k = t->k; info->groups = groups; info->general = rc == OMEGA_GPU_FP8_OK; |
| 563 | info->weight_bytes = (uint64_t)groups * t->n * t->k; info->call_ns = omega_gpu_session_now_ns() - t0; |
| 564 | } |
| 565 | return rc; |
| 566 | } |
| 567 | |
| 568 | /* dev_tab / dev_x non-zero: the group table / the x' row already sit on the device (written by the router launch). |
| 569 | * rec non-NULL (record path, caller holds the session lock): dev_tab, dev_x and dev_y are required, the launch is |
| 570 | * appended to rec instead of submitted, and nothing is staged, poisoned or read back (the list's per-launch semaphore |
| 571 | * and the caller's end-of-token checks replace the poison check). A host-staged call (no rec, dev_tab, dev_x, no test |
| 572 | * override) whose shape the kernels refuse (groups past the table, a spec the codegen refuses) takes the general fallback above. */ |
| 573 | static int gemv_core(const OmegaGpuFp8Tensor *t, uint32_t groups, const uint32_t *ids, const float *x32, const uint16_t *x16, |
| 574 | uint32_t x_rows, float *y, OmegaGpuFp8Info *info, uint64_t dev_tab, uint64_t dev_x, |
| 575 | OmegaGpuCmdList *rec, uint64_t dev_y) { |
| 576 | uint64_t t0 = omega_gpu_session_now_ns(); |
| 577 | if (info) memset(info, 0, sizeof *info); |
| 578 | if (!OMEGA_AH(23, 0, rec != NULL, dev_tab, dev_x, dev_y)) return OMEGA_GPU_FP8_BAD_ARGS; |
| 579 | if (!t || !OMEGA_AH(23, 1, t->w.cpu != NULL, ids != NULL, dev_tab, x32 != NULL, x16 != NULL, dev_x, y != NULL, rec != NULL) || !OMEGA_AH(17, groups, x_rows)) return OMEGA_GPU_FP8_BAD_ARGS; |
| 580 | /* a call with a test override of the kernel shape or a kernel mutant keeps the refusal: those calls test the kernels */ |
| 581 | const int host_staged = (int)OMEGA_AH(16, rec != NULL, dev_tab, dev_x, g.lane_bytes, g.rows, g.warps, g.depth_set, g.mutant); |
| 582 | if (!dev_tab) for (uint32_t gi = 0; gi < groups; gi++) if (!OMEGA_AH(21, t->experts, ids[gi])) return OMEGA_GPU_FP8_BAD_ARGS; |
| 583 | if (OMEGA_AH(21, groups, OMEGA_GPU_FP8_MAX_GROUPS)) return host_staged ? general_gemv(t, groups, ids, x32, x16, x_rows, y, info, OMEGA_GPU_FP8_TOO_LARGE, t0) : OMEGA_GPU_FP8_TOO_LARGE; |
| 584 | const uint32_t n = t->n, k = t->k; |
| 585 | OmegaFp8GemvSpec spec; spec_for(k, n, groups, &spec); |
| 586 | const int ragged = omega_gpu_fp8_ragged_for(&spec, rec != NULL || dev_tab != 0 || dev_x != 0); |
| 587 | if (spec_refused(&spec, n, ragged)) { |
| 588 | if (host_staged) return general_gemv(t, groups, ids, x32, x16, x_rows, y, info, OMEGA_GPU_FP8_CODEGEN_FAIL, t0); |
| 589 | omega_gpu_session_set_error("fp8 codegen refused the shape"); |
| 590 | return OMEGA_GPU_FP8_CODEGEN_FAIL; |
| 591 | } |
| 592 | const uint32_t RG = (uint32_t)OMEGA_AH(22, n, spec.rows); |
| 593 | uint32_t grid_x; |
| 594 | const int variant = variant_now(); |
| 595 | uint32_t budget = (uint32_t)OMEGA_AH(18, g.cta_budget_set, g.cta_budget, groups, variant, !g.cta_budget_set && groups > 1u && variant != 2 ? omega_gpu_fp8_grouped_ctas_auto(groups) : 0); |
| 596 | /* big-k grid (qwen38/decode-speed): up to OMEGA_GPU_FP8_BIGK_CTAS CTAs, split so every warp gets the same number of row |
| 597 | * groups, at least two (measured, runs/sweep5 and sweep7: budget 384 over 256 gave mix +3%, gate_up +2.6%; the n = 5120 |
| 598 | * shapes run best at 160 CTAs, two row groups per warp: o_proj 186 -> 198 GB/s) */ |
| 599 | const int bigk_grid = OMEGA_AH(19, g.cta_budget_set, groups, k, 1) ? omega_gpu_fp8_bigk() : 0; |
| 600 | grid_x = (uint32_t)OMEGA_AH(10, n, spec.rows, spec.warps, budget, bigk_grid); |
| 601 | |
| 602 | if (!rec) omega_gpu_session_lock(); |
| 603 | #define UNLOCK() do { if (!rec) omega_gpu_session_unlock(); } while (0) |
| 604 | int rc = OMEGA_GPU_FP8_OK; |
| 605 | if (!dev_open_locked()) { UNLOCK(); return OMEGA_GPU_FP8_CHIP_FAIL; } |
| 606 | if (!rec) { /* staging buffers: the record path reads and writes only the caller's device buffers */ |
| 607 | if (!g.tab.cpu && omega_gpu_session_alloc((size_t)OMEGA_GPU_FP8_MAX_GROUPS * 16u, &g.tab) != 0) { UNLOCK(); return OMEGA_GPU_FP8_CHIP_FAIL; } |
| 608 | int src = omega_gpu_session_scratch(&g.x_buf, (size_t)x_rows * k * 4u); |
| 609 | if (src == 0) src = omega_gpu_session_scratch(&g.y_buf, (size_t)groups * n * 4u); |
| 610 | if (src != 0) { UNLOCK(); return src == OMEGA_GPU_SESSION_SCRATCH_RESERVED ? OMEGA_GPU_FP8_TOO_LARGE : OMEGA_GPU_FP8_CHIP_FAIL; } |
| 611 | } |
| 612 | Slot *ks = NULL; bool hit = false; |
| 613 | g_rec = rec != NULL; |
| 614 | rc = kernel_for(&spec, ragged, &ks, &hit); |
| 615 | g_rec = 0; |
| 616 | if (rc != OMEGA_GPU_FP8_OK) { UNLOCK(); return rc; } |
| 617 | |
| 618 | /* group table: {w_va, s_va} per group */ |
| 619 | uint64_t *tab = g.tab.cpu; |
| 620 | const uint64_t wstride = (uint64_t)n * k, sstride = (uint64_t)(n / OMEGA_FP8_BLOCK) * (k / OMEGA_FP8_BLOCK) * 4u; |
| 621 | if (!dev_tab) for (uint32_t gi = 0; gi < groups; gi++) { uint64_t ent[2]; av_fp8_group(ent, ids[gi], t->w.va, t->s.va, wstride, sstride); tab[2 * gi] = ent[0]; tab[2 * gi + 1] = ent[1]; } |
| 622 | /* x' rows, y poisoned */ |
| 623 | float *xd = g.x_buf.mem.cpu, *yd = g.y_buf.mem.cpu; |
| 624 | for (uint32_t r = 0; r < x_rows && !dev_x; r++) { |
| 625 | if (x32) omega_fp8_pack_x_f32(x32 + (size_t)r * k, k, xd + (size_t)r * k); |
| 626 | else omega_fp8_pack_x_bf16(x16 + (size_t)r * k, k, xd + (size_t)r * k); |
| 627 | } |
| 628 | if (!rec) { |
| 629 | for (size_t i = 0; i < (size_t)groups * n; i++) yd[i] = POISON_F32; |
| 630 | if (omega_gpu_session_sync_to_gpu(&g.tab) != 0 || omega_gpu_session_sync_to_gpu(&g.x_buf.mem) != 0 || omega_gpu_session_sync_to_gpu(&g.y_buf.mem) != 0) rc = OMEGA_GPU_FP8_CHIP_FAIL; |
| 631 | } |
| 632 | |
| 633 | uint32_t args[OMEGA_FP8_ARGS_WORDS]; |
| 634 | const uint64_t tab_va = dev_tab ? dev_tab : g.tab.va, x_va = dev_x ? dev_x : g.x_buf.mem.va, y_va = rec ? dev_y : g.y_buf.mem.va; |
| 635 | args[0] = (uint32_t)tab_va; args[1] = (uint32_t)(tab_va >> 32); |
| 636 | args[2] = (uint32_t)x_va; args[3] = (uint32_t)(x_va >> 32); |
| 637 | args[4] = (uint32_t)y_va; args[5] = (uint32_t)(y_va >> 32); |
| 638 | args[6] = RG; args[7] = x_rows == 1 ? 0u : k * 4u; args[8] = n * 4u; args[9] = grid_x * spec.warps; |
| 639 | if (info) { |
| 640 | info->experts = t->experts; info->n = n; info->k = k; info->groups = groups; |
| 641 | info->lane_bytes = spec.lane_bytes; info->rows_per_warp = spec.rows; info->warps_per_cta = spec.warps; info->depth = spec.depth; |
| 642 | info->grid_x = grid_x; info->grid_y = groups; info->steps = ragged ? omega_fp8_gemv_ragged_steps(&spec) : omega_fp8_gemv_steps(&spec); |
| 643 | info->weight_bytes = (uint64_t)groups * wstride; info->scale_bytes = (uint64_t)groups * sstride; |
| 644 | info->kernel_cache_hit = hit; |
| 645 | (void)omega_gpu_fmt(info->target_chip, sizeof info->target_chip, "%s", "NVIDIA DGX Spark (Grace Blackwell GB10)"); |
| 646 | info->sm_architecture = 121; |
| 647 | } |
| 648 | if (rc == OMEGA_GPU_FP8_OK) { |
| 649 | OmegaGpuLaunch L = { |
| 650 | .code_va = ks->code.va, .gpr_count = gpr_declared(ks->kernel.gpr_count) + g.gpr_pad, |
| 651 | .threads_x = 32u * spec.warps, .threads_y = 1, .grid_x = grid_x, .grid_y = groups, |
| 652 | .num_elements = (size_t)groups * n, |
| 653 | .args = args, .n_args = OMEGA_FP8_ARGS_WORDS, .timeout_ms = MARKER_TIMEOUT_MS, |
| 654 | .shared_bytes = shared_for(variant, ragged, &spec), |
| 655 | }; |
| 656 | if (L.gpr_count < ks->kernel.gpr_count + 2u) { omega_gpu_session_set_error("declared registers below peak + 2: refusing to launch"); rc = OMEGA_GPU_FP8_CHIP_FAIL; } |
| 657 | else if (rec) { |
| 658 | OmegaGpuNativeLease lease = omega_native_cache_lease(&g.native, (unsigned)(ks - g.cache)); |
| 659 | if (omega_gpu_cmdlist_add_native(rec, &L, &lease) < 0) rc = OMEGA_GPU_FP8_CHIP_FAIL; |
| 660 | } |
| 661 | else if (omega_gpu_session_launch(&L, info ? &info->elapsed_ns : NULL, info ? &info->completion_marker : NULL) != 0) rc = OMEGA_GPU_FP8_CHIP_FAIL; |
| 662 | } |
| 663 | if (rc == OMEGA_GPU_FP8_OK && !rec && omega_gpu_session_sync_to_cpu(&g.y_buf.mem) != 0) rc = OMEGA_GPU_FP8_CHIP_FAIL; |
| 664 | if (rc == OMEGA_GPU_FP8_OK && !rec) { |
| 665 | size_t unchanged = 0; |
| 666 | for (size_t i = 0; i < (size_t)groups * n; i++) if (yd[i] == POISON_F32) unchanged++; |
| 667 | if (unchanged) { omega_gpu_session_set_error("output poison survived"); rc = OMEGA_GPU_FP8_CHIP_FAIL; } |
| 668 | else memcpy(y, yd, (size_t)groups * n * sizeof *y); |
| 669 | } |
| 670 | UNLOCK(); |
| 671 | if (info) info->call_ns = omega_gpu_session_now_ns() - t0; |
| 672 | return rc; |
| 673 | } |
| 674 | #undef UNLOCK |
| 675 | |
| 676 | int omega_gpu_fp8_gemv_f32(const OmegaGpuFp8Tensor *t, uint32_t expert, const float *x, float *y, OmegaGpuFp8Info *info) { |
| 677 | return gemv_core(t, 1, &expert, x, NULL, 1, y, info, 0, 0, NULL, 0); |
| 678 | } |
| 679 | int omega_gpu_fp8_gemv_bf16(const OmegaGpuFp8Tensor *t, uint32_t expert, const uint16_t *x, float *y, OmegaGpuFp8Info *info) { |
| 680 | return gemv_core(t, 1, &expert, NULL, x, 1, y, info, 0, 0, NULL, 0); |
| 681 | } |
| 682 | int omega_gpu_fp8_gemv_grouped_f32(const OmegaGpuFp8Tensor *t, uint32_t groups, const uint32_t *ids, const float *x, uint32_t x_rows, float *y, OmegaGpuFp8Info *info) { |
| 683 | return gemv_core(t, groups, ids, x, NULL, x_rows, y, info, 0, 0, NULL, 0); |
| 684 | } |
| 685 | 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) { |
| 686 | if (!tab_va || !x_va) return OMEGA_GPU_FP8_BAD_ARGS; |
| 687 | return gemv_core(t, groups, NULL, NULL, NULL, 1, y, info, tab_va, x_va, NULL, 0); |
| 688 | } |
| 689 | int omega_gpu_fp8_tensor_entry(const OmegaGpuFp8Tensor *t, uint32_t expert, uint64_t *w_va, uint64_t *s_va) { |
| 690 | if (!t || !t->w.cpu || expert >= t->experts || !w_va || !s_va) return OMEGA_GPU_FP8_BAD_ARGS; |
| 691 | *w_va = t->w.va + (uint64_t)expert * t->n * t->k; |
| 692 | *s_va = t->s.va + (uint64_t)expert * (t->n / OMEGA_FP8_BLOCK) * (t->k / OMEGA_FP8_BLOCK) * 4u; |
| 693 | return OMEGA_GPU_FP8_OK; |
| 694 | } |
| 695 | |
| 696 | /* Record path (OWN-30B W-L2a decode graph): the launch of omega_gpu_fp8_gemv_grouped_dev appended to cl, every input |
| 697 | * and the output in device memory. Caller holds omega_gpu_session_lock(). */ |
| 698 | int omega_gpu_fp8_gemv_grouped_record(OmegaGpuCmdList *cl, const OmegaGpuFp8Tensor *t, uint32_t groups, uint64_t tab_va, |
| 699 | uint64_t x_va, uint32_t x_rows, uint64_t y_va) { |
| 700 | if (!cl) return OMEGA_GPU_FP8_BAD_ARGS; |
| 701 | return gemv_core(t, groups, NULL, NULL, NULL, x_rows, NULL, NULL, tab_va, x_va, cl, y_va); |
| 702 | } |
| 703 | |
| 704 | /* ---- weight cache policy (OWN-30B L2 kernel-time cut) ---- */ |
| 705 | void omega_gpu_fp8_set_weight_cache(int on) { omega_gpu_session_lock(); g.weights_cached = on ? 1 : 0; g.weights_cached_set = 1; omega_gpu_session_unlock(); } |
| 706 | int omega_gpu_fp8_weight_cache(void) { return g.weights_cached; } |
| 707 | |
| 708 | /* ---- host quantiser (qwen38/decode-speed: the FP8 lm_head) ---- */ |
| 709 | |
| 710 | /* f32 -> e4m3 (bias 7, max 448, no infinities), round to nearest, ties to even; |v| >= 448 (and NaN) saturate to 448: |
| 711 | * 0x7F (NaN) is never produced. Pure integer/float bit work, no libm. */ |
| 712 | uint8_t omega_gpu_fp8_e4m3_rne(float v) { return av_e4m3(v); } |
| 713 | |
| 714 | int omega_gpu_fp8_quantize_bf16(uint32_t n, uint32_t k, const uint16_t *w, uint8_t *w8, uint16_t *scale_inv_bf16) { |
| 715 | return av_fp8_quantize(n, k, w, w8, scale_inv_bf16) ? OMEGA_GPU_FP8_BAD_ARGS : OMEGA_GPU_FP8_OK; |
| 716 | } |
| 717 | |
| 718 | /* ---- slot pool support (omega_gpu_fp8_internal.h, Expert Ribbon omega#533) ---- */ |
| 719 | int omega_gpu_fp8_tensor_alloc_uncached(uint32_t experts, uint32_t n, uint32_t k, OmegaGpuFp8Tensor **out) { |
| 720 | if (out) *out = NULL; |
| 721 | if (!out || experts == 0 || n == 0 || k == 0 || n % OMEGA_FP8_BLOCK || k % OMEGA_FP8_BLOCK) return OMEGA_GPU_FP8_BAD_ARGS; |
| 722 | if (k > OMEGA_FP8_MAX_K) return OMEGA_GPU_FP8_TOO_LARGE; |
| 723 | const size_t wbytes = (size_t)experts * n * k, scount = (size_t)experts * (n / OMEGA_FP8_BLOCK) * (k / OMEGA_FP8_BLOCK); |
| 724 | OmegaGpuFp8Tensor *t = omega_gpu_host_calloc(1, sizeof *t); |
| 725 | if (!t) return OMEGA_GPU_FP8_BAD_ARGS; |
| 726 | t->experts = experts; t->n = n; t->k = k; |
| 727 | int rc = OMEGA_GPU_FP8_OK; |
| 728 | omega_gpu_session_lock(); |
| 729 | if (!dev_open_locked() || omega_gpu_session_alloc(wbytes, &t->w) != 0 || omega_gpu_session_alloc(scount * 4u, &t->s) != 0) rc = OMEGA_GPU_FP8_CHIP_FAIL; |
| 730 | omega_gpu_session_unlock(); |
| 731 | if (rc == OMEGA_GPU_FP8_OK) { |
| 732 | memset(t->w.cpu, 0, wbytes); |
| 733 | memset(t->s.cpu, 0, scount * 4u); |
| 734 | if (omega_gpu_session_sync_to_gpu(&t->w) != 0 || omega_gpu_session_sync_to_gpu(&t->s) != 0) rc = OMEGA_GPU_FP8_CHIP_FAIL; |
| 735 | } |
| 736 | if (rc != OMEGA_GPU_FP8_OK) { omega_gpu_fp8_tensor_free(t); return rc; } |
| 737 | *out = t; |
| 738 | return OMEGA_GPU_FP8_OK; |
| 739 | } |
| 740 | int omega_gpu_fp8_tensor_cpu_view(const OmegaGpuFp8Tensor *t, uint32_t expert, uint8_t **w, float **s) { |
| 741 | if (!t || !t->w.cpu || !t->s.cpu || expert >= t->experts || !w || !s) return OMEGA_GPU_FP8_BAD_ARGS; |
| 742 | *w = (uint8_t *)t->w.cpu + (size_t)expert * t->n * t->k; |
| 743 | *s = (float *)t->s.cpu + (size_t)expert * (t->n / OMEGA_FP8_BLOCK) * (t->k / OMEGA_FP8_BLOCK); |
| 744 | return OMEGA_GPU_FP8_OK; |
| 745 | } |
| 746 | int omega_gpu_fp8_tensor_sync_to_gpu(const OmegaGpuFp8Tensor *t) { |
| 747 | if (!t) return -1; |
| 748 | return (omega_gpu_session_sync_to_gpu(&t->w) != 0 || omega_gpu_session_sync_to_gpu(&t->s) != 0) ? -1 : 0; |
| 749 | } |