interplane.Get Omega
Read in your language

physics-mv

FORGE: request-bound machine realization, providers and verification for AienOS. Host, simulated and GB10 hardware paths have separate evidence.

Brought by drakestapleton

branch main Branches Tags History

physics-mv / tests / run_forge_analog_sim_gates.sh

166 lines · 7485 bytes Raw Blame History
1#!/bin/bash
2# tests/run_forge_analog_sim_gates.sh -- AR2 gates (ARCH-0018): simulated analog
3# realization provider with digital oracle parity. Host-only: no hardware, no
4# driver, no GPU lock. Every result is SIMULATED_DEVELOPMENT.
5#
6# tests/run_forge_analog_sim_gates.sh build, run every check, verify the
7# committed receipt is reproduced byte for byte
8# tests/run_forge_analog_sim_gates.sh --regen also (re)write evidence/AR2/<sha256>.json
9#
10# Optional: OMEGA_DIR (default ../omega next to this repo). When it holds
11# src/omega_matvec.c and src/omega_blackwell_matmul.c, the two restated
12# oracles are compared against the omega originals; otherwise that gate SKIPs.
13#
14# Exit code 0 only when every gate passes.
15set -uo pipefail
16
17SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)
18REPO_ROOT=$(cd "$SCRIPT_DIR/.." && pwd -P)
19cd "$REPO_ROOT" || exit 1
20
21BUILD_DIR="$REPO_ROOT/build"
22BIN="$BUILD_DIR/test_forge_analog_sim"
23RECEIPT_DIR="evidence/AR2"
24SRC_C="forge/analog-sim/forge_analog_sim.c"
25SRC_H="forge/analog-sim/forge_analog_sim.h"
26TEST_C="tests/test_forge_analog_sim.c"
27OMEGA_DIR="${OMEGA_DIR:-$REPO_ROOT/../omega}"
28FAILS=0
29PASSES=0
30SKIPS=0
31
32gate() { # name, status(0 = pass)
33 if [ "$2" -eq 0 ]; then echo "PASS $1"; PASSES=$((PASSES + 1));
34 else echo "FAIL $1"; FAILS=$((FAILS + 1)); fi
35}
36
37echo "=== AR2 simulated analog provider gates (host-only, SIMULATED_DEVELOPMENT) ==="
38mkdir -p "$BUILD_DIR"
39
40gcc -O2 -Wall -Wextra -Werror -std=c11 -I./forge/v2 -I./forge/analog-sim \
41 forge/v2/forge_substrate_v2.c "$SRC_C" sha256_clean.c "$TEST_C" -o "$BIN"
42gate "build_gcc_O2_Wall_Wextra_Werror_std_c11" $?
43if [ ! -x "$BIN" ]; then
44 echo "RESULT gates: $PASSES passed, $FAILS failed (build failed)"
45 exit 1
46fi
47
48"$BIN" | grep -E '^(FAIL|RESULT)'
49"$BIN" > /dev/null
50gate "ar2_checks_all_pass" $?
51
52# Same checks under the undefined-behaviour sanitizer (any UB aborts the run).
53gcc -O1 -g -Wall -Wextra -Werror -std=c11 -fsanitize=undefined -fno-sanitize-recover=all \
54 -I./forge/v2 -I./forge/analog-sim \
55 forge/v2/forge_substrate_v2.c "$SRC_C" sha256_clean.c "$TEST_C" -o "$BIN-ubsan" \
56 && "$BIN-ubsan" > /dev/null
57gate "ar2_checks_pass_under_ubsan" $?
58
59# Deterministic receipt, named by the SHA-256 of its bytes.
60"$BIN" --receipt > "$BUILD_DIR/ar2-receipt.json"
61gate "receipt_emitted_by_code" $?
62SUM=$(sha256sum "$BUILD_DIR/ar2-receipt.json" | cut -d' ' -f1)
63if [ "${1:-}" = "--regen" ]; then
64 mkdir -p "$RECEIPT_DIR"
65 rm -f "$RECEIPT_DIR"/*.json
66 cp "$BUILD_DIR/ar2-receipt.json" "$RECEIPT_DIR/$SUM.json"
67 gate "receipt_regenerated" $?
68fi
69"$BIN" --receipt | cmp -s - "$BUILD_DIR/ar2-receipt.json"
70gate "receipt_deterministic_across_runs" $?
71COMMITTED=$(ls "$RECEIPT_DIR"/*.json 2>/dev/null | wc -l)
72[ "$COMMITTED" -eq 1 ] && [ -f "$RECEIPT_DIR/$SUM.json" ] && cmp -s "$RECEIPT_DIR/$SUM.json" "$BUILD_DIR/ar2-receipt.json"
73gate "committed_receipt_reproduced_digest_named" $?
74
75# Provenance: the receipt says SIMULATED_DEVELOPMENT and never claims a physical result.
76grep -q '"provenance_class": "SIMULATED_DEVELOPMENT"' "$BUILD_DIR/ar2-receipt.json" \
77 && grep -q '"status": "PASS"' "$BUILD_DIR/ar2-receipt.json" \
78 && ! grep -qiE 'physical|measured' "$BUILD_DIR/ar2-receipt.json"
79gate "receipt_simulated_development_no_physical_claim" $?
80CASES=$(grep -c '"name": ' "$BUILD_DIR/ar2-receipt.json")
81SIMCASES=$(grep -c '"provenance_class": "SIMULATED_DEVELOPMENT"}' "$BUILD_DIR/ar2-receipt.json")
82[ "$CASES" -gt 0 ] && [ "$CASES" -eq "$SIMCASES" ]
83gate "every_receipt_case_simulated_development" $?
84
85# The provider source never names the PHYSICAL provenance.
86! grep -n 'PROVENANCE_PHYSICAL' "$SRC_C" "$SRC_H" > /dev/null
87gate "provider_source_cannot_emit_physical" $?
88
89# Vendor-neutral: no vendor or product word in any identifier of the new files.
90! grep -nE '\b[A-Za-z_]*(NVIDIA|Nvidia|nvidia|GB10|gb10|BLACKWELL|Blackwell|blackwell|CUDA|cuda|NVRM|nvrm)[A-Za-z_0-9]*\b' \
91 "$SRC_C" "$SRC_H" "$TEST_C" \
92 | grep -vE '^\S+:[0-9]+:\s*(/\*|\*)' | grep -vE 'omega_blackwell_matmul\.[ch]' > /dev/null
93gate "no_vendor_names_in_identifiers" $?
94
95# No hardware, driver, heap or I/O in the provider.
96! grep -nE '#include\s*[<"](nvrm|nv|cuda|sys/ioctl|sys/mman|fcntl|stdio)' "$SRC_C" "$SRC_H" > /dev/null \
97 && ! grep -nE '\b(malloc|calloc|realloc|free|ioctl|mmap|open|printf|fopen)\s*\(' "$SRC_C" > /dev/null
98gate "no_hardware_access_no_heap_no_io_in_provider" $?
99
100# R16 loop-inventory heuristics (same patterns as the V2 gates).
101! grep -nE 'while\s*\(\s*(1|true)\s*\)|for\s*\(\s*;\s*;\s*\)' "$SRC_C" "$TEST_C" > /dev/null \
102 && ! grep -niE '(^|[^a-z])(u?sleep|nanosleep|msleep|poll|recv|heartbeat|tick|dispatch|schedule|scheduler|orchestrate|orchestrator|turns?|pulse|yield|epoll|run_until_complete|max_steps|max_turns)([^a-z]|$)' \
103 "$SRC_C" "$SRC_H" "$TEST_C" > /dev/null
104gate "r16_loop_inventory_heuristics_clean" $?
105
106# Oracle parity with the omega originals (read-only; omega is never modified).
107MV="$OMEGA_DIR/src/omega_matvec.c"
108MM="$OMEGA_DIR/src/omega_blackwell_matmul.c"
109if [ -f "$MV" ] && [ -f "$MM" ]; then
110 X="$BUILD_DIR/ar2-omega-oracle"
111 mkdir -p "$X"
112 {
113 echo '#include <stdint.h>'
114 echo '#include <stddef.h>'
115 awk '/^void omega_matvec_reference\(/,/^}/' "$MV"
116 awk '/^int omega_matmul_cpu_oracle_i32\(/,/^}/' "$MM"
117 } > "$X/omega_oracles.c"
118 cat > "$X/cross.c" <<'EOF'
119#include <stdio.h>
120#include <string.h>
121#include "forge_analog_sim.h"
122void omega_matvec_reference(const uint64_t *A, const uint64_t *x, uint64_t *y, uint32_t M, uint32_t N);
123int omega_matmul_cpu_oracle_i32(const uint32_t *a, const uint32_t *b, uint32_t *c, uint32_t m, uint32_t k, uint32_t n);
124int main(void)
125{
126 static uint64_t A[64 * 64], x[64], y1[64], y2[64];
127 static uint32_t a32[64 * 64], x32[64], z1[64], z2[64];
128 uint64_t s = 0x5eed;
129 int bad = 0;
130 for (int c = 0; c < 64; c++) {
131 uint32_t M = 1 + (uint32_t)(c % 64), N = 1 + (uint32_t)((c * 37) % 64);
132 for (uint32_t k = 0; k < M * N; k++) {
133 s = s * 6364136223846793005ull + 1442695040888963407ull;
134 A[k] = (c & 1) ? s : (s >> 56);
135 a32[k] = (uint32_t)(s >> 7);
136 }
137 for (uint32_t j = 0; j < N; j++) {
138 s = s * 6364136223846793005ull + 1442695040888963407ull;
139 x[j] = (c & 1) ? s : (s >> 56);
140 x32[j] = (uint32_t)(s >> 9);
141 }
142 omega_matvec_reference(A, x, y1, M, N);
143 forge_asim_oracle_exact_u64(A, x, y2, M, N);
144 omega_matmul_cpu_oracle_i32(a32, x32, z1, M, N, 1);
145 forge_asim_oracle_i32(a32, x32, z2, M, N);
146 if (memcmp(y1, y2, M * sizeof y1[0]) || memcmp(z1, z2, M * sizeof z1[0])) bad++;
147 }
148 printf("omega oracle cross-check: %d mismatching cases of 64\n", bad);
149 return bad != 0;
150}
151EOF
152 gcc -O2 -std=c11 -Wall -Werror -I./forge/v2 -I./forge/analog-sim \
153 "$X/omega_oracles.c" "$X/cross.c" "$SRC_C" forge/v2/forge_substrate_v2.c sha256_clean.c -o "$X/cross" \
154 && "$X/cross"
155 gate "restated_oracles_match_omega_originals" $?
156else
157 echo "SKIP restated_oracles_match_omega_originals (no omega checkout at $OMEGA_DIR)"
158 SKIPS=$((SKIPS + 1))
159fi
160
161# The AR1 (V2 descriptor contract) gates are untouched and still pass.
162bash tests/run_forge_v2_gates.sh > "$BUILD_DIR/ar1-gates.log" 2>&1
163gate "ar1_v2_gates_still_pass" $?
164
165echo "RESULT gates: $PASSES passed, $FAILS failed, $SKIPS skipped"
166[ "$FAILS" -eq 0 ]