
    [Th,                         S SK r S SKJrJr  S SK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
\R,                  R.                  4S\R,                  R.                  S\\   S\S\S\\R,                  R.                     S\\R,                  R.                  \\R,                  R.                  \\\4   4   4   4S jj5       rg)    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                 p    UR                  S5       H   n[        X5      (       a  [        X5      n M     g    U $ )N.)splithasattrgetattr)objnamelayers      S/var/www/auris/envauris/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                  R,                     \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(   dictr)   Noder*   r+   r   GraphModule__static_attributes__r,   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H  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   SUR'                  5        35       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R0                  U:  d$   STR                   STR0                   SU 35       eUUUU4S jnTR                  R3                  UU5      nUR,                  Ul        UTU'   TTU'   GM     U
c  [        S5      eU" U
R(                  S   5       H@  nUR                  S	:X  a(  UR5                  UR                  UR                  S9X'   M;  STU'   MB     T H3  nUR                  S:w  d  M  TU   R6                  R                  U5        M5     0 nU GHr  m[9        [;        TR<                  TR6                  5      5      nU(       a  TR                  R?                  U5        O/TR                  R?                  [        U5      S:X  a  US   OU5        [A        U TR                  TR                  S9u  Tl!        nURE                  U5        URG                  TR                  [9        [;        U	R<                  TRH                  5      5      SS9n[        U5      S:X  a  U(       d  UU	TR6                  S   '   GM$  [K        TR6                  5       H5  u  nn[         R                  RM                  U5      U   R                  U	U'   M7     GMu     UR?                  [O        U
R(                  S   U	R<                  5      5        [Q        U Vs0 s H  nUR                  URB                  _M     sn5      nU R                  RR                  Ul)        U" U
R(                  S   5       H?  nUR                  S	:X  d  M  [U        UUR                  [W        XRX                  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.
xrA   c                 4    / n[        XR                  5        U$ )z3
Stores nodes in x to a list and returns the list.
)r   append)rC   rs     r   flattensplit_by_tags.<locals>.flatten   s     88r   NoutputzMultiple output nodes in graph!placeholder	type_exprget_attrtagzNode does not have tag: >   rM   rJ   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   )defaultz
Component z8 order must be >= max of its upstream components, order=z	 and max=c                   > U R                   S:X  a  U TR                  ;  am  TR                  R                  U R                  U R
                  S9TR                  U '   [        R                  " U R                  5      TR                  U    l        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         $ )NrM   rK   rJ   )opr)   r#   rM   targettypecopymetar'   rE   rJ   r   r&   index)rC   rJ   compnode_remappingnode_to_componentused_in_mains     r   
remap_func!split_by_tags.<locals>.remap_func   sB    ttz!D---+/::+>+>AFF ,? ,D%%a( 15		!&&0AD%%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)-r2   r3   nodeArgumentr   r4   r   lenrE   r#   nodesrV   RuntimeErrorrJ   r   rX   rY   rZ   r   format_noderd   re   rN   maxr$   	node_copyrM   r(   tuplemap__getitem__rI   r
   r+   updatecall_moduler'   	enumerateProxyr   r	   _codegenr   r   rW   )r+   r=   r>   r?   r@   rG   tag_to_componentall_componentsmain_gmain_remappingoutput_noderN   rf   rC   upstream_componentsmxr`   norig_to_split_fqn_mappingoutscomp_orig_to_split_fqn_mapping	main_nodeior\   	main_root	result_gmr]   r^   r_   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##T'?@P@P@R?S%TT# TYY''$++*>>
>tt66 !a > 	 
  )"&$ 7#67C JJ"	u		{"Z[_[e[eZffoprost	u	F 	F8 JJ  z2 t#!O R 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m
R Ms   .U5	U5!U:)rY   dataclassesr   r   typingr   r   torch.fxr2   torch.fx._compatibilityr   torch.fx.graphr   torch.fx.passes.utilsr	   r
   tools_commonr   __all__r   r   r   r3   r;   r8   r7   boolrX   rn   r9   r   r,   r   r   <module>r      s:    ( "  1 " G " S e, - e,N -N e,
. .  -.0 e,  %161E1Ett
s)t t 	t
 --.t 588uxx';';T#s(^'K!LLMt -tr   