
    im3                        d dl Z d dlZd dlmZmZ d dlZd dlmZ d dlm	Z	 ddl
mZmZmZmZ  e       s ed      ej                  j                   d<   ee	eedf   Zdd	Zd d
eddfdZd d
eddfdZd d
edeeef   fdZd d
edeeef   fdZd d
edefdZd d
edefdZd d
edefdZd d
edefdZd d
edeeef   fdZd d
ede fdZ!d de d
eddfdZ" G d d      Z# G d de#      Z$de#ddfdZ%de#fdZ&g dZ'y)!    N)AnyUnion)_dummy_type)Device   )_get_device_index_is_compiled
_lazy_initis_initialized_xpu_XPUAllocatorreturnc                  V    t               rt        j                  j                          yy)aZ  Release all unoccupied cached memory currently held by the caching
    allocator so that those can be used in other XPU application.

    .. note::
        :func:`~torch.xpu.empty_cache` doesn't increase the amount of XPU
        memory available for PyTorch. However, it may help reduce fragmentation
        of XPU memory in certain cases.
    N)r   torch_C_xpu_emptyCache     J/var/www/html/engine/venv/lib/python3.12/site-packages/torch/xpu/memory.pyempty_cacher      s       " r   devicec                 Z    t        | d      } t        j                  j                  |       S )a  Reset the "peak" stats tracked by the XPU memory allocator.

    See :func:`~torch.xpu.memory_stats` for details. Peak stats correspond to the
    `"peak"` key in each individual stat dict.

    Args:
        device (torch.device or int or str, optional): selected device. Returns
            statistic for the current device, given by :func:`~torch.xpu.current_device`,
            if :attr:`device` is ``None`` (default).
    Toptional)r   r   r   _xpu_resetPeakMemoryStatsr   s    r   reset_peak_memory_statsr       s%     v5F88--f55r   c                 Z    t        | d      } t        j                  j                  |       S )a  Reset the "accumulated" (historical) stats tracked by the XPU memory allocator.

    See :func:`~torch.xpu.memory_stats` for details. Accumulated stats correspond to
    the `"allocated"` and `"freed"` keys in each individual stat dict.

    Args:
        device (torch.device or int or str, optional): selected device. Returns
            statistic for the current device, given by :func:`~torch.xpu.current_device`,
            if :attr:`device` is ``None`` (default).
    Tr   )r   r   r    _xpu_resetAccumulatedMemoryStatsr   s    r   reset_accumulated_memory_statsr   /   s%     v5F8844V<<r   c                 r    t               si S t        | d      } t        j                  j	                  |       S )zLReturn the result of :func:`~torch.xpu.memory_stats` as a nested dictionary.Tr   )r   r   r   r   _xpu_memoryStatsr   s    r   memory_stats_as_nested_dictr"   >   s.    	v5F88$$V,,r   c                     g dt         dt        ddffdt        |       } d|       j                          t	        j
                        S )a@  Return a dictionary of XPU memory allocator statistics for a given device.

    The return value of this function is a dictionary of statistics, each of
    which is a non-negative integer.

    Core statistics:

    - ``"allocated_bytes.{all,large_pool,small_pool}.{current,peak,allocated,freed}"``:
      amount of allocated memory.
    - ``"reserved_bytes.{all,large_pool,small_pool}.{current,peak,allocated,freed}"``:
      amount of reserved memory.
    - ``"active_bytes.{all,large_pool,small_pool}.{current,peak,allocated,freed}"``:
      amount of active memory.
    - ``"requested_bytes.{all,large_pool,small_pool}.{current,peak,allocated,freed}"``:
      memory requested by client code, compare this with allocated_bytes to check if
      allocation rounding adds too much overhead.

    For these core statistics, values are broken down as follows.

    Pool type:

    - ``all``: combined statistics across all memory pools.
    - ``large_pool``: statistics for the large allocation pool (for size >= 1MB allocations).
    - ``small_pool``: statistics for the small allocation pool (for size < 1MB allocations).

    Metric type:

    - ``current``: current value of this metric.
    - ``peak``: maximum value of this metric.
    - ``allocated``: historical total increase in this metric.
    - ``freed``: historical total decrease in this metric.

    Args:
        device (torch.device or int or str, optional): selected device. Returns
            statistics for the current device, given by :func:`~torch.xpu.current_device`,
            if :attr:`device` is ``None`` (default).
    prefixobjr   Nc                     t        |t              r8t        |       dkD  r| dz  } |j                         D ]  \  }} | |z   |        y j	                  | |f       y )Nr   .)
