
    Ǆg                     ~   d dl Z d dlZd dl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 g dZej                   j"                  Zej                   j&                  Zej                   j*                  ZdZdeej0                  j2                     fdZed        Zd	 Zd
 Z G d d      ZdedefdZ d Z!ed        Z"y)    N)contextmanager)Iterator)enable_python_dispatcherno_python_dispatcherenable_pre_dispatchFreturnc               #      K   t         j                  D ]D  } t        t         j                  |       }|D ]#  }t        ||      }|D ]  }t        ||        % F yw)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/mcse/projects/flask_80/flask-venv/lib/python3.12/site-packages/torch/_dispatch/python.pyall_py_loaded_overloadsr      sb     $ ii 0%))R( 	0GWg.F" 0fh//0	00s   AAc               #   n  K   t         j                  j                  t         j                  j                  j                        } t         j                  j                         }| rt        j                          	 d  | rt        j                  |       y y # | rt        j                  |       w w xY ww)N)reapply_views)r
   _C&_dispatch_tls_is_dispatch_key_includedDispatchKeyFunctionalize$_functionalization_reapply_views_tls_disable_functionalization_enable_functionalization)f_tlsf_rvs     r   suspend_functionalizationr   1   s     HH;;**E 8888:D((*@++$? 5++$? s   A6B59B =B5B22B5c           	         t        |      sJ | j                         |j                         k(  s0J  |        d| j                          d|j                                 | j                  |j                  k(  s(J  |        d| j                   d|j                          t        j                  j                  | |d      \  }}|s4J  |        d| j                          d|j                          d| d       y )	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_matchesr/   @   s    D>>779	!PdfXXbggi[RWWYK#PP!88rxxLDF88BHH:T"((!LL++EE
B% F L# 	U
&BIIK=RYY[M9McURSTU    c                   	 t              sJ t        j                  |       \  }}t        j                  |      \  }}t        |      t        |      k(  sJ t        |       dt        |              t	        t        t        |            ||      D ]2  \  	}}t        |t        j                        s"t        ||	fd       4 y )Nr!   c                                d S )Nz output  )r,   is   r   <lambda>z(check_metadata_matches.<locals>.<lambda>V   s    6L r0   )
r$   pytreetree_flattenlenziprange
isinstancer
   Tensorr/   )
nrr,   n_valsn_specr_valsr_specr*   r+   r4   s
     `      @r   check_metadata_matchesrC   L   s    D>>((+NFF((+NFF v;#f+%H#f+d3v;-'HH%s6{+VV< N	2r"ell+%b".LMNr0   c                       e Zd Zd Zd Zy)Litc                     || _         y Ns)selfrI   s     r   __init__zLit.__init__Z   s	    r0   c                     | j                   S rG   rH   )rJ   s    r   __repr__zLit.__repr__]   s    vvr0   N)__name__
__module____qualname__rK   rM   r3   r0   r   rE   rE   Y   s    r0   rE   ac           	          t        | t        j                        rDt        dt	        | j                                d| j                          d| j                   d      S | S )Nztorch.empty_strided(, z, dtype=r#   )r;   r
   r<   rE   tupler%   r)   r&   )rQ   s    r   _fmtrU   a   sR    !U\\""5?"32ahhj\!''RST
 	
 r0   c                      ddl m  t        j                  j                  j
                  j                  k(  rS  fd}|S )Nr   )FakeTensorModec                    	
         		fd}d }t         j                  j                  j                         5  t	               5  t        j                  || |f      \  }}t        j                  |||f      \  
	5   |i |}d d d        d d d        d d d         j                  g| i |}
fd}t        ||       |S # 1 sw Y   ExY w# 1 sw Y   IxY w# 1 sw Y   MxY w)Nc                 B   t        | t        j                        rt        j                  |       r[t        j                  |       }| j                         |j                         k(  sJ | j                         |j                         k(  sJ | }j                  |      S | S rG   )r;   r
   r<   _is_functional_tensor_from_functional_tensorr%   r)   from_tensor)tr>   	fake_modes     r   fakeify_defunzCmake_crossref_functionalize.<locals>.handler.<locals>.fakeify_defunt   s    !U\\*..q155a8A
 668qvvx///88:333A ,,Q//Hr0   c                 Z    t        | t        j                        r| j                         S | S rG   )r;   r
   r<   detach)r]   s    r   maybe_detachzBmake_crossref_functionalize.<locals>.handler.<locals>.maybe_detach   s!    !U\\*xxz!r0   c                      dj                  t        j                  d D        d j                         D                    }  d|  dS )NrS   c              3   b   K   | ]'  }t        t        j                  t        |             ) y wrG   )reprr6   tree_maprU   ).0rQ   s     r   	<genexpr>zMmake_crossref_functionalize.<locals>.handler.<locals>.desc.<locals>.<genexpr>   s      IT&//$23Is   -/c              3   `   K   | ]&  \  }}| d t        j                  t        |        ( yw)=N)r6   rf   rU   )rg   kvs      r   rh   zMmake_crossref_functionalize.<locals>.handler.<locals>.desc.<locals>.<genexpr>   s3       Aq #QvtQ789s   ,.(r#   )join	itertoolschainitems)fmt_argsoporig_f_argsorig_f_kwargss    r   r,   z:make_crossref_functionalize.<locals>.handler.<locals>.desc   sP    yyI[I$1$7$7$9H T8*A&&r0   )	r
   utils_python_dispatch_disable_current_modesr   r6   rf   _op_dkrC   )argskwargsr_   rb   f_argsf_kwargsf_rr>   r,   r^   rt   ru   rW   	final_keyrs   s            @@@r   handlerz,make_crossref_functionalize.<locals>.handlerq   s    "$		 	 [[))@@B 	.D]D_ 	.%}tVnMFH)/vx0*&K  .&-H-.	. 	. BIIi1$1&1
	' 	sAt,#. .	. 	. 	. 	.s;   C%9C 	C	CC%CCC"	C%%C.)torch._subclasses.fake_tensorrW   r
   r   aten
lift_freshdefault)rs   r   r   rW   s   `` @r   make_crossref_functionalizer   j   s7    < 
UYY^^&&...1f Nr0   c               #   T  K   t               D ]5  } | j                  t        j                  j                  j
                         7 	 t               5  t        j                  j                  dd      5  d  d d d        d d d        t               D ]5  } | j                  t        j                  j                  j
                         7 y # 1 sw Y   TxY w# 1 sw Y   XxY w# t               D ]5  } | j                  t        j                  j                  j
                         7 w xY ww)Nz-torch._dispatch.python.CROSSREF_FUNCTIONALIZET)
r   _uncache_dispatchr
   r   r   r   r   unittestmockpatch)rs   s    r   enable_crossref_functionalizer      s     %' A
UXX11??@AE%' 	)<)<;T*
 	 	 	
 *+ 	EB  !5!5!C!CD	E	 	 	 	
 *+ 	EB  !5!5!C!CD	EsP   AD(
C! !C1C	6C>C! AD(	C	CCC! !AD%%D()#ro   unittest.mockr   
contextlibr   typingr   r
   torch._C
torch._opstorch.utils._python_dispatchtorch.utils._pytreerv   _pytreer6   __all__r   _DisablePythonDispatcherr   _EnablePythonDispatcherr   _EnablePreDispatchr   CROSSREF_FUNCTIONALIZE_ops
OpOverloadr   r   r/   rC   rE   objectrU   r   r   r3   r0   r   <module>r      s      %     # $ $ Vxx88  88;; hh11  0%***?*?!@ 04 @ @	U
N F v :~ 
E 
Er0   