
    PiR                         d dl Z d dlZd dlZd dlmZmZmZmZ d dlm	Z	m
Z
 d dlmZ d dlmZmZ dedef         deedf         d	eeef         d
efdZdeeef         ded
efdZde	ded	ed
efdZdS )    N)AnyCallableDictTuple)
DictConfig	OmegaConf)InstantiationError)_get_component_from_path_has_component_component_.argskwargsreturnc                      | |i |S )N )r   r   r   s      q/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torchtune/config/_instantiate.py_create_componentr      s    
 ;''''    nodec                     t          |           rQt          |                     d                    }d |                                 D             }t	          |||          S t          d          )z
    Creates the object specified in _component_ field with provided positional args
    and kwargs already merged. Raises an InstantiationError if _component_ is not specified.
    r   c                 &    i | ]\  }}|d k    ||S )r   r   ).0kvs      r   
<dictcomp>z%_instantiate_node.<locals>.<dictcomp>    s(    FFF41a13E3E!Q3E3E3Er   ziCannot instantiate specified object.
Make sure you've specified a _component_ field with a valid dotpath.)r   r
   getitemsr   r	   )r   r   r   r   s       r   _instantiate_noder      sr    
 d 
.txx/F/FGGFF4::<<FFF dF;;; W
 
 	
r   configc                 R   | dS t          j        |           st          dt          |                      t	          j                    t          j        vr0t          j                            t	          j                               t          j
        |           }|                    g dg d           |                    |                                            |} |rt          j        | |          } t          j        |            t!          t          j        |           g|R  S )aT  
    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

    Args:
        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:
        Any: the instantiated object.

    Raises:
        ValueError: if config is not a DictConfig.
    Nz+instantiate only supports DictConfigs, got )allow_objectsstructreadonly)TFF)flagsvalues)r   is_dict
ValueErrortypeosgetcwdsyspathappendcopydeepcopy	_set_flag_set_parent_get_parentmergeresolver   	to_object)r   r   r   config_copys       r   instantiater7   )   s!   ` ~tV$$ WUtF||UUVVV 
y{{#(""	$$$-''K555>R>R>R     F..00111F 100 fY088@4@@@@r   )r.   r)   r+   typingr   r   r   r   	omegaconfr   r   torchtune.config._errorsr	   torchtune.config._utilsr
   r   strr   r   r7   r   r   r   <module>r=      s`    				 



 - - - - - - - - - - - - + + + + + + + + 7 7 7 7 7 7 L L L L L L L L(#s(#(
S/( cN( 		( ( ( (
DcN 
3 
3 
 
 
 
 GAGAGA GA 		GA GA GA GA GA GAr   