
    8hK@                     
   S SK r S SKrS SKrS SKrS SKJrJr  S SKJr  S SK	J
r
  S SKJrJr  S SKJr  S SKJr  \R$                  " \5      r\R+                  \R,                  5         " S S	5      r " S
 S5      r " S S5      rg)    N)IterableSequence)Optional)GraphModule)_get_qualified_nameNode)OperatorSupportBase)fuse_by_partitionsc                   n    \ rS rSr SS\\   S\\\      4S jjrS\	4S jr
S\4S	 jrS\4S
 jrS rSrg)	Partition   Nidnodesc                 Z    Xl         Ub  [        R                  U5      U l        g 0 U l        g N)r   dictfromkeysr   )selfr   r   s      [/var/www/fran/franai/venv/lib/python3.13/site-packages/torch/fx/passes/infra/partitioner.py__init__Partition.__init__   s$     -2->T]]5)
B
    returnc                 ,    [        U R                  5      $ r   )strr   r   s    r   __repr__Partition.__repr__       4::r   nodec                 >    U R                   R                  US 05        g r   )r   updater   r    s     r   add_nodePartition.add_node   s    

4,'r   c                     U R                   U	 g r   )r   r#   s     r   remove_nodePartition.remove_node    s    JJtr   c                 ,    [        U R                  5      $ r   )lenr   r   s    r   sizePartition.size#   r   r   r   r   )NN)__name__
__module____qualname____firstlineno__r   intr   r   r   r   r   r$   r'   r+   __static_attributes__ r   r   r   r      sS    JNG3-G/7/GG# (T ( r   r   c                   :    \ rS rSrS\4S jrS\S\\   4S jrSr	g)	_DependencyViewer'   graph_modulec                 F   [         R                  " [        5      U l        [	        UR
                  R                  5       H_  nUR                   HL  nU R                  U   R                  U5        U R                  U   R                  U R                  U   5        MN     Ma     g r   )
collectionsdefaultdictsetdownstreamsreversedgraphr   usersaddr"   )r   r8   r    output_nodes       r   r   _DependencyViewer.__init__(   s{    &2237\//556D#zz  &**;7  &--d.>.>{.KL  * 7r   r    r   c                      U R                   U   $ r   r=   r#   s     r   downstreams_of _DependencyViewer.downstreams_of1   s    %%r   rE   N)
r.   r/   r0   r1   r   r   r   r<   rF   r3   r4   r   r   r6   r6   '   s'    M[ M&4 &CI &r   r6   c                       \ rS rSr   SS\S\S\S\\\	      S\\\	      SS4S	 jjr
S
\S\4S jrS\\   4S jr SS\\   S\	S\4S jjrS\\   4S jrSS\	S\4S jjrSrg)CapabilityBasedPartitioner5   Nr8   operator_supportallows_single_node_partitionnon_compute_ops!allowed_single_node_partition_opsr   c                 x    Xl         X l        X0l        Ub  UO/ U l        Ub  UO/ U l        [        U5      U l        g r   )r8   rK   rL   rM   rN   r6   dependency_viewer)r   r8   rK   rL   rM   rN   s         r   r   #CapabilityBasedPartitioner.__init__6   sM     ) 0,H)2A2MSU 1< . 	.
 "3<!@r   r    c                 |    U R                   R                  [        U R                  R	                  5       5      U5      $ r   )rK   is_node_supportedr   r8   named_modulesr#   s     r   _is_node_supported-CapabilityBasedPartitioner._is_node_supportedI   s4    $$66""0023T
 	
