Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-sovereign-core-mv/blob/main/scripts/bench/headstart-summarize.sh # aien-sovereign-core-mv AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-sovereign-core-mv / scripts / bench / headstart-summarize.sh` 390 lines ยท 21679 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 # Headstart M0 (#532) summarizer. Pure bash + jq + sed, no Python. 18 # 19 # headstart-summarize.sh samples -> summary JSON on stdout 20 # headstart-summarize.sh timings -> UNIT_DATA as a JSON array 21 # headstart-summarize.sh critical [jobs] -> critical-path JSON on stdout 22 # headstart-summarize.sh graph -> package graph (normal + build edges) 23 # headstart-summarize.sh replay -> list-schedule estimate JSON 24 # 25 # `samples` is deterministic: running it again on the same samples.jsonl 26 # reproduces summary.json byte for byte (see benchmarks/headstart/README.md). 27 set -euo pipefail 28 29 usage() { 30 sed -n '2,11p' "$0" >&2 31 exit 64 32 } 33 34 # Shared jq helpers: median, MAD (median absolute deviation), sample stdev. 35 JQ_STATS=' 36 def r3: (. * 1000 | round) / 1000; 37 def median: sort | length as $n 38 | if $n == 0 then null 39 elif $n % 2 == 1 then .[($n - 1) / 2] 40 else (.[$n / 2 - 1] + .[$n / 2]) / 2 end; 41 def mad: median as $m | if $m == null then null else map(. - $m | fabs) | median end; 42 def stdev: length as $n 43 | if $n < 2 then null 44 else (add / $n) as $mu | (map((. - $mu) * (. - $mu)) | add / ($n - 1)) | sqrt end; 45 def stats: if length == 0 then {n: 0, median: null, min: null, max: null, mad: null, stdev: null} 46 else {n: length, median: (median | r3), min: (min | r3), max: (max | r3), 47 mad: (mad | r3), stdev: (stdev | if . == null then null else r3 end)} end; 48 def nn_median: map(select(. != null)) | if length == 0 then null else median | r3 end; 49 ' 50 51 cmd_samples() { 52 local f=$1 53 [ -s "$f" ] || { echo "summarize: $f is missing or empty" >&2; exit 65; } 54 # Every line must parse; a corrupt line is an error, never silently skipped. 55 jq -e -s 'length > 0' "$f" >/dev/null 56 jq -S -s "$JQ_STATS"' 57 . as $all 58 | (map(select(.final == true and .ok == true))) as $okf 59 | { 60 schema: "headstart-summary/v1", 61 source: "samples.jsonl", 62 n_lines: ($all | length), 63 rules: { 64 counted: "final attempt, exit status 0 (failed builds are never counted)", 65 clean: "counted and contaminated == false", 66 dispersion: "mad = median absolute deviation; stdev = sample standard deviation (n-1)", 67 units: "seconds for wall_s, kilobytes for max_rss_kb, percent for cpu_percent" 68 }, 69 groups: ( 70 group_by([.scenario, .arm]) 71 | map( 72 . as $g 73 | (map(select(.final == true and .ok == true))) as $ok 74 | (map(select(.final == true and .ok == true and .contaminated == false))) as $clean 75 | { 76 scenario: $g[0].scenario, 77 arm: $g[0].arm, 78 kind: $g[0].kind, 79 command: $g[0].command, 80 n_lines: ($g | length), 81 n_final: (map(select(.final == true)) | length), 82 n_failed: (map(select(.final == true and .ok == false)) | length), 83 n_superseded: (map(select(.final == false)) | length), 84 n_contaminated_counted: ($ok | map(select(.contaminated == true)) | length), 85 failures: (map(select(.ok == false)) | map({sample_id, exit_status})), 86 wall_s: ($ok | map(.wall_s) | stats), 87 wall_s_clean: ($clean | map(.wall_s) | stats), 88 max_rss_kb_median: ($ok | map(.time_v.max_rss_kb) | nn_median), 89 cpu_percent_median: ($ok | map(.time_v.cpu_percent) | nn_median) 90 } 91 ) 92 ), 93 aa_noise: ( 94 [ $okf[] | select(.aa_pair != null) ] | group_by(.aa_pair) 95 | map( 96 (map(select(.arm == "A") | .wall_s) | median) as $a 97 | (map(select(.arm == "B") | .wall_s) | median) as $b 98 | { 99 scenario: .[0].aa_pair, 100 n_a: (map(select(.arm == "A")) | length), 101 n_b: (map(select(.arm == "B")) | length), 102 median_a_s: ($a | if . == null then null else r3 end), 103 median_b_s: ($b | if . == null then null else r3 end), 104 delta_s: (if $a == null or $b == null then null else ($b - $a) | r3 end), 105 delta_pct: (if $a == null or $b == null or $a == 0 then null 106 else (($b - $a) / $a * 100) | r3 end) 107 } 108 ) 109 ) 110 } 111 # M5 (#536): OFF vs ON per scenario, present only when such arms exist, 112 # so summaries of runs without them stay byte-identical. 113 | ([ $all[] | select(.final == true and (.arm == "OFF" or .arm == "ON")) ]) as $abf 114 # Times, RSS and CPU come from clean samples only (final, exit 0, not 115 # contaminated); an arm without one is NOT_MEASURED, never a number. 116 | if ($abf | length) == 0 then . else . + {ab_rules: { 117 counted: "final attempt, exit status 0 and contaminated == false; contaminated finals are excluded and counted, failures are listed and never timed", 118 status: "measured when both arms have a clean sample; else FAILED when a final sample failed, else NOT_MEASURED (no clean final sample: contaminated host)"}, 119 aa_noise_clean: ( 120 [ $okf[] | select(.aa_pair != null and .contaminated == false) ] | group_by(.aa_pair) 121 | map( 122 (map(select(.arm == "A") | .wall_s) | median) as $a 123 | (map(select(.arm == "B") | .wall_s) | median) as $b 124 | { 125 scenario: .[0].aa_pair, 126 n_a: (map(select(.arm == "A")) | length), 127 n_b: (map(select(.arm == "B")) | length), 128 median_a_s: ($a | if . == null then null else r3 end), 129 median_b_s: ($b | if . == null then null else r3 end), 130 delta_pct: (if $a == null or $b == null or $a == 0 then null 131 else (($b - $a) / $a * 100) | r3 end) 132 } 133 ) 134 ), 135 ab: ( 136 $abf | group_by(.scenario) | map( 137 . as $g 138 | (map(select(.arm == "OFF" and .ok == true and .contaminated == false))) as $off 139 | (map(select(.arm == "ON" and .ok == true and .contaminated == false))) as $on 140 | ($off | map(.wall_s) | median) as $mo 141 | ($on | map(.wall_s) | median) as $mn 142 | ($off | map(.time_v.max_rss_kb) | nn_median) as $ro 143 | ($on | map(.time_v.max_rss_kb) | nn_median) as $rn 144 | ($off | map(if .time_v.user_s == null then null else .time_v.user_s + .time_v.sys_s end) | nn_median) as $co 145 | ($on | map(if .time_v.user_s == null then null else .time_v.user_s + .time_v.sys_s end) | nn_median) as $cn 146 | { 147 scenario: $g[0].scenario, 148 n_off: ($off | length), n_on: ($on | length), 149 failed_off: (map(select(.arm == "OFF" and .ok == false)) | length), 150 failed_on: (map(select(.arm == "ON" and .ok == false)) | length), 151 failures: (map(select(.ok == false)) | map({sample_id, arm, exit_status})), 152 contaminated_excluded_off: (map(select(.arm == "OFF" and .ok == true and .contaminated == true)) | length), 153 contaminated_excluded_on: (map(select(.arm == "ON" and .ok == true and .contaminated == true)) | length), 154 status: (if ($off | length) > 0 and ($on | length) > 0 then "measured" 155 elif any(.[]; .ok == false) then "FAILED" 156 else "NOT_MEASURED" end), 157 ice_reports_on: (map(select(.arm == "ON" and has("ice_reports")) | .ice_reports) 158 | if length == 0 then null else add end), 159 wall_s_off: ($off | map(.wall_s) | stats), 160 wall_s_on: ($on | map(.wall_s) | stats), 161 delta_s: (if $mo == null or $mn == null then null else ($mn - $mo) | r3 end), 162 saving_pct: (if $mo == null or $mn == null or $mo == 0 then null 163 else (($mo - $mn) / $mo * 100) | r3 end), 164 max_rss_kb_median_off: $ro, max_rss_kb_median_on: $rn, 165 max_rss_delta_pct: (if $ro == null or $rn == null or $ro == 0 then null 166 else (($rn - $ro) / $ro * 100) | r3 end), 167 cpu_s_median_off: $co, cpu_s_median_on: $cn, 168 cpu_s_delta_pct: (if $co == null or $cn == null or $co == 0 then null 169 else (($cn - $co) / $co * 100) | r3 end) 170 } 171 ) 172 )} end' "$f" 173 } 174 175 cmd_timings() { 176 local html=$1 177 [ -s "$html" ] || { echo "summarize: $html is missing or empty" >&2; exit 65; } 178 # Cargo embeds the per-unit data as `const UNIT_DATA = [ ... ];` (JSON) in 179 # the timing report. Cut that block out and validate it as JSON. 180 local block 181 block=$(sed -n '/^const UNIT_DATA = \[/,/^\];/p' "$html" | sed -e '1s/^const UNIT_DATA = //' -e '$s/;$//') 182 [ -n "$block" ] || { echo "summarize: no UNIT_DATA block in $html" >&2; exit 65; } 183 printf '%s\n' "$block" | jq -e 'type == "array" and length > 0' >/dev/null 184 printf '%s\n' "$block" | jq -c . 185 } 186 187 cmd_critical() { 188 local units=$1 jobs=${2:-null} 189 jq -S --argjson jobs "$jobs" "$JQ_STATS"' 190 # frontend end (seconds after unit start) = when its .rmeta is ready. 191 def fe_end: if (.sections // null) == null then null 192 else ([.sections[] | select(.[0] == "frontend") | .[1].end] | first) end; 193 . as $u 194 | ($u | map({key: (.i | tostring), value: .}) | from_entries) as $by 195 # Each unit records which units became runnable when it finished 196 # (unblocked_units) or when its metadata was ready (unblocked_rmeta_units). 197 # That edge is the dependency that actually gated the unblocked unit. 198 | ([ $u[] | .i as $d 199 | ((.unblocked_units[]? | {key: tostring, value: {pred: $d, gate: "full"}}), 200 (.unblocked_rmeta_units[]? | {key: tostring, value: {pred: $d, gate: "rmeta"}})) ] 201 | from_entries) as $pred 202 | ($u | max_by(.start + .duration)) as $last 203 | [ $last.i | recurse(($pred[tostring] // empty).pred) ] | reverse 204 | map($by[tostring]) as $chain 205 | ($chain | to_entries | map( 206 .value as $c 207 | ($pred[$c.i | tostring]) as $p 208 | ($p | if . == null then null else $by[.pred | tostring] end) as $pu 209 | { 210 i: $c.i, name: $c.name, version: $c.version, target: $c.target, mode: $c.mode, 211 start_s: ($c.start | r3), duration_s: ($c.duration | r3), 212 frontend_s: ($c | fe_end | if . == null then null else r3 end), 213 gated_by: (if $p == null then null else {i: $p.pred, name: $pu.name, gate: $p.gate} end), 214 gate_time_s: (if $p == null then null 215 elif $p.gate == "full" then ($pu.start + $pu.duration) | r3 216 else (($pu | fe_end) as $fe | if $fe == null then null else ($pu.start + $fe) | r3 end) end), 217 pred_frontend_s: (if $p == null or $p.gate != "rmeta" then null 218 else ($pu | fe_end | if . == null then null else r3 end) end) 219 })) as $links 220 | ($u | map(.duration) | add) as $cpu 221 | ($u | max_by(.duration)) as $longest 222 | ($last.start + $last.duration) as $wall 223 | ([ $links[] | select(.gated_by.gate == "rmeta") | .pred_frontend_s // 0 ] | add // 0) as $rmeta_wait 224 | ([ $links[] | select(.gated_by.gate == "full") ] | length) as $n_full 225 | (if $jobs == null then null else $cpu / $jobs end) as $cpu_floor 226 | ([$longest.duration, ($cpu_floor // 0)] | max) as $floor 227 | { 228 schema: "headstart-critical-path/v1", 229 units: ($u | length), 230 observed: { 231 build_end_s: ($wall | r3), 232 sum_unit_duration_s: ($cpu | r3), 233 longest_unit: {name: $longest.name, target: $longest.target, mode: $longest.mode, duration_s: ($longest.duration | r3)}, 234 chain_length: ($links | length), 235 chain_full_gated_links: $n_full, 236 chain_rmeta_gated_links: ([ $links[] | select(.gated_by.gate == "rmeta") ] | length), 237 chain: $links 238 }, 239 estimate: { 240 label: "ESTIMATE, not a measurement", 241 method: ("Walk back from the last unit to finish along the edge that unblocked each unit " 242 + "(cargo unblocked_units / unblocked_rmeta_units). On rmeta-gated links the dependent " 243 + "waited for the predecessor frontend (sections.frontend.end). A metadata-pipelining " 244 + "compiler change can at most remove those waits on this chain; full-gated links " 245 + "(build scripts, proc macros, linked binaries) need finished artifacts and are not " 246 + "shortened. Upper bound = min(sum of predecessor frontend time on rmeta-gated chain links, " 247 + "build_end - floor) where floor = max(longest unit, sum of unit durations / jobs). " 248 + "This single-chain bound ignores that another chain may become critical; the realistic " 249 + "estimate that accounts for the whole graph and the job limit is the list-schedule " 250 + "replay (headstart-summarize.sh replay, timings/replay.json). Covers cold cargo build only."), 251 scope: "cold cargo build --workspace only", 252 jobs: $jobs, 253 floor_s: ($floor | r3), 254 rmeta_wait_on_chain_s: ($rmeta_wait | r3), 255 upper_bound_saving_s: ([$rmeta_wait, ($wall - $floor)] | min | if . < 0 then 0 else . end | r3), 256 upper_bound_saving_pct: (([$rmeta_wait, ($wall - $floor)] | min | if . < 0 then 0 else . end) / $wall * 100 | r3), 257 realistic_estimate: "see timings/replay.json" 258 } 259 }' "$units" 260 } 261 262 # Package dependency graph from `cargo metadata --format-version 1 263 # --filter-platform `: normal and build edges only (dev edges dropped, 264 # they are not part of `cargo build --workspace`). 265 cmd_graph() { 266 local meta=$1 267 [ -s "$meta" ] || { echo "summarize: $meta is missing or empty" >&2; exit 65; } 268 jq -S ' 269 (.packages | map({key: .id, value: { 270 name, version, links, 271 build_script: any(.targets[]; .kind | index("custom-build")), 272 proc_macro: any(.targets[]; .kind | index("proc-macro"))}}) | from_entries) as $p 273 | [ .resolve.nodes[] | . as $n | $p[$n.id] + { 274 deps: ([ $n.deps[] 275 | {pkg: $p[.pkg], kinds: ([.dep_kinds[].kind | . // "normal"] | unique | map(select(. != "dev")))} 276 | select(.kinds | length > 0) 277 | {name: .pkg.name, version: .pkg.version, kinds} ] 278 | sort_by(.name, .version)) } ] 279 | sort_by(.name, .version)' "$meta" 280 } 281 282 # List-schedule replay of one cold `cargo build` (#532 estimate). 283 # Units and durations come from cargo-timing UNIT_DATA; edges come from the 284 # package graph with cargo unit rules: build-script compile needs its build 285 # dependencies (full); build-script run needs its compile and the runs of 286 # normal deps that declare `links`; a lib needs its own build-script run 287 # (full) and its normal deps (rmeta, or full for proc macros); a bin needs its 288 # own lib and its normal deps (full). Units with the same name, version and 289 # kind (feature variants) are treated as one group: a dependent waits for all 290 # of them (conservative). The replay runs twice at the given job count, with 291 # observed start order as priority: "baseline" (rmeta ready at frontend end) 292 # and "no_rmeta_wait" (rmeta ready when the dependency starts). The saving is 293 # baseline minus no_rmeta_wait; comparing baseline with the observed build 294 # end shows how well the model fits. 295 cmd_replay() { 296 local units=$1 graph=$2 jobs=$3 297 [ -s "$units" ] && [ -s "$graph" ] || { echo "summarize: replay needs units.json and graph.json" >&2; exit 65; } 298 jq -S -n --slurpfile u "$units" --slurpfile g "$graph" --argjson jobs "$jobs" "$JQ_STATS"' 299 def kind: if .mode == "run-custom-build" then "bsrun" 300 elif (.target | test("build-script")) then "bscomp" 301 elif (.target | test("\"bin\"")) then "bin" else "lib" end; 302 def fe_end: if (.sections // null) == null then null 303 else ([.sections[] | select(.[0] == "frontend") | .[1].end] | first) end; 304 $u[0] as $U | $g[0] as $G 305 | ($G | map({key: (.name + "@" + .version), value: .}) | from_entries) as $pkg 306 | (reduce ($U | to_entries[]) as $e ({}; 307 .[$e.value.name + "@" + $e.value.version + "#" + ($e.value | kind)] += [$e.key])) as $lk 308 | def look($nv; $k): $lk[$nv + "#" + $k] // []; 309 [ $U | to_entries[] | .key as $i | .value as $x | ($x.name + "@" + $x.version) as $nv 310 | ($pkg[$nv]) as $P 311 | ($x | kind) as $k 312 | (if $P == null then [] 313 elif $k == "bscomp" then 314 [ $P.deps[] | select(.kinds | index("build")) | look(.name + "@" + .version; "lib")[] | {d: ., gate: "full"} ] 315 elif $k == "bsrun" then 316 [ look($nv; "bscomp")[] | {d: ., gate: "full"} ] 317 + [ $P.deps[] | select(.kinds | index("normal")) 318 | select(($pkg[.name + "@" + .version].links // null) != null) 319 | look(.name + "@" + .version; "bsrun")[] | {d: ., gate: "full"} ] 320 elif $k == "lib" then 321 [ look($nv; "bsrun")[] | {d: ., gate: "full"} ] 322 + [ $P.deps[] | select(.kinds | index("normal")) | (.name + "@" + .version) as $dn 323 | look($dn; "lib")[] | {d: ., gate: (if $pkg[$dn].proc_macro then "full" else "rmeta" end)} ] 324 else 325 [ look($nv; "lib")[] | {d: ., gate: "full"} ] 326 + [ $P.deps[] | select(.kinds | index("normal")) | look(.name + "@" + .version; "lib")[] | {d: ., gate: "full"} ] 327 end) as $deps 328 | {i: $i, dur: $x.duration, fe: ($x | fe_end // $x.duration), prio: $x.start, matched: ($P != null), 329 deps: ($deps | map(select(.d != $i)) | unique)} ] as $N 330 | ($N | length) as $n 331 | def sim($mode): 332 def avail($st; $e): $st[$e.d] as $s 333 | if $s == null then null 334 elif $e.gate == "full" then $s + $N[$e.d].dur 335 elif $mode == "nowait" then $s 336 else $s + $N[$e.d].fe end; 337 def rt($st; $i): [ $N[$i].deps[] | avail($st; .) ] as $a 338 | if any($a[]; . == null) then null else ($a | max // 0) end; 339 {t: 0, st: [range($n) | null], stuck: false} 340 | until((.stuck) or all(.st[]; . != null); 341 . as $s 342 | ([ range($n) | select($s.st[.] != null and $s.st[.] + $N[.].dur > $s.t) ] | length) as $busy 343 | ([ range($n) | select($s.st[.] == null) | {i: ., r: rt($s.st; .)} | select(.r != null) ]) as $rd 344 | ([ $rd[] | select(.r <= $s.t) ] | sort_by($N[.i].prio, .i) | .[0:([$jobs - $busy, 0] | max)]) as $go 345 | if ($go | length) > 0 then 346 reduce $go[] as $x ($s; .st[$x.i] = $s.t) 347 else 348 ([ (range($n) | select($s.st[.] != null) | $s.st[.] + $N[.].dur | select(. > $s.t)), 349 ($rd[] | .r | select(. > $s.t)) ] | min) as $nt 350 | if $nt == null then .stuck = true else .t = $nt end 351 end) 352 | . as $s 353 | {end_s: ([ range($n) | select($s.st[.] != null) | $s.st[.] + $N[.].dur ] | max // 0 | r3), 354 deadlock: .stuck}; 355 (sim("baseline")) as $b | (sim("nowait")) as $w 356 | ($U | map(.start + .duration) | max) as $obs 357 | { 358 schema: "headstart-replay/v1", 359 label: "ESTIMATE, not a measurement", 360 scope: "cold cargo build --workspace only", 361 jobs: $jobs, 362 units: $n, 363 units_without_package: ($N | map(select(.matched | not)) | length), 364 edges: ($N | map(.deps | length) | add), 365 rmeta_edges: ($N | map(.deps[] | select(.gate == "rmeta")) | length), 366 observed_build_end_s: ($obs | r3), 367 baseline_model_end_s: $b.end_s, 368 model_error_pct: (if $obs == 0 then null else (($b.end_s - $obs) / $obs * 100 | r3) end), 369 no_rmeta_wait_model_end_s: $w.end_s, 370 realistic_saving_s: (($b.end_s - $w.end_s) | r3), 371 realistic_saving_pct: (if $b.end_s == 0 then null else (($b.end_s - $w.end_s) / $b.end_s * 100 | r3) end), 372 deadlock: ($b.deadlock or $w.deadlock), 373 method: ("List-schedule replay of UNIT_DATA durations over the cargo metadata package graph " 374 + "(see headstart-summarize.sh cmd_replay for the unit rules) at the given job count, priority " 375 + "= observed start order. baseline: an rmeta edge is ready at the dependency frontend end; " 376 + "no_rmeta_wait: an rmeta edge is ready when the dependency starts, the most a metadata-pipelining " 377 + "change could give. realistic_saving = baseline - no_rmeta_wait, both from the same model, so " 378 + "model error largely cancels. Feature-variant units are grouped conservatively.") 379 }' 380 } 381 382 [ $# -ge 2 ] || usage 383 case "$1" in 384 samples) cmd_samples "$2" ;; 385 timings) cmd_timings "$2" ;; 386 critical) cmd_critical "$2" "${3:-null}" ;; 387 graph) cmd_graph "$2" ;; 388 replay) [ $# -ge 4 ] || usage; cmd_replay "$2" "$3" "$4" ;; 389 *) usage ;; 390 esac