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 / exp445 / sampling-gate.sh
| 1 | #!/usr/bin/env bash |
| 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 | # OWN-8B stage S sampling gate: is seeded sampling observed and honoured by a running |
| 18 | # aien-serve-http? Shared by the 30B (:18091) and the 8B (cut 2, cut 3); only the URL and the |
| 19 | # model id differ. Needs a server that returns `usage.aien_sampling` (stage S or later). |
| 20 | # |
| 21 | # scripts/sampling-gate.sh <base-url> <model-id> <evidence-dir> [repro.req.json ...] |
| 22 | # <base-url> e.g. http://127.0.0.1:18091 (no /v1) |
| 23 | # env GREEDY_GOLDEN optional own30b-serve-smoke.sh nonstream.json: gate (c) also requires the |
| 24 | # greedy reply to the shared prompt (prompt 1) to equal its content |
| 25 | # env REDACT_REPRO=0 keep the repro replies' text (default 1: only usage, finish, content sha256, |
| 26 | # because the repro prompts belong to another experiment) |
| 27 | # |
| 28 | # Gates (design 2026-10-15-own8b-design.md section 4.1), 4 fixed prompts, max_tokens 48, stream false. |
| 29 | # Contents are compared byte for byte (sha256 of the exact content string). |
| 30 | # (a) replay: seed 1, temperature 1.0, top_p 1.0, top_k 0, twice: identical, 4/4 |
| 31 | # (b) spread: seeds 1, 2, 3 at the same settings, 4/4 prompts. Every reply must report |
| 32 | # mode=sampled and the seed it was sent. A prompt passes with at least 2 distinct replies, |
| 33 | # or with 3 identical replies whose p_repeat^2 >= 0.01 (an honest sampler repeats them with |
| 34 | # at least 1% chance: the model is confident, as for a short list prompt). |
| 35 | # (c) greedy: temperature 0, seeds 1 and 2: identical 4/4 (and prompt 1 equal to GREEDY_GOLDEN) |
| 36 | # (d) repro: each repro request re-sent as is (its own seed, temperature, top_p) with the |
| 37 | # record. PASS when the replies differ, or when they are identical, report their own seeds |
| 38 | # and mode=sampled, and p_repeat^(k-1) >= 0.01 (confidence). Otherwise FAIL (the seed is not |
| 39 | # honoured). No repro files: (d) is NOT_RUN and does not change the verdict. |
| 40 | # p_repeat (from the record) is the chance that an honest draw with another seed repeats the |
| 41 | # reply's sampled draws exactly. Writes <dir>/<gate>-*.json, <dir>/gate.txt (one line per check, |
| 42 | # then VERDICT) and SHA256SUMS. Exit 0 PASS, 1 FAIL, 2 usage or transport error. |
| 43 | set -euo pipefail |
| 44 | [ $# -ge 3 ] || { sed -n '2,29p' "$0"; exit 2; } |
| 45 | base=${1%/} model=$2 dir=$3 |
| 46 | shift 3 |
| 47 | repro=("$@") |
| 48 | redact=${REDACT_REPRO:-1} |
| 49 | mkdir -p "$dir" |
| 50 | out="$dir/gate.txt" |
| 51 | : > "$out" |
| 52 | say() { echo "$*" | tee -a "$out"; } |
| 53 | |
| 54 | PROMPTS=( |
| 55 | "In one sentence, what is a Grace Blackwell GB10?" |
| 56 | "Count from one to ten in words, comma separated." |
| 57 | "Name three colours." |
| 58 | "Give me three numbered steps for checking a bolt is torqued correctly." |
| 59 | ) |
| 60 | |
| 61 | # post NAME BODY: writes NAME.json; retries a 503 server_busy (Retry-After) up to 60 times. |
| 62 | post() { |
| 63 | local name=$1 body=$2 code i |
| 64 | for i in $(seq 1 60); do |
| 65 | code=$(curl -s -o "$dir/$name.json" -w '%{http_code}' "$base/v1/chat/completions" \ |
| 66 | -H 'content-type: application/json' -d "$body") || { echo "sampling-gate: curl failed for $name" >&2; exit 2; } |
| 67 | [ "$code" = 503 ] || break |
| 68 | sleep 1 |
| 69 | done |
| 70 | [ "$code" = 200 ] || { echo "sampling-gate: $name answered HTTP $code: $(head -c 300 "$dir/$name.json")" >&2; exit 2; } |
| 71 | jq -e '.usage.aien_sampling.mode' "$dir/$name.json" > /dev/null \ |
| 72 | || { echo "sampling-gate: $name has no usage.aien_sampling (an engine that does not sample, e.g. the stub, or a server older than stage S)" >&2; exit 2; } |
| 73 | } |
| 74 | # Byte-exact content digest (jq -j: no added newline; the shell never strips anything). |
| 75 | digest() { jq -j '.choices[0].message.content' "$dir/$1.json" | sha256sum | cut -d' ' -f1; } |
| 76 | field() { jq -r ".usage.aien_sampling.$2" "$dir/$1.json"; } |
| 77 | body() { # prompt temperature top_p top_k seed |
| 78 | jq -cn --arg m "$model" --arg p "$1" --argjson t "$2" --argjson tp "$3" --argjson k "$4" --argjson s "$5" \ |
| 79 | '{model: $m, max_tokens: 48, stream: false, temperature: $t, top_p: $tp, top_k: $k, seed: $s, |
| 80 | messages: [{role: "user", content: $p}]}' |
| 81 | } |
| 82 | rec() { # name -> one-line sampling summary |
| 83 | jq -r '.usage.aien_sampling | "mode=\(.mode) seed=\(.seed) sampled=\(.sampled_tokens) greedy=\(.greedy_tokens) kept=\(.kept_min)/\(.kept_median)/\(.kept_mean)/\(.kept_max) kept1=\(.kept_single) top_prob=\(.top_prob_min)/\(.top_prob_mean) p_repeat=\(.p_repeat)" + (if .mutant then " MUTANT=\(.mutant)" else "" end)' "$dir/$1.json" |
| 84 | } |
| 85 | at_least() { awk -v x="$1" -v y="$2" 'BEGIN { exit !(x >= y) }'; } # x >= y (floats) |
| 86 | |
| 87 | pass_a=0 pass_b=0 pass_c=0 |
| 88 | for i in 0 1 2 3; do |
| 89 | n=$((i + 1)) |
| 90 | p=${PROMPTS[$i]} |
| 91 | # (a) replay |
| 92 | post "a-p$n-r1" "$(body "$p" 1.0 1.0 0 1)" |
| 93 | post "a-p$n-r2" "$(body "$p" 1.0 1.0 0 1)" |
| 94 | if [ "$(digest "a-p$n-r1")" = "$(digest "a-p$n-r2")" ]; then pass_a=$((pass_a + 1)); ok=same; else ok=DIFFERENT; fi |
| 95 | say "a p$n $ok | $(rec "a-p$n-r1")" |
| 96 | # (b) spread |
| 97 | honest=1 |
| 98 | for s in 1 2 3; do |
| 99 | post "b-p$n-s$s" "$(body "$p" 1.0 1.0 0 "$s")" |
| 100 | [ "$(field "b-p$n-s$s" mode)" = sampled ] && [ "$(field "b-p$n-s$s" seed)" = "$s" ] || honest=0 |
| 101 | done |
| 102 | distinct=$(for s in 1 2 3; do digest "b-p$n-s$s"; done | sort -u | wc -l) |
| 103 | p2=$(awk -v p="$(field "b-p$n-s1" p_repeat)" 'BEGIN { printf "%.6g", p * p }') |
| 104 | if [ "$honest" = 0 ]; then |
| 105 | why="FAIL (a reply was not sampled with its own seed)" |
| 106 | elif [ "$distinct" -ge 2 ]; then |
| 107 | why=PASS pass_b=$((pass_b + 1)) |
| 108 | elif at_least "$p2" 0.01; then |
| 109 | why="PASS (identical, confident: p_repeat^2=$p2 >= 0.01)" pass_b=$((pass_b + 1)) |
| 110 | else |
| 111 | why="FAIL (identical with p_repeat^2=$p2 < 0.01)" |
| 112 | fi |
| 113 | say "b p$n distinct=$distinct/3 $why | $(rec "b-p$n-s1") | $(rec "b-p$n-s2") | $(rec "b-p$n-s3")" |
| 114 | # (c) greedy |
| 115 | post "c-p$n-s1" "$(body "$p" 0 1.0 0 1)" |
| 116 | post "c-p$n-s2" "$(body "$p" 0 1.0 0 2)" |
| 117 | ok=1 |
| 118 | [ "$(digest "c-p$n-s1")" = "$(digest "c-p$n-s2")" ] || ok=0 |
| 119 | golden="" |
| 120 | if [ "$n" = 1 ] && [ -n "${GREEDY_GOLDEN:-}" ]; then |
| 121 | if [ "$(digest "c-p$n-s1")" = "$(jq -j '.choices[0].message.content' "$GREEDY_GOLDEN" | sha256sum | cut -d' ' -f1)" ]; then |
| 122 | golden=" golden=equal" |
| 123 | else |
| 124 | golden=" golden=DIFFERENT" ok=0 |
| 125 | fi |
| 126 | fi |
| 127 | [ "$ok" = 1 ] && pass_c=$((pass_c + 1)) |
| 128 | say "c p$n $([ "$ok" = 1 ] && echo same || echo DIFFERENT)$golden | $(rec "c-p$n-s1")" |
| 129 | done |
| 130 | |
| 131 | verdict=PASS |
| 132 | for g in a b c; do |
| 133 | v=pass_$g |
| 134 | r=$([ "${!v}" = 4 ] && echo PASS || echo FAIL) |
| 135 | [ "$r" = PASS ] || verdict=FAIL |
| 136 | say "gate $g ${!v}/4 $r" |
| 137 | done |
| 138 | |
| 139 | # (d) repro |
| 140 | if [ ${#repro[@]} -eq 0 ]; then |
| 141 | say "gate d NOT_RUN (no repro request files given)" |
| 142 | else |
| 143 | k=0 honest=1 pmin=1 |
| 144 | hashes=() |
| 145 | for f in "${repro[@]}"; do |
| 146 | k=$((k + 1)) |
| 147 | post "d-r$k" "$(jq -c --arg m "$model" '.model = $m | .stream = false' "$f")" |
| 148 | h=$(digest "d-r$k") |
| 149 | hashes+=("$h") |
| 150 | [ "$(field "d-r$k" mode)" = sampled ] && [ "$(field "d-r$k" seed)" = "$(jq -r .seed "$f")" ] || honest=0 |
| 151 | pr=$(field "d-r$k" p_repeat) |
| 152 | at_least "$pr" "$pmin" || pmin=$pr |
| 153 | say "d r$k $(jq -r '"seed=\(.seed) temperature=\(.temperature) top_p=\(.top_p) top_k=\(.top_k // "default")"' "$f") content_sha256=${h:0:16} | $(rec "d-r$k")" |
| 154 | if [ "$redact" = 1 ]; then |
| 155 | t=$(mktemp) |
| 156 | jq --arg h "$h" '{usage, finish: .choices[0].finish_reason, content_sha256: $h}' "$dir/d-r$k.json" > "$t" \ |
| 157 | || { rm -f "$t" "$dir/d-r$k.json"; echo "sampling-gate: redaction of d-r$k failed; reply removed" >&2; exit 2; } |
| 158 | mv "$t" "$dir/d-r$k.json" |
| 159 | fi |
| 160 | done |
| 161 | distinct=$(printf '%s\n' "${hashes[@]}" | sort -u | wc -l) |
| 162 | # Kept-set sizes of every sampled step of every repro reply. |
| 163 | kept_all() { for i in $(seq 1 "$k"); do jq -r '.usage.aien_sampling.kept[]' "$dir/d-r$i.json"; done; } |
| 164 | mean=$(kept_all | awk '{s += $1; n++} END {if (n) printf "%.4f", s / n; else print "0"}') |
| 165 | steps=$(kept_all | wc -l) |
| 166 | choice=$(kept_all | awk '$1 > 1' | wc -l) |
| 167 | chance=$(awk -v p="$pmin" -v k="$k" 'BEGIN { printf "%.6g", p ^ (k - 1) }') |
| 168 | stats="kept_mean=$mean steps_with_choice=$choice/$steps p_repeat_min=$pmin" |
| 169 | if [ "$honest" = 0 ]; then |
| 170 | say "gate d FAIL a reply was not sampled with its own seed; $stats" |
| 171 | verdict=FAIL |
| 172 | elif [ "$distinct" -ge 2 ]; then |
| 173 | say "gate d PASS distinct=$distinct/$k (the seeds produced different replies) $stats" |
| 174 | elif at_least "$chance" 0.01; then |
| 175 | say "gate d PASS identical replies explained by confidence: p_repeat^$((k - 1))=$chance >= 0.01, $stats" |
| 176 | else |
| 177 | say "gate d FAIL identical replies with p_repeat^$((k - 1))=$chance < 0.01: the seed is not honoured; $stats" |
| 178 | verdict=FAIL |
| 179 | fi |
| 180 | fi |
| 181 | |
| 182 | say "VERDICT $verdict" |
| 183 | (cd "$dir" && find . -type f ! -name SHA256SUMS | sort | sed 's|^\./||' | xargs sha256sum > SHA256SUMS) |
| 184 | [ "$verdict" = PASS ] |