
    ;if                         S 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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  SSKJr  SS jrS	 rS
\S\4S jr   SS\\   S\S\\   4S jjrS\4S jrg)z
A collection of utilities for ensuring that training can always occur. Heavily influenced by the
[toma](https://github.com/BlackHC/toma) library.
    N)Optional)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                    U (       a  [         R                  " 5         [        5       (       a  [        R                  R                  5         g[        5       (       a  [        R                  R                  5         g[        5       (       a  [        R                  R                  5         g[        5       (       a  [        R                  R                  5         g[        5       (       a  [        R                  R                  5         g[        SS9(       a  [        R                  R                  5         g[!        5       (       a  [        R"                  R                  5         g[%        5       (       a  gg)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    g/home/dmtnaga/Documents/work/airagagent/rag_env/lib/python3.13/site-packages/accelerate/utils/memory.pyclear_device_cacher    ,   s    
 


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

 			

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

 			 
    c                      [        U [        5      (       d  [        U 5      n [        [        U 5      5       H  nSX'   M	     [	        SS9  U $ )a  
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)   G   sA    , gt$$w-3w< 
 !$/Nr!   	exceptionreturnc                    ^  / SQn[        T [        5      (       a.  [        T R                  5      S:X  a  [	        U 4S jU 5       5      $ g)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   F   >#    U  H  oTR                   S    ;   v   M     g7f)r   N)args).0errr*   s     r   	<genexpr>+should_reduce_batch_size.<locals>.<genexpr>t   s     C{)..++{s   !F)r#   RuntimeErrorr&   r.   any)r*   _statementss   ` r   should_reduce_batch_sizer6   e   s=    K )\**s9>>/Ba/GC{CCCr!   functionstarting_batch_sizereduce_batch_size_fnc                 j   ^ ^^ T c  [         R                  " [        US9$ UmTc  U4S jmUU U4S jnU$ )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)
```
)r8   c                  $   > [        T S-  5      m T $ )Ng?)int)
batch_sizes   r   r9   8find_executable_batch_size.<locals>.reduce_batch_size_fn   s    Z#-.Jr!   c            	      @  > [        SS9  [        [        R                  " T5      R                  R                  5       5      n[        U5      [        U 5      S-   :  ad  SR                  [        USS  U SS  5       VVs/ s H  u  p4U SU 3PM     snn5      n[        STR                   STR                   SU S	35      e TS
:X  a  [        S5      e T" T/U Q70 UD6$ s  snnf ! [         a+  n[        U5      (       a  [        SS9  T	" 5       m S nAO	e S nAff = fM^  )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__r3   	Exceptionr6   )
r.   kwargsparamsargvaluearg_strer=   r7   r9   s
          r   	decorator-find_executable_batch_size.<locals>.decorator   s3   d3g''1<<AACDv;#d)a-(iiCqr
TXYZY[T\D] ^D]jcC5%!1D] ^_G.x/@/@.A BBBJBSBSATTUV]U^^`b  Q"#RSS
<T<V<< !_  +A..&$?!5!7J s$   =C 
C& &
D0 DDD)	functoolspartialfind_executable_batch_size)r7   r8   r9   rQ   r=   s   ` ` @r   rU   rU   x   s=    >   !;Qdee$J#	
. r!   device_indexc                 <   [         R                  " [        R                  5      R                  [         R                  " S5      R                  :  a#   [        R
                  R                  U 5      S   $ [        5       (       aV  [         R                  " [        R                  R                  S5      5      n[        USS5      (       a  SSKJn  U" U 5      S   $ [        R                  " S5        [        R
                  R                  U 5      $ ! [         a     NAf = f)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   rY   rJ   r   	importlibmetadatar   intel_extension_for_pytorch.xpuwarningswarnmax_memory_allocated)rV   ipex_versionrY   s      r   get_xpu_available_memoryrd      s    }}U&&'//7==3G3O3OO	99)),7:: 
		}}Y%7%7%?%?@]%^_L$66D-a00MM 	F 99)),77  		s   !D 
DD)F)N   N) __doc__rS   r   r]   rB   r`   typingr   r   	packagingr   importsr   r   r   r	   r
   r   r   r   r   versionsr   r    r)   rJ   boolr6   callabler<   rU   rd    r!   r   <module>rn      s   
  	      
 
 
 '6<	 d ( $("/3Ax AA #8,AH83 8r!   