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.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_TENSOR_H |
| 18 | #define OMEGA_TENSOR_H |
| 19 | |
| 20 | /* |
| 21 | * M20 OMEGA_TENSOR: immutable semantic tensors (docs/tensor/M20_OMEGA_TENSOR.md). |
| 22 | * |
| 23 | * Meaning and realization are separate. This header defines what a tensor is |
| 24 | * and what each op computes. Arithmetic is never done here: every arithmetic |
| 25 | * step goes through an OmegaTensorRealization (a table of functions). The |
| 26 | * semantic layer only moves data (gathers elements by index) and checks. |
| 27 | * The CPU realization (omega_tensor_cpu_realization) is the only one today; |
| 28 | * a GB10 SIMT or Tensor Core realization plugs into the same table later and |
| 29 | * must reproduce the declared bits, or declare its own bounded contract. |
| 30 | * |
| 31 | * Tensors are immutable. There is no API that writes into an existing tensor: |
| 32 | * creation copies the caller's data into new storage, every op returns a new |
| 33 | * tensor, and views (permute, slice, reshape, broadcast) share the parent's |
| 34 | * storage read-only and never change the parent. |
| 35 | * |
| 36 | * Identity: |
| 37 | * value identity = dtype + shape + element values in logical row-major |
| 38 | * order (NaN payload is not semantic: every NaN counts as |
| 39 | * the canonical quiet NaN; -0 and +0 differ). Strides, |
| 40 | * offset and storage are NOT part of it: "physical layout |
| 41 | * is not identity". omega_tensor_value_id hashes exactly |
| 42 | * this (SHA-256, OMEGA_TENSOR_VALUE_ID_DOMAIN). |
| 43 | * storage identity = the allocation: storage slot + 64-bit generation. |
| 44 | * view identity = storage identity + offset + shape + strides + dtype, |
| 45 | * plus the parent tensor it was taken from. |
| 46 | * |
| 47 | * Storage lifetime: every storage slot carries a 64-bit generation. A handle |
| 48 | * is valid only while its generation equals the slot's and the slot is live. |
| 49 | * Releasing bumps the generation, so every older handle (and every view of |
| 50 | * that storage) fails closed with OMEGA_TENSOR_ERR_STALE. A slot whose |
| 51 | * generation reaches UINT64_MAX is retired forever instead of wrapping. |
| 52 | * |
| 53 | * Shapes: rank 0..OMEGA_TENSOR_MAX_RANK, every dimension >= 1 (no empty |
| 54 | * tensors), total elements <= OMEGA_TENSOR_MAX_ELEMS. Strides are counted in |
| 55 | * elements and are >= 0; 0 appears only in broadcast views. Negative strides |
| 56 | * are not defined (refused). |
| 57 | * |
| 58 | * Dtypes: F32 is the arithmetic dtype. F16 and BF16 are storage dtypes: any |
| 59 | * arithmetic op on them is refused (OMEGA_TENSOR_ERR_DTYPE); convert first |
| 60 | * with omega_tensor_cast, which uses the E1 conversion ops |
| 61 | * (F32_TO_F16 / F32_TO_BF16 RNE, F16_TO_F32 / BF16_TO_F32 exact). |
| 62 | * |
| 63 | * Not thread safe: one context per thread. |
| 64 | */ |
| 65 | |
| 66 | #include <stdbool.h> |
| 67 | #include <stddef.h> |
| 68 | #include <stdint.h> |
| 69 | |
| 70 | #include "omega_numeric.h" |
| 71 | |
| 72 | #define OMEGA_TENSOR_MAX_RANK 8u |
| 73 | #define OMEGA_TENSOR_MAX_ELEMS ((uint64_t)1 << 30) |
| 74 | #define OMEGA_TENSOR_VALUE_ID_DOMAIN "OMEGA_TENSOR_VALUE_ID_V1" |
| 75 | #define OMEGA_TENSOR_VIEW_ID_DOMAIN "OMEGA_TENSOR_VIEW_ID_V1" |
| 76 | |
| 77 | /* Frozen reduction order: must equal OMEGA_REDUCE_DECLARED_ORDER of the E1 |
| 78 | * reduction contract (docs/numeric/E1_REDUCTION_CONTRACT.md, omega PR #134). |
| 79 | * See src/tensor/omega_tensor_reduce_seam.h. */ |
| 80 | #define OMEGA_TENSOR_REDUCE_DECLARED_ORDER \ |
| 81 | "RECURSIVE_TILE32_PAIRWISE_TREE_LANE_DELTA_16_8_4_2_1_PAD_IDENTITY_MIN_ONE_LEVEL" |
| 82 | |
| 83 | /* |
| 84 | * Declared accumulation order of matmul. For every output element |
| 85 | * out[..., i, j]: products p[k] = FMUL_RNE(a[..., i, k], b[..., k, j]) for |
| 86 | * k = 0..K-1 (each rounded once, no fused multiply-add), then |
| 87 | * out = E1 REDUCE_SUM over p[0..K) in OMEGA_TENSOR_REDUCE_DECLARED_ORDER. |
| 88 | * A realization that fuses (FFMA chain, Tensor Core MMA) computes different |
| 89 | * bits and must declare its own bounded contract instead of this one. |
| 90 | */ |
| 91 | #define OMEGA_TENSOR_MATMUL_DECLARED_ORDER \ |
| 92 | "MATMUL_V1_FMUL_RNE_PRODUCTS_THEN_E1_REDUCE_SUM_" \ |
| 93 | "RECURSIVE_TILE32_PAIRWISE_TREE_LANE_DELTA_16_8_4_2_1_PAD_IDENTITY_MIN_ONE_LEVEL" |
| 94 | |
| 95 | typedef enum { |
| 96 | OMEGA_TENSOR_OK = 0, |
| 97 | OMEGA_TENSOR_ERR_BAD_ARGS = -1, /* NULL pointer, unknown op/enum */ |
| 98 | OMEGA_TENSOR_ERR_RANK = -2, /* rank out of range for this op */ |
| 99 | OMEGA_TENSOR_ERR_SHAPE = -3, /* dims mismatch / not broadcastable */ |
| 100 | OMEGA_TENSOR_ERR_DTYPE = -4, /* dtype not allowed for this op */ |
| 101 | OMEGA_TENSOR_ERR_STALE = -5, /* handle generation does not match */ |
| 102 | OMEGA_TENSOR_ERR_BOUNDS = -6, /* view reaches outside its storage */ |
| 103 | OMEGA_TENSOR_ERR_NOT_CONTIGUOUS = -7, /* reshape of a non row-major layout */ |
| 104 | OMEGA_TENSOR_ERR_CAPACITY = -8, /* no free slot / size overflow / OOM */ |
| 105 | OMEGA_TENSOR_ERR_AXIS = -9, /* axis / permutation invalid */ |
| 106 | OMEGA_TENSOR_ERR_NUMERIC = -10, /* realization refused (E1 code kept) */ |
| 107 | OMEGA_TENSOR_ERR_REALIZATION = -11, /* realization table incomplete */ |
| 108 | OMEGA_TENSOR_ERR_MASK = -12 /* where(): cond not exactly +1.0/+0.0 */ |
| 109 | } OmegaTensorStatus; |
| 110 | |
| 111 | typedef enum { |
| 112 | OMEGA_DT_F32 = 1, |
| 113 | OMEGA_DT_F16 = 2, |
| 114 | OMEGA_DT_BF16 = 3 |
| 115 | } OmegaDType; |
| 116 | |
| 117 | /* Opaque handles. Copying a handle never copies or shares write access. */ |
| 118 | typedef struct { uint32_t slot; uint64_t generation; } OmegaStorageHandle; |
| 119 | typedef struct { uint32_t slot; uint64_t generation; } OmegaTensor; |
| 120 | |
| 121 | /* Elementwise ops. Every op is an E1 scalar op applied per element. */ |
| 122 | typedef enum { |
| 123 | OMEGA_TU_SQRT = 0, /* omega_math_sqrt, correctly rounded */ |
| 124 | /* E1 bounded-contract transcendentals (NOT correctly rounded): each is |
| 125 | * the frozen E1 CPU sequence in src/omega_numeric_transc.c applied per |
| 126 | * element, bit-exact with calling it directly; max ulp distance from the |
| 127 | * correctly rounded result per docs/numeric/E1_TRANSCENDENTAL_CONTRACT.md. */ |
| 128 | OMEGA_TU_EXP2, /* omega_math_exp2, <= OMEGA_TRANSC_MAX_ULP_EXP2 (2) */ |
| 129 | OMEGA_TU_LOG2, /* omega_math_log2, <= OMEGA_TRANSC_MAX_ULP_LOG2 (2) */ |
| 130 | OMEGA_TU_SIGMOID, /* omega_math_sigmoid, <= OMEGA_TRANSC_MAX_ULP_SIGMOID (3) */ |
| 131 | OMEGA_TU_TANH, /* omega_math_tanh, <= OMEGA_TRANSC_MAX_ULP_TANH (3) */ |
| 132 | /* M20 cut ops (LT-M21 CR-1..CR-5). EXP / LOG: the E1 Omega-defined |
| 133 | * polynomial sequences omega_math_exp / omega_math_log |
| 134 | * (src/omega_numeric.c:458, :501) applied per element through the |
| 135 | * realization's transc entry, bit-exact with calling them directly. NOT |
| 136 | * correctly rounded: E1 checks them against binary128 within EXP 40 ulp, |
| 137 | * LOG 4 ulp (NUM_ORACLE_EXP_ULP / NUM_ORACLE_LOG_ULP in |
| 138 | * tests/test_omega_numeric.c). E1 row 10 has GB10 whole-program kernels |
| 139 | * for both (src/omega_numeric_divsqrt_gb10.c, body_exp / body_log); the |
| 140 | * GB10 table submits them through its transc entry. |
| 141 | * RELU: no E1 op; a tensor-layer bit select, no float arithmetic: |
| 142 | * x > +0 (incl. +inf, +subnormals) -> x; +0, -0, negatives, -inf -> +0; |
| 143 | * any NaN (any sign or payload) -> canonical qNaN OMEGA_QNAN_BITS. */ |
| 144 | OMEGA_TU_EXP, /* omega_math_exp, E1 polynomial */ |
| 145 | OMEGA_TU_LOG, /* omega_math_log, E1 polynomial */ |
| 146 | OMEGA_TU_RELU, /* bit select in the tensor layer (needs no transc) */ |
| 147 | /* LT-M21 CR-1 (unary2), same pattern: E1 sequence per element, bit-exact |
| 148 | * with calling it directly. Served by the realization's transc entry. */ |
| 149 | OMEGA_TU_RSQRT, /* omega_math_rsqrt, correctly rounded (OMEGA_TRANSC_MAX_ULP_RSQRT 0) */ |
| 150 | OMEGA_TU_ERF, /* omega_math_erf, <= OMEGA_TRANSC_MAX_ULP_ERF (3) */ |
| 151 | OMEGA_TU_GELU, /* omega_math_gelu, <= OMEGA_TRANSC_MAX_ULP_GELU (3), erf form */ |
| 152 | /* E1 trig (bounded contract, not correctly rounded): omega_math_sin / |
| 153 | * omega_math_cos per element, bit-exact with calling them directly. |
| 154 | * Admitted domain |x| <= 2^22 (OMEGA_TRANSC_TRIG_MAX_ABS, inclusive); |
| 155 | * outside it, +-inf and NaN give the canonical qNaN 0x7fc00000. |
| 156 | * sin(+-0) = +-0, cos(+-0) = +1. */ |
| 157 | OMEGA_TU_SIN, /* omega_math_sin, <= OMEGA_TRANSC_MAX_ULP_SIN (2) */ |
| 158 | OMEGA_TU_COS, /* omega_math_cos, <= OMEGA_TRANSC_MAX_ULP_COS (2) */ |
| 159 | /* Pure sign-bit flip, no E1 call, no arithmetic: bits ^ 0x80000000, so |
| 160 | * neg(+0) = -0 and neg(-0) = +0. Any NaN in gives the canonical quiet |
| 161 | * NaN 0x7fc00000 (E1 convention, omega_numeric.h:137). Needs no transc. */ |
| 162 | OMEGA_TU_NEG, |
| 163 | OMEGA_TU_COUNT |
| 164 | } OmegaTensorUnaryOp; |
| 165 | |
| 166 | typedef enum { |
| 167 | OMEGA_TB_ADD = 0, /* FADD RNE */ |
| 168 | OMEGA_TB_SUB, /* FSUB RNE */ |
| 169 | OMEGA_TB_MUL, /* FMUL RNE */ |
| 170 | OMEGA_TB_DIV, /* omega_math_div, correctly rounded */ |
| 171 | OMEGA_TB_MIN, /* FMNMX_MIN: NaN is missing data, -0 < +0 */ |
| 172 | OMEGA_TB_MAX, /* FMNMX_MAX */ |
| 173 | /* compare-select: out = P(a, b) ? a : b (E1 FSETP_<P>_SEL) */ |
| 174 | OMEGA_TB_SEL_GE, OMEGA_TB_SEL_LT, OMEGA_TB_SEL_LE, OMEGA_TB_SEL_GT, |
| 175 | OMEGA_TB_SEL_EQ, OMEGA_TB_SEL_NE, OMEGA_TB_SEL_NUM, OMEGA_TB_SEL_NAN, |
| 176 | OMEGA_TB_SEL_LTU, OMEGA_TB_SEL_LEU, OMEGA_TB_SEL_GTU, OMEGA_TB_SEL_GEU, |
| 177 | OMEGA_TB_SEL_EQU, OMEGA_TB_SEL_NEU, |
| 178 | /* CR-3 mask compare: out = P(a, b) ? +1.0 : +0.0 (bits 0x3f800000 / |
| 179 | * 0x00000000, never -0.0). P is EXACTLY the predicate of the ordered E1 |
| 180 | * compare-select op named on the right (omega_ref_fsetp_pred, |
| 181 | * src/omega_numeric.c:322): false when either operand is NaN (CMP_NE too: |
| 182 | * NaN != x gives +0.0, unlike IEEE !=), and -0 == +0. Realized by the |
| 183 | * realization's compare entry (NULL: OMEGA_TENSOR_ERR_REALIZATION). */ |
| 184 | OMEGA_TB_CMP_EQ, /* FSETP_EQ_SEL predicate */ |
| 185 | OMEGA_TB_CMP_NE, /* FSETP_NE_SEL predicate (ordered) */ |
| 186 | OMEGA_TB_CMP_LT, /* FSETP_LT_SEL predicate */ |
| 187 | OMEGA_TB_CMP_LE, /* FSETP_LE_SEL predicate */ |
| 188 | OMEGA_TB_CMP_GT, /* FSETP_GT_SEL predicate */ |
| 189 | OMEGA_TB_CMP_GE, /* FSETP_SEL (GE) predicate */ |
| 190 | OMEGA_TB_COUNT |
| 191 | } OmegaTensorBinaryOp; |
| 192 | |
| 193 | typedef enum { |
| 194 | OMEGA_TR_SUM = 0, /* identity -0.0 */ |
| 195 | OMEGA_TR_MAX, /* FMNMX_MAX, identity quiet NaN */ |
| 196 | OMEGA_TR_MIN, /* FMNMX_MIN, identity quiet NaN */ |
| 197 | OMEGA_TR_MEAN, /* omega_math_div(SUM, (float)n), n <= 2^24 */ |
| 198 | OMEGA_TR_COUNT |
| 199 | } OmegaTensorReduceOp; |
| 200 | |
| 201 | /* |
| 202 | * Realization table. All buffers are dense and contiguous; the semantic |
| 203 | * layer gathers strided/broadcast operands before calling. |
| 204 | * elementwise: out[i] = op(a[i], b[i], c[i]) for an E1 OmegaNumericOp, |
| 205 | * with the omega_numeric_cpu_realize argument conventions |
| 206 | * (b/c may be NULL when the op does not read them). |
| 207 | * reduce: *out = reduce(op, x[0..n)) in OMEGA_TENSOR_REDUCE_DECLARED_ORDER. |
| 208 | * reduce_order must equal OMEGA_TENSOR_REDUCE_DECLARED_ORDER. |
| 209 | * Return 0 or an OMEGA_NUMERIC_* code (kept in the context, mapped to |
| 210 | * OMEGA_TENSOR_ERR_NUMERIC). |
| 211 | */ |
| 212 | typedef struct { |
| 213 | const char *name; |
| 214 | const char *reduce_order; |
| 215 | int (*elementwise)(OmegaNumericOp op, const float *a, const float *b, const float *c, |
| 216 | float *out, size_t n); |
| 217 | int (*reduce)(OmegaTensorReduceOp op, const float *x, size_t n, float *out); |
| 218 | /* Optional. out[i] = op(a[i]) for the bounded-contract unary ops |
| 219 | * (OMEGA_TU_EXP2 .. OMEGA_TU_TANH, OMEGA_TU_EXP, OMEGA_TU_LOG, OMEGA_TU_RSQRT, |
| 220 | * OMEGA_TU_ERF, OMEGA_TU_GELU, OMEGA_TU_SIN, OMEGA_TU_COS), bit-exact with |
| 221 | * the E1 CPU sequences. |
| 222 | * NULL: those ops return OMEGA_TENSOR_ERR_REALIZATION (other ops work). */ |
| 223 | int (*transc)(OmegaTensorUnaryOp op, const float *a, float *out, size_t n); |
| 224 | /* Optional (CR-3). out[i] = +1.0f if the E1 predicate of the compare- |
| 225 | * select op sel_op (OMEGA_NOP_FSETP_SEL or an OMEGA_NOP_FSETP_<P>_SEL) |
| 226 | * holds for (a[i], b[i]), else +0.0f; the predicate equals |
| 227 | * omega_ref_fsetp_pred(sel_op, a[i], b[i]) bit for bit. |
| 228 | * NULL: the CMP_* binary ops return OMEGA_TENSOR_ERR_REALIZATION. */ |
| 229 | int (*compare)(OmegaNumericOp sel_op, const float *a, const float *b, float *out, size_t n); |
| 230 | /* Optional (NULL = the semantic layer calls reduce once per row): |
| 231 | * out[r] = reduce(op, x[r*n .. r*n + n)) for r < rows, every row in |
| 232 | * OMEGA_TENSOR_REDUCE_DECLARED_ORDER, so the bits equal one reduce call |
| 233 | * per row. Lets a device realization batch many short rows (matmul |
| 234 | * outputs) into a few launches. Added by M20 cut gb10. */ |
| 235 | int (*reduce_rows)(OmegaTensorReduceOp op, const float *x, size_t rows, size_t n, float *out); |
| 236 | } OmegaTensorRealization; |
| 237 | |
| 238 | const OmegaTensorRealization *omega_tensor_cpu_realization(void); |
| 239 | |
| 240 | /* |
| 241 | * GB10 realization (src/tensor/omega_tensor_gb10.c, M20 cut gb10). Needs the |
| 242 | * physics checkout to build; opens the device on every call. |
| 243 | * elementwise: E1 omega_gb10_execute_simt_op, in chunks of at most |
| 244 | * OMEGA_NUMERIC_MAX_COUNT. Only the ops the tensor layer uses |
| 245 | * and that have a GB10 E1 kernel are mapped; any other op is |
| 246 | * refused with OMEGA_NUMERIC_ERR_NOT_ENCODED before a device |
| 247 | * is opened. Never falls back to the CPU. |
| 248 | * reduce: E1 omega_reduce_gb10 (n <= OMEGA_REDUCE_GB10_MAX_N), after |
| 249 | * the same declared-order strcmp as the CPU seam. |
| 250 | * reduce_rows: SUM batches all rows per tree level through the E1 |
| 251 | * REDUCE_SUM warp kernel (same patch words as |
| 252 | * omega_reduce_gb10 SUM); other ops call omega_reduce_gb10 per |
| 253 | * row. Matmul is therefore FMUL_RNE products + the E1 tree, |
| 254 | * bit-exact with the CPU table. No Tensor Core, no FFMA. |
| 255 | */ |
| 256 | const OmegaTensorRealization *omega_tensor_gb10_realization(void); |
| 257 | /* E1 op name the GB10 table submits for op, or NULL if op is refused. */ |
| 258 | const char *omega_tensor_gb10_op_name(OmegaNumericOp op); |
| 259 | /* E1 row-10 kernel (an OmegaDsOp value, src/omega_numeric_divsqrt_gb10.h) the |
| 260 | * GB10 table's transc entry launches for op, or -1 if op is refused. */ |
| 261 | int omega_tensor_gb10_transc_kernel(OmegaTensorUnaryOp op); |
| 262 | |
| 263 | typedef struct OmegaTensorCtx OmegaTensorCtx; |
| 264 | |
| 265 | /* capacity = number of storage slots and of tensor descriptor slots. */ |
| 266 | int omega_tensor_ctx_create(uint32_t capacity, const OmegaTensorRealization *real, |
| 267 | OmegaTensorCtx **out); |
| 268 | void omega_tensor_ctx_destroy(OmegaTensorCtx *ctx); |
| 269 | /* Last OMEGA_NUMERIC_* code a realization returned (0 if none). */ |
| 270 | int omega_tensor_last_numeric_error(const OmegaTensorCtx *ctx); |
| 271 | |
| 272 | /* ---- creation (copies data; data is logical row-major in dtype) ---------- */ |
| 273 | int omega_tensor_from_data(OmegaTensorCtx *ctx, OmegaDType dtype, uint32_t rank, |
| 274 | const uint64_t *shape, const void *data, OmegaTensor *out); |
| 275 | int omega_tensor_from_f32(OmegaTensorCtx *ctx, uint32_t rank, const uint64_t *shape, |
| 276 | const float *data, OmegaTensor *out); |
| 277 | |
| 278 | /* |
| 279 | * Release. On a tensor that owns its storage (made by from_data or an op), |
| 280 | * releases the descriptor AND the storage: every view of that storage turns |
| 281 | * stale. On a view, releases only the view descriptor. Releasing twice, or |
| 282 | * through any stale handle, returns OMEGA_TENSOR_ERR_STALE and changes nothing. |
| 283 | */ |
| 284 | int omega_tensor_release(OmegaTensorCtx *ctx, OmegaTensor t); |
| 285 | |
| 286 | /* ---- read-only inspection ------------------------------------------------- */ |
| 287 | typedef struct { |
| 288 | OmegaDType dtype; |
| 289 | uint32_t rank; |
| 290 | uint64_t shape[OMEGA_TENSOR_MAX_RANK]; |
| 291 | uint64_t strides[OMEGA_TENSOR_MAX_RANK]; /* elements, >= 0 */ |
| 292 | uint64_t offset; /* elements */ |
| 293 | uint64_t elements; |
| 294 | OmegaStorageHandle storage; |
| 295 | bool is_view; |
| 296 | OmegaTensor parent; /* tensor the view was taken from */ |
| 297 | } OmegaTensorInfo; |
| 298 | |
| 299 | int omega_tensor_info(const OmegaTensorCtx *ctx, OmegaTensor t, OmegaTensorInfo *out); |
| 300 | /* Copies elements out in logical row-major order (elem size of the dtype). */ |
| 301 | int omega_tensor_read(const OmegaTensorCtx *ctx, OmegaTensor t, void *out, size_t out_bytes); |
| 302 | int omega_tensor_read_f32(const OmegaTensorCtx *ctx, OmegaTensor t, float *out, size_t count); |
| 303 | int omega_tensor_value_id(const OmegaTensorCtx *ctx, OmegaTensor t, uint8_t id[32]); |
| 304 | /* Semantic equality: same dtype, shape and values (NaN class, -0 != +0). */ |
| 305 | int omega_tensor_value_equal(const OmegaTensorCtx *ctx, OmegaTensor a, OmegaTensor b, bool *eq); |
| 306 | int omega_tensor_view_id(const OmegaTensorCtx *ctx, OmegaTensor t, uint8_t id[32]); |
| 307 | bool omega_tensor_storage_valid(const OmegaTensorCtx *ctx, OmegaStorageHandle s); |
| 308 | size_t omega_dtype_size(OmegaDType dt); |
| 309 | |
| 310 | /* ---- views (share storage, never copy, never mutate the parent) ---------- */ |
| 311 | /* out dim d = in dim perm[d]. */ |
| 312 | int omega_tensor_permute(OmegaTensorCtx *ctx, OmegaTensor t, const uint32_t *perm, OmegaTensor *out); |
| 313 | /* Swaps the last two axes (rank >= 2). */ |
| 314 | int omega_tensor_transpose(OmegaTensorCtx *ctx, OmegaTensor t, OmegaTensor *out); |
| 315 | /* Per axis: start < stop <= dim, step >= 1; out dim = ceil((stop-start)/step). |
| 316 | * step may be NULL (all 1). */ |
| 317 | int omega_tensor_slice(OmegaTensorCtx *ctx, OmegaTensor t, const uint64_t *start, |
| 318 | const uint64_t *stop, const uint64_t *step, OmegaTensor *out); |
| 319 | /* View only if t is row-major contiguous (strides equal the dense row-major |
| 320 | * strides, ignoring axes of size 1); otherwise OMEGA_TENSOR_ERR_NOT_CONTIGUOUS. |
| 321 | * Use omega_tensor_contiguous first to get a copy. */ |
| 322 | int omega_tensor_reshape(OmegaTensorCtx *ctx, OmegaTensor t, uint32_t rank, |
| 323 | const uint64_t *shape, OmegaTensor *out); |
| 324 | /* Broadcast view (numpy rules, see omega_tensor_broadcast_shape): stride 0 on |
| 325 | * expanded axes. */ |
| 326 | int omega_tensor_broadcast_to(OmegaTensorCtx *ctx, OmegaTensor t, uint32_t rank, |
| 327 | const uint64_t *shape, OmegaTensor *out); |
| 328 | |
| 329 | /* ---- new tensors ---------------------------------------------------------- */ |
| 330 | /* Dense row-major copy with its own storage (same value identity). */ |
| 331 | int omega_tensor_contiguous(OmegaTensorCtx *ctx, OmegaTensor t, OmegaTensor *out); |
| 332 | /* F32 <-> F16/BF16 via E1 conversion ops; same dtype = contiguous copy; |
| 333 | * F16 <-> BF16 is refused (go through F32 explicitly). */ |
| 334 | int omega_tensor_cast(OmegaTensorCtx *ctx, OmegaTensor t, OmegaDType to, OmegaTensor *out); |
| 335 | |
| 336 | /* |
| 337 | * Broadcasting (numpy rules, stated): align shapes at the trailing axis; the |
| 338 | * missing leading axes of the shorter shape count as 1; for every axis the |
| 339 | * two sizes must be equal or one of them must be 1; the result size is the |
| 340 | * larger. Anything else is OMEGA_TENSOR_ERR_SHAPE. |
| 341 | */ |
| 342 | int omega_tensor_broadcast_shape(uint32_t ra, const uint64_t *sa, uint32_t rb, const uint64_t *sb, |
| 343 | uint32_t *rout, uint64_t *sout); |
| 344 | |
| 345 | int omega_tensor_unary(OmegaTensorCtx *ctx, OmegaTensorUnaryOp op, OmegaTensor a, OmegaTensor *out); |
| 346 | /* Constant tensors (answers LT-M21 CR-2). full() writes the F32 bits of |
| 347 | * `value` exactly into every element: -0.0 and any NaN payload are kept (no |
| 348 | * canonicalization; value ids still treat all NaNs as one). zeros = +0.0 |
| 349 | * (bits 0x00000000), ones = 1.0f. Shape rules as omega_tensor_from_f32 |
| 350 | * (rank <= 8, dims >= 1, elements <= OMEGA_TENSOR_MAX_ELEMS). */ |
| 351 | int omega_tensor_full(OmegaTensorCtx *ctx, uint32_t rank, const uint64_t *shape, float value, |
| 352 | OmegaTensor *out); |
| 353 | int omega_tensor_zeros(OmegaTensorCtx *ctx, uint32_t rank, const uint64_t *shape, OmegaTensor *out); |
| 354 | int omega_tensor_ones(OmegaTensorCtx *ctx, uint32_t rank, const uint64_t *shape, OmegaTensor *out); |
| 355 | int omega_tensor_binary(OmegaTensorCtx *ctx, OmegaTensorBinaryOp op, OmegaTensor a, OmegaTensor b, |
| 356 | OmegaTensor *out); |
| 357 | /* a*b + c rounded once (E1 FFMA), all three broadcast together. */ |
| 358 | int omega_tensor_fma(OmegaTensorCtx *ctx, OmegaTensor a, OmegaTensor b, OmegaTensor c, OmegaTensor *out); |
| 359 | |
| 360 | /* |
| 361 | * CR-3 where: out[i] = cond[i] is +1.0 ? a[i] : b[i], cond, a and b broadcast |
| 362 | * together (numpy rules above), all F32. The result is a bit copy of the |
| 363 | * chosen element (no arithmetic: -0.0 and NaN payloads are kept). cond must |
| 364 | * hold only the bit patterns OMEGA_TENSOR_MASK_TRUE_BITS (+1.0) and |
| 365 | * OMEGA_TENSOR_MASK_FALSE_BITS (+0.0), as the CMP_* ops produce; any other |
| 366 | * element (-0.0, NaN, 0.5, 2.0, ...) is refused with OMEGA_TENSOR_ERR_MASK |
| 367 | * and no tensor is made. |
| 368 | */ |
| 369 | #define OMEGA_TENSOR_MASK_TRUE_BITS 0x3f800000U |
| 370 | #define OMEGA_TENSOR_MASK_FALSE_BITS 0x00000000U |
| 371 | int omega_tensor_where(OmegaTensorCtx *ctx, OmegaTensor cond, OmegaTensor a, OmegaTensor b, |
| 372 | OmegaTensor *out); |
| 373 | |
| 374 | /* Reduce along one axis. keepdims keeps that axis with size 1. */ |
| 375 | int omega_tensor_reduce(OmegaTensorCtx *ctx, OmegaTensorReduceOp op, OmegaTensor t, uint32_t axis, |
| 376 | bool keepdims, OmegaTensor *out); |
| 377 | |
| 378 | /* |
| 379 | * Reduce over several axes (CR-5). Declared order, bit for bit: a sequence of |
| 380 | * single-axis omega_tensor_reduce calls, one per listed axis, in DESCENDING |
| 381 | * axis order (largest axis index first), each with the caller's keepdims. |
| 382 | * Descending order keeps the remaining axis indices valid when keepdims is |
| 383 | * false. Each step is the frozen E1 reduction (OMEGA_TENSOR_REDUCE_DECLARED_ORDER), |
| 384 | * so the result is fully defined by the existing single-axis contract. |
| 385 | * naxes 1..rank, axes[] distinct and < rank (any order given; they are |
| 386 | * sorted), else OMEGA_TENSOR_ERR_AXIS. naxes == 0: dense copy (no arithmetic). |
| 387 | * OMEGA_TR_MEAN with naxes > 1 is REFUSED (OMEGA_TENSOR_ERR_BAD_ARGS): a |
| 388 | * chain would be a mean of means, a different rounding from one mean over |
| 389 | * all elements. Sum over the axes, then divide, if that is what is wanted. |
| 390 | * F32 only; rank-0 input is OMEGA_TENSOR_ERR_RANK (as omega_tensor_reduce). |
| 391 | */ |
| 392 | int omega_tensor_reduce_axes(OmegaTensorCtx *ctx, OmegaTensorReduceOp op, OmegaTensor t, |
| 393 | uint32_t naxes, const uint32_t *axes, bool keepdims, OmegaTensor *out); |
| 394 | |
| 395 | /* |
| 396 | * Unbroadcast (CR-5): the SUM that undoes omega_tensor_broadcast_to(x, shape(t)). |
| 397 | * Numpy unbroadcast rules: align (rank, shape) with t's shape at the trailing |
| 398 | * axis. Every leading axis of t (index < t.rank - rank) is summed away; every |
| 399 | * aligned axis where shape[d] == 1 and t's size is > 1 is summed with |
| 400 | * keepdims. Axes of size 1 in t are never reduced: their bits are copied. |
| 401 | * Declared order: those SUM reductions as single-axis omega_tensor_reduce |
| 402 | * calls in DESCENDING axis index of t (keepdims for aligned axes, not for |
| 403 | * leading ones), then a bit copy into the requested shape. |
| 404 | * Refused: rank > t.rank, or an aligned target size that is neither equal to |
| 405 | * t's size nor 1 (OMEGA_TENSOR_ERR_SHAPE); zero dim (OMEGA_TENSOR_ERR_SHAPE); |
| 406 | * rank > OMEGA_TENSOR_MAX_RANK (OMEGA_TENSOR_ERR_RANK); non-F32 (OMEGA_TENSOR_ERR_DTYPE). |
| 407 | */ |
| 408 | int omega_tensor_sum_to_shape(OmegaTensorCtx *ctx, OmegaTensor t, uint32_t rank, |
| 409 | const uint64_t *shape, OmegaTensor *out); |
| 410 | |
| 411 | /* |
| 412 | * General matmul, OMEGA_TENSOR_MATMUL_DECLARED_ORDER, F32 only. |
| 413 | * Rank 2: [M,K] x [K,N] -> [M,N], any M, N, K >= 1. |
| 414 | * Rank >= 3 (batched): [...,M,K] x [...,K,N]; the batch axes broadcast with |
| 415 | * the numpy rules above; both operands must have rank >= 2. |
| 416 | */ |
| 417 | int omega_tensor_matmul(OmegaTensorCtx *ctx, OmegaTensor a, OmegaTensor b, OmegaTensor *out); |
| 418 | |
| 419 | /* ---- placement (CR-4): pure bit copies, no arithmetic -------------------- */ |
| 420 | /* |
| 421 | * Embed: the exact inverse placement of omega_tensor_slice. Returns a new |
| 422 | * dense tensor of (rank, out_shape), dtype of src, every element all-zero |
| 423 | * bits (+0.0 for F32/F16/BF16), except that src element i (logical row-major |
| 424 | * multi-index) is copied bit for bit to out position start + i * step, per |
| 425 | * axis. step may be NULL (all 1). For y = embed(x, start, step) and |
| 426 | * stop[d] = start[d] + (x.shape[d] - 1) * step[d] + 1, |
| 427 | * slice(y, start, stop, step) is x bit for bit. |
| 428 | * Refused (typed, nothing created): rank != src rank or rank 0 -> ERR_RANK; |
| 429 | * out_shape with a 0 dim -> ERR_SHAPE; result > OMEGA_TENSOR_MAX_ELEMS -> |
| 430 | * ERR_CAPACITY; a step of 0 (steps are unsigned, so "<= 0" is exactly 0) -> |
| 431 | * ERR_BAD_ARGS (as in slice); any placed position outside out_shape -> |
| 432 | * ERR_BOUNDS. Works for every dtype (no arithmetic). |
| 433 | */ |
| 434 | int omega_tensor_embed(OmegaTensorCtx *ctx, OmegaTensor src, uint32_t rank, const uint64_t *out_shape, |
| 435 | const uint64_t *start, const uint64_t *step, OmegaTensor *out); |
| 436 | /* |
| 437 | * Concat: n >= 1 tensors joined along axis, in array order, bit for bit. |
| 438 | * All parts must have the same dtype (else ERR_DTYPE), the same rank >= 1 |
| 439 | * (else ERR_RANK) and equal sizes on every other axis (else ERR_SHAPE); |
| 440 | * axis >= rank -> ERR_AXIS; total > OMEGA_TENSOR_MAX_ELEMS -> ERR_CAPACITY; |
| 441 | * n == 0 or NULL arrays -> ERR_BAD_ARGS. Parts may be views (strided, |
| 442 | * broadcast). Part k occupies [off_k, off_k + part_k.shape[axis]) on axis, |
| 443 | * off_k = sum of earlier parts' sizes on axis. |
| 444 | */ |
| 445 | int omega_tensor_concat(OmegaTensorCtx *ctx, uint32_t n, const OmegaTensor *tensors, uint32_t axis, |
| 446 | OmegaTensor *out); |
| 447 | |
| 448 | /* Live tensor descriptors and live storage slots (lifetime audits). */ |
| 449 | void omega_tensor_live_counts(const OmegaTensorCtx *ctx, uint32_t *tensors, uint32_t *storages); |
| 450 | |
| 451 | #ifdef OMEGA_TENSOR_TEST_HOOKS |
| 452 | /* Test-only: set the generation of a free storage slot, to exercise the |
| 453 | * UINT64_MAX retirement rule without 2^64 releases. Refused on a live slot. |
| 454 | * Compiled only when OMEGA_TENSOR_TEST_HOOKS is defined (the test build in |
| 455 | * mk/tensor.mk); the default library build does not contain this symbol and |
| 456 | * test-tensor-no-hooks checks that with nm. */ |
| 457 | int omega_tensor_test_set_storage_generation(OmegaTensorCtx *ctx, uint32_t slot, uint64_t gen); |
| 458 | #endif |
| 459 | |
| 460 | #endif /* OMEGA_TENSOR_H */ |