
    [Th_B                        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
  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)copy)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      Y/var/www/auris/envauris/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    \ rS rSrS\4S jrS\S\\   4S jrS\S\\   4S jr	Sr
g	)
_DependencyViewer'   graph_modulec                 v   [         R                  " [        5      U l        [         R                  " [        5      U l        UR
                  R                   H_  nUR                   HL  nU R                  U   R                  U5        U R                  U   R                  U R                  U   5        MN     Ma     [        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	upstreamsdownstreamsgraphr   all_input_nodesaddr#   reversedusers)r   r9   r!   
input_nodeoutput_nodes        r   r   _DependencyViewer.__init__(   s    $005&2237 &&,,D"22
t$((4t$++DNN:,FG 3 - \//556D#zz  &**;7  &--d.>.>{.KL  * 7r   r!   r   c                      U R                   U   $ r   )r?   r$   s     r   downstreams_of _DependencyViewer.downstreams_of8   s    %%r   c                      U R                   U   $ r   )r>   r$   s     r   upstreams_of_DependencyViewer.upstreams_of;   s    ~~d##r   )r?   r>   N)r/   r0   r1   r2   r   r   r	   r=   rI   rL   r4   r5   r   r   r7   r7   '   s>    M[ M &4 &CI &$ $#d) $r   r7   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)CapabilityBasedPartitioner?   Nr9   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   )r9   rQ   rR   rS   rT   r7   dependency_viewer)r   r9   rQ   rR   rS   rT   s         r   r   #CapabilityBasedPartitioner.__init__@   sM     ) 0,H)2A2MSU 1< . 	.
 "3<!@r   r!   c                 |    U R                   R                  [        U R                  R	                  5       5      U5      $ r   )rQ   is_node_supportedr   r9   named_modulesr$   s     r   __is_node_supported.CapabilityBasedPartitioner.__is_node_supportedS   s4    $$66""0023T
 	
r   c           	         ^ ^^^^ [         R                  " [        5      m0 m0 m0 n0 m[        R                  " 5       nS[
        S[
        4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       S 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        M     M     [        R                  S5        0 nT R                  R                  R                   H  nSnUR*                   H0  nUR,                  S:w  d  [/        UR0                  5      S:w  d  M.  Sn  O   U(       d  MN  TR3                  US 5      nUR*                   H  nTR3                  US 5      U:w  d  M  XU'   M!     M     UR#                  5        H  u  p_U" X_5        M     T R4                  (       Gd	  [        R                  S5        SS1nUR7                  [        T R8                  5      5      n/ nTR#                  5        H  u  nnSnUR                   Hy  nUR,                  S:X  d  M  [;        UR0                  5      (       d   e[/        UR0                  5      U;  a  US
-  n[/        UR0                  5      T R<                  ;   d  Mt  US
-  nM{     US
::  d  M  UR?                  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  oUR@                  PM     sn5        MC     TRC                  5        Vs/ s H  nURE                  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                  5      mTR                  T	T   R                  5        S[        [           4UUUUUU 4S jjn[        5       nT H/  nUR
                   H  nUT;  d  M  UR                  U5        M     M1     U" U5      (       a  gTT	T    l        T	T   R                   H  nT TU'   M
     T	T	 [        T
T    T
T   5      T
T '   T
T	 TT    R                  TT   5      TT '   TT	 g)Nall_user_nodesc                    > U  Hq  n[        5       nT
R                  R                  U5       HE  n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        MG     Ms     g)NTF)r=   rV   rI   rB   )ra   	user_nodevisited_partition_ids	path_nodepartition_idp_map
assignmentmerged_nodesr_   partition_mapr   r^   s         r   dfs_iter_find_cycleiCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partition.<locals>.dfs_iter_find_cycles   s    !/I,/E)%)%;%;%J%J9%U	 %4#' %
2+5i+@L  ,D ($1,$?E&%/8u3D'+155lC+ &V "04 r   FT)	r   r   r#   r=   r	   rD   rB   minunion)r^   r_   rk   ra   r!   rc   ri   rh   rj   partitions_by_idpartitions_orderr   s   ``    @r   maybe_merge_partitionLCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partitionn   s#    0 9 ? ?@L 0 : @ @ACI  < !UN$!%I 4&**95 ", %
 #>22  /;W%+(288#*
4  9 !*(+ )+;H+E)W% !*%27%;%A%Ah'&M'" h'r   r!   r   c                   > S[         S[        4U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 '   [	        X/S9TU'   U" X5        g UTU '   TU   R                  U 5        U" X5        g )Nr!   r   c                 N  > 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     T	R                  R                  U 5      nU H.  nTR                  US 5      nUc  M  TU   R                  U5        M0     g r   )rD   getrB   r#   rV   rL   )
r!   r   rc   	target_idupstream_nodes	curr_node	source_idrh   rj   r   s
          r   _update_partition_mapgCapabilityBasedPartitioner.propose_partitions.<locals>.merge_single_node.<locals>._update_partition_map   s     "&I *y$ ?I ,%b)--i8%b)00y1IJ	 ", "&!7!7!D!DT!J!/I *y$ ?I ,%i044R8 "0r   r.   )r	   r3   r(   popr   r%   )r!   r   rz   rh   rj   ro   r   s      r   merge_single_nodeHCapabilityBasedPartitioner.propose_partitions.<locals>.merge_single_node   s    9D 9c 9 9$ z! D!12>>tDzt$++#%
4 '0Bf'E $%d/#%
4  $--d3%d/r   zProposing partitions...c                     U S   $ )N   r5   )items    r   <lambda>?CapabilityBasedPartitioner.propose_partitions.<locals>.<lambda>   s    47r   )keyr   r   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countr3   r	   r   loggerdebugrC   r9   r@   r   ._CapabilityBasedPartitioner__is_node_supportednextsorteditemslistkeysr+   rD   opr   targetru   rR   rn   rS   callablerT   appendnamevaluesr,   )r   nodes_ordernew_partition_idrq   r}   r!   merge_candidatesrf   _merge_candidates_listr^   r_   nodes_reassignmentis_tuple_outputuserr   default_non_compute_opsrS   partitions_to_remove	partitioncompute_node_countrh   rj   ro   rp   s   `                    @@@@r   propose_partitions-CapabilityBasedPartitioner.propose_partitionsX   s    )4(?(?(D ')
  	
  	
  	 %??,
A	3 A	# A	 A	F	0D 	0hsm 	0 	0B 	./T..44::;D02 ''--$j2H#$45$0D!1= .!$515 . $* &&(.B$ 26 .$
 %))9)>)>)@$A!()A-/2 5ab 9H *'<	 !: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   r9   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!   rS   s    r   is_non_compute_nodeVCapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_non_compute_node9  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   rA   )r!   r   r   input_nr   is_transparent_input_nodetransparent_input_nodess       r   r   \CapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_transparent_input_nodeC  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   rD   )r!   r   r   output_nr   is_transparent_output_nodetransparent_output_nodess       r   r   ]CapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_transparent_output_nodeX  s     =())///55"4(( $

H5 ]  :?06$ !+ 26(.-2$T*r   r   )r=   rS   r	   r   rB   r+   r|   )r   r   r   r(   r!   r   r   r   rS   r   r   s        @@@@@@r   remove_bookend_non_compute_ops9CapabilityBasedPartitioner.remove_bookend_non_compute_ops6  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   )rT   rR   rV   r9   rS   rQ   )FNN)fused_)r/   r0   r1   r2   r   r
   boolr   r   r   r   r	   r   r   r   r   r   r   r   r4   r5   r   r   rO   rO   ?   s    
 .337EIA!A .A '+	A
 "(3-0A ,4HSM+BA 
A&
 
 

P
DO P
f :B	
y/	
36	
		
I4i I4V K  r   rO   )r;   r   loggingcollections.abcr   r   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   r7   rO   r5   r   r   <module>r      se       .   - 3 @ @ 
		8	$    ($ $0E Er   