
    Phd                     d   U d dl Z d dlZd dlZd dlmZmZmZmZmZm	Z	m
Z
mZmZ d dlmZ d dlm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mZ d d	lmZ  d d
l!m"Z"m#Z#m$Z$m%Z%  ejL                  d      Z'ejP                  e)d<    G d dejT                        Z+ G d dejX                        Z-y)    N)	AnyCallablecastDictListOptionalSetTupleType)fx)clone_subgraph
get_outputis_leaf_subgraph)partial_lower)_PyTreeCodeGen
PythonCode)Argument)record_function)_pytree)tree_flattentree_maptree_map_onlytree_unflattenIterGraphModuleloggerc                       e Zd ZdZ	 	 	 d=dej
                  dej
                  dej
                  deej                     deed      d	ee	e
ef      f fd
Zdej                  dej
                  deej                     fdZdej
                  de
dededef
 fdZde
dej                  fdZ fdZ fdZdeej                     dej
                  dedefdZdeej                     dej
                  deddfdZdeej                     dej                  ddfdZdeej                     dej                  ddfdZdeej                     dej                  ddfdZ	 	 	 d=d ed!ef   deeed!f      dee	e
ef      d"ee   dej                  f
 fd#Zd$ej                  ddf fd%Zde jB                  jD                  fd&e
d"ee   d'edej                  f fd(Z#d>d)ed"ee   dej                  f fd*Z$d? fd+Z%dej                  dej                  ddfd,Z&dej                  dej                  ddfd-Z'dej                  deed!f   ddfd.Z(dej                  de	e
ef   ddfd/Z)d0 fd1d2dej                  d3ej                  d4eej                  gef   deej                     fd5Z*dej                  d6eddfd7Z+dej                  d6eddfd8Z,d?d9Z-d?d:Z.d?d;Z/d?d<Z0 xZ1S )@	IterGrapha  ``IterGraph`` is used to perform cross-iteration optimization.

    ``IterGraph`` keeps track of the 3 graphs, self (the original graph), setup graph, and
    cleanup graph. The 3 graphs should be identical copies of a ``fx.Graph``.

    IterGraph subclass fx.Graph to override the necessary APIs that will be used
    when constructing a optimization, e.g., communication fusion. IterGraph also
    provides APIs that originally belong to fx.Node and all these APIs will have
    ``node_`` prefix. For example, ``IterGraph.node_prepend`` is the equivalence
    of ``fx.Node.prepend``. Note that all the optimizations must be constructed
    using these APIs.
    N
orig_graphsetup_graphcleanup_graphowning_module
tracer_clsz	fx.Tracertracer_extrasc                    t         |   |||       | j                  |i d      }t        j                  |j
                        | _        t        |t              sJ |\  }}	t         | !  |t        |	dd              || _
        || _        | j                  | j                  t        t        j                  t                     f| _        i | _        i | _        d| _        d| _        t)        | j*                  | j                  j*                  | j                  j*                        D ]$  \  }
}}|| j                   |
<   || j"                  |
<   & d| _        y )NT)return_output_nodetype)	type_exprFr   )super__init__
graph_copycopydeepcopy_codegen
isinstancetupleoutputgetattrr   r    r   r   Graph_all_graphs_setup_mapping_cleanup_mapping_freeze_cross_iter_movement_cross_iter_block_countzipnodesnum_extra_output)selfr   r   r    r!   r"   r#   output_vals
output_valold_output_valnode
setup_nodecleanup_node	__class__s                tC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/distributed/_spmd/iter_graph_module.pyr)   zIterGraph.__init__&   s8    	
MBooj"oN j&9&9:+u---%0"
NzW^VT-RS&*57#2
 798:+0('($.1JJ((..0B0B0H0H/
*D*l )3D%*6D!!$'	/
 !"    r?   graphreturnc                     || j                   k(  r| j                  j                  |d       S || j                  k(  r| j                  j                  |d       S |S N)r   r4   getr    r5   )r;   r?   rE   s      rC   _lookup_nodezIterGraph._lookup_nodeQ   sU    D$$$&&**466d(((((,,T488rD   funcargskwargsc                 z    || k7  r|n!t        t        j                  t                     } t	        ||      |i |S rH   )r   r   r2   r(   r1   )r;   rE   rK   rL   rM   fx_graphrB   s         rC   _fx_graph_callzIterGraph._fx_graph_callX   s8     ',tmUbhh9P&wx&777rD   c                      G d d      }g }| j                   D ]B  }|r| j                  ||      }|	J d       |}|j                   t        ||      |             D  ||      S )Nc                   *    e Zd Zdee   fdZd Zd Zy)/IterGraph._insert_context.<locals>._InsertPointinsert_pointsc                     || _         y rH   )rT   )r;   rT   s     rC   r)   z8IterGraph._insert_context.<locals>._InsertPoint.__init__`   s
    %2"rD   c                      y rH    r;   s    rC   	__enter__z9IterGraph._insert_context.<locals>._InsertPoint.__enter__c   s    rD   c                 L    | j                   D ]  }|j                  |||        y rH   )rT   __exit__)r;   r&   valuetbinsert_points        rC   r[   z8IterGraph._insert_context.<locals>._InsertPoint.__exit__f   s$    $($6$6L ))$r: %7rD   N)__name__
