Skip to content

Commit 676b28d

Browse files
authored
Fix Hydra integration example AttributeError (#2822)
1 parent 9e9e145 commit 676b28d

1 file changed

Lines changed: 21 additions & 7 deletions

File tree

models/integrations/hydra.mdx

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,32 @@ def run_experiment(cfg):
2424

2525
## Track hyperparameters
2626

27-
Hydra uses [omegaconf](https://omegaconf.readthedocs.io/en/2.1_branch/) as the default way to interface with configuration dictionaries. `OmegaConf`'s dictionary are not a subclass of primitive dictionaries so directly passing Hydra's `Config` to `wandb.Run.config` leads to unexpected results on the dashboard. It's necessary to convert `omegaconf.DictConfig` to the primitive `dict` type before passing to `wandb.Run.config`.
27+
Hydra uses [OmegaConf](https://omegaconf.readthedocs.io/en/2.1_branch/) as the default interface to handle configuration dictionaries. OmegaConf config objects (for example, `omegaconf.DictConfig`) are not plain Python `dict` instances.
28+
29+
`wandb.Run.config` is a read-only property, which means that `wandb.Run.config = ...` raises an `AttributeError` if you try to pass an OmegaConf config object.
30+
31+
Convert `cfg` to a plain `dict` with `OmegaConf.to_container()` and pass it to `wandb.init(config=...)` (or call `wandb.Run.config.update(...)`).
2832

2933
```python
30-
@hydra.main(config_path="configs/", config_name="defaults")
34+
import hydra
35+
import omegaconf
36+
import wandb
37+
38+
39+
@hydra.main(version_base=None, config_path="configs/", config_name="defaults")
3140
def run_experiment(cfg):
32-
with wandb.init(entity=cfg.wandb.entity, project=cfg.wandb.project) as run:
33-
run.config = omegaconf.OmegaConf.to_container(
41+
cfg_dict = omegaconf.OmegaConf.to_container(
3442
cfg, resolve=True, throw_on_missing=True
3543
)
36-
run = wandb.init(entity=cfg.wandb.entity, project=cfg.wandb.project)
37-
run.log({"loss": loss})
38-
model = Model(**run.config.model.configs)
44+
# Optional: avoid logging W&B metadata (like entity/project) as part of the run config
45+
cfg_dict.pop("wandb", None)
46+
with wandb.init(
47+
entity=cfg.wandb.entity,
48+
project=cfg.wandb.project,
49+
config=cfg_dict,
50+
) as run:
51+
run.log({"loss": loss})
52+
model = Model(**run.config["model"]["configs"])
3953
```
4054

4155
## Troubleshoot multiprocessing

0 commit comments

Comments
 (0)