§
    ŠŠtjv7  ã                   ó  — d dl Z d dlZd dlZd dlmZ d dlmZ d dl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	j                 ee	j                 z  Ze	j        ez  Zee	j        j                 Zee	j        j                 Zee         Zh d£Z ed	¬
¦  «        deez  def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        j%        de	j        j%        fd„¦   «         Z& ed	¬
¦  «        de	j        j%        de	j        j%        fd„¦   «         Z'dS )é    N)ÚMapping)Ú	dataclass)ÚAny)Ú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_compatibleÚkÚreturnc                 óº   — t          | t          ¦  «        r| S | j        rd| j        v r
d| j        › �S | j                             dd¦  «        }|r|nd› d| j        › �S )NÚacc_opsúacc_ops.z
torch._opsz	torch.opsÚ ú.)Ú
isinstanceÚstrÚ
__module__Ú__name__Úreplace)r   Úmodules     úZ/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/torch/fx/passes/tools_common.pyr   r      s|   € å�!•SÑÔð :ØˆØ	
Œð :˜) q¤|Ð3Ð3Ø&˜!œ*Ð&Ð&Ð&à”×%Ò%Ø˜+ñ
ô 
ˆð #Ð*�&�&¨Ð9Ð9¨Q¬ZÐ9Ð9Ð9ó    Ú
submodulesÚnodec                 óŽ  — |j         t          vr5t          dd                     t          ¦  «        z   d|j         › �z   ¦  «        ‚|j         dk    rxt	          |j        t          ¦  «        s$t          dt          |j        ¦  «        › �¦  «        ‚| |j                 }t          |dt          |¦  «        ¦  «        }t          |¦  «        S |j         dk    r0|j        }|j
        �d	|j
        v r
d
|j        › �nt          |¦  «        S t	          |j        t          ¦  «        s$t          dt          |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   zExpected str target, got Ú_base_class_originr   Nr   r   )ÚopÚCALLABLE_NODE_OPSÚAssertionErrorÚjoinr   Útargetr   ÚtypeÚgetattrr   r   r   r   )r!   r"   ÚsubmodÚsubmod_typer)   s        r   r	   r	   ,   sS  € ð$ „wÕ'Ð'Ð'ÝØ!Ø�iŠiÕ)Ñ*Ô*ñ+à&˜TœWÐ&Ð&ñ'ñ
ô 
ð 	
ð „w�-ÒÐÝ˜$œ+¥sÑ+Ô+ð 	RÝ Ð!P½TÀ$Ä+Ñ=NÔ=NÐ!PÐ!PÑQÔQÐQØ˜DœKÔ(ˆÝ˜fÐ&:½DÀ¹L¼LÑIÔIˆÝ Ñ,Ô,Ð,Ø	Œ�OÒ	#Ð	#Ø”kˆð Ô Ð,°¸fÔ>OÐ1OÐ1Oð )�v”Ð(Ð(Ð(å$ VÑ,Ô,ð	
õ ˜$œ+¥sÑ+Ô+ð 	RÝ Ð!P½TÀ$Ä+Ñ=NÔ=NÐ!PÐ!PÑQÔQÐQØŒ{Ðr    c                 ót   — | j                              dd¦  «        }|duo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
   X   s5   € ð ŒI�MŠM˜& $Ñ'Ô'€EØ˜ÐÐ@¥¨Eµ5´<Ñ!@Ô!@Ð@r    c            	       ó¼   — e Zd ZdZdej        j        deddfd„Ze	 G d„ d¦  «        ¦   «         Z
	 dd	d
deez  dedz  defd„Zdeej        j        ef         fd„ZdS )r   z…
    Finds groups of connected ACC nodes that pass non-tensor data between each other.
    Such groups are called fusion groups.
    r   Ú	acc_nodesr   Nc                 ó¤   — || _         t          |j        j        ¦  «        | _        || _        d„ t          | j        ¦  «        D ¦   «         | _        d S )Nc                 ó   — i | ]\  }}||“Œ	S © r9   )Ú.0Úir"   s      r   ú
