
    IЦi+                        S SK r S SKrS SKJr  S SKJrJrJrJrJ	r	J
r
JrJr  S SKrS SKrS SKJr  S SKJr  / SQr\\\R(                     \\R(                     4   r\\R(                  \4   r\\R.                  R0                     r\
\R.                  R0                     r\\   r1 Skr\" SS	9S
 5       r\" SS	9S\\\R>                  R@                  4   S\R.                  R0                  S\4S j5       r!\" SS	9S\R.                  R0                  S\"4S j5       r#\" SS	9 " S S5      5       r$\" SS	9S\R.                  RJ                  S\R.                  RJ                  4S j5       r&g)    N)	dataclass)AnyDictListMappingOptionalSetTupleUnion)compatibility)_get_qualified_name)get_acc_ops_nameget_node_targetis_node_output_tensorFxNetAccFusionsFinderlegalize_graph>   call_methodcall_modulecall_functionF)is_backward_compatiblec                     [        U [        5      (       a  U $ U R                  (       a  SU R                  ;   a  SU R                   3$ U R                  R	                  SS5      nU(       a  UOS SU R                   3$ )Nacc_opsacc_ops.z
torch._opsz	torch.ops .)
isinstancestr
__module____name__replace)kmodules     [/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/torch/fx/passes/tools_common.pyr   r      sl    !S	
)q||3!**&&%%+
 #&+1QZZL99    
submodulesnodereturnc                 D   UR                   [        ;   d-   SSR                  [        5      -   SUR                    3-   5       eUR                   S:X  aP  [        UR                  [
        5      (       d   eXR                     n[        US[        U5      5      n[        U5      $ UR                   S:X  aC  UR                  nUR                  b  SUR                  ;   a  SUR                   3$ [        U5      $ [        UR                  [
        5      (       d   eUR                  $ )	a  
Given a `node` returns its target typename.

For "call_method" node, return node.target which is the name of that method being called.
This could potential lead to conflict but should be okay because normally it's on a tensor.

For "call_function" node, return typename of node.target.

For "call_module" node, return typename of the module that node.target point to.

If seeing "_VariableFunctionsClass" in the target name string, it will be replaced by
"torch". e.g. _VariableFunctionsClass.relu would become torch.relu.
zExpect op types of z, z, but found r   _base_class_originr   r   r   )opCALLABLE_NODE_OPSjoinr   targetr   getattrtyper   r   r   r   )r%   r&   submodsubmod_typer-   s        r#   r   r   *   s   $ 77'' 		*; <<dggY?WW' ww-$++s++++KK(f&:DLI,,	O	#kk   ,f>O>O1O v'(	
 %V,	
 $++s++++{{r$   c                     U R                   R                  SS5      nUSL=(       a    [        U[        R                  5      $ )a	  Checks if the node output produces a Tensor or not.

NOTE: This requires to run `ShapeProp` on the containing fx graph before
calling this function. This is because it works by checking the `type`
metadata on the node. This metadata is produced by the `ShapeProp`.
r/   N)metaget
issubclasstorchTensor)r&   type_s     r#   r   r   Q   s3     IIMM&$'E@E5<<!@@r$   c                       \ rS rSrSrS\R                  R                  S\4S jr	\
 " S S5      5       r SS	S
S\\\4   S\\   4S jjrS\\R                  R"                  \4   4S jrSrg)r   ]   zy
Finds groups of connected ACC nodes that pass non-tensor data between each other.
Such groups are called fusion groups.
r"   	acc_nodesc                 d    Xl         [        UR                  R                  5      U l        X l        g N)r"   listgraphnodesr;   )selfr"   r;   s      r#   __init__FxNetAccFusionsFinder.__init__d   s"    &,,,,-
"r$   c                   D    \ rS rSr% \\S'   \\S'   \\S'   \\S'   S rSrg)	!FxNetAccFusionsFinder.FusionGroupi   top_node_idxr@   inputsnodes_need_processc                    XR                   ;   a  gU R                  R                  U5        U R                   R                  U5        U R                  R	                  U5        U R                  R                  UR                   Vs1 s H,  nUR                  [        ;   d  M  X R                   ;  d  M*  UiM.     sn5        gs  snf )z
Add a node to fusion group.
N)	r@   rI   addrH   discardupdateall_input_nodesr*   r+   )rA   r&   ns      r#   add_node*FxNetAccFusionsFinder.FusionGroup.add_nodew   s     zz!##''-JJNN4 KK%KK "111tt00 56jj5H 1s   C !C 2C  N)	r   r   __qualname____firstlineno__int__annotations__NodeSetrP   __static_attributes__rR   r$   r#   FusionGrouprE   i   s%        $#	r$   rY   Nfusion_grouprE   rH   visitedc                 X   U H  nUb  XC;   a  M  UR                  U5        UR                  [        ;  a  M4  U R                  R	                  U5      UR
                  :  a  M_  XAR                  ;   a    gU R                  XR                  U5      (       d  M  UR                  U5          g   g)z
Start from inputs and going reverse topological order. If any upstream node
is in the fusion group, add all the nodes in this path to fusion group.
TF)	rK   r*   r+   r@   indexrG   recursive_add_noderN   rP   )rA   rZ   rH   r[   args        r#   r^   (FxNetAccFusionsFinder.recursive_add_node   s     C">C  vv.. zz$|'@'@@ ((( &&|5H5H'RR%%c*1 4 r$   r'   c                 @   0 n[        U R                  5      nU GH  nX1;   a  M  UR                  [        ;  a  M!  SUR                  ;   a  M3  X0R                  ;  a  MD  U R                  U R                  R                  U5      U1[        UR                  5      U1S9nUR                  (       Ga  UR                  R                  5       nU R                  UUR                  [        5       S9  SUR                  ;  an  UR                   H^  nUR                  [        ;  a  M  XTR                  ;   a  M*  UR                  U5        U R                  UUR                  [        5       S9  M`     UR                   H  nUR                  [        ;  a  M  SUR                  ;   a  M+  XdR                  ;   a  M<  UR                  U5        [!        UR"                  U R                  R                  U5      5      Ul        U R                  UUR                  [        5       S9  M     UR                  (       a  GM  [        UR                  5      U R                  ::  d"  U =R                  UR                  -  sl        GM^  UR                   H  nUR                  X'   M     GM     U$ )Ntensor_meta)rG   r@   rH   rI   )r[   )r>   r;   r*   r+   r3   rY   r@   r]   setrN   rI   popr^   rH   usersrP   minrG   )rA   resultr;   r&   rZ   userr_   rO   s           r#   __call__FxNetAccFusionsFinder.__call__   sE   /1(	D~ww//		)>>)>B>N>N!ZZ--d3f4//0$(6	 ?O ?L 111#66::<''  ''E (  !		1 $

77*;;$#5#55$$--d3//((//$'E 0  !+  //Cvv%66 $0 000  ))#.03$114::3C3CC3H1L- ++$$++ # ,  01 111T **+t~~=,"4"44%++A , 2 2FI ,{ @ r$   )r;   r"   r@   r=   )r   r   rS   rT   __doc__r6   fxGraphModulerW   rB   r   rY   r   NodeListr   r^   r   Noderi   rX   rR   r$   r#   r   r   ]   s    
#uxx33 # #
   F &*	$9$ gx'($ '"	$LD$uxx}}g56 Dr$   r   gmc                 R  ^ [         R                  [         R                  [         R                  [         R                  [         R
                  [         R                  [         R                  [         R                  [         R                  [         R                  [         R                  [         R                  [        R                  R                  R                   R"                  [        R                  R                  R$                  R"                  [        R                  R                  R&                  R(                  [        R                  R                  R*                  R"                  [        R                  R                  R,                  R"                  /n[.        R1                  U R2                  R4                  S5      n[        R6                  R9                  5       nU R2                  R4                   H"  nUR:                   H  nX%==   S-  ss'   M     M$     [<        R>                  " 5       nU R2                  R4                   H  nX$   S:X  d  M  URA                  U5        M      0 m[C        U5      S:  a  URE                  5       nURG                  UU4S j5      TU'   UR:                   H]  nX%==   S-  ss'   X%   S:X  d  M  URH                  S:X  a#  URJ                  U;   a  URM                  U5        ML  URA                  U5        M_     [C        U5      S:  a  M  [C        UR4                  5      [C        U R2                  R4                  5      :  a)  [O        SU Vs/ s H  oBU   S:w  d  M  UPM     sn 35      eU R2                  RP                  Ul(        X0l        U $ s  snf )a  
Replace the graph of the given GraphModule with one that contains the same nodes as the
original, but in topologically sorted order.

This is used by the merge_matmul transformation below, which disturbs the topologically sorted
order of its input GraphModule, so that this order is restored before further transformation.

Arguments:
    gm: The graph module to topologically sort. It is modified in-place.

Returns:
    The graph module in-place sorted
r      c                    > TU    $ r=   rR   )xenvs    r#   <lambda> legalize_graph.<locals>.<lambda>.  s	    c!fr$   r   z&Input graph has cycles, unable to add ))operatorrK   mulsubfloordivtruedivmodleltgegteqner6   opsatensym_constrain_rangedefaultsym_constrain_range_for_size_assert_asyncmsgscalar_tensor_assert_scalardictfromkeysr?   r@   rl   Graphre   collectionsdequeappendlenpopleft	node_copyr*   r-   
appendleftRuntimeError_codegen)	rp   PRIORITIZED_OPSindeg	new_graphr&   rh   queuecurru   s	           @r#   r   r      st   * 			**22		33;;		$$((		$$,,		%%--#O( MM"((..!,E IJJDK1K    +002E;!LL  /1C e*q.mmo&&s,<=CIIDK1K{a77o-$++2P$$T*LL&  e*q. 9??c"((..114u5autVZP[_`P`du5a4bc
 	
 **IHI	 6bs   "N$
2N$
)'r   rx   dataclassesr   typingr   r   r   r   r   r	   r
   r   r6   torch.fxtorch.fx._compatibilityr   torch.fx.noder   __all__r7   TensorsTensorOrTensorsrl   ro   rn   rW   r   Namesr+   r   nnModuler   boolr   r   rm   r   rR   r$   r#   <module>r      s     ! H H H   1 - ell#T%,,%77
8g-.
ehhmm
S	C  e,	: -	: e,#UXX__,-#5:XX]]## -#L e,A A$ A -A e,U U -Up e,Guxx++ G0D0D G -Gr$   