
    9i<                        d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZ d dl	m
Z
 d dlmZ d dlmZmZ d dlmZ d dlZddlmZmZ dd	lmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$ d9dZ% e%dd
      Z&d Z'd Z(d Z)d Z*d Z+d Z,d Z-d Z.d Z/d Z0d Z1d Z2d Z3d Z4d Z5d Z6d Z7d Z8d:d Z9d! Z:d" Z;d# Z< e= e"        e       g      xr  e        Z>d$ Z? G d% d&ej                        ZA G d' d(ej                        ZB G d) d*ej                        ZCd+ ZD G d, d-      ZEd. ZFd;d/eEfd0ZGd<d/eEfd1ZH G d2 d3eI      ZJd9d4eeK   fd5ZLed=d6eId7eKd/eMfd8       ZNy)>    N)contextmanager)partial)Path)ListUnion)mock   )AcceleratorStatePartialState)gatheris_bnb_availableis_comet_ml_availableis_datasets_availableis_deepspeed_availableis_mps_availableis_safetensors_availableis_tensorboard_availableis_timm_availableis_torch_versionis_tpu_availableis_transformers_availableis_wandb_availableis_xpu_availablestr_to_boolFc                     	 t         j                  |    }	 t        |      }|S # t        $ r t        d|  d      w xY w# t        $ r |}Y |S w xY w)NzIf set, z must be yes or no.)osenvironr   
ValueErrorKeyError)keydefaultvalue_values       g/var/www/html/backtest/airagagent/rag_env/lib/python3.12/site-packages/accelerate/test_utils/testing.pyparse_flag_from_envr%   2   sp    B

