Skip to content

fix(writer): preserve existing shards and config extensions on export - #3584

Merged
Qubitium merged 2 commits into
ModelCloud:mainfrom
tpurtell:upstream/06-config-only-export-20261108
Nov 8, 2026
Merged

Qubitium merged 2 commits into
ModelCloud:mainfrom
tpurtell:upstream/06-config-only-export-20261108

Conversation

@tpurtell

@tpurtell tpurtell commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

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

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_config_only_export.py tests/test_writer_attention.py tests/test_out_of_model_tensors.py tests/test_split_by_layer_save.py

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.

tpurtell and others added 2 commits November 8, 2026 07:45
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
Qubitium force-pushed the upstream/06-config-only-export-20261108 branch from 719541e to 4e73e00 Compare November 8, 2026 07:49

@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 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 skipped
  • test_reshard.py → 30 passed (exercises the rewritten shard writer)
  • ruff check clean on every touched file

Fixed during review

  1. CI-blocking import order — tests/test_config_only_export.py tripped I001. The repo gate is ruff check, so this PR could not merge as-is. Sorted.
  2. Shard permissions regression — tempfile.mkstemp creates 0600, and os.replace carried that mode onto every rewritten shard, so exports silently became owner-only (0644 → 0600, reproduced). _atomic_shard_file now preserves an existing shard's mode and otherwise honors the process umask, matching the previous open() behavior. Added a regression test.
  3. Nested source-only config fields were dropped — the merge used a top-level setdefault, so an extension field inside text_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 and quantization_config/quantize_config/attention keys excluded so regenerated sections cannot inherit stale source values. Added coverage.
  4. In-place export lost the source fields — config.save_pretrained() overwrote config.json before the merge read it whenever save_dir == model_local_path. The source config is now captured before serialization. Added coverage.
  5. Test fixture wrote the wrong file — _DummyGenerationConfig in test_out_of_model_tensors.py inherited a save_pretrained that writes config.json, which would clobber the model config as soon as that fixture gains can_generate(). Overrode it to write generation_config.json.

Notes / remaining limitations (not blockers)

  • The atomic rename is POSIX-friendly (existing mmaps keep the old inode), but os.replace over 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-*.safetensors files remain. The index is correct; this is disk residue only and is preferable to deleting resumable shards.
  • No fsync before the rename: atomic against process failure, not power loss.
  • generation_config.json is not source-merged (only the model config is). All GenerationConfig fields are declared, so this is likely fine, but it is a different policy from config.json.
  • The direct serializers intentionally mirror Transformers' metadata path and omit the PEFT/hf_quantizer branches; 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
Qubitium marked this pull request as ready for review November 8, 2026 08:10
@Qubitium
Qubitium merged commit bb2dc81 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