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 / tests / test_omega_autodiff.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 | * M21 OMEGA_AUTODIFF CPU tests (docs/autodiff/M21_OMEGA_AUTODIFF.md). |
| 19 | * |
| 20 | * Four independent checks of every backward rule, all computed here: |
| 21 | * 1. Contract: the tape's gradient against an exact reference written in |
| 22 | * this file from the calculus (double precision, index arithmetic on |
| 23 | * shapes only), within the per-op bound of omega_autodiff.h. |
| 24 | * 2. Bit-exact (approved M21 gate): the backward rule replayed here by |
| 25 | * calling the same M20 tensor ops directly, in the tape's order; the |
| 26 | * tape's gradient must equal it bit for bit (memcmp). |
| 27 | * 3. Finite differences (approved M21 gate): central differences of the M20 |
| 28 | * CPU forward pass (the same tape ops, re-run on perturbed inputs), pure |
| 29 | * relative tolerance |tape - fd| <= FD_RTOL * |fd| with step FD_H, on a |
| 30 | * separate data set whose gradients are away from zero (or exactly zero). |
| 31 | * 4. Determinism: two full runs in two contexts give bit-identical |
| 32 | * gradients and the same loss value id. |
| 33 | * Plus the error paths (tape full, shape mismatch, seed shape, state, |
| 34 | * scratch, dtype, bad ids) and a leak check. Each check computes its own |
| 35 | * verdict; the in-process counterexamples prove the comparators can fail. |
| 36 | * No GPU, no device, no libm. |
| 37 | */ |
| 38 | #include <stdbool.h> |
| 39 | #include <stdint.h> |
| 40 | #include <stdio.h> |
| 41 | #include <string.h> |
| 42 | |
| 43 | #include "omega_numeric.h" |
| 44 | #include "omega_tensor.h" |
| 45 | #include "omega_autodiff.h" |
| 46 | |
| 47 | static int g_pass, g_fail; |
| 48 | #define CHECK(cond, ...) do { if (cond) g_pass++; else { g_fail++; \ |
| 49 | printf("FAIL %s:%d: ", __FILE__, __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0) |
| 50 | |
| 51 | static OmegaTensorCtx *g; |
| 52 | |
| 53 | #define FD_H 0.015625 /* 2^-6 */ |
| 54 | /* Approved M21 gate (owner, 2026-10-01): "gradients bit-exact against the same |
| 55 | * M20 operations, plus a finite-difference check within 1e-3 relative error". |
| 56 | * FD_RTOL is that 1e-3 relative bound, applied with no absolute slack term |
| 57 | * (fd_ok below). Near-zero gradients are not compared against FD: the FD data |
| 58 | * sets (gen_fd_data, lin_data_fd) keep every gradient element either >= ~0.1 |
| 59 | * in magnitude or exactly zero, and every element, near zero or not, is |
| 60 | * covered by the bit-exact layer. Do not raise FD_RTOL or add an absolute term. */ |
| 61 | #define FD_RTOL 1e-3 |
| 62 | #define U32 5.9604644775390625e-08 /* 2^-24 */ |
| 63 | #define MAXE 128 |
| 64 | |
| 65 | /* ---- tiny helpers (no libm) ---------------------------------------------------- */ |
| 66 | |
| 67 | static double dabs(double x) { return x < 0 ? -x : x; } |
| 68 | |
| 69 | static double dsqrt(double a) { |
| 70 | if (a <= 0) return 0; |
| 71 | double y = a > 1 ? a : 1; |
| 72 | for (int i = 0; i < 200; i++) { |
| 73 | double ny = 0.5 * (y + a / y); |
| 74 | if (ny == y) break; |
| 75 | y = ny; |
| 76 | } |
| 77 | return y; |
| 78 | } |
| 79 | |
| 80 | /* Approved M21 FD gate: relative error <= 1e-3, no absolute term. */ |
| 81 | static bool fd_ok(double an, double fd) { return dabs(an - fd) <= FD_RTOL * dabs(fd); } |
| 82 | |
| 83 | static double gamma_k(int k) { return k * U32 / (1.0 - k * U32); } |
| 84 | |
| 85 | static int levels(uint64_t m) { |
| 86 | int L = 1; |
| 87 | uint64_t cap = 32; |
| 88 | while (cap < m) { cap *= 32; L++; } |
| 89 | return L; |
| 90 | } |
| 91 | |
| 92 | static uint32_t g_rng = 0x2545F491u; |
| 93 | static float urand(float lo, float hi) { |
| 94 | g_rng = g_rng * 1664525u + 1013904223u; |
| 95 | double r = (double)(g_rng >> 8) / 16777216.0; |
| 96 | return (float)(lo + (hi - lo) * r); |
| 97 | } |
| 98 | |
| 99 | static size_t numel(uint32_t rank, const uint64_t *s) { |
| 100 | size_t n = 1; |
| 101 | for (uint32_t d = 0; d < rank; d++) n *= (size_t)s[d]; |
| 102 | return n; |
| 103 | } |
| 104 | |
| 105 | static OmegaTensor mk(uint32_t rank, const uint64_t *shape, const float *data) { |
| 106 | OmegaTensor t = { 0, 0 }; |
| 107 | uint64_t dummy = 1; |
| 108 | int rc = omega_tensor_from_f32(g, rank, rank ? shape : &dummy, data, &t); |
| 109 | if (rc) printf("mk: rc %d\n", rc); |
| 110 | return t; |
| 111 | } |
| 112 | |
| 113 | static void unravel(size_t flat, uint32_t rank, const uint64_t *s, uint64_t *coord) { |
| 114 | for (uint32_t d = rank; d-- > 0;) { coord[d] = flat % s[d]; flat /= s[d]; } |
| 115 | } |
| 116 | |
| 117 | /* Flat index into an input of shape si for output coordinate (numpy rules). */ |
| 118 | static size_t bmap(const uint64_t *oc, uint32_t ro, uint32_t ri, const uint64_t *si) { |
| 119 | size_t idx = 0; |
| 120 | for (uint32_t d = 0; d < ri; d++) { |
| 121 | uint64_t c = si[d] == 1 ? 0 : oc[d + ro - ri]; |
| 122 | idx = idx * si[d] + c; |
| 123 | } |
| 124 | return idx; |
| 125 | } |
| 126 | |
| 127 | /* D(x): depth the unbroadcast reductions add (header definition). */ |
| 128 | static int unb_depth(uint32_t ro, const uint64_t *so, uint32_t ri, const uint64_t *si) { |
| 129 | int D = 0; |
| 130 | for (uint32_t d = 0; d < ro - ri; d++) if (so[d] > 1) D += 5 * levels(so[d]); |
| 131 | for (uint32_t d = 0; d < ri; d++) if (si[d] == 1 && so[d + ro - ri] > 1) D += 5 * levels(so[d + ro - ri]); |
| 132 | return D; |
| 133 | } |
| 134 | |
| 135 | /* Bound check: |an - ref| <= gamma(k) * abs + tiny slack for the double oracle. */ |
| 136 | static size_t bound_violations(const float *an, const double *ref, const double *abs_, const int *k, |
| 137 | size_t n, size_t *first) { |
| 138 | size_t bad = 0; |
| 139 | for (size_t i = 0; i < n; i++) { |
| 140 | double lim = gamma_k(k[i]) * abs_[i] + 1e-12 * abs_[i] + 1e-30; |
| 141 | if (!(dabs((double)an[i] - ref[i]) <= lim)) { if (!bad) *first = i; bad++; } |
| 142 | } |
| 143 | return bad; |
| 144 | } |
| 145 | |
| 146 | /* ---- op cases ---------------------------------------------------------------------- */ |
| 147 | |
| 148 | enum { DATA_UNIFORM = 0, DATA_AWAY_FROM_ZERO, DATA_OFFSET_FROM_B }; |
| 149 | |
| 150 | typedef struct { |
| 151 | const char *name; |
| 152 | OmegaAdOp op; |
| 153 | uint32_t ra; uint64_t sa[4]; |
| 154 | uint32_t rb; uint64_t sb[4]; /* rb 0 and op unary: no b */ |
| 155 | bool has_b; |
| 156 | uint32_t axis; bool keepdims; /* SUM / MEAN */ |
| 157 | uint32_t rt; uint64_t st[4]; /* BROADCAST target */ |
| 158 | float alo, ahi, blo, bhi; |
| 159 | int data; |
| 160 | } OpCase; |
| 161 | |
| 162 | static const OpCase CASES[] = { |
| 163 | { "add [3,4]+[4]", OMEGA_AD_ADD, 2, {3,4}, 1, {4}, true, 0, false, 0, {0}, -2, 2, -2, 2, DATA_UNIFORM }, |
| 164 | { "add [2,1,3]+[4,1]", OMEGA_AD_ADD, 3, {2,1,3}, 2, {4,1}, true, 0, false, 0, {0}, -2, 2, -2, 2, DATA_UNIFORM }, |
| 165 | { "add [3,4]+[3,4]", OMEGA_AD_ADD, 2, {3,4}, 2, {3,4}, true, 0, false, 0, {0}, -2, 2, -2, 2, DATA_UNIFORM }, |
| 166 | { "sub [3,4]-[3,1]", OMEGA_AD_SUB, 2, {3,4}, 2, {3,1}, true, 0, false, 0, {0}, -2, 2, -2, 2, DATA_UNIFORM }, |
| 167 | { "sub [3,4]-[3,4]", OMEGA_AD_SUB, 2, {3,4}, 2, {3,4}, true, 0, false, 0, {0}, -2, 2, -2, 2, DATA_UNIFORM }, |
| 168 | { "mul [3,4]*[1,4]", OMEGA_AD_MUL, 2, {3,4}, 2, {1,4}, true, 0, false, 0, {0}, -2, 2, -2, 2, DATA_UNIFORM }, |
| 169 | { "mul [2,3]*[1]", OMEGA_AD_MUL, 2, {2,3}, 1, {1}, true, 0, false, 0, {0}, -2, 2, -2, 2, DATA_UNIFORM }, |
| 170 | { "div [3,4]/[4]", OMEGA_AD_DIV, 2, {3,4}, 1, {4}, true, 0, false, 0, {0}, -2, 2, 1, 2, DATA_UNIFORM }, |
| 171 | { "div [3,4]/[3,4]", OMEGA_AD_DIV, 2, {3,4}, 2, {3,4}, true, 0, false, 0, {0}, -2, 2, 1, 2, DATA_UNIFORM }, |
| 172 | { "max relu [3,4] vs [1]", OMEGA_AD_MAX, 2, {3,4}, 1, {1}, true, 0, false, 0, {0}, -1, 1, 0, 0, DATA_AWAY_FROM_ZERO }, |
| 173 | { "max [3,4] vs [3,4]", OMEGA_AD_MAX, 2, {3,4}, 2, {3,4}, true, 0, false, 0, {0}, -1, 1, -1, 1, DATA_OFFSET_FROM_B }, |
| 174 | { "sqrt [3,4]", OMEGA_AD_SQRT, 2, {3,4}, 0, {0}, false, 0, false, 0, {0}, 1, 4, 0, 0, DATA_UNIFORM }, |
| 175 | { "matmul [3,5]x[5,4]", OMEGA_AD_MATMUL, 2, {3,5}, 2, {5,4}, true, 0, false, 0, {0}, -1, 1, -1, 1, DATA_UNIFORM }, |
| 176 | { "matmul [2,3,5]x[5,4]", OMEGA_AD_MATMUL, 3, {2,3,5}, 2, {5,4}, true, 0, false, 0, {0}, -1, 1, -1, 1, DATA_UNIFORM }, |
| 177 | { "transpose [3,4]", OMEGA_AD_TRANSPOSE, 2, {3,4}, 0, {0}, false, 0, false, 0, {0}, -2, 2, 0, 0, DATA_UNIFORM }, |
| 178 | { "broadcast [4]->[3,4]", OMEGA_AD_BROADCAST, 1, {4}, 0, {0}, false, 0, false, 2, {3,4}, -2, 2, 0, 0, DATA_UNIFORM }, |
| 179 | { "broadcast [3,1]->[2,3,4]", OMEGA_AD_BROADCAST, 2, {3,1}, 0, {0}, false, 0, false, 3, {2,3,4}, -2, 2, 0, 0, DATA_UNIFORM }, |
| 180 | { "sum [3,4] axis 1", OMEGA_AD_SUM, 2, {3,4}, 0, {0}, false, 1, false, 0, {0}, -2, 2, 0, 0, DATA_UNIFORM }, |
| 181 | { "sum [3,4] axis 0 keep", OMEGA_AD_SUM, 2, {3,4}, 0, {0}, false, 0, true, 0, {0}, -2, 2, 0, 0, DATA_UNIFORM }, |
| 182 | { "mean [3,4] axis 1", OMEGA_AD_MEAN, 2, {3,4}, 0, {0}, false, 1, false, 0, {0}, -2, 2, 0, 0, DATA_UNIFORM }, |
| 183 | { "mean [3,4] axis 0 keep", OMEGA_AD_MEAN, 2, {3,4}, 0, {0}, false, 0, true, 0, {0}, -2, 2, 0, 0, DATA_UNIFORM }, |
| 184 | }; |
| 185 | #define NCASES (sizeof(CASES) / sizeof(CASES[0])) |
| 186 | |
| 187 | /* Records the case's op on a fresh tape. Leaves a (and b) require grad. */ |
| 188 | static int build(const OpCase *c, OmegaAdTape *t, OmegaTensor ta, OmegaTensor tb, |
| 189 | uint32_t *ia, uint32_t *ib, uint32_t *iy) { |
| 190 | int rc = omega_ad_leaf(t, ta, true, ia); |
| 191 | if (rc) return rc; |
| 192 | *ib = OMEGA_AD_NONE; |
| 193 | if (c->has_b) { rc = omega_ad_leaf(t, tb, true, ib); if (rc) return rc; } |
| 194 | switch (c->op) { |
| 195 | case OMEGA_AD_ADD: case OMEGA_AD_SUB: case OMEGA_AD_MUL: case OMEGA_AD_DIV: case OMEGA_AD_MAX: |
| 196 | return omega_ad_binary(t, c->op, *ia, *ib, iy); |
| 197 | case OMEGA_AD_SQRT: return omega_ad_sqrt(t, *ia, iy); |
| 198 | case OMEGA_AD_MATMUL: return omega_ad_matmul(t, *ia, *ib, iy); |
| 199 | case OMEGA_AD_TRANSPOSE: return omega_ad_transpose(t, *ia, iy); |
| 200 | case OMEGA_AD_BROADCAST: return omega_ad_broadcast(t, *ia, c->rt, c->st, iy); |
| 201 | case OMEGA_AD_SUM: case OMEGA_AD_MEAN: return omega_ad_reduce(t, c->op, *ia, c->axis, c->keepdims, iy); |
| 202 | default: return OMEGA_AD_ERR_BAD_ARGS; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /* Forward of the case on the M20 CPU tier; returns L = sum(y * R) in double. */ |
| 207 | static int forward_loss(const OpCase *c, const float *a, const float *b, const float *R, size_t ny, |
| 208 | double *L) { |
| 209 | OmegaAdNode nodes[4]; |
| 210 | OmegaAdTape t; |
| 211 | OmegaTensor ta = mk(c->ra, c->sa, a), tb = { 0, 0 }; |
| 212 | if (c->has_b) tb = mk(c->rb, c->sb, b); |
| 213 | int rc = omega_ad_tape_init(&t, g, nodes, 4, NULL, 0); |
| 214 | uint32_t ia = 0, ib = 0, iy = 0; |
| 215 | if (!rc) rc = build(c, &t, ta, tb, &ia, &ib, &iy); |
| 216 | float y[MAXE]; |
| 217 | OmegaTensor ty = { 0, 0 }; |
| 218 | if (!rc) rc = omega_ad_value(&t, iy, &ty); |
| 219 | if (!rc) rc = omega_tensor_read_f32(g, ty, y, ny); |
| 220 | if (!rc) { |
| 221 | double s = 0; |
| 222 | for (size_t i = 0; i < ny; i++) s += (double)y[i] * (double)R[i]; |
| 223 | *L = s; |
| 224 | } |
| 225 | omega_ad_tape_release(&t); |
| 226 | omega_tensor_release(g, ta); |
| 227 | if (c->has_b) omega_tensor_release(g, tb); |
| 228 | return rc; |
| 229 | } |
| 230 | |
| 231 | /* Exact reference gradients and bound terms for one case. */ |
| 232 | static void reference(const OpCase *c, const float *a, const float *b, const float *R, |
| 233 | uint32_t ry, const uint64_t *sy, |
| 234 | double *ra_, double *aa, int *ka, double *rb_, double *ab, int *kb) { |
| 235 | size_t na = numel(c->ra, c->sa), nb = c->has_b ? numel(c->rb, c->sb) : 0, ny = numel(ry, sy); |
| 236 | for (size_t i = 0; i < na; i++) { ra_[i] = 0; aa[i] = 0; ka[i] = 0; } |
| 237 | for (size_t i = 0; i < nb; i++) { rb_[i] = 0; ab[i] = 0; kb[i] = 0; } |
| 238 | uint64_t oc[8], ic[8]; |
| 239 | switch (c->op) { |
| 240 | case OMEGA_AD_ADD: case OMEGA_AD_SUB: case OMEGA_AD_MUL: case OMEGA_AD_DIV: case OMEGA_AD_MAX: |
| 241 | case OMEGA_AD_BROADCAST: { |
| 242 | int Da = unb_depth(ry, sy, c->ra, c->sa); |
| 243 | int Db = c->has_b ? unb_depth(ry, sy, c->rb, c->sb) : 0; |
| 244 | for (size_t j = 0; j < ny; j++) { |
| 245 | unravel(j, ry, sy, oc); |
| 246 | size_t xa = bmap(oc, ry, c->ra, c->sa); |
| 247 | size_t xb = c->has_b ? bmap(oc, ry, c->rb, c->sb) : 0; |
| 248 | double gv = R[j], av = a[xa], bv = c->has_b ? b[xb] : 0, ta = 0, tb = 0; |
| 249 | int ea = Da, eb = Db; |
| 250 | switch (c->op) { |
| 251 | case OMEGA_AD_ADD: ta = gv; tb = gv; break; |
| 252 | case OMEGA_AD_SUB: ta = gv; tb = -gv; break; |
| 253 | case OMEGA_AD_MUL: ta = gv * bv; tb = gv * av; ea += 1; eb += 1; break; |
| 254 | case OMEGA_AD_DIV: ta = gv / bv; tb = -gv * av / (bv * bv); ea += 1; eb += 3; break; |
| 255 | case OMEGA_AD_MAX: { |
| 256 | bool to_a = av == av && (bv != bv || av > bv); |
| 257 | ta = to_a ? gv : 0; tb = to_a ? 0 : gv; |
| 258 | break; |
| 259 | } |
| 260 | default: ta = gv; break; /* BROADCAST */ |
| 261 | } |
| 262 | ra_[xa] += ta; aa[xa] += dabs(ta); ka[xa] = ea; |
| 263 | if (c->has_b) { rb_[xb] += tb; ab[xb] += dabs(tb); kb[xb] = eb; } |
| 264 | } |
| 265 | break; |
| 266 | } |
| 267 | case OMEGA_AD_SQRT: |
| 268 | for (size_t i = 0; i < na; i++) { |
| 269 | ra_[i] = (double)R[i] / (2.0 * dsqrt(a[i])); |
| 270 | aa[i] = dabs(ra_[i]); |
| 271 | ka[i] = 2; |
| 272 | } |
| 273 | break; |
| 274 | case OMEGA_AD_MATMUL: { |
| 275 | /* a: [Bt,M,K] (Bt = 1 for rank 2), b: [K,N] */ |
| 276 | size_t Bt = c->ra == 3 ? c->sa[0] : 1, M = c->sa[c->ra - 2], K = c->sa[c->ra - 1], N = c->sb[1]; |
| 277 | int kA = 1 + 5 * levels(N), kB = 1 + 5 * levels(M) + (Bt > 1 ? 5 * levels(Bt) : 0); |
| 278 | for (size_t q = 0; q < Bt; q++) |
| 279 | for (size_t i = 0; i < M; i++) |
| 280 | for (size_t k = 0; k < K; k++) { |
| 281 | double s = 0, sa = 0; |
| 282 | for (size_t j = 0; j < N; j++) { |
| 283 | double p = (double)R[(q * M + i) * N + j] * b[k * N + j]; |
| 284 | s += p; sa += dabs(p); |
| 285 | } |
| 286 | size_t x = (q * M + i) * K + k; |
| 287 | ra_[x] = s; aa[x] = sa; ka[x] = kA; |
| 288 | } |
| 289 | for (size_t k = 0; k < K; k++) |
| 290 | for (size_t j = 0; j < N; j++) { |
| 291 | double s = 0, sa = 0; |
| 292 | for (size_t q = 0; q < Bt; q++) |
| 293 | for (size_t i = 0; i < M; i++) { |
| 294 | double p = (double)a[(q * M + i) * K + k] * R[(q * M + i) * N + j]; |
| 295 | s += p; sa += dabs(p); |
| 296 | } |
| 297 | rb_[k * N + j] = s; ab[k * N + j] = sa; kb[k * N + j] = kB; |
| 298 | } |
| 299 | break; |
| 300 | } |
| 301 | case OMEGA_AD_TRANSPOSE: { |
| 302 | size_t M = c->sa[0], N = c->sa[1]; |
| 303 | for (size_t i = 0; i < M; i++) |
| 304 | for (size_t j = 0; j < N; j++) { |
| 305 | ra_[i * N + j] = R[j * M + i]; |
| 306 | aa[i * N + j] = dabs(R[j * M + i]); |
| 307 | } |
| 308 | break; |
| 309 | } |
| 310 | case OMEGA_AD_SUM: case OMEGA_AD_MEAN: { |
| 311 | double n = (double)c->sa[c->axis]; |
| 312 | for (size_t i = 0; i < na; i++) { |
| 313 | unravel(i, c->ra, c->sa, ic); |
| 314 | size_t j = 0; |
| 315 | for (uint32_t d = 0; d < c->ra; d++) { |
| 316 | if (d == c->axis) continue; /* keepdims: size-1 axis adds nothing */ |
| 317 | j = j * c->sa[d] + ic[d]; |
| 318 | } |
| 319 | ra_[i] = c->op == OMEGA_AD_SUM ? (double)R[j] : (double)R[j] / n; |
| 320 | aa[i] = dabs(ra_[i]); |
| 321 | ka[i] = c->op == OMEGA_AD_SUM ? 0 : 1; |
| 322 | } |
| 323 | break; |
| 324 | } |
| 325 | default: break; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | static void gen_data(const OpCase *c, float *a, float *b) { |
| 330 | size_t na = numel(c->ra, c->sa), nb = c->has_b ? numel(c->rb, c->sb) : 0; |
| 331 | for (size_t i = 0; i < nb; i++) b[i] = urand(c->blo, c->bhi); |
| 332 | for (size_t i = 0; i < na; i++) { |
| 333 | switch (c->data) { |
| 334 | case DATA_AWAY_FROM_ZERO: { |
| 335 | float m = urand(0.1f, 1.0f); |
| 336 | a[i] = (urand(0, 1) < 0.5f) ? -m : m; |
| 337 | break; |
| 338 | } |
| 339 | case DATA_OFFSET_FROM_B: { |
| 340 | float m = urand(0.1f, 0.6f); |
| 341 | a[i] = b[i] + ((urand(0, 1) < 0.5f) ? -m : m); |
| 342 | break; |
| 343 | } |
| 344 | default: a[i] = urand(c->alo, c->ahi); |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * FD data set. With the FD seed R in [0.5, 1], every gradient element is a |
| 351 | * sum of same-sign terms of magnitude >= ~0.1, or exactly zero (MAX routing, |
| 352 | * where the perturbed forward is unchanged and fd is exactly 0 too), so the |
| 353 | * pure relative bound compares meaningful values. ADD/SUB/BROADCAST/SUM/MEAN/ |
| 354 | * TRANSPOSE/MAX gradients depend only on R; SQRT keeps a in [1, 4]; MUL, |
| 355 | * DIV and MATMUL get positive operands here. |
| 356 | */ |
| 357 | #define FD_RLO 0.5f |
| 358 | #define FD_RHI 1.0f |
| 359 | static void gen_fd_data(const OpCase *c, float *a, float *b) { |
| 360 | size_t na = numel(c->ra, c->sa), nb = c->has_b ? numel(c->rb, c->sb) : 0; |
| 361 | gen_data(c, a, b); |
| 362 | switch (c->op) { |
| 363 | case OMEGA_AD_MUL: case OMEGA_AD_MATMUL: |
| 364 | for (size_t i = 0; i < na; i++) a[i] = urand(0.5f, 1.0f); |
| 365 | for (size_t i = 0; i < nb; i++) b[i] = urand(0.5f, 1.0f); |
| 366 | break; |
| 367 | case OMEGA_AD_DIV: /* b stays in [1, 2] */ |
| 368 | for (size_t i = 0; i < na; i++) a[i] = urand(1.0f, 2.0f); |
| 369 | break; |
| 370 | default: break; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | /* Records the case on a fresh tape, seeds backward with R drawn from |
| 375 | * [rlo, rhi] (y's shape, written to R), reads the gradients of a (and b). */ |
| 376 | static int tape_grads(const OpCase *c, const float *a, const float *b, float rlo, float rhi, |
| 377 | float *R, size_t *ny, float *ga, float *gb) { |
| 378 | OmegaAdNode nodes[4]; |
| 379 | OmegaAdTape t; |
| 380 | float scratch[3 * MAXE]; |
| 381 | size_t na = numel(c->ra, c->sa), nb = c->has_b ? numel(c->rb, c->sb) : 0; |
| 382 | memset(&t, 0, sizeof(t)); |
| 383 | OmegaTensor ta = mk(c->ra, c->sa, a), tb = { 0, 0 }, ty = { 0, 0 }, tr = { 0, 0 }; |
| 384 | if (c->has_b) tb = mk(c->rb, c->sb, b); |
| 385 | OmegaTensorInfo yi; |
| 386 | memset(&yi, 0, sizeof(yi)); |
| 387 | uint32_t ia = 0, ib = 0, iy = 0; |
| 388 | int rc = omega_ad_tape_init(&t, g, nodes, 4, scratch, 3 * MAXE); |
| 389 | if (!rc) rc = build(c, &t, ta, tb, &ia, &ib, &iy); |
| 390 | if (!rc) rc = omega_ad_value(&t, iy, &ty); |
| 391 | if (!rc) rc = omega_tensor_info(g, ty, &yi); |
| 392 | if (!rc) { |
| 393 | *ny = (size_t)yi.elements; |
| 394 | for (size_t i = 0; i < *ny; i++) R[i] = urand(rlo, rhi); |
| 395 | tr = mk(yi.rank, yi.shape, R); |
| 396 | rc = omega_ad_backward_seed(&t, iy, tr); |
| 397 | omega_tensor_release(g, tr); |
| 398 | } |
| 399 | if (!rc) rc = omega_ad_grad_read_f32(&t, ia, ga, na); |
| 400 | if (!rc && c->has_b) rc = omega_ad_grad_read_f32(&t, ib, gb, nb); |
| 401 | omega_ad_tape_release(&t); |
| 402 | omega_tensor_release(g, ta); |
| 403 | if (c->has_b) omega_tensor_release(g, tb); |
| 404 | return rc; |
| 405 | } |
| 406 | |
| 407 | /* ---- bit-exact layer: each backward rule replayed with direct M20 calls ----------- */ |
| 408 | |
| 409 | static int rp_scalar(float v, OmegaTensor *out) { |
| 410 | uint64_t dummy = 1; |
| 411 | return omega_tensor_from_f32(g, 0, &dummy, &v, out); |
| 412 | } |
| 413 | |
| 414 | /* Reads x into out and releases x. */ |
| 415 | static int rp_take(OmegaTensor x, float *out, size_t n) { |
| 416 | int rc = omega_tensor_read_f32(g, x, out, n); |
| 417 | omega_tensor_release(g, x); |
| 418 | return rc; |
| 419 | } |
| 420 | |
| 421 | /* Sums `full` (consumed) back to shape (rt, st) in the tape's order: extra |
| 422 | * leading axes first (reduce SUM over axis 0, one at a time), then every |
| 423 | * size-1 axis of the target (reduce SUM, keepdims), and reads the result. */ |
| 424 | static int rp_out(OmegaTensor full, uint32_t rt, const uint64_t *st, float *out) { |
| 425 | OmegaTensor cur = full; |
| 426 | OmegaTensorInfo ci; |
| 427 | int rc = omega_tensor_info(g, cur, &ci); |
| 428 | while (!rc && ci.rank > rt) { |
| 429 | OmegaTensor nx; |
| 430 | rc = omega_tensor_reduce(g, OMEGA_TR_SUM, cur, 0, false, &nx); |
| 431 | omega_tensor_release(g, cur); |
| 432 | if (rc) return rc; |
| 433 | cur = nx; |
| 434 | rc = omega_tensor_info(g, cur, &ci); |
| 435 | } |
| 436 | for (uint32_t d = 0; !rc && d < rt; d++) { |
| 437 | if (st[d] == 1 && ci.shape[d] != 1) { |
| 438 | OmegaTensor nx; |
| 439 | rc = omega_tensor_reduce(g, OMEGA_TR_SUM, cur, d, true, &nx); |
| 440 | omega_tensor_release(g, cur); |
| 441 | if (rc) return rc; |
| 442 | cur = nx; |
| 443 | rc = omega_tensor_info(g, cur, &ci); |
| 444 | } |
| 445 | } |
| 446 | if (rc) { omega_tensor_release(g, cur); return rc; } |
| 447 | return rp_take(cur, out, numel(rt, st)); |
| 448 | } |
| 449 | |
| 450 | /* |
| 451 | * Expected gradients ea (and eb) for seed tg: the backward formula of the |
| 452 | * case's op computed here with the same M20 calls the tape is documented to |
| 453 | * make (omega_autodiff.h), in the same order. MAX routing is the documented |
| 454 | * host data move, with the routing rule written here independently. |
| 455 | */ |
| 456 | static int replay(const OpCase *c, OmegaTensor ta, OmegaTensor tb, OmegaTensor tg, |
| 457 | const OmegaTensorInfo *yi, float *ea, float *eb) { |
| 458 | OmegaTensor x = { 0, 0 }, x2 = { 0, 0 }, k = { 0, 0 }; |
| 459 | int rc = 0; |
| 460 | switch (c->op) { |
| 461 | case OMEGA_AD_ADD: case OMEGA_AD_SUB: |
| 462 | rc = omega_tensor_contiguous(g, tg, &x); |
| 463 | if (!rc) rc = rp_out(x, c->ra, c->sa, ea); |
| 464 | if (rc) return rc; |
| 465 | if (c->op == OMEGA_AD_ADD) { |
| 466 | rc = omega_tensor_contiguous(g, tg, &x); |
| 467 | } else { |
| 468 | rc = rp_scalar(-1.0f, &k); |
| 469 | if (rc) return rc; |
| 470 | rc = omega_tensor_binary(g, OMEGA_TB_MUL, tg, k, &x); |
| 471 | omega_tensor_release(g, k); |
| 472 | } |
| 473 | if (!rc) rc = rp_out(x, c->rb, c->sb, eb); |
| 474 | return rc; |
| 475 | case OMEGA_AD_MUL: |
| 476 | rc = omega_tensor_binary(g, OMEGA_TB_MUL, tg, tb, &x); |
| 477 | if (!rc) rc = rp_out(x, c->ra, c->sa, ea); |
| 478 | if (!rc) rc = omega_tensor_binary(g, OMEGA_TB_MUL, tg, ta, &x); |
| 479 | if (!rc) rc = rp_out(x, c->rb, c->sb, eb); |
| 480 | return rc; |
| 481 | case OMEGA_AD_DIV: { |
| 482 | OmegaTensor y = { 0, 0 }; |
| 483 | rc = omega_tensor_binary(g, OMEGA_TB_DIV, tg, tb, &x); /* g / b */ |
| 484 | if (!rc) rc = rp_out(x, c->ra, c->sa, ea); |
| 485 | if (rc) return rc; |
| 486 | rc = omega_tensor_binary(g, OMEGA_TB_DIV, ta, tb, &y); /* y = a / b */ |
| 487 | if (rc) return rc; |
| 488 | rc = omega_tensor_binary(g, OMEGA_TB_DIV, y, tb, &x2); /* y / b */ |
| 489 | omega_tensor_release(g, y); |
| 490 | if (rc) return rc; |
| 491 | rc = omega_tensor_binary(g, OMEGA_TB_MUL, tg, x2, &x); /* g * (y / b) */ |
| 492 | omega_tensor_release(g, x2); |
| 493 | if (rc) return rc; |
| 494 | rc = rp_scalar(-1.0f, &k); |
| 495 | if (rc) { omega_tensor_release(g, x); return rc; } |
| 496 | rc = omega_tensor_binary(g, OMEGA_TB_MUL, x, k, &x2); /* * -1 */ |
| 497 | omega_tensor_release(g, k); |
| 498 | omega_tensor_release(g, x); |
| 499 | if (!rc) rc = rp_out(x2, c->rb, c->sb, eb); |
| 500 | return rc; |
| 501 | } |
| 502 | case OMEGA_AD_MAX: { |
| 503 | size_t m = (size_t)yi->elements; |
| 504 | float va[MAXE], vb[MAXE], vg[MAXE], da[MAXE], db[MAXE]; |
| 505 | rc = omega_tensor_broadcast_to(g, ta, yi->rank, yi->shape, &x); |
| 506 | if (!rc) rc = rp_take(x, va, m); |
| 507 | if (!rc) rc = omega_tensor_broadcast_to(g, tb, yi->rank, yi->shape, &x); |
| 508 | if (!rc) rc = rp_take(x, vb, m); |
| 509 | if (!rc) rc = omega_tensor_read_f32(g, tg, vg, m); |
| 510 | if (rc) return rc; |
| 511 | for (size_t i = 0; i < m; i++) { |
| 512 | bool to_a = va[i] == va[i] && (vb[i] != vb[i] || va[i] > vb[i]); |
| 513 | da[i] = to_a ? vg[i] : 0.0f; |
| 514 | db[i] = to_a ? 0.0f : vg[i]; |
| 515 | } |
| 516 | rc = omega_tensor_from_f32(g, yi->rank, yi->shape, da, &x); |
| 517 | if (!rc) rc = rp_out(x, c->ra, c->sa, ea); |
| 518 | if (!rc) rc = omega_tensor_from_f32(g, yi->rank, yi->shape, db, &x); |
| 519 | if (!rc) rc = rp_out(x, c->rb, c->sb, eb); |
| 520 | return rc; |
| 521 | } |
| 522 | case OMEGA_AD_SQRT: { |
| 523 | OmegaTensor y = { 0, 0 }; |
| 524 | rc = omega_tensor_unary(g, OMEGA_TU_SQRT, ta, &y); |
| 525 | if (rc) return rc; |
| 526 | rc = omega_tensor_binary(g, OMEGA_TB_ADD, y, y, &x2); /* y + y */ |
| 527 | omega_tensor_release(g, y); |
| 528 | if (rc) return rc; |
| 529 | rc = omega_tensor_binary(g, OMEGA_TB_DIV, tg, x2, &x); /* g / (y + y) */ |
| 530 | omega_tensor_release(g, x2); |
| 531 | if (!rc) rc = rp_out(x, c->ra, c->sa, ea); |
| 532 | return rc; |
| 533 | } |
| 534 | case OMEGA_AD_MATMUL: |
| 535 | rc = omega_tensor_transpose(g, tb, &x2); |
| 536 | if (rc) return rc; |
| 537 | rc = omega_tensor_matmul(g, tg, x2, &x); /* g @ B^T */ |
| 538 | omega_tensor_release(g, x2); |
| 539 | if (!rc) rc = rp_out(x, c->ra, c->sa, ea); |
| 540 | if (rc) return rc; |
| 541 | rc = omega_tensor_transpose(g, ta, &x2); |
| 542 | if (rc) return rc; |
| 543 | rc = omega_tensor_matmul(g, x2, tg, &x); /* A^T @ g */ |
| 544 | omega_tensor_release(g, x2); |
| 545 | if (!rc) rc = rp_out(x, c->rb, c->sb, eb); |
| 546 | return rc; |
| 547 | case OMEGA_AD_TRANSPOSE: |
| 548 | rc = omega_tensor_transpose(g, tg, &x2); |
| 549 | if (rc) return rc; |
| 550 | rc = omega_tensor_contiguous(g, x2, &x); |
| 551 | omega_tensor_release(g, x2); |
| 552 | if (!rc) rc = rp_out(x, c->ra, c->sa, ea); |
| 553 | return rc; |
| 554 | case OMEGA_AD_BROADCAST: |
| 555 | rc = omega_tensor_contiguous(g, tg, &x); |
| 556 | if (!rc) rc = rp_out(x, c->ra, c->sa, ea); |
| 557 | return rc; |
| 558 | case OMEGA_AD_SUM: case OMEGA_AD_MEAN: { |
| 559 | uint64_t ks[4]; |
| 560 | OmegaTensor r = { 0, 0 }, bc = { 0, 0 }; |
| 561 | for (uint32_t d = 0; d < c->ra; d++) ks[d] = d == c->axis ? 1 : c->sa[d]; |
| 562 | rc = omega_tensor_reshape(g, tg, c->ra, ks, &r); |
| 563 | if (rc) return rc; |
| 564 | rc = omega_tensor_broadcast_to(g, r, c->ra, c->sa, &bc); |
| 565 | if (rc) { omega_tensor_release(g, r); return rc; } |
| 566 | rc = omega_tensor_contiguous(g, bc, &x2); |
| 567 | omega_tensor_release(g, bc); |
| 568 | omega_tensor_release(g, r); |
| 569 | if (rc) return rc; |
| 570 | if (c->op == OMEGA_AD_SUM) return rp_out(x2, c->ra, c->sa, ea); |
| 571 | rc = rp_scalar((float)c->sa[c->axis], &k); |
| 572 | if (rc) { omega_tensor_release(g, x2); return rc; } |
| 573 | rc = omega_tensor_binary(g, OMEGA_TB_DIV, x2, k, &x); /* / n */ |
| 574 | omega_tensor_release(g, k); |
| 575 | omega_tensor_release(g, x2); |
| 576 | if (!rc) rc = rp_out(x, c->ra, c->sa, ea); |
| 577 | return rc; |
| 578 | } |
| 579 | default: |
| 580 | return OMEGA_AD_ERR_BAD_ARGS; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | static void test_op_case(const OpCase *c) { |
| 585 | float a[MAXE], b[MAXE], R[MAXE], ga[MAXE], gb[MAXE]; |
| 586 | double ra_[MAXE], aa[MAXE], rb_[MAXE], ab[MAXE]; |
| 587 | int ka[MAXE], kb[MAXE]; |
| 588 | size_t na = numel(c->ra, c->sa), nb = c->has_b ? numel(c->rb, c->sb) : 0; |
| 589 | gen_data(c, a, b); |
| 590 | |
| 591 | OmegaAdNode nodes[4]; |
| 592 | OmegaAdTape t; |
| 593 | float scratch[3 * MAXE]; |
| 594 | OmegaTensor ta = mk(c->ra, c->sa, a), tb = { 0, 0 }; |
| 595 | if (c->has_b) tb = mk(c->rb, c->sb, b); |
| 596 | int rc = omega_ad_tape_init(&t, g, nodes, 4, scratch, sizeof(scratch) / sizeof(scratch[0])); |
| 597 | uint32_t ia = 0, ib = 0, iy = 0; |
| 598 | if (!rc) rc = build(c, &t, ta, tb, &ia, &ib, &iy); |
| 599 | CHECK(rc == 0, "%s: record rc %d", c->name, rc); |
| 600 | if (rc) { omega_ad_tape_release(&t); goto out; } |
| 601 | OmegaTensor ty = { 0, 0 }; |
| 602 | OmegaTensorInfo yi; |
| 603 | memset(&yi, 0, sizeof(yi)); |
| 604 | omega_ad_value(&t, iy, &ty); |
| 605 | omega_tensor_info(g, ty, &yi); |
| 606 | size_t ny = (size_t)yi.elements; |
| 607 | for (size_t i = 0; i < ny; i++) R[i] = urand(-1, 1); |
| 608 | OmegaTensor tr = mk(yi.rank, yi.shape, R); |
| 609 | rc = omega_ad_backward_seed(&t, iy, tr); |
| 610 | CHECK(rc == 0, "%s: backward rc %d", c->name, rc); |
| 611 | int r1 = omega_ad_grad_read_f32(&t, ia, ga, na); |
| 612 | int r2 = c->has_b ? omega_ad_grad_read_f32(&t, ib, gb, nb) : 0; |
| 613 | CHECK(r1 == 0 && r2 == 0, "%s: grad read rc %d %d (grad shape must equal input shape)", c->name, r1, r2); |
| 614 | omega_ad_tape_release(&t); |
| 615 | if (rc || r1 || r2) { omega_tensor_release(g, tr); goto out; } |
| 616 | |
| 617 | /* bit-exact (approved M21 gate): same M20 ops called directly, same bits */ |
| 618 | float ea[MAXE], eb[MAXE]; |
| 619 | int rr = replay(c, ta, tb, tr, &yi, ea, eb); |
| 620 | omega_tensor_release(g, tr); |
| 621 | CHECK(rr == 0, "%s: bit-exact replay rc %d", c->name, rr); |
| 622 | if (!rr) { |
| 623 | CHECK(memcmp(ga, ea, na * sizeof(float)) == 0, |
| 624 | "%s: grad a not bit-exact with the direct M20 replay", c->name); |
| 625 | if (c->has_b) |
| 626 | CHECK(memcmp(gb, eb, nb * sizeof(float)) == 0, |
| 627 | "%s: grad b not bit-exact with the direct M20 replay", c->name); |
| 628 | if (c == &CASES[0]) { /* counterexample: a one-bit change must be seen */ |
| 629 | uint32_t bits; |
| 630 | memcpy(&bits, &ea[0], 4); bits ^= 1u; memcpy(&ea[0], &bits, 4); |
| 631 | CHECK(memcmp(ga, ea, na * sizeof(float)) != 0, "bit-exact comparator missed a one-bit difference"); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | /* 1. contract */ |
| 636 | reference(c, a, b, R, yi.rank, yi.shape, ra_, aa, ka, rb_, ab, kb); |
| 637 | size_t first = 0, bad = bound_violations(ga, ra_, aa, ka, na, &first); |
| 638 | CHECK(bad == 0, "%s: grad a outside contract at %zu elements (first %zu: %.9g vs %.9g)", c->name, bad, |
| 639 | first, (double)ga[first], ra_[first]); |
| 640 | if (c->has_b) { |
| 641 | bad = bound_violations(gb, rb_, ab, kb, nb, &first); |
| 642 | CHECK(bad == 0, "%s: grad b outside contract at %zu elements (first %zu: %.9g vs %.9g)", c->name, |
| 643 | bad, first, (double)gb[first], rb_[first]); |
| 644 | } |
| 645 | |
| 646 | /* central finite differences of the CPU forward (approved M21 gate), on |
| 647 | * the FD data set: a fresh tape, its own inputs and a positive seed */ |
| 648 | float fa[MAXE], fb[MAXE], fR[MAXE], fga[MAXE], fgb[MAXE]; |
| 649 | size_t fny = 0; |
| 650 | gen_fd_data(c, fa, fb); |
| 651 | int rf = tape_grads(c, fa, fb, FD_RLO, FD_RHI, fR, &fny, fga, fgb); |
| 652 | CHECK(rf == 0, "%s: FD data set tape rc %d", c->name, rf); |
| 653 | if (rf) goto out; |
| 654 | for (int which = 0; which < (c->has_b ? 2 : 1); which++) { |
| 655 | float *x = which ? fb : fa; |
| 656 | const float *an = which ? fgb : fga; |
| 657 | size_t nx = which ? nb : na, fbad = 0, ffirst = 0; |
| 658 | double fdv = 0; |
| 659 | for (size_t i = 0; i < nx; i++) { |
| 660 | float x0 = x[i], xp = (float)(x0 + FD_H), xm = (float)(x0 - FD_H); |
| 661 | double Lp = 0, Lm = 0; |
| 662 | x[i] = xp; |
| 663 | int e1 = forward_loss(c, fa, fb, fR, fny, &Lp); |
| 664 | x[i] = xm; |
| 665 | int e2 = forward_loss(c, fa, fb, fR, fny, &Lm); |
| 666 | x[i] = x0; |
| 667 | double fd = (Lp - Lm) / ((double)xp - (double)xm); |
| 668 | if (e1 || e2 || !fd_ok((double)an[i], fd)) { |
| 669 | if (!fbad) { ffirst = i; fdv = fd; } |
| 670 | fbad++; |
| 671 | } |
| 672 | } |
| 673 | CHECK(fbad == 0, "%s: grad %c disagrees with central FD at %zu elements (first %zu: %.9g vs fd %.9g)", |
| 674 | c->name, which ? 'b' : 'a', fbad, ffirst, (double)an[ffirst], fdv); |
| 675 | } |
| 676 | out: |
| 677 | omega_tensor_release(g, ta); |
| 678 | if (c->has_b) omega_tensor_release(g, tb); |
| 679 | } |
| 680 | |
| 681 | /* Counterexamples: the comparators reject a wrong gradient. */ |
| 682 | static void test_comparators_reject(void) { |
| 683 | const OpCase *c = &CASES[5]; /* mul [3,4]*[1,4] */ |
| 684 | float a[MAXE], b[MAXE], R[MAXE], wrong[MAXE]; |
| 685 | double ra_[MAXE], aa[MAXE], rb_[MAXE], ab[MAXE]; |
| 686 | int ka[MAXE], kb[MAXE]; |
| 687 | gen_data(c, a, b); |
| 688 | uint64_t sy[2] = { 3, 4 }; |
| 689 | for (int i = 0; i < 12; i++) R[i] = urand(-1, 1); |
| 690 | reference(c, a, b, R, 2, sy, ra_, aa, ka, rb_, ab, kb); |
| 691 | /* off by one float ulp-ish: 1 + 2^-20 relative, far above gamma(1) */ |
| 692 | for (int i = 0; i < 12; i++) wrong[i] = (float)(ra_[i] * (1.0 + 1.0 / 1048576.0)); |
| 693 | size_t first = 0; |
| 694 | CHECK(bound_violations(wrong, ra_, aa, ka, 12, &first) > 0, "contract comparator accepted a perturbed gradient"); |
| 695 | for (int i = 0; i < 12; i++) wrong[i] = (float)ra_[i]; |
| 696 | CHECK(bound_violations(wrong, ra_, aa, ka, 12, &first) == 0, "contract comparator rejected the exact gradient"); |
| 697 | /* FD comparator (pure 1e-3 relative), on the FD data set: it accepts the |
| 698 | * exact gradient, and rejects a sign-flipped one and one 2e-3 too large */ |
| 699 | gen_fd_data(c, a, b); |
| 700 | for (int i = 0; i < 12; i++) R[i] = urand(FD_RLO, FD_RHI); |
| 701 | reference(c, a, b, R, 2, sy, ra_, aa, ka, rb_, ab, kb); |
| 702 | size_t fbad = 0, facc = 0, fbig = 0; |
| 703 | for (size_t i = 0; i < 12; i++) { |
| 704 | float x0 = a[i], xp = (float)(x0 + FD_H), xm = (float)(x0 - FD_H); |
| 705 | double Lp = 0, Lm = 0; |
| 706 | a[i] = xp; forward_loss(c, a, b, R, 12, &Lp); |
| 707 | a[i] = xm; forward_loss(c, a, b, R, 12, &Lm); |
| 708 | a[i] = x0; |
| 709 | double fd = (Lp - Lm) / ((double)xp - (double)xm); |
| 710 | if (fd_ok(ra_[i], fd)) facc++; |
| 711 | if (!fd_ok(-ra_[i], fd)) fbad++; |
| 712 | if (!fd_ok(ra_[i] * 1.002, fd)) fbig++; |
| 713 | } |
| 714 | CHECK(facc == 12, "FD comparator rejected the exact gradient at %zu of 12 elements", 12 - facc); |
| 715 | CHECK(fbad > 0, "FD comparator accepted a sign-flipped gradient"); |
| 716 | CHECK(fbig == 12, "FD comparator accepted a gradient 2e-3 too large at %zu of 12 elements", 12 - fbig); |
| 717 | } |
| 718 | |
| 719 | /* MAX ties go to b (relu gradient 0 at 0). */ |
| 720 | static void test_max_ties(void) { |
| 721 | uint64_t s[1] = { 4 }; |
| 722 | float a[4] = { 0.5f, -0.25f, 0.0f, -0.0f }, b[4] = { 0.5f, -0.25f, -0.0f, 0.0f }, R[4] = { 1, 2, 3, 4 }; |
| 723 | OmegaAdNode nodes[3]; |
| 724 | OmegaAdTape t; |
| 725 | float scratch[12], ga[4] = { 9, 9, 9, 9 }, gb[4] = { 9, 9, 9, 9 }; |
| 726 | OmegaTensor ta = mk(1, s, a), tb = mk(1, s, b), tr = mk(1, s, R); |
| 727 | uint32_t ia = 0, ib = 0, iy = 0; |
| 728 | int rc = omega_ad_tape_init(&t, g, nodes, 3, scratch, 12); |
| 729 | if (!rc) rc = omega_ad_leaf(&t, ta, true, &ia); |
| 730 | if (!rc) rc = omega_ad_leaf(&t, tb, true, &ib); |
| 731 | if (!rc) rc = omega_ad_binary(&t, OMEGA_AD_MAX, ia, ib, &iy); |
| 732 | if (!rc) rc = omega_ad_backward_seed(&t, iy, tr); |
| 733 | if (!rc) rc = omega_ad_grad_read_f32(&t, ia, ga, 4); |
| 734 | if (!rc) rc = omega_ad_grad_read_f32(&t, ib, gb, 4); |
| 735 | CHECK(rc == 0, "max ties: rc %d", rc); |
| 736 | bool ok = true; |
| 737 | for (int i = 0; i < 4; i++) ok = ok && ga[i] == 0.0f && gb[i] == R[i]; |
| 738 | CHECK(ok, "max ties: gradient must go to b (ga %g %g %g %g, gb %g %g %g %g)", |
| 739 | (double)ga[0], (double)ga[1], (double)ga[2], (double)ga[3], |
| 740 | (double)gb[0], (double)gb[1], (double)gb[2], (double)gb[3]); |
| 741 | omega_ad_tape_release(&t); |
| 742 | omega_tensor_release(g, ta); omega_tensor_release(g, tb); omega_tensor_release(g, tr); |
| 743 | } |
| 744 | |
| 745 | /* ---- composite: linear layer + squared error ------------------------------------------- */ |
| 746 | |
| 747 | #define CX_N 4 |
| 748 | #define CX_IN 3 |
| 749 | #define CX_OUT 2 |
| 750 | |
| 751 | typedef struct { |
| 752 | float X[CX_N * CX_IN], W[CX_IN * CX_OUT], bias[CX_OUT], Y[CX_N * CX_OUT]; |
| 753 | } LinData; |
| 754 | |
| 755 | /* loss = mean_i( sum_j ( (X W + bias - Y)_ij ^2 ) ). d is used twice: fan-out. */ |
| 756 | /* loss_d (optional): the same loss computed in double from the tape's float d |
| 757 | * values, mean_i sum_j d_ij^2, for the FD layer. */ |
| 758 | static int lin_run(OmegaTensorCtx *ctx, const LinData *D, float *loss, float *flat_grads, |
| 759 | uint8_t loss_id[32], double *loss_d) { |
| 760 | uint64_t sx[2] = { CX_N, CX_IN }, sw[2] = { CX_IN, CX_OUT }, sb[1] = { CX_OUT }, sy[2] = { CX_N, CX_OUT }; |
| 761 | OmegaTensor tx, tw, tb, ty; |
| 762 | int rc = omega_tensor_from_f32(ctx, 2, sx, D->X, &tx); |
| 763 | if (!rc) rc = omega_tensor_from_f32(ctx, 2, sw, D->W, &tw); |
| 764 | if (!rc) rc = omega_tensor_from_f32(ctx, 1, sb, D->bias, &tb); |
| 765 | if (!rc) rc = omega_tensor_from_f32(ctx, 2, sy, D->Y, &ty); |
| 766 | if (rc) return rc; |
| 767 | OmegaAdNode nodes[16]; |
| 768 | OmegaAdTape t; |
| 769 | uint32_t ix = 0, iw = 0, ib = 0, iy = 0, p = 0, q = 0, d = 0, s = 0, r = 0, l = 0; |
| 770 | rc = omega_ad_tape_init(&t, ctx, nodes, 16, NULL, 0); |
| 771 | if (!rc) rc = omega_ad_leaf(&t, tx, false, &ix); |
| 772 | if (!rc) rc = omega_ad_leaf(&t, tw, true, &iw); |
| 773 | if (!rc) rc = omega_ad_leaf(&t, tb, true, &ib); |
| 774 | if (!rc) rc = omega_ad_leaf(&t, ty, false, &iy); |
| 775 | if (!rc) rc = omega_ad_matmul(&t, ix, iw, &p); |
| 776 | if (!rc) rc = omega_ad_binary(&t, OMEGA_AD_ADD, p, ib, &q); |
| 777 | if (!rc) rc = omega_ad_binary(&t, OMEGA_AD_SUB, q, iy, &d); |
| 778 | if (!rc) rc = omega_ad_binary(&t, OMEGA_AD_MUL, d, d, &s); |
| 779 | if (!rc) rc = omega_ad_reduce(&t, OMEGA_AD_SUM, s, 1, false, &r); |
| 780 | if (!rc) rc = omega_ad_reduce(&t, OMEGA_AD_MEAN, r, 0, false, &l); |
| 781 | OmegaTensor tl = { 0, 0 }; |
| 782 | if (!rc) rc = omega_ad_value(&t, l, &tl); |
| 783 | if (!rc) rc = omega_tensor_read_f32(ctx, tl, loss, 1); |
| 784 | if (!rc && loss_id) rc = omega_tensor_value_id(ctx, tl, loss_id); |
| 785 | if (!rc && loss_d) { |
| 786 | OmegaTensor td = { 0, 0 }; |
| 787 | float dv[CX_N * CX_OUT]; |
| 788 | rc = omega_ad_value(&t, d, &td); |
| 789 | if (!rc) rc = omega_tensor_read_f32(ctx, td, dv, CX_N * CX_OUT); |
| 790 | if (!rc) { |
| 791 | double acc = 0; |
| 792 | for (int i = 0; i < CX_N * CX_OUT; i++) acc += (double)dv[i] * (double)dv[i]; |
| 793 | *loss_d = acc / CX_N; |
| 794 | } |
| 795 | } |
| 796 | if (!rc && flat_grads) { |
| 797 | rc = omega_ad_backward(&t, l); |
| 798 | uint32_t ids[2] = { iw, ib }; |
| 799 | if (!rc) rc = omega_ad_grads_flatten(&t, ids, 2, flat_grads, CX_IN * CX_OUT + CX_OUT); |
| 800 | if (!rc) { |
| 801 | OmegaTensor gx; |
| 802 | int e1 = omega_ad_grad(&t, ix, &gx), e2 = omega_ad_grad(&t, iy, &gx), e3 = omega_ad_grad(&t, iw, &gx); |
| 803 | if (e1 != OMEGA_AD_ERR_NO_GRAD || e2 != OMEGA_AD_ERR_NO_GRAD || e3 != 0) rc = 1000; |
| 804 | } |
| 805 | } |
| 806 | omega_ad_tape_release(&t); |
| 807 | omega_tensor_release(ctx, tx); omega_tensor_release(ctx, tw); |
| 808 | omega_tensor_release(ctx, tb); omega_tensor_release(ctx, ty); |
| 809 | return rc; |
| 810 | } |
| 811 | |
| 812 | static void lin_data(LinData *D) { |
| 813 | for (int i = 0; i < CX_N * CX_IN; i++) D->X[i] = urand(-1, 1); |
| 814 | for (int i = 0; i < CX_IN * CX_OUT; i++) D->W[i] = urand(-1, 1); |
| 815 | for (int i = 0; i < CX_OUT; i++) D->bias[i] = urand(-1, 1); |
| 816 | for (int i = 0; i < CX_N * CX_OUT; i++) D->Y[i] = urand(-1, 1); |
| 817 | } |
| 818 | |
| 819 | /* FD data set: X, W, bias in [0.5, 1], Y in [-0.5, 0], so E = XW + b - Y lies |
| 820 | * in [1.25, 4.5] and every gradient, (2/N) X^T E and (2/N) sum_i E, is >= 1.25: |
| 821 | * no near-zero gradient element for the pure relative FD bound. */ |
| 822 | static void lin_data_fd(LinData *D) { |
| 823 | for (int i = 0; i < CX_N * CX_IN; i++) D->X[i] = urand(0.5f, 1.0f); |
| 824 | for (int i = 0; i < CX_IN * CX_OUT; i++) D->W[i] = urand(0.5f, 1.0f); |
| 825 | for (int i = 0; i < CX_OUT; i++) D->bias[i] = urand(0.5f, 1.0f); |
| 826 | for (int i = 0; i < CX_N * CX_OUT; i++) D->Y[i] = urand(-0.5f, 0.0f); |
| 827 | } |
| 828 | |
| 829 | static void test_composite(void) { |
| 830 | LinData D; |
| 831 | lin_data(&D); |
| 832 | float loss, grads[CX_IN * CX_OUT + CX_OUT]; |
| 833 | int rc = lin_run(g, &D, &loss, grads, NULL, NULL); |
| 834 | CHECK(rc == 0, "composite: rc %d (incl. no grad for X, Y; grad for W)", rc); |
| 835 | if (rc) return; |
| 836 | /* exact reference in double: dL/dW = (2/N) X^T E, dL/db = (2/N) sum_i E, E = XW + b - Y */ |
| 837 | double E[CX_N * CX_OUT]; |
| 838 | for (int i = 0; i < CX_N; i++) |
| 839 | for (int j = 0; j < CX_OUT; j++) { |
| 840 | double s = D.bias[j]; |
| 841 | for (int k = 0; k < CX_IN; k++) s += (double)D.X[i * CX_IN + k] * D.W[k * CX_OUT + j]; |
| 842 | E[i * CX_OUT + j] = s - D.Y[i * CX_OUT + j]; |
| 843 | } |
| 844 | size_t bad_ref = 0; |
| 845 | for (int k = 0; k < CX_IN; k++) |
| 846 | for (int j = 0; j < CX_OUT; j++) { |
| 847 | double s = 0; |
| 848 | for (int i = 0; i < CX_N; i++) s += D.X[i * CX_IN + k] * E[i * CX_OUT + j]; |
| 849 | s *= 2.0 / CX_N; |
| 850 | if (!(dabs(grads[k * CX_OUT + j] - s) <= 1e-5 + 1e-5 * dabs(s))) bad_ref++; |
| 851 | } |
| 852 | for (int j = 0; j < CX_OUT; j++) { |
| 853 | double s = 0; |
| 854 | for (int i = 0; i < CX_N; i++) s += E[i * CX_OUT + j]; |
| 855 | s *= 2.0 / CX_N; |
| 856 | if (!(dabs(grads[CX_IN * CX_OUT + j] - s) <= 1e-5 + 1e-5 * dabs(s))) bad_ref++; |
| 857 | } |
| 858 | CHECK(bad_ref == 0, "composite: %zu gradients differ from the closed form (2/N) X^T E", bad_ref); |
| 859 | /* central FD on every parameter (W then bias, the flatten order), pure |
| 860 | * 1e-3 relative (approved M21 gate), on the FD data set lin_data_fd */ |
| 861 | LinData F; |
| 862 | lin_data_fd(&F); |
| 863 | float floss, fgrads[CX_IN * CX_OUT + CX_OUT]; |
| 864 | rc = lin_run(g, &F, &floss, fgrads, NULL, NULL); |
| 865 | CHECK(rc == 0, "composite FD data set: rc %d", rc); |
| 866 | if (rc) return; |
| 867 | size_t fbad = 0; |
| 868 | float *params[2] = { F.W, F.bias }; |
| 869 | int counts[2] = { CX_IN * CX_OUT, CX_OUT }; |
| 870 | for (int pnum = 0, off = 0; pnum < 2; off += counts[pnum], pnum++) |
| 871 | for (int i = 0; i < counts[pnum]; i++) { |
| 872 | float *x = ¶ms[pnum][i], x0 = *x, xp = (float)(x0 + FD_H), xm = (float)(x0 - FD_H); |
| 873 | float Lpf = 0, Lmf = 0; |
| 874 | double Lp = 0, Lm = 0; |
| 875 | *x = xp; int e1 = lin_run(g, &F, &Lpf, NULL, NULL, &Lp); |
| 876 | *x = xm; int e2 = lin_run(g, &F, &Lmf, NULL, NULL, &Lm); |
| 877 | *x = x0; |
| 878 | double fd = (Lp - Lm) / ((double)xp - (double)xm); |
| 879 | if (e1 || e2 || !fd_ok((double)fgrads[off + i], fd)) fbad++; |
| 880 | } |
| 881 | CHECK(fbad == 0, "composite: %zu parameter gradients disagree with central FD", fbad); |
| 882 | } |
| 883 | |
| 884 | /* Two full runs in two contexts: bit-identical gradients and loss value id. */ |
| 885 | static void test_determinism(void) { |
| 886 | LinData D; |
| 887 | lin_data(&D); |
| 888 | OmegaTensorCtx *c2 = NULL; |
| 889 | int rc = omega_tensor_ctx_create(1024, omega_tensor_cpu_realization(), &c2); |
| 890 | CHECK(rc == 0, "determinism: second ctx rc %d", rc); |
| 891 | if (rc) return; |
| 892 | float l1, l2, g1[8], g2[8]; |
| 893 | uint8_t id1[32], id2[32]; |
| 894 | int r1 = lin_run(g, &D, &l1, g1, id1, NULL), r2 = lin_run(c2, &D, &l2, g2, id2, NULL); |
| 895 | CHECK(r1 == 0 && r2 == 0, "determinism: runs rc %d %d", r1, r2); |
| 896 | CHECK(memcmp(g1, g2, sizeof(g1)) == 0, "determinism: gradients differ between two runs"); |
| 897 | CHECK(memcmp(id1, id2, 32) == 0, "determinism: loss value id differs between two runs"); |
| 898 | /* counterexample: one flipped low bit must be seen */ |
| 899 | float g3[8]; |
| 900 | memcpy(g3, g1, sizeof(g1)); |
| 901 | uint32_t bits; |
| 902 | memcpy(&bits, &g3[3], 4); bits ^= 1u; memcpy(&g3[3], &bits, 4); |
| 903 | CHECK(memcmp(g1, g3, sizeof(g1)) != 0, "determinism comparator missed a one-bit difference"); |
| 904 | uint32_t lt, ls; |
| 905 | omega_tensor_live_counts(c2, <, &ls); |
| 906 | CHECK(lt == 0 && ls == 0, "determinism: second ctx leaked %u tensors %u storages", lt, ls); |
| 907 | omega_tensor_ctx_destroy(c2); |
| 908 | } |
| 909 | |
| 910 | /* ---- error paths ------------------------------------------------------------------------- */ |
| 911 | |
| 912 | static void live(uint32_t *lt, uint32_t *ls) { omega_tensor_live_counts(g, lt, ls); } |
| 913 | |
| 914 | static void test_errors(void) { |
| 915 | OmegaAdNode nodes[3]; |
| 916 | OmegaAdTape t; |
| 917 | uint64_t s34[2] = { 3, 4 }, s5[1] = { 5 }, s4[1] = { 4 }, s35[2] = { 3, 5 }, s42[2] = { 4, 2 }; |
| 918 | float d[20]; |
| 919 | for (int i = 0; i < 20; i++) d[i] = urand(-1, 1); |
| 920 | OmegaTensor a = mk(2, s34, d), b5 = mk(1, s5, d), b4 = mk(1, s4, d), m35 = mk(2, s35, d), m42 = mk(2, s42, d); |
| 921 | |
| 922 | /* init refusals, with the valid call as counterexample */ |
| 923 | CHECK(omega_ad_tape_init(&t, g, nodes, 0, NULL, 0) == OMEGA_AD_ERR_BAD_ARGS, "capacity 0 accepted"); |
| 924 | CHECK(omega_ad_tape_init(&t, g, NULL, 3, NULL, 0) == OMEGA_AD_ERR_BAD_ARGS, "NULL nodes accepted"); |
| 925 | CHECK(omega_ad_tape_init(&t, g, nodes, 3, NULL, 5) == OMEGA_AD_ERR_BAD_ARGS, "NULL scratch with count accepted"); |
| 926 | CHECK(omega_ad_tape_init(&t, g, nodes, 3, NULL, 0) == 0, "valid init refused"); |
| 927 | |
| 928 | /* tape capacity: 3 nodes fit, the 4th is refused and nothing leaks */ |
| 929 | uint32_t ia = 0, ib = 0, iy = 0, iz = 77; |
| 930 | int rc = omega_ad_leaf(&t, a, true, &ia); |
| 931 | if (!rc) rc = omega_ad_leaf(&t, b4, true, &ib); |
| 932 | if (!rc) rc = omega_ad_binary(&t, OMEGA_AD_ADD, ia, ib, &iy); |
| 933 | CHECK(rc == 0 && t.count == 3, "3 nodes on a capacity-3 tape: rc %d count %u", rc, t.count); |
| 934 | uint32_t lt0, ls0, lt1, ls1; |
| 935 | live(<0, &ls0); |
| 936 | rc = omega_ad_binary(&t, OMEGA_AD_ADD, iy, ib, &iz); |
| 937 | live(<1, &ls1); |
| 938 | CHECK(rc == OMEGA_AD_ERR_TAPE_FULL, "full tape: rc %d, want TAPE_FULL", rc); |
| 939 | CHECK(t.count == 3 && iz == 77 && lt0 == lt1 && ls0 == ls1, "full tape changed state (count %u, live %u->%u)", |
| 940 | t.count, lt0, lt1); |
| 941 | CHECK(omega_ad_leaf(&t, a, true, &iz) == OMEGA_AD_ERR_TAPE_FULL, "leaf on full tape accepted"); |
| 942 | omega_ad_tape_release(&t); |
| 943 | |
| 944 | /* shape mismatches: tensor error passed through, tape and ctx unchanged */ |
| 945 | OmegaAdNode n2[8]; |
| 946 | float scratch[6]; |
| 947 | omega_ad_tape_init(&t, g, n2, 8, scratch, 6); |
| 948 | uint32_t i35 = 0, i42 = 0, i5 = 0; |
| 949 | omega_ad_leaf(&t, a, true, &ia); |
| 950 | omega_ad_leaf(&t, b5, true, &i5); |
| 951 | omega_ad_leaf(&t, m35, true, &i35); |
| 952 | omega_ad_leaf(&t, m42, true, &i42); |
| 953 | CHECK(omega_ad_sqrt(&t, 99, &iz) == OMEGA_AD_ERR_BAD_ARGS, "node id 99 accepted"); |
| 954 | live(<0, &ls0); |
| 955 | uint32_t before = t.count; |
| 956 | int e1 = omega_ad_binary(&t, OMEGA_AD_ADD, ia, i5, &iz); |
| 957 | int e2 = omega_ad_matmul(&t, i35, i42, &iz); |
| 958 | uint64_t s4b[1] = { 4 }; |
| 959 | int e3 = omega_ad_broadcast(&t, i5, 1, s4b, &iz); |
| 960 | int e4 = omega_ad_reduce(&t, OMEGA_AD_SUM, ia, 2, false, &iz); |
| 961 | int e5 = omega_ad_binary(&t, OMEGA_AD_MATMUL, ia, ia, &iz); |
| 962 | live(<1, &ls1); |
| 963 | CHECK(e1 == OMEGA_TENSOR_ERR_SHAPE, "[3,4]+[5]: rc %d, want TENSOR_ERR_SHAPE", e1); |
| 964 | CHECK(e2 == OMEGA_TENSOR_ERR_SHAPE, "[3,5]x[4,2]: rc %d, want TENSOR_ERR_SHAPE", e2); |
| 965 | CHECK(e3 == OMEGA_TENSOR_ERR_SHAPE, "broadcast [5]->[4]: rc %d, want TENSOR_ERR_SHAPE", e3); |
| 966 | CHECK(e4 == OMEGA_TENSOR_ERR_AXIS, "reduce axis 2 of rank 2: rc %d, want TENSOR_ERR_AXIS", e4); |
| 967 | CHECK(e5 == OMEGA_AD_ERR_BAD_ARGS, "binary with MATMUL op: rc %d, want BAD_ARGS", e5); |
| 968 | CHECK(t.count == before && lt0 == lt1 && ls0 == ls1, "refused ops changed the tape or leaked"); |
| 969 | uint32_t imm = 0; |
| 970 | CHECK(omega_ad_matmul(&t, i35, i35, &imm) == OMEGA_TENSOR_ERR_SHAPE, "[3,5]x[3,5] accepted"); |
| 971 | /* counterexample: compatible shapes record (MAX below) */ |
| 972 | uint32_t imax = 0; |
| 973 | rc = omega_ad_binary(&t, OMEGA_AD_MAX, ia, ia, &imax); /* [3,4]: backward needs 36 scratch floats */ |
| 974 | CHECK(rc == 0, "max record rc %d", rc); |
| 975 | /* seed / root shape (enough scratch, so only the shape can refuse) */ |
| 976 | float big[36]; |
| 977 | t.scratch = big; t.scratch_count = 36; |
| 978 | OmegaTensor seed34 = mk(2, s34, d); |
| 979 | CHECK(omega_ad_backward(&t, imax) == OMEGA_AD_ERR_SHAPE, "scalar seed accepted for a [3,4] root"); |
| 980 | CHECK(omega_ad_backward_seed(&t, imax, b4) == OMEGA_AD_ERR_SHAPE, "seed [4] accepted for root [3,4]"); |
| 981 | /* scratch: 6 floats for MAX [3,4] (needs 36) is refused */ |
| 982 | t.scratch = scratch; t.scratch_count = 6; |
| 983 | CHECK(omega_ad_backward_seed(&t, imax, seed34) == OMEGA_AD_ERR_SCRATCH, "6 floats of scratch accepted for MAX [3,4]"); |
| 984 | size_t need = 0; |
| 985 | CHECK(omega_ad_scratch_need(&t, &need) == 0 && need == 36, "scratch need %zu, want 36", need); |
| 986 | t.scratch = big; t.scratch_count = 36; |
| 987 | CHECK(!t.backward_done, "refused backward marked the tape done"); |
| 988 | rc = omega_ad_backward_seed(&t, imax, seed34); |
| 989 | CHECK(rc == 0, "valid seed refused: rc %d", rc); |
| 990 | CHECK(omega_ad_backward_seed(&t, imax, seed34) == OMEGA_AD_ERR_STATE, "second backward accepted"); |
| 991 | CHECK(omega_ad_sqrt(&t, ia, &iz) == OMEGA_AD_ERR_STATE, "recording after backward accepted"); |
| 992 | OmegaTensor gg; |
| 993 | CHECK(omega_ad_grad(&t, ia, &gg) == 0, "max input has no grad"); |
| 994 | CHECK(omega_ad_grad(&t, i35, &gg) == OMEGA_AD_ERR_NO_GRAD, "unreached leaf has a grad"); |
| 995 | float tiny[11]; |
| 996 | CHECK(omega_ad_grad_read_f32(&t, ia, tiny, 11) == OMEGA_AD_ERR_COUNT, "short grad read accepted"); |
| 997 | /* flatten: exact count only, nothing written otherwise */ |
| 998 | float flat[13]; |
| 999 | for (int i = 0; i < 13; i++) flat[i] = 7.0f; |
| 1000 | uint32_t one_id[1] = { ia }; |
| 1001 | CHECK(omega_ad_grads_flatten(&t, one_id, 1, flat, 13) == OMEGA_AD_ERR_COUNT, "flatten count 13 for 12 accepted"); |
| 1002 | bool untouched = true; |
| 1003 | for (int i = 0; i < 13; i++) untouched = untouched && flat[i] == 7.0f; |
| 1004 | CHECK(untouched, "refused flatten wrote the buffer"); |
| 1005 | CHECK(omega_ad_grads_flatten(&t, one_id, 1, flat, 12) == 0, "flatten count 12 refused"); |
| 1006 | float direct[12]; |
| 1007 | omega_ad_grad_read_f32(&t, ia, direct, 12); |
| 1008 | CHECK(memcmp(direct, flat, sizeof(direct)) == 0, "flatten differs from grad read"); |
| 1009 | omega_ad_tape_release(&t); |
| 1010 | omega_tensor_release(g, seed34); |
| 1011 | |
| 1012 | /* root without grad; F16 leaf refused */ |
| 1013 | omega_ad_tape_init(&t, g, nodes, 3, NULL, 0); |
| 1014 | omega_ad_leaf(&t, a, false, &ia); |
| 1015 | rc = omega_ad_binary(&t, OMEGA_AD_ADD, ia, ia, &iy); |
| 1016 | CHECK(rc == 0, "no-grad add rc %d", rc); |
| 1017 | CHECK(omega_ad_backward_seed(&t, iy, a) == OMEGA_AD_ERR_NO_GRAD, "backward from a no-grad root accepted"); |
| 1018 | uint16_t h[4] = { 0x3c00, 0x3c00, 0x3c00, 0x3c00 }; |
| 1019 | OmegaTensor th; |
| 1020 | rc = omega_tensor_from_data(g, OMEGA_DT_F16, 1, s4, h, &th); |
| 1021 | CHECK(rc == 0, "f16 tensor rc %d", rc); |
| 1022 | CHECK(omega_ad_leaf(&t, th, true, &iz) == OMEGA_TENSOR_ERR_DTYPE, "F16 leaf accepted"); |
| 1023 | omega_ad_tape_release(&t); |
| 1024 | omega_tensor_release(g, th); |
| 1025 | |
| 1026 | omega_tensor_release(g, a); omega_tensor_release(g, b5); omega_tensor_release(g, b4); |
| 1027 | omega_tensor_release(g, m35); omega_tensor_release(g, m42); |
| 1028 | } |
| 1029 | |
| 1030 | int main(void) { |
| 1031 | if (!omega_numeric_fpenv_ok()) { printf("FPCR not RNE/no-FTZ: refusing to run\n"); return 2; } |
| 1032 | if (omega_tensor_ctx_create(4096, omega_tensor_cpu_realization(), &g)) { printf("ctx\n"); return 2; } |
| 1033 | for (size_t i = 0; i < NCASES; i++) test_op_case(&CASES[i]); |
| 1034 | test_comparators_reject(); |
| 1035 | test_max_ties(); |
| 1036 | test_composite(); |
| 1037 | test_determinism(); |
| 1038 | test_errors(); |
| 1039 | uint32_t lt, ls; |
| 1040 | omega_tensor_live_counts(g, <, &ls); |
| 1041 | CHECK(lt == 0 && ls == 0, "no leaked tensors (%u) or storage (%u)", lt, ls); |
| 1042 | omega_tensor_ctx_destroy(g); |
| 1043 | printf("M21 OMEGA_AUTODIFF CPU tests: %d pass, %d fail\n", g_pass, g_fail); |
| 1044 | printf("M21 verdict: NOT QUALIFIED (CPU tier only; GB10 NOT_RUN)\n"); |
| 1045 | return g_fail ? 1 : 0; |
| 1046 | } |