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
omega-mv / src / omega_gpu_pfgemm.c
| 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_pfgemm.c: device-resident tensor-core FP8 grouped GEMM v2 (OWN-30B T3b). See omega_gpu_prefill.h. */ |
| 18 | #include "omega_gpu_prefill.h" |
| 19 | #include "omega_blackwell_codegen_tc2.h" |
| 20 | #include "omega_gpu_fp8_api.h" |
| 21 | #include "omega_gpu_cmdlist.h" |
| 22 | #include "omega_gpu_fmt.h" |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #define KIND_TC2 2u |
| 27 | static struct { uint32_t rt, warps, aqn; int mutant; } g; |
| 28 | void omega_gpu_pf_gemm_set_shape(uint32_t rt, uint32_t warps, uint32_t aqn) { g.rt = rt; g.warps = warps; g.aqn = aqn; } |
| 29 | void omega_gpu_pf_gemm_test_set_mutant(int m) { g.mutant = m; } |
| 30 | static uint32_t RT(void) { return g.rt ? g.rt : 8u; } |
| 31 | uint32_t omega_gpu_pf_gemm_bm(void) { return 16u * RT(); } |
| 32 | |
| 33 | static uint32_t perm4(uint32_t q) { static const uint32_t o[4] = { 0, 2, 1, 3 }; return (q & ~3u) | o[q & 3u]; } |
| 34 | |
| 35 | int omega_gpu_pf_weights_host_form(uint32_t E, uint32_t n, uint32_t k, const uint8_t *w, const uint16_t *sinv, uint32_t layout, uint8_t *pw, float *ratio) { |
| 36 | if (!E || !n || !k || n % 128u || k % 128u || !w || !sinv || !pw || !ratio || layout > OMEGA_GPU_PF_SWIGLU) return OMEGA_GPU_PF_BAD_ARGS; |
| 37 | if (layout == OMEGA_GPU_PF_SWIGLU && n % 256u) return OMEGA_GPU_PF_BAD_ARGS; |
| 38 | const uint32_t KB = k / 128u, NB = n / 128u, I = n / 2u; |
| 39 | float *sf = malloc((size_t)NB * KB * 4u); |
| 40 | if (!sf) return OMEGA_GPU_PF_TOO_LARGE; |
| 41 | int rc = OMEGA_GPU_PF_OK; |
| 42 | for (uint32_t e = 0; e < E && rc == OMEGA_GPU_PF_OK; e++) { |
| 43 | if (omega_fp8_pack_scales(sinv + (size_t)e * NB * KB, (size_t)NB * KB, sf, 0) != 0) { rc = OMEGA_GPU_PF_BAD_ARGS; break; } |
| 44 | for (uint32_t p = 0; p < n; p++) { |
| 45 | uint32_t src = p; |
| 46 | if (layout == OMEGA_GPU_PF_SWIGLU) { uint32_t G = p / 16u, h = p % 16u, i = perm4(8u * G + h % 8u); src = h < 8u ? i : I + i; } |
| 47 | memcpy(pw + ((size_t)e * n + p) * k, w + ((size_t)e * n + src) * k, k); |
| 48 | if (p % 8u == 0) { |
| 49 | /* pair layout: [e][p / 16][kb 0..KB][p / 8 % 2] (the kernel loads both column tiles of a warp with one LDG.64) */ |
| 50 | float *rt = ratio + ((size_t)e * (n / 16u) + p / 16u) * (KB + 1u) * 2u + (p / 8u) % 2u; |
| 51 | const float *s = sf + (size_t)(src / 128u) * KB; |
| 52 | rt[0] = 1.0f; |
| 53 | for (uint32_t kb = 1; kb < KB; kb++) rt[2u * kb] = s[kb - 1] / s[kb]; |
| 54 | rt[2u * KB] = s[KB - 1]; |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | free(sf); |
| 59 | return rc; |
| 60 | } |
| 61 | |
| 62 | int omega_gpu_pf_weights_upload(uint32_t E, uint32_t n, uint32_t k, const uint8_t *w, const uint16_t *sinv, uint32_t layout, OmegaPfWeights *out) { |
| 63 | if (!out) return OMEGA_GPU_PF_BAD_ARGS; |
| 64 | memset(out, 0, sizeof *out); |
| 65 | const size_t wb = (size_t)E * n * k, rb = (size_t)E * (n / 8u) * (k / 128u + 1u) * 4u; |
| 66 | uint8_t *pw = malloc(wb); float *ratio = malloc(rb + 16u); |
| 67 | int rc = (!pw || !ratio) ? OMEGA_GPU_PF_TOO_LARGE : omega_gpu_pf_weights_host_form(E, n, k, w, sinv, layout, pw, ratio); |
| 68 | const size_t rb16 = (rb + 15u) & ~(size_t)15u; |
| 69 | if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_alloc_dev(wb, &out->w); |
| 70 | if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_alloc_dev(rb16, &out->r); |
| 71 | if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_upload(&out->w, 0, pw, wb & ~(size_t)15u); |
| 72 | if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_upload(&out->r, 0, ratio, rb16); |
| 73 | free(pw); free(ratio); |
| 74 | if (rc != OMEGA_GPU_PF_OK) { omega_gpu_pf_free(&out->w); omega_gpu_pf_free(&out->r); memset(out, 0, sizeof *out); return rc; } |
| 75 | out->experts = E; out->n = n; out->k = k; out->layout = layout; |
| 76 | return OMEGA_GPU_PF_OK; |
| 77 | } |
| 78 | void omega_gpu_pf_weights_free(OmegaPfWeights *w) { if (!w) return; omega_gpu_pf_free(&w->w); omega_gpu_pf_free(&w->r); memset(w, 0, sizeof *w); } |
| 79 | |
| 80 | typedef struct { uint64_t w_va, r_va; uint32_t row0, cnt; uint64_t pad; } Tile; |
| 81 | _Static_assert(sizeof(Tile) == OMEGA_TC2_TILE_BYTES, "tile entry"); |
| 82 | uint32_t omega_gpu_pf_gemm_tiles(const OmegaPfWeights *w, const uint32_t *counts, void *tab, uint32_t max) { |
| 83 | if (!w || !counts || !tab) return 0; |
| 84 | const uint32_t bm = omega_gpu_pf_gemm_bm(), KB = w->k / 128u; |
| 85 | Tile *t = tab; uint32_t nt = 0, row = 0; |
| 86 | for (uint32_t e = 0; e < w->experts; e++) { |
| 87 | for (uint32_t r = 0; r < counts[e]; r += bm) { |
| 88 | if (nt >= max) return 0; |
| 89 | t[nt].w_va = w->w.va + (uint64_t)e * w->n * w->k; |
| 90 | t[nt].r_va = w->r.va + (uint64_t)e * (w->n / 8u) * (KB + 1u) * 4u; |
| 91 | t[nt].row0 = row + r; t[nt].cnt = counts[e] - r < bm ? counts[e] - r : bm; t[nt].pad = 0; nt++; |
| 92 | } |
| 93 | row += counts[e]; |
| 94 | } |
| 95 | return nt; |
| 96 | } |
| 97 | |
| 98 | static int gen_tc2(const OmegaPfKey *key, void *prog) { OmegaTc2Spec s; memcpy(&s, key->words, sizeof s); return omega_blackwell_codegen_tc2_prog(&s, prog); } |
| 99 | |
| 100 | int omega_gpu_pf_gemm(const OmegaPfWeights *w, uint64_t tiles_va, uint32_t ntiles, uint64_t rowidx_va, uint64_t x_va, uint64_t y_va, uint64_t *ns) { |
| 101 | if (!w || !w->w.va || !tiles_va || !x_va || !y_va) return OMEGA_GPU_PF_BAD_ARGS; |
| 102 | if (ntiles == 0) return OMEGA_GPU_PF_OK; |
| 103 | if (ntiles > 65535u) return OMEGA_GPU_PF_TOO_LARGE; |
| 104 | OmegaTc2Spec s; memset(&s, 0, sizeof s); |
| 105 | s.k = w->k; s.rt = RT(); s.ct = 2; s.warps = g.warps ? g.warps : 16u; s.aqn = g.aqn ? g.aqn : 1u; |
| 106 | s.epi = w->layout == OMEGA_GPU_PF_SWIGLU ? OMEGA_TC2_EPI_SWIGLU : OMEGA_TC2_EPI_F32; s.direct = rowidx_va == 0; s.n = w->n; s.mutant = g.mutant; |
| 107 | if (!omega_tc2_spec_ok(&s)) return OMEGA_GPU_PF_BAD_ARGS; |
| 108 | const uint32_t span = omega_tc2_span(&s); |
| 109 | if (w->n % span) return OMEGA_GPU_PF_BAD_ARGS; |
| 110 | OmegaPfKey key; memset(&key, 0, sizeof key); key.kind = KIND_TC2; memcpy(key.words, &s, sizeof s); |
| 111 | _Static_assert(sizeof(OmegaTc2Spec) <= sizeof key.words, "spec fits the key"); |
| 112 | uint32_t args[OMEGA_TC2_ARGS_WORDS]; |
| 113 | const uint64_t va[4] = { tiles_va, rowidx_va, x_va, y_va }; |
| 114 | for (int i = 0; i < 4; i++) { args[2 * i] = (uint32_t)va[i]; args[2 * i + 1] = (uint32_t)(va[i] >> 32); } |
| 115 | args[8] = w->n; |
| 116 | omega_gpu_session_lock(); |
| 117 | int rc = omega_gpu_pf_open_locked(); |
| 118 | OmegaPfKernel k; |
| 119 | if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_kernel_locked(&key, gen_tc2, &k); |
| 120 | if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_launch_locked(&k, 32u * s.warps, w->n / span, ntiles, omega_tc2_shared_bytes(&s), args, OMEGA_TC2_ARGS_WORDS, ns); |
| 121 | omega_gpu_session_unlock(); |
| 122 | return rc; |
| 123 | } |
| 124 | |
| 125 | /* ---- native FP8 path (W-L1G): activation quantiser (omega_blackwell_codegen_qg.h) ---- */ |
| 126 | #include "omega_blackwell_codegen_qg.h" |
| 127 | #define KIND_PQ 3u |
| 128 | static int pq_mutant; |
| 129 | void omega_gpu_pf_quant_test_set_mutant(int m) { pq_mutant = m; } |
| 130 | static int gen_pq(const OmegaPfKey *key, void *prog) { OmegaPqSpec s; memcpy(&s, key->words, sizeof s); return omega_blackwell_codegen_pq_prog(&s, prog); } |
| 131 | /* cl non-NULL: record (caller holds the lock) instead of launch */ |
| 132 | static int pq_core(OmegaGpuCmdList *cl, uint32_t k, uint32_t in_type, uint32_t rows, uint64_t x_va, uint64_t q_va, uint64_t s_va, uint64_t *ns) { |
| 133 | OmegaPqSpec s; memset(&s, 0, sizeof s); s.k = k; s.in_type = in_type; s.mutant = pq_mutant; |
| 134 | const uint32_t kb = k / 128u; |
| 135 | if (k % 128u == 0 && kb > 32u) for (s.groups = 32u; kb % s.groups; s.groups--) {} /* wide row: split over ctaid.y */ |
| 136 | const uint32_t g = s.groups ? s.groups : kb, gy = s.groups ? kb / s.groups : 1u; |
| 137 | if (!omega_pq_spec_ok(&s) || !x_va || !q_va || !s_va) return OMEGA_GPU_PF_BAD_ARGS; |
| 138 | if (rows == 0) return OMEGA_GPU_PF_OK; |
| 139 | OmegaPfKey key; memset(&key, 0, sizeof key); key.kind = KIND_PQ; memcpy(key.words, &s, sizeof s); |
| 140 | const uint32_t args[OMEGA_PQ_ARGS_WORDS] = { (uint32_t)x_va, (uint32_t)(x_va >> 32), (uint32_t)q_va, (uint32_t)(q_va >> 32), (uint32_t)s_va, (uint32_t)(s_va >> 32) }; |
| 141 | if (!cl) omega_gpu_session_lock(); |
| 142 | int rc = omega_gpu_pf_open_locked(); |
| 143 | OmegaPfKernel kern; |
| 144 | if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_kernel_locked(&key, gen_pq, &kern); |
| 145 | if (rc == OMEGA_GPU_PF_OK) |
| 146 | rc = cl ? omega_gpu_pf_record_locked(cl, &kern, 32u * g, rows, gy, 0, args, OMEGA_PQ_ARGS_WORDS) |
| 147 | : omega_gpu_pf_launch_locked(&kern, 32u * g, rows, gy, 0, args, OMEGA_PQ_ARGS_WORDS, ns); |
| 148 | if (!cl) omega_gpu_session_unlock(); |
| 149 | return rc; |
| 150 | } |
| 151 | int omega_gpu_pf_quant(uint32_t k, uint32_t in_type, uint32_t rows, uint64_t x_va, uint64_t q_va, uint64_t s_va, uint64_t *ns) { |
| 152 | return pq_core(NULL, k, in_type, rows, x_va, q_va, s_va, ns); |
| 153 | } |
| 154 | int omega_gpu_pf_quant_record(OmegaGpuCmdList *cl, uint32_t k, uint32_t in_type, uint32_t rows, uint64_t x_va, uint64_t q_va, uint64_t s_va) { |
| 155 | if (!cl) return OMEGA_GPU_PF_BAD_ARGS; |
| 156 | return pq_core(cl, k, in_type, rows, x_va, q_va, s_va, NULL); |
| 157 | } |
| 158 | |
| 159 | /* ---- native FP8 path (W-L1G): grouped GEMM on QMMA (omega_blackwell_codegen_qg.h) ---- */ |
| 160 | #define KIND_QG 4u |
| 161 | static struct { uint32_t rt, warps, wait, set; int mutant; } q; |
| 162 | void omega_gpu_pf_qgemm_set_shape(uint32_t rt, uint32_t warps, uint32_t wait) { q.rt = rt; q.warps = warps; q.wait = wait; q.set = rt || warps || wait; } |
| 163 | void omega_gpu_pf_qgemm_test_set_mutant(int m) { q.mutant = m; } |
| 164 | static uint32_t QRT(void) { return q.rt ? q.rt : 4u; } |
| 165 | static uint32_t QWARPS(void) { return q.warps ? q.warps : 8u; } |
| 166 | static uint32_t QWAIT(void) { return q.set ? q.wait : 45u; } /* 45 cycles: chip-safe QMMA read distance (evidence OWN30B-L1G-QMMA-PFGEMM-c742aa13) */ |
| 167 | uint32_t omega_gpu_pf_qgemm_bm(void) { return 16u * QRT(); } |
| 168 | |
| 169 | /* tile table from per-expert base addresses (w_va[e], s_va[e]: GEMV-tensor layout, wherever it lives) */ |
| 170 | uint32_t omega_gpu_pf_qgemm_tiles_va(uint32_t E, const uint64_t *w_va, const uint64_t *s_va, const uint32_t *counts, void *tab, uint32_t max) { |
| 171 | if (!E || !w_va || !s_va || !counts || !tab) return 0; |
| 172 | const uint32_t bm = omega_gpu_pf_qgemm_bm(); |
| 173 | Tile *tl = tab; uint32_t nt = 0, row = 0; |
| 174 | for (uint32_t e = 0; e < E; e++) { |
| 175 | for (uint32_t r = 0; r < counts[e]; r += bm) { |
| 176 | if (nt >= max) return 0; |
| 177 | tl[nt].w_va = w_va[e]; tl[nt].r_va = s_va[e]; |
| 178 | tl[nt].row0 = row + r; tl[nt].cnt = counts[e] - r < bm ? counts[e] - r : bm; tl[nt].pad = 0; nt++; |
| 179 | } |
| 180 | row += counts[e]; |
| 181 | } |
| 182 | return nt; |
| 183 | } |
| 184 | /* the same straight from an FP8 GEMV tensor (omega_gpu_fp8_tensor_entry, read in place, no copy) */ |
| 185 | uint32_t omega_gpu_pf_qgemm_tiles(const OmegaGpuFp8Tensor *t, const uint32_t *counts, void *tab, uint32_t max) { |
| 186 | if (!t || !counts || !tab) return 0; |
| 187 | uint32_t E = 0, n = 0, k = 0; omega_gpu_fp8_tensor_shape(t, &E, &n, &k); |
| 188 | const uint32_t bm = omega_gpu_pf_qgemm_bm(); |
| 189 | Tile *tl = tab; uint32_t nt = 0, row = 0; |
| 190 | for (uint32_t e = 0; e < E; e++) { |
| 191 | if (!counts[e]) continue; |
| 192 | uint64_t w_va = 0, s_va = 0; |
| 193 | if (omega_gpu_fp8_tensor_entry(t, e, &w_va, &s_va) != 0) return 0; |
| 194 | for (uint32_t r = 0; r < counts[e]; r += bm) { |
| 195 | if (nt >= max) return 0; |
| 196 | tl[nt].w_va = w_va; tl[nt].r_va = s_va; |
| 197 | tl[nt].row0 = row + r; tl[nt].cnt = counts[e] - r < bm ? counts[e] - r : bm; tl[nt].pad = 0; nt++; |
| 198 | } |
| 199 | row += counts[e]; |
| 200 | } |
| 201 | return nt; |
| 202 | } |
| 203 | |
| 204 | static int gen_qg(const OmegaPfKey *key, void *prog) { OmegaQgSpec s; memcpy(&s, key->words, sizeof s); return omega_blackwell_codegen_qgemm_prog(&s, prog); } |
| 205 | |
| 206 | static int qg_core(OmegaGpuCmdList *cl, uint32_t n, uint32_t k, uint32_t layout, uint64_t tiles_va, uint32_t ntiles, uint64_t rowidx_va, uint64_t xq_va, |
| 207 | uint64_t xs_va, uint64_t y_va, uint64_t *ns) { |
| 208 | if (!tiles_va || !xq_va || !xs_va || !y_va) return OMEGA_GPU_PF_BAD_ARGS; |
| 209 | if (layout != OMEGA_GPU_PF_PLAIN && layout != OMEGA_GPU_PF_SWIGLU_NAT) return OMEGA_GPU_PF_BAD_ARGS; |
| 210 | if (ntiles == 0) return OMEGA_GPU_PF_OK; |
| 211 | if (ntiles > 65535u) return OMEGA_GPU_PF_TOO_LARGE; |
| 212 | OmegaQgSpec s; memset(&s, 0, sizeof s); |
| 213 | s.k = k; s.rt = QRT(); s.warps = QWARPS(); s.wait = QWAIT(); s.n = n; s.mutant = q.mutant; |
| 214 | s.epi = layout == OMEGA_GPU_PF_SWIGLU_NAT ? OMEGA_QG_EPI_SWIGLU : OMEGA_QG_EPI_F32; s.direct = rowidx_va == 0; |
| 215 | if (!omega_qg_spec_ok(&s)) return OMEGA_GPU_PF_BAD_ARGS; |
| 216 | const uint32_t span = omega_qg_span(&s); |
| 217 | OmegaPfKey key; memset(&key, 0, sizeof key); key.kind = KIND_QG; memcpy(key.words, &s, sizeof s); |
| 218 | _Static_assert(sizeof(OmegaQgSpec) <= sizeof key.words, "spec fits the key"); |
| 219 | _Static_assert(sizeof(Tile) == OMEGA_QG_TILE_BYTES, "tile entry"); |
| 220 | uint32_t args[OMEGA_QG_ARGS_WORDS]; |
| 221 | const uint64_t va[5] = { tiles_va, rowidx_va, xq_va, y_va, xs_va }; |
| 222 | for (int i = 0; i < 5; i++) { args[2 * i] = (uint32_t)va[i]; args[2 * i + 1] = (uint32_t)(va[i] >> 32); } |
| 223 | args[10] = n; |
| 224 | if (!cl) omega_gpu_session_lock(); |
| 225 | int rc = omega_gpu_pf_open_locked(); |
| 226 | OmegaPfKernel kern; |
| 227 | if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_kernel_locked(&key, gen_qg, &kern); |
| 228 | if (rc == OMEGA_GPU_PF_OK) |
| 229 | rc = cl ? omega_gpu_pf_record_locked(cl, &kern, 32u * s.warps, n / span, ntiles, omega_qg_shared_bytes(&s), args, OMEGA_QG_ARGS_WORDS) |
| 230 | : omega_gpu_pf_launch_locked(&kern, 32u * s.warps, n / span, ntiles, omega_qg_shared_bytes(&s), args, OMEGA_QG_ARGS_WORDS, ns); |
| 231 | if (!cl) omega_gpu_session_unlock(); |
| 232 | return rc; |
| 233 | } |
| 234 | int omega_gpu_pf_qgemm(uint32_t n, uint32_t k, uint32_t layout, uint64_t tiles_va, uint32_t ntiles, uint64_t rowidx_va, uint64_t xq_va, uint64_t xs_va, uint64_t y_va, uint64_t *ns) { |
| 235 | return qg_core(NULL, n, k, layout, tiles_va, ntiles, rowidx_va, xq_va, xs_va, y_va, ns); |
| 236 | } |
| 237 | int omega_gpu_pf_qgemm_record(OmegaGpuCmdList *cl, uint32_t n, uint32_t k, uint32_t layout, uint64_t tiles_va, uint32_t ntiles, |
| 238 | uint64_t rowidx_va, uint64_t xq_va, uint64_t xs_va, uint64_t y_va) { |
| 239 | if (!cl) return OMEGA_GPU_PF_BAD_ARGS; |
| 240 | return qg_core(cl, n, k, layout, tiles_va, ntiles, rowidx_va, xq_va, xs_va, y_va, NULL); |
| 241 | } |