Replies: 3 comments 19 replies
-
Aside from What do you all think about an from hydra_zen import builds, instantiate, to_yaml
from omegaconf import OmegaConf
class Model:
interpolations = {
'crop_height': '${dataset:crop_height}',
'crop_width': '${dataset:crop_width}'}
def __init__(self, crop_height: int = 224, crop_width: int = 224):
pass
# Basic Python (could be used elsewhere in broader library)
python_model = Model()
# Hydra script usage (with registered dummy interpolation)
ModelConf = builds(Model, **Model.interpolations)
OmegaConf.register_new_resolver('dataset', lambda _: 224)
print(to_yaml(ModelConf))
hydra_model = instantiate(ModelConf) |
Beta Was this translation helpful? Give feedback.
-
Similar to the interpolations thread above, I'm trying to figure out the best way to allow
Currently, the only option I can think of is from dataclasses import field
from hydra_zen import builds
from lib import MyClass
Config = builds(MyClass)
defaults_list = field(default_factory=lambda: getattr(MyClass, 'defaults', []))
Config.__dataclass_fields__.update({'defaults': defaults_list}) Although this has the same issue with cryptic behavior raised by @rsokl above |
Beta Was this translation helpful? Give feedback.
-
@rsokl I'm running into the "Config Defaults for Objects" issue you mentioned. I wanted to have a default model optimizer while being able to select alternates via CLI overrides. I tried making the problematic config group have a default of # Contents of demo.py
from dataclasses import dataclass, field
from functools import partial
from typing import Any, List
import hydra
from hydra.core.config_store import ConfigStore
from hydra_zen import builds
from omegaconf import OmegaConf, MISSING
from torch import optim
@dataclass
class Conf:
defaults: List[Any] = field(default_factory=lambda: [
{'model/optimizer': None}
])
model: Any = MISSING
class BaseModel:
def __init__(self, optimizer=partial(optim.Adam)):
pass
def register_configs():
AdadeltaConf = builds(optim.Adadelta, populate_full_signature=True, zen_partial=True)
cs = ConfigStore.instance()
cs.store('config', Conf)
cs.store(group='model', name='base', node=builds(BaseModel, populate_full_signature=True))
cs.store(group='model/optimizer', name='Adadelta', node=AdadeltaConf)
@hydra.main(config_name='config', config_path=None)
def main(cfg):
resolved_cfg = OmegaConf.to_container(cfg, resolve=True, enum_to_str=True)
print(OmegaConf.to_yaml(resolved_cfg))
if __name__ == '__main__':
register_configs()
main() Works fine without overriding the optimizer (minus the fact that the recursive $ python demo.py +model=base
model:
_target_: __main__.BaseModel
optimizer:
_target_: torch.optim.adam.Adam
_partial_: true But you can't pick an alternative optimizer $ python demo.py +model=base model/optimizer=Adadelta
In 'model/base': Validation error while composing config:
Merge error: PartialBuilds_Adam is not a subclass of PartialBuilds_Adadelta. value: {'_target_': 'torch.optim.adam.Adam', '_partial_': True}
full_key:
object_type=Conf |
Beta Was this translation helpful? Give feedback.
-
A list of "gotchyas":
Configuring "Interactive" Functions
Be careful configuring functions defined in interactive modes for reproducibility.
OmegaConf Interpolations
Be careful in setting OmegaConf interpolations as defaults.
Config Defaults for Objects
If you need to override "objects" in a config you must set those objects to
None
in your configuration.Submitit-Launcher and Environment
If you try to submit jobs from an interactive node on Slurm you should first remove the environment variables for Slurm since submitit copies the current environment.
Beta Was this translation helpful? Give feedback.
All reactions