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 / tensor / omega_tensor_gb10.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 | /* |
| 18 | * GB10 realization of M20 OMEGA_TENSOR (docs/tensor/M20_OMEGA_TENSOR.md, |
| 19 | * declared in omega_tensor.h). Every arithmetic step runs on the GB10 through |
| 20 | * an E1 entry point; this file only maps ops, checks, pads and moves data. |
| 21 | * |
| 22 | * elementwise omega_gb10_execute_simt_op (E1 SIMT ops), chunks of at most |
| 23 | * OMEGA_NUMERIC_MAX_COUNT elements. The op map below lists only |
| 24 | * the ops the tensor layer submits AND that have a GB10 E1 |
| 25 | * kernel; every other op is refused with |
| 26 | * OMEGA_NUMERIC_ERR_NOT_ENCODED before a device is opened. |
| 27 | * transc omega_ds_gb10_run (E1 row-10 whole-program kernels, |
| 28 | * src/omega_numeric_divsqrt_gb10.c) for the eleven bounded- |
| 29 | * contract unary ops EXP2 LOG2 SIGMOID TANH SIN COS ERF GELU |
| 30 | * RSQRT EXP LOG, chunks of at most OMEGA_DS_MAX_BATCH elements; |
| 31 | * each kernel is bit-exact with its E1 CPU sequence (E1 chip |
| 32 | * receipts in evidence-out/E1-TRANSC-GB10). Any other unary op |
| 33 | * is refused with OMEGA_NUMERIC_ERR_NOT_ENCODED. |
| 34 | * reduce omega_reduce_gb10 (E1 WP-D), n <= OMEGA_REDUCE_GB10_MAX_N, |
| 35 | * after the declared-order strcmp the CPU seam also makes. |
| 36 | * reduce_rows SUM: every tree level of every row in one pass of E1 |
| 37 | * REDUCE_SUM warp launches (each row padded with -0.0 to whole |
| 38 | * tiles of 32, lane 0 of each warp gathered on the host: data |
| 39 | * movement only). Same patch words and order as |
| 40 | * omega_reduce_gb10 SUM. MAX/MIN/MEAN: omega_reduce_gb10 per row. |
| 41 | * |
| 42 | * There is no CPU fallback: this file never calls omega_numeric_cpu_realize, |
| 43 | * omega_reduce_cpu or any reference tier (mk/tensor.mk checks with nm). |
| 44 | * Matmul is the semantic layer's FMUL_RNE products + the E1 tree, so it is |
| 45 | * bit-exact with the CPU table. A Tensor Core matmul is not provided: MMA |
| 46 | * accumulates with fused, differently ordered additions and would need its |
| 47 | * own declared bounded contract (doc row stays MISSING_IMPLEMENTATION). |
| 48 | */ |
| 49 | #include "omega_tensor.h" |
| 50 | |
| 51 | #include <stdio.h> |
| 52 | #include <stdlib.h> |
| 53 | #include <string.h> |
| 54 | #include <time.h> |
| 55 | |
| 56 | #include "omega_numeric.h" |
| 57 | #include "omega_numeric_divsqrt_gb10.h" |
| 58 | #include "omega_numeric_reduce.h" |
| 59 | |
| 60 | _Static_assert(sizeof(OMEGA_REDUCE_DECLARED_ORDER) == sizeof(OMEGA_TENSOR_REDUCE_DECLARED_ORDER), |
| 61 | "tensor reduce order must equal the E1 reduction order"); |
| 62 | _Static_assert(OMEGA_NUMERIC_MAX_COUNT % 32u == 0, "chunks must be whole warps"); |
| 63 | |
| 64 | /* Ops the semantic layer submits (omega_tensor.c: unary SQRT, BINARY_MAP, |
| 65 | * fma FFMA_V, matmul FMUL, cast conversions) -> E1 GB10 op name. Ops that |
| 66 | * are absent (EXP, LOG: they go through the transc entry below; uniform-c |
| 67 | * FFMA, MUFU seeds, LDS/SHFL, ...) are refused. */ |
| 68 | static const char *const GB10_OP_NAME[OMEGA_NOP_COUNT] = { |
| 69 | [OMEGA_NOP_FADD] = "FADD", [OMEGA_NOP_FSUB] = "FSUB", |
| 70 | [OMEGA_NOP_FMUL] = "FMUL", [OMEGA_NOP_DIV] = "DIV", |
| 71 | [OMEGA_NOP_SQRT] = "SQRT", [OMEGA_NOP_FFMA_V] = "FFMA_V", |
| 72 | [OMEGA_NOP_FMNMX_MIN] = "FMNMX_MIN", [OMEGA_NOP_FMNMX_MAX] = "FMNMX_MAX", |
| 73 | [OMEGA_NOP_FSETP_SEL] = "FSETP_SEL", [OMEGA_NOP_FSETP_LT_SEL] = "FSETP_LT_SEL", |
| 74 | [OMEGA_NOP_FSETP_LE_SEL] = "FSETP_LE_SEL", [OMEGA_NOP_FSETP_GT_SEL] = "FSETP_GT_SEL", |
| 75 | [OMEGA_NOP_FSETP_EQ_SEL] = "FSETP_EQ_SEL", [OMEGA_NOP_FSETP_NE_SEL] = "FSETP_NE_SEL", |
| 76 | [OMEGA_NOP_FSETP_NUM_SEL] = "FSETP_NUM_SEL", [OMEGA_NOP_FSETP_NAN_SEL] = "FSETP_NAN_SEL", |
| 77 | [OMEGA_NOP_FSETP_LTU_SEL] = "FSETP_LTU_SEL", [OMEGA_NOP_FSETP_LEU_SEL] = "FSETP_LEU_SEL", |
| 78 | [OMEGA_NOP_FSETP_GTU_SEL] = "FSETP_GTU_SEL", [OMEGA_NOP_FSETP_GEU_SEL] = "FSETP_GEU_SEL", |
| 79 | [OMEGA_NOP_FSETP_EQU_SEL] = "FSETP_EQU_SEL", [OMEGA_NOP_FSETP_NEU_SEL] = "FSETP_NEU_SEL", |
| 80 | [OMEGA_NOP_F32_TO_F16] = "F32_TO_F16", [OMEGA_NOP_F32_TO_BF16] = "F32_TO_BF16", |
| 81 | [OMEGA_NOP_F16_TO_F32] = "F16_TO_F32", [OMEGA_NOP_BF16_TO_F32] = "BF16_TO_F32", |
| 82 | }; |
| 83 | |
| 84 | const char *omega_tensor_gb10_op_name(OmegaNumericOp op) { |
| 85 | if ((unsigned)op >= OMEGA_NOP_COUNT) return NULL; |
| 86 | return GB10_OP_NAME[op]; |
| 87 | } |
| 88 | |
| 89 | /* Bounded-contract unary ops -> E1 row-10 whole-program kernel. Entries are |
| 90 | * offset by one so an absent op reads 0 (= refused). */ |
| 91 | static const unsigned char GB10_TRANSC_DS1[OMEGA_TU_COUNT] = { |
| 92 | [OMEGA_TU_EXP2] = 1 + OMEGA_DS_EXP2, [OMEGA_TU_LOG2] = 1 + OMEGA_DS_LOG2, |
| 93 | [OMEGA_TU_SIGMOID] = 1 + OMEGA_DS_SIGMOID, [OMEGA_TU_TANH] = 1 + OMEGA_DS_TANH, |
| 94 | [OMEGA_TU_SIN] = 1 + OMEGA_DS_SIN, [OMEGA_TU_COS] = 1 + OMEGA_DS_COS, |
| 95 | [OMEGA_TU_ERF] = 1 + OMEGA_DS_ERF, [OMEGA_TU_GELU] = 1 + OMEGA_DS_GELU, |
| 96 | [OMEGA_TU_RSQRT] = 1 + OMEGA_DS_RSQRT, |
| 97 | [OMEGA_TU_EXP] = 1 + OMEGA_DS_EXP, [OMEGA_TU_LOG] = 1 + OMEGA_DS_LOG, /* MUT:GB10_TRANSC_EXP_LOG_SWAP */ |
| 98 | }; |
| 99 | |
| 100 | int omega_tensor_gb10_transc_kernel(OmegaTensorUnaryOp op) { |
| 101 | if ((unsigned)op >= OMEGA_TU_COUNT || GB10_TRANSC_DS1[op] == 0) return -1; |
| 102 | return GB10_TRANSC_DS1[op] - 1; |
| 103 | } |
| 104 | |
| 105 | /* Launch log (M20 GB10 instrumentation). Every E1 call this table makes gets |
| 106 | * a process-wide index and one GB10_CALL line on stderr: kind, op, count, |
| 107 | * return code and elapsed ms. A failed call keeps its failing return code: |
| 108 | * no retry, no fallback. The E1 executors add a GB10_DEVFAIL line naming the |
| 109 | * device step when the failure is a device error. */ |
| 110 | static unsigned long g_gb10_call_index; |
| 111 | |
| 112 | static double gb10_ms_since(const struct timespec *t0) { |
| 113 | struct timespec t; |
| 114 | timespec_get(&t, TIME_UTC); |
| 115 | return (double)(t.tv_sec - t0->tv_sec) * 1e3 + (double)(t.tv_nsec - t0->tv_nsec) / 1e6; |
| 116 | } |
| 117 | |
| 118 | static int gb10_simt_call(const char *name, const float *a, const float *b, const float *c, float *out, |
| 119 | size_t cnt) { |
| 120 | unsigned long idx = ++g_gb10_call_index; |
| 121 | struct timespec t0; |
| 122 | timespec_get(&t0, TIME_UTC); |
| 123 | int rc = omega_gb10_execute_simt_op(name, a, b, c, out, cnt); |
| 124 | fprintf(stderr, "GB10_CALL idx=%lu kind=simt op=%s count=%zu rc=%d ms=%.3f%s\n", idx, name, cnt, rc, |
| 125 | gb10_ms_since(&t0), rc == OMEGA_NUMERIC_OK ? "" : " FAILED"); |
| 126 | return rc; |
| 127 | } |
| 128 | |
| 129 | static int gb10_reduce_call(OmegaReduceOp r, const float *x, size_t n, float *out) { |
| 130 | unsigned long idx = ++g_gb10_call_index; |
| 131 | struct timespec t0; |
| 132 | timespec_get(&t0, TIME_UTC); |
| 133 | int rc = omega_reduce_gb10(r, x, n, out); |
| 134 | fprintf(stderr, "GB10_CALL idx=%lu kind=reduce op=%d n=%zu inner_launches=%u rc=%d ms=%.3f%s\n", idx, (int)r, |
| 135 | n, omega_reduce_gb10_last_launches(), rc, gb10_ms_since(&t0), rc == OMEGA_NUMERIC_OK ? "" : " FAILED"); |
| 136 | return rc; |
| 137 | } |
| 138 | |
| 139 | static int gb10_ds_call(OmegaDsOp op, const uint32_t *a, uint32_t *out, size_t cnt) { |
| 140 | unsigned long idx = ++g_gb10_call_index; |
| 141 | struct timespec t0; |
| 142 | timespec_get(&t0, TIME_UTC); |
| 143 | int rc = omega_ds_gb10_run(op, a, NULL, out, cnt); |
| 144 | fprintf(stderr, "GB10_CALL idx=%lu kind=transc op=%s count=%zu rc=%d ms=%.3f%s\n", idx, omega_ds_op_name(op), cnt, |
| 145 | rc, gb10_ms_since(&t0), rc == OMEGA_NUMERIC_OK ? "" : " FAILED"); |
| 146 | return rc; |
| 147 | } |
| 148 | |
| 149 | static int gb10_elementwise(OmegaNumericOp op, const float *a, const float *b, const float *c, |
| 150 | float *out, size_t n) { |
| 151 | const char *name = omega_tensor_gb10_op_name(op); |
| 152 | if (!name) return OMEGA_NUMERIC_ERR_NOT_ENCODED; /* no silent CPU fallback; MUT:GB10_NO_CPU_FALLBACK */ |
| 153 | const OmegaNumericOpInfo *info = omega_numeric_op_find(name); |
| 154 | if (!info || info->op != op || !info->gb10_encoded) return OMEGA_NUMERIC_ERR_NOT_ENCODED; |
| 155 | if (!a || !out || n == 0) return OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 156 | for (size_t base = 0; base < n; base += OMEGA_NUMERIC_MAX_COUNT) { |
| 157 | size_t cnt = n - base < OMEGA_NUMERIC_MAX_COUNT ? n - base : OMEGA_NUMERIC_MAX_COUNT; |
| 158 | int rc = gb10_simt_call(name, a + base, b ? b + base : NULL, c ? c + base : NULL, out + base, cnt); |
| 159 | if (rc != OMEGA_NUMERIC_OK) return rc; |
| 160 | } |
| 161 | return OMEGA_NUMERIC_OK; |
| 162 | } |
| 163 | |
| 164 | _Static_assert(sizeof(float) == sizeof(uint32_t), "f32 bit patterns"); |
| 165 | |
| 166 | static int gb10_transc(OmegaTensorUnaryOp op, const float *a, float *out, size_t n) { |
| 167 | int k = omega_tensor_gb10_transc_kernel(op); |
| 168 | if (k < 0) return OMEGA_NUMERIC_ERR_NOT_ENCODED; /* no silent CPU fallback; MUT:GB10_TRANSC_NO_CPU_FALLBACK */ |
| 169 | if (!a || !out || n == 0) return OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 170 | for (size_t base = 0; base < n; base += OMEGA_DS_MAX_BATCH) { |
| 171 | size_t cnt = n - base < OMEGA_DS_MAX_BATCH ? n - base : OMEGA_DS_MAX_BATCH; |
| 172 | int rc = gb10_ds_call((OmegaDsOp)k, (const uint32_t *)(const void *)(a + base), |
| 173 | (uint32_t *)(void *)(out + base), cnt); |
| 174 | if (rc != OMEGA_NUMERIC_OK) return rc; |
| 175 | } |
| 176 | return OMEGA_NUMERIC_OK; |
| 177 | } |
| 178 | |
| 179 | static int map_reduce_op(OmegaTensorReduceOp op, OmegaReduceOp *r) { |
| 180 | switch (op) { |
| 181 | case OMEGA_TR_SUM: *r = OMEGA_RED_SUM; return OMEGA_NUMERIC_OK; |
| 182 | case OMEGA_TR_MAX: *r = OMEGA_RED_MAX; return OMEGA_NUMERIC_OK; |
| 183 | case OMEGA_TR_MIN: *r = OMEGA_RED_MIN; return OMEGA_NUMERIC_OK; |
| 184 | case OMEGA_TR_MEAN: *r = OMEGA_RED_MEAN; return OMEGA_NUMERIC_OK; |
| 185 | default: return OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | static int gb10_reduce(OmegaTensorReduceOp op, const float *x, size_t n, float *out) { |
| 190 | if (strcmp(OMEGA_REDUCE_DECLARED_ORDER, OMEGA_TENSOR_REDUCE_DECLARED_ORDER) != 0) |
| 191 | return OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 192 | /* size limit first, so it is refused even before the buffers are looked at */ |
| 193 | if (n > OMEGA_REDUCE_GB10_MAX_N) return OMEGA_NUMERIC_ERR_OPERANDS; /* MUT:GB10_REDUCE_MAX_N */ |
| 194 | OmegaReduceOp r; |
| 195 | if (map_reduce_op(op, &r) != OMEGA_NUMERIC_OK || !x || !out || n == 0) return OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 196 | return gb10_reduce_call(r, x, n, out); |
| 197 | } |
| 198 | |
| 199 | /* SUM of rows rows of n values, all rows per level in shared launches. |
| 200 | * Level: each row's len values are padded with -0.0 to a whole number of |
| 201 | * 32-tiles; REDUCE_SUM leaves tile t's warp-tree sum in lane 0 of warp t. */ |
| 202 | static int gb10_sum_rows(const float *x, size_t rows, size_t n, float *out) { |
| 203 | size_t padded = (n + 31u) & ~(size_t)31u; |
| 204 | if (rows > SIZE_MAX / padded) return OMEGA_NUMERIC_ERR_OPERANDS; |
| 205 | float *cur = malloc(rows * n * sizeof(float)); |
| 206 | float *lvl = malloc(rows * padded * sizeof(float)); |
| 207 | float *res = malloc(rows * padded * sizeof(float)); |
| 208 | int rc = OMEGA_NUMERIC_OK; |
| 209 | if (!cur || !lvl || !res) { rc = OMEGA_NUMERIC_ERR_BAD_ARGS; goto done; } |
| 210 | memcpy(cur, x, rows * n * sizeof(float)); |
| 211 | const float pad = omega_bits_to_float(OMEGA_REDUCE_SUM_PAD_BITS); |
| 212 | size_t len = n; |
| 213 | do { /* at least one level, as declared */ |
| 214 | size_t pl = (len + 31u) & ~(size_t)31u, tiles = pl / 32u, total = rows * pl; |
| 215 | for (size_t r = 0; r < rows; r++) { |
| 216 | memcpy(lvl + r * pl, cur + r * len, len * sizeof(float)); |
| 217 | for (size_t i = len; i < pl; i++) lvl[r * pl + i] = pad; |
| 218 | } |
| 219 | for (size_t base = 0; base < total && rc == OMEGA_NUMERIC_OK; base += OMEGA_NUMERIC_MAX_COUNT) { |
| 220 | size_t cnt = total - base < OMEGA_NUMERIC_MAX_COUNT ? total - base : OMEGA_NUMERIC_MAX_COUNT; |
| 221 | rc = gb10_simt_call("REDUCE_SUM", lvl + base, NULL, NULL, res + base, cnt); |
| 222 | } |
| 223 | if (rc != OMEGA_NUMERIC_OK) goto done; |
| 224 | for (size_t r = 0; r < rows; r++) |
| 225 | for (size_t t = 0; t < tiles; t++) cur[r * tiles + t] = res[r * pl + t * 32u]; |
| 226 | len = tiles; |
| 227 | } while (len > 1); |
| 228 | memcpy(out, cur, rows * sizeof(float)); |
| 229 | done: |
| 230 | free(cur); |
| 231 | free(lvl); |
| 232 | free(res); |
| 233 | return rc; |
| 234 | } |
| 235 | |
| 236 | static int gb10_reduce_rows(OmegaTensorReduceOp op, const float *x, size_t rows, size_t n, float *out) { |
| 237 | if (strcmp(OMEGA_REDUCE_DECLARED_ORDER, OMEGA_TENSOR_REDUCE_DECLARED_ORDER) != 0) |
| 238 | return OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 239 | if (n > OMEGA_REDUCE_GB10_MAX_N) return OMEGA_NUMERIC_ERR_OPERANDS; /* MUT:GB10_ROWS_MAX_N */ |
| 240 | OmegaReduceOp r; |
| 241 | if (map_reduce_op(op, &r) != OMEGA_NUMERIC_OK || !x || !out || n == 0 || rows == 0) |
| 242 | return OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 243 | if (op == OMEGA_TR_SUM) return gb10_sum_rows(x, rows, n, out); |
| 244 | for (size_t i = 0; i < rows; i++) { |
| 245 | int rc = gb10_reduce_call(r, x + i * n, n, &out[i]); |
| 246 | if (rc != OMEGA_NUMERIC_OK) return rc; |
| 247 | } |
| 248 | return OMEGA_NUMERIC_OK; |
| 249 | } |
| 250 | |
| 251 | static const OmegaTensorRealization GB10_REALIZATION = { |
| 252 | .name = "GB10_E1_SIMT", |
| 253 | .reduce_order = OMEGA_TENSOR_REDUCE_DECLARED_ORDER, |
| 254 | .elementwise = gb10_elementwise, |
| 255 | .reduce = gb10_reduce, |
| 256 | .transc = gb10_transc, |
| 257 | .reduce_rows = gb10_reduce_rows, |
| 258 | }; |
| 259 | |
| 260 | const OmegaTensorRealization *omega_tensor_gb10_realization(void) { return &GB10_REALIZATION; } |