__module____qualname__r   r   r)   rY   r[   rW   rD   rC   _InsertPointrS   _   s    3d3i 3;rD   rb   zCannot handle None case now.)r3   rJ   appendr1   )r;   rK   r?   rb   rT   rE   actual_nodes          rC   _insert_contextzIterGraph._insert_context^   sy    		; 		; %%E"//e<".N0NN."  !5!5k!BC & M**rD   c                 ^    | j                   rt        | 	  |      S | j                  d|      S )Ninserting_after)r6   r(   rg   re   r;   r?   rB   s     rC   rg   zIterGraph.inserting_afteru   s0    ++7*400##$5t<<rD   c                 ^    | j                   rt        | 	  |      S | j                  d|      S )Ninserting_before)r6   r(   rj   re   rh   s     rC   rj   zIterGraph.inserting_beforez   s0    ++7+D11##$6==rD   subgraph
erase_nodec                    t        |      }g }t        |      }|D ]c  }t        j                  |j                  i |j
                  }|D ]3  }	t        |	t        j                        s|	|v r#|j                  |	       5 e |rt               }
t        |      D ]  }t        |j                        dk(  rt        t        |j                  j                                     }||k(  rt!        |j                  |j
                  f      \  }}||vr|j                  j#                          ngt%        |j&                        j)                  d      r5t+        t        |            D ]  }||   |k(  s|j                  d   ||<    nt-        d|       t/        ||      \  |_        |_        t1        |j                  j                               D ]"  }||
v s|j                  j3                  |       $ |j                  rt-        d| d      | j5                  |d|       |
j7                  |        | j8                  rPt;        t<        t        j                     |j                  d   d         j?                  |       |j                  d   }nt        |j@                  tB              r|j@                  }t1        |j                  d         }|j                  |       |jD                  jF                  J t/        t;        t<        tH           |j                  d         |jD                  jF                        }t!        |d	f      \  }}|jD                  jK                  |
      |_"        n|j                  d   |f}| j5                  |d|       | j5                  |d|       tL        jO                  dt%        |             t        |      S )zTurn the inputs of a subgraph into the extra output of the entire graph.

        If ``erase_node`` is True, the subgraph will be erased from the graph -- essentially forward the inputs
        of the subgraph to the output of the graph.
           z
aten.copy_r   z0IterGraph does not how to forward the output of z[IterGraph has not supported moving the nodes that produce users output result. Error node: .rl   N)out_specr0   z-Extended outputs from the subgraph inputs: %s)(r   setpytreearg_tree_leavesrL   rM   r.   r   Noderc   reversedlenusersnextiterkeysr   clearstrtarget
startswithrangeRuntimeErrorr   listpoprP   addr:   r   r   extendr-   r   pytree_inforq   r   _replacer   info)r;   rk   rE   rl   r0   inputs	all_nodesr?   node_inputs_inputerasedkeyflatten_argsspeciuser
new_outputcodegenoriginal_tree_out_rq   s                        rC   _forward_subgraph_inputsz"IterGraph._forward_subgraph_inputs   s8    E""%h-	D 00$))Kt{{KK%!&"''2Y&f% &   UF *tzz?a'tDJJOO$567Cf}-96;;:V-W*d|3 JJ,,. -88F &+3|+<%=#/?d#:6:iilLO &> #/"RSWRX Y#  6DLRV5W2V]
 !!23Dv~

t, 4 ::&''+fA/ 
 ##E<>

4 G +N   bggAr 23::6BQJ %...9..!&++a.1
!!&)**33???$2cFKKN3W5H5H5Q5Q%!
 +,=t+DE8&-&9&9&B&BH&B&U#$kk!nf5
E<8E8Z8CS[Q6{rD   extra_inputc                 $   |j                   D cg c]  }t        |j                        dk(  s| }}|sJ d       | j                  |d|d         5  t	        t        t        t        |            D cg c]%  }| j                  |dd| j                   d|       ' c}            }ddd       t        |      }d}	|D ]  }t        |j                  |j                  f      \  }
}g }|
D ]K  }t        |t        j                        r||v r|j                  |       3|j                  |	          |	d	z  }	M t!        ||      \  |_
        |_         |	t#              k(  sJ d
t#        |       d|	        t        |j$                  t&              r|j$                  j(                  j*                  |j$                  }t!        ||j(                  j*                        }t        t-        t	        |      |z               \  }}|j(                  j/                  |      |_        |D ]1  }|j(                  j0                  j                  |j2                         3 |j(                  j/                  |      |_        yyyc c}w c c}w # 1 sw Y   xY w)zCreate extra input nodes and forward the input nodes to the ``subgraph``.

        The external input nodes of ``subgraph`` (nodes that are not in ``subgraph``) will replaced by the newly
        created input nodes.
        placeholderzNo placeholders are foundrg   rp   cross_iter_input_r   Nr   rn   zMore inputs than needed z > )in_spec)r9   r}   oprP   r   rv   r   r7   rr   r   rL   rM   r.   r   ru   rc   r   rw   r-   r   r   r   r/   r   	orig_argsname)r;   rk   rE   r   r?   placeholdersr   new_input_nodesr   new_input_indexr   r   new_node_inputs
input_noder   original_tree_inr   r   	new_inputs                      rC   _forward_inputs_to_subgraphz%IterGraph._forward_inputs_to_subgraph   sz    */VDGG8UV888|  (9<;KL" "*%*<!= ">A ++!)/0L0L/MQqcR
 ">	O M M	D ,dii-E FK O)
!*bgg6*	:Q#**:6#**??+KL#q(O * &4OT%J"DIt{  ##
 
 	Q%c/&:%;3>OP	Q 
 u~~~6**22>nnG-lG<O<O<W<WX%eD1A,B_,T&UVJAw")"5"5">">w">"OG,	##--44Y^^D -")"5"5">">w">"OG ? 7K W MLs(   I;I;!J3*J J JJtarget_nodec                    | j                   rt        d      t        | |      st        d      | xj                  dz  c_        g }|D ]3  }| j                  || j                        }|J |j                  |       5 | j                  || j                  d      }| j                  || j                        }|J d       g }|D ]3  }| j                  || j                        }|J |j                  |       5 t        | j                  ||      }	| j                  |	| j                  |       | j                  || d	      }
|
|k(  sJ |D ]  }|j                  |        | j                  || |
       | j                  j                  D ]*  }t        |j                        d
k(  sd|j                  d<   , | j                  D ]*  }t        |j                        d
k(  sd|j                  d<   , | xj                   |
z  c_        y)a$  Move the ``subgraph`` to the next iteration before ``target_node``.

        The ``subgraph`` is a list of fx.Node and must satisfy the following
        restrictions:
            1. The order of the nodes in ``subgraph`` must obey the topological
               sort order.
            2. The users of the node in ``subgraph`` must be one of the following:
                a.) the user is also a node in ``subgraph``.
                b.) the user is the output of the full graph.
                c.) the node has users (side effect node).
        zEThe cross-iteration movement has been frozen for the given IterGraph.zThe target nodes for ``move_to_next_iter_before`` must satisfy one of the following conditions: 1) the user of the node is in the target nodes, 2) the user is the ouput of the graph, 3) there are no users -- the node is a side-effect node. rn   NT)rk   rE   rl   z The target_cleanup_node is None.)r~   Fr   __hold__)r6   r   r   
ValueErrorr7   rJ   r   rc   r   r    r   r   prependr9   rw   rx   r:   )r;   rk   r   setup_subgraphr?   mapped_nodesetup_extra_inputtarget_cleanup_nodecleanup_subgraphcloned_subgraphmain_extra_inputs              rC   move_to_next_iter_beforez"IterGraph.move_to_next_iter_before  s6    ++ 
  h/S  	$$)$ )+D++D$2B2BCK***!!+.  !99#"" : 
 #//T=O=OP".R0RR.*,D++D$2D2DEK***##K0  )&

 	((T//1B	
  88Te 9 
  #4444D% ((49IJ &&,,D4::!#)-

:& - JJD4::!#)-

:&  	!11rD   r9   c                     | j                   D ]O  }|D cg c]  }| j                  ||       }}| j                  ||      }|J |D ]  }|j                  |        Q y c c}w rH   )r3   rJ   r   r;   r9   r   rE   r?   actual_nodesactual_target_noderd   s           rC   move_beforezIterGraph.move_beforej  sm    %%EGLMutD--dE:uLM!%!2!2;!F%111+"**;7  ,	 &Ms   A c                     | j                   D ]Q  }|D cg c]  }| j                  ||       }}| j                  ||      }|D ]  }|J |j                  |       |} S y c c}w rH   )r3   rJ   rc   r   s           rC   
move_afterzIterGraph.move_afterr  ss    %%EGLMutD--dE:uLM!%!2!2;!F+)555"))+6%0"  , &Ms   A"the_function.r'   c                      j                   rt         	  ||||      S t         fd|      }t         fd|      }t         fd|      }t         fd|      } j                  j                  ||||      }	t         	  ||||      }
 j
                  j                  ||||      }|	 j                  |
<   | j                  |
<   |
S )Nc                 t    t        | t        j                        rj                  | j                        S | S rH   r.   r   ru   rJ   r   argr;   s    rC   <lambda>z)IterGraph.call_function.<locals>.<lambda>  6    #rww' ))#t/?/?@ rD   c                 t    t        | t        j                        rj                  | j                        S | S rH   r   r   s    rC   r   z)IterGraph.call_function.<locals>.<lambda>  r   rD   c                 t    t        | t        j                        rj                  | j                        S | S rH   r.   r   ru   rJ   r    r   s    rC   r   z)IterGraph.call_function.<locals>.<lambda>  6    #rww' ))#t/A/AB rD   c                 t    t        | t        j                        rj                  | j                        S | S rH   r   r   s    rC   r   z)IterGraph.call_function.<locals>.<lambda>  r   rD   )r6   r(   call_functionr   r   r    r4   r5   )r;   r   rL   rM   r'   
setup_argssetup_kwargscleanup_argscleanup_kwargsr@   	main_noderA   rB   s   `           rC   r   zIterGraph.call_function{  s     ++7(tVYOO 	

   	
   	
 " 	
 %%33*lI

 G),fiP	))77,	
 *4I&+7i(rD   to_erasec                 H   | j                   rt        | 	  |      S | j                  || j                        }|J d       | j                  j                  |       t        | 	  |       | j                  || j
                        }| j
                  j                  |       y )Nzsetup_node is None)r6   r(   rl   rJ   r   r    )r;   r   r@   rA   rB   s       rC   rl   zIterGraph.erase_node  s    ++7%h//&&x1A1AB
%;';;%##J/8$((43E3EF%%l3rD   r   default_valuec                    | j                   rt        | 	  |||      S t        | 	  |||      }| j                  j                  |||      }| j                  j                  |||      }|| j
                  |<   || j                  |<   |S rH   )r6   r(   r   r   r    r4   r5   )r;   r   r'   r   main_placeholdersetup_placeholdercleanup_placeholderrB   s          rC   r   zIterGraph.placeholder  s     ++7&tYFF 7.tYN ,,88y-X"00<<)]
 1B,-2E./rD   resultc                      j                   rt         	  ||      S t         	  ||      }t         fd|      }t         fd|      } j                  j                  ||        j
                  j                  ||       |S )Nc                 t    t        | t        j                        rj                  | j                        S | S rH   r   _resultr;   s    rC   r   z"IterGraph.output.<locals>.<lambda>  s6    '277+ !--gt7G7GH rD   c                 t    t        | t        j                        rj                  | j                        S | S rH   r   r   s    rC   r   z"IterGraph.output.<locals>.<lambda>  s6    '277+ !--gt7I7IJ rD   )r6   r(   r0   r   r   r    )r;   r   r'   main_outputsetup_resultcleanup_resultrB   s   `     rC   r0   zIterGraph.output  s    ++7>&)44gnVY7 	
 " 	
 	i8!!.)<rD   c                     | j                   j                          t        |           | j                  j                          y rH   )r   lintr(   r    )r;   rB   s    rC   r   zIterGraph.lint  s0    !rD   c                     | j                   r|j                  |       y| j                  D ]I  }| j                  ||      }|J d       | j                  ||      }|J d       |j                  |       K y)zPrepend node to target_node.NzThe node is NonezThe target node is None)r6   r   r3   rJ   r;   r   r?   rE   rd   r   s         rC   node_prependzIterGraph.node_prepend  s    ++%%%E++D%8K*>,>>*!%!2!2;!F%1L3LL1&&{3 &rD   c                     | j                   r|j                  |       y| j                  D ]Q  }| j                  ||      }|J d| d       | j                  ||      }|J d| d       |j                  |       S y)zAppend node to target_node.NzThe actual node is None, ro   z The actual target node is None, )r6   rc   r3   rJ   r   s         rC   node_appendzIterGraph.node_append  s    ++t$%%E++D%8K*O.GvQ,OO*!%!2!2;!F".A1+a@A.%%k2 &rD   c                 P     j                   r||_        y t        t        j                   fd|      } j                  | j                        }|J ||_        t        t        j                   fd|      } j                  | j                        }|J ||_        ||_        y )Nc                 <    j                  | j                        S rH   rJ   r   _argr;   s    rC   r   z)IterGraph.node_set_args.<locals>.<lambda>      $"3"3D$:J:J"KrD   c                 <    j                  | j                        S rH   rJ   r    r   s    rC   r   z)IterGraph.node_set_args.<locals>.<lambda>	      $"3"3D$:L:L"MrD   )r6   rL   r   r   ru   rJ   r   r    )r;   r?   rL   r   r@   r   rA   s   `      rC   node_set_argszIterGraph.node_set_args  s    ++DI"GGKT

 &&tT-=-=>
%%%$
$GGMt
 ((t/A/AB'''(	rD   c                 P     j                   r||_        y t        t        j                   fd|      } j                  | j                        }|J ||_        t        t        j                   fd|      } j                  | j                        }|J ||_        ||_        y )Nc                 <    j                  | j                        S rH   r   r   s    rC   r   z+IterGraph.node_set_kwargs.<locals>.<lambda>  r   rD   c                 <    j                  | j                        S rH   r   r   s    rC   r   z+IterGraph.node_set_kwargs.<locals>.<lambda>  r   rD   )r6   rM   r   r   ru   rJ   r   r    )r;   r?   rM   r   r@   r   rA   s   `      rC   node_set_kwargszIterGraph.node_set_kwargs  s    ++ DK$GGKV
 &&tT-=-=>
%%%(
&GGMv
 ((t/A/AB''',rD   c                      yNTrW   )r   s    rC   r   zIterGraph.<lambda>'  s    rD   Fpropagate_metareplace_withdelete_user_cbc                    | j                   D ]>  }| j                  ||      }| j                  ||      }|J |j                  |||      }@ S )Nr   )r3   rJ   replace_all_uses_with)	r;   r?   r   r   r   rE   rd   actual_replace_withrets	            rC   node_replace_all_uses_withz$IterGraph.node_replace_all_uses_with#  sj     %%E++D%8K"&"3"3L%"H***33#- 4 C	 & 
rD   r   c                     | j                   D ]V  }| j                  ||      }t        |t        j                        r| j                  ||      }n|}|J d |j
                  |<   X y rH   r3   rJ   r.   r   ru   rx   r;   r?   r   rE   rd   actual_user_nodes         rC   node_add_userzIterGraph.node_add_user6  sh    %%E++D%8K$(#'#4#4T5#A #' ***26K./ &rD   c                     | j                   D ]T  }| j                  ||      }t        |t        j                        r| j                  ||      }n|}|J |j
                  |= V y rH   r   r  s         rC   node_remove_userzIterGraph.node_remove_user@  sf    %%E++D%8K$(#'#4#4T5#A #' ***!!"23 &rD   c                     | j                   D ]F  }t        |j                        dk(  st        |j                        dk7  s5| j                  |d       H y )Nr   r0   r   )r9   rw   rx   r}   r   r  )r;   r?   s     rC   keep_unused_nodeszIterGraph.keep_unused_nodesJ  s?    JJD4::!#DGG(@""44 rD   c                 4   t        | j                        D ]  }|j                  j                  d      r|}!|j                  j                  d      r|}?|j                  j                  d      s[|}| j	                         | j	                  ||        y Nr0   _fused_adam__foreach_add_)rv   r9   r   r   r  )r;   r?   output_node
optim_node	step_nodes        rC   functionalize_optimzIterGraph.functionalize_optimO  s     TZZ(Dyy##H-"%% "
%% !	"":{;""9j9 )rD   c                 ~   | j                   D ]  }t        |j                        D ]  }|j                  j	                  d      r|}!|j                  j	                  d      r|}?|j                  j	                  d      s[|}j
                  j                  d        |j
                  j                  |d          y r	  )r3   rv   r9   r   r   rx   r   )r;   rE   r?   r  r  r  s         rC   defunctionalize_optimzIterGraph.defunctionalize_optime  s    %%E -99''1"&KYY))" "&JYY))# !%I$$((d;OO''
