
    iRD                     
   d dl Z d dlZd dlZd dlZd dlmZmZ d dlmZ d dl	m
Z
 d dlmZmZ d dlmZ d dlmZ  ej$                  e      Zej+                  ej,                          G d d	      Z G d
 d      Z G d d      Zy)    N)IterableSequence)Optional)GraphModule)_get_qualified_nameNode)OperatorSupportBase)fuse_by_partitionsc            	       |    e Zd Z	 	 	 ddee   deee      deee      fdZdefdZ	dded	ee   fd
Z
defdZd Zy)	PartitionNidnodesnode_ordersc                     || _         i | _        |m|t        j                  |d       | _        y t	        |      }t	        |      }t        |      t        |      k(  sJ d       t        t        ||            | _        y y )Nz/nodes and node_orders must have the same length)r   r   dictfromkeyslistlenzip)selfr   r   r   
nodes_listnode_orders_lists         [/var/www/html/engine/venv/lib/python3.12/site-packages/torch/fx/passes/infra/partitioner.py__init__zPartition.__init__   s     02
"!]]5$7
!%[
#'#4 :#.>*?? E? "#j2B"CD
     returnc                 ,    t        | j                        S N)strr   r   s    r   __repr__zPartition.__repr__'       4::r   node
node_orderc                 >    | j                   j                  ||i       y r   )r   update)r   r#   r$   s      r   add_nodezPartition.add_node*   s    

4,-r   c                     | j                   |= y r   )r   r   r#   s     r   remove_nodezPartition.remove_node-   s    JJtr   c                 ,    t        | j                        S r   )r   r   r    s    r   sizezPartition.size0   r"   r   )NNNr   )__name__
__module____qualname__r   intr   r   r   r   r!   r'   r*   r,    r   r   r   r      s|     !*./3	ESME 'E hsm,	E&# .T .x} . r   r   c                   .    e Zd ZdefdZdedee   fdZy)_DependencyViewergraph_modulec                 <   t        j                  t              | _        t	        |j
                  j                        D ]\  }|j                  D ]K  }| j                  |   j                  |       | j                  |   j                  | j                  |          M ^ y r   )
collectionsdefaultdictsetdownstreamsreversedgraphr   usersaddr&   )r   r4   r#   output_nodes       r   r   z_DependencyViewer.__init__5   s    &2237\//556 	MD#zz M  &**;7  &--d.>.>{.KLM	Mr   r#   r   c                      | j                   |   S r   )r9   r)   s     r   downstreams_ofz _DependencyViewer.downstreams_of>   s    %%r   N)r-   r.   r/   r   r   r   r8   r@   r1   r   r   r3   r3   4   s'    M[ M&4 &CI &r   r3   c                       e Zd Z	 	 	 ddedededeee      deee      ddfdZ	d	e
defd
Zdee   fdZ	 ddee   dedefdZdee   fdZddedefdZy)CapabilityBasedPartitionerNr4   operator_supportallows_single_node_partitionnon_compute_ops!allowed_single_node_partition_opsr   c                 z    || _         || _        || _        ||ng | _        ||ng | _        t        |      | _        y r   )r4   rC   rD   rE   rF   r3   dependency_viewer)r   r4   rC   rD   rE   rF   s         r   r   z#CapabilityBasedPartitioner.__init__C   sS     ) 0,H)2A2MSU 1< . 	.
 "3<!@r   r#   c                 |    | j                   j                  t        | j                  j	                               |      S r   )rC   is_node_supportedr   r4   named_modulesr)   s     r   _is_node_supportedz-CapabilityBasedPartitioner._is_node_supportedV   s4    $$66""0023T
 	
r   c           	          t        j                  t              i i i }i i t        j                         }dt
        dt
        f fd}dt        dt        t
           dt        t
           ffd}t        j                  d       t        t         j                  j                  j                              D ]  \  }}i } j                  |      r(|vr$t!        |      }|||<   ||<    ||||       d ||<   t#        j%                         t'        j(                  d	      
      D ]
  \  }}	d ||<    t+        |j-                               }
t/        |
      d	kD  s|
