Skip to content

Commit

Permalink
Let EarlyStoppingCallback not require load_best_model_at_end (#35101
Browse files Browse the repository at this point in the history
)

* Bookmark

* Add warning
  • Loading branch information
muellerzr authored Jan 10, 2025
1 parent 0aaf124 commit b02828e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/transformers/trainer_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,10 +707,14 @@ def check_metric_value(self, args, state, control, metric_value):
self.early_stopping_patience_counter += 1

def on_train_begin(self, args, state, control, **kwargs):
assert args.load_best_model_at_end, "EarlyStoppingCallback requires load_best_model_at_end = True"
if not args.load_best_model_at_end:
logger.warning(
"Using EarlyStoppingCallback without load_best_model_at_end=True. "
"Once training is finished, the best model will not be loaded automatically."
)
assert (
args.metric_for_best_model is not None
), "EarlyStoppingCallback requires metric_for_best_model is defined"
), "EarlyStoppingCallback requires metric_for_best_model to be defined"
assert (
args.eval_strategy != IntervalStrategy.NO
), "EarlyStoppingCallback requires IntervalStrategy of steps or epoch"
Expand Down
17 changes: 17 additions & 0 deletions tests/trainer/test_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3484,6 +3484,23 @@ def test_early_stopping_callback(self):
except AssertionError:
self.assertEqual(trainer.state.global_step, 0)

# even if load_best_model_at_end is False, `best_model_checkpoint` should be set
with tempfile.TemporaryDirectory() as tmp_dir:
trainer = get_regression_trainer(
output_dir=tmp_dir,
num_train_epochs=20,
gradient_accumulation_steps=1,
per_device_train_batch_size=16,
load_best_model_at_end=False,
eval_strategy=IntervalStrategy.EPOCH,
save_strategy=IntervalStrategy.EPOCH,
compute_metrics=AlmostAccuracy(),
metric_for_best_model="accuracy",
)
trainer.add_callback(EarlyStoppingCallback(1, 0.0001))
train_output = trainer.train()
self.assertIsNotNone(trainer.state.best_model_checkpoint)

def test_flos_extraction(self):
with tempfile.TemporaryDirectory() as tmp_dir:
trainer = get_regression_trainer(learning_rate=0.1, output_dir=tmp_dir)
Expand Down

0 comments on commit b02828e

Please sign in to comment.