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
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.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.FileNotFoundErrorcase from the issue matrix.Tests
tests/models/test_modeling_common.py::TestModelUtilstest_save_pretrained_removes_superseded_checkpoints[None|ema]— parametrized over both variants and covers the container switch in both directions; asserts the previous file is gone and that reloaded weights match the freshly written ones.test_save_pretrained_preserves_other_variants— a plain save and avariant="ema"save coexist in the same directory.Verified red-then-green: reverting the fix makes both parametrized cases fail with the stale
.safetensorsstill present (assert 'diffusion_pytorch_model.ema.safetensors' not in [...]), and they pass again with the fix applied.