d   }|
d	d  D ]  } |||      \  }}	  j1                         D ]I  }t3        t#        |j                  j%                         t'        j(                  d	      d            |_        K t        j                  d       i } j                  j                  j                  D ]  }d}|j4                  D ]-  }|j6                  dk7  st9        |j:                        dk7  s+d} n |sDj=                  |d       }|j4                  D ]  }j=                  |d       |k7  s|||<     |j%                         D ]  \  }} ||d |         j>                  st        j                  d       ddh}|jA                  t         jB                              }g }j%                         D ]  \  }}d}|j                  D ]l  }|j6                  dk(  stE        |j:                        sJ t9        |j:                        |vr|d	z  }t9        |j:                         jF                  v sh|d	z  }n |d	k  s|jI                  |        |D ]  }|=  t        j                  d       j%                         D ]>  \  }}t        j                  d||j                  D cg c]  }|jJ                   c}       @ j1                         D cg c]  }|jM                         dkD  s| c}S c c}w c c}w )Nself_idother_idc                         j                      j                   dt        t           f	
 fd}       z  }|j                          ||      r dfS  }}t	              t	              k  r||}}|   j                   j                  |   j                          |   j                   D ]  }|	|<   	 |= t        |   |         |<   |= 
|   j                  
|         
|<   
|= ||<   |= |dfS )Nall_user_nodesc                     | D ]g  }t               }
j                  j                  |      D ]=  }|v s|v r  y|v s|   }||v r	|   }|v s|v r  y|j                  |       ? i y)NTF)r8   rH   r@   r=   )rQ   	user_nodevisited_partition_ids	path_nodepartition_idp_map
assignmentrO   other_nodespartition_mapr   rN   
self_nodess         r   dfs_iter_find_cycleziCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partition.<locals>.dfs_iter_find_cycley   s    !/ DI,/E)%)%;%;%J%J9%U D	 %
2i;6N#' %
2+5i+@L  ,/DD ($1,$?E&%/8u3D'+155lC+DD4 r   FT)r   r8   r   difference_updater   r&   minunion)rN   rO   r\   rQ   merge_id
removed_idr#   rY   r[   rX   rZ   partition_userspartitions_by_idpartitions_orderr   s   ``     @@r   maybe_merge_partitionzLCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partitiont   s`   )'288J*84::KCI  < -W58QQN,,[*E #>2 ~% $+HjH:[!11'18* X&,,334DZ4P4V4VW(4:: ,#+
4 , !,), *,<Z,H*X& !,&3H&=&C&Cj)'M(# j)(6OH%
+T>!r   r#   r$   r   c                 B   dt         dt        ffd}| v r|       j                  |        |j                  |        y |vr>|| <   |J t	        || g|g      |<   t        | j                        |<    || |       y || <   |   j                  | |       y )Nr#   r   c                     | j                   D ]B  }j                  |d       }||   j                  |       |   j                  |          D y r   )r<   getr=   r&   )r#   r   rS   	target_idrX   rZ   s       r   _update_partition_mapzgCapabilityBasedPartitioner.propose_partitions.<locals>.merge_single_node.<locals>._update_partition_map   s[     "& KI *y$ ?I ,%b)--i8%b)00y1IJ	Kr   )r   r   r   )r   r0   r*   popr   r8   r<   r'   )r#   r$   r   rj   rX   rZ   rb   rc   s       r   merge_single_nodezHCapabilityBasedPartitioner.propose_partitions.<locals>.merge_single_node   s    KD Kc K z! D!12>>tDzt$++#%
4 !---'0$j\( $ '*$**o#%dB/#%
4  $--dJ?r   zProposing partitions...   )keyr   T)rn   reversez=Reassigning getitem nodes to its producer node's partition...call_functionz_operator.getitemFz'Filtering out single node partitions...ztorch.ops.aten.viewzPartitions proposed:zpartition #%s: %s)'r6   r7   r8   	itertoolscountr0   r   r   loggerdebug	enumerater:   r4   r;   r   rL   nextsorteditemsoperator
itemgetterr   keysr   valuesr   r<   opr   targetrh   rD   r_   rE   callablerF   appendnamer,   )r   nodes_ordernew_partition_idre   rl   r$   r#   merge_candidatesrV   _merge_candidates_listrN   rO   	partitionnodes_reassignmentis_tuple_outputuserr   default_non_compute_opsrE   partitions_to_removecompute_node_countrX   rZ   rb   rc   rd   s   `                     @@@@@r   propose_partitionsz-CapabilityBasedPartitioner.propose_partitions[   s    )4(?(?(D ')
  	
  	
  	
  	 %??,
