Repository navigation
perf(checkpoint): stream continuation capture and verify it once - #5925
Merged
Merged
Conversation
Qubitium
force-pushed
the
fix/5749-continuation-stream
branch
from
November 20, 2026 14:34
1dd3606 to
065eb43
Compare
Continuation capture built the full safetensors payload in memory, cloned every tensor, then copied the bytes again through CheckpointStore.put(). Resume read the whole continuation into a bytes buffer just to verify it, read it again in prepare(), and decoded a second copy; every repeated tensor reference also carried its own payload entry. ContinuationCodec now exposes dump_file(), which writes the safetensors object straight into CheckpointStore.stage(); put_file() hashes and renames it without a second buffer, and sha256() reuses the same bytes for the calibration identity. Encoding registers one entry per distinct tensor object, writes already-CPU tensors without a clone, and copies only views whose storage overlaps another entry. On resume, _manifest() streams the continuation through object_path() once and records the verified path; prepare() decodes it with safe_open, materializing each tensor once for all aliases. Bitwise verification compares the decoded tensor and its version counter, with a cloned reference for inference tensors. Measured on a 192 MiB continuation (128 MiB replay + 64 MiB dense): commit peak delta 552 -> 14 MiB and 2 -> 0 full-payload store reads; restore peak delta 502 -> 186 MiB with 2 -> 0 full reads; encode peak delta 548 -> 386 MiB. Restored tensors remain bitwise identical.
Qubitium
force-pushed
the
fix/5749-continuation-stream
branch
from
November 20, 2026 15:23
065eb43 to
f3a1c32
Compare
Qubitium
marked this pull request as ready for review
November 20, 2026 15:29
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Continuation checkpointing on
mainmaterializes the whole continuationpayload several times. This PR streams it to disk once on capture and verifies
it once on resume, cutting commit peak RSS by 97.5% and restore peak RSS by
63.0% on a 192 MiB continuation while keeping every restored tensor bitwise
identical.
Fixes #5749
Root cause
ContinuationCodec.dumps()cloned every tensor(
gptqmodel/looper/continuation.py:34:.to(device="cpu").contiguous().clone())and then built one contiguous
bytesobject for the whole payload. The callerpassed those bytes to
CheckpointStore.put(), which hashed and copied themagain (
gptqmodel/looper/checkpoint.py:151). The calibration identity built yetanother full buffer to hash it (
gptqmodel/looper/gptq_checkpoint.py:393).On resume,
CheckpointStore._manifest()calledget(), which read and hashedthe entire payload into memory only to discard it
(
gptqmodel/looper/checkpoint_store.py:267).prepare()then calledget()again, andloads()parsed the header and decoded a second copy(
gptqmodel/looper/checkpoint.py:110). Repeated references to one tensor alsoproduced one payload entry and one clone per reference.
What Changed
ContinuationCodecnow separates encoding from I/O:dump_file(state, path)writes the safetensors object directly to a path.dumps(state)keeps the in-memory form for callers that need bytes.sha256(state, directory)hashes exactly the bytesdumps()would produce,using a temporary file under the offload root instead of a heap buffer.
load_file(path)decodes withsafetensors.safe_open, materializing eachpayload tensor once and reusing it for aliases.
id). Already-CPUtensors are written from the live state without a clone; only a view whose
storage overlaps another entry is copied, because safetensors rejects shared
storage. Non-CPU tensors are still moved to CPU.
CheckpointStoregainsstage()(private staging path, always removed) andcontinuation_path();_manifest()now streams the continuation throughobject_path()once and records the verified path, andcommit()usesobject_path()instead ofget().collect()also removes stray.<32 hex>.tmpstaging files.CheckpointExtension.on_boundary()streams capture throughdump_file()+put_file();prepare()decodes throughload_file().and
_version, so an in-place transfer that mutates its source still fails;inference tensors (no version counter) keep a cloned reference.
docs/looper-checkpoint-redesign.mddocuments the shipped flow, the oldmechanism and the failure paths.
The continuation is hashed when the manifest is verified and decoded from that
same verified path. External edits during a run remain out of contract, as
before; the change removes one full read plus a second hash, not the integrity
check.
Tests
New file
tests/test_checkpoint_continuation_stream_5749.py(6 tests):main(99f4720)test_file_codec_matches_in_memory_bytes_and_restores_bitwisedump_file/sha256absent)test_encoder_does_not_clone_contiguous_cpu_tensorscontinuation encoder cloned a CPU tensor)test_commit_does_not_read_continuation_into_memory[4194544, 4194544]full reads)test_resume_restores_all_tensors_bitwisetest_corrupted_continuation_fails_integritytest_checkpoint_commit_peak_rss_is_boundedThe last test drives the 192 MiB state in a fresh subprocess and asserts no
CheckpointStore.get()of 1 MiB or more and a commit peak-RSS delta below256 MiB. The codec test asserts the staged file is byte-identical to
dumps()(so existing calibration digests stay valid), alias deduplication(6 references to 4 payload entries), bitwise restore and alias identity.
Commands and results on this branch (CPU host, Python 3.14.7, Torch
2.15.0+cu132, no GPU):
Broader checkpoint family (
test_checkpoint_*,test_fail_closed_checkpoint_5218.py,test_nested_checkpoint_resave_5380.py): 222 passed, 7 failed. All 7 failuresreproduce unchanged on
mainand are environment limits, not this change:test_checkpoint_modules.py::test_qqq_optional_group_scales_schema(missingoptional import), four
test_checkpoint_state_invariant.pycases (no AVX-512/ GPU), and two
test_public_exl3_loader_checks_checkpoint_codebook_markerscases (no EXL3 native build). GPU, AVX-512, EXL3, BitBLAS and bitsandbytes
validation is unavailable on this CPU-only host, so those paths are not
claimed as tested.
Measured Impact
Same machine, same 192 MiB continuation state (128 MiB bf16 replay
(16, 2048, 2048)+ 64 MiB bf16 dense(8192, 4096)), fresh subprocess perrevision,
resource.getrusage(RUSAGE_SELF).ru_maxrss. No GPU is present, sothese are host RSS measurements; no VRAM claim is made.
CheckpointStore.getreads, commit + resume (count / bytes)The standalone issue reproduction counted 3 verification-only full reads
(576 MiB) on
mainand 0 after this change; the commit harness counted 2get()reads (402,655,040 B) and the restore harness 2 reads onmain, both 0after. Resume still decodes each tensor once from the file (that is the one
unavoidable read); it no longer copies the whole payload into a
bytesbuffer first.
Model Support / Module Plan
Not applicable: no model definitions,
MODEL_MAPentries or module plans aretouched.
Logic Flow Documentation
Applicable: this changes tensor materialization and checkpoint persistence.
flowchart TD A["CheckpointExtension.on_boundary"] --> B["adapter.capture"] B --> C["ContinuationCodec.dump_file"] C --> D["ContinuationCodec._encode"] D --> E{"device is CPU?"} E -- "yes" --> F["Write live tensor; clone only overlapping views"] E -- "no" --> G["tensor.to(device=cpu)"] F --> H["save_file into CheckpointStore.stage path"] G --> H H --> I["CheckpointStore.put_file: hash, fsync, rename to objects/sha256"] I --> J["CheckpointStore.commit: object_path checks"] J --> K["Manifest published; CURRENT replaced"] L["CheckpointExtension.prepare"] --> M["CheckpointStore.load: _manifest calls object_path"] M --> N["_verified_continuations records the hashed path"] N --> O["continuation_path returns the recorded path"] O --> P["ContinuationCodec.load_file: safe_open"] P --> Q["_decode materializes each tensor once"] Q --> R{"version and raw bytes match?"} R -- "no" --> X["ValueError; checkpoint_restore_failed"] R -- "yes" --> S["adapter.restore; committed cursor"] M -- "checksum mismatch" --> Y["CheckpointCorrupt; try the older generation"] I -. "exception" .-> Z["stage deletes its temp file"] classDef failure fill:#fce8e6,stroke:#a33,color:#111 class X,Y,Z failuregptqmodel/looper/continuation.py:34cloned every tensor, thengptqmodel/looper/checkpoint.py:151built a second fullbytesbuffer withdumps(), whichput()copied again.dump_file()streams the payload intoCheckpointStore.stage();put_file()hashes and renames it without a second buffer. CPU tensors are written as-is._encode()registers one entry per distinct tensor object (id); aliases restore to the same object.gptqmodel/looper/gptq_checkpoint.py:393serialized the whole continuation tobytesto hash it.ContinuationCodec.sha256()hashes the same bytes from a temporary file in the offload root.gptqmodel/looper/checkpoint_store.py:267calledget(), reading and hashing the whole payload into memory only to discard it._manifest()callsobject_path(), which streams and hashes the file, and records the verified path.gptqmodel/looper/checkpoint.py:110calledloads(store.get(...)): one fullbytesread, a header parse, thensafetensors.load()copied the payload again.load_file()decodes withsafe_open; each payload tensor is materialized once and shared by aliases._version; inference tensors keep a cloned reference.The same diagram and table are committed in
docs/looper-checkpoint-redesign.md(new "Continuation capture and restore I/O" section), which is already linked
from
docs/README.md.Review Requirements
Kernel Accuracy Requirements
Not applicable: no kernel, kernel-selection or packed-layout code is changed.
Notes
dumps()/loads()remain, so existing callers and tests keep working; thestaged file is byte-identical to
dumps()and the calibration digest isunchanged for the same state.
_verified_continuationsis per lease and cleared in__enter__.than again at prepare time. A file mutated between those two points is not
re-hashed; editing checkpoint objects during a run was already outside the
documented contract.
validation of the restore path.