
    IЦi+                        S SK r S SKJrJr  S SKJrJrJrJrJ	r	J
r
  S SKrS SKJr  S SKJr  S SKJrJr  SSKJr  / S	Qr\" S
S9S 5       r\" S
S9S 5       r\" S
S9\ " S S5      5       5       r\" S
S9S
S
\R4                  R6                  4S\R4                  R6                  S\\   S\S\S\	\R4                  R6                     S\
\R4                  R6                  \\R4                  R6                  \\\4   4   4   4S jj5       rg)    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                 p    UR                  S5       H   n[        X5      (       a  [        X5      n M     g    U $ )N.)splithasattrgetattr)objnamelayers      Z/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/torch/fx/passes/split_utils.pyr   r      s3    C3#%C	 !
 J    c                     SU;  a  [        XU5        g UR                  S5      n[        [        XS   5      SR	                  USS  5      U5        g )Nr   r   r   )setattrr   r   r   join)r   attrvaluer   s       r   r   r      sD    
$5!

3'#Qx0#((592EuMr   c                   j   \ rS rSr% Sr\R                  R                  \S'   \	\S'   \
\S'   \" \S9r\\S'   \" \S9r\\S'   \" \S9r\\S	'   \" \S9r\\R                  R(                  \R                  R(                  4   \S
'   \" \S9r\\
   \S'   Sr\\R                  R0                     \S'   Srg)r   $   zP
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____firstlineno____doc__torchfxGraph__annotations__intstrr   listr*   r   r+   r,   dictr-   r   Noder.   r/   r   GraphModule__static_attributes__r0   r   r   r   r   $   s     88>>J
I  %T:: d3K3 t4L$4 8=T7RL$uxx}}ehhmm34R"'"=d3i=)-B%%&-r   r   r/   tagsreturn_fqn_mappingreturn_tupleGraphModuleClsreturnc           
      
  ^^^^ S[         R                  R                  R                  S[        4S jn0 m0 m0 n/ n0 m[         R                  R                  5       n0 n	Sn
U HK  n[        [         R                  R                  5       [        U5      U 5      mUR                  T5        TXk'   MM     U R                  R                   GHj  nUR                  S:X  a  U
b  [        S5      eUn
M&  UR                  S:X  aO  UR                  UR                  UR                  S9X'   [         R                   " UR"                  5      X   l        M  UR                  S	:X  a  M  [%        US
5      (       d   eU" UR&                  5      U" UR(                  5      -    Vs/ s H  nUR                  S;  d  M  TU   PM     nnXlR*                     mTTU'   [-        S U 5       SS9nTR.                  U:  d   eUUUU4S jnTR                  R1                  UU5      nUR*                  Ul        UTU'   TTU'   GMm     U
c  [        S5      eU" U
R&                  S   5       H@  nUR                  S	:X  a(  UR3                  UR                  UR                  S9X'   M;  STU'   MB     T H3  nUR                  S:w  d  M  TU   R4                  R                  U5        M5     0 nU GHr  m[7        [9        TR:                  TR4                  5      5      nU(       a  TR                  R=                  U5        O/TR                  R=                  [        U5      S:X  a  US   OU5        [?        U TR                  TR                  S9u  Tl         nURC                  U5        URE                  TR                  [7        [9        U	R:                  TRF                  5      5      SS9n[        U5      S:X  a  U(       d  UU	TR4                  S   '   GM$  [I        TR4                  5       H5  u  nn[         R                  RK                  U5      U   R                  U	U'   M7     GMu     UR=                  [M        U
R&                  S   U	R:                  5      5        [O        U Vs0 s H  nUR                  UR@                  _M     sn5      nU R                  RP                  Ul(        U" U
R&                  S   5       H?  nUR                  S	:X  d  M  [S        UUR                  [U        XRV                  5      5        MA     U" UU5      nU(       a  UU4$ U$ s  snf s  snf )a  
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.
xrE   c                 4    / n[        XR                  5        U$ )z3
Stores nodes in x to a list and returns the list.
)r   append)rG   rs     r   flattensplit_by_tags.<locals>.flatten   s     88r   NoutputzMultiple output nodes in graph!placeholder	type_exprget_attrtag>   rQ   rN   c              3   8   #    U  H  oR                   v   M     g 7f)N)r(   ).0cs     r   	<genexpr> split_by_tags.<locals>.<genexpr>   s     7#6a''#6s   r   )defaultc                   > U R                   S:X  aZ  U TR                  ;  a;  TR                  R                  U R                  U R
                  S9TR                  U '   TR                  U    $ U R                   S:w  a  TU    T:X  a  TU    $ U TR                  ;  a  TR                  R                  U 5        TR                  R                  U R                  U R
                  S9n[        R                  " U R                  5      Ul        TR                  R                  U5        S TU '   TR                  TR                  R                  U 5         $ )NrQ   rO   rN   )opr-   r'   rQ   targettyper+   rI   rN   r   copymetar*   index)rG   rN   compnode_remappingnode_to_componentused_in_mains     r   
remap_func!split_by_tags.<locals>.remap_func   s"    ttz!D---+/::+>+>AFF ,? ,D%%a( ((++
 tt}$):1)=)E%a(( (((  ''*"jj44QVVqvv4N#'99QVV#4 ''..{;"&Q**4+;+;+A+A!+DEEr   zGraph had no output node!r   )subgraph	comp_name)argskwargs),r6   r7   nodeArgumentr   r8   r   lenrI   r'   nodesrZ   RuntimeErrorrN   r   r\   r]   r^   r   rh   ri   rR   maxr(   	node_copyrQ   r,   tuplemap__getitem__rM   r   r/   updatecall_moduler+   	enumerateProxyr   r   _codegenr!   r   r[   )r/   rA   rB   rC   rD   rK   tag_to_componentall_componentsmain_gmain_remappingoutput_noderR   rj   rG   upstream_componentsmxrd   norig_to_split_fqn_mappingoutscomp_orig_to_split_fqn_mapping	main_nodeior`   	main_root	result_gmra   rb   rc   s                           `  @@@r   r   r   >   s   B588==)) h  :<N 9; .0 ')N /1L XX^^F :<N ,0K )3~+>3%Id# $  77h&"#DEEK 77m##)#5#5dii499#5#UN (,		$))(<N % 77j  tU#### TYY''$++*>>
>tt66 !a > 	 
  )"&$ 7#67C zzR	F 	F6 JJ  z2 t#!I L 677[%%a()44: !'!&& IN #LO * 44= a --44Q7 
 13S33T5F5FGHJJd# JJTad1gTB2Ityy3
// 	"(()GH &&IIs>55t7G7GHI ' 
	 t9>,3<N4,,Q/0!$"3"341$)HHNN9$=a$@$E$Eq! 57 > MM'+**1-~/I/IJK^L^Tdii0^LMIhh''FO [%%a()44:Iqvv'8XX'FG * y&1I333g
L Ms   T=0	T=!U)r]   dataclassesr   r   typingr   r   r   r   r	   r
   torch.fxr6   torch.fx._compatibilityr   torch.fx.graphr   torch.fx.passes.utilsr   r   tools_commonr   __all__r   r   r   r7   r?   r;   boolr   r0   r   r   <module>r      s=    ( ; ;  1 " G " S e, - e,N -N e,
. .  -.0 e,  %161E1Eqq
s)q q 	q
 --.q 588uxx';';T#s(^'K!LLMq -qr   