o
    ̳iZ                     @   sr   d dl Z d dlZd dlmZ d dlmZmZ d dlZd dlm	Z	 d dl
mZmZ eejjjZG dd de	ZdS )    N)Path)ListOptional)
Subcommand)Configget_all_recipesc                       sz   e Zd ZdZdejf fddZdee fddZ	dede
e fd	d
ZdededdfddZdejddfddZ  ZS )Catz2Holds all the logic for the `tune cat` subcommand.
subparsersc                    sb   t    |jddddtdtjd| _| jjdt	dd | jj
| jd	 | jjd
ddd d S )Ncatztune catz`Pretty print a config, making it easy to know which parameters you can override with `tune run`.an                  examples:
                    $ tune cat llama2/7B_full
                    output_dir: /tmp/torchtune/llama2_7B/full
                    tokenizer:
                        _component_: torchtune.models.llama2.llama2_tokenizer
                        path: /tmp/Llama-2-7b-hf/tokenizer.model
                        max_seq_len: null
                    ...

                    # Pretty print the config in sorted order
                    $ tune cat llama2/7B_full --sort

                    # Pretty print the contents of LOCALFILE.yaml
                    $ tune cat LOCALFILE.yaml

                You can now easily override a key based on your findings from `tune cat`:
                    $ tune run full_finetune_distributed --config llama2/7B_full output_dir=./

                Need to find all the "cat"-able configs? Try `tune ls`!
                )proghelpdescriptionepilogformatter_classconfig_namezName of the config to print)typer   )funcz--sort
store_truez Print the config in sorted order)actionr   )super__init__
add_parsertextwrapdedentargparseRawTextHelpFormatter_parseradd_argumentstrset_defaults_cat_cmd)selfr	   	__class__ F/home/ubuntu/.local/lib/python3.10/site-packages/torchtune/_cli/cat.pyr      s$   

zCat.__init__returnc                 C   s   dd t  D S )Nc                 S   s   g | ]}|j qS r$   )name).0reciper$   r$   r%   
<listcomp>@   s    z(Cat._get_all_recipes.<locals>.<listcomp>)r   )r!   r$   r$   r%   _get_all_recipes?   s   zCat._get_all_recipes
config_strc                 C   s2   t  D ]}|jD ]}|j|kr|    S qqd S )N)r   configsr'   )r!   r,   r)   configr$   r$   r%   _get_configB   s   


zCat._get_configfile	sort_keysNc              
   C   s   z9t |d)}t|}|r&ttj|d|dddddd W d    W d S W d    W d S 1 s2w   Y  W d S  tjyV } z| jd	|  W Y d }~d S d }~ww )
NrF   P   T)default_flow_styler1   indentwidthallow_unicode )endzError parsing YAML file: )openyaml	safe_loadprintdump	YAMLErrorr   error)r!   r0   r1   fdataer$   r$   r%   _print_yaml_fileI   s,   
& zCat._print_yaml_fileargsc                 C   s   |j }||  v rtd| d dS | |}|r$td d |j }nt|}|j dvr;| j	
d| d dS | sK| j	
d	| d
 dS | t||j dS )aP  Display the contents of a configuration file.

        Handles both predefined configurations and direct file paths, ensuring:
        - Input is not a recipe name
        - File exists
        - File is YAML format

        Args:
            args (argparse.Namespace): Command-line arguments containing 'config_name' attribute
        'z6' is a recipe, not a config. Please use a config name.Nrecipesr-   >   .yml.yamlzInvalid config format: 'z'. Must be YAML (.yaml/.yml)zConfig 'z' not found.)r   r+   r>   r/   ROOT	file_pathr   suffixlowerr   rA   existsrE   r   sort)r!   rF   r,   r.   config_pathr$   r$   r%   r    \   s&   


zCat._cat_cmd)__name__
__module____qualname____doc__r   _SubParsersActionr   r   r   r+   r   r   r/   boolrE   	Namespacer    __classcell__r$   r$   r"   r%   r      s    (r   )r   r   pathlibr   typingr   r   r<   torchtune._cli.subcommandr   torchtune._recipe_registryr   r   __file__parentrK   r   r$   r$   r$   r%   <module>   s   