Ë
    f^(hÑ,  ã                   ó  — d dl Z d dlmZmZ d dlmZmZ d dlZd dl	m
Z
 d dlmZ d dlmZmZ ddlmZ g d	¢Z e
d
¬«      d„ «       Z e
d
¬«      d„ «       Z e
d
¬«      e G d„ d«      «       «       Z e
d
¬«      d
d
ej,                  j.                  fdej,                  j.                  dee   dededeej,                  j.                     deej,                  j.                  eej,                  j.                  eeef   f   f   fd„«       Zy)é    N)Ú	dataclassÚfield)ÚOptionalÚUnion)Úcompatibility)Úmap_arg)ÚHolderModuleÚlift_subgraph_as_moduleé   )ÚNodeList)Úgetattr_recursiveÚsetattr_recursiveÚ	ComponentÚsplit_by_tagsF)Úis_backward_compatiblec                 óf   — |j                  d«      D ]  }t        | |«      rt        | |«      } Œ y  | S )Nú.)ÚsplitÚhasattrÚgetattr)ÚobjÚnameÚlayers      úY/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/torch/fx/passes/split_utils.pyr   r      s9   € à—‘˜C“ò ˆÜ�3˜ÔÜ˜#˜uÓ%‰Cáð	ð
 €Jó    c                 ó¢   — d|vrt        | ||«       y |j                  d«      }t        t        | |d   «      dj	                  |dd  «      |«       y )Nr   r   r   )Úsetattrr   r   r   Újoin)r   ÚattrÚvaluer   s       r   r   r      sH   € à
�$�Ü��T˜5Õ!à—
‘
˜3“ˆÜœ' # u¨Q¡xÓ0°#·(±(¸5ÀÀ¸9Ó2EÀuÕMr   c                   ó€  — e Zd ZU dZej
                  j                  ed<   eed<   e	ed<    e
e¬«      Zeed<    e
e¬«      Zeed<    e
e¬«      Zeed<    e
e¬«      Zeej
                  j"                  ej
                  j"                  f   ed	<    e
e¬«      Zee	   ed
<   dZeej
                  j*                     ed<   y)r   zX
    A component serves as a container for a subgraph we want to create afterwards.
    ÚgraphÚorderr   )Údefault_factoryÚinput_placeholdersÚorig_inputsÚorig_outputsÚgetattr_mapsÚconstructor_argsNÚgm)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚtorchÚfxÚGraphÚ__annotations__ÚintÚstrr   Úlistr%   r&   r'   Údictr(   ÚNoder)   r*   r   ÚGraphModule© r   r   r   r   $   s­   … ñð �8‰8�>‰>ÓØƒJØ
ƒIñ  %°TÔ:Ð˜Ó:ñ ¨dÔ3€K�Ó3ñ ¨tÔ4€L�$Ó4ñ 8=ÈTÔ7R€L�$�u—x‘x—}‘} e§h¡h§m¡mÐ3Ñ4ÓRÙ"'¸Ô"=Ð�d˜3‘iÓ=Ø)-€Bˆ�—‘×%Ñ%Ñ&Ô-r   r   r*   ÚtagsÚreturn_fqn_mappingÚreturn_tupleÚGraphModuleClsÚreturnc           
      ó¦
  ‡‡‡‡— dt         j                  j                  j                  dt        fd„}i Ši Ši }g }i Št         j                  j                  «       }i }	d}
