o
    0i                  
   @   sf   d Z ddlZddlmZ dee defddZdee fdd	Z	dd
edededee fddZ	dS )aV  
A collection of utilities for comparing `examples/complete_*_example.py` scripts with the capabilities inside of each
`examples/by_feature` example. `compare_against_test` is the main function that should be used when testing, while the
others are used to either get the code that matters, or to preprocess them (such as stripping comments)
    N)Optionallinesnamec                 C   s   |dkr|dkrt d| dg d}}| D ]2}|s*d| |v r*d}|| q|rI|dkr8d|v r8|  S |dkrDd	|v rD|  S || qd
S )a.  
    Extracts a function from `lines` of segmented source code with the name `name`.

    Args:
        lines (`List[str]`):
            Source code of a script separated by line.
        name (`str`):
            The name of the function to extract. Should be either `training_function` or `main`
    training_functionmainz Incorrect function name passed: z-, choose either 'main' or 'training_function'Fzdef Tzdef mainzif __name__N)
ValueErrorappend)r   r   
good_linesfound_startline r   `/sda-disk/www/egybert/egybert_env/lib/python3.10/site-packages/accelerate/test_utils/examples.pyget_function_contents_by_name   s    



r   c                 C   s   dd | D S )z
    Filters `lines` and removes any entries that start with a comment ('#') or is just a newline ('
')

    Args:
        lines (`List[str]`):
            Source code of a script separated by line.
    c                 S   s&   g | ]}|  d s|dkr|qS )#
)lstrip
startswith.0r   r   r   r   
<listcomp><   s   & zclean_lines.<locals>.<listcomp>r   )r   r   r   r   clean_lines4   s   r   base_filenamefeature_filenameparser_onlysecondary_filenamec                    sr  t | }| }W d   n1 sw   Y  t tjtjdd}| }W d   n1 s4w   Y  t |}| }W d   n1 sLw   Y  |durmt |}| }W d   n1 shw   Y  |rtt|d}	tt|d}
tt|d}|durtt|dn tt|d}	tt|d}
tt|d}|durtt|dd}g }g }t|}t	t
|d D ]'}||vrt|}||
vr| |krd|vr|| || qt|}qg g }t|	D ]$\}}||vr||
vr| |krd|vr| || qfd	d
|D }|dur7fdd
|D   fdd
|D }|S )a  
    Tests whether the additional code inside of `feature_filename` was implemented in `base_filename`. This should be
    used when testing to see if `complete_*_.py` examples have all of the implementations from each of the
    `examples/by_feature/*` scripts.

    It utilizes `nlp_example.py` to extract out all of the repeated training code, so that only the new additional code
    is examined and checked. If something *other* than `nlp_example.py` should be used, such as `cv_example.py` for the
    `complete_cv_example.py` script, it should be passed in for the `secondary_filename` parameter.

    Args:
        base_filename (`str` or `os.PathLike`):
            The filepath of a single "complete" example script to test, such as `examples/complete_cv_example.py`
        feature_filename (`str` or `os.PathLike`):
            The filepath of a single feature example script. The contents of this script are checked to see if they
            exist in `base_filename`
        parser_only (`bool`):
            Whether to compare only the `main()` sections in both files, or to compare the contents of
            `training_loop()`
        secondary_filename (`str`, *optional*):
            A potential secondary filepath that should be included in the check. This function extracts the base
            functionalities off of "examples/nlp_example.py", so if `base_filename` is a script other than
            `complete_nlp_example.py`, the template script should be included here. Such as `examples/cv_example.py`
    Nexamplesznlp_example.pyr   r   zMtrain_dataloader, eval_dataloader = get_dataloaders(accelerator, batch_size)
   TESTING_MOCKED_DATALOADERSc                       g | ]}| vr|qS r   r   r   )new_full_example_partsr   r   r          z(compare_against_test.<locals>.<listcomp>c                    r   r   r   r   )secondary_file_funcr   r   r      r    c                    r   r   r   r   )diff_from_twor   r   r      r    )open	readlinesospathabspathjoinr   r   iterrangelennextr   r   	enumerate)r   r   r   r   fbase_file_contentsfull_file_contentsfeature_file_contentssecondary_file_contentsbase_file_funcfull_file_funcfeature_file_func_dl_linenew_feature_codepassed_idxsitir   _diff_from_exampler   )r"   r   r!   r   compare_against_test?   sl   












r=   )N)
__doc__r%   typingr   liststrr   r   boolr=   r   r   r   r   <module>   s   