<dictcomp>z2FxNetAccFusionsFinder.__init__.<locals>.<dictcomp>o   s   € ÐHÐHÐH¡w q¨$˜4 ÐHÐHÐHr    )r   ÚlistÚgraphÚnodesr6   Ú	enumerateÚ
node_index)Úselfr   r6   s      r   Ú__init__zFxNetAccFusionsFinder.__init__k   sG   € ØˆŒÝ˜&œ,Ô,Ñ-Ô-ˆŒ
Ø"ˆŒØHÐHµ)¸D¼JÑ2GÔ2GÐHÑHÔHˆŒˆˆr    c                   ó\   — e Zd ZU eed<   eed<   eed<   eed<   dej        j        ddfd„Z	dS )	ú!FxNetAccFusionsFinder.FusionGroupÚtop_node_idxr?   ÚinputsÚnodes_need_processr"   r   Nc                 ó  ‡ — |‰ j         v rdS ‰ j                             |¦  «         ‰ j                              |¦  «         ‰ j                             |¦  «         ‰ j                             ˆ fd„|j        D ¦   «         ¦  «         dS )z5
            Add a node to fusion group.
            Nc                 óB   •— h | ]}|j         t          v ¯|‰j        v¯|’ŒS r9   )r%   r&   r?   )r:   ÚnrB   s     €r   ú	<setcomp>z=FxNetAccFusionsFinder.FusionGroup.add_node.<locals>.<setcomp>Š   s=   ø€ ð ð ð àØ”tÕ0Ð0Ð0°Q¸d¼jÐ5HÐ5Hð à5HÐ5HÐ5Hr    )r?   rH   ÚaddrG   ÚdiscardÚupdateÚall_input_nodes)rB   r"   s   ` r   Úadd_nodez*FxNetAccFusionsFinder.FusionGroup.add_node   s¥   ø€ ð �t”zÐ!Ð!Ø�àÔ#×'Ò'¨Ñ-Ô-Ð-ØŒJ�NŠN˜4Ñ Ô Ð ØŒK×Ò Ñ%Ô%Ð%ØŒK×Òðð ð ð à!Ô1ðñ ô ñô ð ð ð r    )
r   r   Ú__qualname__ÚintÚ__annotations__ÚNodeSetr2   ÚfxÚNoderQ   r9   r    r   ÚFusionGrouprE   q   sn   € € € € € € ð ÐÐÑð ˆˆ‰ð ˆˆ‰ð $Ð#Ð#Ñ#ð	 ¤¤ð 	°4ð 	ð 	ð 	ð 	ð 	ð 	r    rX   Úfusion_grouprE   rG   Úvisitedc                 ó  — |D ]„}|�||v rŒ	|                      |¦  «         |j        t          vrŒ-| j        |         |j        k     rŒD||j        v r dS |                      ||j        |¦  «        r|                     |¦  «          dS Œ…dS )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.
        NTF)	rM   r%   r&   rA   rF   r?   Úrecursive_add_noderP   rQ   )rB   rY   rG   rZ   Úargs        r   r\   z(FxNetAccFusionsFinder.recursive_add_node‘   sÁ   € ð ð 	ð 	ˆCàÐ"Ø˜'�>�>ØØ—’˜CÑ Ô Ð ð ŒvÕ.Ð.Ð.Øð Œ˜sÔ# lÔ&?Ò?Ð?Øð �lÔ(Ð(Ð(Ø�t�tð ×&Ò& |°SÔ5HÈ'ÑRÔRð Ø×%Ò% cÑ*Ô*Ð*Ø�t�tðð ˆur    c                 ó(  — i }t          | j        ¦  «        }|D �]÷}||v rŒ|j        t          vrŒd|j        v rŒ!|| j        vrŒ+|                      | j        |         |ht          |j        ¦  «        |h¬¦  «        }|j	        �rD|j	         
                    ¦   «         }|                      ||j        t          ¦   «         ¬¦  «         d|j        vra|j        D ]Y}|j        t          vrŒ||j        v rŒ|                     |¦  «         |                      ||j        t          ¦   «         ¬¦  «         ŒZ|j        D ]ˆ}|j        t          vrŒd|j        v rŒ||j        v rŒ%|                     |¦  «         t!          |j        | j        |         ¦  «        |_        |                      ||j        t          ¦   «         ¬¦  «         Œ‰|j	        �°Dt          |j        ¦  «        | j        k    s| xj        |j        z  c_        �Œã|j        D ]}|j        ||<   Œ�Œù|S )NÚtensor_meta)rF   r?   rG   rH   )rZ   )r=   r6   r%   r&   r/   rX   rA   ÚsetrP   rH   Úpopr\   rG   Úusersr?   rQ   ÚminrF   )rB   Úresultr6   r"   rY   Úuserr]   rK   s           r   Ú__call__zFxNetAccFusionsFinder.__call__·   sŽ  € Ø/1ˆÝ˜œÑ(Ô(ˆ	àð >	3ñ >	3ˆDØ�vˆ~ˆ~ØØŒwÕ/Ð/Ð/ØØ ¤	Ð)Ð)ØØ˜4œ>Ð)Ð)Øà>B×>NÒ>NØ!œ_¨TÔ2Ø�fÝ˜4Ô/Ñ0Ô0Ø$( 6ð	 ?Oñ ?ô ?ˆLð Ô1ñ (Ø#Ô6×:Ò:Ñ<Ô<�Ø×'Ò'Ø Ø Ô'Ý™EœEð (ñ ô ð ð !¨¬	Ð1Ð1Ø $¤
