
    
9j%x                        S SK r S SKrS SKrS SKrS SKrS SKrS SKrS SKrS SKrS SK	r	S SK
Jr  S SKJr  S SKJr  S SKJrJr  S SK	Jr  S SKrS SKrSSKJr  SS	KJrJrJrJrJrJrJrJ r J!r!J"r"J#r#J$r$J%r%J&r&J'r'J(r(J)r)J*r*J+r+J,r,J-r-J.r.J/r/J0r0J1r1J2r2J3r3J4r4J5r5J6r6J7r7J8r8J9r9J:r:J;r;J<r<J=r=J>r>J?r?J@r@JArAJBrBJCrCJDrD  S
 rE\E" 5       u  rFrGrHS\I4S jrJ\J" \GSS9rKSfS jrL\L" SSS9rMS rNS rOS rPS rQS rRS rSS rTS rUS rVS rWS rXS rYS rZS  r[S! r\S" r]S# r^S$ r_S% r`S& raS' rbS( rcS) rdS* reS+ rfS, rgS- rhS. riS/ rjS0 rkS1 rlS2 rmS3 rnS4 roS5 rpS6 rqS7 rrS8 rsSgS9 jrtS: ruS; rvS< rwS= rxS> ryS? rzS@ r{SA r|SB r}SC r~SD rSE rSF rSG rSH rSI r\" \B" 5       \6" 5       \=" 5       \5" 5       /5      =(       a    \" 5       (       + rSJ rSK rSL r " SM SN\	GR                  5      r " SO SP\	GR                  5      r " SQ SR\	GR                  5      rSS r " ST SU5      rSV rShS\4SW jjrSiSX\IS\4SY jjrSZ rS[ r " S\ S]\5      rSjS^\I\   4S_ jjrS`\S\4Sa jr\SkSb\Sc\\   S\4Sd jj5       rSe rg)l    N)contextmanager)partial)Path)OptionalUnion)mock   )AcceleratorState),check_cuda_fp8_capabilitycompare_versionsgatheris_aim_availableis_bnb_availableis_clearml_availableis_comet_ml_availableis_cuda_availableis_datasets_availableis_deepspeed_availableis_dvclive_availableis_fp8_availableis_fp16_availableis_habana_gaudi1is_hpu_availableis_import_timer_availableis_matplotlib_availableis_mlflow_availableis_mlu_availableis_mps_availableis_musa_availableis_neuron_availableis_npu_availableis_pandas_availableis_pippy_availableis_pytest_availableis_schedulefree_availableis_sdaa_availableis_swanlab_availableis_tensorboard_availableis_timm_availableis_torch_versionis_torch_xla_availableis_torchao_available*is_torchdata_stateful_dataloader_availableis_torchvision_availableis_trackio_availableis_transformer_engine_available%is_transformer_engine_mxfp8_availableis_transformers_availableis_triton_availableis_wandb_availableis_xpu_availablestr_to_boolc                     [        5       (       a9  S[        R                  R                  5       [        R                  R                  4$ [        5       (       a9  S[        R                  R                  5       [        R                  R                  4$ [        SS9(       a  SS[        R                  R                  4$ [        5       (       a  SSS 4$ [        5       (       a9  S[        R                  R                  5       [        R                  R                  4$ [        5       (       a9  S	[        R                  R                  5       [        R                  R                  4$ [        5       (       a9  S