isinstancedictlenitemsappend)r$   r%   kv_recurse_add_to_resultresults       r   r/   z,memory_stats.<locals>._recurse_add_to_resultn   sZ    c4 6{Q#		 61&vz156 MM63-(r   r    )strr   r"   sortcollectionsOrderedDict)r   statsr/   r0   s     @@r   memory_statsr7   F   sU    L F)s ) ) ) (v6E2u%
KKM""6**r   c                 :    t        |       j                  dd      S )a  Return the current GPU memory occupied by tensors in bytes for a given device.

    Args:
        device (torch.device or int or str, optional): selected device. Returns
            statistic for the current device, given by :func:`~torch.xpu.current_device`,
            if :attr:`device` is ``None`` (default).

    .. note::
        This is likely less than the amount shown in `xpu-smi` since some
        unused memory can be held by the caching allocator and some context
        needs to be created on GPU.
    r   zallocated_bytes.all.currentr   r7   getr   s    r   memory_allocatedr;   ~   s     v&**+H!LLr   c                 :    t        |       j                  dd      S )a  Return the maximum GPU memory occupied by tensors in bytes for a given device.

    By default, this returns the peak allocated memory since the beginning of
    this program. :func:`~torch.xpu.reset_peak_memory_stats` can be used to
    reset the starting point in tracking this metric. For example, these two
    functions can measure the peak allocated memory usage of each iteration in a
    training loop.

    Args:
        device (torch.device or int or str, optional): selected device. Returns
            statistic for the current device, given by :func:`~torch.xpu.current_device`,
            if :attr:`device` is ``None`` (default).
    r   zallocated_bytes.all.peakr   r9   r   s    r   max_memory_allocatedr=      s     v&**+EqIIr   c                 :    t        |       j                  dd      S )aJ  Return the current GPU memory managed by the caching allocator in bytes for a given device.

    Args:
        device (torch.device or int or str, optional): selected device. Returns
            statistic for the current device, given by :func:`~torch.xpu.current_device`,
            if :attr:`device` is ``None`` (default).
    r   zreserved_bytes.all.currentr   r9   r   s    r   memory_reservedr?      s     v&**+GKKr   c                 :    t        |       j                  dd      S )a  Return the maximum GPU memory managed by the caching allocator in bytes for a given device.

    By default, this returns the peak cached memory since the beginning of this
    program. :func:`~torch.xpu.reset_peak_memory_stats` can be used to reset
    the starting point in tracking this metric. For example, these two functions
    can measure the peak cached memory amount of each iteration in a training
    loop.

    Args:
        device (torch.device or int or str, optional): selected device. Returns
            statistic for the current device, given by :func:`~torch.xpu.current_device`,
            if :attr:`device` is ``None`` (default).
    r   zreserved_bytes.all.peakr   r9   r   s    r   max_memory_reservedrA      s     v&**+DaHHr   c                 n    t                t        | d      } t        j                  j	                  |       S )a  Return the global free and total GPU memory for a given device.

    Args:
        device (torch.device or int or str, optional): selected device. Returns
            statistic for the current device, given by :func:`~torch.xpu.current_device`,
            if :attr:`device` is ``None`` (default).

    Returns:
        int: the memory available on the device in units of bytes.
        int: the total memory on the device in units of bytes
    Tr   )r
   r   r   r   _xpu_getMemoryInfor   s    r   mem_get_inforD      s*     Lv5F88&&v..r   c                 n    t                t        | d      } t        j                  j	                  |       S )ab  
    Retrieve the memory fraction currently set for a process on a given XPU device.
    This fraction represents the portion of the total device memory that
    the caching allocator is allowed to use. The allowed memory is calculated as:

    .. math:: \text{allowed\_memory} = \text{total\_memory} \times \text{fraction}

    Args:
        device (torch.device or int or str, optional): selected device. It uses the current device,
            given by :func:`~torch.xpu.current_device`, if :attr:`device` is ``None`` (default).

    Returns:
        float: The memory fraction in the range 0.0 to 1.0.
    Tr   )r
   r   r   r   _xpu_getMemoryFractionr   s    r   get_per_process_memory_fractionrG      s*     Lv5F88**622r   fractionc                     t                t        |d      }t        | t              st	        d      t
        j                  j                  | |       y)a=  
    Set the memory fraction for a single process on XPU device.
    This function limits the amount of memory that the caching allocator can allocate
    on the specified XPU device. The allowed memory is computed as:

    .. math:: \text{allowed\_memory} = \text{total\_memory} \times \text{fraction}

    If the process attempts to allocate more than this allowed memory,
    an out-of-memory error will be raised by the allocator.

    Arguments:
        fraction (float): Range: 0~1. Allowed memory equals total_memory * fraction.
        device (torch.device or int or str, optional): selected device. It uses the current device,
            given by :func:`~torch.xpu.current_device`, if :attr:`device` is ``None`` (default).

    .. note:: In general, the total available free memory is less than the total capacity.
    Tr   z3Invalid type for fraction argument, must be `float`N)r
   r   r(   float	TypeErrorr   r   _xpu_setMemoryFraction)rH   r   s     r   set_per_process_memory_fractionrM      s?    $ Lv5Fh&MNN	HH##Hf5r   c                   J    e Zd ZdZdej
                  j                  fdZd Zy)_XPUAllocatorz,Wrapper over internal XPU memory allocators.	allocatorc                     || _         y N
