Skip to content

Commit

Permalink
Make predict() close progress bars after finishing (huggingface#17952) (
Browse files Browse the repository at this point in the history
huggingface#18078)

* Make Trainer.predict call on_evaluate (huggingface#17952)

* Add on_predict

* Small fix

* Small and different fix

* Add tests
  • Loading branch information
neverix authored Jul 8, 2022
1 parent 7c046c5 commit 8b332a6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/transformers/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2713,6 +2713,7 @@ def predict(
)
)

self.control = self.callback_handler.on_predict(self.args, self.state, self.control, output.metrics)
self._memory_tracker.stop_and_update_metrics(output.metrics)

return PredictionOutput(predictions=output.predictions, label_ids=output.label_ids, metrics=output.metrics)
Expand Down
15 changes: 15 additions & 0 deletions src/transformers/trainer_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ def on_evaluate(self, args: TrainingArguments, state: TrainerState, control: Tra
"""
pass

def on_predict(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, metrics, **kwargs):
"""
Event called after a successful prediction.
"""
pass

def on_save(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, **kwargs):
"""
Event called after a checkpoint save.
Expand Down Expand Up @@ -372,6 +378,9 @@ def on_evaluate(self, args: TrainingArguments, state: TrainerState, control: Tra
control.should_evaluate = False
return self.call_event("on_evaluate", args, state, control, metrics=metrics)

def on_predict(self, args: TrainingArguments, state: TrainerState, control: TrainerControl, metrics):
return self.call_event("on_predict", args, state, control, metrics=metrics)

def on_save(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
control.should_save = False
return self.call_event("on_save", args, state, control)
Expand Down Expand Up @@ -484,6 +493,12 @@ def on_evaluate(self, args, state, control, **kwargs):
self.prediction_bar.close()
self.prediction_bar = None

def on_predict(self, args, state, control, **kwargs):
if state.is_local_process_zero:
if self.prediction_bar is not None:
self.prediction_bar.close()
self.prediction_bar = None

def on_log(self, args, state, control, logs=None, **kwargs):
if state.is_local_process_zero and self.training_bar is not None:
_ = logs.pop("total_flos", None)
Expand Down
5 changes: 5 additions & 0 deletions src/transformers/utils/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ def on_prediction_step(self, args, state, control, eval_dataloader=None, **kwarg
else:
self.prediction_bar.update(self.prediction_bar.value + 1)

def on_predict(self, args, state, control, **kwargs):
if self.prediction_bar is not None:
self.prediction_bar.close()
self.prediction_bar = None

def on_log(self, args, state, control, logs=None, **kwargs):
# Only for when there is no evaluation
if args.evaluation_strategy == IntervalStrategy.NO and "loss" in logs:
Expand Down
3 changes: 3 additions & 0 deletions tests/trainer/test_trainer_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ def on_step_end(self, args, state, control, **kwargs):
def on_evaluate(self, args, state, control, **kwargs):
self.events.append("on_evaluate")

def on_predict(self, args, state, control, **kwargs):
self.events.append("on_predict")

def on_save(self, args, state, control, **kwargs):
self.events.append("on_save")

Expand Down

0 comments on commit 8b332a6

Please sign in to comment.