
    /hY              	       Z    S r SSKrS\\   S\4S jrS\\   4S jrSS\S\S	\S
\4S jjrg)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linesnamec                    US:w  a  US:w  a  [        SU S35      e/ Sp2U  Hb  nU(       d  SU 3U;   a  SnUR                  U5        M(  U(       d  M1  US:X  a
  SU;   a  Us  $ US:X  a
  S	U;   a  Us  $ UR                  U5        Md     g
)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lines        X/home/james-whalen/.local/lib/python3.13/site-packages/accelerate/test_utils/examples.pyget_function_contents_by_namer      s     ""tv~;D6Anopp %dV}4Kd#;**zT/A!!v~-4"7!!d#     c                     U  Vs/ s H2  oR                  5       R                  S5      (       a  M(  US:w  d  M0  UPM4     sn$ s  snf )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.
    #
)lstrip
startswith)r   r   s     r   clean_linesr   3   s6     #YUT++-*B*B3*GDDTXLDUYYYs   'AAAbase_filenamefeature_filenameparser_onlysecondary_filenamec                 Z   [        U 5       nUR                  5       nSSS5        [        [        R                  R	                  [        R                  R                  SS5      5      5       nUR                  5       nSSS5        [        U5       nUR                  5       nSSS5        Ub$  [        U5       nUR                  5       nSSS5        U(       aX  [        [        WS5      5      n	[        [        WS5      5      n
[        [        WS5      5      nUb  [        [        WS5      5      nOW[        [        WS5      5      n	[        [        WS5      5      n
[        [        WS5      5      nUb  [        [        WS5      5      nSn/ n/ n[        U5      n[        [        U5      S-
  5       Hi  nUU;  d  M  [        U5      nUU
;  d  M  UR                  5       U:w  d  M4  SU;  a$  UR                  U5        UR                  U5        M^  [        U5      nMk     / n/ n[        U	5       HV  u  nnUU;  d  M  UU
;  d  M  UR                  5       U:w  d  M,  SU;  d  M4  UR                  U5        UR                  U5        MX     U Vs/ s H  nUU;  d  M  UPM     nnUb4  U Vs/ s H  nUW;  d  M  UPM     nnU Vs/ s H  nUU;  d  M  UPM     nnU$ ! , (       d  f       GN= f! , (       d  f       GNs= f! , (       d  f       GNa= f! , (       d  f       GNL= fs  snf s  snf s  snf )	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_DATALOADERS)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secondary_file_func_dl_linenew_feature_codepassed_idxsitir   _new_full_example_partsdiff_from_examplediff_from_twos                          r   compare_against_testr;   >   s   0 
m	[[] 
	bggoobggll:7GHI	Ja[[] 
K		1 ! 
 %$%&'kkm# & $%BCUW]%^_$%BCUW]%^_'(EF[]c(de)"-.KLcek.l"m$%BCUWj%kl$%BCUWj%kl'(EF[]p(qr)"--.EGZ[# `H K		 B3()A-.K8DN*(1J/t;$++D1&&q) RA /  K^,4KN*(1J/t;*11$7&&q) - +;a*:$dJ`>`*:a%*<`*<$L_@_*<`.?].?d4}C\T.?]w 
		J	J		 &%^ b`]sS   K+K(K:6L
LL$
L#2L#>
L(L(
K%(
K7:
L	
L)N)__doc__r    liststrr   r   boolr;    r   r   <module>rA      s`     
$c $# $4ZtCy ZS Ss SQU Skn Sr   