|D ]K  }t        t         j                  j                  «       t        |«      |› «      Š|j                  ‰«       ‰||<   ŒM | j                  j                  D �]”  }|j                  dk(  r|
�t        d«      ‚|}
Œ#|j                  dk(  rR|j                  |j                  |j                  ¬«      |	|<   t!        j                   |j"                  «      |	|   _        Œ„|j                  d	k(  rŒ”t%        |d
«      sJ d|j'                  «       › �«       ‚ ||j(                  «       ||j*                  «      z   D �cg c]  }|j                  dvr‰|   ‘Œ }}||j,                     Š‰‰|<   t/        d„ |D «       d¬«      }‰j0                  |k\  s$J d‰j                  › d‰j0                  › d|› �«       ‚ˆˆˆˆfd„}‰j                  j3                  ||«      }|j,                  |_        |‰|<   ‰‰|<   �Œ— |
€t        d«      ‚ ||
j(                  d   «      D ]A  }|j                  d	k(  r+|j5                  |j                  |j                  ¬«      |	|<   Œ=d‰|<   ŒC ‰D ]0  }|j                  dk7  sŒ‰|   j6                  j                  |«       Œ2 i }|D �]h  Št9        t;        ‰j<                  ‰j6                  «      «      }|r‰j                  j?                  |«       n.‰j                  j?                  t        |«      dk(  r|d   n|«       tA        | ‰j                  ‰j                  ¬«      \  ‰_!        }|jE                  |«       |jG                  ‰j                  t9        t;        |	j<                  ‰jH                  «      «      d¬«      }t        |«      dk(  r|s||	‰j6                  d   <   �ŒtK        ‰j6                  «      D ]4  \  }}t         j                  jM                  |«      |   j                  |	|<   Œ6 �Œk |j?                  tO        |
j(                  d   |	j<                  «      «       tQ        |D �ci c]  }|j                  |jB                  “Œ c}«      }| j                  jR                  |_)         ||
j(                  d   «      D ]=  }|j                  d	k(  sŒtU        ||j                  tW        | |jX                  «      «       Œ?  |||«      }|r||fS |S c c}w c c}w )a9  
    Splits a GraphModule using tags on its graph nodes. We honor the order of
    tags. For example, we have tags = ["a", "b", "c"], the function will create
    the initial submodules in the order of "a", "b", "c".

    To set a tag:
    gm.graph.nodes[idx].tag = "mytag"

    This will result in all nodes with the same tag being extracted and placed in their
    own submodule. For placeholder, output and get_attr node, the tag is ignored. placeholder
    and output nodes are created when needed while get_attr nodes get copied to submodules
    where they are used.

    Given the following module def:

    class SimpleModule(torch.nn.Module):
        def __init__(self) -> None:
            super().__init__()
            self.linear1 = torch.nn.Linear(...)
            self.linear2 = torch.nn.Linear(...)
            self.linear3 = torch.nn.Linear(...)

        def forward(self, in1, in2):
            r1 = self.linear1(in1)
            r2 = self.linear2(in2)
            r3 = torch.cat([r1, r2])
            return self.linear3(r3)

    Marking the node corresponding to in1 with the tag sc.REQUEST_ONLY.lower() results in the following split:

    ro:
    def forward(self, in1):
        self = self.root
        linear1 = self.linear1(in1)
        return linear1

    main:
    def forward(self, in2, linear1):
        self = self.root
        linear2 = self.linear2(in2)
        cat_1 = torch.cat([linear1, linear2])
        linear3 = self.linear3(cat_1)
        return linear3

    main:
    def forward(self, in1, in2):
        self = self.root
        ro_0 = self.ro_0(in1)
        main_1 = self.main_1(in2, ro_0)
        return main_1

    Returns:
        split_gm: torch fx graph after split
        orig_to_split_fqn_mapping: a map between the original fqn and the fqn
            after split for call_module and get_attr.
    Úxr>   c                 ó6   — g }t        | |j                  «       |S )zC
        Stores nodes in x to a list and returns the list.
        )r   Úappend)r@   Úrs     r   Úflattenzsplit_by_tags.<locals>.flatten   s   € ð ˆÜ��1—8‘8ÔØˆr   NÚoutputzMultiple output nodes in graph!Úplaceholder©Ú	type_exprÚget_attrÚtagzNode does not have tag: >   rI   rF   c              3   ó4   K  — | ]  }|j                   –— Œ y ­w)N)r#   )Ú.0Úcs     r   ú	<genexpr>z split_by_tags.<locals>.<genexpr>Ç   s   è ø€ Ò7˜a�!—'•'Ñ7ùs   ‚r   )Údefaultz