3	B 'F M  	Bxu,?@AA	B   Ms   ? # <AARUN_SLOW)r!   c                 8     t        j                  d      |       S )z+Decorator that skips a test unconditionallyzTest was skipped)unittestskip	test_cases    r$   r)   r)   E   s    ,8==+,Y77    c                 B     t        j                  t        d      |       S )z
    Decorator marking a test as slow. Slow tests are skipped by default. Set the RUN_SLOW environment variable to a
    truthy value to run them.
    ztest is slow)r(   
skipUnless_run_slow_testsr*   s    r$   slowr0   J   s    
 @8?	JJr,   c                 t     t        j                  t        j                  j	                          d      |       S )zu
    Decorator marking a test that must be only ran on the CPU. These tests are skipped when a GPU is available.
    ztest requires only a CPUr(   r.   torchcudais_availabler*   s    r$   require_cpur6   R   s1     Z85::#:#:#<<>XYZcddr,   c                 r     t        j                  t        j                  j	                         d      |       S )zo
    Decorator marking a test that requires CUDA. These tests are skipped when there are no GPU available.
    test requires a GPUr2   r*   s    r$   require_cudar9   Y   s.     Q8uzz668:OPQZ[[r,   c                 J     t        j                  t               d      |       S )zn
    Decorator marking a test that requires XPU. These tests are skipped when there are no XPU available.
    test requires a XPU)r(   r.   r   r*   s    r$   require_xpur<   `   s$     J8/13HI)TTr,   c                 J     t        j                  t               d      |       S )z
    Decorator marking a test that requires MPS backend. These tests are skipped when torch doesn't support `mps`
    backend.
    z0test requires a `mps` backend support in `torch`)r(   r.   r   r*   s    r$   require_mpsr>   g   s%    
 g8/13efgpqqr,   c                 b     t        j                  t               xr
 t               d      |       S )zv
    Decorator marking a test that requires transformers and datasets. These tests are skipped when they are not.
    z$test requires the Hugging Face suite)r(   r.   r   r   r*   s    r$   require_huggingface_suiter@   o   s3    8!#?(=(?Ag r,   c                 J     t        j                  t               d      |       S )i
    Decorator marking a test that requires transformers. These tests are skipped when they are not.
    z&test requires the transformers library)r(   r.   r   r*   s    r$   require_transformersrC   x   s%     f88:<defoppr,   c                 J     t        j                  t               d      |       S )rB   ztest requires the timm library)r(   r.   r   r*   s    r$   require_timmrE      s%     V8024TUV_``r,   c                 J     t        j                  t               d      |       S )zi
    Decorator marking a test that requires bitsandbytes. These tests are skipped when they are not.
    z&test requires the bitsandbytes library)r(   r.   r   r*   s    r$   require_bnbrG      s%     ]8/13[\]fggr,   c                 J     t        j                  t               d      |       S )zp
    Decorator marking a test that requires TPUs. These tests are skipped when there are no TPUs available.
    ztest requires TPU)r(   r.   r   r*   s    r$   require_tpurI      s$     H8/13FG	RRr,   c                 x     t        j                  t        j                  j	                         dk(  d      |       S )z
    Decorator marking a test that requires CUDA on a single GPU. These tests are skipped when there are no GPU
    available or number of GPUs is more than one.
       r8   r(   r.   r3   r4   device_countr*   s    r$   require_single_gpurN      s3    
 V8uzz668A=?TUV_``r,   c                 x     t        j                  t        j                  j	                         dk(  d      |       S )z
    Decorator marking a test that requires CUDA on a single XPU. These tests are skipped when there are no XPU
    available or number of xPUs is more than one.
    rK   r;   r(   r.   r3   xpurM   r*   s    r$   require_single_xpurR      s3    
 U8uyy5571<>STU^__r,   c                 x     t        j                  t        j                  j	                         dkD  d      |       S )z
    Decorator marking a test that requires a multi-GPU setup. These tests are skipped on a machine without multiple
    GPUs.
    rK   ztest requires multiple GPUsrL   r*   s    r$   require_multi_gpurT      s3    
 ]8uzz6681<>[\]fggr,   c                 x     t        j                  t        j                  j	                         dkD  d      |       S )z
    Decorator marking a test that requires a multi-XPU setup. These tests are skipped on a machine without multiple
    XPUs.
    rK   ztest requires multiple XPUsrP   r*   s    r$   require_multi_xpurV      s3    
 \8uyy557!;=Z[\effr,   c                 J     t        j                  t               d      |       S )z
    Decorator marking a test that requires safetensors installed. These tests are skipped when safetensors isn't
    installed
    ztest requires safetensors)r(   r.   r   r*   s    r$   require_safetensorsrX      %    
 X879;VWXabbr,   c                 J     t        j                  t               d      |       S )z|
    Decorator marking a test that requires DeepSpeed installed. These tests are skipped when DeepSpeed isn't installed
    ztest requires DeepSpeed)r(   r.   r   r*   s    r$   require_deepspeedr[      s%     T8579RST]^^r,   c                 N     t        j                  t        dd      d      |       S )zr
    Decorator marking a test that requires FSDP installed. These tests are skipped when FSDP isn't installed
    >=z1.12.0z%test requires torch version >= 1.12.0)r(   r.   r   r*   s    r$   require_fsdpr^      s)     j8/h?Ahijsttr,   c                 z    | t        t        |      S  t        j                  t	        d|      d|       |       S )z
    Decorator marking that a test requires a particular torch version to be tested. These tests are skipped when an
    installed torch version is less than the required one.
    )versionr]   ztest requires torch version >= )r   require_torch_min_versionr(   r.   r   )r+   r`   s     r$   ra   ra      sE    
 0'BBl8/g>Babiaj@klmvwwr,   c                 J     t        j                  t               d      |       S )z
    Decorator marking a test that requires tensorboard installed. These tests are skipped when tensorboard isn't
    installed
    ztest requires Tensorboard)r(   r.   r   r*   s    r$   require_tensorboardrc      rY   r,   c                 J     t        j                  t               d      |       S )zt
    Decorator marking a test that requires wandb installed. These tests are skipped when wandb isn't installed
    ztest requires wandb)r(   r.   r   r*   s    r$   require_wandbre      s$     L8135JKIVVr,   c                 J     t        j                  t               d      |       S )zz
    Decorator marking a test that requires comet_ml installed. These tests are skipped when comet_ml isn't installed
    ztest requires comet_ml)r(   r.   r   r*   s    r$   require_comet_mlrg      s%     R8468PQR[\\r,   c                 B     t        j                  t        d      |       S )z
    Decorator marking that a test requires at least one tracking library installed. These tests are skipped when none
    are installed
    zYtest requires at least one tracker to be available and for `comet_ml` to not be installed)r(   r.   _atleast_one_tracker_availabler*   s    r$   require_trackersrj      s)    
