instantiate
optimus_dl.core.instantiate
¶
Utility functions for configuration instantiation.
These functions are intended to be used as _target_s in OmegaConf/Hydra configs
to provide structural and logical operations during object instantiation.
compose(*functions)
¶
Compose multiple callables into a single pipeline.
The functions are applied in the order they are provided (left-to-right). i.e., compose(f, g)(x) == g(f(x)).
This is highly useful for data transformation pipelines.
Usage in config
transform: target: optimus_dl.core.instantiate.compose args: - target: optimus_dl.data.transforms.Tokenize - target: optimus_dl.data.transforms.Chunk
Source code in optimus_dl/core/instantiate.py
concat_lists(*lists)
¶
Concatenate multiple lists into a single flat list.
Usage in config
all_metrics: target: optimus_dl.core.instantiate.concat_lists args: - ${metrics.train} - ${metrics.val}
Source code in optimus_dl/core/instantiate.py
cond(condition, true_val, false_val)
¶
Conditional instantiation.
Because optimus_dl uses lazy instantiation, the branch that is not selected will not be instantiated or evaluated, making this a powerful tool for switching out heavy sub-modules like layers or optimizers.
Usage in config
layer: target: optimus_dl.core.instantiate.cond condition: ${use_flash_attn} true_val: target: optimus_dl.modules.FlashAttention false_val: target: optimus_dl.modules.StandardAttention
Source code in optimus_dl/core/instantiate.py
get_item(key, mapping, default=None)
¶
Retrieve an item from a mapping based on a key.
Acts as a dynamic switch/router. If the key is not found and no default is provided, it raises a KeyError.
Usage in config
optimizer: target: optimus_dl.core.instantiate.get_item key: ${args.optim_type} mapping: adamw: target: torch.optim.AdamW sgd: target: torch.optim.SGD
Source code in optimus_dl/core/instantiate.py
merge_dicts(*dicts)
¶
Deep-merge multiple dictionaries into a single dictionary. Later dictionaries in the arguments override earlier ones.
Usage in config
my_kwargs: target: optimus_dl.core.instantiate.merge_dicts args: - ${defaults.kwargs} - {specific_override: 42}
Source code in optimus_dl/core/instantiate.py
repeat(times, item)
¶
Create a list by repeating an item a specific number of times.
If the item is a dictionary or an OmegaConf config (which often represents an object to be instantiated), it will be deep-copied so that each repeated element is an independent instance.
Usage in config
layers: target: optimus_dl.core.instantiate.repeat times: ${model.n_layers} item: target: optimus_dl.modules.TransformerLayer