
    Χg                        d dl Z d dlmZmZmZ d dlZd dlmZ ddlm	Z	m
Z
 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g dZdS )    N)AnyDictUnion)Device   )_get_device_indexis_initializedreturnc                  b    t                      r t          j                                         dS dS )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     L/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/xpu/memory.pyempty_cacher      s6      #  """""# #r   devicec                 b    t          | d          } t          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555F8--f555r   c                 b    t          | d          } t          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555F844V<<<r   c                     t                      si S t          | d          } t          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   8   s=     	v555F8$$V,,,r   c                     g dt           dt          ddffdt          |           } d|                                            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                    rCt          |           dk    r| dz  } |                                D ]\  }} | |z   |           d S                     | |f           d S )Nr   .)
isinstancedictlenitemsappend)r!   r"   kv_recurse_add_to_resultresults       r   r,   z,memory_stats.<locals>._recurse_add_to_resulth   s    c4   	)6{{Q#		 6 61&&vz155556 6 MM63-(((((r   r    )strr   r   sortcollectionsOrderedDict)r   statsr,   r-   s     @@r   memory_statsr4   @   s    L F)s ) ) ) ) ) ) ) ) ) (v666E2u%%%
KKMMM"6***r   c                 J    t          |                               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   r4   getr   s    r   memory_allocatedr8   x   s&     v&&&**+H!LLLr   c                 J    t          |                               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   r6   r   s    r   max_memory_allocatedr:      s&     v&&&**+EqIIIr   c                 J    t          |                               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   r6   r   s    r   memory_reservedr<      s&     v&&&**+GKKKr   c                 J    t          |                               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   r6   r   s    r   max_memory_reservedr>      s&     v&&&**+DaHHHr   )	r   r:   r>   r8   r<   r4   r   r   r   )r
   N)N)r1   typingr   r   r   r   torch.typesr   r.   r   r	   r/   int	_device_tr   r   r   r   r4   r8   r:   r<   r>   __all__r   r   r   <module>rD      s       # # # # # # # # # #        / / / / / / / / &#sD()	
# 
# 
# 
#6 6I 6 6 6 6 6= =9 = = = = =- -	 -T#s(^ - - - -5+ 5+ 5+d38n 5+ 5+ 5+ 5+pM MY M# M M M M J J Jc J J J J"L LI L L L L LI I	 IS I I I I"
 
 
r   