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.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | #ifndef OMEGA_BLACKWELL_MATMUL_H |
| 3 | #define OMEGA_BLACKWELL_MATMUL_H |
| 4 | |
| 5 | #include <stdbool.h> |
| 6 | #include <stddef.h> |
| 7 | #include <stdint.h> |
| 8 | #include "omega_bw_policy.h" |
| 9 | |
| 10 | #define OMEGA_BW_MATMUL_MAX_M 8192 |
| 11 | #define OMEGA_BW_MATMUL_MAX_K 16384 |
| 12 | #define OMEGA_BW_MATMUL_MAX_N 262144 |
| 13 | |
| 14 | /* Precision modes for Milestone 18 tensor matrix multiplication */ |
| 15 | typedef enum { |
| 16 | OMEGA_MATMUL_PRECISION_INT32 = 0, /* Exact integer arithmetic mod 2^32 */ |
| 17 | OMEGA_MATMUL_PRECISION_FP16 = 1, /* FP16 inputs with FP32 accumulator */ |
| 18 | OMEGA_MATMUL_PRECISION_BF16 = 2 /* BF16 inputs with FP32 accumulator */ |
| 19 | } OmegaMatMulPrecision; |
| 20 | |
| 21 | /* Semantic matrix multiplication operation descriptor in G_S */ |
| 22 | typedef struct { |
| 23 | uint32_t m; |
| 24 | uint32_t k; |
| 25 | uint32_t n; |
| 26 | OmegaMatMulPrecision precision; |
| 27 | uint8_t spec_id[32]; |
| 28 | } OmegaMatMulSpec; |
| 29 | |
| 30 | /* Dynamically synthesized sm_121 machine code artifact */ |
| 31 | typedef struct { |
| 32 | uint8_t *code; |
| 33 | size_t code_size; |
| 34 | size_t insn_count; |
| 35 | uint8_t code_digest[32]; |
| 36 | uint32_t gpr_count; |
| 37 | uint32_t uniform_gpr_count; |
| 38 | } OmegaBlackwellKernel; |
| 39 | |
| 40 | /* Four-component realization identity binding */ |
| 41 | typedef struct { |
| 42 | uint8_t spec_id[32]; |
| 43 | uint8_t machine_id[32]; |
| 44 | uint8_t code_digest[32]; |
| 45 | uint32_t sm_arch; |
| 46 | uint8_t realization_id[32]; |
| 47 | } OmegaBlackwellRealizationIdentity; |
| 48 | |
| 49 | /* Initializes semantic MatMul specification and computes canonical spec_id */ |
| 50 | int omega_matmul_spec_init(OmegaMatMulSpec *spec, uint32_t m, uint32_t k, uint32_t n, OmegaMatMulPrecision precision); |
| 51 | |
| 52 | /* Mathematical CPU reference oracles for verification (zero tolerance). |
| 53 | * Row-major positive m/k/n; c must not overlap either input (inputs may overlap). |
| 54 | * Hosted checked views return -1 for an aliased output before touching memory. |
| 55 | * The freestanding C exception is pending omega-bridge#72. |
| 56 | */ |
| 57 | 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); |
| 58 | |
| 59 | /* Mathematical CPU reference oracle for FP16 and BF16 tensor matrix multiplication */ |
| 60 | 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); |
| 61 | 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); |
| 62 | |
| 63 | /* Format conversion functions */ |
| 64 | float omega_fp16_to_fp32(uint16_t h); |
| 65 | float omega_bf16_to_fp32(uint16_t b); |
| 66 | uint16_t omega_fp32_to_fp16(float f); |
| 67 | uint16_t omega_fp32_to_bf16(float f); |
| 68 | /* Checked import-free Omega round-to-nearest-even conversion. NaNs preserve |
| 69 | * sign/top payload and are quieted; the caller transfers raw binary32 bits. */ |
| 70 | static inline uint16_t omega_fp32_to_bf16_rne(float f) { |
| 71 | union { float f; uint32_t u; } v = {f}; |
| 72 | return (uint16_t)omega_bw_value(3, v.u, 0, 0, 0, 0); |
| 73 | } |
| 74 | |
| 75 | /* Dynamic sm_121 code generator: transforms G_S semantic spec into machine code */ |
| 76 | int omega_blackwell_codegen_matmul(const OmegaMatMulSpec *spec, OmegaBlackwellKernel *kernel); |
| 77 | |
| 78 | /* Frees resources allocated for dynamically synthesized kernel */ |
| 79 | void omega_blackwell_kernel_free(OmegaBlackwellKernel *kernel); |
| 80 | |
| 81 | /* Binds realization identity from spec, machine, and dynamically synthesized code */ |
| 82 | int omega_blackwell_bind_matmul_realization(const OmegaMatMulSpec *spec, const OmegaBlackwellKernel *kernel, OmegaBlackwellRealizationIdentity *id); |
| 83 | |
| 84 | #endif /* OMEGA_BLACKWELL_MATMUL_H */ |