Shortcuts

instantiate

torchtune.config.instantiate(config: DictConfig, *args: Any, **kwargs: Any) Any[source]

Given a DictConfig with a _component_ field specifying the object to instantiate and additional fields for keyword arguments, create an instance of the specified object. You can use this function to create the exact instance of a torchtune object you want to use in your recipe using the specification from the config.

This function also supports passing in positional args and keyword args within the function call. These are automatically merged with the provided config, with keyword args taking precedence.

Based on Hydra’s instantiate utility from Facebook Research: https://github.com/facebookresearch/hydra/blob/main/hydra/_internal/instantiate/_instantiate2.py#L148

Parameters:
  • config (DictConfig) – a single field in the OmegaConf object parsed from the yaml file. This is expected to have a _component_ field specifying the path of the object to instantiate.

  • *args (Any) – positional arguments to pass to the object to instantiate.

  • **kwargs (Any) – keyword arguments to pass to the object to instantiate.

Examples

>>> config.yaml:
>>>     model:
>>>       _component_: torchtune.models.llama2
>>>       num_layers: 32
>>>       num_heads: 32
>>>       num_kv_heads: 32
>>> from torchtune import config
>>> vocab_size = 32000
>>> # Pass in vocab size as positional argument. Since it is positioned first
>>> # in llama2(), it must be specified first. Pass in other arguments as kwargs.
>>> # This will return an nn.Module directly for llama2 with specified args.
>>> model = config.instantiate(parsed_yaml.model, vocab_size, max_seq_len=4096, embed_dim=4096)
Returns:

the instantiated object.

Return type:

Any

Raises:

ValueError – if config is not a DictConfig.

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources