
    rhK@                     
   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
 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)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      t/var/www/html/ai-insurance-compliance-backend/venv/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                   .    e Zd ZdefdZdedee   fdZy)_DependencyViewergraph_modulec                 <   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   r.   r   output_nodes       r   r   z_DependencyViewer.__init__(   s    &2237\//556 	MD#zz M  &**;7  &--d.>.>{.KLM	Mr   r   r   c                      | j                   |   S r   )r3   r    s     r   downstreams_ofz _DependencyViewer.downstreams_of1   s    %%r   N)r'   r(   r)   r   r   r   r2   r:   r+   r   r   r-   r-   '   s'    M[ M&4 &CI &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.   r=   r>   r?   r@   r-   dependency_viewer)r   r.   r=   r>   r?   r@   s         r   r   z#CapabilityBasedPartitioner.__init__6   sS     ) 0,H)2A2MSU 1< . 	.
 "3<!@r   r   c                 |    | j                   j                  t        | j                  j	                               |      S r   )r=   is_node_supportedr   r.   named_modulesr    s     r   _is_node_supportedz-CapabilityBasedPartitioner._is_node_supportedI   s4    $$66""0023T
 	
r   c           	          t        j                  t              i 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#                         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 ]-  }|j0                  dk7  st3        |j4                        dk7  s+d} n |sDj7                  |d       }|j.                  D ]  }j7                  |d       |k7  s|||<     |j#                         D ]  \  }} |||         j8                  st        j                  d       ddh}|j;                  t         j<                              }g }j#                         D ]  \  }}d
}|j                  D ]l  }|j0                  dk(  st?        |j4                        sJ t3        |j4                        |vr|dz  }t3        |j4                         j@                  v sh|dz  }n |dk  s|jC                  |        |D ]  }|=  t        j                  d       j#                         D ]>  \  }}t        j                  d||j                  D cg c]  }|jD                   c}       @ jG                         D cg c]  }|jI                         d
kD  s| c}S c c}w c c}w )Nself_idother_idc                         j                      j                   dt        t           f	
 fd}       z  }|j                          ||      r dfS  }}t	              t	              k  r||}}|   j                   j                  |   j                          |   j                   D ]  }|	|<   	 |= t        |   |         |<   |= 
|   j                  
|         
|<   
|= ||<   |= |dfS )Nall_user_nodesc                     | D ]g  }t               }
j                  j                  |      D ]=  }|v s|v r  y|v s|   }||v r	|   }|v s|v r  y|j                  |       ? i y)NTF)r2   rB   r:   r7   )rK   	user_nodevisited_partition_ids	path_nodepartition_idp_map
assignmentrI   other_nodespartition_mapr   rH   
self_nodess         r   dfs_iter_find_cycleziCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partition.<locals>.dfs_iter_find_cyclel   s    !/ DI,/E)%)%;%;%J%J9%U D	 %
2i;6N#' %
2+5i+@L  ,/DD ($1,$?E&%/8u3D'+155lC+DD4 r   FT)r   r2   r   difference_updater%   r   minunion)rH   rI   rV   rK   merge_id
removed_idr   rS   rU   rR   rT   partition_userspartitions_by_idpartitions_orderr   s   ``     @@r   maybe_merge_partitionzLCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partitiong   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%
+T>!r   r   r   c                 4   dt         dt        ffd}| v r|       j                  |        |j                  |        y |vr8|| <   t	        || g      |<   t        | j                        |<    || |       y || <   |   j                  |        y )Nr   r   c                     | j                   D ]B  }j                  |d       }||   j                  |       |   j                  |          D y r   )r6   getr7   r   )r   r   rM   	target_idrR   rT   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r   )r   r   )r   r*   r#   popr   r2   r6   r!   )r   r   rd   rR   rT   r\   r]   s      r   merge_single_nodezHCapabilityBasedPartitioner.propose_partitions.<locals>.merge_single_node   s    KD Kc K z! D!12>>tDzt$++#%
4 '0Btf'E $&)$**o#%dB/#%
4  $--d3r   zProposing partitions...   )keyr   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)%r0   r1   r2   	itertoolscountr*   r   r   loggerdebugr4   r.   r5   r   rF   nextsorteditemsoperator
itemgetterlistkeysr%   r6   opr   targetrb   r>   rY   r?   callabler@   appendnamevaluesr&   )r   nodes_ordernew_partition_idr_   rf   r   merge_candidatesrP   _merge_candidates_listrH   rI   nodes_reassignmentis_tuple_outputuserr   default_non_compute_opsr?   partitions_to_remove	partitioncompute_node_countrR   rT   r\   r]   r^   s   `                    @@@@@r   propose_partitionsz-CapabilityBasedPartitioner.propose_partitionsN   s+    )4(?(?(D ')
  	
  	
  	
  	 %??,
F	"3 F	"# F	" F	"P	4D 	4hsm 	4 	40 	./T..44::; 	JD02 &&t,Z1G#$45$0D!1= .!$515 . $* &&(h.A.A!.D$ 6a 26 .6
 %))9)>)>)@$A!()A-/2 5ab 9 JH "7w!IJGQJ3	J> 	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   )rl   rm   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 )Nri   )ru   r   rv   )r   r?   s    r   is_non_compute_nodezVCapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_non_compute_node-  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)ru   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_node7  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   )ru   r6   )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_nodeL  s     =(	)M)///55"4( $

 %H5 )] :?06$% 26(.-2$T*r   r   )r2   r?   r   r   r7   r%   re   )r   r   r   r#   r   r   r   r   r?   r   r   s        @@@@@@r   remove_bookend_non_compute_opsz9CapabilityBasedPartitioner.remove_bookend_non_compute_ops*  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_fuseu  s*    ,,.
''
6'Br   )FNN)fused_)r'   r(   r)   r   r	   boolr   r   r   r   r   rF   rs   r   r   r   r   r   r+   r   r   r<   r<   5   s    
 .337EIA!A .A '+	A
 "(3-0A ,4HSM+BA 
A&
t 
 

N
DO N
b :B	
y/	
36	
		
I4i I4V K r   r<   )r0   rj   loggingrq   collections.abcr   r   typingr   torch.fx.graph_moduler   torch.fx.noder   r    torch.fx.passes.operator_supportr	   !torch.fx.passes.utils.fuser_utilsr
   	getLoggerr'   rl   setLevelWARNINGr   r-   r<   r+   r   r   <module>r      sg        .  - 3 @ @ 
		8	$    (& &C Cr   