实例化¶
- torchtune.config.instantiate(config: DictConfig, *args: Any, **kwargs: Any) Any[source]¶
给定一个带有指定要实例化的对象的_component_字段和用于关键字参数的附加字段的DictConfig,创建指定对象的实例。您可以使用此函数根据配置中的规范创建您希望在配方中使用的torchtune对象的精确实例。
此函数还支持在函数调用中传入位置参数和关键字参数。这些参数会自动与提供的配置合并,关键字参数优先。
基于Facebook Research的Hydra的instantiate工具: https://github.com/facebookresearch/hydra/blob/main/hydra/_internal/instantiate/_instantiate2.py#L148
- Parameters:
config (DictConfig) – 从yaml文件解析的OmegaConf对象中的一个字段。 预计该字段将包含一个_component_字段,用于指定要实例化的对象的路径。
*args (Any) – 传递给对象以实例化的位置参数。
**kwargs (Any) – 传递给对象以实例化的关键字参数。
示例
>>> 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:
实例化的对象。
- Return type:
任何
- Raises:
ValueError – 如果配置不是DictConfig。