ó
    Eñin7  ã                   óF  • S SK r 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.                  R0                     r\\R.                  R0                     r\\   r1 Skr\" S	S
9S 5       r\" S	S
9S\\\R@                  RB                  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.                  RL                  S\R.                  RL                  4S j5       r'\" S	S
9S\R.                  RL                  S\R.                  RL                  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Ústable_topological_sort>   Ú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     ÚY/home/mande/repo/quber/.venv/lib/python3.13/site-packages/torch/fx/passes/tools_common.pyr
   r
       sl   € ä�!”S×ÑØˆØ	
��˜) q§|¡|Ó3Ø˜!Ÿ*™*˜Ð&Ð&à—‘×%Ñ%Ø˜+ó
ˆö #‘&¨Ð+¨1¨Q¯Z©Z¨LÐ9Ð9ó    Ú
submodulesÚnodeÚreturnc                 óÈ  • UR                   [        ;  a1  [        SSR                  [        5      -   SUR                    3-   5      eUR                   S:X  ao  [	        UR
                  [        5      (       d!  [        S[        UR
                  5       35      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!  [        S[        UR
                  5       35      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   zExpected str target, got Ú_base_class_originr   r   r   )ÚopÚCALLABLE_NODE_OPSÚAssertionErrorÚjoinr   Útargetr   ÚtypeÚgetattrr
   r   r   r	   )r"   r#   ÚsubmodÚsubmod_typer+   s        r    r   r   -   s;  € ð$ ‡w�wÔ'Ó'ÜØ!Ø�i‰iÔ)Ó*ñ+à˜TŸW™W˜IÐ&ñ'ó
ð 	
ð ‡w�w�-ÓÜ˜$Ÿ+™+¤s×+Ñ+Ü Ð#<¼TÀ$Ç+Á+Ó=NÐ<OÐ!PÓQÐQØŸK™KÑ(ˆÜ˜fÐ&:¼DÀ»LÓIˆÜ Ó,Ð,Ø	�‰�OÓ	#Ø—k‘kˆð × Ñ Ñ,°¸f×>OÑ>OÓ1Oð �v—‘Ð'Ð(ð	
ô % VÓ,ð	
ô ˜$Ÿ+™+¤s×+Ñ+Ü Ð#<¼TÀ$Ç+Á+Ó=NÐ<OÐ!PÓQÐQØ�{‰{Ð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   Y   s3   € ð �I‰I�M‰M˜& $Ó'€EØ˜Ð×@¤¨E´5·<±<Ó!@Ð@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   ée   zy
Finds groups of connected ACC nodes that pass non-tensor data between each other.
Such groups are called fusion groups.
r   Ú	acc_nodesc                 óÎ   • Xl         [        UR                  R                  5      U l        X l        [        U R                  5       VVs0 s H  u  p4XC_M	     snnU l        g s  snnf ©N)r   ÚlistÚgraphÚnodesr9   Ú	enumerateÚ
node_index)Úselfr   r9   Úir#   s        r    Ú__init__ÚFxNetAccFusionsFinder.__init__l   sJ   € ØŒÜ˜&Ÿ,™,×,Ñ,Ó-ˆŒ
Ø"ŒÜ2;¸D¿J¹JÔ2GÔHÒ2G¡w q˜4š7Ñ2GÒHˆ�ùÓHs   Á	A!c                   óD   • \ rS rSr% \\S'   \\S'   \\S'   \\S'   S rSrg)	Ú!FxNetAccFusionsFinder.FusionGroupér   Ú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>   rJ   ÚaddrI   ÚdiscardÚupdateÚall_input_nodesr'   r(   )rA   r#   Úns      r    Úadd_nodeÚ*FxNetAccFusionsFinder.FusionGroup.add_node€   s�   € ð —z‘zÓ!Øà×#Ñ#×'Ñ'¨Ô-Ø�J‰J�N‰N˜4Ô Ø�K‰K×Ñ Ô%Ø�K‰K×Ñð "×1Ò1óâ1˜Ø—t‘tÔ0Ñ0ó à56¿j¹jÑ5H÷ Ù1ñõùòs   ÂC Â!C Â2C © N)	r   r   Ú__qualname__Ú__firstlineno__ÚintÚ__annotations__ÚNodeSetrQ   Ú__static_attributes__rS   r!   r    ÚFusionGrouprF   r   s%   ‡ ð Óð ‹ð ‹ð $Ó#õ	r!   rZ   NÚfusion_grouprF   rI   Úvisitedc                 ó@  • U H˜  nUb  XC;   a  M  UR                  U5        UR                  [        ;  a  M4  U R                  U   UR                  :  a  MS  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)	rL   r'   r(   r@   rH   r>   Úrecursive_add_noderO   rQ   )rA   r[   rI   r\   Úargs        r    r^   Ú(FxNetAccFusionsFinder.recursive_add_node’   s˜   € ó ˆCàÑ"Ø“>ÙØ—‘˜CÔ ð �v‰vÔ.Ó.Ùð �‰˜sÑ# l×&?Ñ&?Ó?Ùð ×(Ñ(Ó(Ùð ×&Ñ& |×5HÑ5HÈ'×RÓRØ×%Ñ% cÔ*Ùñ1 ð4 r!   r$   c                 ó  • 0 n[        U R                  5      nU GHg  nX1;   a  M  UR                  [        ;  a  M!  SUR                  ;   a  M3  X0R                  ;  a  MD  U R                  U R                  U   U1[        UR                  5      U1S9nUR                  (       Gaw  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                  U   5      Ul        U R                  UUR                  [        5       S9  Mš     UR                  (       a  GMw  [        UR                  5      U R                  ::  d"  U =R                  UR                  -  sl        GMF  UR                   H  nUR                  X'   M     GMj     U$ )NÚtensor_meta)rH   r>   rI   rJ   )r\   )r<   r9   r'   r(   r1   rZ   r@   ÚsetrO   rJ   Úpopr^   rI   Úusersr>   rQ   ÚminrH   )rA   Úresultr9   r#   r[   Úuserr_   rP   s           r    Ú__call__ÚFxNetAccFusionsFinder.__call__¸   s9  € Ø/1ˆÜ˜Ÿ™Ó(ˆ	äˆDØ‹~ÙØ�w‰wÔ/Ó/ÙØ §	¡	Ó)ÙØŸ>™>Ó)Ùà>B×>NÑ>NØ!Ÿ_™_¨TÑ2Ø�fÜ˜4×/Ñ/Ó0Ø$( 6ð	 ?Oð ?ˆLð ×1×1Ð1Ø#×6Ñ6×:Ñ:Ó<�Ø×'Ñ'Ø Ø ×'Ñ'Ü›Eð (ñ ð !¨¯	©	Ó1Ø $§
¤
˜ØŸ7™7Ô*;Ó;Ù$Ø×#5Ñ#5Ó5Ù$à$×-Ñ-¨dÔ3Ø×/Ñ/Ø(Ø(×/Ñ/Ü$'£Eð 0ó ñ !+ð  ×/Ô/�CØ—v‘vÔ%6Ó6Ù Ø$¨¯©Ó0Ù Ø×0Ñ0Ó0Ù à ×)Ñ)¨#Ô.Ü03Ø$×1Ñ1°4·?±?À3Ñ3Gó1�LÔ-ð ×+Ñ+Ø$Ø$×+Ñ+Ü #£ð ,ó ñ 0ð1 ×1×1Ò1ôT ˜×*Ñ*Ó+¨t¯~©~Ó=Ø—’ ,×"4Ñ"4Ñ4—�à%×+Ô+�AØ ,× 2Ñ 2�F“Iô ,ñ{ ð@ ˆr!   )r9   r   r@   r>   r;   )r   r   rT   rU   Ú__doc__r4   ÚfxÚGraphModulerX   rC   r   rZ   r   ÚNodeListr   r^   ÚdictÚNoderi   rY   rS   r!   r    r   r   e   s—   † ñð
I˜uŸx™x×3Ñ3ð IÀô Ið ÷ð ó ððF &*ñ	$à9ð$ð �g˜xÐ'Ñ(ð$ð ˜'Ñ"õ	$ðLD˜$˜uŸx™xŸ}™}¨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 )ay  
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

Warning:
    This topological sort is NOT stable, it will NOT preserve the original node order.
    If you need a stable topological sort, use stable_topological_sort instead.
r   é   c                 ó   >• TU    $ r;   rS   ©ÚxÚenvs    €r    Ú<lambda>Ú legalize_graph.<locals>.<lambda>;  ó	   ø€ °c¸!²fr!   r   ú&Input graph has cycles, unable to add ))ÚoperatorrL   ÚmulÚsubÚfloordivÚtruedivÚmodÚleÚltÚgeÚgtÚeqÚner4   ÚopsÚatenÚsym_constrain_rangeÚdefaultÚsym_constrain_range_for_sizeÚ_assert_asyncÚmsgÚscalar_tensorÚ_assert_scalarro   Úfromkeysr=   r>   rl   ÚGraphre   ÚcollectionsÚdequeÚappendÚlenÚpopleftÚ	node_copyr'   r+   Ú
appendleftÚRuntimeErrorÚ_codegen)	rq   ÚPRIORITIZED_OPSÚindegÚ	new_graphr#   rh   ÚqueueÚcurrw   s	           @r    r   r   ÿ   st  ø€ ô2 	�‰Ü�‰Ü�‰Ü×ÑÜ×ÑÜ�‰Ü�‰Ü�‰Ü�‰Ü�‰Ü�‰Ü�‰Ü�	‰	�‰×*Ñ*×2Ñ2Ü�	‰	�‰×3Ñ3×;Ñ;Ü�	‰	�‰×$Ñ$×(Ñ(Ü�	‰	�‰×$Ñ$×,Ñ,Ü�	‰	�‰×%Ñ%×-Ñ-ð#€Oô( �M‰M˜"Ÿ(™(Ÿ.™.¨!Ó,€EÜ—‘—‘Ó €Ià—‘—”ˆØ—J”JˆDØ‹K˜1Ñ�Kó ñ ô  +×0Ò0Ó2€Eà—‘—”ˆØ‰;˜!ÕØ�L‰L˜Öñ ð /1€Cô ˆe‹*�q‹.Ø�m‰m‹oˆØ×&Ñ& sÔ,<Ó=ˆˆC‰Ø—I”IˆDØ‹K˜1Ñ‹KØ‰{˜aÕØ—7‘7˜oÓ-°$·+±+ÀÓ2PØ×$Ñ$ TÖ*à—L‘L Ö&ñ ô ˆe‹*�q�.ô ˆ9�?‰?Óœc "§(¡(§.¡.Ó1Ó1ÜØ4ÁuÓ5aÂu¸tÐVZÑP[Ð_`ÑP`·dÁuÑ5aÐ4bÐcó
ð 	
ð Ÿ™×*Ñ*€IÔØ„HØ€Iùò	 6bs   Í"N$
Í2N$
c                 ó"  ^
• [         R                  U R                  R                  S5      n[        R
                  R                  5       n[        U R                  R                  5       VVs0 s H  u  p4XC_M	     nnnU R                  R                   H"  nUR                   H  nX==   S-  ss'   M     M$     / nU R                  R                   H(  nX   S:X  d  M  [        R                  " XuU   U45        M*     0 m
U(       a~  [        R                  " U5      u  p‰UR                  U	U
4S j5      T
U	'   U	R                   H4  nX==   S-  ss'   X   S:X  d  M  [        R                  " XuU   U45        M6     U(       a  M~  [        UR                  5      [        U R                  R                  5      :w  a)  [        SU Vs/ s H  oAU   S:w  d  M  UPM     sn 35      eU R                  R                  Ul        X l        U $ s  snnf 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 while preserving the original node order
as much as possible.

This function performs a stable topological sort where nodes appear in an order that:
1. Respects data dependencies (topological ordering)
2. Preserves the original node order when there are no dependency constraints

The algorithm uses Kahn's algorithm with a priority queue: nodes with all dependencies
satisfied are added to a min-heap, ordered by their original position. This ensures
we always process the earliest node in the original order among ready nodes.

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

Returns:
    The graph module in-place sorted
r   rs   c                 ó   >• TU    $ r;   rS   ru   s    €r    rx   Ú)stable_topological_sort.<locals>.<lambda>}  rz   r!   r{   )ro   r‘   r=   r>   r4   rl   r’   r?   re   ÚheapqÚheappushÚheappopr˜   r–   r)   r›   )rq   r�   rž   Úidxr#   Ú
node_to_idrh   Úready_queueÚ_r    rw   s             @r    r   r   N  s¸  ø€ ô* �M‰M˜"Ÿ(™(Ÿ.™.¨!Ó,€EÜ—‘—‘Ó €Iô $-¨R¯X©X¯^©^Ô#<ô,Ú#<‘i�cˆŠ	Ñ#<ð ñ ,ð
 —‘—”ˆØ—J”JˆDØ‹K˜1Ñ�Kó ñ ð 46€KØ—‘—”ˆØ‰;˜!ÕÜ�NŠN˜;°DÑ)9¸4Ð(@ÖAñ ð /1€Cö ä—’˜{Ó+‰ˆØ×&Ñ& sÔ,<Ó=ˆˆC‰ð —I”IˆDØ‹K˜1Ñ‹KØ‰{˜aÕÜ—’˜{¸Ñ-=¸tÐ,DÖEñ ÷ ˆ+ô ˆ9�?‰?Óœs 2§8¡8§>¡>Ó2Ó2ÜØ4ÁuÓ5aÂu¸tÐVZÑP[Ð_`ÑP`·dÁuÑ5aÐ4bÐcó
ð 	
ð Ÿ™×*Ñ*€IÔØ„HØ€IùóM,ùòB 6bs   Á,HÇH
ÇH
))r“   r¤   r|   Úcollections.abcr   Údataclassesr   Útypingr   r   r   r4   Útorch.fxÚtorch.fx._compatibilityr   Útorch.fx.noder	   Ú__all__Útupler5   r<   ÚTensorsÚTensorOrTensorsrl   rp   rn   rc   rX   r   ÚNamesr(   r
   ÚnnÚModuler   Úboolr   r   rm   r   r   rS   r!   r    Ú<module>r¹      sË  ðã Û Û Ý #Ý !ß 'Ñ 'ã Û Ý 1Ý -ò€ð ��e—l‘lÑ# T¨%¯,©,Ñ%7Ð7Ñ
8€Ø˜Ÿ™ gÐ-Ñ.€Ø�—‘—‘Ñ€Ø
ˆe�h‰h�m‰mÑ
€ØˆS‰	€ÚCÐ ñ  eÑ,ñ	:ó -ð	:ñ  eÑ,ð(Ø˜˜UŸX™XŸ_™_Ð,Ñ-ð(Ø5:·X±X·]±]ð(àó(ó -ð(ñV  eÑ,ðA §¡§¡ð A°$ó Aó -ðAñ  eÑ,÷Vð Vó -ðVñr  eÑ,ðK�u—x‘x×+Ñ+ð K°·±×0DÑ0Dó Kó -ðKñ\  eÑ,ð> §¡× 4Ñ 4ð >¸¿¹×9MÑ9Mó >ó -ñ>r!   