Ë
    L²jÃ*  ã                   óð   — d dl mZ d dlmZ d dlZddlmZmZ g d¢Zdd„Z	d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dfd„Zdd
eddfd„Zdd
edeeef   fd„Zy)é    )ÚOrderedDict)ÚAnyNé   )Ú	_device_tÚ_get_device_index)
Úempty_cacheÚempty_host_cacheÚget_memory_infoÚmax_memory_allocatedÚmax_memory_reservedÚmemory_allocatedÚmemory_reservedÚmemory_statsÚreset_accumulated_memory_statsÚreset_peak_memory_statsÚreturnc                  ó~   — t         j                  j                  «       syt         j                  j                  «        y)a  Release all unoccupied cached memory currently held by the caching
    allocator so that those can be used in other application.

    .. note:: This function is a no-op if the memory allocator for the current
        :ref:`accelerator <accelerators>` has not been initialized.
    N)ÚtorchÚ_CÚ#_accelerator_isAllocatorInitializedÚ_accelerator_emptyCache© ó    úW/var/www/html/ia-enhanced/venv/lib/python3.12/site-packages/torch/accelerator/memory.pyr   r      s'   € ô �8‰8×7Ñ7Ô9ØÜ	‡H�H×$Ñ$Õ&r   c                  ó@   — t         j                  j                  «        y)a*  Release all unoccupied cached host (pinned) memory currently held by the host caching
    allocator so that it can be used by other applications.

    .. note:: This function is a no-op if the memory allocator for the current
        :ref:`accelerator <accelerators>` has not been initialized.
    N)r   r   Ú_accelerator_emptyHostCacher   r   r   r	   r	   #   s   € ô 
‡H�H×(Ñ(Õ*r   Údevice_indexc                ó$  ‡‡— t         j                  j                  «       s
t        «       S t	        | d¬«      } t         j                  j                  | «      }g Šdt        dt        ddfˆˆfd„Š ‰d|«       ‰j                  «        t        ‰«      S )	a.  Return a dictionary of accelerator device memory allocator statistics for a given device index.

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

    Core statistics:

    - ``"allocated.{all,large_pool,small_pool}.{current,peak,allocated,freed}"``:
      number of allocation requests received by the memory allocator.
    - ``"allocated_bytes.{all,large_pool,small_pool}.{current,peak,allocated,freed}"``:
      amount of allocated memory.
    - ``"segment.{all,large_pool,small_pool}.{current,peak,allocated,freed}"``:
      number of reserved segments from device memory allocation.
    - ``"reserved_bytes.{all,large_pool,small_pool}.{current,peak,allocated,freed}"``:
      amount of reserved memory.
    - ``"active.{all,large_pool,small_pool}.{current,peak,allocated,freed}"``:
      number of active memory blocks.
    - ``"active_bytes.{all,large_pool,small_pool}.{current,peak,allocated,freed}"``:
      amount of active memory.
    - ``"inactive_split.{all,large_pool,small_pool}.{current,peak,allocated,freed}"``:
      number of inactive, non-releasable memory blocks.
    - ``"inactive_split_bytes.{all,large_pool,small_pool}.{current,peak,allocated,freed}"``:
      amount of inactive, non-releasable memory.

    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
      (as of June 2025, for size >= 1MB allocations).
    - ``small_pool``: statistics for the small allocation pool
      (as of June 2025, 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.

    In addition to the core statistics, we also provide some simple event
    counters:

    - ``"num_alloc_retries"``: number of failed device memory allocation calls that
      result in a cache flush and retry.
    - ``"num_ooms"``: number of out-of-memory errors thrown.
    - ``"num_sync_all_streams"``: number of ``synchronize_and_free_events`` calls.
    - ``"num_device_alloc"``: number of device memory allocation calls.
    - ``"num_device_free"``: number of device memory free calls.

    Args:
        device_index (:class:`torch.device`, str, int, optional): the index of the device to target.
            If not given, use :func:`torch.accelerator.current_device_index` by default.
            If a :class:`torch.device` or str is provided, its type must match the current
            :ref:`accelerator<accelerators>` device type.

    Returns:
        OrderedDict[str, Any]: an ordered dictionary mapping statistic names to their values.
    T©ÚoptionalÚprefixÚvaluer   Nc                 ó¦   •— t        |t        «      r-|j                  «       D ]  \  }}| r| › d|› �n|} ‰||«       Œ y ‰j                  | |f«       y )Nú.)Ú
