Skip to content
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

Add hydra_defaults argument to builds #253

Closed
rsokl opened this issue Apr 4, 2022 · 0 comments · Fixed by #264
Closed

Add hydra_defaults argument to builds #253

rsokl opened this issue Apr 4, 2022 · 0 comments · Fixed by #264
Labels
enhancement New feature or request

Comments

@rsokl
Copy link
Contributor

rsokl commented Apr 4, 2022

Hydra supports specifying defaults on targeted configs:

from dataclasses import dataclass, field
from typing import Any

from hydra_zen import launch

import hydra
from hydra.core.config_store import ConfigStore


class MyClass:
    def __init__(self, **kwargs):
        self.kwargs = kwargs


Default = builds(int)

cs = ConfigStore.instance()
cs.store(group="x", name="a", node=Default)

@dataclass
class Conf:
    _target_: str = "__main__.MyClass"
    defaults: Any = field(default_factory=lambda: ["_self_", {"x": "a"}])
    x: Any = None
    
cs.store(name="my_app", node=Conf)  

@hydra.main(config_path=None, config_name="my_app")
def task_function(cfg):
    print(instantiate(cfg).kwargs)

Launching the task function sets the default and does not pass defaults to MyClass(...)

>>> launch(Conf, task_function);
{'x': 0}

Presently, builds can accommodate this via meta-field:

builds(MyClass, x=None, zen_meta=dict(defaults=["_self_", {"x": "a"}]))

But it might be nice to expose this as an explicit argument, hydra_defaults:

builds(MyClass, x=None, hydra_defaults=["_self_", {"x": "a"}])

The "pros" of adding hydra_defaults are:

  • It is explicit
  • It is a Hydra feature, and thus people might expect to see parity with hydra_convert and other hydra features.
  • It does not require users to understand meta-fields
  • The config-creation code and resulting yaml will be cleaner

The "cons" are:

  • It might be quite rare that people specify defaults on a targeted config
  • The meta-field workflow already works! Plus the meta-field is explicitly excluded, by hydra-zen, from all instantiation
@rsokl rsokl added the enhancement New feature or request label Apr 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant