
    {Yh6              	          d dl Z d dlZd dlmZ d dlmZ d dlmZm	Z	m
Z
 d dlmZ d dlZd dlZd dlZd dlZd dlmc mZ d dlmZ g dZej        j        Zej        j        Zej        j        ZdZ ed	          Z e	d
          Z deej!        j"                 fdZ#ed             Z$d Z%d Z& G d d          Z'de(de(fdZ)dej!        j"        ee f         dede
eee f         ef         fdZ*ed             Z+dS )    N)Iterator)contextmanager)CallableTypeVarUnion)	ParamSpec)DispatchKey)enable_python_dispatcherno_python_dispatcherenable_pre_dispatchF_P_Treturnc               #      K   t           j        D ]H} t          t           j        |           }|D ])}t          ||          }|D ]}t          ||          V  *IdS )a  
    Warning: the set of overloads this will report is very subtle.  It is precisely
    the set of torch.ops functions that have actually been accessed from Python
    (e.g., we actually called torch.ops.aten.blah at some point.  This is DIFFERENT
    from the set of registered operators, which will in general be a larger set,
    as this would include all operators which we ran C++ static initializers or
    Python operator registration on.  This does not eagerly populate the list on
    torch.ops.aten; this list is lazy!

    In other words, this is good for traversing over everything that has an
    OpOverload object allocated in Python.  We use it for cache invalidation, but
    don't rely on this list being complete.

    Note that even if we did report all C++ registered overloads, this isn't guaranteed
    to be complete either, as a subsequent lazy load of a library which triggers more
    registrations could add more things to the set.
    N)torchopsgetattr)nspacketsop_namepacketoverloads        h/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/torch/_dispatch/python.pyall_py_loaded_overloadsr      s      $ i 0 0%)R(( 	0 	0GWg..F" 0 0fh//////0	00 0    c               #   L  K   t           j                            t           j        j        j                  } t           j                                        }| rt          j                     	 d V  | rt          j        |           d S d S # | rt          j        |           w w xY w)N)reapply_views)r   _C&_dispatch_tls_is_dispatch_key_includedr	   Functionalize$_functionalization_reapply_views_tls_disable_functionalization_enable_functionalization)f_tlsf_rvs     r   suspend_functionalizationr&   7   s      H;;* E 888::D +(***@ 	@+$??????	@ 	@5 	@+$?????	@s   *B	 	B#c           	      2   t          |          sJ |                                 |                                k    s>J  |             d|                                  d|                                             | j        |j        k    s$J  |             d| j         d|j                     t          j                            | |d          \  }}|sBJ  |             d|                                  d|                                 d| d            d S )	Nz: sizes  != z: dtype F)	only_cudaz
: strides z (mismatch at index ))callablesizedtyper   _prims_commoncheck_significant_stridesstride)nvrvdescsame_stridesidxs        r   check_tensor_metadata_matchesr6   F   s2   D>>7799		!!!ddff#P#Pbggii#P#PRWWYY#P#P!!!8rxDDFF!L!LBH!L!L"(!L!L+EE
B% F  L#   466UURYY[[UUbiikkUUsUUU    r   c                   	 t                    sJ t          j        |           \  }}t          j        |          \  }}t          |          t          |          k    s)J t          |           dt          |                       t	          t          t          |                    ||          D ]6\  	}}t          |t          j                  s!t          ||	fd           7d S )Nr(   c                  "                   d S )Nz output  )r3   is   r   <lambda>z(check_metadata_matches.<locals>.<lambda>\   s    6L6L6L6L r   )
r+   pytreetree_flattenlenziprange
isinstancer   Tensorr6   )
nrr3   n_vals_n_specr_vals_r_specr1   r2   r:   s
     `      @r   check_metadata_matchesrI   R   s    D>>)!,,OFG)!,,OFG v;;#f++%%%#f++'H'H3v;;'H'H%%%s6{{++VV<< N N	2r"el++ 	%b".L.L.L.L.LMMMMN Nr   c                       e Zd Zd Zd ZdS )Litc                     || _         d S Ns)selfrO   s     r   __init__zLit.__init__`   s    r   c                     | j         S rM   rN   )rP   s    r   __repr__zLit.__repr__c   s	    vr   N)__name__
__module____qualname__rQ   rS   r9   r   r   rK   rK   _   s2              r   rK   ac           	          t          | t          j                  rOt          dt	          |                                            d|                                  d| j         d          S | S )Nztorch.empty_strided(, z, dtype=r*   )rA   r   rB   rK   tupler,   r0   r-   )rW   s    r   _fmtr[   g   se    !U\"" T5??TTahhjjTT!'TTT
 
 	
 r   op	final_keyc                      ddl m  t          j        j        j        j        k    rS dt          j        dt          j	        dt          f fd}|S )Nr   )FakeTensorModeargskwargsr   c                    	
              		fd}d }t           j        j                                        5  t	                      5  t          j        || |f          \  }}t          j        |||f          \  
	5   |i |}d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y    j        g| R i |}
