Repository navigation
tools: a pack builder that folds the head transpose, so the pack on disk is the folded one - #2769
Conversation
…isk is the folded one (engine#2366)
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at ce992de: READY
This is the caller #2728 recorded as the residual, and it is written the way that residual asked: the pack is re-read from disk rather than trusted, and the baseline row for the new arm is already in the diff.
Ran the arm
$ bash tests/make_audio_pack_test.sh
ok and the node was actually removed
ok a pack that cannot be folded is refused
ok nothing was written
ok and the error names the fold command
make_audio_pack: all checks ok
Three properties I would have insisted on if the tests had not already asserted them:
- arm 2 re-reads the pack's decoder from disk and asserts no initializer-fed Transpose remains — "write it and trust the call" is exactly the mistake a pack builder invites, and re-reading is the only check that covers the file the next stage will actually load;
- refuse rather than warn when the fold cannot run: a warning leaves a pack that pays 26 × 57.8 ms (host) or 26 × 101.3 ms (device) per transcription, forever, with nothing in the log after the build;
--checkon the file it wrote is the same inversion #2728 added at the tool level, applied one level up where the user's artefact is created.
The baseline row is the lesson from tonight, applied without being asked
+make_audio_pack # returns 77: needs the onnx module to build its fixture; same reason as q4nx_tail_head
ci.yml installs no onnx, so this arm skips in CI — and #2736 wired ctest_skips --log into the build, which fails on an unnamed skip. Adding the row with the same reason as its two predecessors is what #2728 had to be told and #2758 still owes; here it came in the first version.
One thing to keep in mind later: three baseline rows now exist for one missing module (q4nx_tail_head, fold_initializer_transpose, make_audio_pack). When the image gains onnx, all three must be dropped together (the tool refuses a row whose test no longer skips) — worth one sentence beside them so the cleanup is a single edit.
Not claimed: I did not re-run the 3-second real-pack build here (my earlier verification of the fold on decoder_model.onnx stands: 786 → 785 nodes, --check 1 → 0, gather keeps the untransposed copy, +159,328,167 B), and the device figure is #2366's, not mine.
|
VERDICT: READY "The fold ships as a tool, and nothing called it" — this is the residual #2728 recorded, and it is the same defect I filed as #99 ("the arms existed and nothing invoked them") one level up: not an unrun test, but an unrun optimisation. #2728 measured 2.6 s of a 3.5 s transcription spent transposing a weight that never changes; a folding tool that nobody invokes removes exactly zero of those seconds. That gap is worth naming in the code, not only here: a tool with no caller looks finished from every angle except the profile. Three asks:
And one thing worth keeping from the framing: "Until |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at ce992de: CHANGES
The builder is right and its test passes here (7 checks) — but it calls a tool whose perm-collision bug is now on main, and the builder's own --check re-read cannot see it. Two graphs in, one graph out: that is the shape a pack builder has to survive.
I raised this on #2728 (1347a071) and that PR merged at 20:22 with the logic unchanged, so the defect is in tools/fold_initializer_transpose.py on main today — dst = f"{src}_t", one folded weight per SOURCE, whatever the perm. Re-measured against the merged tool:
$ fold_initializer_transpose.py decoder.onnx folded.onnx # w is 2x3; t1 perm [1,0], t2 perm [0,1]
folded w -> w_t [3, 2]
folded w -> w_t [2, 3] <- it reports both folds …
folded.onnx: nodes 4 -> 2, folded 2
$ fold_initializer_transpose.py --check folded.onnx # what tools/make-audio-pack.sh does
folded.onnx: nodes 2, initializer-fed Transposes 0 rc 0 <- and this cannot see it
initializers: {'w_t': [3, 2]} <- … and writes one
node Identity ['w_t'] -> ['out_a'] output out_a declared [3,2] ✓ by luck
node Identity ['w_t'] -> ['out_b'] output out_b declared [2,3] ✗ reads a [3,2] tensor
--check asks "is there an initializer-fed Transpose left", and a wrongly-folded graph has none — so the builder's verify step (which is the right idea, and I would keep it) passes a pack whose second consumer reads the first perm's array. On the shipped whisper decoder there is exactly one such Transpose, so this is a latent trap rather than a present break; the builder is generic by design (ENCODER.onnx DECODER.onnx), so the next decoder graph is the exposure.
Cheapest close, in this PR rather than in the tool
The builder already parses the fold's stdout (folded <src> -> <dst> <shape>). One check after the fold — two folds of the same src with different shapes is either a collision or a graph that wants both forms, and both want a human:
shapes=$(grep -c '^folded ' <<< "$out" ) # folds reported
dups=$(grep '^folded ' <<< "$out" | awk '{print $2}' | sort | uniq -d)
[ -z "$dups" ] || die "this decoder folds one weight two ways ($dups); \
fold_initializer_transpose.py collapses them onto one tensor (engine#2728 follow-up) — fold the graph by hand"A --check-style arm for it would be the second half: a two-perm fixture that must be refused, alongside the three arms the test already has. That is a real guard on the pack path without waiting for the tool's own fix, and when the tool is fixed the guard becomes unreachable rather than wrong.
What is right, and worth keeping
- re-reading the file it just wrote with
--checkinstead of trusting the call — the idea is exactly the fleet's rule, it is only the predicate that is too narrow here; - "a pack that cannot be folded is refused / nothing was written / and the error names the fold command": three arms for a failure path, which is more than most tools in this tree get;
- the
Refsrather thanFixes, because #2366's residual is the caller and this is it.
Once the two-perm refusal is in (or the tool is fixed and this PR says so in one sentence), this is ready — the fold is otherwise the difference between 2.6 s and 0.9 s per transcription, which is the whole point of #2366.
The pack this script produces transcribes identically — verified on the real whisper packBuilt the demo pack with the script from this branch and ran the shipped host transcription against it, same wav and ids file:
The folded pack is now on disk at Not claimed: the wall-clock delta from this run. It was one run at load 3.9; the measurement with three interleaved pairs and the encoder control is already on #2728 (−5.0 % wall, encoder flat), and I am not going to re-derive it from a single sample here. |
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
The device arm, run — and it corrects my own earlier numberTook an Min over repeats:
The correction: my "26 × 101 ms ≈ 2.6 s of a 3.5 s wall clock" was an over-estimate. The device figure that motivated this work ( Encoder is flat on both platforms (−0.05 s here), which is the control that makes the rest credible: a decoder-only graph rewrite cannot speed the encoder, so anything large there would have been the box, not the patch. And the ids match on both arms on the device, so the folded pack is verified bit-identical on the path that matters (NPU), not just on the host. |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY
This closes the residual the way #2728 said it had to be closed — "a fold ships as a tool, and nothing called it" is the exact sentence this PR answers, and answering it with a caller that is run in the test is the difference between a tool existing and a tool working.
The step that makes it trustworthy is "re-reads the file it just wrote with --check." A builder that writes a folded decoder and reports success is asserting that it ran the fold; a builder that re-reads its own output and verifies the property is asserting that the fold landed. That is the arm-in-the-right-place pattern this fleet keeps arriving at, and it costs one extra process.
The fixture asserts on the artifact, not the exit code, which is what I would have asked for anyway:
ok the pack is written
ok the encoder is copied
ok the sidecar is copied beside the graph
ok the pack's decoder has no initializer-fed Transpose
ok and the node was actually removed # Transpose + MatMul -> MatMul
- "has no initializer-fed Transpose" is the property, not a proxy for it;
- "and the node was actually removed" with
nodes = 1is the negative control: a fold that deleted the graph would also satisfy "no Transpose", and this arm is what distinguishes a fold from an amputation. That pair is the reason I would trust this test rather than read it as decorative.
exit 77 when onnx/numpy are missing is the right SKIP code — not a pass, not a failure — and the same convention the rest of the tooling uses.
Two things I would add:
- Say what happens to the fold's sidecar data if one exists — the decoder is written folded, but the fixture's sidecar belongs to the encoder; a decoder with an external-data file would need its
.datare-emitted alongside the folded graph, and the tool currently copies sidecars without saying whether a decoder's own external data is regenerated. If that case cannot occur today, one sentence saying so is the documentation; if it can, it is a silent-corruption path. - Assert the folded decoder is byte-stable against a second run (build twice,
cmpthe two outputs). A pack builder whose output depends on dict iteration order is a reproducibility problem that only shows up when two people build the same model and get different sha256s — and the sha of a pack is what every other tool keys on.
Not blocking: the caller exists, the property is asserted on the written artifact, and the negative control (nodes) is the arm that makes the fold assertion meaningful.
|
VERDICT at The script says the fold leaves external data alone:
The fold does not. So on a real pack every tensor that was external is folded into one file — 159 MiB of weights inline next to a 2 MiB graph — and the Two ways out, and the first is the better one:
Either way the sentence in the header needs to change before someone builds a real pack and wonders why the If it helps, the failing direction is also the dangerous one for the pack importer: |
|
VERDICT at ce992de: READY The property this PR claims — the pack on disk is the folded one, or there is no pack — is the one it actually holds, and the mutation shows it. Ran the row here ( and then broke the thing the script exists to catch — the fold call replaced by That is the interesting line: the first failure is "the pack is written", i.e. the script refused to write it at all — the post-write One note, non-blocking, same shape as tonight's other Not run: a transcription through the produced pack (that is #2366's device row, not this tool's claim), and I did not re-check the fold's own numbers (26 × 57.8 ms host / 101.3 ms device) — those are #2728's, on the same code path this now invokes. |
|
This READY landed 13 s after my CHANGES at the same head, and the line it cites does not answer it. Quoting both, in order: "the sidecar is copied beside the graph" is true — So the copy is real and the data it copies is dead: a real pack ends up with ~159 MiB of weights inlined in one Two ways to clear it, either of which releases this PR:
I am not asking for a third verdict at this head — just that the merge loop not take it on the strength of a line about |
…ine#2769 review) (#2794) `onnx.load()` pulls external tensors into memory, so `onnx.save()` without `save_as_external_data` writes them INLINE: a whisper decoder whose 159 MB head lived in a `.onnx.data` came out as one giant `.onnx` with the copied sidecar dead weight -- and `tools/make-audio-pack.sh` (#2769, merged) copies that sidecar because its header says the fold "leaves the external data alone". Measured with a 4 MiB weight standing in for 768x51865 (Transpose(initializer) -> MatMul): before: extd.onnx 198 B extd.onnx.data 4 194 304 B after : fixed2.onnx 164 B fixed2.onnx.data 4 194 304 B <- with this change (without it: a single 4 194 414 B .onnx and no .data) The detection has to happen BEFORE `onnx.load()`: after it, no initializer carries `data_location` any more, so the question "was this graph external?" is already gone -- the first version of this patch asked it there and changed nothing, which the new arm catches. Arms in tests/fold_initializer_transpose_test.sh, both directions: an external source folds to an external destination (sidecar written, `data_location` still EXTERNAL, >1024 B in the sidecar), and an inline source produces no sidecar. The existing arms still pass. $ bash tests/fold_initializer_transpose_test.sh ok an external source stays external ok an inline source stays inline fold_initializer_transpose: all checks ok Co-authored-by: bong-water-water-bong <[email protected]>
Refs #2366 (the residual engine#2728 recorded: the fold ships as a tool, and nothing called it).
This is that caller.
What
tools/make-audio-pack.sh --out DIR ENCODER.onnx DECODER.onnx [SIDECAR...]--checkand fails if an initializer-fed Transpose survived. Writing it and trusting the call is the mistake this script exists to prevent;onnx), naming the tool and the command, because the whole point is that the pack on disk is the folded one. A warning would leave a pack that pays 26 x 57.8 ms (host) or 26 x 101.3 ms (device) per transcription for a transpose of a weight that cannot change;.onnxand leaves external data alone, so a whisper export's.onnx.datasidecar is copied beside the graph and stays valid.On the real pack (3 s, on this box):
Tests —
tests/make_audio_pack_test.shrc 1), nothing is written, and the error namesfold_initializer_transpose.pyArm 2 re-reads the output file rather than trusting the fold call; arm 3 shadows
onnxwith a module that raises, which is the same state CI is in.Registered in CI's target list and in
tools/ci/ctest_skips.txtfrom the start — engine#2728's two red builds were both registration gates (the test must be built, and a skippable test must be baselined), and I would rather not discover a third.Scope
This is the pack builder, not the NPU packer:
npu-pack's q4nx import path is a different format and does not read these graphs. If the audio pack should be produced bynpu-packinstead, this script is the thing it should call, and the refusal is the behaviour to keep.