3
I4W^†  ã               @   sT   d dl mZmZmZmZ d dlZd dlZd dlZG dd„ deƒZ	dd„ Z
dd„ ZdS )	é    )Úabsolute_importÚdivisionÚprint_functionÚunicode_literalsNc               @   sD   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dd„ Z
dS )ÚRemovableHandlez8A handle which provides the capability to remove a hook.r   c             C   s&   t j|ƒ| _tj| _t jd7  _d S )Né   )ÚweakrefÚrefÚhooks_dict_refr   Únext_idÚid)ÚselfÚ
hooks_dict© r   ú2/tmp/pip-build-e66j97b2/torch/torch/utils/hooks.pyÚ__init__   s    zRemovableHandle.__init__c             C   s&   | j ƒ }|d k	r"| j|kr"|| j= d S )N)r
   r   )r   r   r   r   r   Úremove   s    zRemovableHandle.removec             C   s   | j ƒ | jfS )N)r
   r   )r   r   r   r   Ú__getstate__   s    zRemovableHandle.__getstate__c             C   sP   |d d krt jtjƒ ƒ| _nt j|d ƒ| _|d | _ttj| jd ƒt_d S )Nr   r   )	r   r	   ÚcollectionsÚOrderedDictr
   r   Úmaxr   r   )r   Ústater   r   r   Ú__setstate__   s
    
zRemovableHandle.__setstate__c             C   s   | S )Nr   )r   r   r   r   Ú	__enter__"   s    zRemovableHandle.__enter__c             C   s   | j ƒ  d S )N)r   )r   ÚtypeÚvalueÚtbr   r   r   Ú__exit__%   s    zRemovableHandle.__exit__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r   r   r   r      s   	r   c             C   s
   d| _ | S )z·
    Decorator which marks a function as an unserializable hook.
    This suppresses warnings that would otherwise arise if you attempt
    to serialize a tensor that has a hook.
    T)Ú__torch_unserializable__)Úfr   r   r   Úunserializable_hook)   s    r$   c             C   sB   | j r>x6| j D ],}| j | }t|dƒstjdjt|ƒƒƒ qW d S )Nr"   z«backward hook {} on tensor will not be serialized.  If this is expected, you can decorate the function with @torch.utils.hooks.unserializable_hook to suppress this warning)Z_backward_hooksÚhasattrÚwarningsÚwarnÚformatÚrepr)ZtensorÚkÚhookr   r   r   Úwarn_if_has_hooks3   s    

r,   )Ú
__future__r   r   r   r   r   r   r&   Úobjectr   r$   r,   r   r   r   r   Ú<module>   s   "
