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

147 lines
6.7 KiB
Bash
Executable file

#!/bin/sh
set -eu
if [ "$#" -ne 4 ] && [ "$#" -ne 5 ]; then
echo "usage: $0 FULL_MODEL_64K OPERATOR_64K FULL_MODEL_128K OPERATOR_128K [OUTPUT_JSON]" >&2
exit 2
fi
full64=$1
operator64=$2
full128=$3
operator128=$4
output=${5:-}
for input in "$full64" "$operator64" "$full128" "$operator128"; do
if [ ! -r "$input" ]; then
echo "gem: fused split-KV ceiling input is not readable: $input" >&2
exit 2
fi
done
if [ -n "$output" ] && [ -e "$output" ]; then
echo "gem: refusing to overwrite fused split-KV ceiling output: $output" >&2
exit 2
fi
if ! command -v shasum >/dev/null 2>&1; then
echo "gem: shasum is required for analysis provenance" >&2
exit 2
fi
full64_sha=$(shasum -a 256 "$full64" | awk '{print $1}')
operator64_sha=$(shasum -a 256 "$operator64" | awk '{print $1}')
full128_sha=$(shasum -a 256 "$full128" | awk '{print $1}')
operator128_sha=$(shasum -a 256 "$operator128" | awk '{print $1}')
analyze()
{
jq -n \
--arg full64_path "$full64" --arg full64_sha "$full64_sha" \
--arg operator64_path "$operator64" --arg operator64_sha "$operator64_sha" \
--arg full128_path "$full128" --arg full128_sha "$full128_sha" \
--arg operator128_path "$operator128" --arg operator128_sha "$operator128_sha" \
--slurpfile full64 "$full64" --slurpfile operator64 "$operator64" \
--slurpfile full128 "$full128" --slurpfile operator128 "$operator128" '
def finite_positive: type == "number" and . > 0 and . < 1e100;
def valid_full($f; $position):
$f.schema_version == 1 and $f.engine == "gem-native-c-metal" and
$f.workload == "authentic-one-token-decode-stage-profile" and
$f.attention_kernel == "split16" and $f.attention_selector_override == 19 and
$f.prefix_mode == "evaluated-prompt" and $f.position == $position and
$f.context > $position and $f.rows == 1 and $f.layers == 60 and
$f.repetitions >= 3 and ($f.gpu_seconds_samples | length) >= 3 and
($f.mean_gpu_seconds | finite_positive) and
($f.mean_sampled_gpu_seconds | finite_positive) and
$f.sample_coverage >= 0.95 and $f.sample_coverage <= 1.01 and
($f.layers_by_stage.kv_attention.mean_seconds | finite_positive);
def valid_operator($o; $tokens):
$o.schema_version == 1 and $o.engine == "gem-native-c-metal" and
$o.workload == "synthetic-exact-shape-decode-attention-stage-profile" and
$o.shape == "31b-full" and $o.value_kernel == "dim32-partitioned" and
$o.value_dimension_tile == 32 and $o.value_partitions == 16 and
$o.query_heads == 32 and $o.kv_heads == 4 and $o.head_dimension == 512 and
$o.tokens == $tokens and $o.live_tokens == $tokens and
$o.cache_capacity == $tokens and $o.window == 0 and
$o.repetitions >= 3 and ($o.gpu_seconds_samples | length) >= 3 and
($o.mean_gpu_seconds | finite_positive) and
($o.mean_sampled_gpu_seconds | finite_positive) and
$o.sample_coverage >= 0.95 and $o.sample_coverage <= 1.01 and
($o.stages.score.mean_seconds | finite_positive) and
($o.stages.normalize.mean_seconds | finite_positive) and
($o.stages.value.mean_seconds | finite_positive) and
(((($o.stages.score.mean_seconds + $o.stages.normalize.mean_seconds +
$o.stages.value.mean_seconds) / $o.mean_gpu_seconds) - 1) | fabs) < 0.01;
def context_result($f; $o):
10 as $global_layers |
($o.mean_gpu_seconds * $global_layers) as $global_seconds |
($o.stages.score.mean_seconds * $global_layers) as $score_seconds |
(100 * $global_seconds / $f.mean_gpu_seconds) as $global_ceiling |
(100 * $score_seconds / $f.mean_gpu_seconds) as $score_ceiling |
(100 * $global_seconds / $f.layers_by_stage.kv_attention.mean_seconds) as $kv_attribution |
{
tokens: $o.tokens,
model_gpu_seconds: $f.mean_gpu_seconds,
authentic_kv_attention_seconds: $f.layers_by_stage.kv_attention.mean_seconds,
attributed_global_attention_seconds: $global_seconds,
attributed_score_seconds: $score_seconds,
global_attention_attribution_of_kv_pct: $kv_attribution,
theoretical_whole_token_global_attention_ceiling_pct: $global_ceiling,
theoretical_whole_token_score_only_ceiling_pct: $score_ceiling,
global_attention_gain_required_for_3pct_whole_token_pct: (300 / $global_ceiling),
score_gain_required_for_3pct_whole_token_pct: (300 / $score_ceiling),
observed_model_sample_coverage: $f.sample_coverage,
observed_operator_sample_coverage: $o.sample_coverage,
admissible: ($global_ceiling > 3 and $kv_attribution <= 100)
};
$full64[0] as $f64 | $operator64[0] as $o64 |
$full128[0] as $f128 | $operator128[0] as $o128 |
if (valid_full($f64; 65536) and valid_operator($o64; 65536) and
valid_full($f128; 131072) and valid_operator($o128; 131072) and
$f64.model == $f128.model)
then
(context_result($f64; $o64)) as $c64 |
(context_result($f128; $o128)) as $c128 |
{
schema_version: 1,
engine: "gem-native-c-metal",
workload: "fused-split-kv-theoretical-ceiling-screen",
model: $f64.model,
fixed_controls: {
production_selector: 19,
attention_kernel: "split16",
global_layers: 10,
query_heads: 32,
kv_heads: 4,
head_dimension: 512,
value_partitions: 16,
contexts: [65536, 131072],
whole_token_admission_floor_pct: 3
},
inputs: {
at_65536: {
full_model: {path: $full64_path, sha256: $full64_sha},
exact_shape_operator: {path: $operator64_path, sha256: $operator64_sha}
},
at_131072: {
full_model: {path: $full128_path, sha256: $full128_sha},
exact_shape_operator: {path: $operator128_path, sha256: $operator128_sha}
}
},
contexts: {at_65536: $c64, at_131072: $c128},
screening: {
admissible: ($c64.admissible and $c128.admissible),
promotion_eligible: false,
interpretation: (if ($c64.admissible and $c128.admissible) then
"measured residual admits one bounded fused split-KV diagnostic; correctness, Metal counters, and authentic whole-token gates remain"
else
"profile attribution does not justify a fused split-KV diagnostic"
end)
}
}
else error("fused split-KV ceiling inputs are not comparable production profiles") end'
}
if [ -n "$output" ]; then
analyze > "$output"
else
analyze
fi