§
    Ì²Åj9   ã                   óD  — d dl Z d dlmZ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ddedeeef         fd„Zg d¢ZdS )é    N)ÚAnyÚDictÚTupleÚ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© ó    úAC:\Projects\Historical\venv\Lib\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Ô5€FÝŒ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Ô5€FÝŒ8×4Ò4°VÑ<Ô<Ð<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Ô5€FÝŒ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Š   ø€ Ý�c�4Ñ Ô ð 	)Ý�6‰{Œ{˜QŠˆØ˜#‘�ØŸ	š	™œð 6ð 6‘��1Ø&Ð& v°¡z°1Ñ5Ô5Ð5Ð5ð6ð 6ð �MŠM˜6 3˜-Ñ(Ô(Ð(Ð(Ð(r   r   Ú )Ústrr   r    ÚsortÚcollectionsÚOrderedDict)r   Ústatsr-   r.   s     @@r   Úmemory_statsr5   @   s‡   øø€ ðL €Fð)¥sð )µð )¸ð )ð )ð )ð )ð )ð )ð )õ (¨vÐ6Ñ6Ô6€EØÐ˜2˜uÑ%Ô%Ð%Ø
‡K‚K�M„M€MåÔ" 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   ©r5   Úgetr   s    r   Úmemory_allocatedr9   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   r7   r   s    r   Úmax_memory_allocatedr;   ˆ   s&   € õ ˜vÐ&Ñ&Ô&×*Ò*Ð+EÀqÑ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   r7   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   r7   r   s    r   Úmax_memory_reservedr?   ¤   s&   € õ ˜vÐ&Ñ&Ô&×*Ò*Ð+DÀaÑHÔHÐHr   c                 ób   — t          | d¬¦  «        } t          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   Ú_xpu_getMemoryInfor   s    r   Úmem_get_inforB   µ   s,   € õ ˜v°Ð5Ñ5Ô5€FÝŒ8×&Ò& vÑ.Ô.Ð.r   )
r   r;   r?   rB   r9   r=   r5   r    r   r   )r   N)N)r2   Útypingr   r   r   r   r   Útorch.typesr   r/   r	   r
   r0   ÚintÚ	_device_tr   r   r   r    r5   r9   r;   r=   r?   rB   Ú__all__r   r   r   ú<module>rH      s:  ðØ Ð Ð Ð Ø *Ð *Ð *Ð *Ð *Ð *Ð *Ð *Ð *Ð *Ð *Ð *à €€€Ø Ð Ð Ð Ð Ð à /Ð /Ð /Ð /Ð /Ð /Ð /Ð /ð �&˜#˜s DÐ(Ô)€	ð
#ð 
#ð 
#ð 
#ð6ð 6 Ið 6¸ð 6ð 6ð 6ð 6ð=ð =¨9ð =Àð =ð =ð =ð =ð-ð -¨	ð -¸TÀ#ÀsÀ(¼^ð -ð -ð -ð -ð5+ð 5+˜ð 5+¨d°3¸°8¬nð 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ð"/ð /˜ð /¨e°C¸°H¬oð /ð /ð /ð /ð ð ð €€€r   