o
    3wit                     @   s^  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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 d dl	mZ d dlZd dlZddl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&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZA d
d ZBeB \ZCZDZEdeFfddZGeGeDddZHdddZIeIdddZJdd ZKdd ZLdd ZMdd ZNdd ZOd d! ZPd"d# ZQd$d% ZRd&d' ZSd(d) ZTd*d+ ZUd,d- ZVd.d/ ZWd0d1 ZXd2d3 ZYd4d5 ZZd6d7 Z[d8d9 Z\d:d; Z]d<d= Z^d>d? Z_d@dA Z`dBdC ZadDdE ZbdFdG ZcdHdI ZddJdK ZedLdM ZfdNdO ZgdPdQ ZhdRdS ZidTdU ZjdVdW ZkdXdY ZldZd[ Zmd\d] Zndd^d_Zod`da Zpdbdc Zqddde Zrdfdg Zsdhdi Ztdjdk Zudldm Zvdndo Zwdpdq Zxdrds Zydtdu Zzdvdw Z{dxdy Z|dzd{ Z}d|d} Z~ee? e4 e; e3 goe  Zd~d Zdd Zdd ZG dd de	jZG dd de	jZG dd de	jZdd ZG dd dZdd ZddefddZddeFdefddZdd Zdd ZG dd deZddeFe fddZdedefddZeddededefddZdd ZdS )    N)contextmanager)partial)Path)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_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_transformers_availableis_triton_availableis_wandb_availableis_xpu_availablestr_to_boolc                   C   s
  t  rdtj tjjfS t rdtj tjjfS tddr&ddtjjfS t r0dddd fS t	 r=d	tj
 tj
