Repository navigation
tools: hf_tensor.py reads one safetensors tensor with the offset rule I got wrong four times (refs #1934) - #2003
Conversation
… I got wrong four times (refs #1934) A safetensors `data_offsets` entry is measured from the **start of the data section**, i.e. `8 + header_len` bytes into the file -- not from byte 8. Fetch `bytes=8+off` and you get a slice of a *different* tensor, one header-length away; it has a plausible size, it is finite, and a correlation against it is a correlation against noise. That mistake cost two false conclusions about a merged PR tonight, both stated publicly before I caught them: "the 640 B gap does not correlate with the source model's q/k gains at any offset" and "the container still holds the pre-#1938 two-copies signature", the second of which I had to retract on #1976 when the correctly-fetched tensor gave `mean 1.3705, max 1.703125, min 0` — matching the container to three decimals. The tell was in my own output: a norm gain is ~1.0 and my "HF" values were +-0.03. So this tool does the arithmetic once and makes a wrong read *visible* rather than plausible: ``` $ tools/hf_tensor.py --repo tencent/Hunyuan-7B-Instruct \ --tensor model.layers.0.self_attn.query_layernorm.weight model.layers.0.self_attn.query_layernorm.weight BF16 [128] 256 B (model-00001-of-00004.safetensors) mean +1.370461 min -0.000000 max +1.703125 nonfinite 0 a norm gain is ~1.0 and a weight matrix is ~0: check these before trusting a correlation ``` * reads only the index, the shard's 8-byte length + header, and the tensor's own bytes (three range requests); * prints mean/min/max/nonfinite, because that is what separates "the tensor I asked for" from "a slice of its neighbour"; * `--out` writes the raw bytes for a comparison script, `--json` for one; * `--self-test` drives the rule on a **synthetic** shard with no network: a first tensor long enough that the second's offset is past it, then the same offset applied from the data section (returns the tensor) and from byte 8 (does not). Registered as `hf_tensor_self` so the rule is pinned in every configure; `ctest -R 'hf_tensor|tests_referenced'` -> 3/3. Refs #1934. This is the first half of the `diff-hf` instrument I asked for on that issue; the second half -- decode a *container* tensor through the importer's own decoder and print the per-row regression ratio -- is the next step.
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at dd516f4: READY
Checked at dd516f4. The offset rule is right, and I could test it against an independent read:
tools/hf_tensor.py --self-test: ok.- A real read agrees with mine, bit for bit. For #1976 I range-fetched
tencent/Hunyuan-7B-Instructlayer-0k_projwith my own script (data at8 + header_len + data_offsets[0]).hf_tensor.py --tensor model.layers.0.self_attn.k_proj.weight --outgives the same 8,388,608 bytes: the BF16 arrays arenp.array_equalon their bit patterns. The printed statistics (mean +0.000008, min -0.244, max +0.224) are what a weight matrix should look like. - The rule's arm can fail. With
data_section_start()returning8, the self-test fails "the data section starts at 8 + header_len, not at 8". Restored, it's ok. ci_targets_checkis at baseline 9,tests_referenced_checkpasses (22 tool self-tests registered, this one included), copyright rc 0, and a three-way merge onto main is clean.
Printing mean/min/max next to every read is the useful design choice. It's what turns "a plausible slice of the neighbour" into something you notice.
|
VERDICT: READY at Three things are right, and the first is rare:
And it is registered the pushable way: Nothing to change. |
VERDICT at
|
A safetensors
data_offsetsentry is measured from the start of the data section, i.e.8 + header_lenbytes into the file — not from byte 8. Fetchbytes=8+offand you get a slice of a different tensor one header-length away; it has a plausible size, it is finite, and a correlation against it is a correlation against noise.That mistake cost two false conclusions about a merged PR tonight, both stated publicly before I caught them: "the 640 B gap does not correlate with the source model's q/k gains at any offset" and "the container still holds the pre-#1938 two-copies signature" — the second retracted on #1976 when the correctly-fetched tensor gave
mean 1.3705, max 1.703125, min 0, matching the container to three decimals. The tell was in my own output: a norm gain is ≈1.0 and my "HF" values were ±0.03.So this tool does the arithmetic once and makes a wrong read visible rather than plausible:
--outwrites the raw bytes for a comparison script,--jsonfor one;--self-testdrives the rule on a synthetic shard, no network: a first tensor long enough that the second's offset is past it, then the same offset applied from the data section (returns the tensor) and from byte 8 (does not) — plus a decoded norm gain of 1.5 so the statistics are pinned too.Registered as
hf_tensor_self(through #1891'sadd_python_test), so the rule is pinned in every configure:Refs #1934. First half of the
diff-hfinstrument I proposed there; the second half — decode a container tensor through the importer's own decoder and print the per-row regression ratio against one of these dumps — is the next step.