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_blackwell_matmul.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | #include "omega_blackwell_matmul.h" |
| 3 | #include "sha256.h" |
| 4 | #ifndef OMEGA_GPU_NO_POSIX |
| 5 | #include <stdlib.h> |
| 6 | #endif |
| 7 | #include <string.h> |
| 8 | #include "cudapp_native.h" |
| 9 | #ifdef OMEGA_GPU_NO_POSIX |
| 10 | #include "omega_native.h" |
| 11 | #endif |
| 12 | #include "omega_bw_bind.h" |
| 13 | #ifndef BW_BIND_C_PRIMARY |
| 14 | int omega_matmul_spec_init(OmegaMatMulSpec *spec, uint32_t m, uint32_t k, uint32_t n, OmegaMatMulPrecision precision) { |
| 15 | uint8_t w[256] = {0}; |
| 16 | if (!spec) return -1; |
| 17 | if (omega_bw_bind(0, (uint64_t)m | (uint64_t)k << 32, (uint64_t)n | (uint64_t)(uint32_t)precision << 32, w) != 16) |
| 18 | return -1; |
| 19 | spec->m = m; |
| 20 | spec->k = k; |
| 21 | spec->n = n; |
| 22 | spec->precision = precision; |
| 23 | if (omega_bw_id(w, 16, spec->spec_id) != 0) return -1; |
| 24 | return 0; |
| 25 | } |
| 26 | #else |
| 27 | /* Default-OFF C_BIND differential oracle. Both production ABIs bind the spec in Omega |
| 28 | * (NO_POSIX: omega2_cg_bind through src/omega_bw_bind.h). */ |
| 29 | int omega_matmul_spec_init(OmegaMatMulSpec *spec, uint32_t m, uint32_t k, uint32_t n, OmegaMatMulPrecision precision) { |
| 30 | if (!spec || m == 0 || k == 0 || n == 0) return -1; |
| 31 | if (m > OMEGA_BW_MATMUL_MAX_M || k > OMEGA_BW_MATMUL_MAX_K || n > OMEGA_BW_MATMUL_MAX_N) return -1; |
| 32 | |
| 33 | spec->m = m; |
| 34 | spec->k = k; |
| 35 | spec->n = n; |
| 36 | spec->precision = precision; |
| 37 | |
| 38 | /* Canonical spec_id computed over (m, k, n, precision) */ |
| 39 | uint8_t spec_buf[16]; |
| 40 | spec_buf[0] = (uint8_t)(m & 0xff); |
| 41 | spec_buf[1] = (uint8_t)((m >> 8) & 0xff); |
| 42 | spec_buf[2] = (uint8_t)((m >> 16) & 0xff); |
| 43 | spec_buf[3] = (uint8_t)((m >> 24) & 0xff); |
| 44 | |
| 45 | spec_buf[4] = (uint8_t)(k & 0xff); |
| 46 | spec_buf[5] = (uint8_t)((k >> 8) & 0xff); |
| 47 | spec_buf[6] = (uint8_t)((k >> 16) & 0xff); |
| 48 | spec_buf[7] = (uint8_t)((k >> 24) & 0xff); |
| 49 | |
| 50 | spec_buf[8] = (uint8_t)(n & 0xff); |
| 51 | spec_buf[9] = (uint8_t)((n >> 8) & 0xff); |
| 52 | spec_buf[10] = (uint8_t)((n >> 16) & 0xff); |
| 53 | spec_buf[11] = (uint8_t)((n >> 24) & 0xff); |
| 54 | |
| 55 | spec_buf[12] = (uint8_t)(precision & 0xff); |
| 56 | spec_buf[13] = 0; |
| 57 | spec_buf[14] = 0; |
| 58 | spec_buf[15] = 0; |
| 59 | |
| 60 | sha256_hash(spec_buf, sizeof(spec_buf), spec->spec_id); |
| 61 | return 0; |
| 62 | } |
| 63 | #endif /* BW_BIND_C_PRIMARY */ |
| 64 | |
| 65 | #if defined(OMEGA_GPU_NO_POSIX) || (defined(CUDAPP_MIGRATION_PENDING_BW_CPU) && CUDAPP_MIGRATION_PENDING_BW_CPU) |
| 66 | /* Freestanding exception pending omega-bridge#72; otherwise a default-OFF C oracle. */ |
| 67 | int omega_matmul_cpu_oracle_i32(const uint32_t *a, const uint32_t *b, uint32_t *c, uint32_t m, uint32_t k, uint32_t n) { |
| 68 | if (!a || !b || !c || m == 0 || k == 0 || n == 0) return -1; |
| 69 | |
| 70 | for (uint32_t i = 0; i < m; i++) { |
| 71 | for (uint32_t j = 0; j < n; j++) { |
| 72 | uint32_t acc = 0; |
| 73 | for (uint32_t p = 0; p < k; p++) { |
| 74 | acc += a[i * k + p] * b[p * n + j]; |
| 75 | } |
| 76 | c[i * n + j] = acc; |
| 77 | } |
| 78 | } |
| 79 | return 0; |
| 80 | } |
| 81 | #else |
| 82 | int omega_matmul_cpu_oracle_i32(const uint32_t *a, const uint32_t *b, uint32_t *c, uint32_t m, uint32_t k, uint32_t n) { |
| 83 | if (!a || !b || !c) return -1; |
| 84 | if (!omega_bw_value(16, m, k, n, 0, 0)) return -1; |
| 85 | uint64_t result = 0; |
| 86 | uint32_t rc = omega_bw_cpui(&result, a, omega_bw_value(17, m, k, 0, 0, 0), |
| 87 | b, omega_bw_value(17, k, n, 0, 0, 0), |
| 88 | c, omega_bw_value(17, m, n, 0, 0, 0)); |
| 89 | return (int)(int64_t)omega_bw_value(8, rc, result, 0, 0, 0); |
| 90 | } |
| 91 | #endif |
| 92 | |
| 93 | |
| 94 | float omega_fp16_to_fp32(uint16_t h) { |
| 95 | uint32_t val = (uint32_t)omega_bw_value(1, h, 0, 0, 0, 0); |
| 96 | float f; |
| 97 | memcpy(&f, &val, 4); |
| 98 | return f; |
| 99 | } |
| 100 | |
| 101 | float omega_bf16_to_fp32(uint16_t b) { |
| 102 | uint32_t val = (uint32_t)omega_bw_value(2, b, 0, 0, 0, 0); |
| 103 | float f; |
| 104 | memcpy(&f, &val, 4); |
| 105 | return f; |
| 106 | } |
| 107 | |
| 108 | uint16_t omega_fp32_to_fp16(float f) { |
| 109 | uint32_t bits; |
| 110 | uint64_t out; |
| 111 | memcpy(&bits, &f, sizeof(bits)); |
| 112 | if (omega_native_f16(&out, bits) != OMEGA_OK) __builtin_trap(); |
| 113 | return (uint16_t)out; |
| 114 | } |
| 115 | |
| 116 | uint16_t omega_fp32_to_bf16(float f) { |
| 117 | #if defined(OMEGA_GPU_NO_POSIX) && !defined(CUDAPP_MIGRATION_PENDING_C_NO_POSIX) |
| 118 | uint32_t bits; |
| 119 | uint64_t out; |
| 120 | memcpy(&bits, &f, sizeof(bits)); |
| 121 | if (omega2_no_bf16(&out, bits) != OMEGA_OK) __builtin_trap(); |
| 122 | return (uint16_t)out; |
| 123 | #else |
| 124 | return omega_fp32_to_bf16_rne(f); |
| 125 | #endif |
| 126 | } |
| 127 | |
| 128 | #if defined(OMEGA_GPU_NO_POSIX) || (defined(CUDAPP_MIGRATION_PENDING_BW_CPU) && CUDAPP_MIGRATION_PENDING_BW_CPU) |
| 129 | /* Freestanding exception pending omega-bridge#72; otherwise default-OFF C oracles. */ |
| 130 | int omega_matmul_cpu_oracle_f16(const uint16_t *a, const uint16_t *b, float *c, uint32_t m, uint32_t k, uint32_t n) { |
| 131 | if (!a || !b || !c || m == 0 || k == 0 || n == 0) return -1; |
| 132 | for (uint32_t i = 0; i < m; i++) { |
| 133 | for (uint32_t j = 0; j < n; j++) { |
| 134 | float sum = 0.0f; |
| 135 | for (uint32_t p = 0; p < k; p++) { |
| 136 | sum += omega_fp16_to_fp32(a[i * k + p]) * omega_fp16_to_fp32(b[p * n + j]); |
| 137 | } |
| 138 | c[i * n + j] = sum; |
| 139 | } |
| 140 | } |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | int omega_matmul_cpu_oracle_bf16(const uint16_t *a, const uint16_t *b, float *c, uint32_t m, uint32_t k, uint32_t n) { |
| 145 | if (!a || !b || !c || m == 0 || k == 0 || n == 0) return -1; |
| 146 | for (uint32_t i = 0; i < m; i++) { |
| 147 | for (uint32_t j = 0; j < n; j++) { |
| 148 | float sum = 0.0f; |
| 149 | for (uint32_t p = 0; p < k; p++) { |
| 150 | sum += omega_bf16_to_fp32(a[i * k + p]) * omega_bf16_to_fp32(b[p * n + j]); |
| 151 | } |
| 152 | c[i * n + j] = sum; |
| 153 | } |
| 154 | } |
| 155 | return 0; |
| 156 | } |
| 157 | #else |
| 158 | int omega_matmul_cpu_oracle_f16(const uint16_t *a, const uint16_t *b, float *c, uint32_t m, uint32_t k, uint32_t n) { |
| 159 | if (!a || !b || !c) return -1; |
| 160 | if (!omega_bw_value(16, m, k, n, 0, 0)) return -1; |
| 161 | uint64_t result = 0; |
| 162 | uint32_t rc = omega_bw_cpuf16(&result, a, omega_bw_value(17, m, k, 0, 0, 0), |
| 163 | b, omega_bw_value(17, k, n, 0, 0, 0), |
| 164 | c, omega_bw_value(17, m, n, 0, 0, 0)); |
| 165 | return (int)(int64_t)omega_bw_value(8, rc, result, 0, 0, 0); |
| 166 | } |
| 167 | int omega_matmul_cpu_oracle_bf16(const uint16_t *a, const uint16_t *b, float *c, uint32_t m, uint32_t k, uint32_t n) { |
| 168 | if (!a || !b || !c) return -1; |
| 169 | if (!omega_bw_value(16, m, k, n, 0, 0)) return -1; |
| 170 | uint64_t result = 0; |
| 171 | uint32_t rc = omega_bw_cpubf16(&result, a, omega_bw_value(17, m, k, 0, 0, 0), |
| 172 | b, omega_bw_value(17, k, n, 0, 0, 0), |
| 173 | c, omega_bw_value(17, m, n, 0, 0, 0)); |
| 174 | return (int)(int64_t)omega_bw_value(8, rc, result, 0, 0, 0); |
| 175 | } |
| 176 | #endif |
| 177 | #ifndef OMEGA_GPU_NO_POSIX /* frees what the hosted allocating generators malloc (omega_blackwell_codegen.h, cut A.2) */ |
| 178 | void omega_blackwell_kernel_free(OmegaBlackwellKernel *kernel) { |
| 179 | if (!kernel) return; |
| 180 | if (kernel->code) { |
| 181 | free(kernel->code); |
| 182 | kernel->code = NULL; |
| 183 | } |
| 184 | kernel->code_size = 0; |
| 185 | kernel->insn_count = 0; |
| 186 | } |
| 187 | #endif |
| 188 | |
| 189 | #ifndef BW_BIND_C_PRIMARY |
| 190 | int omega_blackwell_bind_matmul_realization(const OmegaMatMulSpec *spec, const OmegaBlackwellKernel *kernel, OmegaBlackwellRealizationIdentity *id) { |
| 191 | uint8_t w[256] = {0}; |
| 192 | if (!spec || !kernel || !id) return -1; |
| 193 | if (omega_bw_bind(2, 0, 0, w) != 27) return -1; |
| 194 | int64_t arch = omega_bw_bind(9, 0, 0, w); |
| 195 | if (arch < 0) return -1; |
| 196 | memcpy(id->spec_id, spec->spec_id, 32); |
| 197 | if (omega_bw_id(w, 27, id->machine_id) != 0) return -1; |
| 198 | memcpy(id->code_digest, kernel->code_digest, 32); |
| 199 | id->sm_arch = (uint32_t)arch; |
| 200 | memcpy(w + 128, id->spec_id, 32); |
| 201 | memcpy(w + 160, id->machine_id, 32); |
| 202 | memcpy(w + 192, id->code_digest, 32); |
| 203 | if (omega_bw_bind(1, id->sm_arch, 0, w) != 100) return -1; |
| 204 | if (omega_bw_id(w, 100, id->realization_id) != 0) return -1; |
| 205 | return 0; |
| 206 | } |
| 207 | #else |
| 208 | int omega_blackwell_bind_matmul_realization(const OmegaMatMulSpec *spec, const OmegaBlackwellKernel *kernel, OmegaBlackwellRealizationIdentity *id) { |
| 209 | if (!spec || !kernel || !id) return -1; |
| 210 | |
| 211 | memcpy(id->spec_id, spec->spec_id, 32); |
| 212 | /* NVIDIA_DGX_SPARK_GB10_SM121 canonical machine descriptor digest */ |
| 213 | const char *machine_str = "NVIDIA_DGX_SPARK_GB10_SM121"; |
| 214 | sha256_hash((const uint8_t *)machine_str, strlen(machine_str), id->machine_id); |
| 215 | memcpy(id->code_digest, kernel->code_digest, 32); |
| 216 | id->sm_arch = 121; |
| 217 | |
| 218 | /* Realization ID = SHA-256(spec_id || machine_id || code_digest || sm_arch) */ |
| 219 | uint8_t bound[100]; |
| 220 | memcpy(bound + 0, id->spec_id, 32); |
| 221 | memcpy(bound + 32, id->machine_id, 32); |
| 222 | memcpy(bound + 64, id->code_digest, 32); |
| 223 | bound[96] = (uint8_t)(id->sm_arch & 0xff); |
| 224 | bound[97] = (uint8_t)((id->sm_arch >> 8) & 0xff); |
| 225 | bound[98] = (uint8_t)((id->sm_arch >> 16) & 0xff); |
| 226 | bound[99] = (uint8_t)((id->sm_arch >> 24) & 0xff); |
| 227 | |
| 228 | sha256_hash(bound, sizeof(bound), id->realization_id); |
| 229 | return 0; |
| 230 | } |
| 231 | #endif /* BW_BIND_C_PRIMARY */ |
| 232 | |
| 233 | #include "omega_blackwell_codegen.h" |
| 234 | |
| 235 | #ifndef OMEGA_GPU_NO_POSIX |
| 236 | int omega_blackwell_codegen_matmul(const OmegaMatMulSpec *spec, OmegaBlackwellKernel *kernel) { |
| 237 | if (!spec || !kernel) return -1; |
| 238 | #ifndef BW_BIND_C_PRIMARY |
| 239 | uint8_t w[256] = {0}; |
| 240 | int64_t g = omega_bw_bind(3, (uint32_t)spec->precision, 0, w); |
| 241 | if (g == 0) return omega_blackwell_codegen_matmul_i32(spec, kernel); |
| 242 | if (g == 1) return omega_blackwell_codegen_matmul_tensor(spec, kernel); |
| 243 | return -1; |
| 244 | #else |
| 245 | if (spec->precision == OMEGA_MATMUL_PRECISION_INT32) { |
| 246 | return omega_blackwell_codegen_matmul_i32(spec, kernel); |
| 247 | } else if (spec->precision == OMEGA_MATMUL_PRECISION_FP16 || spec->precision == OMEGA_MATMUL_PRECISION_BF16) { |
| 248 | return omega_blackwell_codegen_matmul_tensor(spec, kernel); |
| 249 | } |
| 250 | return -1; |
| 251 | #endif |
| 252 | } |
| 253 | #endif |