o
    i/                     @  s   d dl mZ d dlmZ d dlZd dlmZmZ d dlmZ d dl	m
Z
 eZg dZG dd dZG d	d
 d
ejZG dd dejZG dd deZeZG dd dZdS )    )annotationsN)MappingMutableMapping)ref)Tensor)TensorWeakRef	WeakIdRefWeakIdKeyDictionaryWeakTensorKeyDictionaryc                   @  s&   e Zd Zd
ddZdd Zdd Zd	S )_IterationGuardreturnNonec                 C  s   t || _d S N)r   weakcontainer)selfr    r   R/sda-disk/www/egybert/egybert_env/lib/python3.10/site-packages/torch/utils/weak.py__init__   s   z_IterationGuard.__init__c                 C  s    |   }|d ur|j|  | S r   )r   
_iteratingadd)r   wr   r   r   	__enter__#   s   z_IterationGuard.__enter__c                 C  s8   |   }|d ur|j}||  |s|  d S d S d S r   )r   r   remove_commit_removals)r   etbr   sr   r   r   __exit__)   s   
z_IterationGuard.__exit__Nr   r   )__name__
__module____qualname__r   r   r   r   r   r   r   r      s    
r   c                      B   e Zd ZdgZdd fddZ fddZd	d
 Zdd Z  ZS )r   _idNr   r   c                      t || _t || d S r   )idr$   superr   r   keycallback	__class__r   r   r   N      
zWeakIdRef.__init__c                       t   }t|dr|  |S N_fix_weakrefr'   __call__hasattrr0   r   rr+   r   r   r2   W      

zWeakIdRef.__call__c                 C     | j S r   r$   r   r   r   r   __hash__^      zWeakIdRef.__hash__c                 C  s,   |  }| }|d ur|d ur||u S | |u S r   r   r   otherar   r   r   r   __eq__a   s
   zWeakIdRef.__eq__r   r   	r    r!   r"   	__slots__r   r2   r:   r?   __classcell__r   r   r+   r   r   K       	r   c                      r#   )_WeakHashRefr$   Nr   r   c                   r%   r   )hashr$   r'   r   r(   r+   r   r   r   z   r-   z_WeakHashRef.__init__c                   r.   r/   r1   r4   r+   r   r   r2      r6   z_WeakHashRef.__call__c                 C  r7   r   r8   r9   r   r   r   r:      r;   z_WeakHashRef.__hash__c                 C  s4   |  }| }|d ur|d urt |t |kS | |u S r   )rE   r<   r   r   r   r?      s
   z_WeakHashRef.__eq__r   r   r@   r   r   r+   r   rD   w   rC   rD   c                   @  s   e Zd Zdefd7ddZd7ddZd7dd	Zd7d
dZdd Zd8ddZ	d9ddZ
d7ddZdd ZeZdd Zd:ddZd;ddZdd  Zd!d" ZeZd#d$ Zd%d& Zd'd( Zd)d* Zd:d+d,Zd:d7d-d.Zd/d0 Zd1d2 Zd3d4 Zd5d6 ZdS )<r	   Nr   r   c                 C  sR   i | _ || _t| fddd}|| _g | _t | _d| _|d ur'| | d S d S )Nr   r   c                 S  sL   | }|d ur$|j r|j|  d S z|j| = W d S  ty#   Y d S w d S r   )r   _pending_removalsappenddataKeyError)kselfrefr   r   r   r   r      s   z,WeakIdKeyDictionary.__init__.<locals>.removeFr   )	rH   ref_typer   _removerF   setr   
_dirty_lenupdate)r   dictrL   r   r   r   r   r      s   zWeakIdKeyDictionary.__init__c                 C  sP   | j j}| j}	 z| }W n
 ty   Y d S w z||= W n	 ty&   Y nw qr   )rF   poprH   
IndexErrorrI   )r   rR   dr)   r   r   r   r      s   

z$WeakIdKeyDictionary._commit_removalsc                   s&   | j   fdd| jD | _d| _d S )Nc                   s   g | ]}| v r|qS r   r   ).0rJ   rT   r   r   
<listcomp>   s    z7WeakIdKeyDictionary._scrub_removals.<locals>.<listcomp>F)rH   rF   rO   r9   r   rV   r   _scrub_removals   s   
z#WeakIdKeyDictionary._scrub_removalsc                 C  s   d| _ | j| |= d S NT)rO   rH   rL   r   r)   r   r   r   __delitem__   s   zWeakIdKeyDictionary.__delitem__c                 C  s   | j | | S r   )rH   rL   rZ   r   r   r   __getitem__   s   zWeakIdKeyDictionary.__getitem__intc                 C  s(   | j r
| jr
|   t| jt| j S r   )rO   rF   rX   lenrH   r9   r   r   r   __len__   s   zWeakIdKeyDictionary.__len__strc                 C  s   d| j j dt| ddS )N<z at z#x>)r,   r    r&   r9   r   r   r   __repr__   s   zWeakIdKeyDictionary.__repr__c                 C  s   || j | || j< d S r   )rH   rL   rM   )r   r)   valuer   r   r   __setitem__   s   zWeakIdKeyDictionary.__setitem__c                 C  s^   t  }t|  | j D ]\}}| }|d ur|||< qW d    |S 1 s(w   Y  |S r   )r	   r   rH   items)r   newr)   rd   or   r   r   copy   s   

