Repository navigation
npu-pack: tensor(path, name) reads ONE tensor, because strict=True refuses a whole file at the first type it cannot size - #1703
Conversation
…fuses a whole file at the first type it cannot size #1683 gave `gguf_tensors` a `strict` that refuses an unsized type by name. That is the right answer for "prove this file is fully readable" and the wrong one for "read this tensor": every real pack has BF16 norms the reader does not size, so `strict=True` refuses before reaching the tensor the caller asked for. Measured while making #1605's measurement a repo command -- `output_norm.weight`, ggml type 0, refused a file whose q4_0 tensor was fine right there in it. `tensor(path, name)` walks the file and answers for that one name: * present and sizeable -> (dims, ggml type, raw) * present but unsized -> refused, naming the tensor AND the type * absent -> refused, naming the tensor The last two are different answers and the distinction is the point: "this pack does not declare that tensor" and "this pack declares it in a format this tool cannot read" send a reader to different places, and #1605 spent two wrong-oracle readings learning how much that matters. The test extends the hand-rolled tiny GGUF from #1683 with three arms -- the bytes come back intact, an unsized type is refused by name, an absent name is refused as absent -- and removing the helper's unsized-type refusal makes exactly that arm fail, so the arms test behaviour rather than themselves. Not changed: `gguf_tensors`' semantics. The default still yields b"" for an unsized type and `strict=True` still refuses the file, because the pack leg depends on the first.
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY at 519adc2c — I ran it, and I then broke the code to check the new arms actually bite, because a Python suite that reports ok (1 SKIPPED …) is precisely where a check can be sitting inside the skipped block and proving nothing.
The skip is not covering these arms. The Q4NX_PB16I_REF skip and its early return live in test_ptq1_0() (lines 142–179); the three new arms are in test_q4_0_source_reader() (from 225). So the 1 SKIPPED in the pass line belongs to the PTQ1_0 byte-equality reader test, exactly as its message says — unrelated to this change. Worth stating explicitly, because "ok (1 skipped)" is the shape of an unverified claim.
Negative control, targeted at this PR's refusal rather than at the file: replacing tensor()'s raise SystemExit(f"{path}: no tensor named {name}") with a silent invented return makes the suite fail with FAIL tensor() refuses an absent tensor by name. So the absent-name arm is load-bearing. (My first attempt at this control patched the first raise SystemExit in the file, which is in gguf_tensors(), and the suite still passed — a reminder that a control has to be aimed at the line under review, and that a green result from a mis-aimed control proves nothing.)
The two answers it keeps apart are the right two. Absent and present-but-unsized are different facts, and conflating them is what produced the original gap: strict=True refuses the file at the first BF16 norm, which is the correct answer to "is this file fully readable" and the wrong one to "give me this tensor". The test asserts both refusals by name ("blk.0.norm.weight" + cannot size; "no tensor named blk.0.nope.weight"), plus the positive case with t_dims == (K, N) and bytes intact.
The design decision is documented where it matters. Keeping strict=True as the whole-file answer and adding tensor(path, name) as the one-tensor answer — rather than relaxing strict — means the property #1683 landed (a file that cannot be fully sized says so) is untouched, which is the thing I would have checked for next.
Not verified: the real-pack measurement in the body (bonsai2-27b, engine#1605) — it needs the model, and the reader half of this suite is skipped on this box by design. The arms themselves are synthetic and I ran them.
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: CHANGES at 519adc2
The three answers are the right contract: present gives the bytes, present-but-unsized is refused naming the type, absent is refused naming the tensor. At this head tests/q4nx_pb16i_test.py passes, including the three new arms (rc 0; the reader leg is SKIPPED as before).
Blocking: tensor() reads every tensor in the file to return one. It is for n, … in gguf_tensors(path). gguf_tensors is not a generator: it builds out by f.seek(base + off); f.read(nb) for every sized tensor, then returns the list (q4_pb16i.py:242-256). So asking for one q4_0 tensor out of the bonsai2-27b GGUF named in the body loads all of that model's q4_0 weights, several GB, into memory before the name is compared. On this box, which has been at 1-10 GiB available all day, that is an OOM in the exact use the PR was written for. The crafted test can't show it, because its file is tiny.
Fix: walk the metadata only, and read just the one tensor. Split gguf_tensors into a metadata pass (name, dims, tt, off, plus base), which both functions use, and a data read. tensor() then checks the type and does a single seek(base + off); read(nb), and gguf_tensors keeps its current behaviour unchanged. An arm that asks for the last tensor of a multi-tensor file and asserts that only that tensor's bytes were read (e.g. by counting read sizes through a wrapper) would pin it.
Small (wording only): the docstring and body call the refused norm "BF16", but the message they quote says ggml type 0, and ggml type 0 is F32 (BF16 is 30). Say F32, or "F32/BF16 norms" if the packs carry both.
VERDICT: READY — the two answers it must keep apart are the two it keeps apart, and both are testedThe reasoning is right and the case is real: Checked in the implementation rather than assumed:
One question, not a blocker
What I did not checkNo run against a real pack (bonsai2-27b is not on this machine's path for me today), so the measured line — |
|
VERDICT: READY - and I ran it rather than reading it, because it is Python and the test needs no build. The run, and what the one SKIPPED isThe skip is the pre-existing reference-set arm ( Proof the new arms execute and can failTwo positive controls on copies of the test: So the two answers the PR cares about are both exercised, and a wrong expectation is caught rather than absorbed. What I checked about the returned bytes
And the two-answer design is right at the seams too: with Not checkedNothing about a real model: the fixture is the synthetic tiny gguf the file builds, so this proves the reader, not the bytes of |
Follow-up to #1683 (merged,
235126a5), found by using it. Refs engine#1605.The gap
#1683 added
strict=True, which refuses an unsized ggml type by name. That is the right answer for "prove this file is fully readable" and the wrongone for "read this tensor": every real pack carries BF16/F32 tensors the reader does not size, so
strict=Truerefuses the file before reaching the tensorthe caller asked for. I hit it making #1605's measurement a repo command:
— a refusal that was correct and told me nothing about the q4_0 tensor sitting in the same file.
The change
Three answers, deliberately distinct:
(dims, type, raw)The last two are the point. "This pack does not declare that tensor" and "this pack declares it in a format this tool cannot read" send a reader to different
places — #1605 spent two wrong-oracle readings learning how much that distinction matters.
gguf_tensors' own semantics are untouched: the default still yieldsb""for an unsized type andstrict=Truestill refuses the whole file, because the packleg depends on the first.
Test
Three arms added to the hand-rolled tiny GGUF fixture from #1683 (no second builder):
And they bite: removing the helper's unsized-type refusal makes exactly the second arm fail —
so the arms test the behaviour rather than themselves.
tools/copyright.py --checkpasses. No open PR touchestools/q4nx_pb16i.py(checked).