8&c  r,   c                   :    e Zd ZdZdZed        Zed        Zd Zy)TempDirTestCasea  
    A TestCase class that keeps a single `tempfile.TemporaryDirectory` open for the duration of the class, wipes its
    data at the start of a test, and then destroyes it at the end of the TestCase.

    Useful for when a class or API requires a single constant folder throughout it's use, such as Weights and Biases

    The temporary directory location will be stored in `self.tmpdir`
    Tc                 6    t        j                         | _        y)zECreates a `tempfile.TemporaryDirectory` and stores it in `cls.tmpdir`N)tempfilemkdtemptmpdirclss    r$   
setUpClasszTempDirTestCase.setUpClass  s     %%'
r,   c                     t         j                  j                  | j                        r t	        j
                  | j                         yy)z1Remove `cls.tmpdir` after test suite has finishedN)r   pathexistsrp   shutilrmtreerq   s    r$   tearDownClasszTempDirTestCase.tearDownClass  s-     77>>#**%MM#**% &r,   c                     | j                   rqt        | j                        j                  d      D ]I  }|j	                         r|j                          $|j                         s5t        j                  |       K yy)z<Destroy all contents in `self.tmpdir`, but not `self.tmpdir`z**/*N)	clear_on_setupr   rp   globis_fileunlinkis_dirrw   rx   )selfru   s     r$   setUpzTempDirTestCase.setUp  sZ    T[[)..v6 (<<>KKM[[]MM$'	( r,   N)	__name__
__module____qualname____doc__r{   classmethodrs   ry   r    r,   r$   rl   rl      s9     N( ( & &
(r,   rl   c                   "     e Zd ZdZ fdZ xZS )AccelerateTestCasez
    A TestCase class that will reset the accelerator state at the end of every test. Every test that checks or utilizes
    the `AcceleratorState` class should inherit from this to avoid silent failures due to state being shared between
    tests.
    c                 r    t         |           t        j                          t	        j                          y N)supertearDownr
   _reset_stater   )r   	__class__s    r$   r   zAccelerateTestCase.tearDown"  s&    %%'!!#r,   )r   r   r   r   r   __classcell__)r   s   @r$   r   r     s    $ $r,   r   c                   T    e Zd ZdZdeej                  eej                     f   fdZy)MockingTestCasea  
    A TestCase class designed to dynamically add various mockers that should be used in every test, mimicking the
    behavior of a class-wide mock when defining one normally will not do.

    Useful when a mock requires specific information available only initialized after `TestCase.setUpClass`, such as
    setting an environment variable with that information.

    The `add_mocks` function should be ran at the end of a `TestCase`'s `setUp` function, after a call to
    `super().setUp()` such as:
    ```python
    def setUp(self):
        super().setUp()
        mocks = mock.patch.dict(os.environ, {"SOME_ENV_VAR", "SOME_VALUE"})
        self.add_mocks(mocks)
    ```
    mocksc                     t        |t        t        f      r|n|g| _        | j                  D ]-  }|j	                          | j                  |j                         / y)aQ  
        Add custom mocks for tests that should be repeated on each test. Should be called during
        `MockingTestCase.setUp`, after `super().setUp()`.

        Args:
            mocks (`mock.Mock` or list of `mock.Mock`):
                Mocks that should be added to the `TestCase` after `TestCase.setUpClass` has been run
        N)
isinstancetuplelistr   start
addCleanupstop)r   r   ms      r$   	add_mockszMockingTestCase.add_mocks;  sH     )>UUG
 	$AGGIOOAFF#	$r,   N)	r   r   r   r   r   r   Mockr   r   r   r,   r$   r   r   )  s)    "$uTYYTYY%?@ $r,   r   c                 8   t               }| d    j                         j                  |j                        } t	        |       j                         }| d   j                         } t        |j                  d         D ]  }t        j                  ||   |       r y y)Nr   FT)
r
   clonetodevicer   cpurangeshaper3   equal)tensorstatetensorsis       r$   are_the_same_tensorsr   J  s    ED\!$$U\\2FVn  "GAY]]_F7==#$ {{71:v. r,   c                       e Zd Zd Zy)
_RunOutputc                 .    || _         || _        || _        y r   )
returncodestdoutstderr)r   r   r   r   s       r$   __init__z_RunOutput.__init__V  s    $r,   N)r   r   r   r   r   r,   r$   r   r   U  s    r,   r   c                 Z   K   	 | j                          d {   }|r	 ||       ny %7 wr   )readline)streamcallbacklines      r$   _read_streamr   \  s-     
__&&TN &s   +)+returnc           
      T  	K   |rt        ddj                  |              t        j                  | d   g| dd  |t        j                  j
                  t        j                  j
                  |d d {   }g g d
