Skip to content

feat(writer): reuse content-authenticated safetensors shards - #3585

Merged
Qubitium merged 2 commits into
ModelCloud:mainfrom
tpurtell:upstream/07-authenticated-shard-resume-20261108
Nov 8, 2026
Merged

Qubitium merged 2 commits into
ModelCloud:mainfrom
tpurtell:upstream/07-authenticated-shard-resume-20261108

Conversation

@tpurtell

@tpurtell tpurtell commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Reuse a completed shard only when its exact header, size, and all payload digests match the current source. Compare legacy unhashed offload sources by content. Rewrite changed or truncated artifacts through the preceding atomic writer. This avoids repeated writes without treating matching filenames as completion evidence.

Stack

Layer 7/36 of a dependency-ordered fork extraction. Depends on #3584. 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 streaming_state_dict

5 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/07-authenticated-shard-resume-20261108 branch from 12dcf8c to 9d20a5f Compare November 8, 2026 07:52

@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 7/36 — content-authenticated shard reuse

Rebased onto the layer-6 branch (4e73e000) rather than bare main: this layer calls layer 6's _atomic_shard_file, so dropping the predecessor would break it. Diff against the predecessor is only the two files this layer owns (utils/model.py, test_offload_files.py). Pushed as 9d20a5f3.

Verified

  • tests/test_offload_files.py -k streaming_state_dict → 6 passed (5 original + 1 added)
  • Layer-6 fast suites on top of this branch (test_config_only_export, test_writer_attention, test_reshard) → 36 passed
  • ruff check clean on touched files
  • Manual probe: unchanged source reuses, changed source rewrites, corrupt destination rewrites

Changes made

  • Added test_streaming_state_dict_to_shards_rewrites_when_source_changes. The existing tests prove "corrupt destination → rewrite" and "unchanged → reuse", but not the core claim in the other direction: a changed source must not be masked by an unchanged header. It passes.
  • Normalized top-level spacing around the new hashing helpers (ruff does not gate E302 in this repo, so this was cosmetic).

No functional defects found. The reuse decision is sound: exact header bytes + exact total size + per-entry payload digests + no trailing bytes, with a symlink guard, and it falls back to a full atomic rewrite on any miss.

Notes (not blockers)

  • OffloadTensorRef.sha256 is currently never populated: _bundle_module_state_dict writes only dtype/shape/safetensors_file/weight_name/data_offsets, and _load_offload_entry does not read a sha256 key. So today every reuse goes through the content-hash fallback; the digest fast path becomes live with layer 8 (#3586). Consistent with the stack, just worth stating explicitly.
  • Reuse is not free: it hashes the source payload and then re-reads the whole existing shard, i.e. ~2× read instead of 1 read + 1 write. That is the right trade for slow/interrupted exports, but on fast NVMe with large checkpoints it can be close to a wash. If it matters, consider logging the reused byte count/timing or skipping reuse below a size threshold.
  • Reused shards keep their existing file mode; only rewritten shards are normalized by the layer-6 writer. Minor.
  • _existing_shard_matches returns False on digest is None and catches OSError/TypeError/ValueError/struct.error. A corrupt source safetensors raises SafetensorError and aborts rather than rewriting; the write path would fail on it anyway, so this is acceptable, but fail-closed-to-rewrite would be friendlier.
  • _safetensors_header returns a header dict that _write_shard_file discards (_, header_bytes, _). Cosmetic.
  • No fsync before rename (inherited from layer 6): atomic against process failure, not power loss.

Layer looks good to land after #3584.

@Qubitium
Qubitium marked this pull request as ready for review November 8, 2026 08:10
tpurtell and others added 2 commits November 8, 2026 08:14
@Qubitium
Qubitium force-pushed the upstream/07-authenticated-shard-resume-20261108 branch from 9d20a5f to b55819b Compare November 8, 2026 08:15
@Qubitium
Qubitium merged commit 3dfe88a into ModelCloud:main Nov 8, 2026
1 check passed
Qubitium added a commit that referenced this pull request Dec 1, 2026
Remove the ds4rt paths that no longer have a caller on main:

- Drop the DS4/GLM `configure_base_replay_store` and
  `create_quantization_layer_output_writer` model hooks and the generic
  `ForwardExecutor` hook dispatch. `CalibrationConfig.spill_path` (G2)
  is the only output-collection path now.
- Drop GLM5's `_prune_glm5_next_replay_frontier`, reachable only from the
  deleted hook.
- Drop the never-written `distributed_quant_device` state read in
  `build_subset_plan`.
- Retire `EXL3Config.module_include`: old checkpoints still load, the
  field warns once and is ignored, and `to_dict()` stops emitting it.
  `dynamic` `+:`/`-:` rules replace it.

The test-only `offload_to_safetensors_reference` fixture, the #3585
shard-reuse probe and LazyTurtle remain because they still have callers
or nothing is left in tree.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Qubitium added a commit that referenced this pull request Dec 1, 2026
Remove the ds4rt paths that no longer have a caller on main:

- Drop the DS4/GLM `configure_base_replay_store` and
  `create_quantization_layer_output_writer` model hooks and the generic
  `ForwardExecutor` hook dispatch. `CalibrationConfig.spill_path` (G2)
  is the only output-collection path now.
- Drop GLM5's `_prune_glm5_next_replay_frontier`, reachable only from the
  deleted hook.
- Drop the never-written `distributed_quant_device` state read in
  `build_subset_plan`.
- Retire `EXL3Config.module_include`: old checkpoints still load and
  warn once, but quantization now raises with a `dynamic` negative-rule
  example instead of silently quantizing every module; `to_dict()` stops
  emitting it.

The test-only `offload_to_safetensors_reference` fixture, the #3585
shard-reuse probe and LazyTurtle remain because they still have callers
or nothing is left in tree.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Qubitium added a commit that referenced this pull request Dec 1, 2026
Remove the ds4rt paths that no longer have a caller on main:

- Drop the DS4/GLM `configure_base_replay_store` and
  `create_quantization_layer_output_writer` model hooks and the generic
  `ForwardExecutor` hook dispatch. `CalibrationConfig.spill_path` (G2)
  is the only output-collection path now.
- Drop GLM5's `_prune_glm5_next_replay_frontier`, reachable only from the
  deleted hook.
- Drop the never-written `distributed_quant_device` state read in
  `build_subset_plan`.
- Retire `EXL3Config.module_include`: old checkpoints still load and
  warn once, but quantization now raises with a `dynamic` negative-rule
  example instead of silently quantizing every module; `to_dict()` stops
  emitting it.

The test-only `offload_to_safetensors_reference` fixture, the #3585
shard-reuse probe and LazyTurtle remain because they still have callers
or nothing is left in tree.

Co-authored-by: Claude Opus 5.5 <[email protected]>
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