Skip to content

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

Open
vasanthrpjan1-boop wants to merge 1 commit intohuggingface:mainfrom
vasanthrpjan1-boop:save-config-in-checkpoints
Open

Save model config in Trainer checkpoints for non-PreTrainedModel models#45055
vasanthrpjan1-boop wants to merge 1 commit intohuggingface: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
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

1 participant