[        R                  R                  5       [        R                  R                  4$ [        5       (       a9  S[        R                   R                  5       [        R                   R                  4$ [#        5       (       a9  S[        R$                  R                  5       [        R$                  R                  4$ ['        5       (       a9  S[        R(                  R                  5       [        R(                  R                  4$ [+        5       (       a9  S[        R,                  R                  5       [        R,                  R                  4$ SSS 4$ )Nxlacudaz2.0)min_versionmps   c                      gNr    r?       W/home/wildlama/miniconda3/lib/python3.13/site-packages/accelerate/test_utils/testing.py<lambda>get_backend.<locals>.<lambda>\       r@   mlusdaamusanpuxpuhpuneuroncpuc                      gr>   r?   r?   r@   rA   rB   rC   l   rD   r@   )r+   torchr9   device_countmemory_allocatedr   r   r;   current_allocated_memoryr   rE   r&   rF   r   rG   r!   rH   r5   rI   r   rJ   r    rK   r?   r@   rA   get_backendrR   T   s   ejj--/1L1LLL			uzz..0%**2M2MMM	e	,a;;;;			a""			eii,,.		0J0JJJ			uzz..0%**2M2MMM			uzz..0%**2M2MMM			eii,,.		0J0JJJ			eii,,.		0J0JJJ			eii,,.		0J0JJJ			224ell6S6SSSa""r@   returnc                      SS/nU R                  5        HS  u  p#[        U[        5      (       a  U(       a  UR                  SU 35        M7  Uc  M<  UR                  SU SU 35        MU     U$ )z
Wraps around `kwargs` to help simplify launching from `subprocess`.

Example:
```python
# returns ['accelerate', 'launch', '--num_processes=2', '--device_count=2']
get_launch_command(num_processes=2, device_count=2)
```

acceleratelaunchz--=)items
isinstanceboolappend)kwargscommandkvs       rA   get_launch_commandr`   r   se     X&Ga1NNRs8$]NNRs!A3<(	 
 Nr@   g?)num_processesmonitor_intervalFc                      [         R                  U    n [        U5      nU$ ! [         a    [        SU  S35      ef = f! [         a    Un U$ f = f)NzIf set, z must be yes or no.)osenvironr6   
ValueErrorKeyError)keydefaultvalue_values       rA   parse_flag_from_envrl      sp    B

3	B 'F M  	Bxu,?@AA	B   Ms   A  # = AARUN_SLOW)ri   c                 :    [         R                  " S5      " U 5      $ )z+Decorator that skips a test unconditionallyzTest was skipped)unittestskip	test_cases    rA   rp   rp      s    ==+,Y77r@   c                 D    [         R                  " [        S5      " U 5      $ )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)ro   
skipUnless_run_slow_testsrq   s    rA   slowrv      s    
 ?	JJr@   c                 J    [         R                  " [        S:H  S5      " U 5      $ )zm
Decorator marking a test that must be only ran on the CPU. These tests are skipped when a GPU is available.
rL   ztest requires only a CPUro   rt   torch_devicerq   s    rA   require_cpurz      s#     |u46PQR[\\r@   c                 J    [         R                  " [        S:g  S5      " U 5      $ )z
Decorator marking a test that requires a hardware accelerator backend. These tests are skipped when there are no
hardware accelerator available.
rL   test requires a GPUrx   rq   s    rA   require_non_cpur}      s"    
 |u46KLYWWr@   c                 x    [         R                  " [        5       =(       a    [        5       (       + S5      " U 5      $ )z
Decorator marking a test that requires CUDA. These tests are skipped when there are no GPU available or when
TorchXLA is available.
r|   )ro   rt   r   r+   rq   s    rA   require_cudar      s.    
 02S;Q;S7SUjkluvvr@   c                     [         R                  " [        5       =(       a    [        5       (       + =(       d
    [	        5       S5      " U 5      $ )z
Decorator marking a test that requires CUDA or HPU. These tests are skipped when there are no GPU available or when
TorchXLA is available.
ztest requires a GPU or HPU)ro   rt   r   r+   r   rq   s    rA   require_cuda_or_hpur      s=    
 			=%;%=!=TBRBTVr r@   c                 L    [         R                  " [        5       S5      " U 5      $ )zf
Decorator marking a test that requires XPU. These tests are skipped when there are no XPU available.
test requires a XPU)ro   rt   r5   rq   s    rA   require_xpur      s!     /13HI)TTr@   c                     [        5       =(       a    [        5       (       + n[        5       n[        R                  " U=(       d    US5      " U 5      $ )z
Decorator marking a test that requires CUDA or XPU. These tests are skipped when there are no GPU available or when
TorchXLA is available.
ztest requires a CUDA GPU or XPU)r   r+   r5   ro   rt   )rr   cuda_conditionxpu_conditions      rA   require_cuda_or_xpur      s?    
 '(I1G1I-IN$&M~>@abclmmr@   c                 J    [         R                  " [        S:g  S5      " U 5      $ )z:
Decorator marking a test that should be skipped for XPU.
rI   ztest requires a non-XPUrx   rq   s    rA   require_non_xpur      #     |u46OPQZ[[r@   c                 J    [         R                  " [        S:g  S5      " U 5      $ )z:
Decorator marking a test that should be skipped for HPU.
rJ   ztest requires a non-HPUrx   rq   s    rA   require_non_hpur      r   r@   c                 L    [         R                  " [        5       S5      " U 5      $ )zb
Decorator marking a test that requires FP16. These tests are skipped when FP16 is not supported.
ztest requires FP16 support)ro   rt   r   rq   s    rA   require_fp16r      s"    
 024PQR[\\r@   c                     [        5       n[        R                  R                  5       (       a  [	        5       (       d  Sn[        5       (       a  [        5       (       a  Sn[        R                  " US5      " U 5      $ )z`
Decorator marking a test that requires FP8. These tests are skipped when FP8 is not supported.
Fztest requires FP8 support)	r   rN   r9   is_availabler   r   r   ro   rt   )rr   fp8_is_availables     rA   require_fp8r      sa     ()zz  )B)D)D .00 /1LMiXXr@   c                 P    [         R                  " [        SS5      S5      " U 5      $ )N>=z2.5.0z$test requires FSDP2 (torch >= 2.5.0))ro   rt   r*   rq   s    rA   require_fsdp2r     s$    /g>@fghqrrr@   c                 L    [         R                  " [        5       S5      " U 5      $ )zf
Decorator marking a test that requires MLU. These tests are skipped when there are no MLU available.
ztest require a MLU)ro   rt   r   rq   s    rA   require_mlur     !     /13GHSSr@   c                 L    [         R                  " [        5       S5      " U 5      $ )zh
Decorator marking a test that requires SDAA. These tests are skipped when there are no SDAA available.
ztest require a SDAA)ro   rt   r&   rq   s    rA   require_sdaar     !     024IJ9UUr@   c                 L    [         R                  " [        5       S5      " U 5      $ )zh
Decorator marking a test that requires MUSA. These tests are skipped when there are no MUSA available.
ztest require a MUSA)ro   rt   r   rq   s    rA   require_musar     r   r@   c                 L    [         R                  " [        5       S5      " U 5      $ )zf
Decorator marking a test that requires NPU. These tests are skipped when there are no NPU available.
ztest require a NPU)ro   rt   r!   rq   s    rA   require_npur     r   r@   c                 L    [         R                  " [        5       S5      " U 5      $ )zr
Decorator marking a test that requires Neuron. These tests are skipped when there are no Neuron Cores available.
ztest require Neuron Cores)ro   rt   r    rq   s    rA   require_neuronr   "  s"     246QRS\]]r@   c                 L    [         R                  " [        5       S5      " U 5      $ )zw
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`)ro   rt   r   rq   s    rA   require_mpsr   )  s"    
 /13efgpqqr@   c                 n    [         R                  " [        5       =(       a
    [        5       S5      " U 5      $ )zn
Decorator marking a test that requires transformers and datasets. These tests are skipped when they are not.
z$test requires the Hugging Face suite)ro   rt   r2   r   rq   s    rA   require_huggingface_suiter   1  s3     !#?(=(?.  r@   c                 L    [         R                  " [        5       S5      " U 5      $ )z]
Decorator marking a test that requires datasets. These tests are skipped when they are not.
z"test requires the datasets library)ro   rt   r   rq   s    rA   require_datasetsr   ;  s"     468\]^ghhr@   c                 L    [         R                  " [        5       S5      " U 5      $ )za
Decorator marking a test that requires transformers. These tests are skipped when they are not.
z&test requires the transformers library)ro   rt   r2   rq   s    rA   require_transformersr   B  "     8:<defoppr@   c                 L    [         R                  " [        5       S5      " U 5      $ )zY
Decorator marking a test that requires timm. These tests are skipped when they are not.
ztest requires the timm library)ro   rt   r)   rq   s    rA   require_timmr   I  s"     024TUV_``r@   c                 L    [         R                  " [        5       S5      " U 5      $ )z`
Decorator marking a test that requires torchvision. These tests are skipped when they are not.
z%test requires the torchvision library)ro   rt   r.   rq   s    rA   require_torchvisionr   P  s"     79;bcdmnnr@   c                 L    [         R                  " [        5       S5      " U 5      $ )z[
Decorator marking a test that requires triton. These tests are skipped when they are not.
z test requires the triton library)ro   rt   r3   rq   s    rA   require_tritonr   W  s"     246XYZcddr@   c                 L    [         R                  " [        5       S5      " U 5      $ )za
Decorator marking a test that requires schedulefree. These tests are skipped when they are not.
z&test requires the schedulefree library)ro   rt   r%   rq   s    rA   require_schedulefreer   ^  r   r@   c                 L    [         R                  " [        5       S5      " U 5      $ )za
Decorator marking a test that requires bitsandbytes. These tests are skipped when they are not.
z&test requires the bitsandbytes library)ro   rt   r   rq   s    rA   require_bnbr   e  s"     /13[\]fggr@   c                 J    [         R                  " [        SS9S5      " U 5      $ )zh
Decorator marking a test that requires TPUs. These tests are skipped when there are no TPUs available.
T)check_is_tpuztest requires TPUro   rt   r+   rq   s    rA   require_tpur   l  s$     54HJ]^_hiir@   c                 V    [         R                  " [        5       (       + S5      " U 5      $ )z|
Decorator marking a test as requiring an environment without TorchXLA. These tests are skipped when TorchXLA is
available.
z%test requires an env without TorchXLAr   rq   s    rA   require_non_torch_xlar   s  s%    
 #9#;;=defoppr@   c                 j    [         R                  " [        S:g  =(       a	    [        S:H  S5      " U 5      $ )z
