U
    0-e<                     @   sr  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$ dXddZ%e%dd
dZ&dd Z'dd Z(dd Z)dd Z*dd Z+dd Z,dd Z-dd Z.dd  Z/d!d" Z0d#d$ Z1d%d& Z2d'd( Z3d)d* Z4d+d, Z5d-d. Z6d/d0 Z7d1d2 Z8dYd3d4Z9d5d6 Z:d7d8 Z;d9d: Z<e=e" e goe  Z>d;d< Z?G d=d> d>ej@ZAG d?d@ d@ej@ZBG dAdB dBej@ZCdCdD ZDG dEdF dFZEdGdH ZFdZeEdIdJdKZGd[eEdIdNdOZHG dPdQ dQeIZJd\eeK dRdSdTZLed]eIeKeMdUdVdWZNdS )^    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                 C   s^   zt j|  }W n tk
r&   |}Y n4X zt|}W n$ tk
rX   td|  dY nX |S )NzIf set, z must be yes or no.)osenvironKeyErrorr   
ValueError)keydefaultvalue_value r"   ^/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/accelerate/test_utils/testing.pyparse_flag_from_env2   s    
r$   ZRUN_SLOW)r   c                 C   s   t d| S )z+Decorator that skips a test unconditionallyzTest was skipped)unittestskip	test_caser"   r"   r#   r&   E   s    r&   c                 C   s   t 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'   r"   r"   r#   slowJ   s    r+   c                 C   s   t t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cudaZis_availabler'   r"   r"   r#   require_cpuR   s    r/   c                 C   s   t tj d| S )zo
    Decorator marking a test that requires CUDA. These tests are skipped when there are no GPU available.
    test requires a GPUr,   r'   r"   r"   r#   require_cudaY   s    r1   c                 C   s   t 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'   r"   r"   r#   require_xpu`   s    r3   c                 C   s   t 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'   r"   r"   r#   require_mpsg   s    r4   c                 C   s   t t o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'   r"   r"   r#   require_huggingface_suiteo   s    
 r5   c                 C   s   t 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'   r"   r"   r#   require_transformersx   s    r7   c                 C   s   t t d| S )r6   ztest requires the timm library)r%   r)   r   r'   r"   r"   r#   require_timm   s    r8   c                 C   s   t 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'   r"   r"   r#   require_bnb   s    r9   c                 C   s   t 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'   r"   r"   r#   require_tpu   s    r:   c                 C   s   t t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.
       r0   r%   r)   r-   r.   device_countr'   r"   r"   r#   require_single_gpu   s    r>   c                 C   s   t t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.
    r;   r2   r%   r)   r-   Zxpur=   r'   r"   r"   r#   require_single_xpu   s    r@   c                 C   s   t tj dkd| S )z
    Decorator marking a test that requires a multi-GPU setup. These tests are skipped on a machine without multiple
    GPUs.
    r;   ztest requires multiple GPUsr<   r'   r"   r"   r#   require_multi_gpu   s    rA   c                 C   s   t tj dkd| S )z
    Decorator marking a test that requires a multi-XPU setup. These tests are skipped on a machine without multiple
    XPUs.
    r;   ztest requires multiple XPUsr?   r'   r"   r"   r#   require_multi_xpu   s    rB   c                 C   s   t 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'   r"   r"   r#   require_safetensors   s    rC   c                 C   s   t 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'   r"   r"   r#   require_deepspeed   s    rD   c                 C   s   t 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'   r"   r"   r#   require_fsdp   s    rF   c                 C   s0   | dkrt t|dS t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.
    N)versionrE   ztest requires torch version >= )r   require_torch_min_versionr%   r)   r   )r(   rG   r"   r"   r#   rH      s    rH   c                 C   s   t 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'   r"   r"   r#   require_tensorboard   s    rI   c                 C   s   t 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'   r"   r"   r#   require_wandb   s    rJ   c                 C   s   t 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'   r"   r"   r#   require_comet_ml   s    rK   c                 C   s   t 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'   r"   r"   r#   require_trackers   s    rM   c                   @   s4   e Zd ZdZdZedd Zedd Zdd Zd	S )
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                 C   s   t  | _dS )zECreates a `tempfile.TemporaryDirectory` and stores it in `cls.tmpdir`N)tempfilemkdtemptmpdirclsr"   r"   r#   
setUpClass  s    zTempDirTestCase.setUpClassc                 C   s   t j| jrt| j dS )z1Remove `cls.tmpdir` after test suite has finishedN)r   pathexistsrQ   shutilrmtreerR   r"   r"   r#   tearDownClass  s    zTempDirTestCase.tearDownClassc                 C   sD   | j r@t| jdD ](}| r,|  q| rt| qdS )z<Destroy all contents in `self.tmpdir`, but not `self.tmpdir`z**/*N)	clear_on_setupr   rQ   globis_fileunlinkis_dirrW   rX   )selfrU   r"   r"   r#   setUp  s    
zTempDirTestCase.setUpN)	__name__
__module____qualname____doc__rZ   classmethodrT   rY   r`   r"   r"   r"   r#   rN      s   	

rN   c                       s    e Zd ZdZ fddZ  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                    s   t    t  t  d S N)supertearDownr	   Z_reset_stater
   )r_   	__class__r"   r#   ri   "  s    
zAccelerateTestCase.tearDown)ra   rb   rc   rd   ri   __classcell__r"   r"   rj   r#   rf     s   rf   c                   @   s.   e Zd ZdZeejeej f dddZdS )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                 C   s>   t |ttfr|n|g| _| jD ]}|  | |j q dS )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listrn   startZ
addCleanupstop)r_   rn   mr"   r"   r#   	add_mocks;  s    	
zMockingTestCase.add_mocksN)	ra   rb   rc   rd   r   r   ZMockr   ru   r"   r"   r"   r#   rm   )  s   rm   c                 C   s`   t  }| d   |j} t|  }| d  } t|jd D ]}t	|| | s@ dS q@dS )Nr   FT)
r	   clonetoZdevicer   cpurangeshaper-   equal)ZtensorstateZtensorsir"   r"   r#   are_the_same_tensorsJ  s    r~   c                   @   s   e Zd Zdd ZdS )
_RunOutputc                 C   s   || _ || _|| _d S rg   )
returncodestdoutstderr)r_   r   r   r   r"   r"   r#   __init__V  s    z_RunOutput.__init__N)ra   rb   rc   r   r"   r"   r"   r#   r   U  s   r   c                    s$   |   I d H }|r || q q q d S rg   )readline)streamcallbackliner"   r"   r#   _read_stream\  s    
r   )returnc              	      s   |rt dd|  tj| d f| dd  |tjjtjj|dI d H }g g  dfdd	tjtt|j	fd	d
tt|j
 fdd
