Skip to main content

Hydra-Once

A simple library for use with Hydra or OmegaConf configurations. The key feature is the _once_ keyword, which enables objects marked with it to be instantiated as singletons.

Motivation

Sometimes, you want to ensure a subtree of your config is instantiated only once. Making unnecessary copies can waste resources or even break code.

Example:

dataset:
  _target_: lightly.data.LightlyDataset
  root: data_dir
loader:
  _target_: torch.utils.data.DataLoader
  dataset: ${..dataset}  
  sampler:
    _target_: torch.utils.data.RandomSampler
    data_source: ${..dataset}

Instantiating this config with Hydra will create three separate instances of dataset, which is undesirable because each one takes up memory. Even worse, calling instantiate a second time will create three more instances!

Solution

Hydra-Once introduces a new keyword, _once_, to the config. This ensures the subtree is instantiated only once.

dataset:
  _target_: lightly.data.LightlyDataset
  _once_: true  # Only instantiate this tree once, reusing it as needed.
  # Optionally, set _once_ to a string to manually specify a cache key
  # _once_: dataset_unique_key
  root: data_dir
loader:
  _target_: torch.utils.data.DataLoader
  dataset: ${..dataset}  
  sampler:
    _target_: torch.utils.data.RandomSampler
    data_source: ${..dataset}

Now, instantiate your config like so:

from hydra_once import instantiate

x = instantiate(cfg)  # creates dataset just one time
y = instantiate(cfg)  # creates dataset one more time

This will create the dataset and loader, but only instantiate the dataset once. By default, the cache is ephemeral and destroyed at the end of the call to instantiate.

Persistent Caching

To use a persistent cache, set the cache argument to a dictionary you reuse, or to True to use a global cache:

from hydra_once import instantiate

cache = {}  # or use True for a global cache

x = instantiate(cfg, cache=cache)  # creates dataset one time
y = instantiate(cfg, cache=cache)  # reuses dataset, but recreates loader

To clear the global cache, use the clear function:

from hydra_once import clear
clear()  # clears the global cache

Note:
It is best to use the default ephemeral cache or a dictionary you control. Using the global cache can lead to hard-to-debug issues, especially in multi-threaded code.

Hydra Compatibility

All Hydra instantiate tests pass without modification using this library. Just replace the Hydra instantiate function with the one from this library:

from hydra_once import instantiate

Installation

pip install git+https://github.com/swamidass/hydra-once

This is a Python-only library. The only dependencies are Hydra and OmegaConf.

Let me know if you want any further customization or additional sections! PRs welcome.

Metadata

Release files for hydra-once 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hydra-once 0.1.0
File Size Uploaded
hydra_once-0.1.0.tar.gz 7.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for hydra-once 0.1.0
File Interpreter ABI Platform
hydra_once-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.4 kB

Release files / hydra_once-0.1.0.tar.gz

Download URL hydra_once-0.1.0.tar.gz
Size 7.8 kB
Tags Source
SHA-256 checksum
How to use checksums
722dacf3a8bc4150b7e1cb4eec0b07c8b354e49fd5602e57ef44bfd5c1bede6b
BLAKE2b-256 checksum
How to use checksums
b7596752125421124ca22ccdc1dcd4633d677572b9e486e6ee01ecfd6689c505
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.9

Release files / hydra_once-0.1.0-py3-none-any.whl

Download URL hydra_once-0.1.0-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
87ba9ee869801a2b63a74cd244b459b69565e2cd875737f24c5d1b90faeb2320
BLAKE2b-256 checksum
How to use checksums
e6b06224039901893f59979c31a73d2c03292c006b59d338d2ecc655b71a3383
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.9

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page