-
Notifications
You must be signed in to change notification settings - Fork 66
feat: logging control operation #264
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
Merged
ashokponkumar
merged 13 commits into
foundation-model-stack:main
from
seshapad:logging_metric
Aug 8, 2024
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b36364e
feat: logging control operation
seshapad 2faee05
fix: Removed unwanted warning
seshapad 304b5c1
fix: Replaced log_levels with one from package
seshapad 925bc71
fix: Formatting issues resolved
seshapad 73da0b6
fix: log_level value should be small-case
seshapad 29bbb60
fix: Format issues resolved
seshapad 070ff21
fix: Arguments reordered to support folding
seshapad c07bd28
fix: Formatting issues resolved
seshapad 021e7c7
fix: Reordered arguments in logcontrol
seshapad 006a350
fix: Metrics flattened and passed to operations
seshapad 2710e9f
fix: Example log controller yaml
seshapad 76a17b3
fix: Logging control yaml and kwargs corrected
seshapad 4656bee
fix: Format issues
seshapad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,16 @@ | ||
| controller_metrics: | ||
| - name: trainer_state | ||
| class: TrainingState | ||
| operations: | ||
| - name: logcontrolstep | ||
| class: LogControl | ||
| arguments: | ||
| log_format: 'This is a test log format [{event_name}] => {trainer_state}' | ||
| log_level: warning | ||
| controllers: | ||
| - name: log-controller-step | ||
| triggers: | ||
| - on_step_end | ||
| rule: 'True' | ||
| operations: | ||
| - logcontrolstep.should_log |
This file contains hidden or 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 hidden or 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,16 @@ | ||
| controller_metrics: | ||
| - name: trainer_state | ||
| class: TrainingState | ||
| operations: | ||
| - name: logcontrolstep | ||
| class: LogControl | ||
| arguments: | ||
| log_format: 'This is a test log format [{event_name}] => {trainer_state}' | ||
| log_level: warning | ||
| controllers: | ||
| - name: log-controller-step | ||
| triggers: | ||
| - on_step_end | ||
| rule: 'True' | ||
| operations: | ||
| - logcontrolstep.should_log |
4 changes: 2 additions & 2 deletions
4
tests/data/trainercontroller/loss_on_threshold_with_trainer_state.yaml
This file contains hidden or 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 |
|---|---|---|
| @@ -1,12 +1,12 @@ | ||
| controller_metrics: | ||
| - name: state | ||
| - name: trainer_state | ||
| class: TrainingState | ||
| - name: training_loss | ||
| class: Loss | ||
| controllers: | ||
| - name: loss_controller | ||
| triggers: | ||
| - on_log | ||
| rule: training_loss['loss'] < 2 and state["epoch"] >= 0.5 | ||
| rule: training_loss['loss'] < 2 and trainer_state["epoch"] >= 0.5 | ||
| operations: | ||
| - hfcontrols.should_training_stop |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,10 +1,10 @@ | ||
| controller_metrics: | ||
| - name: state | ||
| - name: trainer_state | ||
| class: TrainingState | ||
| controllers: | ||
| - name: stop_on_training_loss_on_save | ||
| triggers: | ||
| - on_save | ||
| rule: state["epoch"] >= 0.5 | ||
| rule: trainer_state["epoch"] >= 0.5 | ||
| operations: | ||
| - hfcontrols.should_training_stop |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,55 @@ | ||
| # Third Party | ||
| from transformers import TrainingArguments | ||
| from transformers.utils import logging | ||
|
|
||
| # Local | ||
| from .operation import Operation | ||
|
|
||
| logger = logging.get_logger(__name__) | ||
| logger.setLevel(level=logging.DEBUG) | ||
|
|
||
|
|
||
| class LogControl(Operation): | ||
| """Operation that can be used to log useful information on specific events.""" | ||
|
|
||
| def __init__(self, log_format: str, log_level: str, **kwargs): | ||
| """Initializes the HuggingFace controls. In this init, the fields with `should_` of the | ||
| transformers.TrainerControl data class are extracted, and for each of those fields, the | ||
| control_action() method's pointer is set, and injected as a class member function. | ||
|
|
||
| Args: | ||
| kwargs: List of arguments (key, value)-pairs | ||
| """ | ||
| log_levels = logging.get_log_levels_dict() | ||
| if log_level not in log_levels: | ||
| raise ValueError( | ||
| "Specified log_level [%s] is invalid for LogControl" % (log_level) | ||
| ) | ||
| self.log_level = log_levels[log_level] | ||
| self.log_format = log_format | ||
| super().__init__(**kwargs) | ||
|
|
||
| def should_log( | ||
| self, | ||
| event_name: str = None, | ||
| control_name: str = None, | ||
| args: TrainingArguments = None, | ||
| **kwargs, | ||
| ): | ||
| """This method peeks into the stack-frame of the caller to get the action the triggered | ||
| a call to it. Using the name of the action, the value of the control is set. | ||
|
|
||
| Args: | ||
| control: TrainerControl. Data class for controls. | ||
| kwargs: List of arguments (key, value)-pairs | ||
| """ | ||
| log_msg = self.log_format.format( | ||
| event_name=event_name, | ||
| control_name=control_name, | ||
| args=args, | ||
| **kwargs, | ||
| ) | ||
| logger.log( | ||
| self.log_level, | ||
| log_msg, | ||
| ) |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the right convention?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems like it. Here is one example.