_allocator)selfrP   s     r   __init__z_XPUAllocator.__init__   s	    #r   c                     | j                   S rR   rS   )rU   s    r   rP   z_XPUAllocator.allocator   s    r   N)	__name__
__module____qualname____doc__r   r   r   rV   rP   r   r   r   rO   rO      s     7$%(("<"< $r   rO   c                   $    e Zd ZdZdededefdZy)XPUPluggableAllocatorz2XPU memory allocator loaded from a shared library.path_to_lib_filealloc_fn_namefree_fn_namec                 ~   t        j                  |      }t        ||      }t        ||      }t        j                  |t         j                        j
                  }t        j                  |t         j                        j
                  }||t        d      t        j                  j                  ||      | _
        y)a  XPU memory allocator loaded dynamically from a shared library.

        This lets users provide custom allocation and free functions implemented
        in a separate shared library. The allocator is registered through
        ``torch._C._xpu_customAllocator`` and becomes available for use via
        ``torch.memory.xpu.change_current_allocator``.

        Arguments:
            path_to_lib_file (str):
                Filesystem path to the shared library file containing the allocation
                and free functions.
            alloc_fn_name (str):
                Name of the allocation function exported from the shared library.
                The function must have the signature:

                    ``void* alloc_fn(size_t size, int device, sycl::queue* queue);``

            free_fn_name (str):
                Name of the free function exported from the shared library.
                The function must have the signature:

                    ``void free_fn(void* ptr, size_t size, sycl::queue* queue);``
        Nz9Failed to load allocator symbols from the shared library.)ctypesCDLLgetattrcastc_void_pvalueRuntimeErrorr   r   _xpu_customAllocatorrT   )	rU   r^   r_   r`   allocator_liballoc_fn_ptrfree_fn_ptralloc_fn_addrfree_fn_addrs	            r   rV   zXPUPluggableAllocator.__init__  s    0 $45}m<m\:L&//BHH{{;@FF L$8K   ((77|Tr   N)rX   rY   rZ   r[   r2   rV   r   r   r   r]   r]     s$    =%U %US %UPS %Ur   r]   rP   c                 ^    t         j                  j                  | j                                y)a  Change the currently used memory allocator to be the one provided.

    .. note::
        If the current allocator has already been used/initialized, this function will error.

    Arguments:
        allocator (torch.xpu.memory._XPUAllocator): allocator to be set as the active one.
    N)r   r   _xpu_changeCurrentAllocatorrP   )rP   s    r   change_current_allocatorrq   /  s     
HH(()<)<)>?r   c                  P    t        t        j                  j                               S )zxReturn the allocator being currently used.

    Returns:
        _XPUAllocator: the allocator being currently used.
    )rO   r   r   _xpu_getAllocatorr   r   r   _get_current_allocatorrt   ;  s     33566r   )r]   rq   r   rG   r=   rA   rD   r;   r?   r7   r"   r   r   rM   )r   NrR   )(r4   rb   typingr   r   r   torch._utilsr   torch.typesr   r1   r   r	   r
   r   r   __dict__r2   int	_device_tr   r   r   r)   r"   r7   r;   r=   r?   rA   tuplerD   rJ   rG   rM   rO   r]   rq   rt   __all__r   r   r   <module>r}      s       $  I I ~-89L-MEHH)*&#sD()	
#6I 6 6=9 = =-	 -T#s(^ -5+ 5+d38n 5+pMY M# M J Jc J"LI L LI	 IS I"/ /eCHo /"3I 3 3(6e 6Y 6RV 64 (UM (UV	@ 	@$ 	@7 7r   