
    Ph2                        d dl mZmZmZmZmZ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Zd dlZd dlmZ d dlmZ  ej,                  e      Zej3                  ej4                          G d	 d
      Z G d d      Zy)    )DictListSetIterableSequenceOptionalDeque)fuse_by_partitions)GraphModule)Node_get_qualified_name)OperatorSupportBaseN)copy)dequec                   Z    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                 V    || _         |t        |      | _        y t               | _        y N)r   setr   )selfr   r   s      lC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/fx/passes/infra/partitioner.py__init__zPartition.__init__   s!    .3.?E

SU
    returnc                 ,    t        | j                        S r   )strr   r   s    r   __repr__zPartition.__repr__       4::r   nodec                 :    | j                   j                  |       y r   )r   addr   r"   s     r   add_nodezPartition.add_node   s    

tr   c                 :    | j                   j                  |       y r   )r   remover%   s     r   remove_nodezPartition.remove_node   s    

$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      sN    K8C= K$8P K# T    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ee   defdZdee   fdZdefdZy)CapabilityBasedPartitionerNgraph_moduleoperator_supportallows_single_node_partitionnon_compute_ops!allowed_single_node_partition_opsr   c                 f    || _         || _        || _        ||ng | _        ||| _        y g | _        y r   )r4   r5   r6   r7   r8   )r   r4   r5   r6   r7   r8   s         r   r   z#CapabilityBasedPartitioner.__init__$   sN     ) 0,H)2A2MSU 1< . 	.  	.r   r"   c                 |    | j                   j                  t        | j                  j	                               |      S r   )r5   is_node_supporteddictr4   named_modulesr%   s     r   __is_node_supportedz.CapabilityBasedPartitioner.__is_node_supported5   s2    !!33D9J9J9X9X9Z4[]ab	
r   c           	         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 ||<   D ]
  }d ||   <    t        |j                               }t        |      dkD  sj|d	   }|dd  D ]  }	 |||	         t
        j                  d
       i }
| j                  j                  j                  D ]  }d}|j                   D ]-  }|j"                  dk7  st%        |j&                        dk7  s+d} n |sDj)                  |d       }|j                   D ]  }j)                  |d       |k7  s||
|<     |
j+                         D ]  \  }} |||        | j,                  st
        j                  d       ddh}|j/                  t1        | j2                              }g }j+                         D ]  \  }}d	}|j                  D ]l  }|j"                  dk(  st5        |j&                        sJ t%        |j&                        |vr|dz  }t%        |j&                        | j6                  v sh|dz  }n |dk  s|j9                  |        |D ]  }|=  t
        j                  d       j+                         D ]>  \  }}t
        j                  d||j                  D cg c]  }|j:                   c}       @ t        j=                               S c c}w )Nself_idother_idc                 6   t        |    j                        j                  |   j                         t               fd}D ]#  }|j                  D ]  }|vs ||      s  y % |    _        |   j                  D ]  }| |<   	 |= y)Nc                    t               }|j                  |        |r|j                         }|v r|v ry|v rO|      j                  D ]9  }|j                  D ](  }||      j                  vs|j                  |       * ; n"|j                  D ]  }|j                  |        j                  |       |ry)NTF)r   appendpopr   usersr$   )		root_nodestackr"   p_node	user_node