fd		t        j                  t        j                  t        |j                  	fd            t        j                  t        |j                  	fd            g|	       d {    t        |j                          d {         S 7 7 )7 w)Nz

Running:  r   rK   )stdinr   r   envc                     | j                  d      j                         } |j                  |        st        || |       y y )Nutf-8)file)decoderstripappendprint)r   sinkpipelabelquiets       r$   teez_stream_subprocess.<locals>.tee}  s:    {{7#**,D%D) r,   c                 8     | t         j                  d      S )Nzstdout:r   )sysr   )loutr   s    r$   <lambda>z$_stream_subprocess.<locals>.<lambda>      QSZZ_hAi r,   c                 8     | t         j                  d      S )Nzstderr:r   )r   r   )r   errr   s    r$   r   z$_stream_subprocess.<locals>.<lambda>  r   r,   )timeout) )r   joinasynciocreate_subprocess_exec
subprocessPIPEwaitcreate_taskr   r   r   r   )
cmdr   r   r   r   echopr   r   r   s
       `  @@@r$   _stream_subprocessr   e  s    mSXXc]+,,A	QR !!&&!!&& 	A" C
C* ,,QXX7i jkQXX7i jk	
    AFFHnc3//E	6 %s7   A3D(9D":B D(:D$;D(D&D($D(&D(c           
          t        j                         }|j                  t        | |||||            }dj	                  |       }|j
                  dkD  r9dj	                  |j                        }	t        d| d|j
                   d|	       |S )N)r   r   r   r   r   r   r   
'z' failed with returncode z,

The combined stderr from workers follows:
)r   get_event_looprun_until_completer   r   r   r   RuntimeError)
r   r   r   r   r   r   loopresultcmd_strr   s
             r$   execute_subprocess_asyncr     s    !!#D$$3CugUY]^F hhsmG16==)y1&2C2C1D E::@C
 	

 Mr,   c                       e Zd Zy)SubprocessCallExceptionN)r   r   r   r   r,   r$   r   r     s    r,   r   commandc                 :   	 t        j                  | t         j                        }|rt        |d      r|j	                  d      }|S y# t         j
                  $ r>}t        ddj                  |        d|j                  j	                                |d}~ww xY w)z
    Runs `command` with `subprocess.check_output` and will potentially return the `stdout`. Will also properly capture
    if an error occured while running `command`
    )r   r   r   z	Command `r   z$` failed with the following error:

N)	r   check_outputSTDOUThasattrr   CalledProcessErrorr   r   output)r   return_stdoutr   es       r$   run_commandr     s    
	((9J9JKvx(w/M  (( %)**PQRQYQYQ`Q`QbPcd
	s   AA	 	B9BBexception_classmsgc              #      K   d}	 d d}|rt	        d|  d	      y# t         $ rR}t        ||       sJ d|  dt        |              |$|t        |      v sJ d| dt        |       d       Y d}~hd}~ww xY ww)
z
    Context manager to assert that the right `Exception` class was raised.

    If `msg` is provided, will check that the message is contained in the raised exception.
    FNTzExpected exception of type z	 but got zExpected message 'z' to be in exception but got 'r   z but ran without issue.)	Exceptionr   typestrAssertionError)r   r   was_ranr   s       r$   assert_exceptionr     s      Gd
 :?:KKbcdd 	  d!_-p1L_L]]fgklmgnfo/pp-?#a&=c$6se;YZ]^_Z`Yaab"cc=ds+   A< A<	A9AA4/A<4A99A<)F)NN)NNNFF)NN   FTr   )Or   r   rw   r   r   rn   r(   
contextlibr   	functoolsr   pathlibr   typingr   r   r   r3   r   r
   r   utilsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r%   r/   r)   r0   r6   r9   r<   r>   r@   rC   rE   rG   rI   rN   rR   rT   rV   rX   r[   r^   ra   rc   re   rg   anyri   rj   TestCaserl   r   r   r   r   r   r   r   r   r   r   r   boolr   r   r,   r$   <module>r     s    	   
   %      2    &  &j%@8
Ke\UrqahSa`hgc_uxcW] 		79:;[DYD[@[ 
(h'' (B$** $$h'' $B &0bl &0R`j "	i 	c " ei ec eT e er,   