gem/scripts/analyze-fused-split-kv-correctness.sh
2026-08-30 18:24:55 -06:00

144 lines
7 KiB
Bash
Executable file

#!/bin/sh
set -eu
if [ "$#" -ne 10 ] && [ "$#" -ne 11 ]; then
echo "usage: $0 CPU_GATE RESOURCE_ANALYSIS OP8191 OP8192 OP8193 OP65536 OP131072 MODEL8191 MODEL8192 MODEL8193 [OUTPUT_JSON]" >&2
exit 2
fi
cpu=$1
resources=$2
op8191=$3
op8192=$4
op8193=$5
op65536=$6
op131072=$7
model8191=$8
model8192=$9
model8193=${10}
output=${11:-}
for input in "$cpu" "$resources" "$op8191" "$op8192" "$op8193" "$op65536" \
"$op131072" "$model8191" "$model8192" "$model8193"; do
if [ ! -r "$input" ]; then
echo "gem: fused split-KV correctness input is not readable: $input" >&2
exit 2
fi
done
if [ -n "$output" ] && [ -e "$output" ]; then
echo "gem: refusing to overwrite fused split-KV correctness analysis: $output" >&2
exit 2
fi
digest()
{
shasum -a 256 "$1" | awk '{print $1}'
}
cpu_sha=$(digest "$cpu")
resources_sha=$(digest "$resources")
op8191_sha=$(digest "$op8191")
op8192_sha=$(digest "$op8192")
op8193_sha=$(digest "$op8193")
op65536_sha=$(digest "$op65536")
op131072_sha=$(digest "$op131072")
model8191_sha=$(digest "$model8191")
model8192_sha=$(digest "$model8192")
model8193_sha=$(digest "$model8193")
analyze()
{
jq -n \
--arg cpu_path "$cpu" --arg cpu_sha "$cpu_sha" \
--arg resources_path "$resources" --arg resources_sha "$resources_sha" \
--arg op8191_path "$op8191" --arg op8191_sha "$op8191_sha" \
--arg op8192_path "$op8192" --arg op8192_sha "$op8192_sha" \
--arg op8193_path "$op8193" --arg op8193_sha "$op8193_sha" \
--arg op65536_path "$op65536" --arg op65536_sha "$op65536_sha" \
--arg op131072_path "$op131072" --arg op131072_sha "$op131072_sha" \
--arg model8191_path "$model8191" --arg model8191_sha "$model8191_sha" \
--arg model8192_path "$model8192" --arg model8192_sha "$model8192_sha" \
--arg model8193_path "$model8193" --arg model8193_sha "$model8193_sha" \
--slurpfile cpu "$cpu" --slurpfile resources "$resources" \
--slurpfile op8191 "$op8191" --slurpfile op8192 "$op8192" \
--slurpfile op8193 "$op8193" --slurpfile op65536 "$op65536" \
--slurpfile op131072 "$op131072" --slurpfile model8191 "$model8191" \
--slurpfile model8192 "$model8192" --slurpfile model8193 "$model8193" '
def digest_record($path; $sha): {path:$path, sha256:$sha};
def valid_operator($o; $tokens):
$o.schema_version == 1 and $o.engine == "gem-native-c-metal" and
$o.workload == "d512-fused-split-kv-operator-differential" and
$o.tokens == $tokens and $o.query_heads == 32 and $o.kv_heads == 4 and
$o.head_dimension == 512 and $o.value_dimension == 512 and
$o.partitions == 32 and $o.tile_tokens == 4 and $o.kv_type == "f32" and
$o.window == 0 and $o.baseline_selector == 19 and
$o.candidate_route == "d512-fused-split-kv-p32-tile4" and
$o.repetitions >= 2 and $o.finite == true and $o.deterministic == true and
$o.correlation >= 0.9999 and $o.nmse <= 0.0001 and
($o.maximum_absolute_error | type) == "number" and
($o.candidate_hash | test("^[0-9a-f]{16}$")) and
$o.candidate_hash == $o.candidate_repeat_hash and $o.passed == true;
def valid_model($m; $position):
$m.schema_version == 1 and $m.engine == "gem-native-c-metal" and
$m.workload == "d512-fused-split-kv-model-differential" and
$m.model_size == 31 and $m.layers == 60 and
($m.model_sha256 | test("^[0-9a-f]{64}$")) and
$m.prefix_mode == "evaluated-prompt" and $m.position == $position and
$m.context > $position and $m.baseline_selector == 19 and
$m.candidate_route == "d512-fused-split-kv-p32-tile4" and
$m.production_unchanged == true and $m.finite == true and
$m.deterministic == true and $m.correlation >= 0.9999 and
$m.nmse <= 0.0001 and $m.baseline_argmax == $m.candidate_argmax and
($m.baseline_top10 | type) == "array" and ($m.baseline_top10 | length) == 10 and
$m.baseline_top10 == $m.candidate_top10 and
($m.baseline_continuation_tokens | type) == "array" and
($m.baseline_continuation_tokens | length) >= 8 and
$m.baseline_continuation_tokens == $m.candidate_continuation_tokens and
($m.candidate_logits_hash | test("^[0-9a-f]{16}$")) and
$m.candidate_logits_hash == $m.candidate_repeat_logits_hash and $m.passed == true;
$cpu[0] as $c | $resources[0] as $r |
$op8191[0] as $o1 | $op8192[0] as $o2 | $op8193[0] as $o3 |
$op65536[0] as $o4 | $op131072[0] as $o5 |
$model8191[0] as $m1 | $model8192[0] as $m2 | $model8193[0] as $m3 |
if ($c.workload == "d512-online-softmax-cpu-gate-audit" and
$c.screening.passed == true and $c.screening.promotion_eligible == false and
$r.workload == "d512-fused-split-kv-resource-analysis" and
$r.screening.passed == true and $r.screening.promotion_eligible == false and
valid_operator($o1;8191) and valid_operator($o2;8192) and
valid_operator($o3;8193) and valid_operator($o4;65536) and
valid_operator($o5;131072) and valid_model($m1;8191) and
valid_model($m2;8192) and valid_model($m3;8193) and
$m1.model_sha256 == $m2.model_sha256 and $m1.model_sha256 == $m3.model_sha256)
then {
schema_version:1,
engine:"gem-native-c-metal",
workload:"d512-fused-split-kv-correctness-analysis",
model_sha256:$m1.model_sha256,
fixed_controls:{baseline_selector:19,candidate_route:"d512-fused-split-kv-p32-tile4",
operator_tokens:[8191,8192,8193,65536,131072],
model_positions:[8191,8192,8193],minimum_continuation_tokens:8},
inputs:{
cpu_gate:digest_record($cpu_path;$cpu_sha),
resource_analysis:digest_record($resources_path;$resources_sha),
operators:{at_8191:digest_record($op8191_path;$op8191_sha),
at_8192:digest_record($op8192_path;$op8192_sha),
at_8193:digest_record($op8193_path;$op8193_sha),
at_65536:digest_record($op65536_path;$op65536_sha),
at_131072:digest_record($op131072_path;$op131072_sha)},
models:{at_8191:digest_record($model8191_path;$model8191_sha),
at_8192:digest_record($model8192_path;$model8192_sha),
at_8193:digest_record($model8193_path;$model8193_sha)}
},
worst_observed:{
operator_nmse:([$o1.nmse,$o2.nmse,$o3.nmse,$o4.nmse,$o5.nmse]|max),
operator_correlation:([$o1.correlation,$o2.correlation,$o3.correlation,
$o4.correlation,$o5.correlation]|min),
model_nmse:([$m1.nmse,$m2.nmse,$m3.nmse]|max),
model_correlation:([$m1.correlation,$m2.correlation,$m3.correlation]|min)
},
screening:{passed:true,promotion_eligible:false,
interpretation:"D512 operator, logits, and greedy continuation correctness gates pass; clean-host timing and adjacent backend-op gates remain"}
}
else error("fused split-KV inputs fail the fixed correctness contract") end'
}
if [ -n "$output" ]; then analyze > "$output"; else analyze; fi