
    Vh_B                        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
 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)copy)Optional)GraphModule)_get_qualified_nameNode)OperatorSupportBase)fuse_by_partitionsc                   \    e Zd Z	 ddee   deee      fdZdefdZ	defdZ
defd	Zd
 Zy)	PartitionNidnodesc                 Z    || _         |t        j                  |      | _        y i | _        y N)r   dictfromkeysr   )selfr   r   s      Q/home/dcms/DCMS/lib/python3.12/site-packages/torch/fx/passes/infra/partitioner.py__init__zPartition.__init__   s&     -2->T]]5)
B
    returnc                 ,    t        | j                        S r   )strr   r   s    r   __repr__zPartition.__repr__       4::r   nodec                 >    | j                   j                  |d i       y r   )r   updater   r   s     r   add_nodezPartition.add_node   s    

4,'r   c                     | j                   |= y r   )r   r!   s     r   remove_nodezPartition.remove_node    s    JJtr   c                 ,    t        | j                        S r   )lenr   r   s    r   sizezPartition.size#   r   r   )NN)__name__
__module____qualname__r   intr   r	   r   r   r   r"   r$   r'    r   r   r   r      sS    JNG3-G/7/GG# (T ( r   r   c                   D    e Zd ZdefdZdedee   fdZdedee   fdZy)_DependencyViewergraph_modulec                 b   t        j                  t              | _        t        j                  t              | _        |j
                  j                  D ]\  }|j                  D ]K  }| j                  |   j                  |       | j                  |   j                  | j                  |          M ^ t        |j
                  j                        D ]\  }|j                  D ]K  }| j                  |   j                  |       | j                  |   j                  | j                  |          M ^ y r   )collectionsdefaultdictset	upstreamsdownstreamsgraphr   all_input_nodesaddr    reversedusers)r   r/   r   
input_nodeoutput_nodes        r   r   z_DependencyViewer.__init__(   s   $005&2237 &&,, 	HD"22 H
t$((4t$++DNN:,FGH	H \//556 	MD#zz M  &**;7  &--d.>.>{.KLM	Mr   r   r   c                      | j                   |   S r   )r5   r!   s     r   downstreams_ofz _DependencyViewer.downstreams_of8   s    %%r   c                      | j                   |   S r   )r4   r!   s     r   upstreams_ofz_DependencyViewer.upstreams_of;   s    ~~d##r   N)	r(   r)   r*   r   r   r	   r3   r>   r@   r,   r   r   r.   r.   '   s>    M[ M &4 &CI &$ $#d) $r   r.   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)CapabilityBasedPartitionerNr/   operator_supportallows_single_node_partitionnon_compute_ops!allowed_single_node_partition_opsr   c                 z    || _         || _        || _        ||ng | _        ||ng | _        t        |      | _        y r   )r/   rC   rD   rE   rF   r.   dependency_viewer)r   r/   rC   rD   rE   rF   s         r   r   z#CapabilityBasedPartitioner.__init__@   sS     ) 0,H)2A2MSU 1< . 	.
 "3<!@r   r   c                 |    | j                   j                  t        | j                  j	                               |      S r   )rC   is_node_supportedr   r/   named_modulesr!   s     r   __is_node_supportedz.CapabilityBasedPartitioner.__is_node_supportedS   s4    $$66""0023T
 	
r   c           	          t        j                  t              i i i }i t        j                         }dt
        dt
        f fd}dt        dt        t
           f fd}t        j                  d       t         j                  j                  j                        D ]  }i } j                  |      r'|vr#t        |      }|||<   ||<    |||       d ||<   t!        j#                         d 	      D ]
  \  }}d ||<    t%        |j'                               }	t)        |	      d
kD  s|	d   }
|	d
d  D ]  } ||
|         t        j                  d       i } j                  j                  j                  D ]  }d}|j*                  D ]-  }|j,                  dk7  st/        |j0                        dk7  s+d} n |sDj3                  |d       }|j*                  D ]  }j3                  |d       |k7  s|||<     |j#                         D ]  \  }} |||         j4                  st        j                  d       ddh}|j7                  t         j8                              }g }j#                         D ]  \  }}d}|j                  D ]l  }|j,                  dk(  st;        |j0                        sJ t/        |j0                        |vr|d
z  }t/        |j0                         j<                  v sh|d
z  }n |d
k  s|j?                  |        |D ]  }|=  t        j                  d       j#                         D ]>  \  }}t        j                  d||j                  D cg c]  }|j@                   c}       @ jC                         D cg c]  }|jE                         dkD  s| c}S c c}w c c}w )Nself_idother_idc                     t        	    j                        j                  	   j                         dt        t           f fd}t               }D ])  }|j
                  D ]  }|vs|j                  |        +  ||      ry	    _        	   j                  D ]  } |<   	 	= t        
    
         
 <   
