a
    d                     @   sX   d Z ddlZddlmZ ee edddZee ddd	Zdeeeed
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)List)linesnamec                 C   s   |dkr |dkr t d| dg d }}| D ]d}|sTd| |v rTd}|| q.|r.|dkrpd|v rp|  S |dkrd	|v r|  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 seperated 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   Z
good_linesZfound_startline r
   g/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/accelerate/test_utils/examples.pyget_function_contents_by_name   s    


r   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 seperated by line.
    c                 S   s&   g | ]}|  d s|dkr|qS )#
)lstrip
startswith.0r	   r
   r
   r   
<listcomp><       zclean_lines.<locals>.<listcomp>r
   r   r
   r
   r   clean_lines4   s    r   )base_filenamefeature_filenameparser_onlysecondary_filenamec                    s  t | d}| }W d   n1 s(0    Y  t tjtjddd}| }W d   n1 sl0    Y  t |d}| }W d   n1 s0    Y  |durt |d}| }W d   n1 s0    Y  |r,tt|d}	tt|d}
tt|d}|durntt|dnBtt|d}	tt|d}
tt|d}|durntt|dd}g }g }t|}t	t
|d D ]X}||vrt|}||
vr| |krd	|vr|| || nt|}qg g }t|	D ]J\}}||vr||
vr| |krd	|vr| || qfd
d|D }|dur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`
    rNZexamplesznlp_example.pyr   r   zMtrain_dataloader, eval_dataloader = get_dataloaders(accelerator, batch_size)
   ZTESTING_MOCKED_DATALOADERSc                    s   g | ]}| vr|qS r
   r
   r   )new_full_example_partsr
   r   r      r   z(compare_against_test.<locals>.<listcomp>c                    s   g | ]}| vr|qS r
   r
   r   )secondary_file_funcr
   r   r      r   c                    s   g | ]}| vr|qS 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   fZbase_file_contentsZfull_file_contentsZfeature_file_contentsZsecondary_file_contentsZbase_file_funcZfull_file_funcZfeature_file_funcZ_dl_lineZnew_feature_codeZpassed_idxsitir	   _Zdiff_from_exampler
   )r   r   r   r   compare_against_test?   s^    &&&&








r/   )N)	__doc__r"   typingr   strr   r   boolr/   r
   r
   r
   r   <module>   s
   