Save a model and its configuration file to a directory, so that it can be re-loaded using the `:func:`~transformers.PreTrainedModel.from_pretrained`` class method. Arguments: save_directory: directory to which to save.
(self, save_directory)
| 472 | self.base_model._prune_heads(heads_to_prune) |
| 473 | |
| 474 | def save_pretrained(self, save_directory): |
| 475 | """ Save a model and its configuration file to a directory, so that it |
| 476 | can be re-loaded using the `:func:`~transformers.PreTrainedModel.from_pretrained`` class method. |
| 477 | |
| 478 | Arguments: |
| 479 | save_directory: directory to which to save. |
| 480 | """ |
| 481 | if os.path.isfile(save_directory): |
| 482 | logger.error("Provided path ({}) should be a directory, not a file".format(save_directory)) |
| 483 | return |
| 484 | os.makedirs(save_directory, exist_ok=True) |
| 485 | |
| 486 | # Only save the model itself if we are using distributed training |
| 487 | model_to_save = self.module if hasattr(self, "module") else self |
| 488 | |
| 489 | # Attach architecture to the config |
| 490 | model_to_save.config.architectures = [model_to_save.__class__.__name__] |
| 491 | |
| 492 | # If we save using the predefined names, we can load using `from_pretrained` |
| 493 | output_model_file = os.path.join(save_directory, WEIGHTS_NAME) |
| 494 | |
| 495 | if getattr(self.config, "xla_device", False): |
| 496 | import torch_xla.core.xla_model as xm |
| 497 | |
| 498 | if xm.is_master_ordinal(): |
| 499 | # Save configuration file |
| 500 | model_to_save.config.save_pretrained(save_directory) |
| 501 | # xm.save takes care of saving only from master |
| 502 | xm.save(model_to_save.state_dict(), output_model_file) |
| 503 | else: |
| 504 | model_to_save.config.save_pretrained(save_directory) |
| 505 | torch.save(model_to_save.state_dict(), output_model_file) |
| 506 | |
| 507 | logger.info("Model weights saved in {}".format(output_model_file)) |
| 508 | |
| 509 | @classmethod |
| 510 | def from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs): |
nothing calls this directly
no outgoing calls
no test coverage detected