
    קg>                         d dl mZ d dlZd dlZd dlZd dlmZ d dlmZmZm	Z	m
Z
mZmZ d dlmZ d dlmZmZ d dlmZ  ej        e          Ze                    ej                    G d d	          Z G d
 d          Z G d d          ZdS )    )fuse_by_partitionsN)copy)DictIterableListOptionalSequenceSet)GraphModule)Node_get_qualified_name)OperatorSupportBasec                   n    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dS )	PartitionNidnodesc                 Z    || _         |t                              |          ni | _        d S N)r   dictfromkeysr   )selfr   r   s      ]/var/www/html/ai-engine/env/lib/python3.11/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                             |d i           d S r   )r   updater   r!   s     r   add_nodezPartition.add_node   s"    
4,'''''r   c                     | j         |= d S r   )r   r$   s     r   remove_nodezPartition.remove_node   s    Jt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      s        G G8C= G$8P G G G G#    (T ( ( ( (        r   r   c                   R    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dS )_DependencyViewergraph_modulec                 (   t          j        t                    | _        t          j        t                    | _        |j        j        D ]W}|j        D ]M}| j        |                             |           | j        |         	                    | j        |                    NXt          |j        j                  D ]W}|j        D ]M}| j        |                             |           | j        |         	                    | j        |                    NXd S r   )collectionsdefaultdictset	upstreamsdownstreamsgraphr   all_input_nodesaddr#   reversedusers)r   r2   r!   
input_nodeoutput_nodes        r   r   z_DependencyViewer.__init__$   s!   $055&2377 &, 	H 	HD"2 H H
t$((444t$++DN:,FGGGGH
 \/566 	M 	MD#z M M &**;777 &--d.>{.KLLLLM	M 	Mr   r!   r   c                     | j         |         S r   )r8   r$   s     r   downstreams_ofz _DependencyViewer.downstreams_of4   s    %%r   c                     | j         |         S r   )r7   r$   s     r   upstreams_ofz_DependencyViewer.upstreams_of7   s    ~d##r   N)	r+   r,   r-   r   r   r   r
   rA   rC   r/   r   r   r1   r1   #   s        M[ M M M M &4 &CI & & & &$ $#d) $ $ $ $ $ $r   r1   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dS )CapabilityBasedPartitionerFNr2   operator_supportallows_single_node_partitionnon_compute_ops!allowed_single_node_partition_opsr   c                     || _         || _        || _        ||ng | _        ||ng | _        t          |          | _        d S r   )r2   rF   rG   rH   rI   r1   dependency_viewer)r   r2   rF   rG   rH   rI   s         r   r   z#CapabilityBasedPartitioner.__init__<   sa     ) 0,H)2A2MSU 1< .- 	.
 "3<!@!@r   r!   c                     | j                             t          | j                                                  |          S r   )rF   is_node_supportedr   r2   named_modulesr$   s     r   __is_node_supportedz.CapabilityBasedPartitioner.__is_node_supportedN   s5    !33D9J9X9X9Z9Z4[4[]abb	
r   c                     t          j        t                    i i t          j                    }dt
          dt
          f fd}dt          dt          t
                   f fd}t          	                    d           t           j        j        j                  D ]}i }                     |          r$|vr t          |          } |||           d ||<   D ]}d ||         <   t!          |                                          }t%          |          dk    r!|d	         }|dd          D ]}	 |||	           t          	                    d
           i }
 j        j        j        D ]v}d}|j        D ])}|j        dk    st+          |j                  dk    rd} n*|r?                    |d           }|j        D ]!}                    |d           |k    r||
|<   "w|
                                D ]\  }} |||            j        st          	                    d           ddh}|                    t           j                            }g }                                D ]\  }}d	}|j        D ]^}|j        dk    rQt9          |j                  sJ t+          |j                  |vr|dz  }t+          |j                   j        v r|dz  }_|dk    r|                    |           |D ]}|= t          	                    d                                           D ]0\  }}t          	                    d|d |j        D                        1d                                 D             S )Nself_idother_idc                     t          	          j                                      	         j                   dt          t                   f
 fd}t                      }D ]%}|j        D ]}|vr|                    |           & ||          rdS 	          _        	         j        D ]} |<   	=                                                   <   = dS )Nall_user_nodesc                     | D ]o}t                      }
j                            |          D ]D}|v r  dS |v r6|         }||v r	|         }|v s|v r  dS |                    |           EpdS )NTF)r6   rK   rA   r;   )rT   	user_nodevisited_partition_ids	path_nodepartition_idp_map
assignmentmerged_nodesrR   partition_mapr   rQ   s         r   dfs_iter_find_cycleziCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partition.<locals>.dfs_iter_find_cyclef   s    !/ D DI,/EE)%)%;%J%J9%U%U D D	 %44#'444 %
22+5i+@L  ,/DDD ($1,$?E&%//8u3D3D'+ttt155lCCC+D. ur   FT)	r   r   r#   r
   r   r6   r=   r;   union)rQ   rR   r^   rT   r!   rV   r\   r[   r]   partitions_by_idr   s   ``    @r   maybe_merge_partitionzLCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partitiona   sS    0 9 ?@@L 0 : @AAACI           < !UUN$ 6 6!% 6 6I 44&**95556 #">22  u /;W%+(28 + +#*
4   *%27%;%A%A-PXBY%Z%ZM'"h'4r   r!   r   c                 `   dt           dt          ffd}| v r!|                                       |            |                    |            d S |vr(|| <   t	          || g          |<    || |           d S || <   |                             |             || |           d S )Nr!   r   c                 P   	j                             |           }|D ]5}                    |d           }||                             |           6	j                             |           }|D ]5}                    |d           }||                             |           6d S r   )rK   rA   getr;   rC   )
r!   r   downstream_nodes	curr_node	target_idupstream_nodes	source_idr[   r]   r   s
          r   _update_partition_mapzgCapabilityBasedPartitioner.propose_partitions.<locals>.merge_single_node.<locals>._update_partition_map   s     $(#9#H#H#N#N !1 9 9I *y$ ? ?I ,%b)--i888
 "&!7!D!DT!J!J!/ 9 9I *y$ ? ?I ,%i044R8889 9r   )r   r   )r   r.   r'   popr   r%   )r!   r   rj   r[   r]   r`   r   s      r   merge_single_nodezHCapabilityBasedPartitioner.propose_partitions.<locals>.merge_single_node   s   9D 9c 9 9 9 9 9 9 9 9& z!! D!12>>tDDDzt$$$$$+++#%
4 '0Btf'E'E'E $%%dB/////#%
4  $--d333%%dB/////r   zProposing partitions...   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: %sc                     g | ]	}|j         
S r/   )name).0r!   s     r   
<listcomp>zACapabilityBasedPartitioner.propose_partitions.<locals>.<listcomp>  s    2Y2Y2Y492Y2Y2Yr   c                 B    g | ]}|                                 d k    |S )r   )r*   rq   	partitions     r   rr   zACapabilityBasedPartitioner.propose_partitions.<locals>.<listcomp>  s-    ]]]i	HXHX[\H\H\	H\H\H\r   ) r4   r5   r6   	itertoolscountr.   r   r   loggerdebugr<   r2   r9   r   ._CapabilityBasedPartitioner__is_node_supportednextlistkeysr)   r=   opr   targetrd   itemsrG   r_   rH   callablerI   appendvalues)r   new_partition_idra   rl   r!   merge_candidatesrY   merge_candidates_listrQ   rR   nodes_reassignmentis_tuple_outputuserr   default_non_compute_opsrH   partitions_to_removeru   compute_node_countr[   r]   r`   s   `                  @@@r   propose_partitionsz-CapabilityBasedPartitioner.propose_partitionsS   sr    *5)@)E)E ')
13$?,,
:	3 :	# :	 :	 :	 :	 :	 :	 :	 :	 :	x 	0D  	0hsm  	0  	0  	0  	0  	0  	0  	0  	0  	0D 	.///T.4:;; 	= 	=D02 ''-- 6$j2H2H#$455!!$55515 . # : :59 D!122$()9)>)>)@)@$A$A!())A--/2 5abb 9 = =H *)'8<<<< 	TUUU.0%+1 	6 	6D"O
  7o--&t{337JJJ&+OE K
  6^^D$// J 6 6D!~~dD11R7735*40*0022 	( 	(HD"dB'''' 0 	)LLBCCC'<>Q&R#5;;C@T<U<UVVO.0 !1!7!7!9!9 
