L i\"ddlZddlmZmZddlZddlmZddlmZm Z eee e dfZ ddZ dde ddfd Zdde ddfd Zdde dee effd Zdde dee effd Zdde de fd Zdde de fdZdde de fdZdde de fdZdde dee e ffdZgdZy)N)AnyUnion)Device)_get_device_indexis_initializedreturncVtrtjjyy)aZRelease 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)rtorch_C_xpu_emptyCacheV/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/torch/xpu/memory.py empty_cacher s   "rdevicecZt|d}tjj|S)aReset 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). Toptional)rr r _xpu_resetPeakMemoryStatsrs rreset_peak_memory_statsrs%v 5F 88 - -f 55rcZt|d}tjj|S)aReset 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)rr r _xpu_resetAccumulatedMemoryStatsrs rreset_accumulated_memory_statsr)s%v 5F 88 4 4V <= 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). prefixobjr Nct|tr8t|dkDr|dz }|jD]\}}||z|yj ||fy)Nr.) isinstancedictlenitemsappend)r r!kv_recurse_add_to_resultresults rr+z,memory_stats.._recurse_add_to_resulthsZ c4 6{Q#   61&vz15 6 MM63- (rr)strrrsort collections OrderedDict)rstatsr+r,s @@r memory_statsr3@sULF)s))) (v 6E2u% KKM  " "6 **rc:t|jddS)aReturn 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. rzallocated_bytes.all.currentrr3getrs rmemory_allocatedr7xs v & * *+H! LLrc:t|jddS)aReturn 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). rzallocated_bytes.all.peakrr5rs rmax_memory_allocatedr9s v & * *+Eq IIrc:t|jddS)aJReturn 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). rzreserved_bytes.all.currentrr5rs rmemory_reservedr;s v & * *+G KKrc:t|jddS)aReturn 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). rzreserved_bytes.all.peakrr5rs rmax_memory_reservedr=s v & * *+Da HHrcZt|d}tjj|S)aReturn 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)rr r _xpu_getMemoryInfors r mem_get_infor@s%v 5F 88 & &v ..r) rr9r=r@r7r;r3rrr)r N)N)r0typingrrr torch.typesrr-rrr.int _device_trrrr%rr3r7r9r;r=tupler@__all__rrrrGs / &#sD( )  # 6I 6 6 =9 = =- -T#s(^-5+5+d38n5+p MY M# M JJcJ"LILLI ISI" / /eCHo / r