-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Trainer state as a trainer controller metric (#150)
Signed-off-by: Padmanabha V Seshadri <seshapad@in.ibm.com> Co-authored-by: Alex Brooks <alex.brooks@ibm.com>
- Loading branch information
1 parent
e8e9f21
commit 521a463
Showing
5 changed files
with
109 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
tests/data/trainercontroller/loss_on_threshold_with_trainer_state.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
controller-metrics: | ||
- name: state | ||
class: TrainingState | ||
- name: loss | ||
class: Loss | ||
controllers: | ||
- name: loss-controller | ||
triggers: | ||
- on_log | ||
rule: loss < 2 and state["epoch"] >= 0.5 | ||
operations: | ||
- hfcontrols.should_training_stop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
tuning/trainercontroller/controllermetrics/trainingstate.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Copyright The IBM Tuning Team | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# SPDX-License-Identifier: Apache-2.0 | ||
# https://spdx.dev/learn/handling-license-info/ | ||
|
||
# Standard | ||
from typing import Any | ||
import dataclasses | ||
|
||
# Third Party | ||
from transformers import TrainerState | ||
|
||
# Local | ||
from tuning.trainercontroller.controllermetrics.metricshandler import MetricHandler | ||
|
||
|
||
class TrainingState(MetricHandler): | ||
"""Implements the controller metric which exposes the trainer state""" | ||
|
||
def __init__(self, **kwargs): | ||
"""Initializes the metric handler, by registering the event \ | ||
list and arguments with base handler. | ||
Args: | ||
kwargs: List of arguments (key, value)-pairs | ||
""" | ||
super().__init__( | ||
events=[ | ||
"on_init_end", | ||
"on_step_end", | ||
"on_epoch_begin", | ||
"on_epoch_end", | ||
"on_prediction_step", | ||
"on_predict", | ||
"on_log", | ||
"on_train_end", | ||
"on_train_begin", | ||
"on_evaluate", | ||
], | ||
**kwargs | ||
) | ||
|
||
def validate(self) -> bool: | ||
"""Validate the training arguments (e.g logging_steps) are \ | ||
compatible with the computation of this metric. | ||
Returns: | ||
bool | ||
""" | ||
return True | ||
|
||
def compute(self, state: TrainerState = None, **kwargs) -> Any: | ||
"""Exposes the trainer state. | ||
Args: | ||
state: TrainerState object | ||
kwargs: Remaining event arguments | ||
Returns: | ||
dict. Trainer state as a dictionary | ||
""" | ||
return dataclasses.asdict(state) |