ð ð ˜Øœ7Õ*;Ð;Ð;Ø$Ø <Ô#5Ð5Ð5Ø$à$×-Ò-¨dÑ3Ô3Ð3Ø×/Ò/Ø(Ø(Ô/Ý$'¡E¤Eð 0ñ ô ð ð ð  Ô/ð ð �CØ”vÕ%6Ð6Ð6Ø Ø$¨¬Ð0Ð0Ø Ø˜lÔ0Ð0Ð0Ø à ×)Ò)¨#Ñ.Ô.Ð.Ý03Ø$Ô1°4´?À3Ô3Gñ1ô 1�LÔ-ð ×+Ò+Ø$Ø$Ô+Ý #¡¤ð ,ñ ô ð ð ðI Ô1ñ (õT ˜Ô*Ñ+Ô+¨t¬~Ò=Ð=Ø�” ,Ô"4Ñ4�”‘à%Ô+ð 3ð 3�AØ ,Ô 2�F˜1‘I�Iñ3ð ˆr    ©N)r   r   rR   Ú__doc__r2   rV   ÚGraphModulerU   rC   r   rX   ÚNodeListÚboolr\   ÚdictrW   rf   r9   r    r   r   r   d   s  € € € € € ðð ð
I˜uœxÔ3ð IÀð IÈDð Ið Ið Ið Ið ðð ð ð ð ñ ô ñ „YððF #'ð	$ð $à9ð$ð ˜(Ñ"ð$ð ˜4‘ð	$ð
 
ð$ð $ð $ð $ðLD˜$˜uœxœ}¨gÐ5Ô6ð Dð Dð Dð Dð Dð 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.                               | j        j        d¦  «        Št          j                             ¦   «         }| j        j        D ]}|j        D ]}‰|xx         dz  cc<   ŒŒt=          j        ¦   «         }| j        j        D ]#}‰|         dk    r|                      |¦  «         Œ$i ŠtC          |¦  «        dk    r¨| "                    ¦   «         }| #                    |ˆfd„¦  «        ‰|<   |j        D ]]}‰|xx         dz  cc<   ‰|         dk    r?|j$        dk    r|j%        |v r| &                    |¦  «         ŒH|                      |¦  «         Œ^tC          |¦  «        dk    °¨tC          |j        ¦  «        tC          | j        j        ¦  «        k     rtO          dˆfd„‰D ¦   «         › �¦  «        ‚| j        j(        |_(        || _        | S )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

    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                 ó   •— ‰|          S rg   r9   ©ÚxÚenvs    €r   ú<lambda>z legalize_graph.<locals>.<lambda>:  ó   ø€ °c¸!´f€ r    r   ú&Input graph has cycles, unable to add c                 ó,   •— g | ]}‰|         d k    ¯|‘ŒS ©r   r9   ©r:   r"   Úindegs     €r   ú
