xnn.common.models.hub.checkpoint.save_pretrained#

xnn.common.models.hub.checkpoint.save_pretrained(model, save_directory, *, config=None, card=None, verify=True, **card_fields)[source]#

Write a model as a portable directory.

The directory holds config.yaml (the configuration that rebuilds the architecture), model.pt (the weights, loadable with weights_only=True) and card.json (the ModelCard, with an MD5 per file). The card is written last, so a directory with a card is complete. Nothing in it refers to the machine it was written on.

Parameters:
  • model (torch.nn.Module or str or pathlib.Path) – A model (bare, or wrapped in ForceStressOutput) or the path of any checkpoint load_checkpoint() reads (e.g. a trainer’s best.pt), which is then repacked.

  • save_directory (str or pathlib.Path) – Output directory; created if missing.

  • config (Config or ModelConfig, optional) – The configuration the model was built from. Taken from the checkpoint when model is a path; required for a module.

  • card (ModelCard or dict, optional) – Card fields (name, description, license, citation, …). The architecture, cutoff, species, dtype, files and xnn version are filled in from the model.

  • verify (bool, optional) – Rebuild the architecture from config and load the weights strictly before writing, so a mismatched config fails here rather than at load time. Defaults to True.

  • **card_fields – More card fields, overriding card.

Returns:

The model directory.

Return type:

pathlib.Path

Raises:

ValueError – If no config is available, or config does not rebuild the weights.