Save model config in Trainer checkpoints for non-PreTrainedModel models#45055
Open
vasanthrpjan1-boop wants to merge 1 commit intohuggingface:mainfrom
Open
Save model config in Trainer checkpoints for non-PreTrainedModel models#45055vasanthrpjan1-boop wants to merge 1 commit intohuggingface:mainfrom
vasanthrpjan1-boop wants to merge 1 commit intohuggingface:mainfrom
Conversation
When the model is not a `PreTrainedModel`, Trainer only saves the state dict but not the model config. This means `from_pretrained(ckpt_path)` fails because there is no `config.json` in the checkpoint directory. Save `config.json` alongside the state dict when the model has a config attribute, enabling argumentless loading from Trainer checkpoints. Fixes huggingface#44450 Made-with: Cursor
evalstate
added a commit
to evalstate/transformers
that referenced
this pull request
Apr 28, 2026
Direct merge conflicted after Trainer refactors; applied the minimal config-saving change from 57cb2b9.
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.
What does this PR do?
When
Trainersaves a checkpoint for a model that is not aPreTrainedModel(e.g. a customnn.Module), it only saves the state dict but not the model config. This meansModel.from_pretrained(ckpt_path)requires the caller to pass all the original init arguments again, which is inconvenient.This PR saves the model's
config.jsonin the checkpoint directory when the model has aconfigattribute, even when it falls through to the state-dict-only saving path. This enables argumentless loading from Trainer checkpoints:The change is a 3-line addition in
Trainer._save()that callsconfig.save_pretrained(output_dir)on the unwrapped model's config when available. This does not affect the existingPreTrainedModelpath, which already saves config viasave_pretrained().Fixes #44450