
    ^jL                         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
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)GraphModule)_get_qualified_nameNode)OperatorSupportBase)fuse_by_partitionsc            	           e Zd Z	 	 	 ddedz  dee   dz  dee   dz  ddfdZdefdZdded	edz  ddfd
Z	deddfdZ
defdZy)	PartitionNidnodesnode_ordersreturnc                    || _         i | _        |q|t        j                  |d       | _        y t	        |      }t	        |      }t        |      t        |      k7  rt        d      t        t        ||            | _        y y )Nz/nodes and node_orders must have the same length)r   r   dictfromkeyslistlenAssertionErrorzip)selfr   r   r   
nodes_listnode_orders_lists         l/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/fx/passes/infra/partitioner.py__init__zPartition.__init__   s     -/
"!]]5$7
!%[
#'#4 z?c*:&;;(I  "#j2B"CD
     c                 ,    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.size/   r"   r   )NNNr   )__name__
__module____qualname__intr   r   r   r   r!   r'   r*   r,    r   r   r   r      s     '+,0	E$JE ~$E c]T)	E
 
E(# .T .sTz .T .  c r   r   c                   2    e Zd ZdeddfdZdedee   fdZy)_DependencyViewergraph_moduler   Nc                 <   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__4   s    &2237\//556 	MD#zz M  &**;7  &--d.>.>{.KLM	Mr   r#   c                      | j                   |   S r   )r9   r)   s     r   downstreams_ofz _DependencyViewer.downstreams_of=   s    %%r   )r-   r.   r/   r   r   r   r8   r@   r1   r   r   r3   r3   3   s/    M[ MT M&4 &CI &r   r3   c                       e Zd Z	 	 	 	 ddedededee   dz  dee   dz  d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   dd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_opsskip_horizontal_fusionr   c                     || _         || _        || _        ||ng | _        ||ng | _        || _        t        |      | _        y r   )r4   rC   rD   rE   rF   rG   r3   dependency_viewer)r   r4   rC   rD   rE   rF   rG   s          r   r   z#CapabilityBasedPartitioner.__init__B   s[     ) 0,H)2A2MSU 1< . 	.
 '=#!2<!@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_supportedW   s4    $$66""0023T
 	
r   c           	      	    t        j                  t              i i i }i i t        j                         }dt
        t           dt
        t           dt        t           f fddt        dt        dt        t        t        f   f fd}dt        d	t        d z  d
t        d z  dd ffd}t        j                  d       t        t         j                  j                  j                               D ]  \  }}i }d} j#                  |      r*|vr&t%        |      }	|	||<   |	|	<    ||||	       d ||	<   d} j&                  r!|rZ|j(                  D ]  }
|
v sd ||
   <    n;t+        j-                         t/        j0                  d            D ]
  \  }	}d ||	<    t3        |j5                               }t7        |      dkD  s|d   }|dd  D ]  } |||      \  }}  j9                         D ]I  }t;        t+        |j                   j-                         t/        j0                  d      d            |_        K t        j                  d       	 i } j                  j                  j                   D ]  }d}|j(                  D ]-  }
|
j<                  dk7  st?        |
j@                        dk7  s+d} n |sDjC                  |      }|j(                  D ]  }
jC                  |
      |k7  s|||
<     |sn#|j-                         D ]  \  }} ||d |        ʉ jD                  st        j                  d       ddh}|jG                  t         jH                              }g }j-                         D ]  \  }}d}|j                   D ]  }|j<                  dk(  stK        |j@                        s!tM        dtO        |j@                               t?        |j@                        |vr|dz  }t?        |j@                         jP                  v s|dz  } |dk  s|jS                  |        |D ]  }|=  t        j                  d       j-                         D ]>  \  }}t        j                  d||j                   D cg c]  }|jT                   c}       @ j9                         D cg c]  }|jW                         dkD  s| c}S c c}w c c}w )N
user_nodes
source_idsr   c                 >   t        |      }t               }| D ]p  }t        j                  |f	j                  j	                  |            D ];  }j                  |      }||j                  |       |j                  |          = r |j                  |       |S r   )	r8   	itertoolschainrI   r@   getr=   r&   difference_update)
rO   rP   source_ids_setdownstream_partition_ids	user_node	path_node	target_id
assignmentpartition_mapr   s
          r   downstream_partitionszLCapabilityBasedPartitioner.propose_partitions.<locals>.downstream_partitionsr   s     !_N14$' N	!*L$"8"8"G"G	"R" NI !+y 9I ( ,00;,33M)4LMNN %66~F++r   self_idother_idc                         j                      j                   dt        t           dt        f	 fd}       z  }|j	                          ||      r dfS  }}t              t              k  r||}}|   j                   j                  |   j                          |   j                   D ]  }|	|<   	 |= t        |   |         |<   |= |   j                  |         |<   |= ||<   |= |   j                   
|||f             |   j                  |       |   j                  |       |dfS )Nall_user_nodesr   c                    | D ]|  }t               }t        j                  |f
j                  j	                  |            D ]=  }|v s|v r  y|v s|   }||v r	|   }|v s|v r  y|j                  |       ? ~ y)NTF)r8   rR   rS   rI   r@   r=   )ra   rX   visited_partition_idsrY   partition_idp_mapr[   r_   other_nodesr\   r   r^   
self_nodess         r   dfs_iter_find_cycleziCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partition.<locals>.dfs_iter_find_cycle   s    !/ DI,/E)%.__"d&<&<&K&KI&V& D	
 %