isinstanceÚdictÚitemsÚappend)r!   r"   ÚkÚvÚnested_prefixÚ
flat_statsÚflattens        €€r   r-   zmemory_stats.<locals>.flattenp   sW   ø€ Ü�eœTÔ"ØŸ™›ò *‘��1Ù39 6 (¨!¨A¨3¡¸q�Ù˜ qÕ)ñ*ð ×Ñ˜v u˜oÕ.r   Ú )	r   r   r   r   r   Ú_accelerator_getDeviceStatsÚstrr   Úsort)r   Ústatsr,   r-   s     @@r   r   r   -   s~   ù€ ôz �8‰8×7Ñ7Ô9Ü‹}ÐÜ$ \¸DÔA€LÜ�H‰H×0Ñ0°Ó>€EØ€Jð/œð /¤Cð /¨Dö /ñ ˆB�ÔØ‡O�OÔä�zÓ"Ð"r   c                ó8   — t        | «      j                  dd«      S )aM  Return the current :ref:`accelerator<accelerators>` device memory occupied by tensors
    in bytes for a given device index.

    Args:
        device_index (:class:`torch.device`, str, int, optional): the index of the device to target.
            If not given, use :func:`torch.accelerator.current_device_index` by default.
            If a :class:`torch.device` or str is provided, its type must match the current
            :ref:`accelerator<accelerators>` device type.

    Returns:
        int: the current memory occupied by live tensors (in bytes) within the current process.
    zallocated_bytes.all.currentr   ©r   Úget©r   s    r   r   r   ~   s   € ô ˜Ó%×)Ñ)Ð*GÈÓKÐKr   c                ó8   — t        | «      j                  dd«      S )a+  Return the current :ref:`accelerator<accelerators>` maximum device memory occupied by tensors
    in bytes for a given device index.

    By default, this returns the peak allocated memory since the beginning of
    this program. :func:`~torch.accelerator.reset_peak_memory_stats` can be used to
    reset the starting point in tracking this metric.

    Args:
        device_index (:class:`torch.device`, str, int, optional): the index of the device to target.
            If not given, use :func:`torch.accelerator.current_device_index` by default.
            If a :class:`torch.device` or str is provided, its type must match the current
            :ref:`accelerator<accelerators>` device type.

    Returns:
        int: the peak memory occupied by live tensors (in bytes) within the current process.
    zallocated_bytes.all.peakr   r4   r6   s    r   r   r   Ž   s   € ô" ˜Ó%×)Ñ)Ð*DÀaÓHÐHr   c                ó8   — t        | «      j                  dd«      S )aU  Return the current :ref:`accelerator<accelerators>` device memory managed by the caching allocator
    in bytes for a given device index.

    Args:
        device_index (:class:`torch.device`, str, int, optional): the index of the device to target.
            If not given, use :func:`torch.accelerator.current_device_index` by default.
            If a :class:`torch.device` or str is provided, its type must match the current
            :ref:`accelerator<accelerators>` device type.

    Returns:
        int: the current memory reserved by PyTorch (in bytes) within the current process.
    zreserved_bytes.all.currentr   r4   r6   s    r   r   r   ¢   s   € ô ˜Ó%×)Ñ)Ð*FÈÓJÐJr   c                ó8   — t        | «      j                  dd«      S )a0  Return the current :ref:`accelerator<accelerators>` maximum device memory managed by the caching allocator
    in bytes for a given device index.

    By default, this returns the peak cached memory since the beginning of this
    program. :func:`~torch.accelerator.reset_peak_memory_stats` can be used to reset
    the starting point in tracking this metric.

    Args:
        device_index (:class:`torch.device`, str, int, optional): the index of the device to target.
            If not given, use :func:`torch.accelerator.current_device_index` by default.
            If a :class:`torch.device` or str is provided, its type must match the current
            :ref:`accelerator<accelerators>` device type.

    Returns:
        int: the peak memory reserved by PyTorch (in bytes) within the current process.
    zreserved_bytes.all.peakr   r4   r6   s    r   r   r   ²   s   € ô" ˜Ó%×)Ñ)Ð*CÀQÓGÐGr   c                óZ   — t        | d¬«      } t        j                  j                  | «      S )aˆ  Reset the "accumulated" (historical) stats tracked by the current :ref:`accelerator<accelerators>`
    memory allocator for a given device index.

    Args:
        device_index (:class:`torch.device`, str, int, optional): the index of the device to target.
            If not given, use :func:`torch.accelerator.current_device_index` by default.
            If a :class:`torch.device` or str is provided, its type must match the current
            :ref:`accelerator<accelerators>` device type.

    .. note:: This function is a no-op if the memory allocator for the current
        :ref:`accelerator <accelerators>` has not been initialized.
    Tr   )r   r   r   Ú"_accelerator_resetAccumulatedStatsr6   s    r   r   r   Æ   s%   € ô % \¸DÔA€LÜ�8‰8×6Ñ6°|ÓDÐDr   c                óZ   — t        | d¬«      } t        j                  j                  | «      S )at  Reset the "peak" stats tracked by the current :ref:`accelerator<accelerators>`
    memory allocator for a given device index.

    Args:
        device_index (:class:`torch.device`, str, int, optional): the index of the device to target.
            If not given, use :func:`torch.accelerator.current_device_index` by default.
            If a :class:`torch.device` or str is provided, its type must match the current
            :ref:`accelerator<accelerators>` device type.

    .. note:: This function is a no-op if the memory allocator for the current
        :ref:`accelerator <accelerators>` has not been initialized.
    Tr   )r   r   r   Ú_accelerator_resetPeakStatsr6   s    r   r   r   ×   s%   € ô % \¸DÔA€LÜ�8‰8×/Ñ/°Ó=Ð=r   c                óZ   — t        | d¬«      } t        j                  j                  | «      S )aË  Return the current device memory information for a given device index.

    Args:
        device_index (:class:`torch.device`, str, int, optional): the index of the device to target.
            If not given, use :func:`torch.accelerator.current_device_index` by default.
            If a :class:`torch.device` or str is provided, its type must match the current
            :ref:`accelerator<accelerators>` device type.

    Returns:
        tuple[int, int]: a tuple of two integers (free_memory, total_memory) in bytes.
            The first value is the free memory on the device (available across all processes and applications),
            The second value is the device's total hardware memory capacity.
    Tr   )r   r   r   Ú_accelerator_getMemoryInfor6   s    r   r
   r
   è   s%   € ô % \¸DÔA€Lä�8‰8×.Ñ.¨|Ó<Ð<r   )r   N)N)Úcollectionsr   Útypingr   r   Ú_utilsr   r   Ú__all__r   r	   r0   r   Úintr   r   r   r   r   r   Útupler
   r   r   r   ú<module>rF      sÞ   ðÝ #Ý ã ç 0ò€ó	'ó+ñN#˜yð N#°{À3ÈÀ8Ñ7Ló N#ñbL 9ð L¸3ó Lñ I yð I¸só Iñ(K )ð K¸#ó Kñ H ið H¸có Hñ(E°ð EÈó Eñ">¨)ð >À$ó >ñ"= )ð =¸%ÀÀSÀ¹/ô =r   