
    קg+                        d dl Z d dlmZmZ d dlmZmZmZmZ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dS )    N)	dataclassfield)DictListOptionalTupleTypeUnion)compatibility)map_arg)HolderModulelift_subgraph_as_module   )NodeList)getattr_recursivesetattr_recursive	Componentsplit_by_tagsF)is_backward_compatiblec                 |    |                     d          D ]%}t          | |          rt          | |          } # d S | S )N.)splithasattrgetattr)objnamelayers      W/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/fx/passes/split_utils.pyr   r      sJ    C  3 	#u%%CC44J    c                     d|vrt          | ||           d S |                    d          }t          t          | |d                   d                    |dd                    |           d S )Nr   r   r   )setattrr   r   r   join)r   attrvaluer   s       r   r   r      sl    
$T5!!!!!

3'#uQx00#((592E2EuMMMMMr   c                   b   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<   dS )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*   r+   dictr,   r   Noder-   r.   r   GraphModule r   r   r   r   #   s          8>JJJ
III  %uT:::::: d333K333 t444L$444 8=uT7R7R7RL$ux}ehm34RRR"'%"="="=d3i===)-B%&-----r   r   r.   tagsreturn_fqn_mappingreturn_tupleGraphModuleClsreturnc           
      L	   dt           j        j        j        dt          fd}i i i }g }i t           j                                        }i }	d}
|D ]W}t          t           j                                        t          |          |           |                               ||<   X| j	        j
        D ]4}|j        dk    r|
t          d          |}
"|j        dk    rI|                    |j        |j                  |	|<   t!          j        |j                  |	|         _        v|j        d	k    rt%          |d
          sJ fd ||j                   ||j                  z   D             }||j                 |<   t-          d |D             d          }j        |k    sJ fd}j	                            ||          }|j        |_        ||<   |<   6|
t          d           ||
j        d                   D ]7}|j        d	k    r%|                    |j        |j                  |	|<   2d|<   8D ]-}|j        dk    r |         j                            |           .i }|D ]et7          t9          j        j                            }|rj	                            |           n5j	                            t          |          dk    r|d         n|           t?          | j	        j                  \  _         }|!                    |           |"                    j        t7          t9          |	j        j#                            d          }t          |          dk    r|s||	j        d         <   tI          j                  D ]2\  }}t           j        %                    |          |         j        |	|<   3g|                    tM          |
j        d         |	j                             tO          d |D                       }| j	        j(        |_(         ||
j        d                   D ]6}|j        d	k    r)tS          ||j        tU          | |j+                             7 |||          }|r||fS |S )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.
    xrB   c                 4    g }t          | |j                   |S )zC
        Stores nodes in x to a list and returns the list.
        )r   append)rD   rs     r   flattenzsplit_by_tags.<locals>.flatten~   s      18r   NoutputzMultiple output nodes in graph!placeholder	type_exprget_attrtagc                 2    g | ]}|j         d v|         S )>   rM   rJ   )op).0rD   node_to_components     r   
<listcomp>z!split_by_tags.<locals>.<listcomp>   s6     
 
 
t666 a 666r   c              3   $   K   | ]}|j         V  d S )N)r'   )rQ   cs     r   	<genexpr>z split_by_tags.<locals>.<genexpr>   s$      77a!'777777r   r   )defaultc                 6   | j         dk    rD| j        vr.j                            | j        | j                  j        | <   j        |          S | j         dk    r|          k    r|          S | j        vr}j                            |            j                            | j	        | j                  }t          j
        | j                  |_        j                            |           d | <   j        j                            |                    S )NrM   rK   rJ   )rP   r,   r&   rM   targettyper*   rF   rJ   r   copymetar)   index)rD   rJ   compnode_remappingrR   used_in_mains     r   
remap_funcz!split_by_tags.<locals>.remap_func   s    tz!!D---+/:+>+>AF ,? , ,D%a( (++
 t}$$):1)=)E)E%a(( ((( ''***"j44QVqv4NN#'9QV#4#4 '..{;;;"&Q*4+;+A+A!+D+DEEr   zGraph had no output node!r   )subgraph	comp_name)argskwargsc                 (    i | ]}|j         |j        S r=   )r   r.   )rQ   r^   s     r   
<dictcomp>z!split_by_tags.<locals>.<dictcomp>"  s    LLLTdiLLLr   ),r3   r4   nodeArgumentr   r5   r   lenrF   r&   nodesrP   RuntimeErrorrJ   r   rZ   r[   r\   r   rd   re   rN   maxr'   	node_copyrM   r+   tuplemap__getitem__rI   r   r.   updatecall_moduler*   	enumerateProxyr   r   _codegenr!   r   rY   )r.   r>   r?   r@   rA   rH   tag_to_componentall_componentsmain_gmain_remappingoutput_noderN   rh   upstream_componentsmxra   nrD   orig_to_split_fqn_mappingoutscomp_orig_to_split_fqn_mapping	main_nodeio	main_root	result_gmr^   r_   rR   r`   s                             @@@@r   r   r   =   s}   B58=) h     :<N 9; .0 ')N /1L X^^F :<N ,0K  % %))3~+>+>3IId### $  D$ D$7h&"#DEEEK 7m###)#5#5di49#5#U#UN4 (,	$)(<(<N4 % 7j   tU#####
 
 
 
WTY''''$+*>*>>
 
 
  )"&$ 77#6777CCC zR	F 	F 	F 	F 	F 	F 	F 	F6 J  z22 t#!6777W[%a()) 	# 	#4: !'!& I IN1 #LOO  8 84=  a -44Q777 13 F FS3T5FGGHH 	CJd#### JTad1ggTBBB2Ity3
 3
 3
// 	"(()GHHH &&Is>5t7GHHII ' 
 
	 t99>>,>3<N4,Q/00!$"344 F F1$)HNN9$=$=a$@$Eq!!F MM'+*1-~/IJJKKKLL^LLLMMIh'FO W[%a()) H H4:Iqv'8QX'F'FGGGy&11I 4333r   )r[   dataclassesr   r   typingr   r   r   r   r	   r
   torch.fxr3   torch.fx._compatibilityr   torch.fx.graphr   torch.fx.passes.utilsr   r   tools_commonr   __all__r   r   r   r4   r<   r8   boolr   r=   r   r   <module>r      s$    ( ( ( ( ( ( ( ( ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;  1 1 1 1 1 1 " " " " " " G G G G G G G G " " " " " "
R
R
R e,,,  -, e,,,N N -,N e,,,
. . . . . . .  -,.0 e,,,  %161Eq qq
s)q q 	q
 -.q 58ux';T#s(^'K!LLMq q q -,q q qr   