Ë
    P/æjs+  ã                   óê  — d dl mZmZmZmZmZmZmZmZ d dl	Z	d dl
mZ d dlZd dlZd dlZd dlmZ d dlmZ g d¢Zeeej(                     eej(                     f   Zeej(                  ef   Zeej.                  j0                     Zeej.                  j0                     Zee   Zh d£Z ed¬	«      d
„ «       Z ed¬	«      deeej>                  j@                  f   dej.                  j0                  defd„«       Z! ed¬	«      dej.                  j0                  de"fd„«       Z# ed¬	«       G d„ d«      «       Z$ ed¬	«      dej.                  jJ                  dej.                  jJ                  fd„«       Z&y)é    )ÚListÚTupleÚUnionÚDictÚAnyÚSetÚMappingÚOptionalN)Ú	dataclass)Ú_get_qualified_name)Úcompatibility)Úget_acc_ops_nameÚget_node_targetÚis_node_output_tensorÚFxNetAccFusionsFinderÚlegalize_graph>   Úcall_methodÚcall_moduleÚcall_functionF)Úis_backward_compatiblec                 óÚ   — t        | t        «      r| S | j                  rd| j                  v rd| j                  › �S | j                  j	                  dd«      }|r|nd› d| j                  › �S )NÚacc_opsúacc_ops.z
torch._opsz	torch.opsÚ ú.)Ú
isinstanceÚstrÚ
__module__Ú__name__Úreplace)ÚkÚmodules     ú_/root/repos/pocs/video-evals/.venv/lib/python3.12/site-packages/torch/fx/passes/tools_common.pyr   r      sd   € ä�!”SÔØˆØ	
�Š˜) q§|¡|Ñ3Ø˜!Ÿ*™*˜Ð&Ð&à—‘×%Ñ% l°KÓ@ˆÙ"‘&¨Ð+¨1¨Q¯Z©Z¨LÐ9Ð9ó    Ú
submodulesÚnodeÚreturnc                 ó$  — |j                   t        v s-J ddj                  t        «      z   d|j                   › �z   «       ‚|j                   dk(  rLt        |j                  t
        «      sJ ‚| |j                     }t        |dt        |«      «      }t        |«      S |j                   dk(  r@|j                  }|j                  �d|j                  v rd|j                  › �S t        |«      S t        |j                  t
        «      sJ ‚|j                  S )	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  € ð  �7‰7Ô'Ñ'ð Ø §	¡	Ô*;Ó <Ñ<ÀÈdÏgÉgÈYÐ?WÑWóÐ'ð ‡w�w�-ÒÜ˜$Ÿ+™+¤sÔ+Ð+Ð+Ø˜DŸK™KÑ(ˆÜ˜fÐ&:¼DÀ»LÓIˆÜ Ó,Ð,Ø	�‰�OÒ	#Ø—k‘kˆð × Ñ Ð,°¸f×>OÑ>OÑ1Oð �v—‘Ð'Ð(ð	
ô % VÓ,ð	
ô ˜$Ÿ+™+¤sÔ+Ð+Ð+Ø�{‰{Ðr$   c                 óz   — | j                   j                  dd«      }|duxr t        |t        j                  «      S )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   E   s3   € ð �I‰I�M‰M˜& $Ó'€EØ˜ÐÒ@¤¨E´5·<±<Ó!@Ð@r$   c                   óÌ   — e Zd ZdZdej
                  j                  defd„Ze	 G d„ d«      «       Z
	 ddd	d
eeef   dee   fd„Zdeej
                  j                   ef   fd„Zy)r   z…
    Finds groups of connected ACC nodes that pass non-tensor data between each other.
    Such groups are called fusion groups.
    r"   Ú	acc_nodesc                 óh   — || _         t        |j                  j                  «      | _        || _        y ©N)r"   ÚlistÚgraphÚnodesr:   )Úselfr"   r:   s      r#   Ú__init__zFxNetAccFusionsFinder.__init__W   s&   € ØˆŒÜ˜&Ÿ,™,×,Ñ,Ó-ˆŒ
