
    %Vji#                         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	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S )    )OrderedDict)AnyN   )	_device_t_get_device_index)empty_cachemax_memory_allocatedmax_memory_reservedmemory_allocatedmemory_reservedmemory_statsreset_accumulated_memory_statsreset_peak_memory_statsreturnc                      t           j                                        sdS t           j                                         dS )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     R/root/voice-cloning/.venv/lib/python3.11/site-packages/torch/accelerator/memory.pyr   r      s9     87799 	H$$&&&&&r   device_indexc                N   t           j                                        st                      S t	          | d          } t           j                            |           }g dt          dt          ddffd d|                                            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.
    Toptionalprefixvaluer   Nc                     t          |t                    r3|                                D ]\  }}| r|  d| n|} ||           d S                     | |f           d S )N.)
isinstancedictitemsappend)r   r   kvnested_prefix
flat_statsflattens        r   r)   zmemory_stats.<locals>.flattena   s    eT"" 	/ * *139 @6Aqq))))* * vuo.....r    )	r   r   r   r   r   _accelerator_getDeviceStatsstrr   sort)r   statsr(   r)   s     @@r   r   r   !   s    t 87799 }}$\DAAALH00>>EJ/ /C /D / / / / / / / GBOOz"""r   c                H    t          |                               dd          S )a  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.
    zallocated_bytes.all.currentr   r   getr   s    r   r   r   n   s#     %%))*GKKKr   c                H    t          |                               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.
    zallocated_bytes.all.peakr   r0   r2   s    r   r	   r	   {   s#     %%))*DaHHHr   c                H    t          |                               dd          S )a  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.
    zreserved_bytes.all.currentr   r0   r2   s    r   r   r      s#     %%))*FJJJr   c                H    t          |                               dd          S )a  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.
    zreserved_bytes.all.peakr   r0   r2   s    r   r
   r
      s#     %%))*CQGGGr   c                b    t          | d          } t          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_resetAccumulatedStatsr2   s    r   r   r      s,     %\DAAAL866|DDDr   c                b    t          | d          } t          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_resetPeakStatsr2   s    r   r   r      s,     %\DAAAL8//===r   )r   N)N)collectionsr   typingr   r   _utilsr   r   __all__r   r,   r   intr   r	   r   r
   r   r   r   r   r   <module>r?      s   # # # # # #        0 0 0 0 0 0 0 0	 	 		' 	' 	' 	'J# J#y J#{387L J# J# J# J#Z
L 
L9 
L3 
L 
L 
L 
LI Iy Is I I I I"
K 
K) 
K# 
K 
K 
K 
KH Hi Hc H H H H"E E E E E E E"> >) >$ > > > > > >r   