-
Notifications
You must be signed in to change notification settings - Fork 946
[refactor] Extend WandbLogger to log config variables, entity and kwargs #1129
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
Changes from all commits
78b1428
f879c53
1a74a25
a0decd2
a97cf2a
5cc98b8
5dbbbd8
23addf0
2571772
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -225,6 +225,12 @@ def summarize_report( | |
| if not is_main() and not is_xla(): | ||
| return | ||
|
|
||
| # Log the learning rate if available | ||
| if wandb_logger and "lr" in extra: | ||
| wandb_logger.log_metrics( | ||
| {"train/learning_rate": float(extra["lr"])}, commit=False | ||
| ) | ||
|
|
||
| if tb_writer: | ||
| scalar_dict = meter.get_scalar_dict() | ||
| tb_writer.add_scalars(scalar_dict, current_iteration) | ||
|
|
@@ -395,21 +401,19 @@ class WandbLogger: | |
| Log using `Weights and Biases`. | ||
|
|
||
| Args: | ||
| name: Display name for the run. | ||
| save_dir: Path where data is saved (./save/logs/wandb/ by default). | ||
| project: Display name for the project. | ||
| **init_kwargs: Arguments passed to :func:`wandb.init`. | ||
| entity: An entity is a username or team name where you're sending runs. | ||
| config: Configuration for the run. | ||
| project: Name of the W&B project. | ||
|
|
||
| Raises: | ||
| ImportError: If wandb package is not installed. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| name: Optional[str] = None, | ||
| save_dir: Optional[str] = None, | ||
| entity: Optional[str] = None, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here you can just keep **init_kwargs and remove all the other parameters. This way it will be more flexible, and users can still find all the relevant fields enumerated in the docs.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will it be okay if we can at least keep the current set of parameters and add **init_kwargs? Explicit mention of some of the arguments will be useful for the users in my opinion, especially, As per your suggestion, we can achieve |
||
| config: Optional[Dict] = None, | ||
| project: Optional[str] = None, | ||
| **init_kwargs, | ||
| ): | ||
| try: | ||
| import wandb | ||
|
|
@@ -420,10 +424,12 @@ def __init__( | |
| ) | ||
|
|
||
| self._wandb = wandb | ||
|
|
||
| self._wandb_init = dict(name=name, project=project, dir=save_dir) | ||
|
|
||
| self._wandb_init.update(**init_kwargs) | ||
| self._wandb_init = dict(entity=entity, config=config, project=project) | ||
| wandb_kwargs = dict(config.training.wandb) | ||
| wandb_kwargs.pop("enabled") | ||
| wandb_kwargs.pop("entity") | ||
| wandb_kwargs.pop("project") | ||
| self._wandb_init.update(**wandb_kwargs) | ||
|
|
||
| self.setup() | ||
|
|
||
|
|
@@ -453,14 +459,16 @@ def _should_log_wandb(self): | |
| else: | ||
| return True | ||
|
|
||
| def log_metrics(self, metrics: Dict[str, float]): | ||
| def log_metrics(self, metrics: Dict[str, float], commit=True): | ||
| """ | ||
| Log the monitored metrics to the wand dashboard. | ||
|
|
||
| Args: | ||
| metrics (Dict[str, float]): [description] | ||
| metrics (Dict[str, float]): A dictionary of metrics to log. | ||
| commit (bool): Save the metrics dict to the wandb server and | ||
| increment the step. (default: True) | ||
| """ | ||
| if not self._should_log_wandb(): | ||
| return | ||
|
|
||
| self._wandb.log(metrics) | ||
| self._wandb.log(metrics, commit=commit) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,42 +1,75 @@ | ||
| --- | ||
| id: concepts | ||
| title: Terminology and Concepts | ||
| sidebar_label: Terminology and Concepts | ||
| id: logger | ||
| title: Weights and Biases Logging | ||
| sidebar_label: Weights and Biases Logging | ||
| --- | ||
|
|
||
| ## Weights and Biases Logger | ||
|
|
||
| MMF has a `WandbLogger` class which lets the user to log their model's progress using [Weights and Biases](https://gitbook-docs.wandb.ai/). | ||
| MMF now has a `WandbLogger` class which lets the user to log their model's progress using [Weights and Biases](https://wandb.ai/site). Enable this logger to automatically log the training/validation metrics, system (GPU and CPU) metrics and configuration parameters. | ||
|
|
||
| ## First time setup | ||
|
|
||
| To set up wandb, run the following: | ||
| ``` | ||
| pip install wandb | ||
| ``` | ||
| In order to log anything to the W&B server you need to authenticate the machine with W&B **API key**. You can create a new account by going to https://wandb.ai/signup which will generate an API key. If you are an existing user you can retrieve your key from https://wandb.ai/authorize. You only need to supply your key once, and then it is remembered on the same device. | ||
|
|
||
| ``` | ||
| wandb login | ||
| ``` | ||
|
|
||
| ## W&B config parameters | ||
|
|
||
| The following options are available in config to enable and customize the wandb logging: | ||
| ```yaml | ||
| training: | ||
| # Weights and Biases control, by default Weights and Biases (wandb) is disabled | ||
| wandb: | ||
| # Whether to use Weights and Biases Logger, (Default: false) | ||
| enabled: false | ||
| enabled: true | ||
| # An entity is a username or team name where you're sending runs. | ||
| # This is necessary if you want to log your metrics to a team account. By default | ||
| # it will log the run to your user account. | ||
| entity: null | ||
| # Project name to be used while logging the experiment with wandb | ||
| wandb_projectname: mmf_${oc.env:USER} | ||
| project: mmf | ||
| # Experiment/ run name to be used while logging the experiment | ||
| # under the project with wandb | ||
| wandb_runname: ${training.experiment_name} | ||
| name: ${training.experiment_name} | ||
| # Specify other argument values that you want to pass to wandb.init(). Check out the documentation | ||
| # at https://docs.wandb.ai/ref/python/init to see what arguments are available. | ||
| # job_type: 'train' | ||
| # tags: ['tag1', 'tag2'] | ||
| env: | ||
| wandb_logdir: ${env:MMF_WANDB_LOGDIR,} | ||
| ``` | ||
| To enable wandb logger the user needs to change the following option in the config. | ||
|
|
||
| `training.wandb.enabled=True` | ||
| * To enable wandb logger the user needs to change the following option in the config. | ||
|
|
||
| `training.wandb.enabled=True` | ||
|
|
||
| * To give the `entity` which is the name of the team or the username, the user needs to change the following option in the config. In case no `entity` is provided, the data will be logged to the `entity` set as default in the user's settings. | ||
|
|
||
| `training.wandb.entity=<teamname/username>` | ||
|
|
||
| * To give the current experiment a project and run name, user should add these config options. The default project name is `mmf` and the default run name is `${training.experiment_name}`. | ||
|
|
||
| `training.wandb.project=<ProjectName>` <br> | ||
| `training.wandb.name=<RunName>` | ||
|
|
||
| * To change the path to the directory where wandb metadata would be stored (Default: `env.log_dir`): | ||
|
|
||
| `env.wandb_logdir=<dir_name>` | ||
|
|
||
| To give the current experiment a project and run name, user should add these config options. | ||
| * To provide extra arguments to `wandb.init()`, the user just needs to define them in the config file. Check out the documentation at https://docs.wandb.ai/ref/python/init to see what arguments are available. An example is shown in the config parameter shown above. Make sure to use the same key name in the config file as defined in the documentation. | ||
|
|
||
| `training.wandb.wandb_projectname=<ProjectName> training.wandb.wandb_runname=<RunName>` | ||
| ## Current features | ||
|
|
||
| To change the path to the directory where wandb metadata would be stored (Default: `env.log_dir`): | ||
| The following features are currently supported by the `WandbLogger`: | ||
|
|
||
| `env.wandb_logdir=<dir_name>` | ||
| * Training & Validation metrics | ||
| * Learning Rate over time | ||
| * GPU: Type, GPU Utilization, power, temperature, CUDA memory usage | ||
| * Log configuration parameters |
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.
Here you can just pass in wandb init_kwargs straightaway from config.training.wandb (without explicitly declaring entity, project, ... fields). Just unpack config.training.wandb and pass it all in at once, similar to what's done with lightning_params_dict here
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.
Thanks for sharing the code snippet. I will make this change and commit.