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 Zdd Zdd ZdS )_IterationGuardc                 C  s   t || _d S N)r   weakcontainer)selfr    r   K/var/www/html/RAG/RAG_venv/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)__name__
__module____qualname__r   r   r   r   r   r   r   r      s    r   c                      @   e Zd ZdgZd fdd	Z fddZdd Zd	d
 Z  ZS )r   _idNc                      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__hasattrr-   r   rr(   r   r   r/   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   	__slots__r   r/   r7   r<   __classcell__r   r   r(   r   r   K       	r   c                      r    )_WeakHashRefr!   Nc                   r"   r   )hashr!   r$   r   r%   r(   r   r   r   z   r*   z_WeakHashRef.__init__c                   r+   r,   r.   r1   r(   r   r   r/      r3   z_WeakHashRef.__call__c                 C  r4   r   r5   r6   r   r   r   r7      r8   z_WeakHashRef.__hash__c                 C  s4   |  }| }|d ur|d urt |t |kS | |u S r   )rB   r9   r   r   r   r<      s
   z_WeakHashRef.__eq__r   r=   r   r   r(   r   rA   w   r@   rA   c                   @  s   e Zd ZdefddZdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd ZeZdd Zd2ddZdd Zdd Zdd ZeZdd Zd d! Zd"d# Zd$d% Zd2d&d'Zd2d(d)Zd*d+ Zd,d- Zd.d/ Zd0d1 ZdS )3r	   Nc                 C  sP   i | _ || _t| fdd}|| _g | _t | _d| _|d ur&| | d S d S )Nc                 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>.removeF)	rE   ref_typer   _removerC   setr   
_dirty_lenupdate)r   dictrI   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   )rC   poprE   
IndexErrorrF   )r   rO   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   ).0rG   rQ   r   r   
<listcomp>   s    z7WeakIdKeyDictionary._scrub_removals.<locals>.<listcomp>F)rE   rC   rL   r6   r   rS   r   _scrub_removals   s   
z#WeakIdKeyDictionary._scrub_removalsc                 C  s   d| _ | j| |= d S NT)rL   rE   rI   r   r&   r   r   r   __delitem__   s   zWeakIdKeyDictionary.__delitem__c                 C  s   | j | | S r   )rE   rI   rW   r   r   r   __getitem__   s   zWeakIdKeyDictionary.__getitem__c                 C  s(   | j r
| jr
|   t| jt| j S r   )rL   rC   rU   lenrE   r6   r   r   r   __len__   s   zWeakIdKeyDictionary.__len__c                 C  s   d| j j dt| ddS )N<z at z#x>)r)   r   r#   r6   r   r   r   __repr__   s   zWeakIdKeyDictionary.__repr__c                 C  s   || j | || j< d S r   )rE   rI   rJ   )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   rE   items)r   newr&   r_   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)rd   re   r)   r   rE   ra   )r   memore   rb   r&   r_   rc   r   r   r   __deepcopy__   s   

z WeakIdKeyDictionary.__deepcopy__c                 C  s   | j | ||S r   )rE   getrI   r   r&   defaultr   r   r   rh      s   zWeakIdKeyDictionary.getc                 C  s.   z|  |}W n
 ty   Y dS w || jv S )NF)rI   	TypeErrorrE   )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   rE   ra   )r   rl   r_   r&   r   r   r   ra      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   rE   )r   rl   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   rn   )r   rl   r_   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.

        )listrE   r6   r   r   r   keyrefs  s   

zWeakIdKeyDictionary.keyrefsc                 C  s.   d| _ 	 | j \}}| }|d ur||fS qrV   )rL   rE   popitem)r   r&   r_   rc   r   r   r   rt     s   zWeakIdKeyDictionary.popitemc                 G  s    d| _ | jj| |g|R  S rV   )rL   rE   rO   rI   )r   r&   argsr   r   r   rO   '  s   zWeakIdKeyDictionary.popc                 C  s   | j | || j|S r   )rE   
setdefaultrI   rJ   ri   r   r   r   rv   +  s   zWeakIdKeyDictionary.setdefaultc                 K  sb   | j }|d ur$t|dsti |}| D ]\}}||| || j< qt|r/| | d S d S )Nra   )rE   r0   typera   rI   rJ   rZ   rM   )r   rN   kwargsrQ   r&   r_   r   r   r   rM   0  s   
zWeakIdKeyDictionary.updatec                 C  s   |  | | S r   )rM   r   r:   r   r   r   __ior__:  s   
zWeakIdKeyDictionary.__ior__c                 C  s&   t |tjr|  }|| |S tS r   )
isinstance_collections_abcr   rd   rM   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)   rM   r}   r~   r   r   r   __ror__E  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#   )rR   rG   vr   r   r   
<dictcomp>R  s    z.WeakIdKeyDictionary.__eq__.<locals>.<dictcomp>)r{   r   r}   ra   ry   r   r   r   r<   O  s
   
zWeakIdKeyDictionary.__eq__r   )r   r   r   r   r   r   rU   rX   rY   r[   r^   r`   rd   __copy__rg   rh   rm   ra   rp   __iter__rq   rs   rt   rO   rv   rM   rz   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   c                 C  s   t |tsJ t|| _d S r   )r{   r   weakrefr   )r   r   r   r   r   r   `  s   zTensorWeakRef.__init__c                 C  s.   |   }|d u r
|S t|tsJ |  |S r   )r   r{   r   r-   )r   outr   r   r   r/   d  s   zTensorWeakRef.__call__N)r   r   )r   r   r   __doc____annotations__r   r/   r   r   r   r   r   [  s
   
 
r   )
__future__r   collections.abcabcr|   r   r   r   r   torchr   WeakRef__all__r   r   rA   r	   r
   r   r   r   r   r   <module>   s   
2," @