fd}t          |||           |S )Nc                 l   t          | t          j                  rt          j        |           rmt          j        |           }|                                 |                                k    sJ |                                 |                                k    sJ n| }                    |          S | S rM   )rA   r   rB   _is_functional_tensor_from_functional_tensorr,   r0   from_tensor)trD   	fake_modes     r   fakeify_defunzCmake_crossref_functionalize.<locals>.handler.<locals>.fakeify_defun|   s    !U\** 0.q11 	5a88A
 6688qvvxx////88::33333A ,,Q///Hr   c                 b    t          | t          j                  r|                                 S | S rM   )rA   r   rB   detach)rg   s    r   maybe_detachzBmake_crossref_functionalize.<locals>.handler.<locals>.maybe_detach   s)    !U\** xxzz!r   c                      d                     t          j        d D             d                                 D                                 }  d|  dS )NrY   c              3   d   K   | ]+}t          t          j        t          |                    V  ,d S rM   )reprr<   tree_mapr[   ).0rW   s     r   	<genexpr>zMmake_crossref_functionalize.<locals>.handler.<locals>.desc.<locals>.<genexpr>   s6      IIT&/$2233IIIIIIr   c              3   Z   K   | ]&\  }}| d t          j        t          |           V  'dS )=N)r<   rp   r[   )rq   kvs      r   rr   zMmake_crossref_functionalize.<locals>.handler.<locals>.desc.<locals>.<genexpr>   sT         Aq 99vtQ7799     r   (r*   )join	itertoolschainitems)fmt_argsr\   orig_f_argsorig_f_kwargss    r   r3   z:make_crossref_functionalize.<locals>.handler.<locals>.desc   sw    yyII[III $1$7$7$9$9    H &&8&&&&r   )	r   utils_python_dispatch_disable_current_modesr&   r<   rp   _op_dkrI   )r`   ra   ri   rl   f_argsf_kwargsf_rrD   r3   rh   r}   r~   r_   r]   r\   s            @@@r   handlerz,make_crossref_functionalize.<locals>.handlery   s   "N$$		 	 	 	 	 	 	 	 K(??AA		. 		.%''		. 		.  &}tVnMMFH)/vx0* *&K  . .b&-H--. . . . . . . . . . . . . . .		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. BIi1$111&11
	' 
	' 
	' 
	' 
	' 
	' 
	' 	sAt,,,sY   C	7B, 	B	B,BB,BB, C,B0	0C3B0	4CC
C)torch._subclasses.fake_tensorr_   r   r   aten
lift_freshdefaultr   r`   ra   r   )r\   r]   r   r_   s   `` @r   make_crossref_functionalizer   p   s     =<<<<< 
UY^&...4rw 4") 4 4 4 4 4 4 4 4 4l Nr   c               #   6  K   t                      D ]+} |                     t          j        j        j                   ,	 t                      5  t          j        	                    dd          5  d V  d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   t                      D ]+} |                     t          j        j        j                   ,d S # t                      D ]+} |                     t          j        j        j                   ,w xY w)Nz-torch._dispatch.python.CROSSREF_FUNCTIONALIZET)
r   _uncache_dispatchr   r   r	   r    r
   unittestmockpatch)r\   s    r   enable_crossref_functionalizer      s     %'' A A
UX1?@@@@E$&&	 	M OQUVV	 	 EEE		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 *++ 	E 	EB  !5!CDDDD	E 	E)++ 	E 	EB  !5!CDDDD	EsS   C !B-A>2B>B	BB	B	C BC BC <D),ry   unittest.mockr   collections.abcr   
contextlibr   typingr   r   r   typing_extensionsr   r   torch._C
torch._opstorch.utils._python_dispatchtorch.utils._pytreer   _pytreer<   r	   __all__r   _DisablePythonDispatcherr   _EnablePythonDispatcherr
   _EnablePreDispatchr   CROSSREF_FUNCTIONALIZEr   r   _ops
OpOverloadr   r&   r6   rI   rK   objectr[   r   r   r9   r   r   <module>r      s=           $ $ $ $ $ $ % % % % % % + + + + + + + + + + ' ' ' ' ' '       # # # # $ $ $ $ $ $ $ $ $             V
U
Ux8  8; h1  Yt__WT]]0%**?!@ 0 0 0 04 @ @ @	 	 	
N 
N 
N       F v    ?
b"f%?2=?
8BF[()? ? ? ?H E E E E Er   