jfS t rJd
tj tjjfS t rWdtj tjjfS t rddtj tjjfS t rqdtj tjjfS t r~dtj tjjfS dddd fS )Nxlacudaz2.0)min_versionmps   c                   S      dS Nr    r:   r:   r:   Z/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/accelerate/test_utils/testing.py<lambda>Z       zget_backend.<locals>.<lambda>mlusdaamusanpuxpuhpucpuc                   S   r8   r9   r:   r:   r:   r:   r;   r<   h   r=   )r(   torchr4   device_countmemory_allocatedr   r   r6   current_allocated_memoryr   r>   r#   r?   r   r@   r   rA   r1   rB   r   rC   r:   r:   r:   r;   get_backendR   s*   
rI   returnc                  K   s\   ddg}|   D ]#\}}t|tr|r|d|  q|dur+|d| d|  q|S )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--N=)items
isinstanceboolappend)kwargscommandkvr:   r:   r;   get_launch_commandn   s   
rV   g?)num_processesmonitor_intervalFc                 C   sV   zt j|  }W n ty   |}Y |S w zt|}W |S  ty*   td|  dw )NzIf set, z must be yes or no.)osenvironKeyErrorr2   
ValueError)keydefaultvalue_valuer:   r:   r;   parse_flag_from_env   s   
ra   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;   rd      s   rd   c                 C      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)rc   
skipUnless_run_slow_testsre   r:   r:   r;   slow   s   rj   c                 C   s   t tdkd| S )zu
    Decorator marking a test that must be only ran on the CPU. These tests are skipped when a GPU is available.
    rD   ztest requires only a CPUrc   rh   torch_devicere   r:   r:   r;   require_cpu      rm   c                 C      t tdkd| S )z
    Decorator marking a test that requires a hardware accelerator backend. These tests are skipped when there are no
    hardware accelerator available.
    rD   test requires a GPUrk   re   r:   r:   r;   require_non_cpu      rq   c                 C      t t ot  d| S )z
    Decorator marking a test that requires CUDA. These tests are skipped when there are no GPU available or when
    TorchXLA is available.
    rp   )rc   rh   r   r(   re   r:   r:   r;   require_cuda      rt   c                 C   s    t t r	t  pt d| S )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)rc   rh   r   r(   r   re   r:   r:   r;   require_cuda_or_hpu   
   rv   c                 C      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)rc   rh   r1   re   r:   r:   r;   require_xpu      rz   c                 C   s(   t  ot  }t }t|p|d| S )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(   r1   rc   rh   )rf   cuda_conditionxpu_conditionr:   r:   r;   require_cuda_or_xpu   s   r~   c                 C   ro   )zB
    Decorator marking a test that should be skipped for XPU.
    rB   ztest requires a non-XPUrk   re   r:   r:   r;   require_non_xpu   rn   r   c                 C   ro   )zB
    Decorator marking a test that should be skipped for HPU.
    rC   ztest requires a non-HPUrk   re   r:   r:   r;   require_non_hpu   rn   r   c                 C   rx   )zj
    Decorator marking a test that requires FP16. These tests are skipped when FP16 is not supported.
    ztest requires FP16 support)rc   rh   r   re   r:   r:   r;   require_fp16      r   c                 C   s:   t  }tj rt sd}t rt rd}t|d| S )zh
    Decorator marking a test that requires FP8. These tests are skipped when FP8 is not supported.
    Fztest requires FP8 support)	r   rE   r4   is_availabler	   r   r   rc   rh   )rf   fp8_is_availabler:   r:   r;   require_fp8   s   r   c                 C   s   t tddd| S )N>=z2.5.0z$test requires FSDP2 (torch >= 2.5.0))rc   rh   r'   re   r:   r:   r;   require_fsdp2   s   r   c                 C   rx   )zn
    Decorator marking a test that requires MLU. These tests are skipped when there are no MLU available.
    ztest require a MLU)rc   rh   r   re   r:   r:   r;   require_mlu  r{   r   c                 C   rx   )zp
    Decorator marking a test that requires SDAA. These tests are skipped when there are no SDAA available.
    ztest require a SDAA)rc   rh   r#   re   r:   r:   r;   require_sdaa	  r{   r   c                 C   rx   )zp
    Decorator marking a test that requires MUSA. These tests are skipped when there are no MUSA available.
    ztest require a MUSA)rc   rh   r   re   r:   r:   r;   require_musa  r{   r   c                 C   rx   )zn
    Decorator marking a test that requires NPU. These tests are skipped when there are no NPU available.
    ztest require a NPU)rc   rh   r   re   r:   r:   r;   require_npu  r{   r   c                 C   rx   )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`)rc   rh   r   re   r:   r:   r;   require_mps  r   r   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)rc   rh   r.   r   re   r:   r:   r;   require_huggingface_suite&  s   
r   c                 C   rx   )zi
    Decorator marking a test that requires transformers. These tests are skipped when they are not.
    z&test requires the transformers library)rc   rh   r.   re   r:   r:   r;   require_transformers0  r{   r   c                 C   rx   )za
    Decorator marking a test that requires timm. These tests are skipped when they are not.
    ztest requires the timm library)rc   rh   r&   re   r:   r:   r;   require_timm7  r{   r   c                 C   rx   )zh
    Decorator marking a test that requires torchvision. These tests are skipped when they are not.
    z%test requires the torchvision library)rc   rh   r+   re   r:   r:   r;   require_torchvision>  r{   r   c                 C   rx   )zc
    Decorator marking a test that requires triton. These tests are skipped when they are not.
    z test requires the triton library)rc   rh   r/   re   r:   r:   r;   require_tritonE  r{   r   c                 C   rx   )zi
    Decorator marking a test that requires schedulefree. These tests are skipped when they are not.
    z&test requires the schedulefree library)rc   rh   r"   re   r:   r:   r;   require_schedulefreeL  r{   r   c                 C   rx   )zi
    Decorator marking a test that requires bitsandbytes. These tests are skipped when they are not.
    z&test requires the bitsandbytes library)rc   rh   r   re   r:   r:   r;   require_bnbS  r{   r   c                 C   s   t tddd| S )zp
    Decorator marking a test that requires TPUs. These tests are skipped when there are no TPUs available.
    T)check_is_tpuztest requires TPUrc   rh   r(   re   r:   r:   r;   require_tpuZ  s   r   c                 C   s   t t  d| S )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   re   r:   r:   r;   require_non_torch_xlaa  rr   r   c                 C   s   t tdko	tdkd| S )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.
    rD   r7   z)test requires a single device accelerator)rc   rh   rl   rF   re   r:   r:   r;   require_single_devicei  s
   r   c                 C      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.
    r7   rp   rc   rh   rE   r4   rF   re   r:   r:   r;   require_single_gpus  ru   r   c                 C   r   )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.
    r7   ry   rc   rh   rE   rB   rF   re   r:   r:   r;   require_single_xpu{  ru   r   c                 C   s   t tdkd| S )z
    Decorator marking a test that requires a multi-device setup. These tests are skipped on a machine without multiple
    devices.
    r7   z,test requires multiple hardware accelerators)rc   rh   rF   re   r:   r:   r;   require_multi_device  rr   r   c                 C      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.
    r7   ztest requires multiple GPUsr   re   r:   r:   r;   require_multi_gpu  ru   r   c                 C   r   )z
    Decorator marking a test that requires a multi-XPU setup. These tests are skipped on a machine without multiple
    XPUs.
    r7   ztest requires multiple XPUsr   re   r:   r:   r;   require_multi_xpu  ru   r   c                 C   s    t t st ot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 or XPUs.
    r7   z#test requires multiple GPUs or XPUs)rc   rh   r   r1   rF   re   r:   r:   r;   require_multi_gpu_or_xpu  rw   r   c                 C   rx   )z|
    Decorator marking a test that requires DeepSpeed installed. These tests are skipped when DeepSpeed isn't installed
    ztest requires DeepSpeed)rc   rh   r   re   r:   r:   r;   require_deepspeed  r{   r   c                 C   s"   t tddotdddd| S )zn
    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)rc   rh   r'   r
   re   r:   r:   r;   
require_tp  s   r   c                 C   s0   | du 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)versionr   ztest requires torch version >= )r   require_torch_min_versionrc   rh   r'   )rf   r   r:   r:   r;   r     s   r   c                 C   rx   )z
    Decorator marking a test that requires tensorboard installed. These tests are skipped when tensorboard isn't
    installed
    ztest requires Tensorboard)rc   rh   r%   re   r:   r:   r;   require_tensorboard  r   r   c                 C   rx   )zt
    Decorator marking a test that requires wandb installed. These tests are skipped when wandb isn't installed
    ztest requires wandb)rc   rh   r0   re   r:   r:   r;   require_wandb  r{   r   c                 C   rx   )zx
    Decorator marking a test that requires trackio installed. These tests are skipped when trackio isn't installed
    ztest requires trackio)rc   rh   r,   re   r:   r:   r;   require_trackio  r{   r   c                 C   rx   )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)rc   rh   r   re   r:   r:   r;   require_comet_ml  r{   r   c                 C   rx   )zp
    Decorator marking a test that requires aim installed. These tests are skipped when aim isn't installed
    ztest requires aim)rc   rh   r   re   r:   r:   r;   require_aim  r{   r   c                 C   rx   )zx
    Decorator marking a test that requires clearml installed. These tests are skipped when clearml isn't installed
    ztest requires clearml)rc   rh   r   re   r:   r:   r;   require_clearml  r{   r   c                 C   rx   )zx
    Decorator marking a test that requires dvclive installed. These tests are skipped when dvclive isn't installed
    ztest requires dvclive)rc   rh   r   re   r:   r:   r;   require_dvclive  r{   r   c                 C   rx   )zx
    Decorator marking a test that requires swanlab installed. These tests are skipped when swanlab isn't installed
    ztest requires swanlab)rc   rh   r$   re   r:   r:   r;   require_swanlab  r{   r   c                 C   rx   )zv
    Decorator marking a test that requires pandas installed. These tests are skipped when pandas isn't installed
    ztest requires pandas)rc   rh   r   re   r:   r:   r;   require_pandas  r{   r   c                 C   rx   )zv
    Decorator marking a test that requires mlflow installed. These tests are skipped when mlflow isn't installed
    ztest requires mlflow)rc   rh   r   re   r:   r:   r;   require_mlflow   r{   r   c                 C   rs   )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)rc   rh   r    r   re   r:   r:   r;   require_pippy  ru   r   c                 C   rx   )z
    Decorator marking a test that requires tuna interpreter installed. These tests are skipped when tuna isn't
    installed
    ztest requires tuna interpreter)rc   rh   r   re   r:   r:   r;   require_import_timer  r   r   c                 C   rx   )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)rc   rh   r-   re   r:   r:   r;   require_transformer_engine  r   r   c                 C   rx   )zx
    Decorator marking a test that requires torchao installed. These tests are skipped when torchao isn't installed
    ztest requires torchao)rc   rh   r)   re   r:   r:   r;   require_torchao  r{   r   c                 C   rx   )z
    Decorator marking a test that requires matplotlib installed. These tests are skipped when matplotlib isn't
    installed
    ztest requires matplotlib)rc   rh   r   re   r:   r:   r;   require_matplotlib&  r   r   c                 C   rg   )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)rc   rh   _atleast_one_tracker_availablere   r:   r:   r;   require_trackers4  s   r   c                 C   rx   )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)rc   rh   r*   re   r:   r:   r;   %require_torchdata_stateful_dataloader?  s
   r   c                 C   s"   t  rddl}|jd| S | S )a  
    Decorator marking a test with order(1). When pytest-order plugin is installed, tests marked with this decorator are
    garanteed 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   Nr7   )r!   pytestmarkorder)rf   r   r:   r:   r;   	run_firstK  s   r   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 t | _dS )zECreates a `tempfile.TemporaryDirectory` and stores it in `cls.tmpdir`N)r   tempfilemkdtemptmpdirclsr:   r:   r;   
setUpClassj  s   zTempDirTestCase.setUpClassc                 C   s"   t j| jrt| j dS dS )z1Remove `cls.tmpdir` after test suite has finishedN)rY   pathexistsr   shutilrmtreer   r:   r:   r;   tearDownClasso  s   zTempDirTestCase.tearDownClassc                 C   sD   | j r| jdD ]}| r|  q	| rt| q	dS dS )z<Destroy all contents in `self.tmpdir`, but not `self.tmpdir`z**/*N)clear_on_setupr   globis_fileunlinkis_dirr   r   )selfr   r:   r:   r;   setUpu  s   

zTempDirTestCase.setUpN)	__name__
__module____qualname____doc__r   classmethodr   r   r   r:   r:   r:   r;   r   ^  s    	

r   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d d S )NT)supertearDownr   _reset_state)r   	__class__r:   r;   r     s   
zAccelerateTestCase.tearDown)r   r   r   r   r   __classcell__r:   r:   r   r;   r     s    r   c                   @   s.   e Zd ZdZdeejeej f f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)rO   tuplelistr   start
addCleanupstop)r   r   mr:   r:   r;   	add_mocks  s
   	
zMockingTestCase.add_mocksN)	r   r   r   r   r   r   Mockr   r   r:   r:   r:   r;   r     s    "r   c                 C   s`   t  }| d   |j} t|  }| d  } t|jd D ]}t	|| | s- dS q dS )Nr   FT)
r   clonetodevicer   rD   rangeshaperE   equal)tensorstatetensorsir:   r:   r;   are_the_same_tensors  s   r   c                   @   s   e Zd Zdd ZdS )
_RunOutputc                 C   s   || _ || _|| _d S N)
returncodestdoutstderr)r   r   r   r   r:   r:   r;   __init__  s   
z_RunOutput.__init__N)r   r   r   r   r:   r:   r:   r;   r     s    r   c                    s&   	 |   I d H }|r|| nd S qr   )readline)streamcallbackliner:   r:   r;   _read_stream  s   
r   c              	      s   |rt dd|  tj| d g| dd  R |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   r7   )stdinr   r   env c                    s2   |  d } ||   st|| |d d S d S )Nutf-8)file)decoderstriprQ   print)r   sinkpipelabel)quietr:   r;   tee  s
   
