Skip to content

fix(writer): stream E8M0 safetensors scales - #3581

Merged
Qubitium merged 2 commits into
ModelCloud:mainfrom
tpurtell:upstream/03-e8m0-export-20261108
Nov 8, 2026
Merged

Qubitium merged 2 commits into
ModelCloud:mainfrom
tpurtell:upstream/03-e8m0-export-20261108

Conversation

@tpurtell

@tpurtell tpurtell commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

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

CUDA_VISIBLE_DEVICES='' HIP_VISIBLE_DEVICES='' OMP_NUM_THREADS=1 MKL_NUM_THREADS=1 OPENBLAS_NUM_THREADS=1 HF_HUB_OFFLINE=1 TRANSFORMERS_OFFLINE=1 PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 python -m pytest -q tests/test_offload_files.py -k float8_e8m0

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.

@Qubitium
Qubitium force-pushed the upstream/03-e8m0-export-20261108 branch from 85781b0 to 03fd1cb Compare November 8, 2026 05:01

@Qubitium Qubitium left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 correct data_offsets;
  • _dtype_string_to_torch("f8_e8m0") maps back to torch.float8_e8m0fnu, so the
    read side round-trips (that mapping already existed in _DTYPE_STR_MAP);
  • _torch_dtype_num_bytes returns 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.

@Qubitium
Qubitium marked this pull request as ready for review November 8, 2026 05:13
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.
@Qubitium

Qubitium commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator

Follow-up: gotcha documented (and a correction to my review)

Added as a comment above the float8 entries in _DTYPE_SAFE_MAP (4a25b863,
comment-only). While writing it I verified the claims against the parser and found
one statement in my earlier review was wrong, so correcting it here:

  • I wrote that float8_e4m3fnuz / float8_e5m2fnuz were "intentionally absent
    because exposing them would emit a header the default reader cannot map back."
    That is false — I tested it, and F8_E4M3FNUZ / F8_E5M2FNUZ parse
    correctly and map to torch.float8_e4m3fnuz / torch.float8_e5m2fnuz on
    safetensors 0.8.0. They are simply not produced by any current export path.
  • My point that the identifier is not a mechanical transformation of the torch
    name is correct, and it is sharper than I stated: emitting a guessed
    F8_E4M3FN makes the reader reject the entire file with
    unknown variant 'F8_E4M3FN' while unpacking the header — not a per-tensor
    failure. The accepted spellings are F8_E4M3, F8_E5M2, F8_E8M0,
    F8_E4M3FNUZ, F8_E5M2FNUZ.

Verified against safetensors 0.8.0:

F8_E4M3      OK    -> torch.float8_e4m3fn
F8_E4M3FN    FAIL  -> invalid JSON in header: unknown variant
F8_E5M2      OK    -> torch.float8_e5m2
F8_E8M0      OK    -> torch.float8_e8m0fnu
F8_E4M3FNUZ  OK    -> torch.float8_e4m3fnuz
F8_E5M2FNUZ  OK    -> torch.float8_e5m2fnuz

The comment records three things so the next float8 addition does not trip:

  1. the identifier must be looked up, never derived from torch.float8_*;
  2. _FLOAT8_DTYPE_NAMES (sizes) and _DTYPE_SAFE_MAP (serializable ids) are
    deliberately separate, so widening one does not make a dtype exportable;
  3. the failure is a loud NotImplementedError at export, not silent corruption.

Tests unchanged: 2 passed. No behaviour change, so this does not need re-review
beyond the wording.

@Qubitium
Qubitium merged commit 19b53e4 into ModelCloud:main Nov 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants