
    pi                        d Z ddl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mZ 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 d	dlmZ d	dlmZmZmZ  ed      Z  ed      Z!ejD                  jF                  Z#e$ee#e%f   ede
f   f   Z&eede
f   e%f   Z' G d dejP                  jR                        Z*e G d d             Z+ G d de      Z,dejZ                  de.e+   dee!e
f   fdZ/y)z?Utilities for lowering subgraphs used by higher order operators    N)	Generator)contextmanager)	dataclass)AnyCallableOptionalTypeVarUnion)	ParamSpec)
OrderedSet   )ir)SubgraphLoweringException)GraphLowering)SimpleCSEHandler)opsVWrapperHandlerT_P.c                       e Zd ZU dZeeej                        ed<   e	ed<   ee
   ed<   eee      ed<   ee   ed<   eej                     ed<   ee   ed<   	 	 dd
ej"                  j$                  de	deee      dee   dd	f
 fdZede
ded   fd       ZdefdZdedd	fdZdddej                  dedefdZdedefdZde
dedeeef   def fdZdedee   deeef   dd	fdZ  xZ!S ) PointwiseSubgraphLoweringz
    Lowers a pointwise subgraph to a single set of buffers with a separate
    lowering object. Errors if buffers are created unexpectedly
    graph_outputs
root_graph_current_opallowed_mutationsadditional_loweringsbuffersmutated_buffersNgmroot_graph_loweringreturnc                     t         |   |       d | _        || _        || _        || _        d | _        t               | _        g | _	        y N)
super__init__r   r   r   r   r   r   r   r   )selfr    r!   r   r   	__class__s        c/opt/services/ai/voice_agent/venv/lib/python3.12/site-packages/torch/_inductor/subgraph_lowering.pyr&   z"PointwiseSubgraphLowering.__init__,   sL     	!-!2$8!  *|    op)NNNc              #   b   K   | j                   }|| _         	 d || _         y# || _         w xY ww)zQSet which op is being processed in call function to know if we can mutate buffersN)r   )r'   r+   previouss      r)   _op_contextz%PointwiseSubgraphLowering._op_context>   s3      ##	('DxDs   /# /	,/c                 R    | j                   d uxr | j                  | j                   v S r$   )r   r   )r'   s    r)   _approved_mutatorz+PointwiseSubgraphLowering._approved_mutatorH   s.    ""$. ;  D$:$::	
r*   namec                     | j                         r| j                  j                  |       y t        d| j                   d      )Nz,Buffer mutation detected during lowering of z. Buffer mutations are only allowed in approved mutation ops. This is an error in the lowering of the subgraph, please file a bug report.)r0   r   addr   r   r'   r1   s     r)   mark_buffer_mutatedz-PointwiseSubgraphLowering.mark_buffer_mutatedN   sJ    !!#  $$T*+>t?O?O>P Q^ ^ r*   Fset_namebufferr7   c                v    | j                         r| j                  j                  ||      }|S t        d      )Nr6   zBuffers cannot be created while lowering a pointwise subgraph. This could be for a good reason (e.g. you're calling an op we can't codegen as a pointwise op), but it could also be a bug. Please file a bug report if you think this should be supportable.)r0   r   register_bufferr   )r'   r8   r7   r1   s       r)   r:   z)PointwiseSubgraphLowering.register_bufferX   s?    !!#??226H2MDK+p r*   c                 .    t        | j                  |      S r$   )getattrr   r4   s     r)   __getattr__z%PointwiseSubgraphLowering.__getattr__c   s    t--r*   targetargskwargsc                    ddl m} | j                  |      5  |t        j                  u r8t        |d   t        t        t        f      rt        | )  |||      cd d d        S | j                  >|| j                  v r0t        |t              sJ  | j                  |   |i |cd d d        S ||vrt        | d       ||   |i |cd d d        S # 1 sw Y   y xY w)Nr   )	loweringsr   z. not supported in subgraph, (missing lowering))loweringrB   r.   operatorgetitem
isinstancelisttupledictr%   call_functionr   
OpOverloadr   )r'   r>   r?   r@   rB   r(   s        r)   rJ   z'PointwiseSubgraphLowering.call_functionf   s     	(f% 	6)))ja4PTBU.Vw,VT6B	6 	6
 ((4T666%fj999<444V<dMfM	6 	6 Y&/hLM  %9V$d5f5	6 	6 	6s   AC$A C.CCc                 8    t        |      dk(  sJ |d   | _        y )Nr   r   )lenr   )r'   r>   r?   r@   s       r)   outputz PointwiseSubgraphLowering.output   s    4yA~~!!Wr*   )NN)"__name__
__module____qualname____doc__r   rG   r   IRNode__annotations__r   
TargetTyper   rK   LoweringDictBufferstrtorchfxGraphModuler&   r   r   r.   boolr0   r5   r:   r   r=   rI   rJ   rH   rN   __classcell__r(   s   @r)   r   r      s   
 DO,,*%%
