
    [Th+                        S SK r S SKrS SKJr  S SKJr  S SK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,                  R.                     r\\
R,                  R.                     r\\   r1 Skr\" S	S
9S 5       r\" S	S
9S\\\
R>                  R@                  4   S\
R,                  R.                  S\4S j5       r!\" S	S
9S\
R,                  R.                  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)Mapping)	dataclass)AnyOptional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     T/var/www/auris/envauris/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   R   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nodesr7   )selfr   r7   s      r   __init__FxNetAccFusionsFinder.__init__e   s"    &,,,,-
"r    c                   D    \ rS rSr% \\S'   \\S'   \\S'   \\S'   S rSrg)	!FxNetAccFusionsFinder.FusionGroupj   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<   rE   addrD   discardupdateall_input_nodesr&   r'   )r=   r"   ns      r   add_node*FxNetAccFusionsFinder.FusionGroup.add_nodex   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__NodeSetrL   __static_attributes__rN   r    r   FusionGrouprA   j   s%        $#	r    rU   Nfusion_grouprA   rD   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)	rG   r&   r'   r<   indexrC   recursive_add_noderJ   rL   )r=   rV   rD   rW   args        r   rZ   (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)rC   r<   rD   rE   )rW   )r:   r7   r&   r'   r/   rU   r<   rY   setrJ   rE   poprZ   rD   usersrL   minrC   )r=   resultr7   r"   rV   userr[   rK   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    )r7   r   r<   r9   )r   r   rO   rP   __doc__r2   fxGraphModulerS   r>   r   rU   r   NodeListr   rZ   dictNodere   rT   rN   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    $ r9   rN   )xenvs    r   <lambda> legalize_graph.<locals>.<lambda>/  s	    c!fr    r   z&Input graph has cycles, unable to add ))operatorrG   mulsubfloordivtruedivmodleltgegteqner2   opsatensym_constrain_rangedefaultsym_constrain_range_for_size_assert_asyncmsgscalar_tensor_assert_scalarrk   fromkeysr;   r<   rh   Graphra   collectionsdequeappendlenpopleft	node_copyr&   r)   
appendleftRuntimeError_codegen)	rm   PRIORITIZED_OPSindeg	new_graphr"   rd   queuecurrr   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   ru   collections.abcr   dataclassesr   typingr   r   r   r2   torch.fxtorch.fx._compatibilityr   torch.fx.noder	   __all__tupler3   r:   TensorsTensorOrTensorsrh   rl   rj   r_   rS   r   Namesr'   r
   nnModuler   boolr   r   ri   r   rN   r    r   <module>r      s     # ! ' '   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    