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.c # 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.c` 634 lines ยท 39348 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 /* omega_gpu_fp8_api.c: FP8 e4m3 GEMV entry points on the shared session (OWN-30B Cut 6 / Cut 5). 18 * See the header. Submission path and buffer policy are the matmul API's: every buffer both sides 19 * touch is GPU-uncached (nvos.h NVOS32_ATTR2_GPU_CACHEABLE: "For system memory this will not be 20 * coherent with direct CPU mappings"), one launch = omega_gpu_session_launch (shader-cache 21 * invalidate, constant bank, QMD0/QMD1, marker, L2_FLUSH_DIRTY, second marker on uncached memory). */ 22 #include "omega_gpu_fp8_api.h" 23 #include "omega_gpu_mem_cached.h" 24 #include "omega_blackwell_codegen_moe.h" 25 #include "omega_blackwell_codegen_moe2.h" /* variant 2 (W-L2c) */ 26 #include "omega_gpu_session.h" 27 #include "omega_gpu_fmt.h" 28 #include "omega_gpu_cmdlist.h" 29 #include "omega_blackwell_qmd.h" /* OMEGA_BW_QMD_MAX_SHARED_BYTES: the deep kernel's x stage must fit the launch descriptor */ 30 #include "omega_gpu_fp8_internal.h" 31 #include 32 #include 33 34 #define CACHE_SLOTS 16 35 #define MARKER_TIMEOUT_MS 20000ull 36 #define POISON_F32 (-999.0f) 37 38 struct OmegaGpuFp8Tensor { 39 uint32_t experts, n, k; 40 OmegaGpuMem w; /* experts * n * k e4m3 bytes, row-major */ 41 OmegaGpuMem s; /* experts * (n/128) * (k/128) f32 (scale * 2^56) */ 42 }; 43 44 typedef struct { 45 bool valid, pinned; 46 uint32_t variant; /* 1 or 2: which generator built the kernel (part of the cache key) */ 47 OmegaFp8GemvSpec spec; 48 OmegaBlackwellKernel kernel; 49 OmegaGpuMem code; 50 } Slot; 51 52 static struct { 53 OmegaGpuScratch x_buf, y_buf; 54 OmegaGpuMem tab; /* group table, OMEGA_GPU_FP8_MAX_GROUPS * 16 bytes */ 55 uint32_t cta_budget; int cta_budget_set; 56 uint32_t lane_bytes, rows, warps; /* overrides, 0 = automatic */ 57 int depth_set; uint32_t depth; /* depth override (depth_set: 0 = automatic) */ 58 uint32_t gpr_pad; /* registers declared beyond the kernel's peak (diagnosis knob) */ 59 int mutant, no_fold, sealed; 60 int variant, variant_set; /* GEMV variant 1 or 2 (omega_gpu_fp8_set_variant / AIEN_OMEGA_FP8_GEMV), read once */ 61 int weights_cached; /* omega_gpu_fp8_set_weight_cache: uploads after it allocate GPU-cached (read-only weights) */ 62 int weights_cached_set; /* 0: never set, the first upload reads env AIEN_OMEGA_FP8_WEIGHT_CACHE (W-L2d: "1" = cached) */ 63 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 */ 64 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 */ 65 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 */ 66 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 */ 67 bool hooked; 68 Slot cache[CACHE_SLOTS]; 69 unsigned next; 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 omega_gpu_session_free(&g.cache[i].code); 90 memset(&g.cache[i], 0, sizeof g.cache[i]); 91 } 92 g.next = 0; 93 } 94 static void on_session_close(void) { 95 cache_clear_locked(); 96 omega_gpu_session_scratch_free(&g.x_buf); 97 omega_gpu_session_scratch_free(&g.y_buf); 98 omega_gpu_session_free(&g.tab); 99 g.sealed = 0; 100 } 101 static OmegaGpuSession *dev_open_locked(void) { 102 if (!g.hooked) { (void)omega_gpu_session_on_close(on_session_close); g.hooked = true; } 103 return omega_gpu_session_open(); 104 } 105 void omega_gpu_fp8_cache_clear(void) { omega_gpu_session_lock(); cache_clear_locked(); omega_gpu_session_unlock(); } 106 void omega_gpu_fp8_set_shape(uint32_t lb, uint32_t rows, uint32_t warps) { 107 omega_gpu_session_lock(); g.lane_bytes = lb; g.rows = rows; g.warps = warps; cache_clear_locked(); omega_gpu_session_unlock(); 108 } 109 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(); } 110 void omega_gpu_fp8_set_gpr_pad(uint32_t pad) { omega_gpu_session_lock(); g.gpr_pad = pad; omega_gpu_session_unlock(); } 111 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(); } 112 void omega_gpu_fp8_test_set_mutant(int m) { omega_gpu_session_lock(); g.mutant = m; cache_clear_locked(); omega_gpu_session_unlock(); } 113 void omega_gpu_fp8_test_set_no_fold(int on) { omega_gpu_session_lock(); g.no_fold = on ? 1 : 0; omega_gpu_session_unlock(); } 114 void omega_gpu_fp8_seal(int on) { omega_gpu_session_lock(); g.sealed = on ? 1 : 0; omega_gpu_session_unlock(); } 115 116 /* ---- GEMV variant (W-L2c): 1 = omega_blackwell_codegen_moe.c (default), 2 = omega_blackwell_codegen_moe2.c (row-pipelined) ---- */ 117 static int variant_now(void) { 118 if (!g.variant_set) { 119 const char *v = getenv("AIEN_OMEGA_FP8_GEMV"); 120 g.variant = (v && v[0] == '2' && v[1] == 0) ? 2 : 1; 121 g.variant_set = 1; 122 } 123 return g.variant; 124 } 125 void omega_gpu_fp8_set_variant(int variant) { 126 omega_gpu_session_lock(); 127 g.variant = variant == 2 ? 2 : 1; g.variant_set = 1; 128 cache_clear_locked(); 129 omega_gpu_session_unlock(); 130 } 131 int omega_gpu_fp8_variant(void) { return variant_now(); } 132 133 /* ---- kernel schedule (CUT5): 0 = emitted stalls (default), 1 = pipelined decode + dependency stall pass (variant 1 only) ---- */ 134 static int sched_now(void) { 135 if (!g.sched_set) { 136 const char *v = getenv("AIEN_OMEGA_FP8_SCHED"); 137 g.sched = (v && v[0] == '1' && v[1] == 0) ? 1 : 0; 138 g.sched_set = 1; 139 } 140 return g.sched; 141 } 142 void omega_gpu_fp8_set_sched(int sched) { 143 omega_gpu_session_lock(); 144 g.sched = sched == 1 ? 1 : 0; g.sched_set = 1; 145 cache_clear_locked(); 146 omega_gpu_session_unlock(); 147 } 148 int omega_gpu_fp8_sched(void) { return sched_now(); } 149 /* ---- CUT5 variants A and B (variant 1 only): 0 = off, 1 = on. Never set: the env decides once; A (coal) defaults ON (env 150 * value "0" turns it off; SPEED-A 2026-10-17: OWN-30B decode graph 24.49 -> 21.34 ms/token, tokens identical), B (wfirst) 151 * defaults OFF (env value "1" turns it on; cold bench: not distinguishable from noise) ---- */ 152 static int flag_now(int *v, int *set, const char *env, int dflt) { 153 if (!*set) { 154 const char *s = getenv(env); 155 *v = (s && s[0] == '0' && s[1] == 0) ? 0 : (s && s[0] == '1' && s[1] == 0) ? 1 : dflt; 156 *set = 1; 157 } 158 return *v; 159 } 160 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(); } 161 void omega_gpu_fp8_set_coal(int on) { flag_set(&g.coal, &g.coal_set, on); } 162 int omega_gpu_fp8_coal(void) { return flag_now(&g.coal, &g.coal_set, "AIEN_OMEGA_FP8_COAL", 1); } 163 void omega_gpu_fp8_set_wfirst(int on) { flag_set(&g.wfirst, &g.wfirst_set, on); } 164 int omega_gpu_fp8_wfirst(void) { return flag_now(&g.wfirst, &g.wfirst_set, "AIEN_OMEGA_FP8_WFIRST", 0); } 165 void omega_gpu_fp8_set_deep(int on) { flag_set(&g.deep, &g.deep_set, on); } 166 int omega_gpu_fp8_deep(void) { return flag_now(&g.deep, &g.deep_set, "AIEN_OMEGA_FP8_DEEP", 1); } 167 void omega_gpu_fp8_set_bigk(int on) { flag_set(&g.bigk, &g.bigk_set, on); } 168 int omega_gpu_fp8_bigk(void) { return flag_now(&g.bigk, &g.bigk_set, "AIEN_OMEGA_FP8_BIGK", 1); } 169 /* codegen by variant */ 170 static int prog_for(int variant, const OmegaFp8GemvSpec *spec, BlackwellIRProgram *prog) { 171 return variant == 2 ? omega_blackwell_codegen_fp8_gemv2_prog(spec, prog) : omega_blackwell_codegen_fp8_gemv_prog(spec, prog); 172 } 173 static int stats_for(int variant, const OmegaFp8GemvSpec *spec, uint32_t *insns, uint32_t *gprs) { 174 return variant == 2 ? omega_fp8_gemv2_prog_stats(spec, insns, gprs) : omega_fp8_gemv_prog_stats(spec, insns, gprs); 175 } 176 static uint32_t shared_for(int variant, const OmegaFp8GemvSpec *spec) { 177 return variant == 2 ? omega_fp8_gemv2_shared_bytes(spec) : omega_fp8_gemv_shared_bytes(spec); 178 } 179 180 /* ---- shape choice ---- */ 181 182 /* Largest lane width dividing k (16 for k % 512 == 0: one LDG.E.128 per row per step); one row per warp: 183 * the chip sweep (evidence/OWN30B-FP8-GEMV-`7ce5dd4b`-*, rows 1/2/4/8 one variable at a time) measured rows = 1 184 * fastest at every shape (more warps and CTAs in flight on the same bytes beats fewer, wider warps). */ 185 void omega_gpu_fp8_shape_for(uint32_t k, uint32_t n, uint32_t *lane_bytes, uint32_t *rows, uint32_t *warps) { 186 uint32_t lb = k % 512u == 0 ? 16u : 8u; /* k % 256 == 0 is required (codegen refuses otherwise) */ 187 uint32_t r = 1; (void)n; 188 if (lane_bytes) *lane_bytes = g.lane_bytes ? g.lane_bytes : lb; 189 if (rows) *rows = g.rows ? g.rows : r; 190 if (warps) *warps = g.warps ? g.warps : 8u; 191 } 192 193 /* ---- kernel cache (lock held, device open) ---- */ 194 195 /* Registers declared in the QMD: the peak plus one, rounded up to a multiple of 8. Measured on the chip 196 * (evidence OWN30B-FP8-GEMV-`c4006391`, kernel log Xid 13 "Out Of Range Register"): a kernel whose peak is 197 * exactly 64 (top register R63) and declares 64 faults; declaring 65 runs. Every earlier kernel had a peak 198 * that was not a multiple of 8, so the hardware's rounding hid the rule. */ 199 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) */ 200 201 /* Register file: a CTA of 32 * warps threads must fit the SM's 65536 registers at the declared count. Measured 202 * (chip, 2026-10-13, kernel log Xid 13): the deep k=2048 kernel (72 declared) at 32 warps faults, 73728 > 65536. */ 203 static bool register_file_fits(uint32_t warps, uint32_t peak) { 204 uint32_t decl = gpr_declared(peak); 205 if ((uint64_t)32u * warps * decl <= 65536u) return true; 206 char e[160]; omega_gpu_fmt(e, sizeof e, "fp8 kernel: %u warps x %u declared registers exceed the 65536-register file", warps, decl); 207 omega_gpu_session_set_error(e); 208 return false; 209 } 210 211 int omega_gpu_fp8_kernel_fits(const OmegaFp8GemvSpec *spec, uint32_t *declared) { 212 uint32_t peak = 0; 213 if (!spec || stats_for(variant_now(), spec, NULL, &peak) != 0) { omega_gpu_session_set_error("fp8 codegen refused the shape"); return OMEGA_GPU_FP8_CODEGEN_FAIL; } 214 if (declared) *declared = gpr_declared(peak); 215 return register_file_fits(spec->warps, peak) ? OMEGA_GPU_FP8_OK : OMEGA_GPU_FP8_TOO_LARGE; 216 } 217 218 static int kernel_for(const OmegaFp8GemvSpec *spec, Slot **out, bool *hit) { 219 const uint32_t variant = (uint32_t)variant_now(); 220 for (unsigned i = 0; i < CACHE_SLOTS; i++) { 221 Slot *s = &g.cache[i]; 222 if (s->valid && s->variant == variant && memcmp(&s->spec, spec, sizeof *spec) == 0) { *out = s; *hit = true; return OMEGA_GPU_FP8_OK; } 223 } 224 if (g.sealed) { 225 char e[160]; 226 omega_gpu_fmt(e, sizeof e, "serving reservation exceeded: fp8 kernel k=%u lb=%u rows=%u warps=%u was not prepared", 227 spec->k, spec->lane_bytes, spec->rows, spec->warps); 228 omega_gpu_session_set_error(e); 229 return OMEGA_GPU_FP8_TOO_LARGE; 230 } 231 Slot *s = NULL; 232 for (unsigned i = 0; i < CACHE_SLOTS && !s; i++) { 233 Slot *c = &g.cache[(g.next + i) % CACHE_SLOTS]; 234 if (!c->valid || !c->pinned) { s = c; g.next = (unsigned)((c - g.cache) + 1) % CACHE_SLOTS; } 235 } 236 if (!s) { omega_gpu_session_set_error("fp8 kernel cache: every slot holds a prepared kernel"); return OMEGA_GPU_FP8_TOO_LARGE; } 237 /* record path: a recorded launch may still point at the slot's code until the list is submitted */ 238 if (g_rec && s->valid) { omega_gpu_session_set_error("fp8 record: kernel cache miss would evict a resident kernel (warm up before recording)"); return OMEGA_GPU_FP8_TOO_LARGE; } 239 BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); 240 if (!prog) { omega_gpu_session_set_error("fp8 codegen: host memory"); return OMEGA_GPU_FP8_CODEGEN_FAIL; } 241 int cg = prog_for((int)variant, spec, prog); 242 size_t need = cg == 0 ? omega_bw_program_code_bytes(prog) : 0; 243 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; } 244 if (!register_file_fits(spec->warps, prog->regalloc.peak_gpr_usage)) { omega_gpu_host_free(prog); return OMEGA_GPU_FP8_TOO_LARGE; } 245 OmegaGpuMem code; 246 if (omega_gpu_session_alloc_code(need, &code) != 0) { omega_gpu_host_free(prog); return OMEGA_GPU_FP8_CHIP_FAIL; } 247 OmegaBlackwellKernel kernel; 248 cg = omega_blackwell_kernel_encode_into(prog, code.cpu, need, &kernel); 249 omega_gpu_host_free(prog); 250 if (cg != 0 || !kernel.code || kernel.code_size != need) { omega_gpu_session_free(&code); return OMEGA_GPU_FP8_CODEGEN_FAIL; } 251 if (omega_gpu_session_sync_to_gpu(&code) != 0) { omega_gpu_session_free(&code); return OMEGA_GPU_FP8_CHIP_FAIL; } 252 if (s->valid) omega_gpu_session_free(&s->code); 253 memset(s, 0, sizeof *s); 254 s->valid = true; s->variant = variant; s->spec = *spec; s->kernel = kernel; s->code = code; 255 *out = s; *hit = false; 256 return OMEGA_GPU_FP8_OK; 257 } 258 259 /* Deep kernel (x' staged in shared memory, every load of the row issued at once) whenever the whole row fits 260 * the budgets (rows * KS * lane_bytes <= 128, i.e. rows * k <= 4096) and the staging rounds are exact; else the step-by-step kernel. */ 261 uint32_t omega_gpu_fp8_depth_for(const OmegaFp8GemvSpec *spec, uint32_t n, uint32_t groups) { 262 if (g.depth_set) return g.depth; 263 uint32_t ks = spec->k / (32u * spec->lane_bytes); 264 /* big k (qwen38/decode-speed, opt-in AIEN_OMEGA_FP8_BIGK=1): the whole row cannot be in flight, so the largest step 265 * count that divides KS within the 128-byte lane budget; x' still has to fit the launch descriptor's shared memory */ 266 const int partial = ks * spec->rows * spec->lane_bytes > 128u && spec->k > 4096u && groups == 1u && omega_gpu_fp8_bigk(); 267 if (partial) { do ks--; while (ks > 1u && (spec->k / (32u * spec->lane_bytes) % ks || ks * spec->rows * spec->lane_bytes > 128u)); if (ks < 2u) return 0; } 268 if (ks * spec->rows * spec->lane_bytes > 128u || spec->k % (32u * spec->warps) || spec->k > 2048u * spec->warps) return 0; 269 /* Measured (evidence OWN30B-FP8-GEMV-`fc05176a`-*, main vs depth0 in one run, before variant A): the deep kernel won on 270 * single projections up to a few MB (gate+up 1536x2048: 30 us vs 51; k_proj: 30 vs 44) and lost on the grouped 271 * 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 272 * was "classic above 4 MB". Re-measured under A (SPEED-1B, 2026-10-17, cold GPU-stamped, 5/5 interleaved, 273 * ~/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 274 * 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 275 * kernel's x stage exceeds the launch descriptor's shared-memory limit (qmd build (shared_bytes), 5/5), which is the bound 276 * checked below. AIEN_OMEGA_FP8_DEEP=0 or set_deep(0) restores the classic kernel everywhere. */ 277 (void)n; 278 if (groups != 1u || !omega_gpu_fp8_deep()) return 0; 279 OmegaFp8GemvSpec deep = *spec; deep.depth = ks; deep.mutant = 0; 280 if (omega_fp8_gemv_shared_bytes(&deep) > (partial ? OMEGA_BW_QMD_MAX_SHARED_BYTES : OMEGA_GPU_FP8_DEEP_MAX_SHARED_BYTES)) return 0; 281 /* The register check builds the whole kernel (codegen + register allocation). spec_for runs on every launch and every 282 * record, so the answer is remembered per spec: SPEED-1B graph A/B (2026-10-17) measured the unremembered check at 283 * 853..1172 us of host record time per token (48 qkv records) against 45..113 us without the deep default. The answer is 284 * a pure function of the spec (deterministic codegen), so the memo never needs clearing; spec_for zeroes the struct, 285 * so memcmp over the padding is exact. */ 286 static struct { OmegaFp8GemvSpec spec; uint32_t fits; } memo[8]; 287 static unsigned memo_n, memo_next; 288 for (unsigned i = 0; i < memo_n; i++) 289 if (memcmp(&memo[i].spec, &deep, sizeof deep) == 0) return memo[i].fits ? ks : 0; 290 uint32_t peak = 0; 291 const uint32_t fits = omega_fp8_gemv_prog_stats(&deep, NULL, &peak) == 0 && (uint64_t)32u * spec->warps * gpr_declared(peak) <= 65536u; 292 const unsigned at = memo_n < 8u ? memo_n++ : (memo_next++ % 8u); 293 memo[at].spec = deep; memo[at].fits = fits; 294 return fits ? ks : 0; 295 } 296 297 static void spec_for(uint32_t k, uint32_t n, uint32_t groups, OmegaFp8GemvSpec *spec) { 298 memset(spec, 0, sizeof *spec); 299 spec->k = k; 300 omega_gpu_fp8_shape_for(k, n, &spec->lane_bytes, &spec->rows, &spec->warps); 301 /* Grouped launches (8 experts): 32 warps per CTA and few CTAs per group (OMEGA_GPU_FP8_GROUPED_CTAS, one per SM 302 * for 8 groups) measured fastest: down 2048x768 x 8 went from 181 us (256 x 8 CTAs of 8 warps) to 90 us 303 * (4..6 x 8 CTAs of 32 warps); gate+up 146 -> 140 us (probe logs listed in ~/handoffs/own30b-K-report.md). */ 304 if (groups > 1u && !g.warps) spec->warps = 32u; 305 /* big k (opt-in AIEN_OMEGA_FP8_BIGK=1): 16 warps per CTA (sweep 2026-10-19, ~/workspace/omega-q38-speed-work/runs/sweep1: 306 * warps 16 beat 8 on every Qwen3.8-27B projection, 4 lost) */ 307 if (groups == 1u && k > 4096u && !g.warps && omega_gpu_fp8_bigk()) spec->warps = 16u; 308 if (variant_now() == 2) { 309 /* variant 2 (W-L2c): 16 warps per CTA (two CTAs per SM at its register count) for every launch unless overridden; 310 * the whole row group is always in flight (depth = KS, informational: the generator ignores the field). */ 311 if (!g.warps) spec->warps = OMEGA_GPU_FP8_V2_WARPS; 312 spec->depth = spec->k / (32u * spec->lane_bytes); 313 } else { 314 spec->sched = (uint32_t)sched_now(); 315 spec->coal = omega_gpu_fp8_coal() && spec->warps * spec->rows <= 32u; /* A: shapes whose round fits one store warp */ 316 spec->wfirst = (uint32_t)omega_gpu_fp8_wfirst(); /* B: deep kernels ignore it */ 317 spec->depth = omega_gpu_fp8_depth_for(spec, n, groups); /* after coal: the deep kernel's shared bytes include A's slots */ 318 } 319 spec->mutant = g.mutant; 320 } 321 /* Host-only: the spec the automatic choice makes for a call (no chip, no cache; tests check the defaults with it). */ 322 void omega_gpu_fp8_spec_for(uint32_t k, uint32_t n, uint32_t groups, OmegaFp8GemvSpec *spec) { spec_for(k, n, groups, spec); } 323 324 int omega_gpu_fp8_prepare(uint32_t k) { 325 OmegaFp8GemvSpec spec; spec_for(k, 1u << 20, 1, &spec); 326 omega_gpu_session_lock(); 327 int rc = OMEGA_GPU_FP8_OK; 328 if (!dev_open_locked()) rc = OMEGA_GPU_FP8_CHIP_FAIL; 329 else { Slot *s = NULL; bool hit = false; rc = kernel_for(&spec, &s, &hit); if (rc == OMEGA_GPU_FP8_OK) s->pinned = true; } 330 omega_gpu_session_unlock(); 331 return rc; 332 } 333 334 /* ---- tensors ---- */ 335 336 void omega_gpu_fp8_tensor_shape(const OmegaGpuFp8Tensor *t, uint32_t *experts, uint32_t *n, uint32_t *k) { 337 if (experts) *experts = t ? t->experts : 0; 338 if (n) *n = t ? t->n : 0; 339 if (k) *k = t ? t->k : 0; 340 } 341 342 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) { 343 if (out) *out = NULL; 344 if (!w || !scale || !out || experts == 0 || n == 0 || k == 0 || n % OMEGA_FP8_BLOCK || k % OMEGA_FP8_BLOCK) return OMEGA_GPU_FP8_BAD_ARGS; 345 if (k > OMEGA_FP8_MAX_K) return OMEGA_GPU_FP8_TOO_LARGE; 346 const size_t wbytes = (size_t)experts * n * k, scount = (size_t)experts * (n / OMEGA_FP8_BLOCK) * (k / OMEGA_FP8_BLOCK); 347 size_t first = 0; 348 if (omega_fp8_pack_scan_nan(w, wbytes, &first)) { 349 char e[120]; omega_gpu_fmt(e, sizeof e, "fp8 upload: e4m3 NaN code at byte %zu", first); 350 omega_gpu_session_set_error(e); 351 return OMEGA_GPU_FP8_NAN_WEIGHT; 352 } 353 OmegaGpuFp8Tensor *t = omega_gpu_host_calloc(1, sizeof *t); 354 if (!t) return OMEGA_GPU_FP8_BAD_ARGS; 355 t->experts = experts; t->n = n; t->k = k; 356 int rc = OMEGA_GPU_FP8_OK; 357 omega_gpu_session_lock(); 358 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; } 359 int (*const alloc)(size_t, OmegaGpuMem *) = g.weights_cached ? omega_gpu_session_alloc_cached : omega_gpu_session_alloc; 360 if (!dev_open_locked() || alloc(wbytes, &t->w) != 0 || alloc(scount * 4u, &t->s) != 0) rc = OMEGA_GPU_FP8_CHIP_FAIL; 361 /* Fill and sync under the lock (omega#456): t is not shared yet, but its CPU views belong to the open device, and a 362 * device close on another thread between unlock and memcpy would unmap them; sync_to_gpu also reads the open state. 363 * g.no_fold is read under the lock, as its setter now writes it. The cost is holding the lock for one weight copy. */ 364 if (rc == OMEGA_GPU_FP8_OK) { 365 memcpy(t->w.cpu, w, wbytes); 366 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; } 367 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; 368 } 369 omega_gpu_session_unlock(); 370 if (rc != OMEGA_GPU_FP8_OK) { omega_gpu_fp8_tensor_free(t); return rc; } 371 *out = t; 372 return OMEGA_GPU_FP8_OK; 373 } 374 375 void omega_gpu_fp8_tensor_free(OmegaGpuFp8Tensor *t) { 376 if (!t) return; 377 omega_gpu_session_lock(); 378 if (t->w.cpu) omega_gpu_session_free(&t->w); 379 if (t->s.cpu) omega_gpu_session_free(&t->s); 380 omega_gpu_session_unlock(); 381 omega_gpu_host_free(t); 382 } 383 384 /* ---- the GEMV (lock held inside) ---- */ 385 386 /* L310 (2026-10-24): CTAs per group of a grouped launch. A grouped CTA is 32 warps = 1024 threads; CC 12.x holds 1536 387 * resident threads per SM (CUDA Programming Guide, Compute Capabilities, Table 30), so one such CTA fits per SM and the 388 * GB10's 48 SMs (GB10 Superchip Whitepaper WP-12718-001_v02, Table 4) hold one wave of 48 CTAs. The compiled 6 was 389 * measured on 8 groups (48 CTAs = one wave). The 35B launches 9 groups: 54 CTAs run as two waves, the second 6 CTAs wide; 390 * 5 per group (45 = one wave) measured gate_up + down 6.962 -> 5.874 ms per 1-row step 391 * (~/workspace/investigations/2026-10-24-l310-grouped-ctas). Env, read once like the other switches: 392 * AIEN_OMEGA_FP8_GROUPED_CTAS=1..256 exact CTAs per group (experiments; wins over the wave rule); 393 * AIEN_OMEGA_FP8_GROUPED_WAVE=1 wave rule: min(GROUPED_CTAS, SM_COUNT / groups), at least 1. 394 * Neither set: the compiled GROUPED_CTAS, unchanged until the merge train has judged the wave arm. */ 395 uint32_t omega_gpu_fp8_grouped_ctas_rule(uint32_t groups, uint32_t forced, int wave) { 396 if (forced >= 1u && forced <= OMEGA_GPU_FP8_MAX_CTAS) return forced; 397 if (!wave || groups == 0u) return OMEGA_GPU_FP8_GROUPED_CTAS; 398 uint32_t one_wave = OMEGA_GPU_SM_COUNT / groups; 399 if (one_wave == 0u) one_wave = 1u; 400 return one_wave < OMEGA_GPU_FP8_GROUPED_CTAS ? one_wave : OMEGA_GPU_FP8_GROUPED_CTAS; 401 } 402 static uint32_t env_u32(const char *name) { /* 0 when unset, empty or not a plain number */ 403 const char *e = getenv(name); uint32_t n = 0; 404 if (!e || !*e) return 0; 405 for (; *e >= '0' && *e <= '9' && n < 1000u; e++) n = n * 10u + (uint32_t)(*e - '0'); 406 return *e ? 0 : n; 407 } 408 uint32_t omega_gpu_fp8_grouped_ctas_auto(uint32_t groups) { 409 static uint32_t forced; static int wave, set; 410 if (!set) { forced = env_u32("AIEN_OMEGA_FP8_GROUPED_CTAS"); const char *w = getenv("AIEN_OMEGA_FP8_GROUPED_WAVE"); wave = w && w[0] == '1' && !w[1]; set = 1; } 411 return omega_gpu_fp8_grouped_ctas_rule(groups, forced, wave); 412 } 413 414 /* dev_tab / dev_x non-zero: the group table / the x' row already sit on the device (written by the router launch). 415 * rec non-NULL (record path, caller holds the session lock): dev_tab, dev_x and dev_y are required, the launch is 416 * appended to rec instead of submitted, and nothing is staged, poisoned or read back (the list's per-launch semaphore 417 * and the caller's end-of-token checks replace the poison check). */ 418 static int gemv_core(const OmegaGpuFp8Tensor *t, uint32_t groups, const uint32_t *ids, const float *x32, const uint16_t *x16, 419 uint32_t x_rows, float *y, OmegaGpuFp8Info *info, uint64_t dev_tab, uint64_t dev_x, 420 OmegaGpuCmdList *rec, uint64_t dev_y) { 421 uint64_t t0 = omega_gpu_session_now_ns(); 422 if (info) memset(info, 0, sizeof *info); 423 if (rec && (!dev_tab || !dev_x || !dev_y)) return OMEGA_GPU_FP8_BAD_ARGS; 424 if (!t || !t->w.cpu || (!ids && !dev_tab) || (!x32 && !x16 && !dev_x) || (!y && !rec) || groups == 0 || (x_rows != 1 && x_rows != groups)) return OMEGA_GPU_FP8_BAD_ARGS; 425 if (groups > OMEGA_GPU_FP8_MAX_GROUPS) return OMEGA_GPU_FP8_TOO_LARGE; 426 if (!dev_tab) for (uint32_t gi = 0; gi < groups; gi++) if (ids[gi] >= t->experts) return OMEGA_GPU_FP8_BAD_ARGS; 427 const uint32_t n = t->n, k = t->k; 428 OmegaFp8GemvSpec spec; spec_for(k, n, groups, &spec); 429 if (n % spec.rows) return OMEGA_GPU_FP8_CODEGEN_FAIL; 430 const uint32_t RG = n / spec.rows; 431 uint32_t grid_x = (RG + spec.warps - 1) / spec.warps; 432 const int variant = variant_now(); 433 uint32_t budget = g.cta_budget_set ? g.cta_budget : (groups > 1u ? (variant == 2 ? OMEGA_GPU_FP8_V2_GROUPED_CTAS : omega_gpu_fp8_grouped_ctas_auto(groups)) : OMEGA_GPU_FP8_MAX_CTAS); 434 /* big-k grid (qwen38/decode-speed): up to OMEGA_GPU_FP8_BIGK_CTAS CTAs, split so every warp gets the same number of row 435 * groups, at least two (measured, runs/sweep5 and sweep7: budget 384 over 256 gave mix +3%, gate_up +2.6%; the n = 5120 436 * shapes run best at 160 CTAs, two row groups per warp: o_proj 186 -> 198 GB/s) */ 437 const int bigk_grid = !g.cta_budget_set && groups == 1u && k > 4096u && omega_gpu_fp8_bigk(); 438 if (bigk_grid) { uint32_t waves = (grid_x + OMEGA_GPU_FP8_BIGK_CTAS - 1u) / OMEGA_GPU_FP8_BIGK_CTAS; if (waves < 2u) waves = 2u; grid_x = (grid_x + waves - 1u) / waves; budget = grid_x; } 439 if (grid_x > budget) grid_x = budget; 440 if (grid_x == 0) grid_x = 1; 441 442 if (!rec) omega_gpu_session_lock(); 443 #define UNLOCK() do { if (!rec) omega_gpu_session_unlock(); } while (0) 444 int rc = OMEGA_GPU_FP8_OK; 445 if (!dev_open_locked()) { UNLOCK(); return OMEGA_GPU_FP8_CHIP_FAIL; } 446 if (!rec) { /* staging buffers: the record path reads and writes only the caller's device buffers */ 447 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; } 448 int src = omega_gpu_session_scratch(&g.x_buf, (size_t)x_rows * k * 4u); 449 if (src == 0) src = omega_gpu_session_scratch(&g.y_buf, (size_t)groups * n * 4u); 450 if (src != 0) { UNLOCK(); return src == OMEGA_GPU_SESSION_SCRATCH_RESERVED ? OMEGA_GPU_FP8_TOO_LARGE : OMEGA_GPU_FP8_CHIP_FAIL; } 451 } 452 Slot *ks = NULL; bool hit = false; 453 g_rec = rec != NULL; 454 rc = kernel_for(&spec, &ks, &hit); 455 g_rec = 0; 456 if (rc != OMEGA_GPU_FP8_OK) { UNLOCK(); return rc; } 457 458 /* group table: {w_va, s_va} per group */ 459 uint64_t *tab = g.tab.cpu; 460 const uint64_t wstride = (uint64_t)n * k, sstride = (uint64_t)(n / OMEGA_FP8_BLOCK) * (k / OMEGA_FP8_BLOCK) * 4u; 461 if (!dev_tab) for (uint32_t gi = 0; gi < groups; gi++) { tab[2 * gi] = t->w.va + ids[gi] * wstride; tab[2 * gi + 1] = t->s.va + ids[gi] * sstride; } 462 /* x' rows, y poisoned */ 463 float *xd = g.x_buf.mem.cpu, *yd = g.y_buf.mem.cpu; 464 for (uint32_t r = 0; r < x_rows && !dev_x; r++) { 465 if (x32) omega_fp8_pack_x_f32(x32 + (size_t)r * k, k, xd + (size_t)r * k); 466 else omega_fp8_pack_x_bf16(x16 + (size_t)r * k, k, xd + (size_t)r * k); 467 } 468 if (!rec) { 469 for (size_t i = 0; i < (size_t)groups * n; i++) yd[i] = POISON_F32; 470 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; 471 } 472 473 uint32_t args[OMEGA_FP8_ARGS_WORDS]; 474 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; 475 args[0] = (uint32_t)tab_va; args[1] = (uint32_t)(tab_va >> 32); 476 args[2] = (uint32_t)x_va; args[3] = (uint32_t)(x_va >> 32); 477 args[4] = (uint32_t)y_va; args[5] = (uint32_t)(y_va >> 32); 478 args[6] = RG; args[7] = x_rows == 1 ? 0u : k * 4u; args[8] = n * 4u; args[9] = grid_x * spec.warps; 479 if (info) { 480 info->experts = t->experts; info->n = n; info->k = k; info->groups = groups; 481 info->lane_bytes = spec.lane_bytes; info->rows_per_warp = spec.rows; info->warps_per_cta = spec.warps; info->depth = spec.depth; 482 info->grid_x = grid_x; info->grid_y = groups; info->steps = omega_fp8_gemv_steps(&spec); 483 info->weight_bytes = (uint64_t)groups * wstride; info->scale_bytes = (uint64_t)groups * sstride; 484 info->kernel_cache_hit = hit; 485 (void)omega_gpu_fmt(info->target_chip, sizeof info->target_chip, "%s", "NVIDIA DGX Spark (Grace Blackwell GB10)"); 486 info->sm_architecture = 121; 487 } 488 if (rc == OMEGA_GPU_FP8_OK) { 489 OmegaGpuLaunch L = { 490 .code_va = ks->code.va, .gpr_count = gpr_declared(ks->kernel.gpr_count) + g.gpr_pad, 491 .threads_x = 32u * spec.warps, .threads_y = 1, .grid_x = grid_x, .grid_y = groups, 492 .num_elements = (size_t)groups * n, 493 .args = args, .n_args = OMEGA_FP8_ARGS_WORDS, .timeout_ms = MARKER_TIMEOUT_MS, 494 .shared_bytes = shared_for(variant, &spec), 495 }; 496 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; } 497 else if (rec) { if (omega_gpu_cmdlist_add(rec, &L) < 0) rc = OMEGA_GPU_FP8_CHIP_FAIL; } 498 else if (omega_gpu_session_launch(&L, info ? &info->elapsed_ns : NULL, info ? &info->completion_marker : NULL) != 0) rc = OMEGA_GPU_FP8_CHIP_FAIL; 499 } 500 if (rc == OMEGA_GPU_FP8_OK && !rec && omega_gpu_session_sync_to_cpu(&g.y_buf.mem) != 0) rc = OMEGA_GPU_FP8_CHIP_FAIL; 501 if (rc == OMEGA_GPU_FP8_OK && !rec) { 502 size_t unchanged = 0; 503 for (size_t i = 0; i < (size_t)groups * n; i++) if (yd[i] == POISON_F32) unchanged++; 504 if (unchanged) { omega_gpu_session_set_error("output poison survived"); rc = OMEGA_GPU_FP8_CHIP_FAIL; } 505 else memcpy(y, yd, (size_t)groups * n * sizeof *y); 506 } 507 UNLOCK(); 508 if (info) info->call_ns = omega_gpu_session_now_ns() - t0; 509 return rc; 510 } 511 #undef UNLOCK 512 513 int omega_gpu_fp8_gemv_f32(const OmegaGpuFp8Tensor *t, uint32_t expert, const float *x, float *y, OmegaGpuFp8Info *info) { 514 return gemv_core(t, 1, &expert, x, NULL, 1, y, info, 0, 0, NULL, 0); 515 } 516 int omega_gpu_fp8_gemv_bf16(const OmegaGpuFp8Tensor *t, uint32_t expert, const uint16_t *x, float *y, OmegaGpuFp8Info *info) { 517 return gemv_core(t, 1, &expert, NULL, x, 1, y, info, 0, 0, NULL, 0); 518 } 519 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) { 520 return gemv_core(t, groups, ids, x, NULL, x_rows, y, info, 0, 0, NULL, 0); 521 } 522 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) { 523 if (!tab_va || !x_va) return OMEGA_GPU_FP8_BAD_ARGS; 524 return gemv_core(t, groups, NULL, NULL, NULL, 1, y, info, tab_va, x_va, NULL, 0); 525 } 526 int omega_gpu_fp8_tensor_entry(const OmegaGpuFp8Tensor *t, uint32_t expert, uint64_t *w_va, uint64_t *s_va) { 527 if (!t || !t->w.cpu || expert >= t->experts || !w_va || !s_va) return OMEGA_GPU_FP8_BAD_ARGS; 528 *w_va = t->w.va + (uint64_t)expert * t->n * t->k; 529 *s_va = t->s.va + (uint64_t)expert * (t->n / OMEGA_FP8_BLOCK) * (t->k / OMEGA_FP8_BLOCK) * 4u; 530 return OMEGA_GPU_FP8_OK; 531 } 532 533 /* Record path (OWN-30B W-L2a decode graph): the launch of omega_gpu_fp8_gemv_grouped_dev appended to cl, every input 534 * and the output in device memory. Caller holds omega_gpu_session_lock(). */ 535 int omega_gpu_fp8_gemv_grouped_record(OmegaGpuCmdList *cl, const OmegaGpuFp8Tensor *t, uint32_t groups, uint64_t tab_va, 536 uint64_t x_va, uint32_t x_rows, uint64_t y_va) { 537 if (!cl) return OMEGA_GPU_FP8_BAD_ARGS; 538 return gemv_core(t, groups, NULL, NULL, NULL, x_rows, NULL, NULL, tab_va, x_va, cl, y_va); 539 } 540 541 /* ---- weight cache policy (OWN-30B L2 kernel-time cut) ---- */ 542 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(); } 543 int omega_gpu_fp8_weight_cache(void) { return g.weights_cached; } 544 545 /* ---- host quantiser (qwen38/decode-speed: the FP8 lm_head) ---- */ 546 547 /* f32 -> e4m3 (bias 7, max 448, no infinities), round to nearest, ties to even; |v| >= 448 (and NaN) saturate to 448: 548 * 0x7F (NaN) is never produced. Pure integer/float bit work, no libm. */ 549 uint8_t omega_gpu_fp8_e4m3_rne(float v) { 550 uint32_t u; memcpy(&u, &v, 4); 551 const uint8_t s = (uint8_t)((u >> 24) & 0x80u); 552 u &= 0x7FFFFFFFu; 553 float a; memcpy(&a, &u, 4); 554 if (!(a < 448.0f)) return (uint8_t)(s | 0x7Eu); 555 const int ex = (int)(u >> 23) - 127; 556 if (ex < -6) { /* e4m3 subnormals: units of 2^-9 (code 8 is the smallest normal, 2^-6) */ 557 const float x = a * 512.0f; uint32_t xi = (uint32_t)x; const float fr = x - (float)xi; 558 if (fr > 0.5f || (fr == 0.5f && (xi & 1u))) xi++; 559 return (uint8_t)(s | xi); 560 } 561 const uint32_t m = u & 0x7FFFFFu, rb = (m >> 19) & 1u, st = m & 0x7FFFFu; 562 uint32_t q = m >> 20, e = (uint32_t)(ex + 7); 563 if (rb && (st || (q & 1u))) q++; 564 if (q == 8u) { q = 0; e++; } 565 uint32_t code = (e << 3) | q; 566 if (code > 0x7Eu) code = 0x7Eu; 567 return (uint8_t)(s | code); 568 } 569 570 int omega_gpu_fp8_quantize_bf16(uint32_t n, uint32_t k, const uint16_t *w, uint8_t *w8, uint16_t *scale_inv_bf16) { 571 if (!w || !w8 || !scale_inv_bf16 || n == 0 || k == 0 || n % OMEGA_FP8_BLOCK || k % OMEGA_FP8_BLOCK) return OMEGA_GPU_FP8_BAD_ARGS; 572 const uint32_t kb = k / OMEGA_FP8_BLOCK; 573 for (uint32_t bn = 0; bn < n / OMEGA_FP8_BLOCK; bn++) 574 for (uint32_t bk = 0; bk < kb; bk++) { 575 float amax = 0.0f; 576 for (uint32_t r = 0; r < OMEGA_FP8_BLOCK; r++) { 577 const uint16_t *row = w + ((size_t)bn * OMEGA_FP8_BLOCK + r) * k + (size_t)bk * OMEGA_FP8_BLOCK; 578 for (uint32_t c = 0; c < OMEGA_FP8_BLOCK; c++) { 579 uint32_t b = (uint32_t)(row[c] & 0x7FFFu) << 16; float f; memcpy(&f, &b, 4); 580 if (!(f <= 3.0e38f)) return OMEGA_GPU_FP8_BAD_ARGS; /* inf / NaN weight */ 581 if (f > amax) amax = f; 582 } 583 } 584 /* scale = amax / 448 rounded UP to bf16, so every |w / scale| <= 448 before rounding; an all-zero block gets 1 */ 585 uint16_t sb = 0x3F80u; 586 if (amax > 0.0f) { 587 const float q = amax / 448.0f; uint32_t b; memcpy(&b, &q, 4); 588 sb = (uint16_t)((b >> 16) + ((b & 0xFFFFu) ? 1u : 0u)); 589 } 590 scale_inv_bf16[(size_t)bn * kb + bk] = sb; 591 uint32_t sbits = (uint32_t)sb << 16; float sc; memcpy(&sc, &sbits, 4); 592 for (uint32_t r = 0; r < OMEGA_FP8_BLOCK; r++) { 593 const size_t at = ((size_t)bn * OMEGA_FP8_BLOCK + r) * k + (size_t)bk * OMEGA_FP8_BLOCK; 594 for (uint32_t c = 0; c < OMEGA_FP8_BLOCK; c++) { 595 uint32_t b = (uint32_t)w[at + c] << 16; float f; memcpy(&f, &b, 4); 596 w8[at + c] = omega_gpu_fp8_e4m3_rne(f / sc); 597 } 598 } 599 } 600 return OMEGA_GPU_FP8_OK; 601 } 602 603 /* ---- slot pool support (omega_gpu_fp8_internal.h, Expert Ribbon omega#533) ---- */ 604 int omega_gpu_fp8_tensor_alloc_uncached(uint32_t experts, uint32_t n, uint32_t k, OmegaGpuFp8Tensor **out) { 605 if (out) *out = NULL; 606 if (!out || experts == 0 || n == 0 || k == 0 || n % OMEGA_FP8_BLOCK || k % OMEGA_FP8_BLOCK) return OMEGA_GPU_FP8_BAD_ARGS; 607 if (k > OMEGA_FP8_MAX_K) return OMEGA_GPU_FP8_TOO_LARGE; 608 const size_t wbytes = (size_t)experts * n * k, scount = (size_t)experts * (n / OMEGA_FP8_BLOCK) * (k / OMEGA_FP8_BLOCK); 609 OmegaGpuFp8Tensor *t = omega_gpu_host_calloc(1, sizeof *t); 610 if (!t) return OMEGA_GPU_FP8_BAD_ARGS; 611 t->experts = experts; t->n = n; t->k = k; 612 int rc = OMEGA_GPU_FP8_OK; 613 omega_gpu_session_lock(); 614 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; 615 omega_gpu_session_unlock(); 616 if (rc == OMEGA_GPU_FP8_OK) { 617 memset(t->w.cpu, 0, wbytes); 618 memset(t->s.cpu, 0, scount * 4u); 619 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; 620 } 621 if (rc != OMEGA_GPU_FP8_OK) { omega_gpu_fp8_tensor_free(t); return rc; } 622 *out = t; 623 return OMEGA_GPU_FP8_OK; 624 } 625 int omega_gpu_fp8_tensor_cpu_view(const OmegaGpuFp8Tensor *t, uint32_t expert, uint8_t **w, float **s) { 626 if (!t || !t->w.cpu || !t->s.cpu || expert >= t->experts || !w || !s) return OMEGA_GPU_FP8_BAD_ARGS; 627 *w = (uint8_t *)t->w.cpu + (size_t)expert * t->n * t->k; 628 *s = (float *)t->s.cpu + (size_t)expert * (t->n / OMEGA_FP8_BLOCK) * (t->k / OMEGA_FP8_BLOCK); 629 return OMEGA_GPU_FP8_OK; 630 } 631 int omega_gpu_fp8_tensor_sync_to_gpu(const OmegaGpuFp8Tensor *t) { 632 if (!t) return -1; 633 return (omega_gpu_session_sync_to_gpu(&t->w) != 0 || omega_gpu_session_sync_to_gpu(&t->s) != 0) ? -1 : 0; 634 }