-
Notifications
You must be signed in to change notification settings - Fork 1
Add auto torch optimizer parameter logging convenience functionality #33
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
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,9 @@ | ||
| from .loss_group_config_logger import AutoLossGroupConfigLogger | ||
| from .model_config_logger import AutoModelConfigLogger | ||
| from .optimizer_config_logger import AutoOptimizerConfigLogger | ||
|
|
||
| __all__ = [ | ||
| "AutoModelConfigLogger", | ||
| "AutoOptimizerConfigLogger", | ||
| "AutoLossGroupConfigLogger", | ||
| ] |
97 changes: 97 additions & 0 deletions
97
src/virtual_stain_flow/vsf_logging/auto_loggers/loss_group_config_logger.py
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,97 @@ | ||
| from typing import Any, Dict, Optional | ||
|
|
||
| import mlflow | ||
|
|
||
| from ...trainers.trainer_protocol import TrainerProtocol | ||
|
|
||
|
|
||
| class AutoLossGroupConfigLogger: | ||
| """ | ||
| Auto-log loss group metadata to MLflow. | ||
| """ | ||
|
|
||
| def __init__(self, logger: Any) -> None: | ||
| self._logger = logger | ||
|
|
||
| def discover_loss_groups( | ||
| self, | ||
| trainer: Optional[TrainerProtocol], | ||
| ) -> Dict[str, Any]: | ||
| if trainer is None: | ||
| return {} | ||
|
|
||
| loss_groups: Dict[str, Any] = {} | ||
|
|
||
| explicit_groups = getattr(trainer, "loss_groups", None) | ||
| if isinstance(explicit_groups, dict): | ||
| for group_name, group in explicit_groups.items(): | ||
| if hasattr(group, "get_config"): | ||
| loss_groups[str(group_name)] = group | ||
|
|
||
| fallback_attrs = { | ||
| "main": "_loss_group", | ||
| "generator": "_generator_loss_group", | ||
| "discriminator": "_discriminator_loss_group", | ||
| } | ||
| for group_name, attr in fallback_attrs.items(): | ||
| if group_name in loss_groups: | ||
| continue | ||
| group = getattr(trainer, attr, None) | ||
| if group is not None and hasattr(group, "get_config"): | ||
| loss_groups[group_name] = group | ||
|
|
||
| return loss_groups | ||
|
|
||
| def log_loss_group_configs( | ||
| self, | ||
| trainer: Optional[TrainerProtocol], | ||
| ) -> None: | ||
| loss_groups = self.discover_loss_groups(trainer) | ||
| if not loss_groups: | ||
| return None | ||
|
|
||
| for group_name, group in loss_groups.items(): | ||
| try: | ||
| group_config = group.get_config() | ||
| except Exception as e: | ||
| print( | ||
| f"Could not get loss group config for logging " | ||
| f"({group_name}): {e}" | ||
| ) | ||
| continue | ||
|
|
||
| if not isinstance(group_config, list): | ||
| continue | ||
|
|
||
| for idx, item_cfg in enumerate(group_config): | ||
| if not isinstance(item_cfg, dict): | ||
| continue | ||
|
|
||
| if "key" in item_cfg and item_cfg["key"] is not None: | ||
| mlflow.set_tag( | ||
| f"loss.{group_name}.{idx}.name", | ||
| str(item_cfg["key"]), | ||
| ) | ||
|
|
||
| if "weight" in item_cfg and item_cfg["weight"] is not None: | ||
| mlflow.set_tag( | ||
| f"loss.{group_name}.{idx}.weight", | ||
| str(item_cfg["weight"]), | ||
| ) | ||
|
|
||
| try: | ||
| self._logger.log_config( | ||
| tag=f"loss_group_{group_name}", | ||
| config={ | ||
| "group_name": group_name, | ||
| "items": group_config, | ||
| }, | ||
| stage=None, | ||
| ) | ||
| except Exception as e: | ||
| print( | ||
| f"Fail to log loss group config as artifact " | ||
| f"({group_name}): {e}" | ||
| ) | ||
|
|
||
| return None |
61 changes: 61 additions & 0 deletions
61
src/virtual_stain_flow/vsf_logging/auto_loggers/model_config_logger.py
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,61 @@ | ||
| from typing import Any, List, Optional | ||
|
|
||
| import mlflow | ||
|
|
||
| from ...models.model import BaseModel | ||
| from ...trainers.trainer_protocol import TrainerProtocol | ||
|
|
||
|
|
||
| class AutoModelConfigLogger: | ||
| """ | ||
| Auto-log model configuration metadata to MLflow. | ||
| """ | ||
|
|
||
| def __init__(self, logger: Any) -> None: | ||
| self._logger = logger | ||
|
|
||
| def _discover_models(self, trainer: Optional[TrainerProtocol]) -> List[Any]: | ||
| if trainer is None: | ||
| return [] | ||
|
|
||
| explicit_models = getattr(trainer, "_models", None) | ||
| if isinstance(explicit_models, list): | ||
| return explicit_models | ||
|
|
||
| model = getattr(trainer, "model", None) | ||
| if model is not None: | ||
| return [model] | ||
|
|
||
| return [] | ||
|
|
||
| def log_model_configs(self, trainer: Optional[TrainerProtocol]) -> None: | ||
| models = self._discover_models(trainer) | ||
|
|
||
| for idx, model in enumerate(models): | ||
| if not isinstance(model, BaseModel) or not hasattr(model, "to_config"): | ||
| continue | ||
|
|
||
| try: | ||
| config = model.to_config() | ||
| except Exception as e: | ||
| print(f"Could not get model config for logging: {e}") | ||
| config = None | ||
|
|
||
| if not isinstance(config, dict): | ||
| continue | ||
|
|
||
| class_path = config.get("class_path") | ||
| if class_path: | ||
| mlflow.set_tag( | ||
| f"model.{idx}.class_path", | ||
| str(class_path), | ||
| ) | ||
|
|
||
| try: | ||
| self._logger.log_config( | ||
| tag=model.__class__.__name__, | ||
| config=config, | ||
| stage=None, | ||
| ) | ||
| except Exception as e: | ||
| print(f"Fail to log model config as artifact: {e}") |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.