Decorator marking a test that requires a single device. These tests are skipped when there is no hardware
accelerator available or number of devices is more than one.
rL   r<   z)test requires a single device accelerator)ro   rt   ry   rO   rq   s    rA   require_single_devicer   {  s4    
 3,!"35` r@   c                 z    [         R                  " [        R                  R	                  5       S:H  S5      " U 5      $ )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.
r<   r|   ro   rt   rN   r9   rO   rq   s    rA   require_single_gpur     s0    
 uzz668A=?TUV_``r@   c                 z    [         R                  " [        R                  R	                  5       S:H  S5      " U 5      $ )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<   r   ro   rt   rN   rI   rO   rq   s    rA   require_single_xpur     s0    
 uyy5571<>STU^__r@   c                 J    [         R                  " [        S:  S5      " U 5      $ )z}
Decorator marking a test that requires a multi-device setup. These tests are skipped on a machine without multiple
devices.
r<   z,test requires multiple hardware accelerators)ro   rt   rO   rq   s    rA   require_multi_devicer     s#    
 |a/1_`ajkkr@   c                 z    [         R                  " [        R                  R	                  5       S:  S5      " U 5      $ )zw
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   rq   s    rA   require_multi_gpur     s0    
 uzz6681<>[\]fggr@   c                 z    [         R                  " [        R                  R	                  5       S:  S5      " U 5      $ )zw
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   rq   s    rA   require_multi_xpur     s0    
 uyy557!;=Z[\effr@   c                     [         R                  " [        5       =(       d
    [        5       =(       a	    [        S:  S5      " U 5      $ )z
Decorator marking a test that requires a multi-GPU setup. These tests are skipped on a machine without multiple
GPUs or XPUs.
r<   z#test requires multiple GPUs or XPUs)ro   rt   r   r5   rO   rq   s    rA   require_multi_gpu_or_xpur     s;    
 			2 0 2Hq8HJo r@   c                 L    [         R                  " [        5       S5      " U 5      $ )zt
Decorator marking a test that requires DeepSpeed installed. These tests are skipped when DeepSpeed isn't installed
ztest requires DeepSpeed)ro   rt   r   rq   s    rA   require_deepspeedr     s"     579RST]^^r@   c                 x    [         R                  " [        SS5      =(       a    [        SSS5      S5      " U 5      $ )zf
Decorator marking a test that requires TP installed. These tests are skipped when TP isn't installed
r   z2.3.0transformersz4.52.0zGtest requires torch version >= 2.3.0 and transformers version >= 4.52.0)ro   rt   r*   r   rq   s    rA   
require_tpr     s>     w'\,<^TS[,\Q  r@   c                 x    U c  [        [        US9$ [        R                  " [	        SU5      SU 35      " U 5      $ )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_versionro   rt   r*   )rr   r   s     rA   r   r     sC    
 0'BB/g>Babiaj@klmvwwr@   c                 L    [         R                  " [        5       S5      " U 5      $ )zx
Decorator marking a test that requires tensorboard installed. These tests are skipped when tensorboard isn't
installed
ztest requires Tensorboard)ro   rt   r(   rq   s    rA   require_tensorboardr     s"    
 79;VWXabbr@   c                 L    [         R                  " [        5       S5      " U 5      $ )zl
Decorator marking a test that requires wandb installed. These tests are skipped when wandb isn't installed
ztest requires wandb)ro   rt   r4   rq   s    rA   require_wandbr     s!     135JKIVVr@   c                 L    [         R                  " [        5       S5      " U 5      $ )zp
Decorator marking a test that requires trackio installed. These tests are skipped when trackio isn't installed
ztest requires trackio)ro   rt   r/   rq   s    rA   require_trackior     "     357NOPYZZr@   c                 L    [         R                  " [        5       S5      " U 5      $ )zr
Decorator marking a test that requires comet_ml installed. These tests are skipped when comet_ml isn't installed
ztest requires comet_ml)ro   rt   r   rq   s    rA   require_comet_mlr     s"     468PQR[\\r@   c                 L    [         R                  " [        5       S5      " U 5      $ )zh
Decorator marking a test that requires aim installed. These tests are skipped when aim isn't installed
ztest requires aim)ro   rt   r   rq   s    rA   require_aimr     s!     /13FG	RRr@   c                 L    [         R                  " [        5       S5      " U 5      $ )zp
Decorator marking a test that requires clearml installed. These tests are skipped when clearml isn't installed
ztest requires clearml)ro   rt   r   rq   s    rA   require_clearmlr     r   r@   c                 L    [         R                  " [        5       S5      " U 5      $ )zp
Decorator marking a test that requires dvclive installed. These tests are skipped when dvclive isn't installed
ztest requires dvclive)ro   rt   r   rq   s    rA   require_dvcliver     r   r@   c                 L    [         R                  " [        5       S5      " U 5      $ )zp
Decorator marking a test that requires swanlab installed. These tests are skipped when swanlab isn't installed
ztest requires swanlab)ro   rt   r'   rq   s    rA   require_swanlabr     r   r@   c                 L    [         R                  " [        5       S5      " U 5      $ )zn
Decorator marking a test that requires pandas installed. These tests are skipped when pandas isn't installed
ztest requires pandas)ro   rt   r"   rq   s    rA   require_pandasr     !     246LMiXXr@   c                 L    [         R                  " [        5       S5      " U 5      $ )zn
Decorator marking a test that requires mlflow installed. These tests are skipped when mlflow isn't installed
ztest requires mlflow)ro   rt   r   rq   s    rA   require_mlflowr     r   r@   c                 x    [         R                  " [        5       =(       a    [        5       (       + S5      " U 5      $ )z
Decorator marking a test that requires pippy installed. These tests are skipped when pippy isn't installed It is
also checked if the test is running on a Gaudi1 device which doesn't support pippy.
ztest requires pippy)ro   rt   r#   r   rq   s    rA   require_pippyr     s.    
 13N<L<N8NPefgpqqr@   c                 L    [         R                  " [        5       S5      " U 5      $ )zv
Decorator marking a test that requires tuna interpreter installed. These tests are skipped when tuna isn't
installed
ztest requires tuna interpreter)ro   rt   r   rq   s    rA   require_import_timerr   !  s"    
 8:<\]^ghhr@   c                 L    [         R                  " [        5       S5      " U 5      $ )z
Decorator marking a test that requires transformers engine installed. These tests are skipped when transformers
engine isn't installed
z!test requires transformers engine)ro   rt   r0   rq   s    rA   require_transformer_enginer   )  s"    
 >@Befgpqqr@   c                 L    [         R                  " [        5       S5      " U 5      $ )z
Decorator marking a test that requires transformers engine MXFP8 block scaling available. These tests are skipped
when transformers engine MXFP8 block scaling isn't available
z5test requires transformers engine MXFP8 block scaling)ro   rt   r1   rq   s    rA    require_transformer_engine_mxfp8r   1  s(    
 -/1h r@   c                 L    [         R                  " [        5       S5      " U 5      $ )zp
Decorator marking a test that requires torchao installed. These tests are skipped when torchao isn't installed
ztest requires torchao)ro   rt   r,   rq   s    rA   require_torchaor   ;  r   r@   c                 L    [         R                  " [        5       S5      " U 5      $ )zv
Decorator marking a test that requires matplotlib installed. These tests are skipped when matplotlib isn't
installed
ztest requires matplotlib)ro   rt   r   rq   s    rA   require_matplotlibr   B  s"    
 68:TUV_``r@   c                 D    [         R                  " [        S5      " U 5      $ )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)ro   rt   _atleast_one_tracker_availablerq   s    rA   require_trackersr   P  s'    
 &c  r@   c                 L    [         R                  " [        5       S5      " U 5      $ )z
Decorator marking a test that requires torchdata.stateful_dataloader.

These tests are skipped when torchdata with stateful_dataloader module isn't installed.

z+test requires torchdata.stateful_dataloader)ro   rt   r-   rq   s    rA   %require_torchdata_stateful_dataloaderr   [  s(     246c r@   c                 n    [        5       (       a%  SSKnUR                  R                  S5      " U 5      $ U $ )a  
Decorator marking a test with order(1). When pytest-order plugin is installed, tests marked with this decorator are
guaranteed to run first.

This is especially useful in some test settings like on a Gaudi instance where a Gaudi device can only be used by a
single process at a time. So we make sure all tests that run in a subprocess are launched first, to avoid device
allocation conflicts.

If pytest is not installed, test will be returned as is.
r   Nr<   )r$   pytestmarkorder)rr   r   s     rA   	run_firstr   g  s0     {{  #I..r@   c                   B    \ rS rSrSrSr\S 5       r\S 5       rS r	Sr
g)	TempDirTestCaseiz  at  
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 destroys 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                 J    [        [        R                  " 5       5      U l        g)zECreates a `tempfile.TemporaryDirectory` and stores it in `cls.tmpdir`N)r   tempfilemkdtemptmpdirclss    rA   
setUpClassTempDirTestCase.setUpClass  s     (**,-
r@   c                     [         R                  R                  U R                  5      (       a!  [        R
                  " U R                  5        gg)z1Remove `cls.tmpdir` after test suite has finishedN)rd   pathexistsr  shutilrmtreer  s    rA   tearDownClassTempDirTestCase.tearDownClass  s0     77>>#**%%MM#**% &r@   c                    U R                   (       aw  U R                  R                  S5       HW  nUR                  5       (       a  UR	                  5         M*  UR                  5       (       d  MA  [        R                  " U5        MY     gg)z<Destroy all contents in `self.tmpdir`, but not `self.tmpdir`z**/*N)clear_on_setupr  globis_fileunlinkis_dirr  r  )selfr  s     rA   setUpTempDirTestCase.setUp  sT    ((0<<>>KKM[[]]MM$'	 1 r@   r?   N)__name__
__module____qualname____firstlineno____doc__r  classmethodr  r  r  __static_attributes__r?   r@   rA   r  r  z  s9     N. . & &
(r@   r  c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )AccelerateTestCasei  z
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                 N   > [         TU ]  5         [        R                  " S5        g )NT)supertearDownr
   _reset_state)r  	__class__s    rA   r%  AccelerateTestCase.tearDown  s    %%d+r@   r?   )r  r  r  r  r  r%  r   __classcell__)r'  s   @rA   r"  r"    s    , ,r@   r"  c                   ^    \ rS rSrSrS\\R                  \\R                     4   4S jr	Sr
g)MockingTestCasei  au  
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                     [        U[        [        45      (       a  UOU/U l        U R                   H.  nUR	                  5         U R                  UR                  5        M0     g)a!  
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)rY   tuplelistr,  start
addCleanupstop)r  r,  ms      rA   	add_mocksMockingTestCase.add_mocks  sF     )>>UUG
AGGIOOAFF# r@   )r,  N)r  r  r  r  r  r   r   Mockr/  r4  r   r?   r@   rA   r+  r+    s)    "$uTYYTYY%?@ $r@   r+  c                 F   [        5       nU S    R                  5       R                  UR                  5      n [	        U 5      R                  5       nU S   R                  5       n [        UR                  S   5       H#  n[        R                  " X#   U 5      (       a  M#    g   g)Nr   FT)
r
   clonetodevicer   rL   rangeshaperN   equal)tensorstatetensorsis       rA   are_the_same_tensorsrB    s    ED\!$$U\\2FVn  "GAY]]_F7==#${{7:v.. % r@   c                       \ rS rSrS rSrg)
_RunOutputi  c                 (    Xl         X l        X0l        g N)
returncodestdoutstderr)r  rG  rH  rI  s       rA   __init___RunOutput.__init__  s    $r@   )rG  rI  rH  N)r  r  r  r  rJ  r   r?   r@   rA   rD  rD    s    r@   rD  c                 f   #     U R                  5       I S h  vN nU(       a	  U" U5        Og M+   N7frF  )readline)streamcallbacklines      rA   _read_streamrQ    s,     
__&&TN &s   1/1c           
      h  ^^^^	#    U(       a  [        SSR                  U 5      5        [        R                  " U S   /U SS  Q7U[        R                  R
                  [        R                  R
                  US.6I S h  vN n/ m/ mS
U4S jjm	[        R                  " [        R                  " [        UR                  UU	4S j5      5      [        R                  " [        UR                  UU	4S j5      5      /US	9I S h  vN   [        UR                  5       I S h  vN TT5      $  N N) N7f)Nz

Running:  r   r<   )stdinrH  rI  envc                    > U R                  S5      R                  5       n UR                  U 5        T(       d  [        X0US9  g g )Nutf-8)file)decoderstripr[   print)rP  sinkpipelabelquiets       rA   tee_stream_subprocess.<locals>.tee  s8    {{7#**,D%D) r@   c                 2   > T" U T[         R                  SS9$ )Nzstdout:r^  )sysrH  )loutr`  s    rA   rB   $_stream_subprocess.<locals>.<lambda>      QSZZ_hAir@   c                 2   > T" U T[         R                  SS9$ )Nzstderr:rc  )rd  rI  )re  errr`  s    rA   rB   rg    rh  r@   )timeout) )r[  joinasynciocreate_subprocess_exec
subprocessPIPEwaitcreate_taskrQ  rH  rI  rD  )
cmdrU  rT  rk  r_  echoprj  rf  r`  s
       `  @@@rA   _stream_subprocessrw    s     mSXXc]+,,A	QR !!&&!!&& 	A" C
