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_blackwell_codegen_qmma.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_blackwell_codegen_qmma.c: QMMA.16832.F32.E4M3.E4M3 placeholder, patch, probe and peak kernels (W-L1G). See the header. */ |
| 18 | #include "omega_blackwell_codegen_qmma.h" |
| 19 | #include "omega_bw_em.h" |
| 20 | #include "omega_bw_em_q.h" |
| 21 | #include "omega_bw_om_ir.h" |
| 22 | #include "omega_gpu_fmt.h" |
| 23 | #include "omega_native.h" |
| 24 | |
| 25 | static uint32_t rd32q(const uint8_t *p) { return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24; } |
| 26 | static void wr32q(uint8_t *p, uint32_t v) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); p[2] = (uint8_t)(v >> 16); p[3] = (uint8_t)(v >> 24); } |
| 27 | |
| 28 | int omega_qmma_is_mark(const BlackwellIRInsn *in) { return in && in->op == BW_IR_HMMA_BF16 && in->imm == OMEGA_QMMA_MARK; } |
| 29 | |
| 30 | int omega_qmma_patch_code(const BlackwellIRProgram *prog, uint8_t *code, size_t len) { |
| 31 | if (!prog || !code || len < prog->count * 16u) return -1; |
| 32 | int n = 0; |
| 33 | for (size_t i = 0; i < prog->count; i++) { |
| 34 | if (prog->insns[i].op == BW_IR_FMNMX_MAX && prog->insns[i].imm == OMEGA_QMMA_MARK_F2E4) { |
| 35 | uint8_t *w = code + i * 16u; uint32_t w0 = rd32q(w), w1 = rd32q(w + 4), w2 = rd32q(w + 8); |
| 36 | if ((w0 & 0xffffu) != 0x7209u || (w1 & 0xffffff00u) != 0 || w2 != 0x07800000u) return -1; |
| 37 | wr32q(w, (w0 & 0xffff0000u) | 0x723eu); wr32q(w + 8, 0x048070ffu); n++; continue; |
| 38 | } |
| 39 | if (!omega_qmma_is_mark(&prog->insns[i])) continue; |
| 40 | uint8_t *w = code + i * 16u; |
| 41 | uint32_t w0 = rd32q(w), w1 = rd32q(w + 4), w2 = rd32q(w + 8); |
| 42 | if ((w0 & 0xffffu) != 0x723cu || (w1 & 0xffffff00u) != 0 || (w2 & 0xffffff00u) != 0x00041800u) return -1; |
| 43 | wr32q(w, (w0 & 0xffff0000u) | 0x727au); |
| 44 | wr32q(w + 8, (w2 & 0xffu) | 0x00002c00u); |
| 45 | n++; |
| 46 | } |
| 47 | if (n > 0) { size_t b = omega_bw_program_code_bytes(prog); omega_kernel_dump(code, b && b <= len ? b : prog->count * 16u); } /* the patched body is a different kernel */ |
| 48 | return n; |
| 49 | } |
| 50 | |
| 51 | int omega_qmma_patch_selftest(char *msg, size_t msgn) { |
| 52 | static BlackwellIRProgram p; static uint8_t buf[64 * 16]; size_t len = 0; |
| 53 | omega_bw_ir_init(&p); |
| 54 | Em em; memset(&em, 0, sizeof em); em.p = &p; Em *e = &em; |
| 55 | int udesc = UV64(e), py = V64(e), a = V128(e), b = V64(e), c = V128(e); |
| 56 | if (e->err) { omega_gpu_fmt(msg, msgn, "vregs"); return -1; } |
| 57 | ldcu64(e, udesc, 0x358); ldc64(e, py, 0x380); |
| 58 | ldg_wide(e, BW_IR_LDG_E_128, a, py, 0, udesc, 1); ldg_wide(e, BW_IR_LDG_E_64, b, py, 16, udesc, 0); ldg_wide(e, BW_IR_LDG_E_128, c, py, 32, udesc, 0); |
| 59 | qmma(e, c, a, b, c, 15); nop(e, 15); |
| 60 | { int f = V(e); if (e->err) { omega_gpu_fmt(msg, msgn, "vregs"); return -1; } f2e4(e, f, c, 0, c, 1); stg(e, py, 64, f, udesc); } |
| 61 | hmma(e, c, a, b, c, 15); nop(e, 15); |
| 62 | for (unsigned q = 0; q < 4; q++) stg(e, py, 4u * q, c, udesc); |
| 63 | tail(e); |
| 64 | if (e->err || omega_bw_regalloc_solve(&p) != 0 || omega_bw_encode_program(&p, buf, sizeof buf, &len) != 0) { omega_gpu_fmt(msg, msgn, "build/encode failed"); return -1; } |
| 65 | int np = omega_qmma_patch_code(&p, buf, len); |
| 66 | if (np != 2) { omega_gpu_fmt(msg, msgn, "patched %d placeholders, expected 2", np); return -1; } |
| 67 | int seenq = 0, seenh = 0, seenf = 0; |
| 68 | for (size_t i = 0; i < p.count; i++) { |
| 69 | if (p.insns[i].op == BW_IR_FMNMX_MAX) { const uint8_t *w = buf + i * 16u; uint32_t w0 = rd32q(w), w1 = rd32q(w + 4), w2 = rd32q(w + 8); |
| 70 | if ((w0 & 0xffffu) != 0x723eu || (w1 & 0xffffff00u) != 0 || w2 != 0x048070ffu) { omega_gpu_fmt(msg, msgn, "F2FP.E4M3 word %zu: %08x %08x %08x", i, w0, w1, w2); return -1; } seenf++; continue; } |
| 71 | if (p.insns[i].op != BW_IR_HMMA_BF16) continue; |
| 72 | const uint8_t *w = buf + i * 16u; uint32_t w0 = rd32q(w), w1 = rd32q(w + 4), w2 = rd32q(w + 8); |
| 73 | if (omega_qmma_is_mark(&p.insns[i])) { |
| 74 | /* oracle 0x0414727a 0x0000000c 0x00002c14 with registers masked */ |
| 75 | if ((w0 & 0xffffu) != 0x727au || (w1 & 0xffffff00u) != 0 || (w2 & 0xffffff00u) != 0x00002c00u) { omega_gpu_fmt(msg, msgn, "QMMA word %zu: %08x %08x %08x", i, w0, w1, w2); return -1; } |
| 76 | seenq++; |
| 77 | } else { |
| 78 | if ((w0 & 0xffffu) != 0x723cu || (w2 & 0xffffff00u) != 0x00041800u) { omega_gpu_fmt(msg, msgn, "HMMA word %zu changed: %08x %08x", i, w0, w2); return -1; } |
| 79 | seenh++; |
| 80 | } |
| 81 | } |
| 82 | if (seenq != 1 || seenh != 1 || seenf != 1) { omega_gpu_fmt(msg, msgn, "saw %d QMMA, %d HMMA", seenq, seenh); return -1; } |
| 83 | omega_gpu_fmt(msg, msgn, "QMMA.16832.F32.E4M3.E4M3 and F2FP.SATFINITE.E4M3.F32.PACK_AB_MERGE_C match the oracle bits, HMMA untouched"); |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | int omega_blackwell_codegen_qmma_probe_prog(int mutant, uint32_t wait_nops, BlackwellIRProgram *prog) { |
| 88 | if (!prog) return -1; |
| 89 | omega_bw_ir_init(prog); |
| 90 | uint64_t sp[8] = { (uint64_t)(uint32_t)mutant, wait_nops, 0, 0, 0, 0, 0, 0 }; |
| 91 | uint64_t ins[8192], meta[512], rc = 1; |
| 92 | memset(ins, 0, sizeof ins); |
| 93 | memset(meta, 0, sizeof meta); |
| 94 | if (omega2_cg_qmma_probe(&rc, sp, 8u, ins, 8192u, meta, 512u) != 0 || rc != 0) return -1; |
| 95 | return omega_bw_ir_from_om(ins, meta, prog); |
| 96 | } |
| 97 | |
| 98 | int omega_blackwell_codegen_qmma_peak_prog(uint32_t iters, uint32_t chains, uint32_t stall, uint32_t threads, BlackwellIRProgram *prog) { |
| 99 | if (!prog) return -1; |
| 100 | omega_bw_ir_init(prog); |
| 101 | uint64_t sp[8] = { iters, chains, stall, threads, 0, 0, 0, 0 }; |
| 102 | uint64_t ins[8192], meta[512], rc = 1; |
| 103 | memset(ins, 0, sizeof ins); |
| 104 | memset(meta, 0, sizeof meta); |
| 105 | if (omega2_cg_qmma_peak(&rc, sp, 8u, ins, 8192u, meta, 512u) != 0 || rc != 0) return -1; |
| 106 | return omega_bw_ir_from_om(ins, meta, prog); |
| 107 | } |
| 108 | |
| 109 | int omega_blackwell_codegen_f2e4_probe_prog(BlackwellIRProgram *prog) { |
| 110 | if (!prog) return -1; |
| 111 | omega_bw_ir_init(prog); |
| 112 | uint64_t ins[8192], meta[512], rc = 1; |
| 113 | memset(ins, 0, sizeof ins); |
| 114 | memset(meta, 0, sizeof meta); |
| 115 | if (omega2_cg_f2e4_probe(&rc, ins, 8192u, meta, 512u) != 0 || rc != 0) return -1; |
| 116 | return omega_bw_ir_from_om(ins, meta, prog); |
| 117 | } |