2i;6N#' %
2+5i+@L  ,/DD ($1,$?E&%/8u3D'+155lC/DD8 r   FT)
r   r8   r   boolrU   r   r&   minuniondiscard)r^   r_   rh   ra   merge_id
removed_idr#   rf   rg   r[   r]   r\   partition_userspartitions_by_idpartitions_orderr   s   ``     @@r   maybe_merge_partitionzLCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partition   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%
+(#**%nx6LM (#++H5(#++J7T>!r   r#   r$   r   c                 X   dt         dt        dd ffd}| v r|       j                  |        |j                  |        y |vrG|| <   |t	        d      t        || g|g      |<   t        | j                        |<    || |       y || <   |   j                  | |       y )Nr#   r   r   c                 R    |   j                   | j                  |f             y r   )r&   r<   )r#   r   r]   r\   s     r   _update_partition_mapzgCapabilityBasedPartitioner.propose_partitions.<locals>.merge_single_node.<locals>._update_partition_map   s$    b!(()>tzzB5)QRr   z)node_order is required for new partitions)r   r   r   )	r   r0   r*   popr   r   r8   r<   r'   )	r#   r$   r   ru   r[   r]   r\   ro   rp   s	       r   merge_single_nodezHCapabilityBasedPartitioner.propose_partitions.<locals>.merge_single_node   s    SD Sc Sd S z! D!12>>tDzt$++#%
4 %()TUU'0$j\( $ '*$**o#%dB/#%
4  $--dJ?r   zProposing partitions...FT   )keyr   )ry   reversez=Reassigning getitem nodes to its producer node's partition...call_functionz_operator.getitemz'Filtering out single node partitions...ztorch.ops.aten.viewzExpected callable target, got zPartitions proposed:zpartition #%s: %s),r6   r7   r8   rR   countr   r   r0   tupleri   loggerdebug	enumerater:   r4   r;   r   rM   nextrG   r<   sorteditemsoperator
itemgetterr   keysr   valuesr   opr   targetrT   rD   rk   rE   callabler   typerF   appendnamer,   )r   nodes_ordernew_partition_idrr   rw   r$   r#   merge_candidatescreated_partitionrd   user_merge_candidates_listr^   r_   	partitionnodes_reassignmentis_tuple_outputr   default_non_compute_opsrE   partitions_to_removecompute_node_countr[   r]   r\   ro   rp   rq   s   `                      @@@@@@r   propose_partitionsz-CapabilityBasedPartitioner.propose_partitions\   sK    .9-D-DS-I ')
  	
  	
  	
  	 %??,	, 	,4<SM	,X	,*N	"3 N	"# N	"%T	BR N	" N	"`	@	@$'$J	@47$J	@	@ 	@2 	./ )(43D3D3J3J3P3P*Q R &	JJ02 % &&t,Z1G#$45$0D!1= .!$
LA15 .$(!**$ $

 F:-AE,Z-=>F
 (.$**,(2E2Ea2H( :OL! 6:$\2:
 %))9)>)>)@$A!()A-/2 5ab 9 JH "7w!IJGQJG&	JR *002 	I"OO))+1D1DQ1GQUIO	 	TU24))//55 :"& JJ D?2.t{{;?RR*/ ##-B $

 :%>>$/2579.t4::$ &.446 2b!$b12/ 6 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"0"@dkkAR@S T#  /t{{;?R.!3./<#EEF /!3.4 &*(//3!4" + )$R() 	+,-335 	MB	LL#R	)P$)))P	 (8'>'>'@
#INNDTWXDXI
 	
 *Q
s   S+S0$S0
partitionsprefixc                     t         j                  d       t        | j                  |D cg c]  }|j                   c}|      S c c}w )NzFusing partitions...r   )r~   r   r	   r4   r   )r   r   r   r   s       r   fuse_partitionsz*CapabilityBasedPartitioner.fuse_partitionsd  s?     	+,!.89Y__9
 	
9s   A
c                 p  	
 t        | j                        dt        dt        ffdi 	i 
dt        dt         t           dt         t           dt        f	fddt        dt         t           dt         t           d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#   r   c                 T    | j                   dk(  xr t        | j                        v S )Nr{   )r   r   r   )r#   rE   s    r   is_non_compute_nodezVCapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_non_compute_nodes  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_node}  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_node  s     =(	)M)///55"4( $

 %H5 )] :?06$% 26(.-2$T*r   r   )r8   rE   r   ri   r   r=   r   rv   )r   r   r   r*   r#   r   r   r   rE   r   r   s        @@@@@@r   remove_bookend_non_compute_opsz9CapabilityBasedPartitioner.remove_bookend_non_compute_opsp  s0   d223	d 	t 	 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   )FNNF)fused_)r-   r.   r/   r   r   ri   r   r   r   r   rM   r   r   r   r   r   r   r1   r   r   rB   rB   A   s    
 .304BF',A!A .A '+	A
 "#-A ,4C=4+?A !%A 
A*
t 
 

F
DO F
R :B	
y/	
36	
		
I4i I4T I4V K r   rB   )r6   rR   loggingr   collections.abcr   r   torch.fx.graph_moduler   torch.fx.noder   r    torch.fx.passes.operator_supportr   !torch.fx.passes.utils.fuser_utilsr	   	getLoggerr-   r~   setLevelWARNINGr   r3   rB   r1   r   r   <module>r      se        . - 3 @ @ 
		8	$    D& &} }r   