interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / docs / autodiff / M21_OMEGA_AUTODIFF.md

240 lines · 14899 bytes Raw Blame History Source

M21 OMEGA_AUTODIFF: reverse-mode tape, CPU tier

Status 2026-10-01: M21 NOT QUALIFIED. The tape, its tests and its mutation sweep are written. Forge host run on commit c098a74 (before the rebase onto omega#178/#179 and before the gate fixes): test-autodiff PASS 182 pass / 0 fail (log HIVE-M21-181152.log), mutation sweep 10/10 caught (log HIVE-M21-181157.log). Forge host run on commit 0f0938f (rebased, bit-exact layer, pure 1e-3 relative FD, 12 mutants): test-autodiff PASS 262 pass / 0 fail, plain and ASan/UBSan (log HIVE-M21-light-185710.log); mutation sweep PASS 12/12 caught (log HIVE-M21-light-185715.log). This doc-only commit changes no code, so the 0f0938f host receipt applies to its code. GB10 NOT_RUN. M20, which this layer sits on, is itself not qualified (GB10 parity NOT_RUN).

Plan source: aien-architecture/CURRENT_EXECUTION_PLAN.md, section "E3. M21 OMEGA_AUTODIFF": "Omega forward graph -> differentiate -> Omega backward graph. The backward pass is ordinary Omega and uses the same Forge/AEGIS path." ROADMAP row M21: "Sovereign automatic differentiation generating gradient semantic graphs", PLANNED. Neither document states an M21 exit gate or a gradient tolerance. The owner approved the M21 gate (queen, 2026-10-01):

**M21 gate: gradients bit-exact against the same M20 operations, plus a finite-difference check within 1e-3 relative error.**

See "Gate conformance" below for how the current tests map onto it.

Scope

A reverse-mode tape over the M20 OMEGA_TENSOR CPU tier (src/tensor/omega_tensor.h). Forward: each recorded op is one M20 call. Backward: walks the tape from the root down and builds every gradient with M20 calls (ADD, SUB, MUL, DIV, matmul, transpose, reshape, broadcast_to, reduce SUM), so the backward pass runs on the same E1 realization as the forward pass. One exception: MAX routing is a host-side data move (compare and copy, no arithmetic) in the caller's scratch buffer, because M20 has no mask-producing op (CR-3 below).

Out of scope here: GB10 realization, gradients for the M20 transcendental unary ops (EXP2, LOG2, SIGMOID, TANH, added by omega#178; a later cut, pending LT-M20), slicing/concat/gather gradients, second derivatives, dispatch provenance records for forward/backward ops (open question for E5), and any edit of src/tensor or src/train.

Files

FileRole
src/autodiff/omega_autodiff.hThe meaning: ops, ownership, determinism rules, per-op error contract
src/autodiff/omega_autodiff.cTape recording, reverse walk, tensor ownership and M22 flatten
src/autodiff/gradient_shape.omUnbroadcast reduction order, shape equality and restoration of a reduced axis on the caller’s stack
tests/test_omega_autodiff.cContract, bit-exact replay, finite-difference, determinism, error-path and leak checks
tools/autodiff_mutations.shSource mutation sweep (12 mutants)
mk/autodiff.mkmake test-autodiff, make test-autodiff-mutations (alias autodiff-mutants)

Determinism and resources

Ops, backward rules and error bounds

g = incoming gradient, y = stored forward value, unb = sum back to the input's shape (extra leading axes by reduce SUM over axis 0, then every axis where the input has size 1 by reduce SUM with keepdims). u = 2^-24, gamma(k) = k u / (1 - k u). L(m) = max(1, ceil(log32 m)) E1 reduction levels, 5 tree steps each, so a declared-order sum of m terms has error at most gamma(5 L(m)) times the sum of absolute terms (E1 reduction contract; padding with -0.0 is exact). D(x) = sum of 5 L(m) over every reduction unb makes for input x (0 when x was not broadcast).

Bounds are local: each gradient against exact real arithmetic on the float32 values that are on the tape (stored forward values and the incoming g), in the normal range (no overflow, no subnormal intermediates).

OpM20 forwardBackward (M20 calls)Bound per gradient element
ADDbinary ADDga = unb(g), gb = unb(g)gamma(D(x)) sum abs(g); exact when not broadcast
SUBbinary SUBga = unb(g), gb = unb(g * -1)same as ADD (negation is exact)
MULbinary MULga = unb(g * b), gb = unb(g * a)gamma(D(x)+1) sum abs(g * other)
DIVbinary DIVga = unb(g / b); gb = unb(-(g * (y / b)))a: gamma(D(a)+1) sum abs(g/b); b: gamma(D(b)+3) sum abs(g a / b^2)
MAXbinary MAXg routed: to a if b is NaN and a is not, or a > b; else to bgamma(D(x)) sum abs(routed g); routing exact
SQRTunary SQRTga = g / (y + y)gamma(2) abs(g / (2 sqrt a)); inf at a = 0
MATMUL [..,M,K]x[..,K,N]matmulgA = unb(g @ B^T), gB = unb(A^T @ g)A: gamma(1 + 5 L(N) + D(A)) sum abs(g) abs(B); B: gamma(1 + 5 L(M) + D(B)) sum abs(A) abs(g)
TRANSPOSEtranspose + contiguousga = g^Texact
BROADCASTbroadcast_to + contiguousga = unb(g)gamma(D(a)) sum abs(g)
SUM(axis)reduce SUMga = broadcast_to(reshape(g))exact
MEAN(axis)reduce MEANga = broadcast_to(reshape(g)) / ngamma(1) abs(g / n), n <= 2^24
fan-outk contributions added in tape orderextra gamma(k-1) sum abs(contribution)

MAX tie rule: ties (including -0 vs +0) and a NaN a send g to b, so relu(x) = MAX(x, 0) has gradient 0 at x = 0.

M22 bridge (tg_sgd compatibility)

tg_sgd_step(st, sh, const float *grad, size_t n, lr, momentum, grad_ref) takes one flat float32 gradient whose length equals the shadow's parameter count. omega_ad_grads_flatten(tape, ids, n, out, count) writes the gradients of the given parameter nodes back to back, each in logical row-major order, and refuses (writing nothing) unless the total equals count. Compatible as long as the caller lays the shadow's parameters out in the same node order. src/train is unchanged and still includes no tensor header (its purity rule). grad_ref is the caller's: a natural choice is a prefix of omega_tensor_value_id of the gradient tensors (omega_ad_grad gives the handle). NaN/Inf gradients are not refused by the tape or by tg_sgd_step; where to refuse them (tape end or the tg_commit validate callback) is an open decision.

Test design (M21 gate, approved 2026-10-01)

Approved M21 gate (owner, via the queen, 2026-10-01): **"gradients bit-exact against the same M20 operations, plus a finite-difference check within 1e-3 relative error"**. Layers 2 and 3 below are that gate; layers 1, 4 and 5 are extra.

tests/test_omega_autodiff.c, 21 op cases (every op, with and without broadcasting, batched matmul) plus a composite graph:

  1. Contract layer. Each tape gradient against an exact reference written in the test from the calculus (double precision, index arithmetic on shapes only), within the bound in the table above.
  2. Bit-exact layer (gate). For every op case the test replays the backward rule itself with direct M20 calls in the tape's order (for example MUL: binary MUL (g, b), then reduce SUM over the extra leading axes and the size-1 axes of a, keepdims) and compares the tape's gradient with it bit for bit (memcmp), for a and for b. MAX routing, a host data move, is replayed with the routing rule written independently in the test. In-process counterexample: a one-bit flip of the replay must make memcmp differ.
  3. Finite-difference layer (gate). Central differences of the M20 CPU forward pass, step h = 2^-6 (actual step taken from the rounded floats), loss L = sum(y * R) in double. Pass if |tape - fd| <= 1e-3 |fd| (FD_RTOL, fd_ok): pure relative, no absolute term. Near-zero gradients are handled by choosing the test points, not by loosening: the FD layer runs on its own data set (gen_fd_data, seed R in [0.5, 1]; MUL and MATMUL operands in [0.5, 1], DIV numerator in [1, 2], SQRT input in [1, 4]) where every gradient element is a sum of same-sign terms of magnitude about 0.1 or more, or exactly zero (MAX routing, where the perturbed forward is unchanged and fd is exactly 0 too). Every element, near zero or not, is still covered by layers 1 and 2 on the mixed-sign data. Inputs are kept away from kinks (MAX) and singular points (DIV, SQRT). In-process counterexamples: the FD comparator accepts the exact gradient on all 12 elements of a MUL case, rejects a sign flip, and rejects a gradient 2e-3 too large on all 12.
  4. Composite. Linear layer + squared error, mean_i sum_j (XW + b - Y)^2, with a reused node (fan-out): closed form (2/N) X^T E within 1e-5 on mixed-sign data; FD (pure 1e-3 relative) on every parameter on a second data set (lin_data_fd: X, W, b in [0.5, 1], Y in [-0.5, 0], so every gradient is >= 1.25), with the FD loss computed in double from the tape's float d values. X and Y have no gradient.
  5. Determinism. Two full runs in two M20 contexts: flat gradients bit-identical (memcmp) and the loss value id identical.
  6. Error paths. Tape full (no state change, no leak), shape mismatch ([3,4]+[5], [3,5]x[4,2], [3,5]x[3,5], broadcast [5]->[4], bad axis), wrong seed shape, scalar seed on a non-scalar root, too little scratch, second backward, recording after backward, unreached leaf, no-grad root, F16 leaf, flatten count mismatch (buffer untouched), bad node ids, leak check of the M20 context.

Counterexamples in process: the contract comparator must reject a gradient off by 2^-20 relative and accept the exact one; the FD comparator as above; the bit-exact and determinism comparators must see a one-bit flip.

Mutation sweep (tools/autodiff_mutations.sh), each must make the test fail (12 mutants): TAPE_WALK (skip the root), ACCUMULATE (replace instead of add), UNBROADCAST (skip size-1 axis sums), SUB_NEGATE, MUL_OPERAND, MATMUL_TRANSPOSE, SQRT_TWICE, MEAN_DIVISOR (n+1), MAX_ROUTE (reverse comparison), MAX_TIE (ties to a), DIV_B_SIGN (drop the minus on the DIV b-gradient) and DIV_FORMULA. DIV_FORMULA computes the DIV b-gradient as -((g * y) / b) instead of -(g * (y / b)): the same real number with the same number of roundings, so by construction it stays inside the contract bound gamma(D(b)+3) and the 1e-3 FD bound, and only the bit-exact layer is expected to catch it (UNVERIFIED until the forge runs the sweep).

Qualification checklist

Two commits matter here. c098a74 (before the rebase, before the fixes below) has a forge receipt. The current commit (rebased onto omega#178 and #179, with the bit-exact layer, the pure relative FD check and 12 mutants) has a forge host PASS receipt (0f0938f).

Itemc098a74 (forge receipt)Current commitEvidence
Reverse-mode tape over M20 CPU opsWRITTENWRITTENsrc/autodiff/
Contract layerPASSPASS (0f0938f)make test-autodiff
Bit-exact layer (gate)not presentPASS (0f0938f)make test-autodiff
Finite differences, pure 1e-3 relative (gate)not present (c098a74 used 5e-4 + 1e-3 |fd|)PASS (0f0938f)make test-autodiff
Composite graph end to endPASSPASS (0f0938f)make test-autodiff
Determinism (bit-identical rerun)PASSPASS (0f0938f)make test-autodiff
Error paths and leak checkPASSPASS (0f0938f)make test-autodiff (plain and ASan/UBSan)
Host test totalsPASS, 182 pass / 0 fail (log HIVE-M21-181152.log)PASS, 262 pass / 0 fail (log HIVE-M21-light-185710.log)forge
Mutation sweepPASS, 10/10 caught (log HIVE-M21-181157.log)PASS, 12/12 caught (log HIVE-M21-light-185715.log)sh tools/autodiff_mutations.sh
CI wiringMISSINGMISSINGno autodiff job in host-suites-2.yml yet
GB10NOT_RUNNOT_RUNno GB10 realization of M20 yet
Owner approval of the gateAPPROVEDqueen, 2026-10-01 (wording above)
Reproducible receiptMISSING_IMPLEMENTATIONMISSING_IMPLEMENTATIONno receipt writer for this gate yet; the forge receipt lines do not name the commit hash

M21 verdict: NOT QUALIFIED. Host PASS on 0f0938f; a GB10 chip receipt does not exist yet.

Gate conformance (approved wording vs current tests)

Tensor API change requests (not made here; owner LT-M20)

Open questions

  1. Whether forward/backward ops should write E5 tier (a) dispatch records.
  2. Where NaN/Inf gradients are refused before an optimizer commit.
  3. What "the same Forge/AEGIS path" (CEP E3) requires of a CPU-tier tape.