
    bi-                         d 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
mZmZmZmZmZmZmZmZ ddlmZ ddZd Zd	ed
efdZ	 ddededefdZdefdZy)z
A collection of utilities for ensuring that training can always occur. Heavily influenced by the
[toma](https://github.com/BlackHC/toma) library.
    N)version   )	is_cuda_availableis_hpu_availableis_ipex_availableis_mlu_availableis_mps_availableis_musa_availableis_npu_availableis_sdaa_availableis_xpu_available)compare_versionsc                    | rt        j                          t               rt        j                  j                          yt               rt        j                  j                          yt               rt        j                  j                          yt               rt        j                  j                          yt               rt        j                  j                          yt        d      rt        j                  j                          yt!               rt        j"                  j                          yt%               ryy)z
    Clears the device cache by calling `torch.{backend}.empty_cache`. Can also run `gc.collect()`, but do note that
    this is a *considerable* slowdown and should be used sparingly.
    z2.0)min_versionN)gccollectr   torchxpuempty_cacher   mlur   sdaar
   musar   npur	   mpsr   cudar   garbage_collections    R/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/accelerate/utils/memory.pyclear_device_cacher   +   s    
 


								

 		

 					e	,				

 		 
    c                      t        | t              st        |       } t        t        |             D ]  }d| |<   	 t	        d       | S )aN  
    Releases memory from `objects` by setting them to `None` and calls `gc.collect()` and `torch.cuda.empty_cache()`.
    Returned objects should be reassigned to the same variables.

    Args:
        objects (`Iterable`):
            An iterable of objects
    Returns:
        A list of `None` objects to replace `objects`

    Example:

        ```python
        >>> import torch
        >>> from accelerate.utils import release_memory

        >>> a = torch.ones(1000, 1000).cuda()
        >>> b = torch.ones(1000, 1000).cuda()
        >>> a, b = release_memory(a, b)
        ```
    NTr   )
isinstancelistrangelenr   )objectsis     r   release_memoryr(   F   sE    , gt$w-3w<  
$/Nr    	exceptionreturnc                      g d}t         t              r,t         j                        dk(  rt	         fd|D              S y)z
    Checks if `exception` relates to CUDA out-of-memory, XPU out-of-memory, CUDNN not supported, or CPU out-of-memory

    Args:
        exception (`Exception`):
            An exception
    )z out of memory.z(cuDNN error: CUDNN_STATUS_NOT_SUPPORTED.z*DefaultCPUAllocator: can't allocate memoryz1FATAL ERROR :: MODULE:PT_DEVMEM Allocation failedr   c              3   @   K   | ]  }|j                   d    v   yw)r   N)args).0errr)   s     r   	<genexpr>z+should_reduce_batch_size.<locals>.<genexpr>s   s     C3)..++Cs   F)r"   RuntimeErrorr%   r-   any)r)   _statementss   ` r   should_reduce_batch_sizer4   d   s:    K )\*s9>>/Ba/GC{CCCr    functionstarting_batch_sizereduce_batch_size_fnc                 f      t        j                  t        |      S |fd fd}|S )a  
    A basic decorator that will try to execute `function`. If it fails from exceptions related to out-of-memory or
    CUDNN, the batch size is multiplied by 0.9 and passed to `function`

    `function` must take in a `batch_size` parameter as its first argument.

    Args:
        function (`callable`, *optional*):
            A function to wrap
        starting_batch_size (`int`, *optional*):
            The batch size to try and fit into memory

    Example:

    ```python
    >>> from accelerate.utils import find_executable_batch_size


    >>> @find_executable_batch_size(starting_batch_size=128)
    ... def train(batch_size, model, optimizer):
    ...     ...


    >>> train(model, optimizer)
    ```
    )r6   c                  $    t         dz          S )Ng?)int)
batch_sizes   r   r7   z8find_executable_batch_size.<locals>.reduce_batch_size_fn   s    Z#-.Jr    c            	      6   t        d       t        t        j                        j                  j                               }t        |      t        |       dz   k  rbdj                  t        |dd  | dd        D cg c]  \  }}| d|  c}}      }t        dj                   dj                   d| d	      	 d
k(  rt        d      	  g| i |S c c}}w # t        $ r*}t        |      rt        d        	       n Y d }~nd }~ww xY wZ)NTr   r   z, =zBatch size was passed into `zS` as the first argument when called.Remove this as the decorator already does so: `(z)`r   z-No executable batch size found, reached zero.)r   r#   inspect	signature
parameterskeysr%   joinzip	TypeError__name__r1   	Exceptionr4   )
r-   kwargsparamsargvaluearg_strer;   r5   r7   s
          r   	decoratorz-find_executable_batch_size.<locals>.decorator   s1   d3g''1<<AACDv;#d)a-(iiCqr
TXYZY[T\D] ^jc5C5%!1 ^_G.x/@/@.A BBBJBSBSATTUV]U^^`b  Q"#RSS
<T<V<< !_  +A.&$?!5!7J  s   >C
C$ $	D- DD)	functoolspartialfind_executable_batch_size)r5   r6   r7   rN   r;   s   ` ` @r   rQ   rQ   w   s=    :   !;Qdee$J#	
. r    device_indexc                    t        j                  t        j                        j                  t        j                  d      j                  k\  r#	 t        j
                  j                  |       d   S t               rPt        j                  t        j                  j                  d            }t        |dd      rddlm}  ||       d   S t        j                  d       t        j
                  j                  |       S # t        $ r Y ?w xY w)Nz2.6r   intel_extension_for_pytorchz>=z2.5)mem_get_infozThe XPU `mem_get_info` API is available in IPEX version >=2.5 or PyTorch >=2.6. The current returned available memory is incorrect. Please consider upgrading your IPEX or PyTorch version.)r   parser   __version__releaser   rU   rG   r   	importlibmetadatar   intel_extension_for_pytorch.xpuwarningswarnmax_memory_allocated)rR   ipex_versionrU   s      r   get_xpu_available_memoryr`      s    }}U&&'//7==3G3O3OO	99)),7:: 
	}}Y%7%7%?%?@]%^_L$6D-a00MM 	F 99)),77  		s   !C? ?	D
D)F)N   N)__doc__rO   r   rY   r?   r\   r   	packagingr   importsr   r   r   r   r	   r
   r   r   r   versionsr   r   r(   rG   boolr4   callabler:   rQ   r`    r    r   <module>ri      s   
  	     
 
 
 '6<	 d ( ae??47?U]?D83 8r    