<listcomp>z"legalize_graph.<locals>.<listcomp>F  ó+   ø€ Ð5aÐ5aÐ5a¸tÐPUÐVZÔP[Ð_`ÒP`ÐP`°dÐP`ÐP`ÐP`r    ))ÚoperatorrM   Ú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_scalarrl   Úfromkeysr>   r?   rV   ÚGraphrb   ÚcollectionsÚdequeÚappendÚlenÚpopleftÚ	node_copyr%   r)   Ú
appendleftÚRuntimeErrorÚ_codegen)	rm   ÚPRIORITIZED_OPSÚ	new_graphr"   re   ÚqueueÚcurrs   rz   s	          @@r   r   r   þ   sz  øø€ õ2 	ŒÝŒÝŒÝÔÝÔÝŒÝŒÝŒÝŒÝŒÝŒÝŒÝŒ	ŒÔ*Ô2ÝŒ	ŒÔ3Ô;ÝŒ	ŒÔ$Ô(ÝŒ	ŒÔ$Ô,ÝŒ	ŒÔ%Ô-ð#€Oõ( �MŠM˜"œ(œ.¨!Ñ,Ô,€EÝ”—’Ñ Ô €Ià””ð ð ˆØ”Jð 	ð 	ˆDØ�$ˆKˆKŒK˜1ÑˆKˆK‰KˆKð	å.9Ô.?Ñ.AÔ.A€Eà””ð ð ˆØ�Œ;˜!ÒÐØ�LŠL˜ÑÔÐøØ.0€Cõ ˆe‰*Œ*�qŠ.ˆ.Ø�mŠm‰oŒoˆØ×&Ò& sÐ,<Ð,<Ð,<Ð,<Ñ=Ô=ˆˆC‰Ø”Ið 	'ð 	'ˆDØ�$ˆKˆKŒK˜1ÑˆKˆK‰KØ�TŒ{˜aÒÐØ”7˜oÒ-Ð-°$´+ÀÐ2PÐ2PØ×$Ò$ TÑ*Ô*Ð*Ð*à—L’L Ñ&Ô&Ð&øõ ˆe‰*Œ*�qŠ.ˆ.õ ˆ9Œ?ÑÔ�c "¤(¤.Ñ1Ô1Ò1Ð1ÝØcÐ5aÐ5aÐ5aÐ5aÀuÐ5aÑ5aÔ5aÐcÐcñ
ô 
ð 	
ð œÔ*€IÔØ€B„HØ€Ir    c                 ó^  ‡‡	— t                                | j        j        d¦  «        Š	t          j                             ¦   «         }d„ t          | j        j        ¦  «        D ¦   «         }| j        j        D ]}|j        D ]}‰	|xx         dz  cc<   ŒŒg }| j        j        D ]+}‰	|         dk    rt          j
        |||         |f¦  «         Œ,i Š|rxt          j        |¦  «        \  }}|                     |ˆfd„¦  «        ‰|<   |j        D ];}‰	|xx         dz  cc<   ‰	|         dk    rt          j
        |||         |f¦  «         Œ<|°xt          |j        ¦  «        t          | j        j        ¦  «        k    rt          dˆ	fd„‰	D ¦   «         › �¦  «        ‚| j        j        |_        || _        | S )aR  
    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   c                 ó   — i | ]\  }}||“Œ	S r9   r9   )r:   Úidxr"   s      r   r<   z+stable_topological_sort.<locals>.<dictcomp>f  s+   € ð ,ð ,ð ,Ù�c˜4ˆˆcð,ð ,ð ,r    ro   c                 ó   •— ‰|          S rg   r9   rq   s    €r   rt   z)stable_topological_sort.<locals>.<lambda>|  ru   r    rv   c                 ó,   •— g | ]}‰|         d k    ¯|‘ŒS rx   r9   ry   s     €r   r{   z+stable_topological_sort.<locals>.<listcomp>‡  r|   r    )rl   r’   r>   r?   r2   rV   r“   r@   rb   ÚheapqÚheappushÚheappopr™   r—   r'   rœ   )
