gem/scripts/analyze-decode-residual.sh
2026-08-30 18:24:55 -06:00

86 lines
3.9 KiB
Bash
Executable file

#!/bin/sh
set -eu
if [ "$#" -ne 2 ] && [ "$#" -ne 3 ]; then
echo "usage: $0 FULL_MODEL_PROFILE_JSON OPERATOR_PROFILE_JSON [OUTPUT_JSON]" >&2
exit 2
fi
full_model=$1
operator=$2
output=${3:-}
analyze()
{
jq -n --slurpfile full "$full_model" --slurpfile operator "$operator" '
$full[0] as $f | $operator[0] as $o |
if ($f.schema_version != 1 or $o.schema_version != 1 or
$f.engine != "gem-native-c-metal" or $o.engine != $f.engine or
$f.workload != "authentic-one-token-decode-stage-profile" or
$o.workload != "synthetic-exact-shape-decode-attention-stage-profile" or
$o.shape != "31b-full" or $f.position != $o.tokens or
$o.live_tokens != $o.tokens or $f.rows != 1 or $f.layers != 60 or
$f.repetitions < 3 or $o.repetitions < 3 or
($f.gpu_seconds_samples | length) < 3 or
($o.stages.value.samples_seconds | length) < 3 or
$f.mean_gpu_seconds <= 0 or $f.mean_sampled_gpu_seconds <= 0 or
$f.sample_coverage < 0.95 or
$f.layers_by_stage.kv_attention.mean_seconds <= 0 or
$o.mean_gpu_seconds <= 0 or $o.stages.value.mean_seconds <= 0)
then error("decode residual inputs are not comparable") else
10 as $full_attention_layers |
($o.mean_gpu_seconds * $full_attention_layers) as $global_attention_seconds |
($o.stages.value.mean_seconds * $full_attention_layers) as $global_value_seconds |
(100 * $global_value_seconds / $f.mean_gpu_seconds) as $value_ceiling_pct |
(100 * $global_attention_seconds /
$f.layers_by_stage.kv_attention.mean_seconds) as $kv_attribution_pct |
(3 / $value_ceiling_pct * 100) as $required_value_gain_pct |
($f.gpu_seconds_samples |
map((. - $f.mean_gpu_seconds) | if . < 0 then -. else . end) |
max * 100 / $f.mean_gpu_seconds) as $max_model_deviation_pct |
{
schema_version: 1,
engine: $f.engine,
workload: "decode-value-residual-screen",
model: $f.model,
position: $f.position,
model_attention_kernel: ($f.attention_kernel // "production-default"),
operator_value_kernel: ($o.value_kernel // "unspecified"),
full_attention_layers: $full_attention_layers,
evidence: {
model_mean_gpu_seconds: $f.mean_gpu_seconds,
model_sample_coverage: $f.sample_coverage,
model_max_sample_deviation_pct: $max_model_deviation_pct,
kv_attention_mean_seconds: $f.layers_by_stage.kv_attention.mean_seconds,
per_layer_operator_mean_seconds: $o.mean_gpu_seconds,
per_layer_value_mean_seconds: $o.stages.value.mean_seconds,
attributed_global_attention_seconds: $global_attention_seconds,
attributed_global_value_seconds: $global_value_seconds,
attributed_global_attention_fraction_of_kv_pct: $kv_attribution_pct
},
residual: {
theoretical_whole_token_value_ceiling_pct: $value_ceiling_pct,
value_gain_required_for_3pct_whole_token_pct: $required_value_gain_pct,
projected_whole_token_gain_at_25pct_value_gain_pct: ($value_ceiling_pct * 0.25),
projected_whole_token_gain_at_50pct_value_gain_pct: ($value_ceiling_pct * 0.50)
},
screening: {
admissible: ($value_ceiling_pct >= 3 and $kv_attribution_pct <= 100),
promotion_eligible: false,
reason: (if $value_ceiling_pct < 3 then
"theoretical P*V ceiling is below the whole-token promotion floor"
elif $kv_attribution_pct > 100 then
"synthetic attention attribution exceeds authentic KV-attention time"
else
"residual ceiling admits one-axis operator screening; authentic correctness and model/server gates remain required"
end)
}
}
end'
}
if [ -n "$output" ]; then
analyze > "$output"
else
analyze
fi