Repository navigation
fix(writer): preserve existing shards and config extensions on export - #3584
Merged
Qubitium merged 2 commits intoNov 8, 2026
Merged
Conversation
Serialize configs without the empty state-dict cleanup path and replace completed shard files atomically. Keep live config fields authoritative and preserve source-only extensions. Retain the previous shard on write failures.
- Restore shard file permissions: mkstemp creates 0600 files, so the atomic rename made every exported shard owner-only. Preserve an existing shard's mode and otherwise honor the process umask like a plain open(). - Merge source-only config fields recursively so nested sub-configs such as text_config keep their extensions; skip regenerated/attention keys. - Read the source config before serializing so an in-place export does not lose the fields it is meant to preserve. - Fix the dummy generation config fixture, which wrote config.json. - Sort imports in the new test (CI ruff I001 gate).
Qubitium
force-pushed
the
upstream/06-config-only-export-20261108
branch
from
November 8, 2026 07:49
719541e to
4e73e00
Compare
Qubitium
reviewed
Nov 8, 2026
Qubitium
left a comment
Collaborator
There was a problem hiding this comment.
Review: layer 6/36 — config-only export + atomic shard writes
Rebased the branch onto current main (eb433e86) to isolate this layer; the diff is now only the 6 files this layer owns (writer.py, utils/model.py, 4 tests). Pushed as 4e73e000.
Verified
test_config_only_export.py test_writer_attention.py test_out_of_model_tensors.py test_split_by_layer_save.py test_writer_quant_log.py→ 27 passed, 3 skippedtest_reshard.py→ 30 passed (exercises the rewritten shard writer)ruff checkclean on every touched file
Fixed during review
- CI-blocking import order —
tests/test_config_only_export.pytrippedI001. The repo gate isruff check, so this PR could not merge as-is. Sorted. - Shard permissions regression —
tempfile.mkstempcreates0600, andos.replacecarried that mode onto every rewritten shard, so exports silently became owner-only (0644→0600, reproduced)._atomic_shard_filenow preserves an existing shard's mode and otherwise honors the process umask, matching the previousopen()behavior. Added a regression test. - Nested source-only config fields were dropped — the merge used a top-level
setdefault, so an extension field insidetext_config/vision_config(the shape used by the repo's multimodal fixtures, e.g. Qwen3.5) was still lost. It now merges recursively, with live values winning andquantization_config/quantize_config/attention keys excluded so regenerated sections cannot inherit stale source values. Added coverage. - In-place export lost the source fields —
config.save_pretrained()overwroteconfig.jsonbefore the merge read it wheneversave_dir == model_local_path. The source config is now captured before serialization. Added coverage. - Test fixture wrote the wrong file —
_DummyGenerationConfigintest_out_of_model_tensors.pyinherited asave_pretrainedthat writesconfig.json, which would clobber the model config as soon as that fixture gainscan_generate(). Overrode it to writegeneration_config.json.
Notes / remaining limitations (not blockers)
- The atomic rename is POSIX-friendly (existing mmaps keep the old inode), but
os.replaceover a destination that is still open/mapped can fail on Windows. In-place rewrite already failed there before, so this is not a new regression, but worth keeping in mind for Windows reshard-in-place. - If a re-export produces fewer shards than the previous checkpoint in the same directory, stale
model-*-of-*.safetensorsfiles remain. The index is correct; this is disk residue only and is preferable to deleting resumable shards. - No
fsyncbefore the rename: atomic against process failure, not power loss. generation_config.jsonis not source-merged (only the model config is). All GenerationConfig fields are declared, so this is likely fine, but it is a different policy fromconfig.json.- The direct serializers intentionally mirror Transformers' metadata path and omit the PEFT/
hf_quantizerbranches; GPTQModel saves adapters separately via_eora_save, so that looks correct.
The layer itself is sound: avoiding save_pretrained(state_dict={}) is the right fix for the shard-deletion behavior, and the direct serializers match Transformers' metadata preparation.
Qubitium
marked this pull request as ready for review
November 8, 2026 08:10
This was referenced Nov 8, 2026
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
Save model/generation metadata directly rather than invoking model.save_pretrained with an empty state dict that can remove existing shards. Preserve source-only config fields while live values win and attention implementation fields stay stripped. Write shard replacements to a sibling temporary file and rename only after a successful close, preserving existing memory maps and the previous shard on failure. This is atomic per shard, not a transaction across a complete multi-shard model. Update writer fixtures and add failure-preservation coverage.
Stack
Layer 6/36 of a dependency-ordered fork extraction. Depends on #3583. 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
21 passed, 3 skipped. 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.