
    Ph+)                    V   d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZmZ d dlmZ d dlZd dlmZ d d	lmZmZ d
dlmZmZmZ erd dlmZ  e       Z G d d      Z G d d      Z G d de      Zd)dZ G d d      Z  G d d      Z! G d d      Z"e jG                           ede       Z$ ede      Z% ede      Z& ede      Z' ede      Z( ede      Z) ed e      Z* ed!e      Z+ ed"e      Z, G d# d$      Z- G d% d&      Z. e.       Z/e Z0 G d' d(      Z1 e1       Z2y)*    )annotationsN)contextmanager)chain)local)AnyCallableTYPE_CHECKINGUnion)patch)IndentedBuffer)inplace_methodsmagic_methods   )reduction_num_outputs	sympy_strsympy_symbol)GraphLoweringc                  *    e Zd ZdZddZd Zd Zd Zy)Virtualizedz
    A global variable that redirects via thread local variable

    This allows us to swap in different op implementations in codegen.
    c                &    d| | _         || _        y )N__torchinductor_)_key_default)selfvnamedefaults      fC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/_inductor/virtualized.py__init__zVirtualized.__init__   s    +E73	    c                      j                         t        t         j                  |       t         fd       } |       S )Nc               3  f   K   	 d  j                          y # j                          w xY wwN)_set_handler)priorr   s   r   ctxz%Virtualized._set_handler.<locals>.ctx'   s+     )!!%(!!%(s   1 1.1)_get_handlersetattrthreadlocalr   r   )r   valuer%   r$   s   `  @r   r#   zVirtualized._set_handler#   s<    !!#TYY.		) 
	) ur   c                v    	 t        t        | j                        S # t        $ r | j	                         cY S w xY wr"   )getattrr(   r   AttributeErrorr   r   s    r   r&   zVirtualized._get_handler0   s3    	#;		22 	#==?"	#s    88c                6    t        | j                         |      S r"   )r+   r&   )r   names     r   __getattr__zVirtualized.__getattr__6   s    t((*D11r   N)r   str)__name__
__module____qualname____doc__r   r#   r&   r0    r   r   r   r      s     #2r   r   c                      e Zd Zy)NullHandlerN)r2   r3   r4   r6   r   r   r8   r8   :   s    r   r8   c                  "     e Zd ZdZ fdZ xZS )NullKernelHandleraO  
    We need access `V.kernel.removed_buffers` in DeferredLine class when there
    is no kernel in the context. This happens when codegening the wrapper.
    Initialize `removed_buffers` and `inplaced_to_remove` explicitly so we don't
    need call 'getattr' with default value which is error prone to typo in
    attribute name.
    c                ^    t         |           t               | _        t               | _        y r"   )superr   setremoved_buffersinplaced_to_remove)r   	__class__s    r   r   zNullKernelHandler.__init__G   s"    "u"%%r   )r2   r3   r4   r5   r   __classcell__)r@   s   @r   r:   r:   >   s    ( (r   r:   c                b    t        | t        j                        rt        |       S t	        |       S r"   )
isinstancesympyExprr   r1   )as    r   _arg_strrG   M   s#    !UZZ |q6Mr   c                  H    e Zd Zd Zedd       Zeddd       Zed        Zy)	MockHandlerc                    dk(  ryfd}|S )Nr/   rI   c                     | D cg c]  }t        |       }}|j                  d |j                         D               d ddj                  |       dS c c}w )Nc              3  0   K   | ]  \  }}| d |   yw)=Nr6   ).0kvs      r   	<genexpr>z9MockHandler.__getattr__.<locals>.inner.<locals>.<genexpr>Z   s     ?1A3ass   zops.(, ))rG   extenditemsjoin)argskwargsrF   fargsr/   s       r   innerz&MockHandler.__getattr__.<locals>.innerX   sW    *./$QXa[$E/LL???$q5!1 2!44 0s   Ar6   r   r/   r[   s    ` r   r0   zMockHandler.__getattr__T   s    6> 	5
 r   c                $    d|  d |        d| dS )Nzops.masked(rS   rT   r6   )maskbodyothers      r   maskedzMockHandler.masked_   s    TF"TVHBugQ77r   c                2    t        dt        |        d      S )NrR   rT   )r   r1   )	index_varsizechecks      r   indirect_indexingzMockHandler.indirect_indexingc   s    aI/q122r   c                    d }t        t        j                         t        j                               D ]  \  }}t	        | | ||              y )Nc                $     t          fd       }|S )Nc                 "     j                   |  S r"   )format)rX   format_strings    r   r[   z:MockHandler._init_cls.<locals>.make_handler.<locals>.innerj   s    +}++T22r   )staticmethod)rk   r[   s   ` r   make_handlerz+MockHandler._init_cls.<locals>.make_handleri   s    3 3 Lr   )r   r   rV   r   r'   )clsrm   r/   rk   s       r   	_init_clszMockHandler._init_clsg   sG    	 $)!?#8#8#:$