Ø"ˆ�r$   c                   ó<   — e Zd ZU eed<   eed<   eed<   eed<   d„ Zy)ú!FxNetAccFusionsFinder.FusionGroupÚtop_node_idxr?   ÚinputsÚnodes_need_processc                 ót  — || j                   v ry| j                  j                  |«       | j                   j                  |«       | j                  j	                  |«       | j                  j                  |j                  D �ch c]$  }|j                  t        v r|| j                   vr|’Œ& c}«       yc c}w )z5
            Add a node to fusion group.
            N)	r?   rF   ÚaddrE   ÚdiscardÚupdateÚall_input_nodesr*   r+   )r@   r&   Úns      r#   Úadd_nodez*FxNetAccFusionsFinder.FusionGroup.add_nodej   s–   € ð �t—z‘zÑ!Øà×#Ñ#×'Ñ'¨Ô-Ø�J‰J�N‰N˜4Ô Ø�K‰K×Ñ Ô%Ø�K‰K×Ñð "×1Ñ1öàØ—t‘tÔ0Ñ0°Q¸d¿j¹jÑ5Hò òõùòs   Â)B5N)r   r   Ú__qualname__ÚintÚ__annotations__ÚNodeSetrM   © r$   r#   ÚFusionGrouprC   \   s%   … ð Óð ‹ð ‹ð $Ó#ó	r$   rS   NÚfusion_grouprC   rE   Úvisitedc                 ó:  — |D ]–  }|�||v rŒ
|j                  |«       |j                  t        vrŒ.| j                  j	                  |«      |j
                  k  rŒW||j                  v r y| j                  ||j                  |«      sŒ…|j                  |«        y y)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)	rH   r*   r+   r?   ÚindexrD   Úrecursive_add_noderK   rM   )r@   rT   rE   rU   Úargs        r#   rX   z(FxNetAccFusionsFinder.recursive_add_node|   s¦   € ð ò 	ˆCàÐ"Ø˜'‘>ØØ—‘˜CÔ ð �v‰vÔ.Ñ.Øð �z‰z×Ñ Ó$ |×'@Ñ'@Ò@Øð �l×(Ñ(Ñ(Ùð ×&Ñ& |°S×5HÑ5HÈ'ÕRØ×%Ñ% cÔ*Ùð1	ð4 r$   r'   c                 óþ  — i }t        | j                  «      }|D �]_  }||v rŒ	|j                  t        vrŒd|j                  v rŒ+|| j                  vrŒ:| j                  | j                  j                  |«      |ht        |j                  «      |h¬«      }|j                  �rs|j                  j                  «       }| j                  ||j                  t        «       ¬«       d|j                  vrj|j                  D ][  }|j                  t        vrŒ||j                  v rŒ%|j                  |«       | j                  ||j                  t        «       ¬«       Œ] |j                  D ]ž  }|j                  t        vrŒd|j                  v rŒ%||j                  v rŒ4|j                  |«       t!        |j"                  | j                  j                  |«      «      |_        | j                  ||j                  t        «       ¬«       Œ  |j                  r�Œst        |j                  «      | j                  k  s!| xj                  |j                  z  c_        �Œ@|j                  D ]  }|j                  ||<   Œ �Œb |S )NÚtensor_meta)rD   r?   rE   rF   )rU   )r=   r:   r*   r+   r3   rS   r?   rW   ÚsetrK   rF   ÚpoprX   rE   ÚusersrM   ÚminrD   )r@   Úresultr:   r&   rT   ÚuserrY   rL   s           r#   Ú__call__zFxNetAccFusionsFinder.__call__¢   s[  € Ø/1ˆÜ˜Ÿ™Ó(ˆ	àó >	3ˆDØ�v‰~ØØ�w‰wÔ/Ñ/ØØ §	¡	Ñ)ØØ˜4Ÿ>™>Ñ)Øà>B×>NÑ>NØ!ŸZ™Z×-Ñ-¨dÓ3Ø�fÜ˜4×/Ñ/Ó0Ø$( 6ð	 ?Oó ?ˆLð ×1Ó1Ø#×6Ñ6×:Ñ:Ó<�Ø×'Ñ'Ø Ø ×'Ñ'Ü›Eð (ô ð !¨¯	©	Ñ1Ø $§
¡
ò ˜ØŸ7™7Ô*;Ñ;Ø$Ø <×#5Ñ#5Ñ5Ø$à$×-Ñ-¨dÔ3Ø×/Ñ/Ø(Ø(×/Ñ/Ü$'£Eð 0õ ðð  ×/Ñ/ò �CØ—v‘vÔ%6Ñ6Ø Ø$¨¯©Ñ0Ø Ø˜l×0Ñ0Ñ0Ø à ×)Ñ)¨#Ô.Ü03Ø$×1Ñ1°4·:±:×3CÑ3CÀCÓ3Hó1�LÔ-ð ×+Ñ+Ø$Ø$×+Ñ+Ü #£ð ,õ ðð1 ×1Ô1ôT ˜×*Ñ*Ó+¨t¯~©~Ò=Ø—’ ,×"4Ñ"4Ñ4—à%×+Ñ+ò 3�AØ ,× 2Ñ 2�F˜1’Iò3ð{>	3ð@ ˆr$   r<   )r   r   rN   Ú__doc__r6   ÚfxÚGraphModulerQ   rA   r   rS   r   ÚNodeListr
   rX   r   ÚNoderb   rR   r$   r#   r   r   P   s”   „ ñð
