interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / scripts / pi5-8 / host_golden.sh

72 lines · 4695 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# PI5-8 host golden check: the engine's forward pass vs llama.cpp, greedy tokens, one model, three prompts.
18# host_golden.sh MODEL.gguf TEMPLATE EOS_ID [N=16] [sdot]
19# TEMPLATE: llama3 | zephyr | danube | chatml sdot: use the dotprod int8 path (default: f32 scalar)
20# llama.cpp is the build in $LLAMA (default ~/llama.cpp-build, commit 4a89937, the one PI5-6 used).
21# Prompt ids come from llama.cpp's own tokenizer and are fed to the engine, so the engine's tokenizer
22# is not judged here (see docs/native/PI5-8-MODEL-CHOICE.md, "Limits").
23# Files for the Pi image land in $OUT (default /tmp/pi58-golden/<model name>): <p>.ref holds the ids line
24# and the llama.cpp steps, <p>.probe the engine's output, <p>.exp / <p>.got the compared "index id" lists.
25set -euo pipefail
26model="${1:?model}"; tpl="${2:?template}"; eos="${3:?eos id}"; n="${4:-16}"; dot="${5:-}"
27here="$(cd "$(dirname "$0")" && pwd)"; repo="$(cd "$here/../.." && pwd)"
28L="${LLAMA:-$HOME/llama.cpp-build}"
29name="$(basename "$model" .gguf)"; out="${OUT:-/tmp/pi58-golden/$name${dot:+-$dot}}"; mkdir -p "$out"
30ref="${out}/llama_ref"
31[[ -x "$ref" ]] || gcc -O1 -o "$ref" "$repo/crates/aienos-infer/tests/ref/llama_ref.c" -I"$L/include" -I"$L/ggml/include" \
32 -L"$L/build/bin" -lllama -lggml -lggml-base -Wl,-rpath,"$L/build/bin"
33probe="$repo/target/release/examples/model_probe"
34[[ -x "$probe" ]] || cargo build --release -p aienos-infer --example model_probe --manifest-path "$repo/Cargo.toml"
35wrap() {
36 case "$tpl" in
37 llama3) printf '<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n\n%s<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n' "$1" ;;
38 zephyr) printf '<s><|user|>\n%s</s>\n<|assistant|>\n' "$1" ;;
39 danube) printf '<s><|prompt|>%s</s><|answer|>' "$1" ;;
40 chatml) printf '<|im_start|>user\n%s<|im_end|>\n<|im_start|>assistant\n' "$1" ;;
41 *) echo "unknown template $tpl" >&2; exit 2 ;;
42 esac
43}
44# Exact token match = PASS. If the lists differ, look at the FIRST divergence: when llama.cpp's own top-1 minus top-2
45# logit gap there is below 1.0 (the largest single-logit difference seen between the engine and llama.cpp on the Llama
46# control, 0.9), the two implementations may legitimately disagree (floats are summed in a different order, a near-tie
47# flips): NOISE. A larger gap is FAIL. The 1.0 threshold is this script's own choice, not a published standard.
48fail=0
49for p in short long1 long2; do
50 wrap "$(cat "$here/prompt-$p.txt")" > "$out/$p.prompt"
51 # read back with a sentinel so the template's trailing newlines survive command substitution
52 text="$(cat "$out/$p.prompt"; printf x)"; text="${text%x}"
53 # CPU only, as llama_ref.c intends (n_gpu_layers=0); hiding the GPU also stops a busy GPU from aborting the run
54 CUDA_VISIBLE_DEVICES="" "$ref" "$model" "$text" "$n" "$out/$p.logits" > "$out/$p.ref" 2>"$out/$p.ref.err"
55 AIENOS_DOT="$dot" AIENOS_LOGITS="$out/$p.logits" "$probe" "$model" "$out/$p.ref" "$n" "$eos" > "$out/$p.probe"
56 awk '$1=="step"{print $2,$3} $1=="eog"{print "eog"}' "$out/$p.ref" > "$out/$p.exp"
57 awk '$1=="step"{print $2,$3} $1=="eog"{print "eog"}' "$out/$p.probe" > "$out/$p.got"
58 ids_n=$(( $(head -1 "$out/$p.ref" | wc -w) - 1 ))
59 if cmp -s "$out/$p.exp" "$out/$p.got"; then
60 r=PASS
61 else
62 div=$(paste -d' ' "$out/$p.exp" "$out/$p.got" | awk 'NF==4 && $2!=$4 {print $1; exit}')
63 gap=$(awk -v s="$div" '$1=="step" && $2==s {print $4}' "$out/$p.ref")
64 if awk -v g="$gap" 'BEGIN{exit !(g<1.0)}'; then r="NOISE(first divergence step $div, llama.cpp gap $gap)"
65 else r="FAIL(first divergence step $div, llama.cpp gap $gap)"; fail=1; fi
66 fi
67 echo "golden $name prompt=$p ids=$ids_n tokens=$(grep -c '^step' "$out/$p.ref") dot=${dot:-f32} $r"
68 echo " $(grep logits_vs_llama "$out/$p.probe" || true)"
69 echo " llama.cpp: $(awk '$1=="step"{printf "%s ", $3}' "$out/$p.ref")"
70 [[ $r == PASS ]] || echo " engine: $(awk '$1=="step"{printf "%s ", $3}' "$out/$p.probe")"
71done
72exit $fail