interplane.Get Omega
Read in your language

aien-sovereign-core-mv

AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.

Brought by drakestapleton

branch main Branches Tags History

aien-sovereign-core-mv / scripts / sampling-gate.sh

185 lines · 9133 bytes Raw Blame History
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.
43set -euo pipefail
44[ $# -ge 3 ] || { sed -n '2,29p' "$0"; exit 2; }
45base=${1%/} model=$2 dir=$3
46: "${AIEN_SERVE_HTTP_BEARER_TOKEN:?AIEN_SERVE_HTTP_BEARER_TOKEN required for chat}"
47shift 3
48repro=("$@")
49redact=${REDACT_REPRO:-1}
50mkdir -p "$dir"
51out="$dir/gate.txt"
52: > "$out"
53say() { echo "$*" | tee -a "$out"; }
54
55PROMPTS=(
56 "In one sentence, what is a Grace Blackwell GB10?"
57 "Count from one to ten in words, comma separated."
58 "Name three colours."
59 "Give me three numbered steps for checking a bolt is torqued correctly."
60)
61
62# post NAME BODY: writes NAME.json; retries a 503 server_busy (Retry-After) up to 60 times.
63post() {
64 local name=$1 body=$2 code i
65 for i in $(seq 1 60); do
66 code=$(curl -s -o "$dir/$name.json" -w '%{http_code}' "$base/v1/chat/completions" \
67 -H "Authorization: Bearer $AIEN_SERVE_HTTP_BEARER_TOKEN" -H 'content-type: application/json' -d "$body") || { echo "sampling-gate: curl failed for $name" >&2; exit 2; }
68 [ "$code" = 503 ] || break
69 sleep 1
70 done
71 [ "$code" = 200 ] || { echo "sampling-gate: $name answered HTTP $code: $(head -c 300 "$dir/$name.json")" >&2; exit 2; }
72 jq -e '.usage.aien_sampling.mode' "$dir/$name.json" > /dev/null \
73 || { 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; }
74}
75# Byte-exact content digest (jq -j: no added newline; the shell never strips anything).
76digest() { jq -j '.choices[0].message.content' "$dir/$1.json" | sha256sum | cut -d' ' -f1; }
77field() { jq -r ".usage.aien_sampling.$2" "$dir/$1.json"; }
78body() { # prompt temperature top_p top_k seed
79 jq -cn --arg m "$model" --arg p "$1" --argjson t "$2" --argjson tp "$3" --argjson k "$4" --argjson s "$5" \
80 '{model: $m, max_tokens: 48, stream: false, temperature: $t, top_p: $tp, top_k: $k, seed: $s,
81 messages: [{role: "user", content: $p}]}'
82}
83rec() { # name -> one-line sampling summary
84 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"
85}
86at_least() { awk -v x="$1" -v y="$2" 'BEGIN { exit !(x >= y) }'; } # x >= y (floats)
87
88pass_a=0 pass_b=0 pass_c=0
89for i in 0 1 2 3; do
90 n=$((i + 1))
91 p=${PROMPTS[$i]}
92 # (a) replay
93 post "a-p$n-r1" "$(body "$p" 1.0 1.0 0 1)"
94 post "a-p$n-r2" "$(body "$p" 1.0 1.0 0 1)"
95 if [ "$(digest "a-p$n-r1")" = "$(digest "a-p$n-r2")" ]; then pass_a=$((pass_a + 1)); ok=same; else ok=DIFFERENT; fi
96 say "a p$n $ok | $(rec "a-p$n-r1")"
97 # (b) spread
98 honest=1
99 for s in 1 2 3; do
100 post "b-p$n-s$s" "$(body "$p" 1.0 1.0 0 "$s")"
101 [ "$(field "b-p$n-s$s" mode)" = sampled ] && [ "$(field "b-p$n-s$s" seed)" = "$s" ] || honest=0
102 done
103 distinct=$(for s in 1 2 3; do digest "b-p$n-s$s"; done | sort -u | wc -l)
104 p2=$(awk -v p="$(field "b-p$n-s1" p_repeat)" 'BEGIN { printf "%.6g", p * p }')
105 if [ "$honest" = 0 ]; then
106 why="FAIL (a reply was not sampled with its own seed)"
107 elif [ "$distinct" -ge 2 ]; then
108 why=PASS pass_b=$((pass_b + 1))
109 elif at_least "$p2" 0.01; then
110 why="PASS (identical, confident: p_repeat^2=$p2 >= 0.01)" pass_b=$((pass_b + 1))
111 else
112 why="FAIL (identical with p_repeat^2=$p2 < 0.01)"
113 fi
114 say "b p$n distinct=$distinct/3 $why | $(rec "b-p$n-s1") | $(rec "b-p$n-s2") | $(rec "b-p$n-s3")"
115 # (c) greedy
116 post "c-p$n-s1" "$(body "$p" 0 1.0 0 1)"
117 post "c-p$n-s2" "$(body "$p" 0 1.0 0 2)"
118 ok=1
119 [ "$(digest "c-p$n-s1")" = "$(digest "c-p$n-s2")" ] || ok=0
120 golden=""
121 if [ "$n" = 1 ] && [ -n "${GREEDY_GOLDEN:-}" ]; then
122 if [ "$(digest "c-p$n-s1")" = "$(jq -j '.choices[0].message.content' "$GREEDY_GOLDEN" | sha256sum | cut -d' ' -f1)" ]; then
123 golden=" golden=equal"
124 else
125 golden=" golden=DIFFERENT" ok=0
126 fi
127 fi
128 [ "$ok" = 1 ] && pass_c=$((pass_c + 1))
129 say "c p$n $([ "$ok" = 1 ] && echo same || echo DIFFERENT)$golden | $(rec "c-p$n-s1")"
130done
131
132verdict=PASS
133for g in a b c; do
134 v=pass_$g
135 r=$([ "${!v}" = 4 ] && echo PASS || echo FAIL)
136 [ "$r" = PASS ] || verdict=FAIL
137 say "gate $g ${!v}/4 $r"
138done
139
140# (d) repro
141if [ ${#repro[@]} -eq 0 ]; then
142 say "gate d NOT_RUN (no repro request files given)"
143else
144 k=0 honest=1 pmin=1
145 hashes=()
146 for f in "${repro[@]}"; do
147 k=$((k + 1))
148 post "d-r$k" "$(jq -c --arg m "$model" '.model = $m | .stream = false' "$f")"
149 h=$(digest "d-r$k")
150 hashes+=("$h")
151 [ "$(field "d-r$k" mode)" = sampled ] && [ "$(field "d-r$k" seed)" = "$(jq -r .seed "$f")" ] || honest=0
152 pr=$(field "d-r$k" p_repeat)
153 at_least "$pr" "$pmin" || pmin=$pr
154 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")"
155 if [ "$redact" = 1 ]; then
156 t=$(mktemp)
157 jq --arg h "$h" '{usage, finish: .choices[0].finish_reason, content_sha256: $h}' "$dir/d-r$k.json" > "$t" \
158 || { rm -f "$t" "$dir/d-r$k.json"; echo "sampling-gate: redaction of d-r$k failed; reply removed" >&2; exit 2; }
159 mv "$t" "$dir/d-r$k.json"
160 fi
161 done
162 distinct=$(printf '%s\n' "${hashes[@]}" | sort -u | wc -l)
163 # Kept-set sizes of every sampled step of every repro reply.
164 kept_all() { for i in $(seq 1 "$k"); do jq -r '.usage.aien_sampling.kept[]' "$dir/d-r$i.json"; done; }
165 mean=$(kept_all | awk '{s += $1; n++} END {if (n) printf "%.4f", s / n; else print "0"}')
166 steps=$(kept_all | wc -l)
167 choice=$(kept_all | awk '$1 > 1' | wc -l)
168 chance=$(awk -v p="$pmin" -v k="$k" 'BEGIN { printf "%.6g", p ^ (k - 1) }')
169 stats="kept_mean=$mean steps_with_choice=$choice/$steps p_repeat_min=$pmin"
170 if [ "$honest" = 0 ]; then
171 say "gate d FAIL a reply was not sampled with its own seed; $stats"
172 verdict=FAIL
173 elif [ "$distinct" -ge 2 ]; then
174 say "gate d PASS distinct=$distinct/$k (the seeds produced different replies) $stats"
175 elif at_least "$chance" 0.01; then
176 say "gate d PASS identical replies explained by confidence: p_repeat^$((k - 1))=$chance >= 0.01, $stats"
177 else
178 say "gate d FAIL identical replies with p_repeat^$((k - 1))=$chance < 0.01: the seed is not honoured; $stats"
179 verdict=FAIL
180 fi
181fi
182
183say "VERDICT $verdict"
184(cd "$dir" && find . -type f ! -name SHA256SUMS | sort | sed 's|^\./||' | xargs sha256sum > SHA256SUMS)
185[ "$verdict" = PASS ]