D9 . &rD   c                     d| _         y r   )r6   rX   s    rC   freeze_cross_iter_movementz$IterGraph.freeze_cross_iter_movementv  s
    +/(rD   )NNNrH   rF   N)2r_   r`   ra   __doc__r   r2   r   GraphModuler   r   r}   r   r)   ru   rJ   rP   re   rg   rj   r   boolintr   r   r   r   r   r   r
   r   r   rl   inspect	Signatureemptyr   r0   r   r   r   r   r   r   r  r  r  r  r  r  __classcell__rB   s   @rC   r   r      s   $ 372626)"HH)" XX)" xx	)"
  /)" T+./)"  S#X/)"V  hrww>O 8XX8%(8148@C8	8+C +rww +.=
>
YRWWY.0hhYDHY	Yv6PRWW6P.0hh6PEH6P	6PpV2RWWV246GGV2	V2p8bgg 8RWW 8 81RWW 1BGG 1 1 0404#',sCx(, uXs]+,, c8m,-	,
 C=, 
,\	4277 	4t 	4 $($..44	   C=  	 
 
 $X (3- 277 ,"
4 4rww 44 43rww 3bgg 3$ 3"'' x}1E $ &BGG T#x-5H T . 5F	 gg gg !"''D1	 
bgg&7"'' 7 7 74RWW 4C 4D 45
:,:"0rD   r   c            	            e Zd ZdZ	 	 ddej
                  dededdf fdZdd	Z	d
ej
                  dede
fdZddde
dede
de
fdZedefd       ZdefdZddZddedefdZddZddZ xZS )r   a  ``IterGraphModule`` provides the ability to do cross-iteration optimization.

    Given a ``fx.GraphModule``, main_gm, ``IterGraphModule`` internally
    duplicate it to 3 copies and redirect the ``forward`` request to a different
    ``fx.GraphModule`` based on the iteration count. This allows users to do
    graph optimizations that across iterations (e.g., moving collective wait in
    the backward to the forward of the next iteration).

    Note that users must call the APIs provided by ``IterGraphModule`` or
    ``IterGraph`` to rewrite the graph so that ``IterGraphModule`` can keep the
    data dependency for all 3 graphs.
    Fmain_gm	max_itersenable_inductorrF   Nc                 0   t         |           dt        j                  dt        j                  dt        j                  fd} ||t        j                  |j                              | _         ||t        j                  |j                              | _	         ||t        |j                  | j                  j                  | j                  j                              | _        d| _        || _        t               | _        d| _        d| _        || _        y )NsrcrE   rF   c                 V    t        j                  | |      }t        |di       |_        |S )Nmeta)r   r  r1   r%  )r#  rE   gms      rC   _copy_gmz*IterGraphModule.__init__.<locals>._copy_gm  s'    U+BeVR0BGIrD   r   F)r(   r)   r   r  r2   r+   r,   rE   setup_gm
cleanup_gmr   r  _iter
_max_itersr/   _previous_output_num_extra_output
_is_frozen_enable_inductor)r;   r  r   r!  r'  rB   s        rC   r)   zIterGraphModule.__init__  s     		".. 	 	bnn 	
 !$--*FG"7DMM'--,HIgmmT]]%8%8$//:O:OP

 
#16!" /rD   c                     | j                   sb| j                  j                          | j                  j                  | _        | j
                  rt        | j                        | _        d| _         d| _        y)zSet up the internal states and also get the signal from users that what is the maximum iteration count.

        This method must be called before the forward() is called.
        Tr   N)	r.  rE   r  r:   r-  r/  r   r  r*  rX   s    rC   finalize_setupzIterGraphModule.finalize_setup  sT    
 JJ113%)ZZ%@%@D"$$,T\\:"DO
rD   r&  	last_iterc                     | j                   dkD  rb|| j                  z   } ||i |}|sGt        |      dk(  sJ t        |d         | _        t        | j                        dkD  sJ d       |d   }|S  ||i |}|S )Nr      rp   z*There should be at least one extra output.)r-  r,  rw   r/   )r;   r&  r2  rL   rM   new_argsr0   s          rC   _runzIterGraphModule._run  s    !!A%t445H,V,F6{a'''(-fRj(9%--.2@?@2
  ((FrD   )r2  rL   rM   c                    | xj                   dz  c_         |xs | j                   | j                  k(  }|r+t        j                  d       | j                  }d}d| _         nh| j                   dk(  r$t        j                  d       | j
                  }d}n5| j                  }| j                   dk(  rt        j                  d       d	}nd
}t        |      5   | j                  ||g|i |cd d d        S # 1 sw Y   y xY w)Nrn   zUsing the cleanup graphz$## IterGraphModule: Cleanup Graph ##r   zUsing the setup graphz"## IterGraphModule: Setup Graph ##r4  zUsing the main graphz(## IterGraphModule -- Maybe Compiling ##z## IterGraphModule ##)	r*  r+  r   r   r)  r(  r  r   r6  )r;   r2  rL   rM   r&  profiler_strings         rC   forwardzIterGraphModule.forward  s    

a
>t!>	KK12BDODJZZ1_KK/0BBOBzzQ23"L"9_-499R<T<V< .--s   C44C=c                 J    t        t        | j                  j                        S rH   )r   r   r  rE   rX   s    rC   rE   zIterGraphModule.graph  s    It||1122rD   c                     | j                   j                          | j                  j                          | j                  j                         S rH   )r(  	recompiler)  r  rX   s    rC   r<  zIterGraphModule.recompile  s5    !!!#||%%''rD   c                 n    | j                   j                          | j                   j                  | _        y rH   )rE   r  r:   r-  rX   s    rC   r  z*IterGraphModule.freeze_cross_iter_movement  s$    

--/!%!<!<rD   print_outputc                 8    | j                   j                  |      S rH   )r  print_readable)r;   r>  s     rC   r@  zIterGraphModule.print_readable  s    ||**<88rD   c                    t         j                  d       t         j                  d       t         j                  d| j                  j                         t         j                  d       t         j                  d| j                  j                         t         j                  d       t         j                  d| j
                  j                         y )NzPrinting the three fx.Graph:z1. Setup fx.Graph:%sz2. Main fx.Graph:z3. Cleanup fx.Graph:)r   r   r(  rE   r  r)  rX   s    rC   print_all_graphsz IterGraphModule.print_all_graphs  s~    23()D$----.'(D$,,,,-*+D$////0rD   c                    t         j                  d       t         j                  d       t         j                  d| j                  j                  d             t         j                  d       t         j                  d| j                  j                  d             t         j                  d       t         j                  d| j
                  j                  d             y )NzPrinting the three fx gm:z1. Setup fx.GraphModule:rB  Fz2. Main fx.GraphModule:z3. Cleanup fx.GraphModule:)r   r   r(  r@  r  r)  rX   s    rC   print_all_graph_modulesz'IterGraphModule.print_all_graph_modules  s    /0./D$--66u=>-.D$,,55e<=01D$//88?@rD   )rp   Fr  )T)r_   r`   ra   r  r   r  r  r  r)   r1  r   r6  r9  propertyr   rE   r   r<  r  r}   r@  rC  rE  r  r  s   @rC   r   r   z  s       %	00 0 	0
 
06r~~ $ C " 5: =S =T =S =S =. 3y 3 3(: (
=
94 93 91ArD   ).r+   r  loggingtypingr   r   r   r   r   r   r	   r
   r   torch.nnnntorchr   #torch.distributed._spmd.graph_utilsr   r   r   %torch.distributed._spmd.partial_lowerr   torch.fx.graphr   r   torch.fx.noder   torch.profilerr   torch.utilsr   rs   torch.utils._pytreer   r   r   r   	getLoggerr   Logger__annotations__r2   r   Moduler   rW   rD   rC   <module>rW     s       N N N   
 @ 5 " * ) U U +**+<= =_	0 _	0D@Abii @ArD   