D- C|M:;$
r   Nreturnr1   T)rq   zsympy.Symbol)	r2   r3   r4   r0   rl   ra   rf   classmethodro   r6   r   r   rI   rI   S   sC    	 8 8 3 3 < <r   rI   c                  @    e Zd Zd Zeddd       Zd	dZ	 	 d
dZd Zy)KernelFormatterHandlerc                d    || _         t        d      | _        t        j                         | _        y )Nr   )parent_handlerr   output	itertoolscountvar_counter)r   rw   s     r   r   zKernelFormatterHandler.__init__w   s%    ,$Q'$??,r   Nc                   ddl m} |||gn|g}|ddgndg}t        t                     }|j                  j                  d      5  |j                  j                  ddj                  |       d       d d d        t        ||      D ]r  \  }}|s	dj                  |D 	cg c]/  }	t        t        |	t        t        j                  f      rd	n|	      1 c}	      }
|j                  j                  |
 d
|        t t        j                  |      5  t!        j"                  |dd      5   | | }|j%                  |      cd d d        cd d d        S # 1 sw Y   xY wc c}	w # 1 sw Y   nxY wd d d        y # 1 sw Y   y xY w)Nr   )FlexibleLayoutindexrindexzdef inner_fn(rS   z):_ = allow_indexingT)irr}   ru   rI   rx   indent	writelinerW   zipr1   rC   intrD   IntegerVset_ops_handlerr   objectgetvalue)ir_fnr~   r   r}   rX   names	formatterr/   argrP   lhsresults               r   ir_to_stringz#KernelFormatterHandler.ir_to_string|   sa   &"("4v5''-'9(#y*;=9	$$R(&&tyy7G6H'KL )UD)ID#ii "%!$A :a#u}}1E#FCAN!$   **cU#dV+<= * y)5<<,d,
 D\F%%f-	,
 ,
)) )(
,
 ,
)))s6   /E"4E!
E;,E&	E;E&E/	+E;;Fc                      fd}|S )Nc                      t        j                        | i |}dk(  r|S dt        j                         }j                  j                  | d|        |S )Nrf   tmpr   )r+   rw   nextr{   rx   r   )rX   rY   linevarnamer/   r   s       r   r[   z1KernelFormatterHandler.__getattr__.<locals>.inner   si    574..5tFvFD**D!1!1234GKK!!WIS"78Nr   r6   r\   s   `` r   r0   z"KernelFormatterHandler.__getattr__   s    	 r   c                B   | j                   j                  ||||      }t        |      }t        |      D cg c]  }dt	        | j
                          }}| j                  j                  dj                  |       d|        |dkD  rt        |      S |d   S c c}w )Nr   ,r   r   r   )
rw   	reductionr   ranger   r{   rx   r   rW   tuple)	r   dtype	src_dtypereduction_typer)   r   
num_valuesr   varnamess	            r   r   z KernelFormatterHandler.reduction   s     "",,UI~uU*>:
<A*<MN<Mqc$t//012<MN(!3 4Cv>?",q.uXAhqkA Os   Bc                r    | j                   j                  d|        | j                   j                         S )Nzreturn )rx   r   r   )r   r   s     r   r   zKernelFormatterHandler.getvalue   s.    x01{{##%%r   r"   rp   )rq   zCallable[..., str])rq   zUnion[tuple[str, ...], str])	r2   r3   r4   r   rl   r   r0   r   r   r6   r   r   ru   ru   v   s4    -
 . .2