C* ,,QXX7i jkQXX7i jk	
    AFFHnc3//E	6 %s7   A9D2?D, BD2D.D2!D0"D2.D20D2rt  c                 h   [        U 5       H)  u  pg[        U[        5      (       d  M  [        U5      X'   M+     [        R
                  " [        XX#XES95      nSR                  U 5      n	UR                  S:  a9  SR                  UR                  5      n
[        SU	 SUR                   SU
 35      eU$ )N)rU  rT  rk  r_  ru  rS  r   
'z' failed with returncode z,

The combined stderr from workers follows:
)	enumeraterY   r   strrn  runrw  rm  rG  rI  RuntimeError)rt  rU  rT  rk  r_  ru  rA  cresultcmd_strrI  s              rA   execute_subprocess_asyncr    s    #aVCF  [[+C^copFhhsmG16==)y1&2C2C1D E::@C
 	

 Mr@   c                      [         R                  R                  SS5      n [        R                  " SSU S[        R
                  5      n [        U 5      $ )z
Returns an int value of worker's numerical id under `pytest-xdist`'s concurrent workers `pytest -n N` regime, or 0
if `-n 1` or `pytest-xdist` isn't being used.
PYTEST_XDIST_WORKERgw0z^gwrl  r   )rd   re   getresubMint)workers    rA   pytest_xdist_worker_idr     s=    
 ZZ^^159FVVFB2440Fv;r@   c                  "    Sn [        5       nX-   $ )z