F	"3 F	"# F	" F	"P	@D 	@hsm 	@RU 	@ 	@6 	./ )(43D3D3J3J3P3P*Q R 	JJ02 &&t,Z1G#$45$0D!1= .!$
LA15 . $* &&(h.A.A!.D$ 6a 26 .6
 %))9)>)>)@$A!()A-/2 5ab 9 JH "7w!IJGQJ3	J> *002 	I"OO))+1D1DQ1GQUIO	 	TU.0%%++11 	6D"O

 GG.*4;;7;NN&+O ^^D$/ JJ 6D!~~dD1R735*406	6  +002 	.HD"dD"-	. 00LLBC'<>Q&R#5;;C@T@T<UVO.0 !1!7!7!9 4I%&"%OO 	4Dww/1'444.t{{;?R.!3./<#EEF /!3.	4 &*(//34 + )$R() 	+,-335 	MB	LL#R	)P$)))P	 (8'>'>'@
#INNDTWXDXI
 	
 *Q
s   
Q8QQ
partitionsprefixc                     t         j                  d       t        | j                  |D cg c]  }|j                   c}|      S c c}w )NzFusing partitions...r   )rs   rt   r
   r4   r   )r   r   r   r   s       r   fuse_partitionsz*CapabilityBasedPartitioner.fuse_partitions6  s?     	+,!.89Y__9
 	
9s   A
c                 L  	
 t        | j                        dt        ffdi 	i 
dt        dt         t           dt         t           f	fddt        dt         t           dt         t           f
fd|D ]  }t               }|j                  D ]W  } |      s |t        |j                        |      s |t        |j                        |      sG|j	                  |       Y t        |      dk7  s|D ]  }|j                  j                  |d           y )Nr#   c                 T    | j                   dk(  xr t        | j                        v S )Nrp   )r}   r   r~   )r#   rE   s    r   is_non_compute_nodezVCapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_non_compute_nodeE  s+    ?* H'4Gr   r   removed_nodesc                     | j                   dk(  s| |vs| |v ry| v r|    S  |       r(| j                  D ]  } |||      rd| <    y d| <   yd| <   yNplaceholderTF)r}   all_input_nodes)r#   r   r   input_nr   is_transparent_input_nodetransparent_input_nodess       r   r   z\CapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_transparent_input_nodeO  s     =(	)M)...t44"4(#33 %G4WiW8=/5$% 15'-,1#D)r   c                     | j                   dk(  s| |vs| |v ry| v r|    S  |       r(| j                  D ]  } |||      rd| <    y d| <   yd| <   yr   )r}   r<   )r#   r   r   output_nr   is_transparent_output_nodetransparent_output_nodess       r   r   z]CapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_transparent_output_noded  s     =(	)M)///55"4( $

 %H5 )] :?06$% 26(.-2$T*r   r   )r8   rE   r   r   r=   r   rk   )r   r   r   r*   r#   r   r   r   rE   r   r   s        @@@@@@r   remove_bookend_non_compute_opsz9CapabilityBasedPartitioner.remove_bookend_non_compute_opsB  s   d223	d 	 5757 		#&t9	=@Y	*		#&t9	=@Y	. $ 	4I &)UK! *&t,-dC	4H+V1c)//2K  OOD)* ;1$' 4DOO''d34	4r   c                 L    | j                         }| j                  ||      }|S )Nr   )r   r   )r   r   r   fused_gms       r   partition_and_fusez-CapabilityBasedPartitioner.partition_and_fuse  s*    ,,.
''
6'Br   )FNN)fused_)r-   r.   r/   r   r	   boolr   r   r   r   r   rL   r   r   r   r   r   r   r1   r   r   rB   rB   B   s    
 .337EIA!A .A '+	A
 "(3-0A ,4HSM+BA 
A&
t 
 

Y
DO Y
x :B	
y/	
36	
		
I4i I4V K r   rB   )r6   rq   loggingry   collections.abcr   r   typingr   torch.fx.graph_moduler   torch.fx.noder   r    torch.fx.passes.operator_supportr	   !torch.fx.passes.utils.fuser_utilsr
   	getLoggerr-   rs   setLevelWARNINGr   r3   rB   r1   r   r   <module>r      sh        .  - 3 @ @ 
		8	$    B& &N Nr   