assignmentmerged_nodespartitions_by_idvisiteds	        r   dfs_iter_find_cycleziCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partition.<locals>.dfs_iter_find_cycleM   s    &+gY' 99;Dw |+# z)
 '7z$7G&H&N&NF-3\\	#,4DZPTEU4V4\4\#\$)LL$; .: 'O
 *.I!LL3 *4 KK%- 0 r   FT)r   r   updater   rF   )	r@   rA   rO   r"   rJ   rL   rN   rK   rM   s	        @@r   maybe_merge_partitionzLCapabilityBasedPartitioner.propose_partitions.<locals>.maybe_merge_partitionC   s     0 9 ? ?@L 0 : @ @A
 "%G> %!%I 49LY9W  %	 ", % /;W%+(288#*
4  9 !*r   r"   r   c                     | v r|       j                  |        |j                  |        y |vr|| <   t        || g      |<   y || <   |   j                  |        y )N)r   r   )r)   rE   r   r&   )r"   r   rK   rM   s     r   merge_single_nodezHCapabilityBasedPartitioner.propose_partitions.<locals>.merge_single_node~   sx    z! D!12>>tDzt$++#%
4 '0Btf'E $#%
4  $--d3r   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: %s)	itertoolscountr0   r   r   loggerdebugreversedr4   graphr   ._CapabilityBasedPartitioner__is_node_supportednextlistkeysr+   rF   opr   targetgetitemsr6   unionr   r7   callabler8   rD   namevalues)r   new_partition_idrQ   rS   r"   merge_candidatespartition_idmerge_candidates_listr@   rA   nodes_reassignmentis_tuple_outputuserr   default_non_compute_opsr7   partitions_to_remove	partitioncompute_node_countrK   rM   s                      @@r   propose_partitionsz-CapabilityBasedPartitioner.propose_partitions:   sL   &(
13$??,
9	3 9	# 9	v	4D 	4hsm 	4 	./T..44::;D02 ''-$j2H#$45!$515 . #59 D!12 # %))9)>)>)@$A!()A-/2 5ab 9H *'8<	 !:+ <8 	TU.0%%++11D"O

77o-&t{{37JJ&+O	 # ^^D$/ JJD!~~dD1R735*40 ' 2 +002HD"dB' 3 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.t{{;?R.!3..t{{;t?e?ee.!3. , &*(//3 ": +$R( + 	+,-335MB	LL,b2Y4992YZ 6 $++-.. 3Zs   1M&
partitionsc           	          t         j                  d       t        | j                  |D cg c]  }t	        |j
                         c}      S c c}w )NzFusing partitions...)rX   rY   r
   r4   r^   r   )r   rt   rq   s      r   fuse_partitionsz*CapabilityBasedPartitioner.fuse_partitions   sA    +,!$"3"3]g5h]gPYd9??6K]g5hii5hs   A
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 ]E  } |      s ||j                  |      s ||j                  |      s5|j                  |       G t        |      dk7  sp|j                  |z
  |_         y )Nr"   c                 T    | j                   dk(  xr t        | j                        v S )NrU   )r`   r   ra   )r"   r7   s    r   is_non_compute_nodezVCapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_non_compute_node   s,    77o- D#DKK0OCDr   rq   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"   rq   rz   input_nry   is_transparent_input_nodetransparent_input_nodess       r   r   z\CapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_transparent_input_node   s    ww-'D	,At}G\...t44"4(#33G4WiW8=/5$  4 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`   rF   )r"   rq   rz   output_nry   is_transparent_output_nodetransparent_output_nodess       r   r   z]CapabilityBasedPartitioner.remove_bookend_non_compute_ops.<locals>.is_transparent_output_node   s    ww-'D	,At}G\///55"4( $

H5h	=Y9>06$ !+ 26(.-2$T*r   r   )r   r7   r   r   r   r$   r+   )r   rt   rq   r)   r"   ry   r   r   r7   r   r   s        @@@@@@r   remove_bookend_non_compute_opsz9CapabilityBasedPartitioner.remove_bookend_non_compute_ops   s    d223	Dd 	D
 5757 	D 	SY 	WZ[_W` 		T 	c$i 	X[\`Xa 	 $I &)UK!&t,.tY__kR/ioo{SOOD)	 ( ;1$"+//K"?	 $r   c                 H    | j                         }| j                  |      }|S r   )rs   rv   )r   rt   fused_gms      r   partition_and_fusez-CapabilityBasedPartitioner.partition_and_fuse  s%    ,,.
''
3r   )FNN)r-   r.   r/   r   r   boolr   r   r   r   r   r\   r   r   rs   rv   r   r   r1   r   r   r3   r3   "   s    
 7<<@NR
*
#6
 04
 #+8C="9	

 5=Xc]4K
 
"
 
 

Z/DO Z/xj$y/ jk j5@i 5@nK r   r3   )typingr   r   r   r   r   r   r	   !torch.fx.passes.utils.fuser_utilsr
   torch.fx.graph_moduler   torch.fx.noder   r    torch.fx.passes.operator_supportr   loggingrV   r   collectionsr   	getLoggerr-   rX   setLevelWARNINGr   r3   r1   r   r   <module>r      s^    G G G @ - 3 @    			8	$    "t tr   