o
    پi                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZ d dlZddlm	Z	 ddl
mZmZ eeZdZG dd	 d	ZG d
d dZdd ZdS )    N)contextmanager)Optional   )BinaryWrapper)HookUtilBaseHookModedefaultc                   @   s   e Zd Zdd ZeedfdedefddZeddd	edfdedefd
dZ	edd Z
ddee fddZddee fddZedd Zedd ZejdefddZdd ZdS )TorchMemorySaverc                 C   s   i | _ d | _d S N)_impl_ctor_kwargs_implself r   Q/home/ubuntu/.local/lib/python3.10/site-packages/torch_memory_saver/entrypoint.py__init__   s   
zTorchMemorySaver.__init__Ftagenable_cpu_backupc                 c   sF    |    | jj||d dV  W d   dS 1 sw   Y  dS )z3Context manager for memory saving with optional tagr   r   N)_ensure_initializedr   regionr   r   r   r   r   r   r      s
   "zTorchMemorySaver.regionNglobalc                 c   sN    |    | jj||||||d dV  W d   dS 1 s w   Y  dS )zHSimilar to `torch.cuda.graph`, but ensures memory in it to be pauseable.)
cuda_graphpoolstreamcapture_error_moder   r   N)r   r   r   r   r   r   r   r   r   r   r   r   r   r      s   "zTorchMemorySaver.cuda_graphc                 c   s@    |    | j  d V  W d    d S 1 sw   Y  d S r
   )r   r   disabler   r   r   r   r   +   s
   "zTorchMemorySaver.disablec                 C      |    | jj|d dS )z:Pause memory for specific tag or all memory if tag is Noner   N)r   r   pauser   r   r   r   r   r!   1      zTorchMemorySaver.pausec                 C   r   )z;Resume memory for specific tag or all memory if tag is Noner    N)r   r   resumer"   r   r   r   r$   6   r#   zTorchMemorySaver.resumec                 C   s   dS )NTr   r   r   r   r   enabled<      zTorchMemorySaver.enabledc                 C   s   t r
   )AttributeErrorr   r   r   r   	hook_mode@   r&   zTorchMemorySaver.hook_moder(   c                 C   s    | j d us	J d|| j d< d S )Nz%Cannot configure after initializationr(   )r   r   r(   r   r   r   r(   D   s   c                 C   s(   | j d urd S tdi | j| _ | `d S )Nr   )r   _TorchMemorySaverImplr   r   r   r   r   r   I   s   
z$TorchMemorySaver._ensure_initializedr
   )__name__
__module____qualname__r   r   _TAG_DEFAULTstrboolr   r   r   r   r!   r$   propertyr%   r(   setterr   r   r   r   r   r   r	      s.    


r	   c                   @   s   e Zd ZddefddZededefddZededefd	d
Z	ededefddZ
eddefddZdee fddZdee fddZdS )r*   preloadr(   c                 C   sF   || _ tj|d| _t| j d| _tjj	| j
 d| _t  d S )N)r(   )path_binary)	allocator)
_hook_moder   create
_hook_utilr   get_path_binary_binary_wrappertorchcudaMemPoolget_allocator_primary_mem_pool_sanity_checksr)   r   r   r   r   Q   s
   
z_TorchMemorySaverImpl.__init__r   r   c              	   c   sz    t j| j+ | j||d d V  W d    n1 sw   Y  W d    d S W d    d S 1 s6w   Y  d S )Nr   )r;   r<   use_mem_poolr?   _with_region_configr   r   r   r   r   X   s   "z_TorchMemorySaverImpl.regionc              	   c   s    | j dks
J dtjj||||d+ | j||d d V  W d    n1 s*w   Y  W d    d S W d    d S 1 sBw   Y  d S )Nr3   z>Only hook_mode=preload supports pauseable CUDA Graph currently)r   r   r   r   )r6   r;   r<   graphrB   r   r   r   r   r   ^   s   "z _TorchMemorySaverImpl.cuda_graphc                 c   s    | j j r	J | j j }| j j|d|d zd V  W | j j s%J | j jtd|d d S | j j s8J | j jtd|d w )NT)r   interesting_regionr   F)r:   cdlltms_get_interesting_regiontms_get_enable_cpu_backup
set_configr.   )r   r   r   original_enable_cpu_backupr   r   r   rB   e   s   z)_TorchMemorySaverImpl._with_region_configTdispose_mem_pool_after_usec              	   c   s    |sJ d| j j sJ d| j jd z)tj }tj| d V  W d    n1 s2w   Y  ~W | j jd d S | j jd w )Nz5Only dispose_mem_pool_after_use=true is supported nowz2disable() should be called only when tms is activeFT)r:   rE   rF   tms_set_interesting_regionr;   r<   r=   rA   )r   rJ   r   r   r   r   r   q   s   
"z_TorchMemorySaverImpl.disablec                 C   $   |r| dnd }| jj| d S Nzutf-8)encoder:   rE   	tms_pauser   r   	tag_bytesr   r   r   r!         z_TorchMemorySaverImpl.pausec                 C   rL   rM   )rN   r:   rE   
tms_resumerP   r   r   r   r$      rR   z_TorchMemorySaverImpl.resumeN)r3   )T)r+   r,   r-   r   r   r   r/   r0   r   r   rB   r   r   r!   r$   r   r   r   r   r*   P   s    r*   c                   C   s   dt jddv rtdd S )Nzexpandable_segments:TruePYTORCH_CUDA_ALLOC_CONF zfTorchMemorySaver is disabled for the current process because expandable_segments is not supported yet.)osenvirongetRuntimeErrorr   r   r   r   r@      s
   r@   )ctypesloggingrV   
contextlibr   typingr   r;   binary_wrapperr   
hooks.baser   r   	getLoggerr+   loggerr.   r	   r*   r@   r   r   r   r   <module>   s    
@: