
    i/                        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      Z G d	 d
ej                        Z G d dej                        Z G d de      ZeZ G d d      Zy)    )annotationsN)MappingMutableMapping)ref)Tensor)TensorWeakRef	WeakIdRefWeakIdKeyDictionaryWeakTensorKeyDictionaryc                       e Zd ZddZd Zd Zy)_IterationGuardc                $    t        |      | _        y N)r   weakcontainer)selfr   s     J/var/www/html/engine/venv/lib/python3.12/site-packages/torch/utils/weak.py__init__z_IterationGuard.__init__   s     /    c                `    | j                         }||j                  j                  |        | S r   )r   
_iteratingadd)r   ws     r   	__enter__z_IterationGuard.__enter__#   s,     =LLT"r   c                    | j                         }|1|j                  }|j                  |        |s|j                          y y y r   )r   r   remove_commit_removals)r   etbr   ss         r   __exit__z_IterationGuard.__exit__)   sB     =AHHTN""$  r   NreturnNone)__name__
__module____qualname__r   r   r!    r   r   r   r      s    0%r   r   c                  >     e Zd ZdgZdd fdZ fdZd Zd Z xZS )r	   _idc                F    t        |      | _        t        |   ||       y r   )idr*   superr   r   keycallback	__class__s      r   r   zWeakIdRef.__init__N   s     c7h'r   c                \    t         |          }t        |d      r|j                          |S N_fix_weakrefr-   __call__hasattrr4   r   rr1   s     r   r6   zWeakIdRef.__call__W   )    G1n%NNr   c                    | j                   S r   r*   r   s    r   __hash__zWeakIdRef.__hash__^       xxr   c                6     |        } |       }||||u S | |u S r   r(   r   otherar   s       r   __eq__zWeakIdRef.__eq__a   s.     FG=Q]6Mu}r   r   r"   	r%   r&   r'   	__slots__r   r6   r>   rD   __classcell__r1   s   @r   r	   r	   K   s    I(r   r	   c                  >     e Zd ZdgZdd fdZ fdZd Zd Z xZS )_WeakHashRefr*   c                F    t        |      | _        t        |   ||       y r   )hashr*   r-   r   r.   s      r   r   z_WeakHashRef.__init__z   s     9h'r   c                \    t         |          }t        |d      r|j                          |S r3   r5   r8   s     r   r6   z_WeakHashRef.__call__   r:   r   c                    | j                   S r   r<   r=   s    r   r>   z_WeakHashRef.__hash__   r?   r   c                \     |        } |       }||t        |      t        |      k(  S | |u S r   )rL   rA   s       r   rD   z_WeakHashRef.__eq__   s8     FG=Q]7d1g%%u}r   r   r"   rE   rH   s   @r   rJ   rJ   w   s    I(r   rJ   c                      e Zd ZdefddZddZddZddZd ZddZ	ddZ
dd	Zd
 ZeZd ZddZddZd Zd ZeZd Zd Zd Zd ZddZdddZd Zd Zd Zd Zy)r
   Nc                    i | _         || _        t        |       fdd}|| _        g | _        t               | _        d| _        || j                  |       y y )Nc                     |       }|7|j                   r|j                  j                  |        y 	 |j                  | = y y # t        $ r Y y w xY wr   )r   _pending_removalsappenddataKeyError)kselfrefr   s      r   r   z,WeakIdKeyDictionary.__init__.<locals>.remove   sS    9D??**11!4 IIaL   $ s   A 	AAFr"   )	rU   ref_typer   _removerS   setr   
_dirty_lenupdate)r   dictrY   r   s       r   r   zWeakIdKeyDictionary.__init__   sT    	 !$i 		 !#%KK r   c                    | j                   j                  }| j                  }	 	  |       }	 ||= # t        $ r Y y w xY w# t        $ r Y w xY wr   )rS   poprU   
IndexErrorrV   )r   r`   dr/   s       r   r   z$WeakIdKeyDictionary._commit_removals   se    
 $$((IIecF   
  s   1 A  	== 	AAc                |    | j                   }| j                  D cg c]	  }||v s| c}| _        d| _        y c c}w NF)rU   rS   r\   )r   rb   rW   s      r   _scrub_removalsz#WeakIdKeyDictionary._scrub_removals   s7    II-1-C-C!NqAv!!N "Os   	99c                J    d| _         | j                  | j                  |      = y NT)r\   rU   rY   r   r/   s     r   __delitem__zWeakIdKeyDictionary.__delitem__   s    IIdmmC()r   c                >    | j                   | j                  |         S r   )rU   rY   rh   s     r   __getitem__zWeakIdKeyDictionary.__getitem__   s    yys+,,r   c                    | j                   r| j                  r| j                          t        | j                        t        | j                        z
  S r   )r\   rS   re   lenrU   r=   s    r   __len__zWeakIdKeyDictionary.__len__   s=    ??t55   "499~D$:$: ;;;r   c                P    d| j                   j                   dt        |       ddS )N<z at z#x>)r1   r%   r,   r=   s    r   __repr__zWeakIdKeyDictionary.__repr__   s(    4>>**+44}A>>r   c                V    || j                   | j                  || j                        <   y r   )rU   rY   rZ   )r   r/   values      r   __setitem__zWeakIdKeyDictionary.__setitem__   s    6;		$--T\\23r   c                    t               }t        |       5  | j                  j                         D ]  \  }} |       }||||<    	 d d d        |S # 1 sw Y   |S xY wr   )r
   r   rU   items)r   newr/   rt   os        r   copyzWeakIdKeyDictionary.copy   sc    !#T" 	#"iioo/ #
UE="CF#	#
 
	#
 
s   *AAAc                    ddl m} | j                         }t        |       5  | j                  j                         D ]  \  }} |       }| |||      ||<    	 d d d        |S # 1 sw Y   |S xY w)Nr   )deepcopy)rz   r|   r1   r   rU   rw   )r   memor|   rx   r/   rt   ry   s          r   __deepcopy__z WeakIdKeyDictionary.__deepcopy__   sq    !nnT" 	3"iioo/ 3
UE=%eT2CF3	3
 
	3
 
s   *A&A&&A0c                X    | j                   j                  | j                  |      |      S r   )rU   getrY   r   r/   defaults      r   r   zWeakIdKeyDictionary.get   s     yy}}T]]3/99r   c                `    	 | j                  |      }|| j                  v S # t        $ r Y yw xY wrd   )rY   	TypeErrorrU   )r   r/   wrs      r   __contains__z WeakIdKeyDictionary.__contains__   s9    	s#B TYY  		s   ! 	--c              #     K   t        |       5  | j                  j                         D ]  \  }} |       }|||f  	 d d d        y # 1 sw Y   y xY wwr   r   rU   rw   )r   r   rt   r/   s       r   rw   zWeakIdKeyDictionary.items   sV     T" 	%!YY__. %	Ed?u*$%	% 	% 	%s!   A*AA	AAAc              #     K   t        |       5  | j                  D ]  } |       }||  	 d d d        y # 1 sw Y   y xY wwr   )r   rU   )r   r   objs      r   keyszWeakIdKeyDictionary.keys  sF     T" 	ii d?I	 	 	s   A88	AAAc              #     K   t        |       5  | j                  j                         D ]  \  }} |       |  	 d d d        y # 1 sw Y   y xY wwr   r   )r   r   rt   s      r   valueszWeakIdKeyDictionary.values  sM     T" 	 !YY__.  	E4#K 	  	  	 s    A(AA	AAAc                ,    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.

        )listrU   r=   s    r   keyrefszWeakIdKeyDictionary.keyrefs  s     DIIr   c                h    d| _         	 | j                  j                         \  }} |       }|||fS +rg   )r\   rU   popitem)r   r/   rt   ry   s       r   r   zWeakIdKeyDictionary.popitem  s<    **,JCA}%x	 r   c                h    d| _          | j                  j                  | j                  |      g| S rg   )r\   rU   r`   rY   )r   r/   argss      r   r`   zWeakIdKeyDictionary.pop(  s,    tyy}}T]]3/7$77r   c                n    | j                   j                  | j                  || j                        |      S r   )rU   
setdefaultrY   rZ   r   s      r   r   zWeakIdKeyDictionary.setdefault-  s-    yy##MM#t||,g
 	
r   c                    | j                   }|Tt        |d      s t        i       |      }|j                         D ]$  \  }}||| j	                  || j
                        <   & |r| j                  |       y y )Nrw   )rU   r7   typerw   rY   rZ   r]   )r   r^   kwargsrb   r/   rt   s         r   r]   zWeakIdKeyDictionary.update2  sp    II4)tBx~"jjl <
U6;$--T\\23<KK r   c                (    | j                  |       | S r   )r]   )r   rB   s     r   __ior__zWeakIdKeyDictionary.__ior__<  s    Er   c                    t        |t        j                        r#| j                         }|j	                  |       |S t
        S r   )
isinstance_collections_abcr   rz   r]   NotImplementedr   rB   cs      r   __or__zWeakIdKeyDictionary.__or__@  s4    e-556		AHHUOHr   c                    t        |t        j                        r4| j                         }|j	                  |       |j	                  |        |S t
        S r   )r   r   r   r1   r]   r   r   s      r   __ror__zWeakIdKeyDictionary.__ror__G  s@    e-556 AHHUOHHTNHr   c                    t        |t              st        S | j                         D ci c]  \  }}t	        |      | c}}|j                         D ci c]  \  }}t	        |      | c}}k(  S c c}}w c c}}w r   )r   r   r   rw   r,   )r   rB   rW   vs       r   rD   zWeakIdKeyDictionary.__eq__Q  si    %)!!%)ZZ\2TQ1q2!&7
ABqE1H7
 
 	
2 7
s   A2A8r"   )r#   int)r#   strr   )r#   bool)r%   r&   r'   r	   r   r   re   ri   rk   rn   rr   ru   rz   __copy__r~   r   r   rw   r   __iter__r   r   r   r`   r   r]   r   r   r   rD   r(   r   r   r
   r
      s     9 0$ 
*-<?< H	:% H 
 8


 
r   r
   c                  *    e Zd ZU dZded<   ddZd Zy)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   c                    t        |t              st        dt        |       d      t	        j
                  |      | _        y Nzexpected torch.Tensor, got .)r   r   AssertionErrorr   weakrefr   )r   tensors     r   r   zTensorWeakRef.__init__b  s5    &&) #>tF|nA!NOO;;v&r   c                    | j                         }||S t        |t              st        dt	        |       d      |j                          |S r   )r   r   r   r   r   r4   )r   outs     r   r6   zTensorWeakRef.__call__g  sK    hhj;J#v& #>tCyk!KLL
r   N)r   r   r#   r$   )r%   r&   r'   __doc____annotations__r   r6   r(   r   r   r   r   ]  s    	'
r   r   )
__future__r   collections.abcabcr   r   r   r   r   torchr   WeakRef__all__r   r	   rJ   r
   r   r   r(   r   r   <module>r      ss    " *  3   % %d' 'X7;; D}
. }
B .  r   