
    PiS                         d dl Z d dlZd dlZd dl mZ d dlmZmZmZmZ d dl	m
Z
mZ d dlmZ ee
gef         Z G d de j                  Zded	ed
ef         fdZdS )    N)	Namespace)AnyCallableListTuple)
DictConfig	OmegaConf)_merge_yaml_and_cli_argsc                   P     e Zd ZdZd fdZdeeee         f         f fdZ	 xZ
S )TuneRecipeArgumentParsera  
    A helpful utility subclass of the ``argparse.ArgumentParser`` that
    adds a builtin argument "config". The config argument takes a file path to a YAML file
    and loads in argument defaults from said file. The YAML file must only contain
    argument names and their values and nothing more, it does not have to include all of the
    arguments. These values will be treated as defaults and can still be overridden from the
    command line. Everything else works the same as the base ArgumentParser and you should
    consult the docs for more info: https://docs.python.org/3/library/argparse.html.

    Note:
        This class uses "config" as a builtin argument so it is not available to use.
    returnNc                      t                      j        |i | t                                          dt          dd           d S )Nz--configz)Path/name of a yaml file with recipe argsT)typehelprequired)super__init__add_argumentstr)selfargskwargs	__class__s      k/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torchtune/config/_parse.pyr   z!TuneRecipeArgumentParser.__init__"   sV    $)&)))<	 	 	
 	
 	
 	
 	
    c                 R    t                      j        |i |\  }}d |D             }|rt          d| d          t          j        |j                  }d|vs
J d             | j        di t          j        |d            t                      j        |i |\  }}|`||fS )	a  This acts the same as the base parse_known_args but will first load in defaults from
        from the config yaml file if it is provided. The command line args will always take
        precident over the values in the config file. All other parsing method, such as parse_args,
        internally call this method so they will inherit this property too. For more info see
        the docs for the base method: https://docs.python.org/3/library/argparse.html#the-parse-args-method.
        c                 <    g | ]}|                     d           |S )z--)
startswith).0args     r   
<listcomp>z=TuneRecipeArgumentParser.parse_known_args.<locals>.<listcomp>4   s)    QQQSCNN4<P<PQSQQQr   z)Additional flag arguments not supported: z,. Please use --config or key=value overridesconfigz(Cannot use 'config' within a config fileF)resolve )r   parse_known_args
ValueErrorr	   loadr"   set_defaultsto_container)r   r   r   	namespaceunknown_argsunknown_flag_argsr"   r   s          r   r%   z)TuneRecipeArgumentParser.parse_known_args+   s     #;%''":D"KF"K"K	<QQLQQQ 	{<M{{{   	 011v%%%'Q%%%JJI265IIIJJJ":%''":D"KF"K"K	<,&&r   )r   N)__name__
__module____qualname____doc__r   r   r   r   r   r%   __classcell__)r   s   @r   r   r      sz         
 
 
 
 
 
'5DI9M3N ' ' ' ' ' ' ' ' ' 'r   r   recipe_mainr   .c                 r     t          j                   dt          dt          dt          f fd            }|S )a  
    Decorator that handles parsing the config file and CLI overrides
    for a recipe. Use it on the recipe's main function.

    Args:
        recipe_main (Recipe): The main method that initializes
            and runs the recipe

    Examples:
        >>> @parse
        >>> def main(cfg: DictConfig):
        >>>     ...

        >>> # With the decorator, the parameters will be parsed into cfg when run as:
        >>> tune my_recipe --config config.yaml foo=bar

    Returns:
        Callable[..., Any]: the decorated main
    r   r   r   c                      t          j        t          j                  }|                                \  }}t          ||          }t          j         |                     d S )N)descriptionformatter_class)r   r0   argparseRawDescriptionHelpFormatterr%   r
   sysexit)r   r   parser	yaml_argscli_argsconfr2   s         r   wrapperzparse.<locals>.wrapperY   sj    )#+$@
 
 

 %5577	8'	8<<T""#####r   )	functoolswrapsr   )r2   r?   s   ` r   parserB   D   sT    * _[!!	$s 	$c 	$c 	$ 	$ 	$ 	$ 	$ "!	$ Nr   )r7   r@   r9   r   typingr   r   r   r   	omegaconfr   r	   torchtune.config._utilsr
   RecipeArgumentParserr   rB   r$   r   r   <module>rH      s         



       - - - - - - - - - - - - + + + + + + + + < < < < < < 
:,#	$-' -' -' -' -'x6 -' -' -'`!v !(38"4 ! ! ! ! ! !r   