MCPcopy
hub / github.com/huggingface/transformers / _save

Method _save

src/transformers/trainer.py:3833–3870  ·  view source on GitHub ↗

Save model weights, configuration, and processing class to `output_dir`.

(self, output_dir: str | None = None, state_dict: dict | None = None)

Source from the content-addressed store, hash-verified

3831 self.push_to_hub(commit_message="Model save", revision=self.args.hub_revision)
3832
3833 def _save(self, output_dir: str | None = None, state_dict: dict | None = None) -> None:
3834 """Save model weights, configuration, and processing class to `output_dir`."""
3835 # If we are executing this function, we are the process zero, so we don't check for that.
3836 output_dir = output_dir if output_dir is not None else self.args.output_dir
3837 os.makedirs(output_dir, exist_ok=True)
3838 logger.info(f"Saving model checkpoint to {output_dir}")
3839
3840 supported_classes = (PreTrainedModel,) if not is_peft_available() else (PreTrainedModel, PeftModel)
3841 # Save a trained model and configuration using `save_pretrained()`.
3842 # They can then be reloaded using `from_pretrained()`
3843 if not isinstance(self.model, supported_classes):
3844 if state_dict is None:
3845 state_dict = self.model.state_dict()
3846
3847 if isinstance(self.accelerator.unwrap_model(self.model, keep_torch_compile=False), supported_classes):
3848 self.accelerator.unwrap_model(self.model, keep_torch_compile=False).save_pretrained(
3849 output_dir, state_dict=state_dict
3850 )
3851 else:
3852 logger.info("Trainer.model is not a `PreTrainedModel`, only saving its state dict.")
3853 safetensors.torch.save_file(
3854 state_dict, os.path.join(output_dir, SAFE_WEIGHTS_NAME), metadata={"format": "pt"}
3855 )
3856 else:
3857 self.model.save_pretrained(output_dir, state_dict=state_dict)
3858
3859 if self.processing_class is not None:
3860 self.processing_class.save_pretrained(output_dir)
3861 elif (
3862 self.data_collator is not None
3863 and hasattr(self.data_collator, "tokenizer")
3864 and self.data_collator.tokenizer is not None
3865 ):
3866 logger.info("Saving Trainer.data_collator.tokenizer by default as Trainer.processing_class is `None`")
3867 self.data_collator.tokenizer.save_pretrained(output_dir)
3868
3869 # Good practice: save your training arguments together with the trained model
3870 torch.save(self.args, os.path.join(output_dir, TRAINING_ARGS_NAME))
3871
3872 # ---- Logging & Metrics ----
3873

Callers 1

save_modelMethod · 0.95

Calls 6

is_peft_availableFunction · 0.85
joinMethod · 0.80
infoMethod · 0.45
state_dictMethod · 0.45
save_pretrainedMethod · 0.45
saveMethod · 0.45

Tested by

no test coverage detected