Repository navigation
Conversation
`ModelMixin.save_pretrained` cleaned a previous save by removing files whose name matched the shard pattern of the current save, but it only recognized sharded files. Single-file checkpoints of the other container were left behind: re-saving a `.safetensors` checkpoint as `.bin` (or the reverse) kept the old file on disk, and because `from_pretrained` prefers sharded and safetensors checkpoints, the stale one silently took precedence over the freshly written weights. Extract the cleanup into `_get_superseded_checkpoint_files`, which returns every artifact of the current save's variant that the new save replaces: single-file weights, shards, and sharded indexes, in either container. Files belonging to other variants (e.g. `ema`) share the directory and are left untouched. Add a regression test covering both directions of the container switch and the variant coexistence case. Fixes huggingface#14769
kyo-zzz
force-pushed
the
fix/save-pretrained-stale-checkpoints
branch
from
October 31, 2026 03:27
1c895ee to
055e979
Compare
This branch has not been deployed
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.
Fixes #14769.
ModelMixin.save_pretrainedcleans a previous save by removing files that match the shard pattern of the current save, but it only recognized sharded files. Single-file checkpoints of the other container were left behind: re-saving a.safetensorscheckpoint as.bin(or the reverse) kept the old file on disk, and becausefrom_pretrainedprefers sharded and safetensors checkpoints, the stale one silently took precedence over the freshly written weights — no error, no warning.Relationship to #14844
#14844 fixed the sibling problem: cleanup matched another variant's shards through a bare prefix, and a formerly-sharded checkpoint left a stale index behind. It scopes the match to the container being written (
filename.removesuffix(ext)), so a.safetensors→.binre-save still leaves the old.safetensorson disk.This PR is rebased on top of it and keeps that anchoring, then extends the cleanup to both containers.
Changes
_get_superseded_checkpoint_files, which returns every artifact of the current save's variant that the new save replaces: single-file weights, shards, and sharded indexes, in either container.diffusion_pytorch_modelvsdiffusion_pytorch_model.ema), so checkpoints of other variants that share the directory are preserved. Shards are matched through_REGEX_SHARDgroup 1 rather than a prefix, which keeps the fix: scope save_pretrained shard cleanup to the saved variant (avoid deleting another variant's shards) #14844 regression fixed.FileNotFoundErrorcase from the issue matrix.Tests
tests/models/test_modeling_common.py::TestModelUtilstest_save_pretrained_preserves_other_variant_shards(from fix: scope save_pretrained shard cleanup to the saved variant (avoid deleting another variant's shards) #14844) — unchanged.test_save_pretrained_removes_superseded_checkpoints[None|ema]— parametrized over both variants, keeps a checkpoint of the other variant in the directory, switches the container in both directions, and asserts that the superseded file is gone, the other variant survives, and the reloaded weights match the freshly written ones.Verified red-then-green against the current
main: reverting onlysrc/diffusers/models/modeling_utils.pytomainmakes both new cases fail (assert 'diffusion_pytorch_model.ema.safetensors' not in [...]) while the #14844 tests still pass; with the fix applied all four pass.ruff checkandruff format --checkare clean.