Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/tests/test_omega_ad_fixture.c # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / tests / test_omega_ad_fixture.c` 217 lines ยท 9186 bytes Raw Blame History 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 * AD fixture oracle for the native .om tape (native/autodiff664, omega #664). 19 * 20 * test_omega_ad_fixture --host [--mutant-sign] FIXTURE 21 * test_omega_ad_fixture --chip [--mutant-sign] FIXTURE (-DOMEGA_AD_FIXTURE_CHIP) 22 * 23 * FIXTURE is written by the native trainer (aien-train tests/adtrain, mode D): 24 * the inputs of one MLP step and the forward values / gradients the native 25 * tape computed. This program rebuilds the same graph on the C M21 tape over 26 * an M20 realization (CPU table or GB10 table), runs backward, and compares 27 * every value: Y[8,2], loss, dW1, db1, dW2, db2 (75 values). 28 * 29 * Graph: H = max(X.W1 + broadcast(b1), 0); Y = H.W2 + broadcast(b2); 30 * loss = sum_axis0(sum_axis1((Y - T) * (Y - T))). 31 * 32 * Format: little-endian u32 words. "ADFX", version 1, M K H N = 8 4 8 2, then 33 * X[32] W1[32] b1[8] W2[16] b2[2] T[16], then Y[16] loss dW1[32] db1[8] 34 * dW2[16] db2[2]: 187 words, 748 bytes. Any other size is refused. 35 * 36 * Comparison: equal values (==, so +0 and -0 agree) are exact; otherwise the 37 * ULP distance of two finite same-sign values. PASS needs max_ulp 0; PASS_TOL 38 * allows max_ulp <= 2 (the declared contingency); anything else is FAIL. 39 * --mutant-sign negates the expected dW1 first: a negative control that must 40 * FAIL. Exit 0 PASS/PASS_TOL, 1 FAIL, 2 refused or setup error. 41 */ 42 #include 43 #include 44 #include 45 #include 46 #include 47 48 #include "omega_autodiff.h" 49 #include "omega_numeric.h" 50 #include "omega_tensor.h" 51 52 enum { FM = 8, FK = 4, FH = 8, FN = 2 }; 53 enum { 54 OFF_X = 6, 55 OFF_W1 = OFF_X + FM * FK, 56 OFF_B1 = OFF_W1 + FK * FH, 57 OFF_W2 = OFF_B1 + FH, 58 OFF_B2 = OFF_W2 + FH * FN, 59 OFF_T = OFF_B2 + FN, 60 OFF_Y = OFF_T + FM * FN, 61 OFF_L = OFF_Y + FM * FN, 62 OFF_DW1 = OFF_L + 1, 63 OFF_DB1 = OFF_DW1 + FK * FH, 64 OFF_DW2 = OFF_DB1 + FH, 65 OFF_DB2 = OFF_DW2 + FH * FN, 66 FIX_WORDS = OFF_DB2 + FN 67 }; 68 enum { NCMP = FIX_WORDS - OFF_Y }; 69 70 static uint32_t W[FIX_WORDS]; 71 static float wf(size_t i) { 72 float f; 73 memcpy(&f, &W[i], sizeof f); 74 return f; 75 } 76 77 static uint32_t fbits(float f) { 78 uint32_t u; 79 memcpy(&u, &f, sizeof u); 80 return u; 81 } 82 83 static int load(const char *path) { 84 FILE *f = fopen(path, "rb"); 85 if (!f) { printf("AD_FIXTURE: cannot open %s\n", path); return 2; } 86 unsigned char buf[4 * FIX_WORDS + 1]; 87 size_t got = fread(buf, 1, sizeof buf, f); 88 fclose(f); 89 if (got != 4 * FIX_WORDS) { printf("AD_FIXTURE: size %zu, want %d\n", got, 4 * FIX_WORDS); return 2; } 90 if (memcmp(buf, "ADFX", 4) != 0) { printf("AD_FIXTURE: bad magic\n"); return 2; } 91 for (size_t i = 0; i < FIX_WORDS; i++) 92 W[i] = (uint32_t)buf[4 * i] | (uint32_t)buf[4 * i + 1] << 8 | (uint32_t)buf[4 * i + 2] << 16 | 93 (uint32_t)buf[4 * i + 3] << 24; 94 if (W[1] != 1 || W[2] != FM || W[3] != FK || W[4] != FH || W[5] != FN) { 95 printf("AD_FIXTURE: bad version or dimensions\n"); 96 return 2; 97 } 98 return 0; 99 } 100 101 /* ULP distance of two finite same-sign floats; UINT32_MAX when not comparable. */ 102 static uint32_t ulps(float a, float b) { 103 if (a == b) return 0; 104 uint32_t ua, ub; 105 memcpy(&ua, &a, 4); 106 memcpy(&ub, &b, 4); 107 if ((ua & 0x7f800000U) == 0x7f800000U || (ub & 0x7f800000U) == 0x7f800000U) return UINT32_MAX; 108 if ((ua ^ ub) & 0x80000000U) return UINT32_MAX; 109 return ua > ub ? ua - ub : ub - ua; 110 } 111 112 static int leaf(OmegaAdTape *t, OmegaTensorCtx *ctx, size_t off, uint64_t r, uint64_t c, bool rg, 113 OmegaTensor *keep, uint32_t *id) { 114 float v[64]; 115 for (size_t i = 0; i < r * c; i++) v[i] = off ? wf(off + i) : 0.0f; 116 uint64_t sh[2] = { r, c }; 117 int rc = omega_tensor_from_f32(ctx, 2, sh, v, keep); 118 if (!rc) rc = omega_ad_leaf(t, *keep, rg, id); 119 return rc; 120 } 121 122 int main(int argc, char **argv) { 123 bool chip = false, mutant = false; 124 const char *path = NULL; 125 for (int i = 1; i < argc; i++) { 126 if (!strcmp(argv[i], "--host")) chip = false; 127 else if (!strcmp(argv[i], "--chip")) chip = true; 128 else if (!strcmp(argv[i], "--mutant-sign")) mutant = true; 129 else if (!path) path = argv[i]; 130 else path = NULL, i = argc; 131 } 132 if (!path) { printf("usage: %s --host|--chip [--mutant-sign] FIXTURE\n", argv[0]); return 2; } 133 const char *arm = chip ? "CHIP" : "HOST"; 134 const OmegaTensorRealization *real = omega_tensor_cpu_realization(); 135 if (chip) { 136 #ifdef OMEGA_AD_FIXTURE_CHIP 137 const char *e = getenv("GB10_CHIP_RUN"); 138 if (!e || strcmp(e, "1") != 0) { printf("--chip needs GB10_CHIP_RUN=1 (tests/run_ad_chip.sh)\n"); return 2; } 139 real = omega_tensor_gb10_realization(); 140 #else 141 printf("--chip refused: built without OMEGA_AD_FIXTURE_CHIP\n"); 142 return 2; 143 #endif 144 } 145 if (!omega_numeric_fpenv_ok()) { printf("FPCR not RNE/no-FTZ: refusing to run\n"); return 2; } 146 int rc = load(path); 147 if (rc) return rc; 148 if (mutant) 149 for (size_t i = 0; i < FK * FH; i++) W[OFF_DW1 + i] ^= 0x80000000U; 150 151 OmegaTensorCtx *ctx = NULL; 152 if (omega_tensor_ctx_create(256, real, &ctx)) { printf("AD_FIXTURE: ctx\n"); return 2; } 153 OmegaAdNode nodes[32]; 154 float scratch[3 * 64]; 155 OmegaAdTape t; 156 OmegaTensor lv[7]; 157 uint32_t x, w1, b1, w2, b2, tt, z, mm1, bc1, a1, h, mm2, bc2, y, d, sq, s1, loss; 158 rc = omega_ad_tape_init(&t, ctx, nodes, 32, scratch, sizeof scratch / sizeof scratch[0]); 159 uint64_t sh88[2] = { FM, FH }, sh82[2] = { FM, FN }; 160 if (!rc) rc = leaf(&t, ctx, OFF_X, FM, FK, false, &lv[0], &x); 161 if (!rc) rc = leaf(&t, ctx, OFF_W1, FK, FH, true, &lv[1], &w1); 162 if (!rc) rc = leaf(&t, ctx, OFF_B1, 1, FH, true, &lv[2], &b1); 163 if (!rc) rc = leaf(&t, ctx, OFF_W2, FH, FN, true, &lv[3], &w2); 164 if (!rc) rc = leaf(&t, ctx, OFF_B2, 1, FN, true, &lv[4], &b2); 165 if (!rc) rc = leaf(&t, ctx, OFF_T, FM, FN, false, &lv[5], &tt); 166 if (!rc) rc = leaf(&t, ctx, 0, FM, FH, false, &lv[6], &z); 167 if (!rc) rc = omega_ad_matmul(&t, x, w1, &mm1); 168 if (!rc) rc = omega_ad_broadcast(&t, b1, 2, sh88, &bc1); 169 if (!rc) rc = omega_ad_binary(&t, OMEGA_AD_ADD, mm1, bc1, &a1); 170 if (!rc) rc = omega_ad_binary(&t, OMEGA_AD_MAX, a1, z, &h); 171 if (!rc) rc = omega_ad_matmul(&t, h, w2, &mm2); 172 if (!rc) rc = omega_ad_broadcast(&t, b2, 2, sh82, &bc2); 173 if (!rc) rc = omega_ad_binary(&t, OMEGA_AD_ADD, mm2, bc2, &y); 174 if (!rc) rc = omega_ad_binary(&t, OMEGA_AD_SUB, y, tt, &d); 175 if (!rc) rc = omega_ad_binary(&t, OMEGA_AD_MUL, d, d, &sq); 176 if (!rc) rc = omega_ad_reduce(&t, OMEGA_AD_SUM, sq, 1, true, &s1); 177 if (!rc) rc = omega_ad_reduce(&t, OMEGA_AD_SUM, s1, 0, true, &loss); 178 if (!rc) rc = omega_ad_backward(&t, loss); 179 180 float got[NCMP]; 181 OmegaTensor v; 182 if (!rc) rc = omega_ad_value(&t, y, &v); 183 if (!rc) rc = omega_tensor_read_f32(ctx, v, got, FM * FN); 184 if (!rc) rc = omega_ad_value(&t, loss, &v); 185 if (!rc) rc = omega_tensor_read_f32(ctx, v, got + (OFF_L - OFF_Y), 1); 186 if (!rc) rc = omega_ad_grad_read_f32(&t, w1, got + (OFF_DW1 - OFF_Y), FK * FH); 187 if (!rc) rc = omega_ad_grad_read_f32(&t, b1, got + (OFF_DB1 - OFF_Y), FH); 188 if (!rc) rc = omega_ad_grad_read_f32(&t, w2, got + (OFF_DW2 - OFF_Y), FH * FN); 189 if (!rc) rc = omega_ad_grad_read_f32(&t, b2, got + (OFF_DB2 - OFF_Y), FN); 190 if (rc) { 191 printf("AD_FIXTURE_%s: C tape error %d (numeric %d)\n", arm, rc, omega_tensor_last_numeric_error(ctx)); 192 omega_ad_tape_release(&t); 193 omega_tensor_ctx_destroy(ctx); 194 return 2; 195 } 196 197 unsigned exact = 0; 198 uint32_t max_ulp = 0; 199 bool comparable = true; 200 for (size_t i = 0; i < NCMP; i++) { 201 uint32_t u = ulps(got[i], wf(OFF_Y + i)); 202 if (u == 0) exact++; 203 if (u == UINT32_MAX) comparable = false; 204 else if (u > max_ulp) max_ulp = u; 205 if (u) printf("AD_FIXTURE_%s: word %zu native %08x C %08x ulp %u\n", arm, (size_t)OFF_Y + i, 206 (unsigned)W[OFF_Y + i], (unsigned)fbits(got[i]), (unsigned)u); 207 } 208 const char *verdict = !comparable || max_ulp > 2 ? "FAIL" : max_ulp ? "PASS_TOL" : "PASS"; 209 char ulpbuf[16]; 210 snprintf(ulpbuf, sizeof ulpbuf, "%u", (unsigned)max_ulp); 211 printf("AD_FIXTURE_%s: exact=%u/%d max_ulp=%s verdict=%s%s\n", arm, exact, NCMP, 212 comparable ? ulpbuf : "inf", verdict, mutant ? " (mutant-sign)" : ""); 213 omega_ad_tape_release(&t); 214 for (int i = 0; i < 7; i++) omega_tensor_release(ctx, lv[i]); 215 omega_tensor_ctx_destroy(ctx); 216 return strcmp(verdict, "FAIL") ? 0 : 1; 217 }