
    i              
          S SK Jr  S SKrS SKJr  S SKJr  S\R                  R                  S\	4S jr
S\R                  R                  S\\R                  R                     S	\\R                  R                     S
\\R                  R                     S\	4
S jrS\R                  R                  S\S\	4S jrS\R                  R                  S\S\	4S jrS\S\4S jrg)    )SetN)ops)ExportedProgramnodereturnc                     U R                   S:H  =(       ai    U R                  [        R                  R                  R
                  R                  [        R                  R                  R
                  R                  4;   $ )z*Check if a node is an index_put operation.call_function)optargettorchr   aten	index_putdefaultedge)r   s    _/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/executorch/exir/passes/reinplace.py_is_index_putr      sV    77o% $++		  (('': +     later_nodesinputsmutable_inputsc                 (    X;   a  gX;  a  gX;   a  gg)NFT )r   r   r   r   s       r   _is_safe_to_reinplacer      s"     		r   exported_programc                 d    U R                   UR                  R                  R                  5       ;   $ )N)r   graph_signatureuser_inputs_to_mutatevalues)r   r   s     r   _is_mutable_user_inputr   ,   s+     	'77MMTTVVr   c                     U R                   UR                  R                  ;  a  gUR                  R                  U R                      nX!R                  R                  R	                  5       ;   $ )NF)r   r   inputs_to_buffersbuffers_to_mutater   )r   r   bufs      r   _is_mutable_bufferr$   4   sV    {{*::LLL

*
*
<
<T[[
IC22DDKKMMMr   epc           	         [        5       n[        5       nU R                  R                   H&  nUR                  S:X  d  M  UR	                  U5        M(     [        U Vs/ s H'  n[        X05      (       d  [        X05      (       d  M%  UPM)     sn5      n[        5       n[        U R                  R                  5       GH5  n[        U5      (       a  UR                  S   n[        XaX$5      (       a  U R                  R                  U5         U R                  R                  [        R                  R                  R                   R"                  UR                  S9nUR$                  S   UR$                  S'   UR'                  U5        U R                  R)                  U5        SSS5        UR	                  U5        GM  GM  UR                  S:X  d  GM  UR+                  UR,                  5        GM8     U $ s  snf ! , (       d  f       N_= f)a  
Pass that loops over nodes in an exported program and collects the first argument
of every call_function node that is a view_copy operation.

Args:
    exported_program: The ExportedProgram to analyze

Returns:
    Set of nodes that are first arguments to view_copy operations
placeholderr   )argsvalNr	   )setgraphnodesr
   addr   r$   reversedr   r(   r   inserting_beforer	   r   r   r   
index_put_r   metareplace_all_uses_with
erase_nodeupdateall_input_nodes)r%   
seen_nodesr   r   mutable_nodesresults	first_argnew_nodes           r   reinplace_passr;   ;   s    &)UJUF77m#JJt   	
%d//3Ed3O 	
M eG( 		!I$YFRRXX..t4!xx550088tyy  6  H ,099U+;HMM%(..x8HH''- 5 I& S WW'd223! )" I3	
 54s   $G8G8BG==
H	)typingr   r   executorch.exir.dialects._opsr   torch.exportr   fxNodeboolr   r   r   r$   r;   r   r   r   <module>rB      s      - ( $ 
((--UXX]]#  &	
 
(
((--+:	NUXX]] No NRV N, ,? ,r   