Ë
    f^(hÂ+  ã                   óâ  — d dl Z d dlZd dlmZ d dlmZ d dlmZmZm	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,                  j.                     Zee
j,                  j.                     Zee   Zh d£Z ed	¬
«      d„ «       Z ed	¬
«      deee
j>                  j@                  f   de
j,                  j.                  defd„«       Z! ed	¬
«      de
j,                  j.                  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)é    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                 óÚ   — 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     úZ/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/torch/fx/passes/tools_common.pyr
   r
      si   € ä�!”SÔØˆØ	
�Š˜) q§|¡|Ñ3Ø˜!Ÿ*™*˜Ð&Ð&à—‘×%Ñ%Ø˜+ó
ˆñ #‘&¨Ð+¨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   R   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Únodesr6   )Úselfr   r6   s      r   Ú__init__zFxNetAccFusionsFinder.__init__e   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;   rB   ÚaddrA   ÚdiscardÚupdateÚall_input_nodesr&   r'   )r<   r"   Úns      r   Úadd_nodez*FxNetAccFusionsFinder.FusionGroup.add_nodex   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__ÚNodeSetrI   © r    r   ÚFusionGroupr?   j   s%   … ð Óð ‹ð ‹ð $Ó#ó	r    rO   NÚfusion_groupr?   rA   Ú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)	rD   r&   r'   r;   Úindexr@   Úrecursive_add_noderG   rI   )r<   rP   rA   rQ   Úargs        r   rT   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)r@   r;   rA   rB   )rQ   )r9   r6   r&   r'   r/   rO   r;   rS   ÚsetrG   rB   ÚpoprT   rA   ÚusersrI   Úminr@   )r<   Úresultr6   r"   rP   ÚuserrU   rH   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    r8   )r   r   rJ   Ú__doc__r2   ÚfxÚGraphModulerM   r=   r   rO   r   ÚNodeListr   rT   ÚdictÚNoder^   rN   r    r   r   r   ^   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 r8   rN   )ÚxÚenvs    €r   ú<lambda>z legalize_graph.<locals>.<lambda>/  s   ø€ °c¸!±f€ r    r   z&Input graph has cycles, unable to add ))ÚoperatorrD   Ú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_scalarrc   Úfromkeysr:   r;   r`   ÚGraphrZ   ÚcollectionsÚdequeÚappendÚlenÚpopleftÚ	node_copyr&   r)   Ú
appendleftÚRuntimeErrorÚ_codegen)	re   ÚPRIORITIZED_OPSÚindegÚ	new_graphr"   r]   ÚqueueÚcurrj   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ÜØ4ÀuÖ5a¸tÐPUÐVZÑP[Ð_`ÓP`²dÒ5aÐ4bÐcó
ð 	
ð Ÿ™×*Ñ*€IÔØ€B„HØ€Iùò	 6bs   ÍN
ÍN
)'rƒ   rl   Úcollections.abcr   Údataclassesr   Útypingr   r   r   r2   Útorch.fxÚtorch.fx._compatibilityr   Útorch.fx.noder	   Ú__all__Útupler3   r9   ÚTensorsÚTensorOrTensorsr`   rd   rb   rX   rM   r   ÚNamesr'   r
   ÚnnÚModuler   Úboolr   r   ra   r   rN   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·]±]ð#àò#ó -ð#ñL  eÔ,ðA §¡§¡ð A°$ò Aó -ðAñ  eÔ,÷Uð Uó -ðUñp  eÔ,ðG�u—x‘x×+Ñ+ð G°·±×0DÑ0Dò Gó -ñGr    