4 
4I%&"%O 4 4Dw/11'44444.t{;;?RR.!3..t{;;t?eee.!3.%**(//333* ) )$R((+,,,-3355 	[ 	[MB	LL,b2Y2Y2Y2Y2YZZZZ]]+;+B+B+D+D]]]]r   fused_
partitionsprefixc                 x    t                               d           t          | j        d |D             |          S )NzFusing partitions...c                 6    g | ]}t          |j                  S r/   )r|   r   rt   s     r   rr   z>CapabilityBasedPartitioner.fuse_partitions.<locals>.<listcomp>  s"    ???yT)/""???r   r   )rx   ry   r   r2   )r   r   r   s      r   fuse_partitionsz*CapabilityBasedPartitioner.fuse_partitions
  sF    +,,,!??J???
 
 
 	
r   c                   	
 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 ]`} |          rS |t          |j                  |          s |t          |j                  |          r|                    |           at          |          dk    r |D ]}|j                            |d            d S )Nr!   c                 F    | j         dk    ot          | j                  v S )Nrn   )r~   r   r   )r!   rH   s    r   is_non_compute_nodezVCapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_non_compute_node  s+    7o- D#DK00OCDr   ru   removed_nodesc                     | j         dk    s| |vs| |v rdS | v r|          S  |           r&| j        D ]} |||          sd| <    dS d| <   dS d| <   dS NplaceholderTF)r~   r:   )r!   ru   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    w-''D	,A,At}G\G\t....t44""4(( #3 % %G44WiWW %8=/5$uu% 15'-t,1#D)5r   c                     | j         dk    s| |vs| |v rdS | v r|          S  |           r&| j        D ]} |||          sd| <    dS d| <   dS d| <   dS r   )r~   r=   )r!   ru   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    w-''D	,A,At}G\G\t////55""4((  $
 % %H55h	=YY %9>06$uu% 26(.t-2$T*5r   r   )r6   rH   r   r
   r   r;   r)   rk   )r   r   ru   r'   r!   r   r   r   rH   r   r   s        @@@@@@r   remove_bookend_non_compute_opsz9CapabilityBasedPartitioner.remove_bookend_non_compute_ops  s   d233	Dd 	D 	D 	D 	D 	D 	D
 5757 	D 	SY 	WZ[_W` 	 	 	 	 	 	 	 		T 	c$i 	X[\`Xa 	 	 	 	 	 	 	 	 $ 	4 	4I &)UUK! * *&&t,, *..tS5I5I;WW*//c)/6J6JKXX*  OOD)));1$$' 4 4DO''d3333	4 	4r   c                 \    |                                  }|                     ||          }|S )Nr   )r   r   )r   r   r   fused_gms       r   partition_and_fusez-CapabilityBasedPartitioner.partition_and_fuseL  s0    ,,..
''
6'BBr   )FNN)r   )r+   r,   r-   r   r   boolr   r	   r   r   r   rz   r   r   r   r   r   r   r/   r   r   rE   rE   :   sS       
 7<<@NRA A*A#6A 04A #+8C="9	A
 5=Xc]4KA A A A A$
 
 
 
 
 

u^DO u^ u^ u^ u^n
 
$y/ 
3 
Va 
 
 
 
64i 64 64 64 64p  K      r   rE   )!torch.fx.passes.utils.fuser_utilsr   r4   rv   loggingr   typingr   r   r   r   r	   r
   torch.fx.graph_moduler   torch.fx.noder   r    torch.fx.passes.operator_supportr   	getLoggerr+   rx   setLevelWARNINGr   r1   rE   r/   r   r   <module>r      sj   @ @ @ @ @ @                @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ - - - - - - 3 3 3 3 3 3 3 3 @ @ @ @ @ @ 
	8	$	$              "$ $ $ $ $ $ $ $.U U U U U U U U U Ur   