Skip to content

Save model config in Trainer checkpoints for non-PreTrainedModel models - #45055

Closed
vasanthrpjan1-boop wants to merge 1 commit into
huggingface:mainfrom
vasanthrpjan1-boop:save-config-in-checkpoints
Closed

vasanthrpjan1-boop wants to merge 1 commit into
huggingface:mainfrom
vasanthrpjan1-boop:save-config-in-checkpoints

Conversation

@vasanthrpjan1-boop

Copy link
Copy Markdown
Contributor

What does this PR do?

When Trainer saves a checkpoint for a model that is not a PreTrainedModel (e.g. a custom nn.Module), it only saves the state dict but not the model config. This means Model.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.json in the checkpoint directory when the model has a config attribute, even when it falls through to the state-dict-only saving path. This enables argumentless loading from Trainer checkpoints:

# Before: needed init args
model = MyModel.from_pretrained(ckpt_path, **init_args)

# After: just works
model = MyModel.from_pretrained(ckpt_path)

The change is a 3-line addition in Trainer._save() that calls config.save_pretrained(output_dir) on the unwrapped model's config when available. This does not affect the existing PreTrainedModel path, which already saves config via save_pretrained().

Fixes #44450

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
@ArthurZucker

Copy link
Copy Markdown
Collaborator

Closing this one: no activity since 2026-03-27. Thanks for the contribution, and feel free to open a fresh PR against main if it's still relevant!

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.

Support argumentless loading from Trainer checkpoints

2 participants