g|dI d H  t| I d H  S )Nz

Running:  r   r;   )stdinr   r   env c                    s.   |  d } ||   s*t|| |d d S )Nutf-8)file)decoderstripappendprint)r   Zsinkpipelabel)quietr"   r#   tee}  s    
z_stream_subprocess.<locals>.teec                    s   |  t jddS )Nzstdout:r   )sysr   l)outr   r"   r#   <lambda>      z$_stream_subprocess.<locals>.<lambda>c                    s   |  t jddS )Nzstderr:r   )r   r   r   )errr   r"   r#   r     r   )timeout)r   )r   joinasynciocreate_subprocess_exec
subprocessPIPEwaitcreate_taskr   r   r   r   )cmdr   r   r   r   echopr"   )r   r   r   r   r#   _stream_subprocesse  s,    
r      Tc           
   
   C   sb   t  }|t| |||||d}d| }|jdkr^d|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resultZcmd_strr   r"   r"   r#   execute_subprocess_async  s    

r   c                   @   s   e Zd ZdS )SubprocessCallExceptionN)ra   rb   rc   r"   r"   r"   r#   r     s   r   )commandc              
   C   s~   z2t j| t jd}|r0t|dr*|d}|W S W nF t jk
rx } z&tdd|  d|j  |W 5 d}~X Y nX dS )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   Zreturn_stdoutr   er"   r"   r#   run_command  s    


r   )exception_classmsgr   c              
   c   s   d}zdV  d}W nr t k
r } zTt|| sFtd|  dt| |dk	rt|t|ksttd| dt| dW 5 d}~X Y nX |rtd|  d	dS )
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.)	Exceptionro   AssertionErrortypestr)r   r   Zwas_ranr   r"   r"   r#   assert_exception  s    "8r   )F)NN)NNNFF)NNr   FT)F)N)Or   r   rW   r   r   rO   r%   
contextlibr   	functoolsr   pathlibr   typingr   r   r   r-   r|   r	   r
   utilsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r$   r*   r&   r+   r/   r1   r3   r4   r5   r7   r8   r9   r:   r>   r@   rA   rB   rC   rD   rF   rH   rI   rJ   rK   anyrL   rM   ZTestCaserN   rf   rm   r~   r   r   r   r   r   r   r   r   boolr   r"   r"   r"   r#   <module>   sj   D
	

!!	)