save_pretrained: mkdir(exist_ok=True) (#5258)

* all save_pretrained methods mkdir if not os.path.exists
This commit is contained in:
Sam Shleifer
2020-06-28 14:53:47 -04:00
committed by GitHub
parent 12dfbd4f7a
commit 45e26125de
16 changed files with 17 additions and 59 deletions

View File

@@ -477,9 +477,10 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin):
Arguments:
save_directory: directory to which to save.
"""
assert os.path.isdir(
save_directory
), "Saving path should be a directory where the model and configuration can be saved"
if os.path.isfile(save_directory):
logger.error("Provided path ({}) should be a directory, not a file".format(save_directory))
return
os.makedirs(save_directory, exist_ok=True)
# Only save the model itself if we are using distributed training
model_to_save = self.module if hasattr(self, "module") else self