interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / src / omega_blackwell_matmul.c

205 lines · 6883 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#include "omega_blackwell_matmul.h"
18#include "sha256.h"
19#ifndef OMEGA_GPU_NO_POSIX
20#include <stdlib.h>
21#endif
22#include <string.h>
23
24int omega_matmul_spec_init(OmegaMatMulSpec *spec, uint32_t m, uint32_t k, uint32_t n, OmegaMatMulPrecision precision) {
25 if (!spec || m == 0 || k == 0 || n == 0) return -1;
26 if (m > OMEGA_BW_MATMUL_MAX_M || k > OMEGA_BW_MATMUL_MAX_K || n > OMEGA_BW_MATMUL_MAX_N) return -1;
27
28 spec->m = m;
29 spec->k = k;
30 spec->n = n;
31 spec->precision = precision;
32
33 /* Canonical spec_id computed over (m, k, n, precision) */
34 uint8_t spec_buf[16];
35 spec_buf[0] = (uint8_t)(m & 0xff);
36 spec_buf[1] = (uint8_t)((m >> 8) & 0xff);
37 spec_buf[2] = (uint8_t)((m >> 16) & 0xff);
38 spec_buf[3] = (uint8_t)((m >> 24) & 0xff);
39
40 spec_buf[4] = (uint8_t)(k & 0xff);
41 spec_buf[5] = (uint8_t)((k >> 8) & 0xff);
42 spec_buf[6] = (uint8_t)((k >> 16) & 0xff);
43 spec_buf[7] = (uint8_t)((k >> 24) & 0xff);
44
45 spec_buf[8] = (uint8_t)(n & 0xff);
46 spec_buf[9] = (uint8_t)((n >> 8) & 0xff);
47 spec_buf[10] = (uint8_t)((n >> 16) & 0xff);
48 spec_buf[11] = (uint8_t)((n >> 24) & 0xff);
49
50 spec_buf[12] = (uint8_t)(precision & 0xff);
51 spec_buf[13] = 0;
52 spec_buf[14] = 0;
53 spec_buf[15] = 0;
54
55 sha256_hash(spec_buf, sizeof(spec_buf), spec->spec_id);
56 return 0;
57}
58
59int 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) {
60 if (!a || !b || !c || m == 0 || k == 0 || n == 0) return -1;
61
62 for (uint32_t i = 0; i < m; i++) {
63 for (uint32_t j = 0; j < n; j++) {
64 uint32_t acc = 0;
65 for (uint32_t p = 0; p < k; p++) {
66 acc += a[i * k + p] * b[p * n + j];
67 }
68 c[i * n + j] = acc;
69 }
70 }
71 return 0;
72}
73
74
75float omega_fp16_to_fp32(uint16_t h) {
76 uint32_t sign = (uint32_t)(h & 0x8000) << 16;
77 uint32_t exp = (uint32_t)(h & 0x7c00) >> 10;
78 uint32_t mant = (uint32_t)(h & 0x03ff);
79 uint32_t val;
80 if (exp == 0) {
81 if (mant == 0) {
82 val = sign;
83 } else {
84 exp = 1;
85 while ((mant & 0x0400) == 0) {
86 mant <<= 1;
87 exp--;
88 }
89 mant &= 0x03ff;
90 val = sign | ((exp + (127 - 15)) << 23) | (mant << 13);
91 }
92 } else if (exp == 31) {
93 val = sign | 0x7f800000 | (mant << 13);
94 } else {
95 val = sign | ((exp + (127 - 15)) << 23) | (mant << 13);
96 }
97 float f;
98 memcpy(&f, &val, 4);
99 return f;
100}
101
102float omega_bf16_to_fp32(uint16_t b) {
103 uint32_t val = (uint32_t)b << 16;
104 float f;
105 memcpy(&f, &val, 4);
106 return f;
107}
108
109uint16_t omega_fp32_to_fp16(float f) {
110 _Float16 h = (_Float16)f;
111 uint16_t u;
112 memcpy(&u, &h, 2);
113 return u;
114}
115
116uint16_t omega_fp32_to_bf16(float f) {
117#ifndef OMEGA_GPU_NO_POSIX
118 __bf16 b = (__bf16)f;
119 uint16_t u;
120 memcpy(&u, &b, 2);
121 return u;
122#else
123 /* Freestanding (cut A.2): the __bf16 cast calls libgcc __truncsfbf2. Same rounding in integer form
124 * (omega_fp32_to_bf16_rne); the hosted cast and it agree on every one of the 2^32 inputs
125 * (make test-codegen-freestanding, bf16 sweep). */
126 return omega_fp32_to_bf16_rne(f);
127#endif
128}
129
130int 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
144int 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#ifndef OMEGA_GPU_NO_POSIX /* frees what the hosted allocating generators malloc (omega_blackwell_codegen.h, cut A.2) */
158void omega_blackwell_kernel_free(OmegaBlackwellKernel *kernel) {
159 if (!kernel) return;
160 if (kernel->code) {
161 free(kernel->code);
162 kernel->code = NULL;
163 }
164 kernel->code_size = 0;
165 kernel->insn_count = 0;
166}
167#endif
168
169int omega_blackwell_bind_matmul_realization(const OmegaMatMulSpec *spec, const OmegaBlackwellKernel *kernel, OmegaBlackwellRealizationIdentity *id) {
170 if (!spec || !kernel || !id) return -1;
171
172 memcpy(id->spec_id, spec->spec_id, 32);
173 /* NVIDIA_DGX_SPARK_GB10_SM121 canonical machine descriptor digest */
174 const char *machine_str = "NVIDIA_DGX_SPARK_GB10_SM121";
175 sha256_hash((const uint8_t *)machine_str, strlen(machine_str), id->machine_id);
176 memcpy(id->code_digest, kernel->code_digest, 32);
177 id->sm_arch = 121;
178
179 /* Realization ID = SHA-256(spec_id || machine_id || code_digest || sm_arch) */
180 uint8_t bound[100];
181 memcpy(bound + 0, id->spec_id, 32);
182 memcpy(bound + 32, id->machine_id, 32);
183 memcpy(bound + 64, id->code_digest, 32);
184 bound[96] = (uint8_t)(id->sm_arch & 0xff);
185 bound[97] = (uint8_t)((id->sm_arch >> 8) & 0xff);
186 bound[98] = (uint8_t)((id->sm_arch >> 16) & 0xff);
187 bound[99] = (uint8_t)((id->sm_arch >> 24) & 0xff);
188
189 sha256_hash(bound, sizeof(bound), id->realization_id);
190 return 0;
191}
192
193#include "omega_blackwell_codegen.h"
194
195#ifndef OMEGA_GPU_NO_POSIX
196int omega_blackwell_codegen_matmul(const OmegaMatMulSpec *spec, OmegaBlackwellKernel *kernel) {
197 if (!spec || !kernel) return -1;
198 if (spec->precision == OMEGA_MATMUL_PRECISION_INT32) {
199 return omega_blackwell_codegen_matmul_i32(spec, kernel);
200 } else if (spec->precision == OMEGA_MATMUL_PRECISION_FP16 || spec->precision == OMEGA_MATMUL_PRECISION_BF16) {
201 return omega_blackwell_codegen_matmul_tensor(spec, kernel);
202 }
203 return -1;
204}
205#endif