zWeakIdKeyDictionary.copyc                 C  sr   ddl m} |  }t| ! | j D ]\}}| }|d ur&|||||< qW d    |S 1 s2w   Y  |S )Nr   )deepcopy)ri   rj   r,   r   rH   rf   )r   memorj   rg   r)   rd   rh   r   r   r   __deepcopy__   s   

z WeakIdKeyDictionary.__deepcopy__c                 C  s   | j | ||S r   )rH   getrL   r   r)   defaultr   r   r   rm      s   zWeakIdKeyDictionary.getboolc                 C  s.   z|  |}W n
 ty   Y dS w || jv S )NF)rL   	TypeErrorrH   )r   r)   wrr   r   r   __contains__   s   
z WeakIdKeyDictionary.__contains__c                 c  s\    t |  | j D ]\}}| }|d ur||fV  qW d    d S 1 s'w   Y  d S r   r   rH   rf   )r   rr   rd   r)   r   r   r   rf      s   

"zWeakIdKeyDictionary.itemsc                 c  sP    t |  | jD ]}| }|d ur|V  q	W d    d S 1 s!w   Y  d S r   )r   rH   )r   rr   objr   r   r   keys  s   

"zWeakIdKeyDictionary.keysc                 c  sT    t |  | j D ]\}}| d ur|V  qW d    d S 1 s#w   Y  d S r   rt   )r   rr   rd   r   r   r   values  s   

"zWeakIdKeyDictionary.valuesc                 C  s
   t | jS )az  Return a list of weak references to the keys.

        The references are not guaranteed to be 'live' at the time
        they are used, so the result of calling the references needs
        to be checked before being used.  This can be used to avoid
        creating references that will cause the garbage collector to
        keep the keys around longer than needed.

        )listrH   r9   r   r   r   keyrefs  s   

zWeakIdKeyDictionary.keyrefsc                 C  s.   d| _ 	 | j \}}| }|d ur||fS qrY   )rO   rH   popitem)r   r)   rd   rh   r   r   r   rz     s   zWeakIdKeyDictionary.popitemc                 G  s    d| _ | jj| |g|R  S rY   )rO   rH   rR   rL   )r   r)   argsr   r   r   rR   (  s   zWeakIdKeyDictionary.popc                 C  s   | j | || j|S r   )rH   
setdefaultrL   rM   rn   r   r   r   r|   -  s   zWeakIdKeyDictionary.setdefaultc                 K  s^   | j }|d ur$t|dsti |}| D ]\}}||| || j< q|r-| | d S d S )Nrf   )rH   r3   typerf   rL   rM   rP   )r   rQ   kwargsrT   r)   rd   r   r   r   rP   2  s   
zWeakIdKeyDictionary.updatec                 C  s   |  | | S r   )rP   r   r=   r   r   r   __ior__<  s   
zWeakIdKeyDictionary.__ior__c                 C  s&   t |tjr|  }|| |S tS r   )
isinstance_collections_abcr   ri   rP   NotImplementedr   r=   cr   r   r   __or__@  s
   
zWeakIdKeyDictionary.__or__c                 C  s0   t |tjr|  }|| ||  |S tS r   )r   r   r   r,   rP   r   r   r   r   r   __ror__G  s   

zWeakIdKeyDictionary.__ror__c                 C  s2   t |tstS dd |  D dd | D kS )Nc                 S  s   i | ]	\}}t ||qS r   )r&   )rU   rJ   vr   r   r   
<dictcomp>T  s    z.WeakIdKeyDictionary.__eq__.<locals>.<dictcomp>)r   r   r   rf   r   r   r   r   r?   Q  s
   
zWeakIdKeyDictionary.__eq__r   )r   r]   )r   r`   r   )r   rp   )r    r!   r"   r   r   r   rX   r[   r\   r_   rc   re   ri   __copy__rl   rm   rs   rf   rv   __iter__rw   ry   rz   rR   r|   rP   r   r   r   r?   r   r   r   r   r	      s6    





	

	


r	   c                   @  s,   e Zd ZU dZded< ddd	Zd
d ZdS )r   zuWrapper around a weak ref of a Tensor that handles the _fix_weakref() call required when unwrapping a Tensor weakref.zWeakRef[Tensor]r   tensorr   r   r   c                 C  s.   t |tstdt| dt|| _d S Nzexpected torch.Tensor, got .)r   r   AssertionErrorr}   weakrefr   )r   r   r   r   r   r   b  s   
zTensorWeakRef.__init__c                 C  s>   |   }|d u r
|S t|tstdt| d|  |S r   )r   r   r   r   r}   r0   )r   outr   r   r   r2   g  s   
zTensorWeakRef.__call__N)r   r   r   r   )r    r!   r"   __doc____annotations__r   r2   r   r   r   r   r   ]  s
   
 
r   )
__future__r   collections.abcabcr   r   r   r   r   torchr   WeakRef__all__r   r   rD   r	   r
   r   r   r   r   r   <module>   s   
2," B