r   c           	      R  ^ ^^^^^ [         R                  " [        5      m0 m0 m0 n0 m0 m[        R                  " 5       nS[
        S[
        4UUUUUU 4S jjnS[        S[        [
           4UUUU4S jjn[        R                  S5        [        T R                  R                  R                  5       H  n0 nT R                  U5      (       a&  UT;  a   [        U5      nXqU'   UTU'   U" XW5        S Xg'   [!        TR#                  5       [$        R&                  " S5      S	9 H	  u  pxS Xg'   M     [)        UR+                  5       5      n	[-        U	5      S:  d  M  U	S
   n
U	SS   H  nU" X5      u  pM     M     [        R                  S5        0 nT R                  R                  R                   H  nSnUR.                   H0  nUR0                  S:w  d  [3        UR4                  5      S:w  d  M.  Sn  O   U(       d  MN  TR7                  US 5      nUR.                   H  nTR7                  US 5      U:w  d  M  XU'   M!     M     UR#                  5        H  u  p_U" X_5        M     T R8                  (       Gd	  [        R                  S5        SS1nUR;                  [        T R<                  5      5      n/ nTR#                  5        H  u  nnS
nUR                   Hy  nUR0                  S:X  d  M  [?        UR4                  5      (       d   e[3        UR4                  5      U;  a  US-  n[3        UR4                  5      T R@                  ;   d  Mt  US-  nM{     US::  d  M  URC                  U5        M     U H  nTU	 M     [        R                  S5        TR#                  5        HA  u  nn[        R                  SUUR                   Vs/ s H  oURD                  PM     sn5        MC     TRG                  5        Vs/ s H  nURI                  5       S
:  d  M  UPM     sn$ s  snf s  snf )Nself_idother_idc                 (  >^ ^^^ TT    R                   mTT   R                   mS[        [           4U	UUU
UU U4S jjnTT    TT   -  nUR                  TT5        U" U5      (       a  T S4$ T TpT[	        T5      [	        T5      :  a  XTpTTU   R                   R                  TU   R                   5        TU   R                    H  nUT	U'   M
     TU	 [        TU   TU   5      TU'   TU	 T
U   R                  T
U   5      T
U'   T
U	 UTU'   TU	 US4$ )Nall_user_nodesc                    > U  Hw  n[        5       nT
R                  R                  U5       HK  nUT;   d  UT;   a      gUT;   d  M  TU   nXB;   a  M&  T	U   nTU;   d  TU;   a      gUR                  U5        MM     My     g)NTF)r<   rP   rF   rA   )r[   	user_nodevisited_partition_ids	path_nodepartition_idp_map
assignmentrY   other_nodespartition_mapr   rX   
self_nodess         r   dfs_iter_find_cycleiCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partition.<locals>.dfs_iter_find_cyclel   s    !/I,/E)%)%;%;%J%J9%U	 %
2i;6N#' %
2+5i+@L  ,D ($1,$?E&%/8u3D'+155lC+ &V "04 r   FT)r   r<   r   difference_updater*   r"   minunion)rX   rY   rf   r[   merge_id
removed_idr    rc   re   rb   rd   partition_userspartitions_by_idpartitions_orderr   s   ``     @@r   maybe_merge_partitionLCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partitiong   sX   )'288J*84::KCI  < -W58QQN,,[*E #>22 ~% $+Hj:[!11'1* X&,,334DZ4P4V4VW(4::#+
4  ; !,), *,<Z,H*X& !,&3H&=&C&Cj)'M(# j)(6OH%
+T>!r   r    r   c                 8  > S[         S[        4UU4S jjnU T;   a  TTU       R                  U 5        Uc  TR                  U 5        g UT;  a3  UTU '   [	        X/S9TU'   [        U R                  5      TU'   U" X5        g UTU '   TU   R                  U 5        g )Nr    r   c                    > U R                    HE  nTR                  US 5      nUc  M  TU   R                  U5        TU   R                  TU   5        MG     g r   )r@   getrA   r"   )r    r   r]   	target_idrb   rd   s       r   _update_partition_mapgCapabilityBasedPartitioner.propose_partitions.<locals>.merge_single_node.<locals>._update_partition_map   sT     "&I *y$ ?I ,%b)--i8%b)00y1IJ	 ",r   r-   )r   r2   r'   popr   r<   r@   r$   )r    r   rv   rb   rd   rm   rn   s      r   merge_single_nodeHCapabilityBasedPartitioner.propose_partitions.<locals>.merge_single_node   s    KD Kc K K z! D!12>>tDzt$++#%
4 '0Bf'E $&)$**o#%d/#%
4  $--d3r   zProposing partitions...   )keyr   z=Reassigning getitem nodes to its producer node's partition...Tcall_functionz_operator.getitemFz'Filtering out single node partitions...ztorch.ops.aten.viewzPartitions proposed:zpartition #%s: %s)%r:   r;   r<   	itertoolscountr2   r   r   loggerdebugr>   r8   r?   r   rU   nextsorteditemsoperator
itemgetterlistkeysr*   r@   opr   targetrt   rL   rj   rM   callablerN   appendnamevaluesr+   )r   nodes_ordernew_partition_idrp   ry   r    merge_candidatesr`   _merge_candidates_listrX   rY   nodes_reassignmentis_tuple_outputuserr   default_non_compute_opsrM   partitions_to_remove	partitioncompute_node_countrb   rd   rm   rn   ro   s   `                    @@@@@r   propose_partitions-CapabilityBasedPartitioner.propose_partitionsN   s    )4(?(?(D ')
  	
  	
  	
  	 %??,
F	"3 F	"# F	" F	"P	4D 	4hsm 	4 	40 	./T..44::;D02 &&t,,Z1G#$45$0D!1= .!$515 . $* &&(h.A.A!.D$ 26 .$
 %))9)>)>)@$A!()A-/2 5ab 9H "7w!IJGQ !:3 <> 	TU.0%%++11D"O

GG.*4;;7;NN&+O # ^^D$/ JJD!~~dD1R73540 ' 2  +002HDd' 3 000LLBC'<>Q&R#5;;C@T@T<UVO.0 !1!7!7!9I%&"%OODww/1'4444.t{{;?R.!3./<#EEF /!3. , &*(//3 ": +$R( + 	+,-335MB	LL#R	)P)))P 6 (8'>'>'@
'@)INNDTWXDXI'@
 	
 *Q
s   P:P$P$
partitionsprefixc                     [         R                  S5        [        U R                  U Vs/ s H  o3R                  PM     snUS9$ s  snf )NzFusing partitions...r   )r   r   r
   r8   r   )r   r   r   r   s       r   fuse_partitions*CapabilityBasedPartitioner.fuse_partitions  sA     	+,!.89j__j9
 	
9s   A
c                   ^^^^^	^
 [        U R                  5      mS[        4U4S jjm0 m	0 m
S[        S[         [           S[         [           4UUU	4S jjmS[        S[         [           S[         [           4UUU
4S jjmU H  n[        5       nUR                   Hi  nT" U5      (       d  M  T" U[        UR                  5      U5      (       d$  T" U[        UR                  5      U5      (       d  MX  UR	                  U5        Mk     [        U5      S:w  d  M  U H  nUR                  R                  US 5        M!     M     g )Nr    c                 `   > U R                   S:H  =(       a    [        U R                  5      T;   $ )Nr}   )r   r   r   )r    rM   s    r   is_non_compute_nodeVCapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_non_compute_node-  s+    ?* H'4Gr   r   removed_nodesc                    > U R                   S:X  d
  X;  d  X;   a  gU T;   a  TU    $ T" U 5      (       a.  U R                   H  nT" X1U5      (       a  M  STU '     g   STU '   gSTU '   gNplaceholderTF)r   all_input_nodes)r    r   r   input_nr   is_transparent_input_nodetransparent_input_nodess       r   r   \CapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_transparent_input_node7  s     =())...t44"4((#33G4WWW8=/5$  4 15'-,1#D)r   c                    > U R                   S:X  d
  X;  d  X;   a  gU T;   a  TU    $ T" U 5      (       a.  U R                   H  nT" X1U5      (       a  M  STU '     g   STU '   gSTU '   gr   )r   r@   )r    r   r   output_nr   is_transparent_output_nodetransparent_output_nodess       r   r   ]CapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_transparent_output_nodeL  s     =())///55"4(( $

H5 ]  :?06$ !+ 26(.-2$T*r   r   )r<   rM   r   r   rA   r*   rx   )r   r   r   r'   r    r   r   r   rM   r   r   s        @@@@@@r   remove_bookend_non_compute_ops9CapabilityBasedPartitioner.remove_bookend_non_compute_ops*  s   d223	d 	 5757 		#&t9	=@Y	 	*		#&t9	=@Y	 	. $I &)UK!&t,,-dC	4H+VV1c)//2K   OOD) ( ;1$'DOO''d3 ( $r   c                 D    U R                  5       nU R                  X!S9nU$ )Nr   )r   r   )r   r   r   fused_gms       r   partition_and_fuse-CapabilityBasedPartitioner.partition_and_fuseu  s(    ,,.
''
'Br   )rN   rL   rP   r8   rM   rK   )FNN)fused_)r.   r/   r0   r1   r   r	   boolr   r   r   r   r   rU   r   r   r   r   r   r   r3   r4   r   r   rI   rI   5   s    
 .337EIA!A .A '+	A
 "(3-0A ,4HSM+BA 
A&
t 
 

N
DO N
b :B	
y/	
36	
		
I4i I4V K  r   rI   )r:   r~   loggingr   collections.abcr   r   typingr   torch.fx.graph_moduler   torch.fx.noder   r    torch.fx.passes.operator_supportr	   !torch.fx.passes.utils.fuser_utilsr
   	getLoggerr.   r   setLevelWARNINGr   r6   rI   r4   r   r   <module>r      se        .  - 3 @ @ 
		8	$    (& &C Cr   