#˜uŸx™x×3Ñ3ð #Àó #ð
 ÷ð ó ððF &*ñ	$à9ð$ð �g˜xÐ'Ñ(ð$ð ˜'Ñ"ó	$ðLD˜$˜uŸx™xŸ}™}¨gÐ5Ñ6ô Dr$   r   Úgmc                 ó(  ‡— t         j                  t         j                  t         j                  t         j                  t         j
                  t         j                  t         j                  t         j                  t         j                  t         j                  t         j                  t         j                  t        j                  j                  j                   j"                  t        j                  j                  j$                  j"                  t        j                  j                  j&                  j(                  t        j                  j                  j*                  j"                  t        j                  j                  j,                  j"                  g}t.        j1                  | j2                  j4                  d«      }t        j6                  j9                  «       }| j2                  j4                  D ]   }|j:                  D ]  }||xx   dz  cc<   Œ Œ" t=        j>                  «       }| j2                  j4                  D ]  }||   dk(  sŒ|jA                  |«       Œ i ŠtC        |«      dkD  rž|jE                  «       }|jG                  |ˆfd„«      ‰|<   |j:                  D ]X  }||xx   dz  cc<   ||   dk(  sŒ|jH                  dk(  r |jJ                  |v r|jM                  |«       ŒH|jA                  |«       ŒZ tC        |«      dkD  rŒžtC        |j4                  «      tC        | j2                  j4                  «      k  r%tO        d|D �cg c]  }||   dk7  sŒ|‘Œ c}› �«      ‚| j2                  jP                  |_(        || _        | S c c}w )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                 ó   •— ‰|    S r<   rR   )ÚxÚenvs    €r#   ú<lambda>z legalize_graph.<locals>.<lambda>!  s   ø€ °c¸!±f€ r$   r   z&Input graph has cycles, unable to add ))ÚoperatorrH   Ú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?   rd   ÚGraphr^   ÚcollectionsÚdequeÚappendÚlenÚpopleftÚ	node_copyr*   r-   Ú
appendleftÚRuntimeErrorÚ_codegen)	rh   ÚPRIORITIZED_OPSÚindegÚ	new_graphr&   ra   ÚqueueÚcurrm   s	           @r#   r   r   é   s�  ø€ ô* 	�‰Ü�‰Ü�‰Ü×ÑÜ×ÑÜ�‰Ü�‰Ü�‰Ü�‰Ü�‰Ü�‰Ü�‰Ü�	‰	�‰×*Ñ*×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Ø�T‰{˜aÓØ—7‘7˜oÒ-°$·+±+ÀÑ2PØ×$Ñ$ TÕ*à—L‘L Õ&ð	'ô ˆe‹*�q‹.ô ˆ9�?‰?Óœc "§(¡(§.¡.Ó1Ò1ÜÐCÐV[ÖDpÈdÐ_dÐeiÑ_jÐnoÓ_oÂTÒDpÐCqÐrÓsÐsØŸ™×*Ñ*€IÔØ€B„HØ€Iùò Eqs   ÍN
ÍN
)'Útypingr   r   r   r   r   r   r	   r
   r‡   Údataclassesr   ro   r6   Útorch.fxÚtorch.fx.noder   Útorch.fx._compatibilityr   Ú__all__r7   ÚTensorsÚTensorOrTensorsrd   rg   rf   rQ   r   ÚNamesr+   r   ÚnnÚModuler   Úboolr   r   re   r   rR   r$   r#   ú<module>r¡      s†  ðç H× HÓ HÛ Ý !Û ã Û Ý -Ý 1â
u€à
��e—l‘lÑ# T¨%¯,©,Ñ%7Ð7Ñ
8€Ø˜Ÿ™ gÐ-Ñ.€Ø�—‘—‘Ñ€Ø
ˆe�h‰h�m‰mÑ
€ØˆS‰	€ÚCÐ ñ  eÔ,ñ:ó -ð:ñ  eÔ,ð! ¨¨U¯X©X¯_©_Ð(<Ñ =ð !ÀUÇXÁXÇ]Á]ð !ÐWZò !ó -ð!ñF  eÔ,ðA §¡§¡ð A°$ò Aó -ðAñ  eÔ,÷Uð Uó -ðUñp  eÔ,ðE�u—x‘x×+Ñ+ð E°·±×0DÑ0Dò Eó -ñEr$   