Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support relative path for best_model_checkpoint #7973

Merged
merged 1 commit into from
Oct 22, 2020

Conversation

HaebinShin
Copy link
Contributor

What does this PR do?

Fixes #7431

When I give a relative path to output_dir, it raises some error at line 1222.

def _sorted_checkpoints(self, checkpoint_prefix=PREFIX_CHECKPOINT_DIR, use_mtime=False) -> List[str]:
ordering_and_checkpoint_path = []
glob_checkpoints = [str(x) for x in Path(self.args.output_dir).glob(f"{checkpoint_prefix}-*")]
for path in glob_checkpoints:
if use_mtime:
ordering_and_checkpoint_path.append((os.path.getmtime(path), path))
else:
regex_match = re.match(f".*{checkpoint_prefix}-([0-9]+)", path)
if regex_match and regex_match.groups():
ordering_and_checkpoint_path.append((int(regex_match.groups()[0]), path))
checkpoints_sorted = sorted(ordering_and_checkpoint_path)
checkpoints_sorted = [checkpoint[1] for checkpoint in checkpoints_sorted]
# Make sure we don't delete the best model.
if self.state.best_model_checkpoint is not None:
best_model_index = checkpoints_sorted.index(self.state.best_model_checkpoint)
checkpoints_sorted[best_model_index], checkpoints_sorted[-1] = (
checkpoints_sorted[-1],
checkpoints_sorted[best_model_index],
)
return checkpoints_sorted

If I give './path1/path2' to output_dir, checkpoints_sorted becomes 'path1/path2' by Path library on line 1208.
But self.state.best_model_checkpoint is still './path1/path2'.
So it will raise error as below.

Traceback (most recent call last):
  File "../finetune.py", line 369, in <module>
    main(**vars(args))
  File "../finetune.py", line 315, in main
    device)
  File "../finetune.py", line 120, in train
    trainer.train(optim_pretrained_path)
  File "/usr/local/lib/python3.6/dist-packages/transformers/trainer.py", line 803, in train
    self._maybe_log_save_evaluate(tr_loss, model, trial, epoch)
  File "/usr/local/lib/python3.6/dist-packages/transformers/trainer.py", line 860, in _maybe_log_save_evaluate
    self._save_checkpoint(model, trial, metrics=metrics)
  File "/usr/local/lib/python3.6/dist-packages/transformers/trainer.py", line 918, in _save_checkpoint
    self._rotate_checkpoints(use_mtime=True)
  File "/usr/local/lib/python3.6/dist-packages/transformers/trainer.py", line 1235, in _rotate_checkpoints
    checkpoints_sorted = self._sorted_checkpoints(use_mtime=use_mtime)
  File "/usr/local/lib/python3.6/dist-packages/transformers/trainer.py", line 1223, in _sorted_checkpoints
    best_model_index = checkpoints_sorted.index(self.state.best_model_checkpoint)
ValueError: './results/use_pretrained_test/checkpoint-1162' is not in list

So I resolve this error by using Path for self.state.best_model_checkpoint.
Any other idea is also welcome. please check it

Copy link
Collaborator

@sgugger sgugger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, since there is a Path above, one might be necessary here too. Thanks for fixing!
Failure seems spurious but just in case, waiting for the test to pass to merge.

@sgugger sgugger merged commit f774b2e into huggingface:master Oct 22, 2020
fabiocapsouza pushed a commit to fabiocapsouza/transformers that referenced this pull request Nov 15, 2020
fabiocapsouza added a commit to fabiocapsouza/transformers that referenced this pull request Nov 15, 2020
This pull request was closed.
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.

2 participants