diff --git a/om/README.md b/om/README.md index 79e74ce0..8319b7b7 100644 --- a/om/README.md +++ b/om/README.md @@ -12,6 +12,7 @@ aien-architecture-mv#300). | `tur-ledger/tur-ledger.om` | Aienet-0 test-credit ledger CLI: v2 genesis pins issuer, independent verifier and account/node policy. Canonical mutations require issuer signatures; settlement also requires verifier authorization over signed-job, result and attestation digests and the reserved quote. Single-writer hash chain, fsync, recovery, integer conservation and exactly-once settle remain. See `tur-ledger/README.md`. | Host admission/durability tests and guard mutants in `tur-ledger/check.sh`; no wallet, Core RPC or public economic qualification. Historical unsigned v1 receipts remain historical. | | `flint/manifest.om` | Bounded manifest reader for the pinned Flint BF16 checkpoint (RecursiveSI/flint `2a247be0`, Core #802 P0): admits config, index and every safetensors header, refuses anything outside the pinned layout with a distinct exit code, prints the tensor inventory TSV. See `flint/README.md`. | Host: `flint/check.sh` 89/89 with the downloaded checkpoint and the operator arm (38 refusal fixtures, 25 guard mutants; 70 without the operator arm). | | `flint/ops.om` | Flint P0 forward operators recomputed in f64 from the real BF16 weights (Core #802): RMSNorm, q/k/v with q/k norm and RoPE, attention with sinks and the 513-key window, the sigmoid top-6 router, shared expert, routed expert and MoE block, each within twice a first-order bound on the reference's f32 error (`flint/test/TOLERANCE.md`). | Host: in `flint/check.sh` 89/89, 9 operator fixtures and 2 router controls against the pinned transformers reference (fixtures sha256 pinned), 6 forward mutants caught. Layer, logits, generation, bf16-activation parity and GPU NOT_RUN. | +| `golden/golden.om` | Golden-token recorder and comparator (Core #853): writes greedy request bodies, takes `usage.aien_token_ids` from engine replies into an `aien-golden-record/1` file with the engine sha256, model repo and revision, sampling, prompts sha256, host id and UTC time; `cmp` reports the first divergence index per prompt. `golden/fake.om` is a test-only HTTP responder. See `golden/README.md`. | Host: `golden/check.sh` 27/27 (9 comparator arms on synthetic fixtures, 15 recorder arms against `fake.om`, 3 guard mutants). No real reference recorded. | | `vendor/sha256.om` | Incremental SHA-256 in Omega, vendored from benchmarks-mv. | Exercised through `world.om` and `tur-ledger.om`. | | `vendor/ec.om` | Native Omega ECDSA P-256/P-384 verification, byte-identical to omega-mv `6999fa9e6dae4847c155ef4eacaece74f491d3ca` `om/tls/ec.om`. | P-256/SHA-256 strict DER verification used by `tur-ledger.om`; OpenSSL is a test-only signing oracle, not a runtime dependency. | | `vendor/json.om` | Seeded JSON tape parser, byte-identical to omega-mv `bf54b19fa590e8ae925c0e80815516dbf41e7326` `seed/json/json.om`. | Used by `flint/manifest.om`, `expert-bytes/expert-bytes.om` and `crumb-coverage/crumb-coverage.om` for config, safetensors headers and `.crumb` files. | diff --git a/om/golden/README.md b/om/golden/README.md new file mode 100644 index 00000000..27362377 --- /dev/null +++ b/om/golden/README.md @@ -0,0 +1,57 @@ +# Golden-token harness (Core #853, DRA-52) + +Records the greedy token ids an aien-serve-http engine produces for a fixed prompt set, and compares +two such records token by token. Every judgment is made in `golden.om`; `record.sh` sequences and +curl moves bytes. Formats: [SCHEMA.md](SCHEMA.md). + +| File | Role | +|---|---| +| `golden.om` | `req` writes request bodies, `rec` turns engine replies into a record, `cmp` compares two records | +| `fake.om` | TEST ONLY: HTTP responder on a random 127.0.0.1 port serving canned replies; never an engine | +| `build.sh` | Builds a `.om` with the pinned omc (omega-mv `bf54b19f`, sha256 `62775d2a...3712b`); exit 3 NOT_RUN otherwise | +| `record.sh` | Sequencer: build, `golden req`, one curl POST per prompt, `golden rec`, no-clobber link to `--out` | +| `check.sh` | Host arms with synthetic fixtures, the fake responder and guard mutants | +| `fixtures/` | Hand-written records labelled `SYNTHETIC` (`synthetic/not-a-model`, revision of zeros) and a 2-prompt set | + +Reference records are not committed. A reference is only meaningful when recorded on an independent +machine with a known engine sha256 and model revision. + +## Record + +```sh +OMC=/path/to/omega-mv/seed/out/omc bash om/golden/record.sh \ + --url http://127.0.0.1:PORT --prompts prompts.json \ + --model-repo ORG/NAME --model-rev <40 hex> --max-tokens 256 \ + --engine-sha256 "$(sha256sum /path/to/aien-serve-http | cut -c1-64)" \ + --host-id "$(hostname -s)" --label REAL --out ref.json [--header-file auth.hdr] +``` + +`--label REAL` is for engine replies only; a run against a test responder must say `FAKE`. +Exit: 0 written, 2 usage or existing `--out`, 3 compiler not pinned, 4 prompt/file refusal, +5 engine reply refused (error, no `usage.aien_token_ids`, not greedy), 6 transport. + +## Compare + +```sh +golden cmp ref.json cand.json +``` + +Exit 0 every prompt matches, 1 a token differs (`DIVERGE index= ref= cand=`), +3 not comparable (label, engine sha256, model repo or revision, sampling, max_tokens, prompts sha256 +or prompt ids differ), 4 file or schema refusal, 2 usage. + +## Check + +```sh +OMC=/path/to/omega-mv/seed/out/omc bash om/golden/check.sh +``` + +27 arms: 9 comparator arms on the synthetic fixtures (match, 1-token divergence with its index, +engine and model-revision header mismatch, early end, malformed JSON, non-greedy record, unknown +key, usage), 15 recorder arms against `fake.om` (ids from `usage.aien_token_ids`, FAKE label, +prompts sha256, UTC window, request bodies byte-equal to `golden req`, header file sent, re-record +divergence, sampled reply, missing ids, HTTP 500, existing output) and 3 guard mutants of +`golden.om` (header check disabled, divergence index off by one, sampled replies accepted), each of +which must fail its arm. + +NOT_RUN: a real reference recorded from the served model on an independent machine. diff --git a/om/golden/SCHEMA.md b/om/golden/SCHEMA.md new file mode 100644 index 00000000..ad4388ed --- /dev/null +++ b/om/golden/SCHEMA.md @@ -0,0 +1,52 @@ +# Golden-token file schemas + +`golden.om` admits these two shapes and refuses anything else (unknown or missing keys, wrong types, +out-of-range values) with exit 4. + +## Prompt set: `aien-golden-prompts/1` + +```json +{"schema":"aien-golden-prompts/1","enable_thinking":false,"prompts":[ +{"id":"p0","content":"..."} +]} +``` + +- `enable_thinking`: bool, sent as `chat_template_kwargs.enable_thinking` in every request. +- `prompts`: 1 to 1024 objects with exactly `id` (unique, 1 to 64 of `[A-Za-z0-9._-]`) and `content` + (non-empty JSON string, copied into the request as the same JSON lexeme). +- The record pins the prompt set by `prompts_sha256`, the sha256 of the file's exact bytes. + +Each request body is one line: + +```json +{"model":"","messages":[{"role":"user","content":}],"temperature":0,"top_p":1,"seed":0,"max_tokens":,"stream":false,"chat_template_kwargs":{"enable_thinking":},"aien_return_token_ids":true} +``` + +An engine reply is refused (exit 5) when it carries `error`, lacks `usage.aien_token_ids` (an array +of u32), or has `usage.aien_sampling` that is not an object with `"mode":"greedy"`. + +## Record: `aien-golden-record/1` + +```json +{"schema":"aien-golden-record/1","label":"REAL","engine_sha256":"<64 hex>","model_repo":"org/name","model_rev":"<40 hex>","sampling":{"temperature":0,"top_p":1,"seed":0,"enable_thinking":false},"max_tokens":256,"prompts_sha256":"<64 hex>","host_id":"","utc":"YYYY-MM-DDTHH:MM:SSZ","prompts":[ +{"id":"p0","ids":[1,2,3]} +]} +``` + +| Key | Rule | Compared by `cmp` | +|---|---|---| +| `label` | `REAL` (recorded from an engine), `FAKE` (recorded from a test responder), `SYNTHETIC` (hand-written fixture) | yes | +| `engine_sha256` | 64 lowercase hex: sha256 of the serving binary | yes | +| `model_repo` | 1 to 96 of `[A-Za-z0-9._-/]` | yes | +| `model_rev` | 40 lowercase hex: the model repository commit | yes | +| `sampling.temperature`, `top_p`, `seed`, `enable_thinking` | integer 0, number, unsigned integer, bool | yes, as lexemes | +| `max_tokens` | 1 to 1000000 | yes | +| `prompts_sha256` | 64 lowercase hex | yes | +| `host_id` | 1 to 64 of `[A-Za-z0-9._-]` | no, printed | +| `utc` | recorder wall clock at record time | no, printed | +| `prompts` | 1 to 1024 `{id, ids}`; ids are u32 | ids per prompt, in order | + +`cmp REF CAND` exits 3 (`NOTCOMPARABLE ...`) when any compared header field, the prompt count or a +prompt id differs, before looking at tokens. Otherwise it prints `MATCH n=` or +`DIVERGE index= ref= cand=` per prompt, where K is the first differing position and +`end` marks a list that stopped early, and exits 1 when any prompt diverges. diff --git a/om/golden/build.sh b/om/golden/build.sh new file mode 100755 index 00000000..46574401 --- /dev/null +++ b/om/golden/build.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +# Build om/golden/golden.om (or another source given as $2) with the pinned seeded omc. +# Usage: OMC=/path/to/omega-mv/seed/out/omc bash om/golden/build.sh OUT [SOURCE] +# Exit 0 built, 3 omc missing or not the pinned compiler (NOT_RUN), 1 compile error. +set -u +PIN=62775d2a391cfef7cde64e387655caa9809a267093112910ec76bfa568f3712b # omega-mv bf54b19f seed/out/omc +root=$(cd "$(dirname "$0")/../.." && pwd) +out=${1:?usage: OMC=... build.sh OUT [SOURCE]} +src=${2:-$root/om/golden/golden.om} +OMC=${OMC:-${OMEGA_OMC:-}} +[ -n "$OMC" ] && [ -x "$OMC" ] || { echo "NOT_RUN: set OMC to omega-mv bf54b19f seed/out/omc" >&2; exit 3; } +got=$(sha256sum "$OMC" | cut -c1-64) +[ "$got" = "$PIN" ] || { echo "NOT_RUN: omc sha256 $got, pinned $PIN" >&2; exit 3; } +{ printf 'om2\n'; cat "$root/om/vendor/json.om" "$root/om/vendor/sha256.om" "$src"; } | "$OMC" > "$out" 2> "$out.err" \ + || { echo "compile $src: $(cat "$out.err")" >&2; rm -f "$out" "$out.err"; exit 1; } +rm -f "$out.err" +chmod 700 "$out" diff --git a/om/golden/check.sh b/om/golden/check.sh new file mode 100755 index 00000000..81ae3da1 --- /dev/null +++ b/om/golden/check.sh @@ -0,0 +1,139 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: Apache-2.0 +# om/golden/check.sh: host arms for the golden-token harness (Core #853, DRA-52). +# Usage: OMC=/path/to/omega-mv/seed/out/omc bash om/golden/check.sh +# Every engine reply here comes from fake.om on a random 127.0.0.1 port and every record it makes is +# labelled FAKE. No live engine, model or GPU is touched. Exit 0 all arms as expected, 1 an arm +# failed, 3 omc missing or not pinned (NOT_RUN). +set -u +here=$(cd "$(dirname "$0")" && pwd) +fx=$here/fixtures +t=$(mktemp -d) +trap 'rm -rf "$t"' EXIT +bash "$here/build.sh" "$t/golden" || exit $? +bash "$here/build.sh" "$t/fake" "$here/fake.om" || exit $? +G=$t/golden +fail=0 +ok() { echo "PASS $1"; } +bad() { echo "FAIL $1: $2"; fail=1; } +# arm NAME WANT_RC WANT_GREP cmd...: exit code must be WANT_RC; stdout+stderr must contain WANT_GREP +arm() { + local name=$1 want=$2 pat=$3; shift 3 + "$@" > "$t/out" 2>&1; local rc=$? + if [ "$rc" != "$want" ]; then bad "$name" "exit $rc, want $want: $(cat "$t/out")"; return; fi + if [ -n "$pat" ] && ! grep -qxF -- "$pat" "$t/out"; then bad "$name" "no line '$pat' in: $(cat "$t/out")"; return; fi + ok "$name (exit $rc${pat:+, '$pat'})" +} + +echo "== comparator, synthetic fixtures" +arm cmp-match 0 'golden-cmp prompts=2 match=2 diverge=0' "$G" cmp "$fx/synthetic-ref.json" "$fx/synthetic-match.json" +arm cmp-diverge-1-token 1 'DIVERGE p1 index=1 ref=20 cand=21' "$G" cmp "$fx/synthetic-ref.json" "$fx/synthetic-diverge.json" +arm cmp-header-engine 3 "NOTCOMPARABLE engine_sha256 ref=\"$(printf 'e%.0s' $(seq 64))\" cand=\"$(printf 'f%.0s' $(seq 64))\"" "$G" cmp "$fx/synthetic-ref.json" "$fx/synthetic-header.json" +sed 's/"model_rev":"0000000000000000000000000000000000000000"/"model_rev":"1111111111111111111111111111111111111111"/' "$fx/synthetic-match.json" > "$t/rev.json" +arm cmp-header-model-rev 3 'NOTCOMPARABLE model_rev ref="0000000000000000000000000000000000000000" cand="1111111111111111111111111111111111111111"' "$G" cmp "$fx/synthetic-ref.json" "$t/rev.json" +sed 's/"ids":\[10,20,30\]/"ids":[10,20]/' "$fx/synthetic-match.json" > "$t/short.json" +arm cmp-prefix-ends-early 1 'DIVERGE p1 index=2 ref=30 cand=end' "$G" cmp "$fx/synthetic-ref.json" "$t/short.json" +head -c 100 "$fx/synthetic-match.json" > "$t/trunc.json" +arm cmp-malformed-json 4 'golden: refused 4 json' "$G" cmp "$fx/synthetic-ref.json" "$t/trunc.json" +sed 's/"temperature":0,/"temperature":0.7,/' "$fx/synthetic-match.json" > "$t/temp.json" +arm cmp-not-greedy-record 4 'golden: refused 4 record-schema' "$G" cmp "$fx/synthetic-ref.json" "$t/temp.json" +sed 's/"label":"SYNTHETIC",/"label":"SYNTHETIC","extra":1,/' "$fx/synthetic-match.json" > "$t/extra.json" +arm cmp-unknown-key 4 'golden: refused 4 record-schema' "$G" cmp "$fx/synthetic-ref.json" "$t/extra.json" +arm usage 2 '' "$G" cmp "$fx/synthetic-ref.json" + +echo "== recorder against fake.om (FAKE replies, random 127.0.0.1 port)" +# canned DIR K STATUS BODY: one full HTTP/1.1 response for connection K +canned() { + mkdir -p "$1" + printf 'HTTP/1.1 %s\r\nContent-Type: application/json\r\nContent-Length: %d\r\nConnection: close\r\n\r\n%s' \ + "$3" "${#4}" "$4" > "$1/$2.http" +} +reply() { printf '{"id":"FAKE","object":"chat.completion","choices":[{"index":0,"message":{"role":"assistant","content":"FAKE"},"finish_reason":"length"}],"usage":{"prompt_tokens":3,"completion_tokens":3,"total_tokens":6,"aien_token_ids":[%s],"aien_sampling":{"mode":"%s","temperature":0.0}}}' "$1" "$2"; } +# serve NAME N: start fake for N connections on canned dir $t/NAME.c, capture to $t/NAME.cap; sets port +serve() { + mkdir -p "$t/$1.cap" + timeout 60 "$t/fake" "$t/$1.port" "$t/$1.c" "$t/$1.cap" "$2" 2> "$t/$1.fakeerr" & + for _ in $(seq 100); do [ -s "$t/$1.port" ] && break; sleep 0.1; done + port=$(cat "$t/$1.port") +} +eng=$(printf 'e%.0s' $(seq 64)) +rev=0000000000000000000000000000000000000000 +printf 'Authorization: Bearer synthetic-test-token\n' > "$t/hdr" +rec() { # rec NAME OUT + GOLDEN=$G bash "$here/record.sh" --url "http://127.0.0.1:$port" --prompts "$fx/synthetic-prompts.json" \ + --model-repo synthetic/not-a-model --model-rev $rev --max-tokens 8 --engine-sha256 "$eng" \ + --host-id fake-host --label FAKE --out "$2" --header-file "$t/hdr" --timeout 20 +} +canned "$t/a.c" 0 '200 OK' "$(reply 1,2,3,4,5 greedy)" +canned "$t/a.c" 1 '200 OK' "$(reply 10,20,30 greedy)" +serve a 2 +before=$(date -u +%s) +arm rec-fake-success 0 '' rec a "$t/a.json" +after=$(date -u +%s) +wait +if [ -s "$t/a.json" ]; then + grep -qxF '{"id":"p0","ids":[1,2,3,4,5]},' "$t/a.json" && grep -qxF '{"id":"p1","ids":[10,20,30]}' "$t/a.json" \ + && ok "rec-ids-taken-from-usage" || bad rec-ids-taken-from-usage "$(cat "$t/a.json")" + grep -qF '"label":"FAKE"' "$t/a.json" && ok "rec-record-says-FAKE" || bad rec-record-says-FAKE "no FAKE label" + ps=$(sha256sum "$fx/synthetic-prompts.json" | cut -c1-64) + grep -qF "\"prompts_sha256\":\"$ps\"" "$t/a.json" && ok "rec-prompts-sha256 = sha256sum ($ps)" || bad rec-prompts-sha256 "want $ps" + utc=$(grep -o '"utc":"[^"]*"' "$t/a.json" | cut -d'"' -f4); us=$(date -u -d "$utc" +%s) + [ "$us" -ge "$before" ] && [ "$us" -le "$after" ] && ok "rec-utc $utc within [$before,$after]" || bad rec-utc "$utc not within [$before,$after]" + "$G" req "$fx/synthetic-prompts.json" synthetic/not-a-model 8 "$(mkdir "$t/rq" && echo "$t/rq")" > /dev/null + for i in 0 1; do + tail -c "$(stat -c %s "$t/rq/$i.json")" "$t/a.cap/$i.http" | cmp -s - "$t/rq/$i.json" \ + && ok "rec-request-body-$i byte-equal to golden req" || bad "rec-request-body-$i" "differs" + done + grep -qaF 'Authorization: Bearer synthetic-test-token' "$t/a.cap/0.http" && ok "rec-header-file-sent" || bad rec-header-file-sent "missing" +fi +canned "$t/b.c" 0 '200 OK' "$(reply 1,2,3,4,5 greedy)" +canned "$t/b.c" 1 '200 OK' "$(reply 10,20,31 greedy)" +serve b 2 +arm rec-fake-rerecord 0 '' rec b "$t/b.json" +wait +arm rec-then-cmp-divergence 1 'DIVERGE p1 index=2 ref=30 cand=31' "$G" cmp "$t/a.json" "$t/b.json" +canned "$t/c.c" 0 '200 OK' "$(reply 1,2,3,4,5 greedy)" +canned "$t/c.c" 1 '200 OK' "$(reply 10,20,30 sampled)" +serve c 2 +arm rec-sampled-reply-refused 5 'golden: refused 5 not-greedy' rec c "$t/c.json" +wait +[ ! -e "$t/c.json" ] && ok "rec-refusal-leaves-no-record" || bad rec-refusal-leaves-no-record "$t/c.json written" +canned "$t/d.c" 0 '200 OK' '{"choices":[],"usage":{"prompt_tokens":3}}' +canned "$t/d.c" 1 '200 OK' "$(reply 10,20,30 greedy)" +serve d 2 +arm rec-missing-token-ids-refused 5 'golden: refused 5 no-usage-aien_token_ids' rec d "$t/d.json" +wait +canned "$t/e.c" 0 '500 Internal Server Error' '{"error":{"message":"FAKE"}}' +serve e 1 +arm rec-http-500-transport 6 'record: refused 6 transport prompt 0' rec e "$t/e.json" +wait +arm rec-existing-out-refused 2 "record: refused 2 $t/a.json exists" rec x "$t/a.json" + +echo "== negative controls: mutants of golden.om must fail their arm" +mutant() { # mutant NAME FROM TO: replace exactly one source line, build, set M + awk -v f="$2" -v r="$3" '$0 == f { $0 = r; n++ } { print } END { if (n != 1) exit 9 }' "$here/golden.om" > "$t/$1.om" \ + || { bad "$1" "line '$2' not unique"; return 1; } + bash "$here/build.sh" "$t/$1" "$t/$1.om" || { bad "$1" "mutant did not build"; return 1; } + M=$t/$1 +} +mflip() { # mflip NAME CHECK-CMD...: the arm must now fail + "${@:2}" > "$t/mout" 2>&1 && bad "$1" "mutant survived" || ok "$1 killed" +} +if mutant M1-ignore-header 'if gt bad 0' 'if gt bad 99'; then + mflip M1-ignore-header bash -c '"$0" cmp "$1" "$2"; [ $? = 3 ]' "$M" "$fx/synthetic-ref.json" "$fx/synthetic-header.json" +fi +if mutant M2-index-off-by-one 'ret k' 'ret add k 1'; then + mflip M2-index-off-by-one bash -c '"$0" cmp "$1" "$2" | grep -qxF "DIVERGE p1 index=1 ref=20 cand=21"' "$M" "$fx/synthetic-ref.json" "$fx/synthetic-diverge.json" +fi +if mutant M3-accept-sampled 'die 5 tsampled' 'ret'; then + canned "$t/m.c" 0 '200 OK' "$(reply 1,2,3,4,5 greedy)" + canned "$t/m.c" 1 '200 OK' "$(reply 10,20,30 sampled)" + serve m 2 + G0=$G; G=$M + m3() { rec m "$t/m.json" > /dev/null 2>&1; [ $? = 5 ]; } + mflip M3-accept-sampled m3 + G=$G0 + wait +fi +[ "$fail" = 0 ] && echo "golden check: all arms as expected" || echo "golden check: FAILED" +exit "$fail" diff --git a/om/golden/fake.om b/om/golden/fake.om new file mode 100644 index 00000000..0862a217 --- /dev/null +++ b/om/golden/fake.om @@ -0,0 +1,329 @@ +note SPDX-License-Identifier: Apache-2.0 +note om/golden/fake.om: TEST ONLY fake HTTP responder for om/golden/check.sh. It is not an engine and +note never produces a REAL record: check.sh records against it with label FAKE. +note fake PORTFILE CANNED CAPTURE N +note Binds 127.0.0.1 on a kernel-chosen port, writes "PORT\n" to PORTFILE (created, never overwritten), +note then serves exactly N connections in order: connection k reads one HTTP/1.1 request (headers up to +note 65536 bytes, body by Content-Length), stores the raw request bytes as CAPTURE/k.http and answers +note with the bytes of CANNED/k.http verbatim. Exit 0 after N connections; 10 usage, 11 socket, +note 12 file, 13 malformed request. + +const FQCAP ulong 1048576 +const FHCAP ulong 65536 + +global fa uchar 8192 +global fas ulong 8 +global fal ulong 8 +global fp uchar 4200 +global fq uchar 1048576 +global fr uchar 1048576 +global fb uchar 64 +global fst uchar 128 + +text tfpre "fake: exit " +text tfsuf ".http" +text tfcl "content-length:" + +fn fx code int to void +d = new uchar 3 0 +w = write 2 tfpre 0 len tfpre +put d 0 uchar add 48 div code 10 +put d 1 uchar add 48 rem code 10 +put d 2 10 +w = write 2 d 0 3 +exit code +end + +note fpath: fp = argument a, a slash, decimal k, .http; returns the length +fn fpath a ulong k ulong to ulong +n = get fal a +if or eq n 0 gt n 4096 +fx 10 +end +o = get fas a +for i ulong n +put fp i get fa add o i +end +put fp n 47 +n = add n 1 +if ge k 100 +put fp n uchar add 48 div k 100 +n = add n 1 +end +if ge k 10 +put fp n uchar add 48 rem div k 10 10 +n = add n 1 +end +put fp n uchar add 48 rem k 10 +n = add n 1 +for j ulong len tfsuf +put fp add n j get tfsuf j +end +ret add n len tfsuf +end + +note fcreate: write fq[0..n) to the new file fp[0..pn) (O_WRONLY|O_CREAT|O_EXCL|O_NOFOLLOW|O_CLOEXEC, 0600) +fn fcreate pn ulong n ulong to void +fd = int fopen fp 0 pn 557249 384 +if lt fd 0 +fx 12 +end +o = ulong 0 +while lt o n bound 999999 +r = write fd fq o sub n o +if le r 0 +fx 12 +end +o = add o ulong r +end +if ne fclose fd 0 +fx 12 +end +end + +fn fle64 o ulong to ulong +v = ulong 0 +for i ulong 8 +v = or v shl ulong get fst add o i mul 8 i +end +ret v +end + +note fload: read the regular file fp[0..pn) into fr (O_RDONLY|O_CLOEXEC|O_NOFOLLOW); returns its size +fn fload pn ulong to ulong +fd = int fopen fp 0 pn 557056 0 +if lt fd 0 +fx 12 +end +if ne fstat fd fst 0 128 0 +fx 12 +end +sz = fle64 48 +if gt sz FQCAP +fx 12 +end +o = ulong 0 +while lt o sz bound 999999 +q = pread fd fr o sub sz o long o +if le q 0 +fx 12 +end +o = add o ulong q +end +if ne fclose fd 0 +fx 12 +end +ret sz +end + +fn fcrlf i ulong to bool +ret and eq get fq i 13 eq get fq add i 1 10 +end + +note fhend: the offset just past the first CR LF CR LF in fq[0..n), or 0 +fn fhend n ulong to ulong +if lt n 4 +ret 0 +end +for i ulong sub n 3 +if and fcrlf i fcrlf add i 2 +ret add i 4 +end +end +ret 0 +end + +fn flow c uchar to uchar +if and ge c 65 le c 90 +ret uchar add c 32 +end +ret c +end + +fn fiscl p ulong h ulong to bool +if gt add p len tfcl h +ret false +end +for i ulong len tfcl +if ne flow get fq add p i get tfcl i +ret false +end +end +ret true +end + +fn fblank q ulong to bool +ret or eq get fq q 32 eq get fq q 9 +end + +fn fdig q ulong to bool +ret and ge get fq q 48 le get fq q 57 +end + +note fcval: the decimal after optional blanks at p; 13 on a missing or oversized value +fn fcval p ulong h ulong to ulong +q = p +while and lt q h fblank q bound 65536 +q = add q 1 +end +v = ulong 0 +d = ulong 0 +while and lt q h fdig q bound 65536 +v = add mul v 10 ulong sub get fq q 48 +if gt v FQCAP +fx 13 +end +d = add d 1 +q = add q 1 +end +if eq d 0 +fx 13 +end +ret v +end + +note fclen: Content-Length of the header block fq[0..h), 0 when absent +fn fclen h ulong to ulong +p = ulong 0 +while lt p h bound 65536 +if fiscl p h +ret fcval add p len tfcl h +end +while and lt p h ne get fq p 10 bound 65536 +p = add p 1 +end +p = add p 1 +end +ret 0 +end + +note frdreq: read one request from connection a into fq; returns its byte count +fn frdreq a int to ulong +n = ulong 0 +h = ulong 0 +while eq h 0 bound 65536 +if ge n FHCAP +fx 13 +end +r = recv a fq n sub FHCAP n 0 +if le r 0 +fx 13 +end +n = add n ulong r +h = fhend n +end +t = add h fclen h +if gt t FQCAP +fx 13 +end +while lt n t bound 999999 +r = recv a fq n sub t n 0 +if le r 0 +fx 13 +end +n = add n ulong r +end +if ne n t +fx 13 +end +ret t +end + +fn fsendall a int n ulong to void +o = ulong 0 +while lt o n bound 999999 +r = send a fr o sub n o 0 +if le r 0 +fx 11 +end +o = add o ulong r +end +end + +fn fserve s int k ulong to void +a = int accept s 0 +if lt a 0 +fx 11 +end +t = frdreq a +fcreate fpath 3 k t +fsendall a fload fpath 2 k +if ne fclose a 0 +fx 11 +end +end + +note fport: bind 127.0.0.1:0, listen, write the chosen port to argument 1; returns the socket +fn fport to int +s = int socknew 2 1 0 +if lt s 0 +fx 11 +end +put fb 0 2 +put fb 4 127 +put fb 7 1 +if ne bind s fb 0 16 0 +fx 11 +end +if ne listen s 16 0 +fx 11 +end +if ne sockname s fb 0 64 16 +fx 11 +end +v = add mul ulong get fb 2 256 ulong get fb 3 +note the port digits go through fq, which holds no request yet +k = ulong 0 +x = v +while gt x 0 bound 6 +x = div x 10 +k = add k 1 +end +x = v +for i ulong k +put fq sub sub k 1 i uchar add 48 rem x 10 +x = div x 10 +end +put fq k 10 +n = get fal 1 +o = get fas 1 +for i ulong n +put fp i get fa add o i +end +fcreate n add k 1 +ret s +end + +fn main to int +c = argcnt +if ne c 5 +fx 10 +end +o = ulong 0 +for i ulong ulong c +r = argread i fa o sub len fa o +if lt r 0 +fx 10 +end +put fas i o +put fal i ulong r +o = add o ulong r +end +nn = ulong 0 +o = get fas 4 +for i ulong get fal 4 +x = get fa add o i +if or lt x 48 gt x 57 +fx 10 +end +nn = add mul nn 10 ulong sub x 48 +if gt nn 999 +fx 10 +end +end +s = fport +for k ulong nn +fserve s k +end +ret 0 +end diff --git a/om/golden/fixtures/synthetic-diverge.json b/om/golden/fixtures/synthetic-diverge.json new file mode 100644 index 00000000..68b402b6 --- /dev/null +++ b/om/golden/fixtures/synthetic-diverge.json @@ -0,0 +1,4 @@ +{"schema":"aien-golden-record/1","label":"SYNTHETIC","engine_sha256":"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","model_repo":"synthetic/not-a-model","model_rev":"0000000000000000000000000000000000000000","sampling":{"temperature":0,"top_p":1,"seed":0,"enable_thinking":false},"max_tokens":8,"prompts_sha256":"0000000000000000000000000000000000000000000000000000000000000000","host_id":"synthetic-host-b","utc":"2000-01-02T00:00:00Z","prompts":[ +{"id":"p0","ids":[1,2,3,4,5]}, +{"id":"p1","ids":[10,21,30]} +]} diff --git a/om/golden/fixtures/synthetic-header.json b/om/golden/fixtures/synthetic-header.json new file mode 100644 index 00000000..a3bf2f29 --- /dev/null +++ b/om/golden/fixtures/synthetic-header.json @@ -0,0 +1,4 @@ +{"schema":"aien-golden-record/1","label":"SYNTHETIC","engine_sha256":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","model_repo":"synthetic/not-a-model","model_rev":"0000000000000000000000000000000000000000","sampling":{"temperature":0,"top_p":1,"seed":0,"enable_thinking":false},"max_tokens":8,"prompts_sha256":"0000000000000000000000000000000000000000000000000000000000000000","host_id":"synthetic-host-b","utc":"2000-01-02T00:00:00Z","prompts":[ +{"id":"p0","ids":[1,2,3,4,5]}, +{"id":"p1","ids":[10,20,30]} +]} diff --git a/om/golden/fixtures/synthetic-match.json b/om/golden/fixtures/synthetic-match.json new file mode 100644 index 00000000..ddbe1ed6 --- /dev/null +++ b/om/golden/fixtures/synthetic-match.json @@ -0,0 +1,4 @@ +{"schema":"aien-golden-record/1","label":"SYNTHETIC","engine_sha256":"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","model_repo":"synthetic/not-a-model","model_rev":"0000000000000000000000000000000000000000","sampling":{"temperature":0,"top_p":1,"seed":0,"enable_thinking":false},"max_tokens":8,"prompts_sha256":"0000000000000000000000000000000000000000000000000000000000000000","host_id":"synthetic-host-b","utc":"2000-01-02T00:00:00Z","prompts":[ +{"id":"p0","ids":[1,2,3,4,5]}, +{"id":"p1","ids":[10,20,30]} +]} diff --git a/om/golden/fixtures/synthetic-prompts.json b/om/golden/fixtures/synthetic-prompts.json new file mode 100644 index 00000000..ef274a8b --- /dev/null +++ b/om/golden/fixtures/synthetic-prompts.json @@ -0,0 +1,4 @@ +{"schema":"aien-golden-prompts/1","enable_thinking":false,"prompts":[ +{"id":"p0","content":"SYNTHETIC fixture prompt zero"}, +{"id":"p1","content":"SYNTHETIC fixture prompt one: \"quoted\" \u00e9"} +]} diff --git a/om/golden/fixtures/synthetic-ref.json b/om/golden/fixtures/synthetic-ref.json new file mode 100644 index 00000000..47d1687c --- /dev/null +++ b/om/golden/fixtures/synthetic-ref.json @@ -0,0 +1,4 @@ +{"schema":"aien-golden-record/1","label":"SYNTHETIC","engine_sha256":"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","model_repo":"synthetic/not-a-model","model_rev":"0000000000000000000000000000000000000000","sampling":{"temperature":0,"top_p":1,"seed":0,"enable_thinking":false},"max_tokens":8,"prompts_sha256":"0000000000000000000000000000000000000000000000000000000000000000","host_id":"synthetic-host-a","utc":"2000-01-01T00:00:00Z","prompts":[ +{"id":"p0","ids":[1,2,3,4,5]}, +{"id":"p1","ids":[10,20,30]} +]} diff --git a/om/golden/golden.om b/om/golden/golden.om new file mode 100644 index 00000000..adaaedb2 --- /dev/null +++ b/om/golden/golden.om @@ -0,0 +1,1460 @@ +note SPDX-License-Identifier: Apache-2.0 +note om/golden/golden.om: golden-token recorder and comparator (Core #853, DRA-52). Every judgment is +note made here; record.sh only sequences and curl only moves bytes. Build (build.sh does this): +note { printf 'om2\n'; cat om/vendor/json.om om/vendor/sha256.om om/golden/golden.om; } | omc +note Modes: +note golden req PROMPTS MODEL_REPO MAX_TOKENS OUTDIR +note validates the prompt set, writes one greedy request body per prompt to OUTDIR/.json +note (temperature 0, aien_return_token_ids true) and prints the prompt count. +note golden rec PROMPTS MODEL_REPO MODEL_REV MAX_TOKENS ENGINE_SHA256 HOST_ID LABEL RESPDIR +note reads the engine reply RESPDIR/.json for every prompt, takes usage.aien_token_ids, refuses a +note reply whose usage.aien_sampling.mode is not greedy, and prints one aien-golden-record/1. +note golden cmp REF CAND +note prints MATCH or DIVERGE id index=K ref=X cand=Y per prompt (K is the first differing position; +note a list that ends early shows end). +note Exit: 0 written or all match, 1 token mismatch, 2 usage or bad argument, 3 not comparable +note (label, engine_sha256, model_repo, model_rev, sampling, max_tokens, prompts_sha256 or prompt ids +note differ), 4 file, json or schema refusal, 5 engine reply refused. Schema: om/golden/SCHEMA.md. + +const SRCCAP ulong 524288 +const TAPECAP ulong 786432 +const OUTCAP ulong 524288 +const PMAX ulong 1024 +const TKMAX ulong 262144 +const NFLD ulong 12 +const FCAP ulong 128 +const MAXTOK ulong 1000000 + +global ga uchar 16384 +global gas ulong 16 +global gal ulong 16 +global gna ulong 1 +global gpth uchar 4352 +global gout uchar 524288 +global gon ulong 1 +global gst ulong 4 +global gsd ulong 1 +global gseen uchar 32 +global gpid uchar 131072 +global gpidl ulong 2048 +global gpco ulong 1024 +global gpcs ulong 1024 +global gtk uint 524288 +global gtko ulong 2048 +global gtkn ulong 2048 +global gtkt ulong 2 +global gnp ulong 2 +global ghv uchar 3072 +global ghl ulong 24 +global gstb uchar 128 +global gutc uchar 20 + +text tpre "golden: refused " +text tuse "usage: golden req PROMPTS MODEL_REPO MAX_TOKENS OUTDIR | golden rec PROMPTS MODEL_REPO MODEL_REV MAX_TOKENS ENGINE_SHA256 HOST_ID LABEL RESPDIR | golden cmp REF CAND" +text tmreq "req" +text tmrec "rec" +text tmcmp "cmp" +text tio "io" +text tnreg "not-a-regular-file" +text tbig "too-large" +text tjson "json" +text tpsch "prompts-schema" +text trsch "record-schema" +text tclock "clock" +text tbrepo "bad-model-repo" +text tbrev "bad-model-rev" +text tbmax "bad-max-tokens" +text tbeng "bad-engine-sha256" +text tbhost "bad-host-id" +text tblabel "bad-label" +text tengerr "engine-error-reply" +text tnoids "no-usage-aien_token_ids" +text tsamp "aien_sampling-not-object" +text tsampled "not-greedy" +text tbadtok "bad-token-id" +text tjsuf ".json" + +text tq1 "{\"model\":\"" +text tq2 "\",\"messages\":[{\"role\":\"user\",\"content\":" +text tq3 "}],\"temperature\":0,\"top_p\":1,\"seed\":0,\"max_tokens\":" +text tq4 ",\"stream\":false,\"chat_template_kwargs\":{\"enable_thinking\":" +text tq5 "},\"aien_return_token_ids\":true}\n" +text ttrue "true" +text tfalse "false" + +text tr1 "{\"schema\":\"aien-golden-record/1\",\"label\":\"" +text tr2 "\",\"engine_sha256\":\"" +text tr3 "\",\"model_repo\":\"" +text tr4 "\",\"model_rev\":\"" +text tr5 "\",\"sampling\":{\"temperature\":0,\"top_p\":1,\"seed\":0,\"enable_thinking\":" +text tr6 "},\"max_tokens\":" +text tr7 ",\"prompts_sha256\":\"" +text tr8 "\",\"host_id\":\"" +text tr9 "\",\"utc\":\"" +text tr10 "\",\"prompts\":[" +text tp1 "{\"id\":\"" +text tp2 "\",\"ids\":[" +text tp3 "]}" +text tend "\n]}\n" + +text kschema "schema" +text kthink "enable_thinking" +text kprom "prompts" +text kid "id" +text kcont "content" +text klabel "label" +text keng "engine_sha256" +text krepo "model_repo" +text krev "model_rev" +text ksamp "sampling" +text kmaxt "max_tokens" +text kpsha "prompts_sha256" +text khost "host_id" +text kutc "utc" +text kids "ids" +text ktemp "temperature" +text ktopp "top_p" +text kseed "seed" +text kerror "error" +text kusage "usage" +text ktids "aien_token_ids" +text kasamp "aien_sampling" +text kmode "mode" +text vpsch "aien-golden-prompts/1" +text vrsch "aien-golden-record/1" +text vgreedy "greedy" +text vreal "REAL" +text vfake "FAKE" +text vsynth "SYNTHETIC" + +text cinfo1 "golden-cmp ref_host=" +text cinfo2 " ref_utc=" +text cinfo3 " cand_host=" +text cinfo4 " cand_utc=" +text cmatch "MATCH " +text cdivg "DIVERGE " +text cidx " index=" +text cref " ref=" +text ccand " cand=" +text cn " n=" +text cend "end" +text cnot "NOTCOMPARABLE " +text cpcnt "prompt_count" +text cpidx "prompt_id index=" +text csum "golden-cmp prompts=" +text csm " match=" +text csd " diverge=" + +note ======== failure and output ======== + +fn die code int msg view uchar * to void +d = new uchar 2 0 +w = write 2 tpre 0 len tpre +put d 0 uchar add 48 code +put d 1 32 +w = write 2 d 0 2 +w = write 2 msg 0 len msg +put d 0 10 +w = write 2 d 0 1 +exit code +end + +fn obyte c uchar to void +n = get gon 0 +if ge n OUTCAP +die 4 tbig +end +put gout n c +put gon 0 add n 1 +end + +fn ot t view uchar * to void +for i ulong len t +obyte get t i +end +end + +fn osp src view uchar * off ulong n ulong to void +for i ulong n +obyte get src add off i +end +end + +fn ou v ulong to void +d = new uchar 24 0 +x = v +k = ulong 0 +put d k uchar add 48 rem x 10 +x = div x 10 +k = add k 1 +while gt x 0 bound 20 +put d k uchar add 48 rem x 10 +x = div x 10 +k = add k 1 +end +while gt k 0 bound 21 +k = sub k 1 +obyte get d k +end +end + +fn oflush fd int to void +n = get gon 0 +o = ulong 0 +while lt o n bound 999999 +r = write fd gout o sub n o +if le r 0 +die 4 tio +end +o = add o ulong r +end +put gon 0 0 +end + +note ======== arguments and paths ======== + +fn argeq a ulong t view uchar * to bool +n = get gal a +if ne n len t +ret false +end +o = get gas a +for i ulong n +if ne get ga add o i get t i +ret false +end +end +ret true +end + +fn argp a ulong to ulong +n = get gal a +if or eq n 0 gt n 4000 +die 2 tuse +end +o = get gas a +for i ulong n +put gpth i get ga add o i +end +ret n +end + +fn oarg a ulong to void +o = get gas a +for i ulong get gal a +obyte get ga add o i +end +end + +fn namec c uchar sl bool to bool +if and ge c 48 le c 57 +ret true +end +if and ge c 65 le c 90 +ret true +end +if and ge c 97 le c 122 +ret true +end +if or eq c 46 or eq c 95 eq c 45 +ret true +end +ret and sl eq c 47 +end + +fn hexc c uchar to bool +if and ge c 48 le c 57 +ret true +end +ret and ge c 97 le c 102 +end + +fn aname a ulong mx ulong sl bool to bool +n = get gal a +if or eq n 0 gt n mx +ret false +end +o = get gas a +for i ulong n +if not namec get ga add o i sl +ret false +end +end +ret true +end + +fn ahex a ulong n ulong to bool +if ne get gal a n +ret false +end +o = get gas a +for i ulong n +if not hexc get ga add o i +ret false +end +end +ret true +end + +note argu: a decimal argument 1 .. MAXTOK without a leading zero, or 0 when it is anything else +fn argu a ulong to ulong +n = get gal a +if or eq n 0 gt n 7 +ret 0 +end +o = get gas a +if eq get ga o 48 +ret 0 +end +v = ulong 0 +for i ulong n +c = get ga add o i +if or lt c 48 gt c 57 +ret 0 +end +v = add mul v 10 ulong sub c 48 +end +if gt v MAXTOK +ret 0 +end +ret v +end + +fn pdec n ulong v ulong to ulong +d = new uchar 24 0 +x = v +k = ulong 0 +put d k uchar add 48 rem x 10 +x = div x 10 +k = add k 1 +while gt x 0 bound 20 +put d k uchar add 48 rem x 10 +x = div x 10 +k = add k 1 +end +m = n +while gt k 0 bound 21 +k = sub k 1 +put gpth m get d k +m = add m 1 +end +ret m +end + +note pthidx: gpth = argument a, a slash, decimal i, .json +fn pthidx a ulong i ulong to ulong +n = argp a +put gpth n 47 +n = pdec add n 1 i +for j ulong len tjsuf +put gpth add n j get tjsuf j +end +ret add n len tjsuf +end + +note ======== files ======== + +fn le64 o ulong to ulong +v = ulong 0 +for i ulong 8 +v = or v shl ulong get gstb add o i mul 8 i +end +ret v +end + +note rdfile: read the regular file named by gpth[0..n) (O_RDONLY|O_CLOEXEC|O_NOFOLLOW) into src +fn rdfile n ulong src edit uchar * to ulong +fd = fopen gpth 0 n 557056 0 +if lt fd 0 +die 4 tio +end +r = fstat int fd gstb 0 128 +if ne r 0 +die 4 tio +end +md = or ulong get gstb 16 shl ulong get gstb 17 8 +if ne and md 61440 32768 +die 4 tnreg +end +sz = le64 48 +if gt sz len src +die 4 tbig +end +o = ulong 0 +while lt o sz bound 999999 +q = pread int fd src o sub sz o long o +if le q 0 +die 4 tio +end +o = add o ulong q +end +r = fclose int fd +if ne r 0 +die 4 tio +end +ret sz +end + +note wrfile: create gpth[0..n) (O_WRONLY|O_CREAT|O_EXCL|O_NOFOLLOW|O_CLOEXEC, 0600) from gout +fn wrfile n ulong to void +fd = fopen gpth 0 n 557249 384 +if lt fd 0 +die 4 tio +end +oflush int fd +r = fclose int fd +if ne r 0 +die 4 tio +end +end + +fn jparse src view uchar * tape edit uint * n ulong to void +r = js_init tape n 64 +if lt r 0 +die 4 tjson +end +r = js_parse src tape n +if lt r 0 +die 4 tjson +end +if ne js_type tape 0 jsobj +die 4 tjson +end +end + +note ======== token checks ======== + +fn keyis src view uchar * off ulong key view uchar * to bool +ret js_streq src off key +end + +note vname: kind 0 is an id (1..64 of [A-Za-z0-9._-]), kind 1 a model repo (1..96, slash allowed) +fn vname src view uchar * tape view uint * t ulong kind ulong to bool +mx = ulong 64 +sl = false +if eq kind 1 +mx = ulong 96 +sl = true +end +if ne js_type tape t jsstr +ret false +end +off = js_off tape t +sp = js_span tape t +if or lt sp 3 gt sp add mx 2 +ret false +end +for i ulong sub sp 2 +if not namec get src add off add i 1 sl +ret false +end +end +ret true +end + +fn vhex src view uchar * tape view uint * t ulong n ulong to bool +if ne js_type tape t jsstr +ret false +end +if ne js_span tape t add n 2 +ret false +end +off = js_off tape t +for i ulong n +if not hexc get src add off add i 1 +ret false +end +end +ret true +end + +fn utcc i ulong c uchar to bool +if or eq i 4 eq i 7 +ret eq c 45 +end +if eq i 10 +ret eq c 84 +end +if or eq i 13 eq i 16 +ret eq c 58 +end +if eq i 19 +ret eq c 90 +end +ret and ge c 48 le c 57 +end + +fn vutc src view uchar * tape view uint * t ulong to bool +if ne js_type tape t jsstr +ret false +end +if ne js_span tape t 22 +ret false +end +off = add js_off tape t 1 +for i ulong 20 +if not utcc i get src add off i +ret false +end +end +ret true +end + +fn vuint src view uchar * tape view uint * t ulong to bool +if ne js_type tape t jsnum +ret false +end +ret js_isu src js_off tape t +end + +fn tokv src view uchar * tape view uint * e ulong code int to ulong +if not vuint src tape e +die code tbadtok +end +v = js_tou src js_off tape e +if gt v 4294967295 +die code tbadtok +end +ret v +end + +fn seenclr to void +for i ulong 32 +put gseen i 0 +end +end + +fn seen i ulong msg view uchar * to void +if ne get gseen i 0 +die 4 msg +end +put gseen i 1 +end + +fn seenall a ulong n ulong msg view uchar * to void +for i ulong n +if eq get gseen add a i 0 +die 4 msg +end +end +end + +note ======== prompt ids (side 0 = prompt set or reference, side 1 = candidate) ======== + +fn cpcopy src view uchar * off ulong n ulong s ulong to void +b = mul s 64 +for j ulong n +put gpid add b j get src add off j +end +put gpidl s n +end + +fn cpid src view uchar * tape view uint * t ulong i ulong to void +s = add mul get gsd 0 PMAX i +cpcopy src add js_off tape t 1 sub js_span tape t 2 s +end + +fn pideq x ulong y ulong n ulong to bool +p = mul x 64 +q = mul y 64 +for j ulong n +if ne get gpid add p j get gpid add q j +ret false +end +end +ret true +end + +fn ideq sa ulong a ulong sb ulong b ulong to bool +x = add mul sa PMAX a +y = add mul sb PMAX b +if ne get gpidl x get gpidl y +ret false +end +ret pideq x y get gpidl x +end + +fn dupid sd ulong i ulong to bool +for j ulong i +if ideq sd j sd i +ret true +end +end +ret false +end + +fn oid sd ulong i ulong to void +s = add mul sd PMAX i +for j ulong get gpidl s +obyte get gpid add mul s 64 j +end +end + +note ======== prompt set (aien-golden-prompts/1) ======== + +fn pr_key src view uchar * tape view uint * k ulong to ulong +if keyis src js_off tape k kschema +ret 0 +end +if keyis src js_off tape k kthink +ret 1 +end +if keyis src js_off tape k kprom +ret 2 +end +die 4 tpsch +ret 9 +end + +fn pr_think tape view uint * v ulong to void +ty = js_type tape v +if eq ty jstrue +put gst 1 1 +ret +end +if eq ty jsfalse +put gst 1 0 +ret +end +die 4 tpsch +end + +fn pr_mem src view uchar * tape view uint * k ulong i ulong to ulong +v = add k 1 +if keyis src js_off tape k kid +if not vname src tape v 0 +die 4 tpsch +end +cpid src tape v i +if dupid 0 i +die 4 tpsch +end +ret 0 +end +if keyis src js_off tape k kcont +if ne js_type tape v jsstr +die 4 tpsch +end +if eq js_slen tape v 0 +die 4 tpsch +end +put gpco i js_off tape v +put gpcs i js_span tape v +ret 1 +end +die 4 tpsch +ret 9 +end + +fn pr_one src view uchar * tape view uint * o ulong i ulong to void +if ne js_type tape o jsobj +die 4 tpsch +end +if ne js_nkids tape o 2 +die 4 tpsch +end +k = js_first tape o +a = pr_mem src tape k i +b = pr_mem src tape js_mnext tape k i +if eq a b +die 4 tpsch +end +end + +fn pr_list src view uchar * tape view uint * a ulong to void +if ne js_type tape a jsarr +die 4 tpsch +end +np = js_nkids tape a +if or eq np 0 gt np PMAX +die 4 tpsch +end +put gnp 0 np +e = js_first tape a +for i ulong np +pr_one src tape e i +e = js_enext tape e +end +end + +note pr_load: read argument 2, hash its exact bytes into hd, validate it, keep ids and contents +fn pr_load src edit uchar * tape edit uint * to void +put gsd 0 0 +n = rdfile argp 2 src +hreset +for i ulong n +b = hpush get src i +end +ok = hfinal +if not ok +die 4 tio +end +jparse src tape n +seenclr +k = js_first tape 0 +while ne k jsnone bound 999999 +id = pr_key src tape k +seen id tpsch +v = add k 1 +if eq id 0 +if not js_streq src js_off tape v vpsch +die 4 tpsch +end +end +if eq id 1 +pr_think tape v +end +if eq id 2 +pr_list src tape v +end +k = js_mnext tape k +end +seenall 0 3 tpsch +end + +note ======== req ======== + +fn othink to void +if eq get gst 1 1 +ot ttrue +else +ot tfalse +end +end + +fn req_one src view uchar * i ulong mt ulong to void +ot tq1 +oarg 3 +ot tq2 +osp src get gpco i get gpcs i +ot tq3 +ou mt +ot tq4 +othink +ot tq5 +end + +fn m_req src edit uchar * tape edit uint * to int +if ne get gna 0 6 +die 2 tuse +end +if not aname 3 96 true +die 2 tbrepo +end +mt = argu 4 +if eq mt 0 +die 2 tbmax +end +pr_load src tape +np = get gnp 0 +for i ulong np +req_one src i mt +wrfile pthidx 5 i +end +ou np +obyte 10 +oflush 1 +ret 0 +end + +note ======== rec ======== + +fn put2 p ulong v ulong to void +put gutc p uchar add 48 div v 10 +put gutc add p 1 uchar add 48 rem v 10 +end + +fn civyoe doe ulong to ulong +a = sub doe div doe 1460 +a = add a div doe 36524 +a = sub a div doe 146096 +ret div a 365 +end + +fn civput y ulong m ulong d ulong to void +put gutc 0 uchar add 48 div y 1000 +put gutc 1 uchar add 48 rem div y 100 10 +put gutc 2 uchar add 48 rem div y 10 10 +put gutc 3 uchar add 48 rem y 10 +put gutc 4 45 +put2 5 m +put gutc 7 45 +put2 8 d +end + +note civil: proleptic Gregorian date of a day count since 1970-01-01 (days-to-civil, era of 146097 days) +fn civil days ulong to void +z = add days 719468 +era = div z 146097 +doe = sub z mul era 146097 +yoe = civyoe doe +doy = sub doe sub add mul 365 yoe div yoe 4 div yoe 100 +mp = div add mul 5 doy 2 153 +m = add mp 3 +if ge mp 10 +m = sub mp 9 +end +y = add yoe mul era 400 +if le m 2 +y = add y 1 +end +civput y m add sub doy div add mul 153 mp 2 5 1 +end + +fn utcfmt to void +t = realns +if lt t 0 +die 4 tclock +end +s = div ulong t 1000000000 +civil div s 86400 +sod = rem s 86400 +put gutc 10 84 +put2 11 div sod 3600 +put gutc 13 58 +put2 14 div rem sod 3600 60 +put gutc 16 58 +put2 17 rem sod 60 +put gutc 19 90 +end + +fn rs_greedy src view uchar * tape view uint * s ulong to void +if ne js_type tape s jsobj +die 5 tsamp +end +k = js_first tape s +while ne k jsnone bound 999999 +if keyis src js_off tape k kmode +if js_streq src js_off tape add k 1 vgreedy +ret +end +end +k = js_mnext tape k +end +die 5 tsampled +end + +fn rs_usage src view uchar * tape view uint * u ulong to ulong +a = jsnone +k = js_first tape u +while ne k jsnone bound 999999 +v = add k 1 +if keyis src js_off tape k ktids +if or ne a jsnone ne js_type tape v jsarr +die 5 tnoids +end +a = v +end +if keyis src js_off tape k kasamp +rs_greedy src tape v +end +k = js_mnext tape k +end +if eq a jsnone +die 5 tnoids +end +ret a +end + +fn rs_one src view uchar * tape view uint * i ulong to void +u = jsnone +k = js_first tape 0 +while ne k jsnone bound 999999 +if keyis src js_off tape k kerror +die 5 tengerr +end +if keyis src js_off tape k kusage +if ne u jsnone +die 5 tnoids +end +u = add k 1 +end +k = js_mnext tape k +end +if eq u jsnone +die 5 tnoids +end +if ne js_type tape u jsobj +die 5 tnoids +end +a = rs_usage src tape u +ot tp1 +oid 0 i +ot tp2 +e = js_first tape a +c = ulong 0 +while ne e jsnone bound 999999 +if gt c 0 +obyte 44 +end +ou tokv src tape e 5 +c = add c 1 +e = js_enext tape e +end +ot tp3 +end + +fn rec_head mt ulong to void +ot tr1 +oarg 8 +ot tr2 +oarg 6 +ot tr3 +oarg 3 +ot tr4 +oarg 4 +ot tr5 +othink +ot tr6 +ou mt +ot tr7 +for i ulong 64 +obyte get hd i +end +ot tr8 +oarg 7 +ot tr9 +for j ulong 20 +obyte get gutc j +end +ot tr10 +end + +fn m_rec src edit uchar * tape edit uint * to int +if ne get gna 0 10 +die 2 tuse +end +if not aname 3 96 true +die 2 tbrepo +end +if not ahex 4 40 +die 2 tbrev +end +mt = argu 5 +if eq mt 0 +die 2 tbmax +end +if not ahex 6 64 +die 2 tbeng +end +if not aname 7 64 false +die 2 tbhost +end +if not or argeq 8 vreal argeq 8 vfake +die 2 tblabel +end +pr_load src tape +utcfmt +rec_head mt +np = get gnp 0 +for i ulong np +if gt i 0 +obyte 44 +end +obyte 10 +n = rdfile pthidx 9 i src +jparse src tape n +rs_one src tape i +end +ot tend +oflush 1 +ret 0 +end + +note ======== cmp: record loading (aien-golden-record/1) ======== +note header fields kept per side: 0 label, 1 engine_sha256, 2 model_repo, 3 model_rev, 4 temperature, +note 5 top_p, 6 seed, 7 enable_thinking, 8 max_tokens, 9 prompts_sha256 (all compared), 10 host_id, 11 utc +note (printed, never compared). Values are kept as their exact JSON lexeme. + +fn rc_need ok bool to void +if not ok +die 4 trsch +end +end + +fn fcopy src view uchar * off ulong n ulong s ulong to void +b = mul s FCAP +for j ulong n +put ghv add b j get src add off j +end +put ghl s n +end + +fn fstore src view uchar * tape view uint * v ulong f ulong to void +if gt js_span tape v FCAP +die 4 trsch +end +fcopy src js_off tape v js_span tape v add mul get gsd 0 NFLD f +end + +fn rc_key src view uchar * tape view uint * k ulong to ulong +if keyis src js_off tape k kschema +ret 0 +end +if keyis src js_off tape k klabel +ret 1 +end +if keyis src js_off tape k keng +ret 2 +end +if keyis src js_off tape k krepo +ret 3 +end +if keyis src js_off tape k krev +ret 4 +end +if keyis src js_off tape k ksamp +ret 5 +end +if keyis src js_off tape k kmaxt +ret 6 +end +if keyis src js_off tape k kpsha +ret 7 +end +if keyis src js_off tape k khost +ret 8 +end +if keyis src js_off tape k kutc +ret 9 +end +if keyis src js_off tape k kprom +ret 10 +end +die 4 trsch +ret 99 +end + +fn rc_skey src view uchar * tape view uint * k ulong to ulong +if keyis src js_off tape k ktemp +ret 4 +end +if keyis src js_off tape k ktopp +ret 5 +end +if keyis src js_off tape k kseed +ret 6 +end +if keyis src js_off tape k kthink +ret 7 +end +die 4 trsch +ret 99 +end + +note rc_sval: greedy records only: temperature is the integer 0 +fn rc_sval src view uchar * tape view uint * v ulong f ulong to void +ty = js_type tape v +if eq f 4 +rc_need vuint src tape v +rc_need eq js_tou src js_off tape v 0 +ret +end +if eq f 5 +rc_need eq ty jsnum +ret +end +if eq f 6 +rc_need vuint src tape v +ret +end +rc_need or eq ty jstrue eq ty jsfalse +end + +fn rc_samp src view uchar * tape view uint * o ulong to void +rc_need eq js_type tape o jsobj +rc_need eq js_nkids tape o 4 +k = js_first tape o +while ne k jsnone bound 8 +f = rc_skey src tape k +seen add 16 f trsch +v = add k 1 +rc_sval src tape v f +fstore src tape v f +k = js_mnext tape k +end +seenall 20 4 trsch +end + +fn rc_label src view uchar * tape view uint * v ulong to void +off = js_off tape v +if js_streq src off vreal +ret +end +if js_streq src off vfake +ret +end +if js_streq src off vsynth +ret +end +die 4 trsch +end + +fn rc_fill src view uchar * tape view uint * a ulong d ulong to void +e = js_first tape a +j = ulong 0 +while ne e jsnone bound 999999 +put gtk add d j uint tokv src tape e 4 +j = add j 1 +e = js_enext tape e +end +end + +fn rc_ids src view uchar * tape view uint * a ulong i ulong to void +rc_need eq js_type tape a jsarr +sd = get gsd 0 +s = add mul sd PMAX i +t = get gtkt sd +n = js_nkids tape a +if gt add t n TKMAX +die 4 tbig +end +put gtko s t +put gtkn s n +put gtkt sd add t n +rc_fill src tape a add mul sd TKMAX t +end + +fn rc_pmem src view uchar * tape view uint * k ulong i ulong to ulong +v = add k 1 +if keyis src js_off tape k kid +rc_need vname src tape v 0 +cpid src tape v i +if dupid get gsd 0 i +die 4 trsch +end +ret 0 +end +if keyis src js_off tape k kids +rc_ids src tape v i +ret 1 +end +die 4 trsch +ret 9 +end + +fn rc_prompt src view uchar * tape view uint * o ulong i ulong to void +rc_need eq js_type tape o jsobj +rc_need eq js_nkids tape o 2 +k = js_first tape o +a = rc_pmem src tape k i +b = rc_pmem src tape js_mnext tape k i +if eq a b +die 4 trsch +end +end + +fn rc_prompts src view uchar * tape view uint * a ulong to void +rc_need eq js_type tape a jsarr +np = js_nkids tape a +if or eq np 0 gt np PMAX +die 4 trsch +end +put gnp get gsd 0 np +e = js_first tape a +for i ulong np +rc_prompt src tape e i +e = js_enext tape e +end +end + +fn rc_val src view uchar * tape view uint * v ulong id ulong to void +if eq id 0 +rc_need js_streq src js_off tape v vrsch +ret +end +if eq id 1 +rc_label src tape v +fstore src tape v 0 +ret +end +if eq id 2 +rc_need vhex src tape v 64 +fstore src tape v 1 +ret +end +if eq id 3 +rc_need vname src tape v 1 +fstore src tape v 2 +ret +end +if eq id 4 +rc_need vhex src tape v 40 +fstore src tape v 3 +ret +end +if eq id 5 +rc_samp src tape v +ret +end +rc_val2 src tape v id +end + +fn rc_val2 src view uchar * tape view uint * v ulong id ulong to void +if eq id 6 +rc_need vuint src tape v +mt = js_tou src js_off tape v +rc_need and ge mt 1 le mt MAXTOK +fstore src tape v 8 +ret +end +if eq id 7 +rc_need vhex src tape v 64 +fstore src tape v 9 +ret +end +if eq id 8 +rc_need vname src tape v 0 +fstore src tape v 10 +ret +end +if eq id 9 +rc_need vutc src tape v +fstore src tape v 11 +ret +end +rc_prompts src tape v +end + +fn rc_load src edit uchar * tape edit uint * sd ulong to void +put gsd 0 sd +n = rdfile argp add 2 sd src +jparse src tape n +seenclr +put gtkt sd 0 +k = js_first tape 0 +while ne k jsnone bound 999999 +id = rc_key src tape k +seen id trsch +rc_val src tape add k 1 id +k = js_mnext tape k +end +seenall 0 11 trsch +end + +note ======== cmp: comparison ======== + +fn feq f ulong to bool +a = f +b = add NFLD f +n = get ghl a +if ne n get ghl b +ret false +end +for j ulong n +if ne get ghv add mul a FCAP j get ghv add mul b FCAP j +ret false +end +end +ret true +end + +fn ofld sd ulong f ulong to void +s = add mul sd NFLD f +for j ulong get ghl s +obyte get ghv add mul s FCAP j +end +end + +fn ofname f ulong to void +if eq f 0 +ot klabel +end +if eq f 1 +ot keng +end +if eq f 2 +ot krepo +end +if eq f 3 +ot krev +end +if eq f 4 +ot ktemp +end +if eq f 5 +ot ktopp +end +if eq f 6 +ot kseed +end +if eq f 7 +ot kthink +end +if eq f 8 +ot kmaxt +end +if eq f 9 +ot kpsha +end +end + +fn c_info to void +ot cinfo1 +ofld 0 10 +ot cinfo2 +ofld 0 11 +ot cinfo3 +ofld 1 10 +ot cinfo4 +ofld 1 11 +obyte 10 +end + +fn c_ids to ulong +bad = ulong 0 +for i ulong get gnp 0 +if not ideq 0 i 1 i +ot cnot +ot cpidx +ou i +ot cref +oid 0 i +ot ccand +oid 1 i +obyte 10 +bad = add bad 1 +end +end +ret bad +end + +fn c_head to ulong +bad = ulong 0 +for f ulong 10 +if not feq f +ot cnot +ofname f +ot cref +ofld 0 f +ot ccand +ofld 1 f +obyte 10 +bad = add bad 1 +end +end +if ne get gnp 0 get gnp 1 +ot cnot +ot cpcnt +ot cref +ou get gnp 0 +ot ccand +ou get gnp 1 +obyte 10 +ret add bad 1 +end +ret add bad c_ids +end + +fn cdiv ao ulong an ulong bo ulong bn ulong to ulong +m = an +if lt bn m +m = bn +end +k = ulong 0 +while lt k m bound 999999 +if ne get gtk add ao k get gtk add bo k +ret k +end +k = add k 1 +end +ret m +end + +fn otokat o ulong n ulong k ulong to void +if lt k n +ou ulong get gtk add o k +else +ot cend +end +end + +fn c_one i ulong to bool +ao = get gtko i +an = get gtkn i +bo = add TKMAX get gtko add PMAX i +bn = get gtkn add PMAX i +k = cdiv ao an bo bn +if and eq k an eq k bn +ot cmatch +oid 0 i +ot cn +ou an +obyte 10 +ret true +end +ot cdivg +oid 0 i +ot cidx +ou k +ot cref +otokat ao an k +ot ccand +otokat bo bn k +obyte 10 +ret false +end + +fn m_cmp src edit uchar * tape edit uint * to int +if ne get gna 0 4 +die 2 tuse +end +rc_load src tape 0 +rc_load src tape 1 +c_info +bad = c_head +if gt bad 0 +oflush 1 +ret 3 +end +mm = ulong 0 +np = get gnp 0 +for i ulong np +if not c_one i +mm = add mm 1 +end +end +ot csum +ou np +ot csm +ou sub np mm +ot csd +ou mm +obyte 10 +oflush 1 +if gt mm 0 +ret 1 +end +ret 0 +end + +fn main to int +c = argcnt +if or lt c 2 gt c 10 +die 2 tuse +end +o = ulong 0 +for i ulong ulong c +r = argread i ga o sub len ga o +if lt r 0 +die 2 tuse +end +put gas i o +put gal i ulong r +o = add o ulong r +end +put gna 0 ulong c +src = new uchar SRCCAP 0 +tape = new uint TAPECAP 0 +if argeq 1 tmreq +ret m_req src tape +end +if argeq 1 tmrec +ret m_rec src tape +end +if argeq 1 tmcmp +ret m_cmp src tape +end +die 2 tuse +ret 2 +end diff --git a/om/golden/record.sh b/om/golden/record.sh new file mode 100755 index 00000000..5ec68435 --- /dev/null +++ b/om/golden/record.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: Apache-2.0 +# om/golden/record.sh: record greedy golden token ids from an aien-serve-http endpoint. +# Sequencing only: golden (om/golden/golden.om) validates prompts, writes every request body, judges +# every reply and writes the record; curl only moves bytes. +# Usage: OMC=... bash om/golden/record.sh --url http://HOST:PORT --prompts P.json --model-repo ORG/NAME +# --model-rev SHA40 --max-tokens N --engine-sha256 SHA64 --host-id ID --label REAL|FAKE --out FILE +# [--header-file F] [--timeout SECONDS] +# GOLDEN=path skips the build and uses that golden binary. +# Exit: 0 written, 2 usage or existing --out, 3 compiler not pinned, 4/5 refused by golden, 6 transport. +set -u +here=$(cd "$(dirname "$0")" && pwd) +url= prompts= repo= rev= maxt= eng= host= label= out= hdr= tmo=600 +while [ $# -gt 1 ]; do + case $1 in + --url) url=$2 ;; --prompts) prompts=$2 ;; --model-repo) repo=$2 ;; --model-rev) rev=$2 ;; + --max-tokens) maxt=$2 ;; --engine-sha256) eng=$2 ;; --host-id) host=$2 ;; --label) label=$2 ;; + --out) out=$2 ;; --header-file) hdr=$2 ;; --timeout) tmo=$2 ;; + *) echo "record: refused 2 unknown option $1" >&2; exit 2 ;; + esac + shift 2 +done +[ $# -eq 0 ] || { echo "record: refused 2 option without value" >&2; exit 2; } +for v in url prompts repo rev maxt eng host label out; do + [ -n "${!v}" ] || { echo "record: refused 2 missing $v" >&2; exit 2; } +done +if [ -e "$out" ] || [ -L "$out" ]; then echo "record: refused 2 $out exists" >&2; exit 2; fi +work=$(mktemp -d "$(dirname "$out")/.golden-record.XXXXXX") || exit 2 +trap 'rm -rf "$work"' EXIT +if [ -z "${GOLDEN:-}" ]; then + bash "$here/build.sh" "$work/golden" || exit $? + GOLDEN=$work/golden +fi +mkdir "$work/req" "$work/resp" +n=$("$GOLDEN" req "$prompts" "$repo" "$maxt" "$work/req") || exit $? +hargs=() +[ -z "$hdr" ] || hargs=(-H "@$hdr") +for ((i = 0; i < n; i++)); do + curl --silent --show-error --fail --max-time "$tmo" --noproxy '*' --proto '=http,https' \ + -H 'Content-Type: application/json' -H 'Expect:' "${hargs[@]}" \ + --data-binary "@$work/req/$i.json" -o "$work/resp/$i.json" "$url/v1/chat/completions" \ + || { echo "record: refused 6 transport prompt $i" >&2; exit 6; } +done +"$GOLDEN" rec "$prompts" "$repo" "$rev" "$maxt" "$eng" "$host" "$label" "$work/resp" > "$work/record.json" || exit $? +ln "$work/record.json" "$out" || { echo "record: refused 2 $out exists" >&2; exit 2; }