: 677"<00"))__$ ?C7;HH   + $Jz$:;	
 '|4 
$ (j (Y7G-H ( (
4 
   FK 	bii 	d 	s 	. . .66 6 S#X	6
 
62%S %c
 %DcN %t %r*   r   c                   J    e Zd ZU ej                  ed<   ej                  ed<   y)InputDescriptordtypedeviceN)rO   rP   rQ   rY   ra   rT   rb    r*   r)   r`   r`      s    ;;LLr*   r`   c                        e Zd Zdej                  j
                  deddf fdZdedej                  j                  fdZ	de
e   ddfd	Z xZS )
TracingOpsHandlertracer
num_inputsr"   Nc           	          |j                  dddi       }t        | 	  |       || _        t	        |      D cg c]#  }| j                  j                  dd| di       % c}| _        y c c}w )Nplaceholderr   rc   input)create_proxyr%   r&   rf   rangeplaceholders)r'   rf   rg   parentir(   s        r)   r&   zTracingOpsHandler.__init__   so    $$]E2rB  :&
 KK$$]eA3KRH
 
s   (A*idxc                      | j                   |   S r$   )rm   )r'   rp   s     r)   ri   zTracingOpsHandler.placeholder   s      %%r*   r?   c                 h      j                   j                  ddt         fd|D              fi        y )NrN   c              3   T   K   | ]  }j                   j                  |       ! y wr$   )rf   
create_arg).0ar'   s     r)   	<genexpr>z+TracingOpsHandler.output.<locals>.<genexpr>   s      &OQt{{'='=a'@&Os   %()rf   create_noderH   )r'   r?   s   ` r)   rN   zTracingOpsHandler.output   s-    h&O$&O!O QSU	
r*   )rO   rP   rQ   rY   rZ   Tracerintr&   Proxyri   rH   objectrN   r]   r^   s   @r)   re   re      sW    
uxx 
C 
D 
&s &uxx~~ &
E&M 
d 
r*   re   subgraphinputsr"   c                    dt         dt         dt        t        j                  t        j                  d f   fd}t        |      D cg c]Q  \  }}t        j                  j                  |j                  |j                  t        j                  ||      g       S }}}| j                  }t        |t        j                        }t        j                   |      5   |j"                  |  d d d        t$        j&                  j)                         }t$        j&                  j+                  |j,                        |_        t/        t1        |t3        |                  }	|j4                  J t        j6                  |	      5  g }
|j4                  D ]  }t9        |t        j                        sJ t;        |             |j=                         g k(  sJ t9        |j>                  t        j@                        sJ t9        |j>                  j>                  t        j                        sJ d	}|j>                  j>                  jC                  |      }|
jE                  |        tG        jH                  |
  d d d        t$        j&                  jK                  i |j                        d
tL        jN                  dtL        jP                  dtR        ffd}|S c c}}w # 1 sw Y   xY w# 1 sw Y   txY w)Nloop_idx	input_idxr"   c                 ,    t        j                  |      S r$   )r   ri   )r   r   s     r)   fake_inner_fnz/lower_pointwise_subgraph.<locals>.fake_inner_fn   s     y))r*   )r   )rb   ra   inner_fnranges)r!   )
tracer_clsrc   r?   r@   c                  @     t        j                         g| i |S r$   )r   get_ops_handler)r?   r@   
lowered_gms     r)   r   z*lower_pointwise_subgraph.<locals>.inner_fn   s!    !++-????r*   )*rz   r
   r   Expr	TensorBox	enumerate	Pointwisecreaterb   ra   	functoolspartialgraph_moduler   r   graphset_graph_handlerrunrY   rZ   ry   Graphr(   r   re   rM   r   set_ops_handlerrF   typeget_sizedata
StorageBoxr   appendr   rN   r[   r   r?   r@   r   )r}   r~   r   ro   descgraph_inputsr    pw_subgraphrf   	trace_ops
output_irsout_varrp   ir_outr   r   s                  @r)   lower_pointwise_subgraphr      sh   **"%*	rwwd*	+* !( At 	;;**&&}B	 	 	
L  
		B+BAGGLK	
		[	) '&'
 XX__F88>>V-=-=>>FL !263v;!GHI$$000	
		9	%  
"00 		&Ggr||4Cd7mC4##%+++gllBMM:::gll//>>>C\\&&//4Ff%		& 	

J   %%b&,,7J@ @299 @ @ OU' '   s    AKKC3K)K&)K2)0rR   r   rD   collections.abcr   
contextlibr   dataclassesr   typingr   r   r   r	   r
   typing_extensionsr   rY   torch.utils._ordered_setr    r   excr   r   r   ops_handlerr   virtualizedr   r   r   r   r   _opsrK   rI   rX   rV   rU   rZ   Interpreterr   r`   re   SubgraphrG   r   rc   r*   r)   <module>r      s   E   % % ! : : '  /  *   ) / / CLt_ZZ""
E*c/*HS#X,>>?8CH%s*+
d% 4 4 d%N   

 
(3kk3#'#83b#g3r*   