
    gq              	       l    d Z ddlZddlmZ dee         defdZdee         fdZdded	ed
edef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                     |dk    r|dk    rt          d| d          g d}}| D ]V}|sd| |v rd}|                    |           #|r1|dk    rd|v r|c S |dk    rd	|v r|c S |                    |           Wd
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   
good_linesfound_startlines        Z/var/www/html/ai-engine/env/lib/python3.11/site-packages/accelerate/test_utils/examples.pyget_function_contents_by_namer      s     """tv~~oDoooppp %J 
$ 
$ 	}d}}44Kd### 	$***zT/A/A!!!!v~~-4"7"7!!!!d###
$ 
$    c                     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                 l    g | ]1}|                                                     d           )|dk    /|2S )#
)lstrip
startswith).0r   s     r   
<listcomp>zclean_lines.<locals>.<listcomp><   s9    YYYT$++--*B*B3*G*GYDTXLLDLLLr    )r   s    r   clean_linesr   4   s     ZYUYYYYr   base_filenamefeature_filenameparser_onlysecondary_filenamec                    t          |           5 }|                                }ddd           n# 1 swxY w Y   t          t          j                            t          j                            dd                              5 }|                                }ddd           n# 1 swxY w Y   t          |          5 }|                                }ddd           n# 1 swxY w Y   |;t          |          5 }|                                }ddd           n# 1 swxY w Y   |rwt          t          |d                    }	t          t          |d                    }
t          t          |d                    }|t          t          |d                    nvt          t          |d                    }	t          t          |d                    }
t          t          |d                    }|t          t          |d                    d}g }g }t          |          }t          t          |          dz
            D ]o}||vrit          |          }||
vrV|                                |k    r>d|vr+|                    |           |                    |           `t          |          }pg g }t          |	          D ]S\  }}||vrJ||
vrF|                                |k    r.d|vr*                    |           |                    |           Tfd	|D             }|fd
|D             f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|	S r   r   )r   r   new_full_example_partss     r   r   z(compare_against_test.<locals>.<listcomp>   s$    aaa$dJ`>`>`>`>`>`r   c                     g | ]}|v|	S r   r   )r   r   secondary_file_funcs     r   r   z(compare_against_test.<locals>.<listcomp>   s$    ```$L_@_@_@_@_@_r   c                     g | ]}|v|	S r   r   )r   r   diff_from_twos     r   r   z(compare_against_test.<locals>.<listcomp>   s#    ]]]d4}C\C\TC\C\C\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&   s                       @@@r   compare_against_testrC   ?   sw   0 
m		 +[[]]+ + + + + + + + + + + + + + +	bgoobgll:7GHHII	J	J +a[[]]+ + + + + + + + + + + + + + +			 .1 !. . . . . . . . . . . . . . .%$%% 	4&'kkmm#	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4  $%BCUW]%^%^__$%BCUW]%^%^__'(EF[]c(d(dee)"-.KLcek.l.l"m"m$%BCUWj%k%kll$%BCUWj%k%kll'(EF[]p(q(qrr)"--.EGZ[[# # `H K		 	 B3())A-.. 	! 	!K88DN**(1J1J/t;;$++D111&&q)))) RA  K^,, * *4KN**(1J1J/t;;*11$777&&q))) baaa*:aaa%````*<```]]]].?]]]sB   488	B**B.1B.C%%C),C)D""D&)D&)N)	__doc__r+   typingr   strr   r   boolrC   r   r   r   <module>rH      s      
			      $c $# $ $ $ $4ZtCy Z Z Z ZS S Ss SQU Skn S S S S S Sr   