z_stream_subprocess.<locals>.teec                       |  t jddS )Nzstdout:r  )sysr   l)outr
  r:   r;   r<         z$_stream_subprocess.<locals>.<lambda>c                    r  )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_subprocess  s.   
r     Tr  c              
   C   s   t | D ]\}}t|trt|| |< qt }|t| |||||d}	d| }
|	j	dkrCd|	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:
)	enumeraterO   r   strr  get_event_looprun_until_completer  r  r   r   RuntimeError)r  r   r   r  r	  r  r   cloopresultcmd_strr   r:   r:   r;   execute_subprocess_async  s"   


r+  c                  C   s*   t jdd} tdd| dtj} t| S )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^gwr   r   )rY   rZ   getresubMint)workerr:   r:   r;   pytest_xdist_worker_id  s   r4  c                  C   s   d} t  }| | S )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  )r4  )port
uniq_deltar:   r:   r;   get_torch_dist_unique_port  s   r7  c                   @   s   e Zd ZdS )SubprocessCallExceptionN)r   r   r   r:   r:   r:   r;   r8    s    r8  rS   c              
   C   s   t | D ]\}}t|trt|| |< q|du rtj }ztj| tj	|d}|r6t
|dr3|d}|W S W dS  tjyV } ztdd|  d|j  |d}~ww )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)r   r   r  r  z	Command `r   z$` failed with the following error:

)r"  rO   r   r#  rY   rZ   copyr  check_outputSTDOUThasattrr  CalledProcessErrorr8  r  output)rS   return_stdoutr   r   r'  r>  er:   r:   r;   run_command   s*   



