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 / tools / autodiff_mutations.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | # M21 OMEGA_AUTODIFF mutation sweep. For each rule marked MUT:<name> in |
| 18 | # src/autodiff, copy the sources to a scratch directory, break that one |
| 19 | # rule, rebuild the CPU test against the copy and require the test to FAIL. |
| 20 | # A mutation that does not apply, does not build, or that the tests do not |
| 21 | # catch, fails the sweep. The source tree is never edited. CPU only, no |
| 22 | # device. Shell only (no Python). |
| 23 | set -u |
| 24 | cd "$(dirname "$0")/.." || exit 2 |
| 25 | SCRATCH=$(mktemp -d "${TMPDIR:-/tmp}/autodiff-mut.XXXXXX") || exit 2 |
| 26 | trap 'rm -rf "$SCRATCH"' EXIT INT TERM |
| 27 | TENSOR="src/tensor/omega_tensor.c src/tensor/omega_tensor_cpu.c src/tensor/omega_tensor_reduce_seam.c \ |
| 28 | src/omega_numeric_reduce.c src/omega_numeric_transc.c" |
| 29 | DEPS="src/omega_numeric.c src/omega_numeric_provenance.c src/omega_blackwell_encoder.c \ |
| 30 | src/omega_blackwell_codegen.c src/omega_blackwell_matmul.c src/omega_blackwell_qmd.c src/omega_om_units.c" |
| 31 | FLAGS="-std=gnu11 -Wall -Wextra -Werror -D_GNU_SOURCE -ffp-contract=off -O2 -DOMEGA_NUMERIC_CPU_ONLY" |
| 32 | |
| 33 | # name|file|sed expression (applied only to the line carrying MUT:<name>) |
| 34 | # DIV_FORMULA computes gb as -((g * y) / b) instead of -(g * (y / b)): the same |
| 35 | # real number with the same rounding count, so it stays inside the per-op |
| 36 | # contract and the 1e-3 FD bound; it is aimed at the bit-exact layer. |
| 37 | MUTATIONS='TAPE_WALK|omega_autodiff.c|s/root + 1; i-- > 0/root; i-- > 0/ |
| 38 | ACCUMULATE|omega_autodiff.c|s/omega_tensor_binary(t->ctx, OMEGA_TB_ADD, n->grad, contrib, &sum)/omega_tensor_contiguous(t->ctx, contrib, \&sum)/ |
| 39 | UNBROADCAST|gradient_shape.om|{n;s/if and eq get target axis 1 ne get source add leading axis 1/if false/;} |
| 40 | SUB_NEGATE|omega_autodiff.c|s/-1\.0f/1.0f/ |
| 41 | MUL_OPERAND|omega_autodiff.c|s/t->nodes\[n->in1\]\.value/t->nodes[n->in0].value/ |
| 42 | MATMUL_TRANSPOSE|omega_autodiff.c|s/matmul(t->ctx, g, tmp, &c)/matmul(t->ctx, g, nb->value, \&c)/ |
| 43 | SQRT_TWICE|omega_autodiff.c|s/omega_tensor_binary(t->ctx, OMEGA_TB_ADD, n->value, n->value, &tmp)/omega_tensor_contiguous(t->ctx, n->value, \&tmp)/ |
| 44 | MEAN_DIVISOR|omega_autodiff.c|s/(float)len/(float)(len + 1)/ |
| 45 | MAX_ROUTE|omega_autodiff.c|s/return a > b;/return a < b;/ |
| 46 | MAX_TIE|omega_autodiff.c|s/return a > b;/return a >= b;/ |
| 47 | DIV_B_SIGN|omega_autodiff.c|s/-1\.0f/1.0f/ |
| 48 | DIV_FORMULA|omega_autodiff.c|{s/OMEGA_TB_DIV, n->value, nb->value, &tmp)/OMEGA_TB_MUL, g, n->value, \&tmp)/;s/OMEGA_TB_MUL, g, tmp, &c2)/OMEGA_TB_DIV, tmp, nb->value, \&c2)/}' |
| 49 | |
| 50 | # Runs on the shared runner tools/mutation_runner.sh (marker reader). This |
| 51 | # script is a thin adapter: it feeds the rows above, then maps the runner's |
| 52 | # per-mutant stderr lines back to the legacy stdout lines and exit codes. |
| 53 | # Rows name src/autodiff/<file>, relative to the runner's scratch tree. |
| 54 | # Rebuild the seeded binding only for a changed Omega source. |
| 55 | AUTODIFF_ROOT=$(pwd) |
| 56 | export AUTODIFF_ROOT |
| 57 | case " ${ONLY:-UNBROADCAST} " in |
| 58 | *" UNBROADCAST "*) |
| 59 | compiler=${OMEGA_OMC:-$AUTODIFF_ROOT/seed/out/omc} |
| 60 | bridge=${OMEGA_BRIDGE:-$AUTODIFF_ROOT/../omega-bridge} |
| 61 | if [ ! -x "$compiler" ] || [ ! -r "$bridge/src/native.om" ]; then |
| 62 | echo "autodiff mutations: NOT_RUN (UNBROADCAST needs the pinned omc and omega-bridge)" >&2 |
| 63 | exit 3 |
| 64 | fi |
| 65 | OMEGA_OMC=$(realpath "$compiler") || exit 3 |
| 66 | OMEGA_BRIDGE=$(realpath "$bridge") || exit 3 |
| 67 | export OMEGA_OMC OMEGA_BRIDGE ;; |
| 68 | esac |
| 69 | REGEN='if ! cmp -s src/autodiff/gradient_shape.om "$AUTODIFF_ROOT/src/autodiff/gradient_shape.om"; then |
| 70 | unset OMEGA_UNITS |
| 71 | OMEGA_UNITS_DEST="$PWD/src" sh om/units.sh || exit 1 |
| 72 | fi' |
| 73 | BUILD="$REGEN |
| 74 | gcc $FLAGS -Isrc -Isrc/tensor -Isrc/autodiff -o t tests/test_omega_autodiff.c src/autodiff/omega_autodiff.c \ |
| 75 | $TENSOR $DEPS" |
| 76 | printf '%s\n' "$MUTATIONS" | sed 's#^\([A-Z_]*\)|#\1|src/autodiff/#' > "$SCRATCH/rows" |
| 77 | tools/mutation_runner.sh -k marker -m "$SCRATCH/rows" -d . -c "src om gpu tests/test_omega_autodiff.c" \ |
| 78 | -b "$BUILD" -t ./t -f FAIL -B > "$SCRATCH/runner.out" 2> "$SCRATCH/runner.err" |
| 79 | if grep -q 'unmutated baseline' "$SCRATCH/runner.out"; then |
| 80 | cat "$SCRATCH/runner.err" |
| 81 | echo "autodiff mutation sweep: FAIL (unmutated baseline does not pass)"; exit 1 |
| 82 | fi |
| 83 | fail=0 |
| 84 | total=0 |
| 85 | while IFS= read -r line; do |
| 86 | case $line in |
| 87 | *' KILLED '*|*' SURVIVED '*|*' ERROR '*) ;; |
| 88 | *) continue;; |
| 89 | esac |
| 90 | name=${line%% *} |
| 91 | rest=${line#* }; status=${rest%% *} |
| 92 | note=${line#* (}; note=${note%)} |
| 93 | total=$((total + 1)) |
| 94 | case $status in |
| 95 | KILLED) |
| 96 | n=0 |
| 97 | case $note in *' failing case(s)'*) n=${note%% failing case(s)*};; esac |
| 98 | echo "MUTATION $name: caught ($n failing checks)";; |
| 99 | SURVIVED) echo "MUTATION $name: NOT CAUGHT (tests still pass)"; fail=1;; |
| 100 | *) |
| 101 | case $note in |
| 102 | 'edit did not apply'*) echo "MUTATION $name: NOT APPLIED (marker or pattern missing)";; |
| 103 | *) echo "$note"; echo "MUTATION $name: does not build (fix the sed expression)";; |
| 104 | esac |
| 105 | fail=1;; |
| 106 | esac |
| 107 | done < "$SCRATCH/runner.err" |
| 108 | if [ "$total" -lt 5 ]; then echo "autodiff mutation sweep: FAIL (fewer than 5 mutations)"; exit 1; fi |
| 109 | if [ "$fail" -ne 0 ]; then echo "autodiff mutation sweep: FAIL"; exit 1; fi |
| 110 | echo "autodiff mutation sweep: PASS ($total of $total mutations caught)" |