o
    zi!                     @   s   d Z ddlZddlmZ ddlZddlmZ ddlmZ ddeded	efd
dZ	de
d	efddZde
d	efddZde
d	efddZde
d	efddZdddZdS )zUtilities related to memory.    N)Any)apply_to_collection)TensorFin_dictto_cpureturnc                 C   s&   dt dtdt fdd}t| t ||dS )a  Detach all tensors in `in_dict`.

    May operate recursively if some of the values in `in_dict` are dictionaries
    which contain instances of `Tensor`. Other types in `in_dict` are
    not affected by this utility function.

    Args:
        in_dict: Dictionary with tensors to detach
        to_cpu: Whether to move tensor to cpu

    Return:
        out_dict: Dictionary with detached tensors

    tr   r   c                 S   s   |   } |r
|  } | S N)detachcpu)r   r    r   V/home/ubuntu/.local/lib/python3.10/site-packages/pytorch_lightning/utilities/memory.pydetach_and_move(   s   z)recursive_detach.<locals>.detach_and_move)r   )r   boolr   )r   r   r   r   r   r   recursive_detach   s   r   	exceptionc                 C   s   t | pt| pt| S r	   )is_cuda_out_of_memoryis_cudnn_snafuis_out_of_cpu_memoryr   r   r   r   is_oom_error1   s   r   c                 C   s4   t | tot| jdkod| jd v od| jd v S )N   CUDAr   zout of memory
isinstanceRuntimeErrorlenargsr   r   r   r   r   6   s   
r   c                 C   &   t | tot| jdkod| jd v S )Nr   z(cuDNN error: CUDNN_STATUS_NOT_SUPPORTED.r   r   r   r   r   r   r   @   s
   
r   c                 C   r   )Nr   z*DefaultCPUAllocator: can't allocate memoryr   r   r   r   r   r   r   J   s
   
r   c               
   C   sJ   t   ztj  W dS  ty$ }  zt| s W Y d} ~ dS d} ~ ww )z'Garbage collection Torch (CUDA) memory.N)gccollecttorchcudaempty_cacher   r   r   r   r   r   garbage_collection_cudaS   s   r$   )F)r   N)__doc__r   typingr   r!   #lightning_utilities.core.apply_funcr   r   r   r   BaseExceptionr   r   r   r   r$   r   r   r   r   <module>   s   

	