Returns a port number that can be fed to `torch.distributed.launch`'s `--master_port` argument.

Under `pytest-xdist` it adds a delta number based on a worker id so that concurrent tests don't try to use the same
port at once.
i<s  )r  )port
uniq_deltas     rA   get_torch_dist_unique_portr  *  s     D')Jr@   c                       \ rS rSrSrg)SubprocessCallExceptioni6  r?   N)r  r  r  r  r   r?   r@   rA   r  r  6  s    r@   r  r]   c                     [        U 5       H)  u  p4[        U[        5      (       d  M  [        U5      X'   M+     Uc  [        R
                  R                  5       n [        R                  " U [        R                  US9nU(       a$  [        US5      (       a  UR                  S5      nU$ g! [        R                   a>  n[        SSR                  U 5       SUR                  R                  5        35      UeSnAff = f)z
Runs `command` with `subprocess.check_output` and will potentially return the `stdout`. Will also properly capture
if an error occurred while running `command`
N)rI  rU  rY  rW  z	Command `rS  z$` failed with the following error:

)r{  rY   r   r|  rd   re   copyrp  check_outputSTDOUThasattrrY  CalledProcessErrorr  rm  output)r]   return_stdoutrU  rA  r  r  es          rA   run_commandr  :  s     '"aQGJ # {jjoo	((9J9JPSTvx((w/M  (( %)**PQRQYQYQ`Q`QbPcd
	s   AB+ +C=?9C88C=