=     j                            <   = y)Nall_user_nodesc                     | D ]c  }t               }
j                  j                  |      D ]9  }|v r  y|v s|   }||v r	|   }|v s|v r  y|j                  |       ; e y)NTF)r3   rH   r>   r8   )rQ   	user_nodevisited_partition_ids	path_nodepartition_idp_map
assignmentmerged_nodesrO   partition_mapr   rN   s         r   dfs_iter_find_cycleziCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partition.<locals>.dfs_iter_find_cycles   s    !/ DI,/E)%)%;%;%J%J9%U D	 %4#' %
2+5i+@L  ,/DD ($1,$?E&%/8u3D'+155lC+DD4 r   FT)	r   r   r    r3   r	   r:   r8   minunion)rN   rO   r[   rQ   r   rS   rY   rX   rZ   partitions_by_idpartitions_orderr   s   ``    @r   maybe_merge_partitionzLCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partitionn   s/    0 9 ? ?@L 0 : @ @ACI  < !UN$ 6!% 6I 4&**9566
 #>2  /;W%+(288 +#*
4 + !*(+ )+;H+E)W% !*%27%;%A%Ah'&M'" h'r   r   r   c                    dt         dt        ffd}| v r|       j                  |        |j                  |        y |vr || <   t	        || g      |<    || |       y || <   |   j                  |         || |       y )Nr   r   c                 >   | j                   D ]B  }j                  |d       }||   j                  |       |   j                  |          D 	j                  j                  |       }|D ]+  }j                  |d       }||   j                  |       - y r   )r:   getr8   r    rH   r@   )
r   r   rS   	target_idupstream_nodes	curr_node	source_idrX   rZ   r   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 "&!7!7!D!DT!J!/ 9I *y$ ?I ,%i044R89r   )r   r   )r	   r+   r$   popr   r"   )r   r   rh   rX   rZ   r^   r   s      r   merge_single_nodezHCapabilityBasedPartitioner.propose_partitions.<locals>.merge_single_node   s    9D 9c 9$ z! D!12>>tDzt$++#%
4 '0Btf'E $%dB/#%
4  $--d3%dB/r   zProposing partitions...c                     | d   S )N   r,   )items    r   <lambda>z?CapabilityBasedPartitioner.propose_partitions.<locals>.<lambda>   s
    47 r   )keyrl   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)#r1   r2   r3   	itertoolscountr+   r	   r   loggerdebugr9   r/   r6   r   ._CapabilityBasedPartitioner__is_node_supportednextsorteditemslistkeysr&   r:   opr   targetrc   rD   r]   rE   callablerF   appendnamevaluesr'   )r   nodes_ordernew_partition_idr`   rj   r   merge_candidatesrV   _merge_candidates_listrN   rO   nodes_reassignmentis_tuple_outputuserr   default_non_compute_opsrE   partitions_to_remove	partitioncompute_node_countrX   rZ   r^   r_   s   `                    @@@@r   propose_partitionsz-CapabilityBasedPartitioner.propose_partitionsX   s    )4(?(?(D ')
  	
  	
  	 %??,
A	3 A	# A	 A	F	0D 	0hsm 	0 	0B 	./T..44::; 	=D02 ''-$j2H#$45$0D!1= .!$515 . $* &&(.B$ 6a 26 .6
 %))9)>)>)@$A!()A-/2 5ab 9 =H *'8<	=3	=@ 	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"dB'	( 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   :O(OO
partitionsprefixc                     t         j                  d       t        | j                  |D cg c]  }|j                   c}|      S c c}w )NzFusing partitions...r   )rs   rt   r   r/   r   )r   r   r   r   s       r   fuse_partitionsz*CapabilityBasedPartitioner.fuse_partitions*  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_node9  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{   r7   )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_nodeC  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_nodeX  s     =(	)M)///55"4( $

 %H5 )] :?06$% 26(.-2$T*r   r   )r3   rE   r	   r   r8   r&   ri   )r   r   r   r$   r   r   r   r   rE   r   r   s        @@@@@@r   remove_bookend_non_compute_opsz9CapabilityBasedPartitioner.remove_bookend_non_compute_ops6  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	   ru   ry   r   r   r   r   r   r,   r   r   rB   rB   ?   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   rB   )r1   rq   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(   rs   setLevelWARNINGr   r.   rB   r,   r   r   <module>r      sg       .   - 3 @ @ 
		8	$    ($ $0E Er   