
    9iRD                     
   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            	           \ rS rSr   SS\\   S\\\      S\\\      4S jjrS\	4S jr
SS	\S
\\   4S jjrS	\4S jrS rSrg)	Partition   Nidnodesnode_ordersc                     Xl         0 U l        Ubn  Uc  [        R                  US 5      U l        g [	        U5      n[	        U5      n[        U5      [        U5      :X  d   S5       e[        [        XE5      5      U l        g g )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         a/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/torch/fx/passes/infra/partitioner.py__init__Partition.__init__   s|     02
"!]]5$7
!%[
#'#4 :#.>*?? E? "#j"CD
     returnc                 ,    [        U R                  5      $ N)strr   r   s    r   __repr__Partition.__repr__'       4::r   node
node_orderc                 <    U R                   R                  X05        g r    )r   update)r   r&   r'   s      r   add_nodePartition.add_node*   s    

4,-r   c                     U R                   U	 g r    )r   r   r&   s     r   remove_nodePartition.remove_node-   s    JJtr   c                 ,    [        U R                  5      $ r    )r   r   r"   s    r   sizePartition.size0   r%   r   )r   r   )NNNr    )__name__
__module____qualname____firstlineno__r   intr   r   r   r!   r#   r*   r.   r1   __static_attributes__ r   r   r   r      s|     !*./3	ESME 'E hsm,	E&# .T .x} . r   r   c                   :    \ rS rSrS\4S jrS\S\\   4S jrSr	g)	_DependencyViewer4   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   r=   r&   output_nodes       r   r   _DependencyViewer.__init__5   s{    &2237\//556D#zz  &**;7  &--d.>.>{.KL  * 7r   r&   r   c                      U R                   U   $ r    rB   r-   s     r   downstreams_of _DependencyViewer.downstreams_of>   s    %%r   rJ   N)
r3   r4   r5   r6   r   r   r   rA   rK   r8   r9   r   r   r;   r;   4   s'    M[ M&4 &CI &r   r;   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)CapabilityBasedPartitionerB   Nr=   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    )r=   rP   rQ   rR   rS   r;   dependency_viewer)r   r=   rP   rQ   rR   rS   s         r   r   #CapabilityBasedPartitioner.__init__C   sM     ) 0,H)2A2MSU 1< . 	.
 "3<!@r   r&   c                 |    U R                   R                  [        U R                  R	                  5       5      U5      $ r    )rP   is_node_supportedr   r=   named_modulesr-   s     r   _is_node_supported-CapabilityBasedPartitioner._is_node_supportedV   s4    $$66""0023T
 	
r   c           	      B	  ^ ^^^^^ [         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[        [
           S[        [
           4UUUU4S jjn[        R                  S5        [        [        T R                  R                  R                  5      5       H  u  pV0 nT R                  U5      (       a'  UT;  a!  [!        U5      nXU'   UTU'   U" XeU5        S Xx'   [#        TR%                  5       [&        R(                  " S	5      S
9 H	  u  pS Xx'   M     [+        UR-                  5       5      n
[/        U
5      S	:  d  M  U
S   nU
S	S   H  nU" X5      u  pM     M     TR1                  5        HH  n[3        [#        UR                  R%                  5       [&        R(                  " S	5      SS95      Ul        MJ     [        R                  S5        0 nT R                  R                  R                   H  nSnUR4                   H0  nUR6                  S:w  d  [9        UR:                  5      S:w  d  M.  Sn  O   U(       d  MN  TR=                  US 5      nUR4                   H   nTR=                  US 5      U:w  d  M  UUU'   M"     M     UR%                  5        H  u  nnU" US U5        M     T R>                  (       Gd	  [        R                  S5        SS1nURA                  [        T RB                  5      5      n/ nTR%                  5        H  u  nnSnUR                   Hy  nUR6                  S:X  d  M  [E        UR:                  5      (       d   e[9        UR:                  5      U;  a  US	-  n[9        UR:                  5      T RF                  ;   d  Mt  US	-  nM{     US	::  d  M  URI                  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  ofRJ                  PM     sn5        MC     TR1                  5        Vs/ s H  oRM                  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)rA   rU   rK   rF   )r`   	user_nodevisited_partition_ids	path_nodepartition_idp_map
assignmentr^   other_nodespartition_mapr   r]   
self_nodess         r   dfs_iter_find_cycleiCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partition.<locals>.dfs_iter_find_cycley   s    !/I,/E)%)%;%;%J%J9%U	 %
2i;6N#' %
2+5i+@L  ,D ($1,$?E&%/8u3D'+155lC+ &V "04 r   FT)r   rA   r   difference_updater   r)   minunion)r]   r^   rk   r`   merge_id
removed_idr&   rh   rj   rg   ri   partition_userspartitions_by_idpartitions_orderr   s   ``     @@r   maybe_merge_partitionLCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partitiont   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'   r   c                 F  > 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;  a:  UTU '   Uc   e[	        X /U/S9TU'   [        U R                  5      TU'   U" X5        g UTU '   TU   R                  X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    )rE   getrF   r)   )r&   r   rb   	target_idrg   ri   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   r   )r   r7   r.   popr   rA   rE   r*   )r&   r'   r   r{   rg   ri   rr   rs   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 !---'0j\( $ '*$**o#%d/#%
4  $--d?r   zProposing partitions...   )keyr   T)r   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)'r?   r@   rA   	itertoolscountr7   r   r   loggerdebug	enumeraterC   r=   rD   r   rZ   nextsorteditemsoperator
itemgetterr   keysr   valuesr   rE   opr   targetry   rQ   ro   rR   callablerS   appendnamer1   )r   nodes_ordernew_partition_idru   r~   r'   r&   merge_candidatesre   _merge_candidates_listr]   r^   	partitionnodes_reassignmentis_tuple_outputuserr   default_non_compute_opsrR   partitions_to_removecompute_node_countrg   ri   rr   rs   rt   s   `                     @@@@@r   propose_partitions-CapabilityBasedPartitioner.propose_partitions[   s[    )4(?(?(D ')
  	
  	
  	
  	 %??,
F	"3 F	"# F	" F	"P	@D 	@hsm 	@RU 	@ 	@6 	./ )(43D3D3J3J3P3P*Q RJ02 &&t,,Z1G#$45$0D!1= .!$LA15 . $* &&(h.A.A!.D$ 26 .$
 %))9)>)>)@$A!()A-/2 5ab 9H "7w!IJGQ !:3 !S> *002I"OO))+1D1DQ1GQUIO 3 	TU.0%%++11D"O

GG.*4;;7;NN&+O # ^^D$/ JJD!~~dD1R735*40 ' 2  +002HD"dD"- 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'>'>'@
'@)NNDTWXDXI'@
 	
 *Q
s   R3RR
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
   r=   r   )r   r   r   r   s       r   fuse_partitions*CapabilityBasedPartitioner.fuse_partitions6  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&   rR   s    r   is_non_compute_nodeVCapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_non_compute_nodeE  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_nodeO  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   rE   )r&   r   r   output_nr   is_transparent_output_nodetransparent_output_nodess       r   r   ]CapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_transparent_output_noded  s     =())///55"4(( $

H5 ]  :?06$ !+ 26(.-2$T*r   r   )rA   rR   r   r   rF   r   r}   )r   r   r   r.   r&   r   r   r   rR   r   r   s        @@@@@@r   remove_bookend_non_compute_ops9CapabilityBasedPartitioner.remove_bookend_non_compute_opsB  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_fuse  s(    ,,.
''
'Br   )rS   rQ   rU   r=   rR   rP   )FNN)fused_)r3   r4   r5   r6   r   r	   boolr   r   r!   r   r   rZ   r   r   r   r   r   r   r8   r9   r   r   rN   rN   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   rN   )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
   	getLoggerr3   r   setLevelWARNINGr   r;   rN   r9   r   r   <module>r      sf        .  - 3 @ @ 
		8	$    B& &N Nr   