componentsc                  z    [        [        R                  " [        5      5      R                  nUR
                  " U 6 $ )z
Get a path within the `accelerate` package's directory.

Args:
    *components: Components of the path to join after the package directory.

Returns:
    `Path`: The path to the requested file or directory.
)r   inspectgetfilerU   parentjoinpath)r  accelerate_package_dirs     rA   path_in_accelerate_packager  Q  s/     "'//*"=>EE!**J77r@   exception_classmsgc              #     #    Sn Sv   SnU(       a  [	        SU  S	35      eg! [          aY  n[        X05      (       d   SU  S[        U5       35       eUb&  U[        U5      ;   d   SU S[        U5       S35       e SnANuSnAff = f7f)
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 'rz  z but ran without issue.)	ExceptionrY   typer|  AssertionError)r  r  was_ranr  s       rA   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+   B	# B	
BAB<B	BB	c                     [         R                  " 5       n[        R                  n U[        l        U " U0 UD6   U[        l        UR                  5       $ ! [         a  nUeSnAff = f! U[        l        f = f)zX
Takes in a `func` with `args` and `kwargs` and returns the captured stdout as a string
N)ioStringIOrd  rH  r  getvalue)funcargsr\   captured_outputoriginal_stdoutr  s         rA   capture_call_outputr  s  sj     kkmOjjO%$
df %
##%%	   %
s#   A 
A' A""A''A* *A7)F)NN)NNNFF)NN   FT)FNrF  )rn  r  r  rd   r  r  rp  rd  r  ro   
contextlibr   	functoolsr   pathlibr   typingr   r   r   rN   rU   r?  r
   utilsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   rR   ry   rO   memory_allocated_funcr/  r`   DEFAULT_LAUNCH_COMMANDrl   ru   rp   rv   rz   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   anyr   r   r   r   TestCaser  r"  r+  rB  rD  rQ  rw  r  r  r  r  r  r|  r  r  rZ   r  r  r?   r@   rA   <module>r     s&     	 	 	   
   %   "    $- - - - - - - - - - - -`#6 5@M 1l1D & ,,Y\]   &j%@8
K]XwUn\\]Y$sTVVT^riqaoeqhjqa`lhg_xcW[]S[[[YYrir[a 		79;O;QSgSijk $!## 	&(h'' (B
,** 
,$h'' $B &0bl &0R$ fp &		i 	c .8C 8D 8 ei ehsm et e e$&r@   