Skip to content

Commit

Permalink
access hydra config through ${hydra:key}
Browse files Browse the repository at this point in the history
  • Loading branch information
omry committed May 27, 2020
1 parent 8257a2a commit 85ed46b
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 13 deletions.
2 changes: 1 addition & 1 deletion hydra/core/hydra_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def set_config(self, cfg: DictConfig) -> None:
assert cfg is not None
OmegaConf.set_readonly(cfg.hydra, True)
assert OmegaConf.get_type(cfg, "hydra") == HydraConf
self.cfg = cfg # type: ignore
self.cfg = OmegaConf.masked_copy(cfg, "hydra") # type: ignore

@staticmethod
def get() -> HydraConf:
Expand Down
16 changes: 9 additions & 7 deletions hydra/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,15 @@ def get_valid_filename(s: str) -> str:


def setup_globals() -> None:
try:
OmegaConf.register_resolver(
"now", lambda pattern: strftime(pattern, localtime())
)
except AssertionError:
# calling it again in no_workers mode will throw. safe to ignore.
pass
def register(name: str, f: Any):
try:
OmegaConf.register_resolver(name, f)
except AssertionError:
# calling it again in no_workers mode will throw. safe to ignore.
pass

register("now", lambda pattern: strftime(pattern, localtime()))
register("hydra", lambda path: OmegaConf.select(HydraConfig.get(), path))


@dataclass
Expand Down
3 changes: 3 additions & 0 deletions hydra/test_utils/configs/accessing_hydra_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cwd: ${hydra:runtime.cwd}
job_name: ${hydra:job.name}
config_name: ${hydra:job.config_name}
1 change: 1 addition & 0 deletions news/325.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hydra config can now be accessed through interpolation using ${hydra:key}, for example ${hydra:job.name}
24 changes: 24 additions & 0 deletions tests/test_core_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
from omegaconf import OmegaConf, open_dict
from typing import Any

from hydra._internal.config_loader_impl import ConfigLoaderImpl
from hydra._internal.utils import create_config_search_path
from hydra.core import utils
from hydra.core.hydra_config import HydraConfig


def test_foo(restore_singletons: Any) -> Any:
utils.setup_globals()

config_loader = ConfigLoaderImpl(
config_search_path=create_config_search_path("pkg://hydra.test_utils.configs")
)
cfg = config_loader.load_configuration(
config_name="accessing_hydra_config", overrides=[]
)
HydraConfig.instance().set_config(cfg)
with open_dict(cfg):
del cfg["hydra"]
assert cfg.job_name == "UNKNOWN_NAME"
assert cfg.config_name == "accessing_hydra_config"
20 changes: 16 additions & 4 deletions website/docs/configure_hydra/Intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ title: Overview
sidebar_label: Introduction
---

Many things in Hydra can be customized, this includes:
Many things in Hydra can be customized. This includes:
* Launcher configurations
* Sweeper configuration
* Logging configuration
* Run and Multirun output directory patterns
* Application help (--help and --hydra-help)
Expand Down Expand Up @@ -43,14 +45,24 @@ hydra:
```

## Runtime variables
The following variables are populated at runtime.
You can access them as config variables.
The `hydra` package is deleted from your config when the function runs to reduce the amount of noise
in the config passed to the function.
You can still access all config nodes in Hydra through the custom resolver `hydra`.

For example:
```yaml
config_name: ${hydra:job.config_name}
```
Pay close attention to the syntax: The resolver name is `hydra`, and the `key` is passed after the colon.

The following variables are some of the variables populated at runtime.
You can see the full Hydra config using `--cfg hydra`:

`hydra.job`:
- *hydra.job.name* : Job name, defaults to python file name without suffix. can be overridden.
- *hydra.job.override_dirname* : Pathname derived from the overrides for this job
- *hydra.job.num* : job serial number in sweep
- *hydra.job.id* : Job ID in the underlying jobs system (Slurm etc)
- *hydra.job.id* : Job ID in the underlying jobs system (SLURM etc)

`hydra.runtime`:
- *hydra.runtime.version*: Hydra's version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Learn more about packages directive [here](/advanced/package_directive.md).

### Using config groups
Since we moved all the configs into the `conf` directory, we need to tell Hydra where to find them using the `config_path` parameter.
`config_path` is a directory relative to `my_app.py`.
**`config_path` is a directory relative to `my_app.py`**.
```python title="my_app.py" {1}
@hydra.main(config_path="conf")
def my_app(cfg: DictConfig) -> None:
Expand Down

0 comments on commit 85ed46b

Please sign in to comment.