Skip to content

feat(offload): reference authenticated safetensors payloads - #3586

Merged
Qubitium merged 2 commits into
ModelCloud:mainfrom
tpurtell:upstream/08-safetensors-reference-offload-20261108
Nov 8, 2026
Merged

Qubitium merged 2 commits into
ModelCloud:mainfrom
tpurtell:upstream/08-safetensors-reference-offload-20261108

Conversation

@tpurtell

@tpurtell tpurtell commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Add explicit reference offload for an existing safetensors artifact, validate the tensor set, dtypes, shapes and contiguous ranges, and publish an Accelerate-compatible index while moving module leaves to meta. Bind payload digests and verify them during streaming export. Support exact-name/prefix filtering before state collection. The source remains caller-owned and must stay available; this is opt-in. Test that changed source payloads fail closed.

Stack

Layer 8/36 of a dependency-ordered fork extraction. Depends on #3585. 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 tests/test_save_state_dict_buffer_collection.py tests/test_config_only_export.py

69 passed, 1 CUDA skip. 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 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: layer 8/36 — authenticated safetensors reference offload

Rebased onto the layer-7 branch (9d20a5f3). One conflict in utils/model.py: this layer moves _atomic_shard_file next to the writer, which collided with the layer-6 permission fix at its old location. Resolved by keeping the fixed body at the new location. Diff vs the predecessor is only this layer's three files. Pushed as ea590da0.

Verified

  • tests/test_offload_files.py -k "streaming_state_dict or safetensors_reference" → 9 passed (7 original + 2 added)
  • ruff check clean
  • Manual probes for float8 round-trip and restore behavior

Fixed

  1. Duplicated dtype table omitted float8 — the new _SAFETENSORS_DTYPE copied a subset of _DTYPE_SAFE_MAP (the exact drift the float8 gotcha comment in utils/model.py warns about), so any float8 tensor made reference offload fail with "metadata differs". It now resolves spellings through _torch_dtype_to_safetensors (local import; model.py imports this module lazily). float8_e4m3fn round-trips byte-exactly; added coverage.
  2. Silent non-persistent buffer loss — module.to_empty(recurse=True) replaces all parameters and buffers, but state_dict() and the published index only record persistent ones. Reproduced: a persistent=False buffer becomes meta and is unrecoverable. The helper now fails closed before touching the module; added coverage.
  3. Windows portability — os.fchmod is Unix-only and os.O_DIRECTORY may not exist. Guarded (hasattr / os.name == "posix", matching looper/checkpoint_store.py).
  4. Restore behavior documented — no Accelerate hooks are attached, so undo_offload_to_disk leaves the module on meta (verified). Added a docstring note that this path is export-only via get_state_dict_for_save.

Notes (not blockers)

  • include_prefixes uses str.startswith, so a prefix model.layers.1 also matches model.layers.10.... There are no callers yet; nail down boundary semantics (name == p or name.startswith(p + ".")) before the first caller lands.
  • The slow branch (format="safetensors" without data_offsets) does not verify sha256; the new index writer always sets offsets, so only externally produced indexes are affected.
  • Source symlinks are rejected (os.path.islink). HF cache snapshots are symlinks, so callers must pass the resolved blob path (or the helper could realpath).
  • Index temp file is f"{index_path}.{os.getpid()}.tmp" opened with "x"; a stale file from a recycled PID would fail. tempfile.mkstemp would be more robust.
  • No parent_module_lock around the offload; concurrent offloads of the same module can race.
  • to_empty breaks tied-weight identity; export still writes both names, which HF tolerates.
  • Computing sha256 from the in-memory tensors validates content against the module, not against the source file; a source/module mismatch is caught at write time by _copy_file_stream (fail-closed), not here.

The design is sound: geometry is validated against the exact header, ranges must be contiguous to EOF, and the digest is verified while streaming so a changed source fails closed.

@Qubitium
Qubitium marked this pull request as ready for review November 8, 2026 08:10
@Qubitium
Qubitium force-pushed the upstream/08-safetensors-reference-offload-20261108 branch from ea590da to b49520f Compare November 8, 2026 08:16
tpurtell and others added 2 commits November 8, 2026 08:20
… buffers

- Derive safetensors spellings from utils/model.py's _DTYPE_SAFE_MAP instead of
  a local table that omitted float8; float8 reference offload now round-trips.
- Reject modules with non-persistent buffers: to_empty(recurse=True) would
  replace them with meta while state_dict()/index cannot record them, silently
  losing data. Fail before touching the module.
- Guard os.fchmod and directory fsync for non-POSIX hosts.
- Document that reference offload installs no Accelerate hooks, so
  undo_offload_to_disk cannot restore it.
@Qubitium
Qubitium force-pushed the upstream/08-safetensors-reference-offload-20261108 branch from b49520f to 64ddd1a Compare November 8, 2026 08:20
@Qubitium
Qubitium merged commit 6e769e0 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