
    biY              	       R    d Z ddl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y)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                     |dk7  r|dk7  rt        d| d      g d}}| D ]M  }|sd| |v rd}|j                  |        |s#|dk(  rd|v r|c S |dk(  rd	|v r|c S |j                  |       O y
)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        Y/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/accelerate/test_utils/examples.pyget_function_contents_by_namer      s     ""tv~;D6Anopp %J 
$dV}4Kd#**zT/A!!v~-4"7!!d#
$    c                 x    | D cg c]*  }|j                         j                  d      r#|dk7  s)|, c}S c c}w )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   s2     #YT$++-*B*B3*GDTXLDYYYs   $777base_filenamefeature_filenameparser_onlysecondary_filenamec                    t        |       5 }|j                         }ddd       t        t        j                  j	                  t        j                  j                  dd                  5 }|j                         }ddd       t        |      5 }|j                         }ddd       |$t        |      5 }|j                         }ddd       |rWt        t        d            }	t        t        d            }
t        t        d            }|lt        t        d            }nVt        t        d            }	t        t        d            }
t        t        d            }|t        t        d            }d}g }g }t        |      }t        t        |      dz
        D ]]  }||vst        |      }||
vs|j                         |k7  s,d|vr#|j                  |       |j                  |       St        |      }_ g }g }t        |	      D ]J  \  }}||vs||
vs|j                         |k7  s$d|vs)|j                  |       |j                  |       L |D cg c]	  }||vs| }}|(|D cg c]	  }|vs| }}|D cg c]	  }||vs| }}|S # 1 sw Y   xY w# 1 sw Y   4xY w# 1 sw Y   xY w# 1 sw Y   xY wc c}w c c}w c c}w )	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	J +a[[]+		 .1 !.%$% 	4&'kkm#	4 $%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a%*<`$L_@_``.?]d4}C\T]]w+ ++ +. .	4 	4^ b`]sS   J!+J.J;5K%	K/K;	KK	KK!J+.J8;KK)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   