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_qmd.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 | #include "omega_blackwell_qmd.h" |
| 18 | #include "omega_native.h" |
| 19 | #include <string.h> |
| 20 | |
| 21 | /* |
| 22 | * Driver constant buffer 0 base template (224 words, 896 bytes). |
| 23 | * Empirically captured from physical Blackwell GB10 compute channel bring-up. |
| 24 | */ |
| 25 | const uint32_t omega_bw_cbank_driver_template[OMEGA_BW_CBANK_DRIVER_WORDS] = { |
| 26 | 0x26420000, 0x00000003, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 27 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 28 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 29 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 30 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 31 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 32 | 0x94fe5d00, 0xe811c39b, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 33 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x000fffff, 0x00000000, |
| 34 | 0x20000000, 0x00000003, 0x22000000, 0x00000003, 0x24280000, 0x00000003, 0x24010000, 0x00000003, |
| 35 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 36 | 0x00000001, 0x00000000, 0x0400c000, 0x00000002, 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 37 | 0x00000000, 0x00000000, 0x00000000, 0x00000400, 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 38 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 39 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x04ba32c8, 0x00000000, 0x00000000, 0x00000000, |
| 40 | 0x00000000, 0x00000000, 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000120, 0x00000000, |
| 41 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x25b66900, 0x00000003, |
| 42 | 0x00000001, 0x00000001, 0x00000001, 0x00000000, 0x3f800000, 0x3f800000, 0x3f800000, 0x00000400, |
| 43 | 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 44 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x04ba32c8, 0x00000000, 0x00000000, 0x00000000, |
| 45 | 0x00000000, 0x00000000, 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000120, 0x00000000, |
| 46 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x25b66900, 0x00000003, |
| 47 | 0x00000001, 0x00000001, 0x00000001, 0x00000000, 0x3f800000, 0x3f800000, 0x3f800000, 0x00000400, |
| 48 | 0x00000001, 0x00000001, 0x00000001, 0x00000001, 0x00000030, 0x00000000, 0x00000000, 0x01000000, |
| 49 | 0x00000000, 0x0000e9c0, 0x00000000, 0x00000000, 0x00000000, 0x0000e9c0, 0x01000000, 0x0000e9bf, |
| 50 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 51 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 52 | 0x00000000, 0x00000000, 0x24280380, 0x00000003, 0x2428039c, 0x00000003, 0x00000000, 0x00000000, |
| 53 | 0x00000040, 0x00000001, 0x00000001, 0x00000000, 0x00000001, 0x00000001, 0x00000001, 0x00fffdc0, |
| 54 | }; |
| 55 | |
| 56 | int omega_blackwell_build_qmd0(uint32_t *qmd0_words, uint64_t qmd0_va, uint64_t qmd1_va) { |
| 57 | if (!qmd0_words) return -1; |
| 58 | (void)qmd0_va; |
| 59 | uint64_t res = 1; |
| 60 | if (omega2_bw_qmd0(&res, qmd0_words, OMEGA_BW_QMD_WORDS, qmd1_va) != OMEGA_OK || res != 0) return -1; |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | int omega_blackwell_build_qmd1(uint32_t *qmd1_words, const OmegaBlackwellQmdConfig *cfg) { |
| 65 | if (!qmd1_words || !cfg) return -1; |
| 66 | const uint64_t m[16] = { |
| 67 | cfg->code_va, cfg->cbank_va, cfg->scratch_va, cfg->sem_va, cfg->qmd0_va, |
| 68 | cfg->num_elements, cfg->threads_per_block, cfg->grid_width, |
| 69 | cfg->threads_x, cfg->threads_y, cfg->grid_x, cfg->grid_y, |
| 70 | cfg->gpr_count, cfg->shared_bytes, 0, 0 |
| 71 | }; |
| 72 | uint64_t res = 1; |
| 73 | if (omega2_bw_qmd1(&res, m, 16, qmd1_words, OMEGA_BW_QMD_WORDS) != OMEGA_OK || res != 0) return -1; |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | int omega_blackwell_build_cbank_driver(uint32_t *cbank_words, uint64_t cbank_va) { |
| 78 | if (!cbank_words) return -1; |
| 79 | memcpy(cbank_words, omega_bw_cbank_driver_template, sizeof(omega_bw_cbank_driver_template)); |
| 80 | |
| 81 | /* Patch driver template addresses with actual cbank_va */ |
| 82 | cbank_words[68] = (uint32_t)(cbank_va >> 32); |
| 83 | cbank_words[69] = (uint32_t)cbank_va; |
| 84 | |
| 85 | cbank_words[210] = (uint32_t)((cbank_va + 0x380) >> 32); |
| 86 | cbank_words[211] = (uint32_t)(cbank_va + 0x380); |
| 87 | cbank_words[212] = (uint32_t)((cbank_va + 0x39c) >> 32); |
| 88 | cbank_words[213] = (uint32_t)(cbank_va + 0x39c); |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | int omega_blackwell_build_cbank_args(uint32_t *args_words, uint64_t a_va, uint64_t b_va, |
| 94 | uint64_t c_va, uint32_t n) { |
| 95 | if (!args_words) return -1; |
| 96 | |
| 97 | args_words[0] = (uint32_t)a_va; |
| 98 | args_words[1] = (uint32_t)(a_va >> 32); |
| 99 | args_words[2] = (uint32_t)b_va; |
| 100 | args_words[3] = (uint32_t)(b_va >> 32); |
| 101 | args_words[4] = (uint32_t)c_va; |
| 102 | args_words[5] = (uint32_t)(c_va >> 32); |
| 103 | args_words[6] = n; |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | int omega_blackwell_build_cbank_driver_2d(uint32_t *cbank_words, uint64_t cbank_va, |
| 109 | uint32_t threads_x, uint32_t threads_y, |
| 110 | uint32_t grid_x, uint32_t grid_y) { |
| 111 | if (!cbank_words) return -1; |
| 112 | if (omega_blackwell_build_cbank_driver(cbank_words, cbank_va) != 0) return -1; |
| 113 | if (threads_x > 0) cbank_words[216] = threads_x; |
| 114 | if (threads_y > 0) cbank_words[217] = threads_y; |
| 115 | if (grid_x > 0) cbank_words[220] = grid_x; |
| 116 | if (grid_y > 0) cbank_words[221] = grid_y; |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | int omega_blackwell_build_cbank_args_matmul(uint32_t *args_words, uint64_t a_va, uint64_t b_va, |
| 121 | uint64_t c_va, uint32_t m, uint32_t k, uint32_t n) { |
| 122 | if (!args_words) return -1; |
| 123 | args_words[0] = (uint32_t)a_va; |
| 124 | args_words[1] = (uint32_t)(a_va >> 32); |
| 125 | args_words[2] = (uint32_t)b_va; |
| 126 | args_words[3] = (uint32_t)(b_va >> 32); |
| 127 | args_words[4] = (uint32_t)c_va; |
| 128 | args_words[5] = (uint32_t)(c_va >> 32); |
| 129 | args_words[6] = m; |
| 130 | args_words[7] = k; |
| 131 | args_words[8] = n; |
| 132 | args_words[9] = 0; |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | int omega_blackwell_verify_qmd_invariants(const uint32_t *qmd1_words) { |
| 137 | if (!qmd1_words) return -1; |
| 138 | uint64_t res = 1; |
| 139 | if (omega2_bw_qmd_verify(&res, qmd1_words, OMEGA_BW_QMD_WORDS) != OMEGA_OK || res != 0) return -1; |
| 140 | return 0; |
| 141 | } |