§
    ŠŠtj$/  ã                   ó  — 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ddefd„ZdS )é    )Ú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                             ¦   «         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© ó    úV/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/torch/accelerator/memory.pyr   r      s9   € õ Œ8×7Ò7Ñ9Ô9ð ØˆÝ	„H×$Ò$Ñ&Ô&Ð&Ð&Ð&r   c                  óB   — t           j                             ¦   «          dS )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×(Ò(Ñ*Ô*Ð*Ð*Ð*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.

    Returns:
        OrderedDict[str, Any]: an ordered dictionary mapping statistic names to their values.
    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>.flattenp   s‡   ø€ Ý�e�TÑ"Ô"ð 	/ØŸš™œð *ð *‘��1Ø39Ð @ 6  ¨A   ¸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×7Ò7Ñ9Ô9ð Ý‰}Œ}ÐÝ$ \¸DÐAÑAÔA€LÝŒH×0Ò0°Ñ>Ô>€EØ€Jð/�ð /¥Cð /¨Dð /ð /ð /ð /ð /ð /ð /ð €GˆB�ÑÔÐØ‡O‚OÑÔÐå�zÑ"Ô"Ð"r   c                óH   — t          | ¦  «                             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Ô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.

    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ÔHÐHr   c                óH   — t          | ¦  «                             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ÔJÐJr   c                óH   — t          | ¦  «                             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ÔGÐGr   c                ó¢   — t           j                             ¦   «         sdS 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.
    NTr   )r   r   r   r   Ú"_accelerator_resetAccumulatedStatsr6   s    r   r   r   Æ   sG   € õ Œ8×7Ò7Ñ9Ô9ð ØˆÝ$ \¸DÐAÑAÔA€LÝŒ8×6Ò6°|ÑDÔDÐDr   c                ó¢   — t           j                             ¦   «         sdS 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.
    NTr   )r   r   r   r   Ú_accelerator_resetPeakStatsr6   s    r   r   r   Ù   sG   € õ Œ8×7Ò7Ñ9Ô9ð ØˆÝ$ \¸DÐAÑAÔA€LÝŒ8×/Ò/°Ñ=Ô=Ð=r   c                ób   — t          | d¬¦  «        } t          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ÑAÔA€LåŒ8×.Ò.¨|Ñ<Ô<Ð<r   FÚaugment_with_fx_tracesc                 óð   — t           j                             ¦   «         }|�1|j        dk    r&t           j        j                             | |¬¦  «        S t           j        j                             | |¬¦  «        S )a:  Return a snapshot of the current :ref:`accelerator<accelerators>` memory allocator state.

    Requires :func:`_record_memory_history` on the appropriate device module
    (e.g., :func:`torch.cuda.memory._record_memory_history`) to have been called.

    Args:
        device: the device to snapshot. If not given, uses the current device.
        augment_with_fx_traces (bool, optional): if True, augment stack traces
            with FX graph information. Default: ``False``.

    Returns:
        dict: a dictionary containing memory allocator state information.
    NÚxpu)r@   )r   ÚacceleratorÚcurrent_acceleratorÚtyperB   ÚmemoryÚ	_snapshotÚcuda)Údevicer@   Úaccs      r   rG   rG   ÿ   s{   € õ Ô
×
/Ò
/Ñ
1Ô
1€CØ
€˜3œ8 uÒ,Ð,ÝŒyÔ×)Ò)ØÐ+Að *ñ 
ô 
ð 	
õ Œ:Ô×&Ò&ØÐ'=ð 'ñ ô ð r   )r   N)N)NF)Úcollectionsr   Útypingr   r   Ú_utilsr   r   Ú__all__r   r	   r0   r   Úintr   r   r   r   r   r   Útupler
   ÚboolrG   r   r   r   ú<module>rR      s!  ðØ #Ð #Ð #Ð #Ð #Ð #Ø Ð Ð Ð Ð Ð à €€€à 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0ðð ð €ð	'ð 	'ð 	'ð 	'ð+ð +ð +ð +ðN#ð N#˜yð N#°{À3ÈÀ8Ô7Lð N#ð N#ð N#ð N#ðb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ð&>ð >¨)ð >À$ð >ð >ð >ð >ð&=ð = )ð =¸%ÀÀSÀ¼/ð =ð =ð =ð =ð&ð °4ð ð ð ð ð ð r   