Repository navigation
fix(writer): stream E8M0 safetensors scales - #3581
Conversation
85781b0 to
03fd1cb
Compare
Qubitium
left a comment
There was a problem hiding this comment.
Review
Rebased onto main after #3580 merged. PR is now MERGEABLE, changedFiles: 2
(down from 5), diff is exactly this layer: model.py +2, test_offload_files.py +27.
Was conflicting — now rebased
The branch carried layers 1 and 2 (254b3112, 76c9ed33), both now merged and
both conflicted with main. Dropped them and rebased only the E8M0 commit:
git rebase --onto origin/main 76c9ed33
Verified the merged predecessors are present in the result (restore_device from
#3580, _rope_parameters_are_per_layer from #3579) and that no hf.py /
structure.py / nested-rope changes remain in the diff.
Change itself is correct
torch.float8_e8m0fnu was already listed in _FLOAT8_DTYPE_NAMES (so byte math
and the warning-colour path knew about it) but absent from _DTYPE_SAFE_MAP, which
is what _torch_dtype_to_safetensors consults before writing the header. The
two-line addition fixes a real NotImplementedError:
NotImplementedError: Unsupported dtype for safetensors export: torch.float8_e8m0fnu
I confirmed the test is a genuine regression test — removing the two lines makes
test_streaming_state_dict_to_shards_writes_float8_e8m0_tensors fail with exactly
that error, and restoring them passes.
I also verified the paths the test does not cover:
- hand-written header emits
"dtype": "F8_E8M0"with correctdata_offsets; _dtype_string_to_torch("f8_e8m0")maps back totorch.float8_e8m0fnu, so the
read side round-trips (that mapping already existed in_DTYPE_STR_MAP);_torch_dtype_num_bytesreturns 1, consistent with the offset math;- all five torch float8 dtypes still resolve to a size.
tests/test_offload_files.py: 59 passed, 1 skipped. Ruff clean.
Answering the dependency question
No, the safetensors requirement does not need raising. >=0.8.0 is already
sufficient — and it is not actually the floor for E8M0.
I tested the real library behaviour at three versions by installing each wheel into
an isolated target dir and overriding PYTHONPATH (the >=0.8.0 import guard comes
from transformers, not from safetensors' own parser, so it masks the underlying
capability):
| safetensors | save_file E8M0 |
read gptqmodel's hand-written F8_E8M0 header |
get_tensor dtype |
|---|---|---|---|
| 0.6.2 | ok | ok | torch.float8_e8m0fnu |
| 0.7.0 | ok | ok | torch.float8_e8m0fnu |
| 0.8.0 | ok | ok | torch.float8_e8m0fnu |
The C parser accepts F8_E8M0 well before 0.8.0, and byte-for-byte payload
equality holds at every version. So this PR introduces no new dependency floor.
The existing safetensors>=0.8.0 pin in requirements.txt should stay as it is —
it is driven by transformers 5.x, which hard-fails below it:
ImportError: safetensors>=0.8.0 is required for a normal functioning of this module, but found safetensors==0.7.0
Raising it further would be wrong: 0.8.0 is the latest stable release (no 0.9.0
final exists yet), so there is nothing newer to pin to.
One caveat worth recording: the reason the E8M0 dtype fails today is gptqmodel's
own _DTYPE_SAFE_MAP, not safetensors. If a future torch adds another float8
flavour, the same two-line gap will reappear — this PR fixes e8m0 specifically
rather than making the map derive from _FLOAT8_DTYPE_NAMES. A follow-up could
generate the entries from the dtype-name tuple to remove the whole class of bug.
Not blocking; the explicit entries also carry the exact safetensors spelling
(F8_E8M0 vs a derived guess), which is the safer default.
The dtype map and `_FLOAT8_DTYPE_NAMES` are intentionally separate, and the header identifier is not derivable from the torch dtype name. Both facts are easy to get wrong the next time a float8 flavour is added, so note them where the map is defined: - the schema uses short names (`float8_e4m3fn` -> `F8_E4M3`, `float8_e8m0fnu` -> `F8_E8M0`); emitting a guessed `F8_E4M3FN` makes the reader reject the whole file with "unknown variant" while unpacking the header; - `float8_e4m3fnuz`/`float8_e5m2fnuz` are valid identifiers and absent here only because no export path produces them, not because they cannot be written; - a widened `_FLOAT8_DTYPE_NAMES` does not become exportable on its own, and a missing entry fails loudly with NotImplementedError rather than corrupting bytes. Comment only; no behaviour change.
Follow-up: gotcha documented (and a correction to my review)Added as a comment above the float8 entries in
Verified against safetensors 0.8.0: The comment records three things so the next float8 addition does not trip:
Tests unchanged: 2 passed. No behaviour change, so this does not need re-review |
Summary
Recognize Torch float8_e8m0fnu as the F8_E8M0 safetensors dtype. Verify byte-preserving export of scale values without arithmetic conversion. This changes dtype serialization only.
Stack
Layer 3/36 of a dependency-ordered fork extraction. Depends on #3580. Review only this layer.
GitHub requires this cross-fork PR to target upstream
main, so its Files changed tab includes earlier stack layers. After the predecessor lands, rebase this branch and retarget the next layer; do not merge later layers out of order. No predecessor is merged automatically.Tests
1 passed. Used Python 3.12, Torch 2.14.1, Transformers 5.19.0. No model quantization, GPU work, downloads, or heavyweight model validation. Unit fixtures are small and CPU execution is constrained to one math thread.
Review and remaining validation
This layer and its dependency diff were reviewed for scope, existing extension points, and compatibility with the resolved fork snapshot. New focused regression coverage is included. Kept as a draft for maintainer review; hardware paths and end-to-end model behavior have not been revalidated in this session. No speed claim. Original integration/history is preserved in tpurtell#3.