rm   rž   Ú
node_to_idr"   re   Úready_queueÚ_r    rs   rz   s
           @@r   r   r   M  sû  øø€ õ* �MŠM˜"œ(œ.¨!Ñ,Ô,€EÝ”—’Ñ Ô €Ið,ð ,Ý#,¨R¬X¬^Ñ#<Ô#<ð,ñ ,ô ,€Jð
 ””ð ð ˆØ”Jð 	ð 	ˆDØ�$ˆKˆKŒK˜1ÑˆKˆK‰KˆKð	ð
 46€KØ””ð Bð BˆØ�Œ;˜!ÒÐÝŒN˜;¨°DÔ)9¸4Ð(@ÑAÔAÐAøà.0€Cð ð 	Få”˜{Ñ+Ô+‰ˆˆ3Ø×&Ò& sÐ,<Ð,<Ð,<Ð,<Ñ=Ô=ˆˆC‰ð ”Ið 	Fð 	FˆDØ�$ˆKˆKŒK˜1ÑˆKˆK‰KØ�TŒ{˜aÒÐÝ”˜{¨Z¸Ô-=¸tÐ,DÑEÔEÐEøð ð 	Fõ ˆ9Œ?ÑÔ�s 2¤8¤>Ñ2Ô2Ò2Ð2ÝØcÐ5aÐ5aÐ5aÐ5aÀuÐ5aÑ5aÔ5aÐcÐcñ
ô 
ð 	
ð œÔ*€IÔØ€B„HØ€Ir    )(r”   r¦   r}   Úcollections.abcr   Údataclassesr   Útypingr   r2   Útorch.fxÚtorch.fx._compatibilityr   Útorch.fx.noder   Ú__all__Útupler3   r=   ÚTensorsÚTensorOrTensorsrV   rW   rj   r`   rU   r   ÚNamesr&   r*   r   ÚnnÚModuler	   rk   r
   r   ri   r   r   r9   r    r   ú<module>r¹      s¾  ðØ Ð Ð Ð Ø €€€Ø €€€Ø #Ð #Ð #Ð #Ð #Ð #Ø !Ð !Ð !Ð !Ð !Ð !Ø Ð Ð Ð Ð Ð à €€€Ø €€€Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø -Ð -Ð -Ð -Ð -Ð -ðð ð €ð �”Ô
  U¤\Ô 2Ñ
2€Ø”, Ñ(€Ø�””Ô€Ø
ˆeŒhŒmÔ
€ØˆSŒ	€ØCÐCÐCÐ ð € eÐ,Ñ,Ô,ð	:˜˜d™
ð 	: sð 	:ð 	:ð 	:ñ -Ô,ð	:ð € eÐ,Ñ,Ô,ð(Ø˜˜UœXœ_Ð,Ô-ð(Ø5:´X´]ð(àð(ð (ð (ñ -Ô,ð(ðV € eÐ,Ñ,Ô,ðA ¤¤ð A°$ð Að Að Añ -Ô,ðAð € eÐ,Ñ,Ô,ðVð Vð Vð Vð Vñ Vô Vñ -Ô,ðVðr € eÐ,Ñ,Ô,ðK�u”xÔ+ð K°´Ô0Dð Kð Kð Kñ -Ô,ðKð\ € eÐ,Ñ,Ô,ð> ¤Ô 4ð >¸¼Ô9Mð >ð >ð >ñ -Ô,ð>ð >ð >r    