B	$B&r   ru   c                      e Zd Zd Zd Zy)WrapperHandlerc                    || _         y r"   )_inner)r   r[   s     r   r   zWrapperHandler.__init__   s	    r   c                .    t        | j                  |      S r"   )r+   r   )r   items     r   r0   zWrapperHandler.__getattr__   s    t{{D))r   N)r2   r3   r4   r   r0   r6   r   r   r   r      s    *r   r   opsgraphreal_inputs	fake_modekerneldebuginterpreteraot_compilationcurrent_nodec                  ^    e Zd ZU dZded<   d Zd Zd Zd Zd Z	d	 Z
d
 Zd Zd Zd Zd Zy)OpsValuea,  The return type of most ops calls.

    This exists so we can overload magic methods, and write mathematical
    expressions much more fluently. So instead of

        ops.add(ops.mul(ops.mul(ops.sub(ops.mul(_Ap2, x), _Ap3), x), x), _1)

    we can write

        (_Ap2 * x - _Ap3) * x * x + _1

    r   r)   c                    || _         y r"   r)   )r   r)   s     r   r   zOpsValue.__init__   s	    
r   c                ,    t        | j                        S r"   )r1   r)   r-   s    r   __str__zOpsValue.__str__   s    4::r   c                "    d| j                   dS )Nz	OpsValue(rT   r   r-   s    r   __repr__zOpsValue.__repr__   s    4::.**r   c                .    t         j                  | |      S r"   )r   addr   r`   s     r   __add__zOpsValue.__add__       wwtU##r   c                .    t         j                  | |      S r"   )r   mulr   s     r   __mul__zOpsValue.__mul__   r   r   c                .    t         j                  | |      S r"   )r   subr   s     r   __sub__zOpsValue.__sub__   r   r   c                ,    t         j                  |       S r"   )r   negr-   s    r   __neg__zOpsValue.__neg__   s    wwt}r   c                .    t         j                  | |      S r"   )r   truedivr   s     r   __truediv__zOpsValue.__truediv__   s    {{4''r   c                .    t         j                  | |      S r"   )r   floordivr   s     r   __floordiv__zOpsValue.__floordiv__   s    ||D%((r   c                .    t         j                  | |      S r"   )r   modr   s     r   __mod__zOpsValue.__mod__   r   r   c                .    t         j                  | |      S r"   )r   powr   s     r   __pow__zOpsValue.__pow__   r   r   N)r2   r3   r4   r5   __annotations__r   r   r   r   r   r   r   r   r   r   r   r6   r   r   r   r      sE     J+$$$()$$r   r   c                  H    e Zd ZdZd Zed        Zed        Zedd       Zy)
OpsWrapperzThis wraps any returned IR values into an `OpsValue` instance, so that we
    can overload the magic methods for writing mathematical expressions fluently.
    c                    fd}|S )Nc                    | D cg c]  }t         j                  |       }}|j                         D ci c]  \  }}|t         j                  |       }}}t         j                   t	        t
              |i |      S c c}w c c}}w r"   )r   _unwraprV   _wrapr+   _ops)rX   rY   rF   new_argsrO   rP   
