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_matmul.h
| 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 | #ifndef OMEGA_BLACKWELL_MATMUL_H |
| 18 | #define OMEGA_BLACKWELL_MATMUL_H |
| 19 | |
| 20 | #include <stdbool.h> |
| 21 | #include <stddef.h> |
| 22 | #include <stdint.h> |
| 23 | |
| 24 | #define OMEGA_BW_MATMUL_MAX_M 8192 |
| 25 | #define OMEGA_BW_MATMUL_MAX_K 16384 |
| 26 | #define OMEGA_BW_MATMUL_MAX_N 262144 |
| 27 | |
| 28 | /* Precision modes for Milestone 18 tensor matrix multiplication */ |
| 29 | typedef enum { |
| 30 | OMEGA_MATMUL_PRECISION_INT32 = 0, /* Exact integer arithmetic mod 2^32 */ |
| 31 | OMEGA_MATMUL_PRECISION_FP16 = 1, /* FP16 inputs with FP32 accumulator */ |
| 32 | OMEGA_MATMUL_PRECISION_BF16 = 2 /* BF16 inputs with FP32 accumulator */ |
| 33 | } OmegaMatMulPrecision; |
| 34 | |
| 35 | /* Semantic matrix multiplication operation descriptor in G_S */ |
| 36 | typedef struct { |
| 37 | uint32_t m; |
| 38 | uint32_t k; |
| 39 | uint32_t n; |
| 40 | OmegaMatMulPrecision precision; |
| 41 | uint8_t spec_id[32]; |
| 42 | } OmegaMatMulSpec; |
| 43 | |
| 44 | /* Dynamically synthesized sm_121 machine code artifact */ |
| 45 | typedef struct { |
| 46 | uint8_t *code; |
| 47 | size_t code_size; |
| 48 | size_t insn_count; |
| 49 | uint8_t code_digest[32]; |
| 50 | uint32_t gpr_count; |
| 51 | uint32_t uniform_gpr_count; |
| 52 | } OmegaBlackwellKernel; |
| 53 | |
| 54 | /* Four-component realization identity binding */ |
| 55 | typedef struct { |
| 56 | uint8_t spec_id[32]; |
| 57 | uint8_t machine_id[32]; |
| 58 | uint8_t code_digest[32]; |
| 59 | uint32_t sm_arch; |
| 60 | uint8_t realization_id[32]; |
| 61 | } OmegaBlackwellRealizationIdentity; |
| 62 | |
| 63 | /* Initializes semantic MatMul specification and computes canonical spec_id */ |
| 64 | int omega_matmul_spec_init(OmegaMatMulSpec *spec, uint32_t m, uint32_t k, uint32_t n, OmegaMatMulPrecision precision); |
| 65 | |
| 66 | /* Mathematical CPU reference oracle for verification (zero tolerance) */ |
| 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 | |
| 69 | /* Mathematical CPU reference oracle for FP16 and BF16 tensor matrix multiplication */ |
| 70 | 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); |
| 71 | 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); |
| 72 | |
| 73 | /* Format conversion functions */ |
| 74 | float omega_fp16_to_fp32(uint16_t h); |
| 75 | float omega_bf16_to_fp32(uint16_t b); |
| 76 | uint16_t omega_fp32_to_fp16(float f); |
| 77 | uint16_t omega_fp32_to_bf16(float f); |
| 78 | /* The integer form of omega_fp32_to_bf16, inline for bulk loops: round to nearest even, NaN kept quiet with its |
| 79 | * sign and top payload bits. It equals the hosted __bf16 cast on all 2^32 inputs (the freestanding |
| 80 | * omega_fp32_to_bf16 is this function; make test-codegen-freestanding sweeps it against the cast). The hosted |
| 81 | * cast is a libgcc call per element (about 5 ns on the Spark), so bulk converters use this form. */ |
| 82 | static inline uint16_t omega_fp32_to_bf16_rne(float f) { |
| 83 | union { float f; uint32_t u; } v = {f}; |
| 84 | const uint32_t u = v.u; |
| 85 | if ((u & 0x7f800000u) == 0x7f800000u && (u & 0x007fffffu)) return (uint16_t)((u >> 16) | 0x0040u); |
| 86 | return (uint16_t)((u + 0x7fffu + ((u >> 16) & 1u)) >> 16); |
| 87 | } |
| 88 | |
| 89 | /* Dynamic sm_121 code generator: transforms G_S semantic spec into machine code */ |
| 90 | int omega_blackwell_codegen_matmul(const OmegaMatMulSpec *spec, OmegaBlackwellKernel *kernel); |
| 91 | |
| 92 | /* Frees resources allocated for dynamically synthesized kernel */ |
| 93 | void omega_blackwell_kernel_free(OmegaBlackwellKernel *kernel); |
| 94 | |
| 95 | /* Binds realization identity from spec, machine, and dynamically synthesized code */ |
| 96 | int omega_blackwell_bind_matmul_realization(const OmegaMatMulSpec *spec, const OmegaBlackwellKernel *kernel, OmegaBlackwellRealizationIdentity *id); |
| 97 | |
| 98 | #endif /* OMEGA_BLACKWELL_MATMUL_H */ |