Component z8 order must be >= max of its upstream components, order=z	 and max=c                 óþ  •— | j                   dk(  rŒ| ‰j                  vro‰j                  j                  | j                  | j
                  ¬«      ‰j                  | <   t        j                  | j                  «      ‰j                  |    _        ‰j                  |    S | j                   dk7  r‰|    ‰k(  r‰|    S | ‰j                  vr�‰j                  j                  | «       ‰j                  j                  | j                  | j
                  ¬«      }t        j                  | j                  «      |_        ‰j                  j                  |«       d ‰| <   ‰j                  ‰j                  j                  | «         S )NrI   rG   rF   )Úopr(   r"   rI   ÚtargetÚtypeÚcopyÚmetar&   rB   rF   r   r%   Úindex)r@   rF   ÚcompÚnode_remappingÚnode_to_componentÚused_in_mains     €€€€r   Ú
remap_funcz!split_by_tags.<locals>.remap_funcÏ   sB  ø€ ð �t‰t�zÒ!Ø˜D×-Ñ-Ñ-Ø+/¯:©:×+>Ñ+>ØŸ™¨A¯F©Fð ,?ó ,�D×%Ñ% aÑ(ô 15·	±	¸!¿&¹&Ó0A�D×%Ñ% aÑ(Ô-Ø×(Ñ(¨Ñ+Ð+ð
 �t‰t�}Ò$Ð):¸1Ñ)=ÀÒ)EØ% aÑ(Ð(ð ˜×(Ñ(Ñ(Ø× Ñ ×'Ñ'¨Ô*Ø"Ÿj™j×4Ñ4°Q·V±VÀqÇvÁvÐ4ÓN�Ü#'§9¡9¨Q¯V©VÓ#4�Ô Ø×'Ñ'×.Ñ.¨{Ô;Ø"&�˜Q‘à×*Ñ*¨4×+;Ñ+;×+AÑ+AÀ!Ó+DÑEÐEr   zGraph had no output node!r   )ÚsubgraphÚ	comp_name)ÚargsÚkwargs)-r/   r0   ÚnodeÚArgumentr   r1   r   ÚlenrB   r"   ÚnodesrQ   ÚRuntimeErrorrF   r   rS   rT   rU   r   Úformat_noder^   r_   rJ   Úmaxr#   Ú	node_copyrI   r'   ÚtupleÚmapÚ__getitem__rE   r
   r*   ÚupdateÚcall_moduler&   Ú	enumerateÚProxyr   r	   Ú_codegenr   r   rR   )r*   r:   r;   r<   r=   rD   Útag_to_componentÚall_componentsÚmain_gÚmain_remappingÚoutput_noderJ   r`   r@   Úupstream_componentsÚmxr[   ÚnÚorig_to_split_fqn_mappingÚoutsÚcomp_orig_to_split_fqn_mappingÚ	main_nodeÚiÚorW   Ú	main_rootÚ	result_gmrX   rY   rZ   s                           `  @@@r   r   r   >   s0  û€ ðB”5—8‘8—=‘=×)Ñ)ð ¬hó ð :<€Nð 9;Ðð .0Ðð ')€Nð /1€Lô �X‰X�^‰^Ó€Fð :<€Nð ,0€Kð ò %ˆÜœŸ™Ÿ™Ó)¬3¨~Ó+>À3À%ÓIˆØ×Ñ˜dÔ#Ø $Ð˜Òð%ð —‘—‘ó G$ˆØ�7‰7�hÒØÐ&Ü"Ð#DÓEÐEØˆKØð �7‰7�mÒ#Ø#)×#5Ñ#5°d·i±iÈ4Ï9É9Ð#5Ó#UˆN˜4Ñ Ü(,¯	©	°$·)±)Ó(<ˆN˜4Ñ Ô%Øð �7‰7�jÒ Øô �t˜UÔ#ÐTÐ'?À×@PÑ@PÓ@RÐ?SÐ%TÓTÐ#ñ ˜TŸY™YÓ'©'°$·+±+Ó*>Ñ>ö