new_kwargsr/   s          r   r[   z%OpsWrapper.__getattr__.<locals>.inner   s}    7;<t!
**1-tH<?E||~N~tq!!Z//22~JN##$7GD$$7$PZ$PQQ =Ns
   B Br6   r\   s    ` r   r0   zOpsWrapper.__getattr__   s    	R
 r   c                    t        | t        t        f      rt        d | D              S t        | t              r| j                  S | S )Nc              3  F   K   | ]  }t         j                  |        y wr"   )r   r   rN   rP   s     r   rQ   z%OpsWrapper._unwrap.<locals>.<genexpr>  s     :1++A.s   !)rC   listr   r   r)   xs    r   r   zOpsWrapper._unwrap  s9    a$'::::a"77Nr   c                h    t        | t        t        f      rt        d | D              S t        |       S )Nc              3  2   K   | ]  }t        |        y wr"   )r   r   s     r   rQ   z#OpsWrapper._wrap.<locals>.<genexpr>  s     0a!as   )rC   r   r   r   r   s    r   r   zOpsWrapper._wrap  s+    a$'0a000{r   c                Z    t         j                  |       } t        j                  | ||      S r"   )r   r   r   rf   )r~   rd   re   s      r   rf   zOpsWrapper.indirect_indexing  s)     ""5)%%eT599r   Nrr   )	r2   r3   r4   r5   r0   rl   r   r   rf   r6   r   r   r   r      sH        
 : :r   r   c                     e Zd ZU eZeZeZej                  Zde	d<   ej                  Zde	d<   ej                  Zde	d<   ej                  Zde	d<   ej                  Zde	d<   ej                  Zde	d	<   ej                  Zde	d
<   ej                  Zde	d<   ej                  Zde	d<   ej                  Zde	d<   ej                  Zde	d<   ej                  Zde	d<   ej                  Zde	d<   ej                  Zde	d<   e dd       Z!e dd       Z"e d        Z#e d        Z$e d        Z%e d        Z&e d        Z'e d        Z(e d        Z)y)_VzCallable[[Any], Any]r   zCallable[[], Any]get_ops_handlerzCallable[[GraphLowering], Any]set_graph_handlerset_real_inputsget_real_inputsset_fake_modeget_fake_modeset_kernel_handlerset_debug_handlerset_interpreter_handlerset_aot_compilationget_aot_compilationset_current_nodeget_current_nodec                *    t         j                         S )z9The operator handler specific to the current codegen task)r   r&   r-   s    r   r   z_V.ops2  s       ""r   c                *    t         j                         S z#The graph currently being generated)_graphr&   r-   s    r   r   z_V.graph7  s     ""$$r   c                *    t         j                         S )znon-fake example inputs)_real_inputsr&   r-   s    r   r   z_V.real_inputs<  s     ((**r   c                *    t         j                         S r   )
_fake_moder&   r-   s    r   r   z_V.fake_modeA  s     &&((r   c                *    t         j                         S )z$The kernel currently being generated)_kernelr&   r-   s    r   r   z	_V.kernelF  s     ##%%r   c                *    t         j                         S r"   )_debugr&   r-   s    r   r   z_V.debugK  s    ""$$r   c                *    t         j                         S r"   )_interpreterr&   r-   s    r   r   z_V.interpreterO  s    ((**r   c                *    t         j                         S r"   )_aot_compilationr&   r-   s    r   r   z_V.aot_compilationS  s    ,,..r   c                *    t         j                         S r"   )_current_noder&   r-   s    r   r   z_V.current_nodeW  s    ))++r   N)rq   _MockHandler)rq   r   )*r2   r3   r4   rI   ru   r   r   r#   r   r   r&   r   r   r   r   r   r   r   r   r   r  r   r  r   r  r   r  r   r   r
  r   r   propertyr   r   r   r   r   r   r   r   r   r6   r   r   r   r     s   K3#N,0,=,=O)=)-):):O&:8>8K8K5K,8,E,EO)E)5)B)BO&B*4*A*AM'A'1'>'>M$>/6/C/C,C.4.A.A+A4@4M4M1M0@0M0M-M-=-J-J*J-:-G-G*G*7*D*D'D# # % % + + ) ) & & % % + + / / , ,r   r   rp   )3
__future__r   ry   
contextlibr   r   	threadingr   typingr   r   r	   r
   unittest.mockr   rD   torch._inductor.utilsr   torch.fx.graphr   r   utilsr   r   r   torch._inductor.graphr   r(   r   r8   r:   rG   rI   ru   r   ro   r   r   r   r   r  r  r  r  r
  r   r   r   r  r   r   r6   r   r   <module>r     s4   "  %   6 6   0 9 A A3g2 2D	 	( ( <  <F7& 7&t* *    5+&	Wk	*=+6k2

h 1
2	Wk	*=+60+> NK8/$ /$d: :D l;, ;,| Dr   