Skip to content
durandtiboPublic

About

A library of custom OmegaConf resolvers

Topics

Resources

Code of conduct

Contributing

Stars

1 star

Watchers

1 watching

Forks

Repository files navigation

hya

CI Nightly Package Tests Codecov Documentation (stable)
PyPI version Python BSD-3-Clause
Ruff Doc style: google OpenSSF Scorecard
Downloads Monthly downloads GitHub stars

Overview

hya is a library of custom OmegaConf resolvers, designed to be used with Hydra. With hya, a configuration can compute values instead of repeating hand-computed ones:

  • arithmetic and math functions, such as hya.mul, hya.ceildiv or hya.sqrt;
  • paths and identifiers, with hya.path, hya.iter_join and hya.sha256;
  • NumPy arrays, PyTorch tensors and data types, and brace expansion, with optional dependencies;
  • your own resolvers, managed by a registry.

The only required dependency is omegaconf. An optional package is imported only when a resolver that needs it is called.

This page describes the main branch. For the latest release, see the stable documentation. In 0.4.0, importing hya does not register the resolvers: call hya.get_default_registry().register_resolvers() first.

Quick start

Install hya:

pip install hya

Importing hya registers the resolvers, so a configuration can call them with the syntax ${key:argument,...}:

>>> import hya
>>> from omegaconf import OmegaConf
>>> conf = OmegaConf.create(
...     {
...         "num_samples": 1000,
...         "batch_size": 32,
...         "num_batches": "${hya.ceildiv:${num_samples},${batch_size}}",
...         "learning_rate": "${hya.pow:10,-3}",
...         "run_name": "${hya.iter_join:[mnist,${batch_size}],-}",
...     }
... )
>>> conf.num_batches
32
>>> conf.learning_rate
0.001
>>> conf.run_name
'mnist-32'

Using with Hydra

Install Hydra with pip install hydra-core, and add import hya to your application. Given the file config.yaml:

dataset:
  train_samples: 60000

training:
  batch_size: 64
  steps_per_epoch: ${hya.ceildiv:${dataset.train_samples},${training.batch_size}}

the following script prints 938:

import hya
import hydra
from omegaconf import DictConfig


@hydra.main(version_base=None, config_path=".", config_name="config")
def main(cfg: DictConfig):
    print(cfg.training.steps_per_epoch)


if __name__ == "__main__":
    main()

With hydra-core 1.3.8, use Python 3.11 to 3.13: on Python 3.14, Hydra stops with ValueError: badly formed help string. See Using with Hydra.

Custom resolvers

Add a resolver to the default registry, then publish it to OmegaConf:

>>> from hya import get_default_registry
>>> registry = get_default_registry()
>>> @registry.register("multiply")
... def multiply_resolver(x, y):
...     return x * y
>>> registry.register_resolvers()
>>> OmegaConf.create({"result": "${multiply:5,3}"}).result
15

See Custom Resolvers to override a resolver, load it lazily or build your own registry.

Available resolvers

Category Resolvers
Arithmetic hya.add, hya.sub, hya.mul, hya.truediv, hya.floordiv, hya.ceildiv, hya.neg
Math functions hya.pow, hya.sqrt, hya.exp, hya.log, hya.log10, hya.sinh, hya.asinh
Comparison hya.max, hya.min
Casts hya.int, hya.float
Collections hya.len, hya.sum
Constants hya.pi
Paths hya.path, hya.iter_join
Hashing hya.sha256
Optional hya.braceexpand (braceexpand), hya.np.array (NumPy), hya.torch.tensor, hya.torch.dtype (PyTorch)

The resolvers page documents each one.

Installation

We highly recommend installing a virtual environment. hya can be installed from pip using the following command:

pip install hya

To make the package as slim as possible, only the minimal packages required to use hya are installed. To include all the packages, you can use the following command:

pip install 'hya[all]'

A single extra can be installed too: braceexpand, numpy, torch (Linux and macOS with Apple Silicon) or testing (the pytest marks of hya.testing), for example pip install 'hya[numpy]'.

Please check the get started page to see how to install only some specific packages or other alternatives to install the library. The following is the corresponding hya versions and tested dependencies.

hya omegaconf braceexpand* numpy* torch* python
main >=2.2,<3.0 >=0.1.7,<0.2.0 >=2.0,<3.0 >=2.4,<3.0 >=3.11
0.4.0 >=2.2,<3.0 >=0.1.7,<0.2.0 >=1.24,<3.0 >=2.0,<3.0 >=3.10
0.3.1 >=2.2,<3.0 >=0.1.7,<0.2.0 >=1.24,<3.0 >=2.0,<3.0 >=3.10,<3.15
0.3.0 >=2.2,<3.0 >=0.1.7,<0.2.0 >=1.24,<3.0 >=2.0,<3.0 >=3.9,<3.14
0.2.4 >=2.2,<3.0 >=0.1.7,<0.2.0 >=1.21,<3.0 >=1.11,<3.0 >=3.9,<3.14
0.2.3 >=2.1,<3.0 >=0.1.7,<0.2.0 >=1.21,<2.0 >=1.10,<3.0 >=3.9,<3.13
0.2.2 >=2.1,<3.0 >=0.1.7,<0.2.0 >=1.21,<2.0 >=1.10,<3.0 >=3.9,<3.13
0.2.1 >=2.1,<3.0 >=0.1.7,<0.2.0 >=1.21,<2.0 >=1.10,<3.0 >=3.9,<3.13
0.2.0 >=2.1,<3.0 >=0.1.7,<0.2.0 >=1.21,<2.0 >=1.10,<3.0 >=3.9,<3.13

* indicates an optional dependency

older versions
hya omegaconf braceexpand* torch* python
0.1.3 >=2.1,<3.0 >=0.1.7,<0.2.0 >=1.10,<2.2 >=3.9,<3.13
0.1.2 >=2.1,<3.0 >=0.1.7,<0.2.0 >=1.10,<2.2 >=3.9,<3.13
0.1.1 >=2.1,<3.0 >=0.1.7,<0.2.0 >=1.10,<2.2 >=3.9,<3.12
0.1.0 >=2.1,<3.0 >=0.1.7,<0.2.0 >=1.10,<2.2 >=3.9,<3.12
0.0.14 >=2.1,<3.0 >=0.1.7,<0.2.0 >=1.10,<2.2 >=3.9,<3.12
0.0.13 >=2.1,<3.0 >=1.10,<2.1 >=3.9,<3.12
0.0.12 >=2.1,<3.0 >=1.10,<2.1 >=3.9,<3.12

Contributing

Please check the instructions in CONTRIBUTING.md.

API stability

⚠️ While hya is in development stage, no API is guaranteed to be stable from one release to the next. In fact, it is very likely that the API will change multiple times before a stable 1.0.0 release. In practice, this means that upgrading hya to a new version will possibly break any code that was using the old version of hya.

License

hya is licensed under BSD 3-Clause "New" or "Revised" license available in LICENSE file.

About

A library of custom OmegaConf resolvers

Topics

Resources

Code of conduct

Contributing

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages