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

bug: On save event added to callback #256

Merged
merged 5 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions tests/data/trainercontroller/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,4 @@
TRAINER_CONFIG_TEST_THRESHOLDED_TRAINING_LOSS_YAML = os.path.join(
_DATA_DIR, "thresholded-training-loss.yaml"
)
TRAINER_CONFIG_TEST_ON_SAVE_YAML = os.path.join(_DATA_DIR, "on-save.yaml")
10 changes: 10 additions & 0 deletions tests/data/trainercontroller/on-save.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
controller_metrics:
- name: state
class: TrainingState
controllers:
- name: stop_on_training_loss_on_save
triggers:
- on_save
rule: state["epoch"] >= 0.5
operations:
- hfcontrols.should_training_stop
16 changes: 16 additions & 0 deletions tests/trainercontroller/test_tuning_trainercontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,22 @@ def test_thresholded_training_loss():
assert control.should_training_stop is True


def test_thresholded_training_loss_on_save():
"""Tests the thresholded training loss example in
`examples/trainer-controller-configs/on-save.yaml`
"""
test_data = _setup_data()
tc_callback = tc.TrainerControllerCallback(td.TRAINER_CONFIG_TEST_ON_SAVE_YAML)
control = TrainerControl(should_training_stop=False)
# Trigger on_init_end to perform registration of handlers to events
tc_callback.on_init_end(
args=test_data.args, state=test_data.states[2], control=control
)
# Trigger rule and test the condition
tc_callback.on_save(args=test_data.args, state=test_data.states[2], control=control)
assert control.should_training_stop is True


def test_non_decreasing_training_loss():
"""Tests the non-decreasing training loss example in
`examples/trainer-controller-configs/non-decreasing-training-loss.yaml`
Expand Down
14 changes: 14 additions & 0 deletions tuning/trainercontroller/callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,3 +548,17 @@ def on_evaluate(
kwargs["state"] = state
kwargs["control"] = control
self._actions_on_event(event_name="on_evaluate", **kwargs)

def on_save(
self,
args: TrainingArguments,
state: TrainerState,
control: TrainerControl,
**kwargs,
):
# Training arguments, state and controls are folded into kwargs to be passed off to
# handlers
kwargs["args"] = args
kwargs["state"] = state
kwargs["control"] = control
self._actions_on_event(event_name="on_save", **kwargs)
Loading