rA  
componentsc                  G   s   t ttj}|j|  S )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getfilerK   parentjoinpath)rB  accelerate_package_dirr:   r:   r;   path_in_accelerate_package7  s   
rH  exception_classmsgc              
   c   s    d}zdV  d}W n: t yD } z.t|| s#J d|  dt| |dur:|t|v s:J d| dt| dW Y d}~nd}~ww |rOt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   typer#  AssertionError)rI  rJ  was_ranr@  r:   r:   r;   assert_exceptionF  s   "&rO  c              
   O   s^   t  }tj}z#z|t_| |i | W n ty" } z|d}~ww W |t_| S |t_w )z`
    Takes in a `func` with `args` and `kwargs` and returns the captured stdout as a string
    N)ioStringIOr  r   rK  getvalue)funcargsrR   captured_outputoriginal_stdoutr@  r:   r:   r;   capture_call_outputY  s   rW  )F)NN)NNNFF)NNr  FT)FNr   )r  rC  rP  rY   r/  r   r  r  r   rc   
contextlibr   	functoolsr   pathlibr   typingr   r   rE   rK   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-   r.   r/   r0   r1   r2   rI   rl   rF   memory_allocated_funcr   rV   DEFAULT_LAUNCH_COMMANDra   ri   rd   rj   rm   rq   rt   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   anyr   r   r   r   TestCaser   r   r   r   r   r   r  r+  r4  r7  rK  r8  r#  rA  rH  rP   rO  rW  r:   r:   r:   r;   <module>   s   .








	!!	)