àØ�t‰tÐ6Ñ6ð ˜aÓ ð
Ðð 
ð   §¡Ñ)ˆØ"&Ð˜$Ñô Ñ7Ð#6Ô7ÀÔCˆð �J‰J˜"Òð	uà˜Ÿ	™	�{Ð"ZÐ[_×[eÑ[eÐZfÐfoÐprÐosÐtó	uØ÷	Fð8 �J‰J× Ñ   zÓ2ˆØ—‘ˆŒØ ˆ�tÑØ#Ð˜!ÓðOG$ðR ÐÜÐ6Ó7Ð7á�[×%Ñ% aÑ(Ó)ò 	#ˆØ�4‰4�:Òð !'§¡°·±À!Ç&Á& Ó IˆN˜1Òð #ˆL˜ŠOð	#ð ò 8ˆØ�4‰4�=Ó Ø˜aÑ ×-Ñ-×4Ñ4°QÕ7ð8ð
 13ÐØó FˆÜ”S˜×3Ñ3°T×5FÑ5FÓGÓHˆáØ�J‰J×Ñ˜dÕ#ð �J‰J×Ñ¬¨T«°aª˜d 1šg¸TÔBä2IØ˜Ÿ™¨t¯y©yô3
Ñ/ˆŒÐ/ð 	"×(Ñ(Ð)GÔHð ×&Ñ&Ø�I‰IÜ”s˜>×5Ñ5°t×7GÑ7GÓHÓIØð 'ó 
ˆ	ô ˆt‹9˜Š>¡,Ø3<ˆN˜4×,Ñ,¨QÑ/Ó0ä! $×"3Ñ"3Ó4ò F‘��1ä$)§H¡H§N¡N°9Ó$=¸aÑ$@×$EÑ$E�˜qÒ!òFð7Fð> ‡M�M”'˜+×*Ñ*¨1Ñ-¨~×/IÑ/IÓJÔKÜ¸^ÖL°T˜dŸi™i¨¯©Ñ0ÒLÓM€IØ—h‘h×'Ñ'€F„Oñ �[×%Ñ% aÑ(Ó)ò HˆØ�4‰4�:ÓÜ�I˜qŸv™vÔ'8¸¸Q¿X¹XÓ'FÕGðHñ ˜y¨&Ó1€IÙØÐ3Ð3Ð3àÐùòm
ùòR Ms   Æ$U	Ò#U)rT   Údataclassesr   r   Útypingr   r   Útorch.fxr/   Útorch.fx._compatibilityr   Útorch.fx.graphr   Útorch.fx.passes.utilsr	   r
   Útools_commonr   Ú__all__r   r   r   r0   r8   r5   r4   ÚboolrS   rh   r6   r   r9   r   r   ú<module>r‰      s:  ðã ß (ß "ã Ý 1Ý "ß Gå "ò S€ñ  eÔ,ñó -ðñ  eÔ,ñNó -ðNñ  eÔ,Ø
÷.ð .ó ó -ð.ñ0  eÔ,ð  %ØØ16·±×1EÑ1EñtØ�‰×Ñðtà
ˆs‰)ðtð ðtð ð	tð
 ˜Ÿ™×-Ñ-Ñ.ðtð ˆ5�8‰8×Ñ  u§x¡x×';Ñ';¸TÀ#ÀsÀ(¹^Ð'KÑ!LÐLÑMòtó -ñtr   