Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/transformers/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,8 +808,15 @@ def train(self, model_path: Optional[str] = None, trial: Union["optuna.Trial", D
logger.info(
f"Loading best model from {self.state.best_model_checkpoint} (score: {self.state.best_metric})."
)
is_data_parallel = False
if isinstance(model, torch.nn.DataParallel):
model = model.module
is_data_parallel = True
if isinstance(model, PreTrainedModel):
self.model = model.from_pretrained(self.state.best_model_checkpoint)
if is_data_parallel:
# re-wrap with DataParallel
self.model = torch.nn.DataParallel(self.model)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The model attribute of the Learner is never wrapped in DataParallel or the like, it stays a reference to the original model so these lines are not necessary.
With the same reasoning, I think the only change to make is on the line above this comment (L816) and replace

self.model = model.from_pretrained(self.state.best_model_checkpoint)

by

self.model = self.model.from_pretrained(self.state.best_model_checkpoint)

if not self.args.model_parallel:
self.model = self.model.to(self.args.device)
else:
Expand Down