
    /j^              	          d dl Z d dlZd dlmZmZmZ d dlmZ d dl	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! e
d          Z"deej#        j$                 fdZ%eded         fd            Z&dej'        dej'        deg e(f         ddfdZ)de*de*deg e(f         ddf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ed         fd            Z/dS )    N)Callable	GeneratorIterator)contextmanager)TypeVarUnion)	ParamSpec)DispatchKey)enable_python_dispatcherno_python_dispatcherenable_pre_dispatchF_P_T_R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        [/home/longshao/multi-rider-rag/.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    )NNNc               #   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#nvrvdescc           
         t          |          st          dt          |                     |                                 |                                k    rCt           |             d|                                  d|                                           | j        |j        k    r)t           |             d| j         d|j                   t
          j                            | |d          \  }}|sGt           |             d|                                  d|                                 d| d	          d S )
Ndesc must be callable, got z: sizes  != z: dtype F)	only_cudaz
: strides z (mismatch at index ))	callableAssertionErrortypesizedtyper   _prims_commoncheck_significant_stridesstride)r)   r*   r+   same_stridesidxs        r   check_tensor_metadata_matchesr;   F   sM    D>> IG4::GGHHH	wwyyBGGIIJJ		JJrwwyyJJKKK	x28HHHHbhHHIII+EE
B% F  L#  
tvvYYYY"))++YYSVYYY
 
 	

 
r   nrc                 "  	 t                    st          dt                               t          j        |           \  }}t          j        |          \  }}t          |          t          |          k    r.t          t          |           dt          |                     t          t          t          |                    ||          D ]6\  	}}t          |t          j
                  s!t          ||	fd           7d S )Nr-   r.   c                  "                   d S )Nz output  )r+   is   r   <lambda>z(check_metadata_matches.<locals>.<lambda>d   s    6L6L6L6L r   )r1   r2   r3   pytreetree_flattenlenziprange
isinstancer   Tensorr;   )
r<   r=   r+   n_vals_n_specr_vals_r_specr)   r*   rA   s
     `      @r   check_metadata_matchesrN   X   s   D>> IG4::GGHHH)!,,OFG)!,,OFG 6{{c&kk!!F>>V>>???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eddfdZdefdZdS )Litsr   Nc                     || _         d S NrQ   )selfrQ   s     r   __init__zLit.__init__h   s    r   c                     | j         S rS   rT   )rU   s    r   __repr__zLit.__repr__k   s	    vr   )__name__
__module____qualname__strrV   rX   r@   r   r   rP   rP   g   sQ        # $    #      r   rP   ac           	          t          | t          j                  rOt          dt	          |                                            d|                                  d| j         d          S | S )Nztorch.empty_strided(, z, dtype=r0   )rH   r   rI   rP   tupler4   r8   r5   )r]   s    r   _fmtra   o   se    !U\"" T5??TTahhjjTT!'TTT
 
 	
 r   op	final_keyc                      ddl m  t          j        j        j        j        u rS dt          j        dt          j	        dt          f fd}|S )Nr   )FakeTensorModeargskwargsr   c                    	
              	dt           dt           t          j        j        j        z  f	fd}dt           dt           t          j        z  f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 |}dt          f
fd}t          |||           |S )Ntr   c                 J   t          | t          j                  rt          j        |           rt          j        |           }|                                 |                                k    r9t          d|                                  d|                                           |                                 |                                k    r9t          d|                                  d|                                           n| }                    |          S | S )Nzsize mismatch: r.   zstride mismatch: )	rH   r   rI   _is_functional_tensor_from_functional_tensorr4   r2   r8   from_tensor)ri   r=   	fake_modes     r   fakeify_defunzCmake_crossref_functionalize.<locals>.handler.<locals>.fakeify_defun   s    !U\** 0.q11 5a88A
 vvxx16688++,-Wqvvxx-W-WQVVXX-W-WXXXxxzzQXXZZ//,L

LL

LL   0
 A ,,Q///Hr   c                 b    t          | t          j                  r|                                 S | S rS   )rH   r   rI   detach)ri   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 )Nr_   c              3   d   K   | ]+}t          t          j        t          |                    V  ,d S rS   )reprrC   tree_mapra   ).0r]   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)rC   rv   ra   )rw   kvs      r   rx   zMmake_crossref_functionalize.<locals>.handler.<locals>.desc.<locals>.<genexpr>   sT         Aq 99vtQ7799     r   (r0   )join	itertoolschainitems)fmt_argsrb   orig_f_argsorig_f_kwargss    r   r+   z:make_crossref_functionalize.<locals>.handler.<locals>.desc   sw    yyII[III $1$7$7$9$9    H &&8&&&&r   )r   r   _subclassesfake_tensor
FakeTensorrI   utils_python_dispatch_disable_current_modesr(   rC   rv   _op_dkr\   rN   )rf   rg   ro   rr   f_argsf_kwargsf_rr=   r+   rn   r   r   re   rc   rb   s            @@@r   handlerz,make_crossref_functionalize.<locals>.handler   s]   "N$$		R 	B):)F)Q$Q 	 	 	 	 	 	(	B 	2#4 	 	 	 	 K(??AA		. 		.%''		. 		.  &}tVnMMFH)/vx0* *&K  . .b&-H--. . . . . . . . . . . . . . .		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. 		. BIi1$111&11
	'c 
	' 
	' 
	' 
	' 
	' 
	' 
	' 
	' 	sAt,,,sZ   <D7C.	CC.CC.CC."D.C2	2D5C2	6DD	D	)torch._subclasses.fake_tensorre   r   r   aten
lift_freshdefaultr   rf   rg   r   )rb   rc   r   re   s   `` @r   make_crossref_functionalizer   x   s     =<<<<< 
UY^&...8rw 8") 8 8 8 8 8 8 8 8 8t 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)rb   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)0r   unittest.mockr   collections.abcr   r   r   
contextlibr   typingr   r   typing_extensionsr	   r   torch._C
torch._opstorch.utils._python_dispatchtorch.utils._pytreer   _pytreerC   r
   __all__r    _DisablePythonDispatcherr   _EnablePythonDispatcherr   _EnablePreDispatchr   CROSSREF_FUNCTIONALIZEr   r   r   _ops
OpOverloadr   r(   rI   r\   r;   objectrN   rP   ra   r   r   r@   r   r   <module>r      s           9 9 9 9 9 9 9 9 9 9 % % % % % % ! ! ! ! ! ! ! ! ' ' ' ' ' '       # # # # $ $ $ $ $ $ $ $ $             V
U
Ux8  8; h1  Yt__WT]]WT]]0%**?!@ 0 0 0 04 @9-=#> @ @ @ @

,
.6r3w.?
	
 
 
 
$Nf N NxC7H NT N N N N       F v    C
b"f%C2=C
8BF[()C C C CP Ey1A'B E E E E E Er   