
    קg_                        d dl Z d dlmZmZmZmZmZ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 d dlmZ d dlmZ dd	gZ e
j        e          xZZ ed
           G d d                      Z ed
          	 	 	 ddedej        j        deegef         deeeef                  dee         dee         fd            ZdS )    N)AnyCallableDictListOptionalSet)OrderedDict)compatibility)GraphModule)Node)lazy_format_graph_code	Partitionsplit_moduleT)is_backward_compatiblec                   &    e Zd ZdefdZdefdZdS )r   namec                     || _         d| | _        g | _        i | _        i | _        i | _        i | _        t          j        j	        
                                | _	        i | _        i | _        d S )Nsubmod_)r   submod_name
node_namesinputsoutputsdependencies
dependentstorchfxgraphGraphenvironmenttargets)selfr   s     X/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/fx/passes/split_module.py__init__zPartition.__init__   sg    	+T++%'')(*-/+-+08>+?+?+A+A
-/')    returnc                 f    d| j          d| j         d| j         d| j         d| j         d| j         S )Nzname: z
,
 nodes: z,
 inputs: z,
 outputs: z,
 partitions depended on: z,
 partition dependents: )r   r   r   r   r   r   )r!   s    r"   __repr__zPartition.__repr__   sl    8TY 8 88 88 8 8 8 )-(9	8 8
 '+o8 8	
r$   N)__name__
__module____qualname__strr#   r'    r$   r"   r   r      sJ        
*S 
* 
* 
* 
*
# 
 
 
 
 
 
r$   Fmroot_msplit_callbackqualname_mapkeep_original_orderkeep_original_node_namec                 $   789:;<=>?@A t                               dt          d d                     dt          dt          t
          t          f         dt          t
          t          j        j        j	        f         f8 fd}d	d
l
}i ?i <i Adt          dt          t                   f?Afd@?fd}t          j        j        t          j        j        t          j        j        g}	t#                      }
t#                      }i }d
}t%                      } j        j        D ]a::j                            d          x}Nt/          |t          j                  r4t/          |j        j        x}|j                  r|Avr:A|j        j        <   :j        dv rw |:           :j        dk    r:j        |	v rz:j        t          j        j        k    r\t=          :j                  dk    sJ t/          :j        d	         t@                    sJ :}t%           :          h          |
|<   n:j        t          j        j        k    rWtC          d :j        D                       sJ |"                    :           t%           :          h          |:<   d
|:<   n:j        t          j        j        k    ryt=          :j                  dk    sJ |:j        d	                  "                     :                     |#                    :j        d	                    :|:j        d	         <   |$|
|         "                     :                     |D ]&}||         "                     :                     'ctC          d |$                                D                       s
J d            d |%                                D             }d |
%                                D             }
tL          '                    tP          j)                  r6tL                              d|           tL                              d|
           tA          |          ptA          |
          }d} j        j        D ]ߊ::<:j*        <   :j        dv r:j        dk    r4t          j        j        +                    :j        d	         @fd           U|r# :          }||k    sJ d| d|             |}:j        |	vr\t          j        j        +                    :j        :@fd            t          j        j        +                    :j,        :@fd!           t[          ?.                                          }g }?%                                D ].\  }>t=          >j/                  s|0                    |           /g }|r}|1                                }|0                    |           ?|         j2        D ]D}?|         j/        1                    |           ?|         j/        s|0                    |           E|}t=          |          t=          ?          k    rtg          d"          ||
fD ]}|%                                D ]\  :}t=          |          d	k    sJ :?t          |d	                            j4        :<   |dd
         D ]}?t          |                   >>j        5                    :j        :j        tm          d# :j        D                       i :j7        $          }:j        8                                |_        |>j4        :<   Ҍ|D ]o}?|         >>j9        D ]]}>j        :                    |<|         j7        %          } <|         j        8                                | _        | >j4        <|         <   ^p j        j        D ]:tw          :d&          r?:j<                 >>j4        9t          j        j        +                    :j        9fd'          }!t          j        j        +                    :j,        9fd(          }":j        d)vr:j        }#n:j        =                    d*          }$ }%|$D ]:}&tw          |%|&          st}          d+:j         d,          t          |%|&          }%;d-@                    |$          }#|%>jA        |#<   |>jB         d*|# }':j        ||'<   t/          |!tl                    sJ t/          |"t                    sJ r:j*        nd
}(>j        5                    :j        |#|!|":j7        |(.          }:j        8                                |_        |>j4        :<   |fD ]}t          |          D ]:|:         }t=          |          d	k    sJ |d
d         D ]}?t          |                   >|:         })|)
J d/            >j        5                    |)j        |)j        >j4        :         fi |)j7        $          }|)j        8                                |_        i ;i 7t          j        j        E                                8i }*|s  j        j        D ]: |:7|*          \  7}*n j        j        D ]::;:j*        <   |s|n|}+t%                      },d0  j        j        D             =|+D ]}?|         >tm          <>fd1>jF        D                       }-t=          |-          }.|.dk    r!>j        G                    |-d	                    n |.dk    r>j        G                    |-           |rr;=fd2>j9        D             }/=D ],::|,v r |:7|*          \  7}0|,"                    :           -|/D ],::|,v r |:7|*          \  7}*|,"                    :           -t          j        j        	                    >jA        >j                  |*>jB        <   8H                    >jB        tm          7fd3>j9        D                                 }1t=          >jF                  }2|2dk    rPt          j        jI        J                    |1          }3t          >jF                  D ]\  }4}5|3|4         j        7|5<   |2dk    r$|17t          t          >jF                            <    j        j        D ]S::j        dk    rF8G                    t          j        j        +                    :j        d	         7fd4                     Tt          j        j        	                    |*8          }6t                               dt          d5|6d                     |6S )6a  
    Creates subgraphs out of main graph

    Args:
        m (GraphModule): Graph module to split
        root_m (torch.nn.Module): root nn module. Not currently used. Included
            because the root nn module is usually transformed via
            torch.fx._symbolic_trace.symbolic_trace (see example below)
        split_callback (Callable[[Node], int]): Callable function
            that maps a given Node instance to a numeric partition identifier.
            split_module will use this function as the policy for which operations
            appear in which partitions in the output Module.
        qualname_map: Optional[Dict[str, str]]: optional output parameter that returns a
            mapping from new target names in the module after split to old target
            names in the original module.
        keep_original_order: Optional[bool]: keep the original order of the GraphModule
            or use the Topological order of the new constructed GraphModule


    Returns:
        GraphModule: the module after split.

    Example:

        This is a sample setup:

            import torch
            from torch.fx.symbolic_trace import symbolic_trace
            from torch.fx.graph_module import GraphModule
            from torch.fx.node import Node
            from torch.fx.passes.split_module import split_module

            class MyModule(torch.nn.Module):
                def __init__(self) -> None:
                    super().__init__()
                    self.param = torch.nn.Parameter(torch.rand(3, 4))
                    self.linear = torch.nn.Linear(4, 5)

                def forward(self, x, y):
                    z = self.linear(x + self.param).clamp(min=0.0, max=1.0)
                    w = self.linear(y).clamp(min=0.0, max=1.0)
                    return z + w

            # symbolically trace model
            my_module = MyModule()
            my_module_traced = symbolic_trace(my_module)

            # random mod partitioning
            partition_counter = 0
            NPARTITIONS = 3

            def mod_partition(node: Node):
                global partition_counter
                partition = partition_counter % NPARTITIONS
                partition_counter = (partition_counter + 1) % NPARTITIONS
                return partition

            # split module in module with submodules
            module_with_submodules = split_module(
                my_module_traced, my_module, mod_partition
            )

        Output looks like this. Original graph is broken into partitions

            > print(module_with_submodules)
            GraphModule(
                (submod_0): GraphModule(
                    (linear): Linear(in_features=4, out_features=5, bias=True)
                )
                (submod_1): GraphModule(
                    (linear): Linear(in_features=4, out_features=5, bias=True)
                )
                (submod_2): GraphModule()
            )

            def forward(self, x, y):
                param = self.param
                submod_0 = self.submod_0(x, param, y);  x = param = y = None
                getitem = submod_0[0]
                getitem_1 = submod_0[1];  submod_0 = None
                submod_1 = self.submod_1(getitem, getitem_1);  getitem = getitem_1 = None
                getitem_2 = submod_1[0]
                getitem_3 = submod_1[1];  submod_1 = None
                submod_2 = self.submod_2(getitem_2, getitem_3);  getitem_2 = getitem_3 = None
                return submod_2

        Output of split module is the same as output of input traced module.
        This is an example within a test setting:

            > orig_out = my_module_traced(x, y)
            > submodules_out = module_with_submodules(x, y)
            > self.assertEqual(orig_out, submodules_out)
            True
    z%szpre split_moduleT)colorednodebase_mod_envbase_mod_attrsc                 0   | j         dk    rt          | j                  dk    r| j        d         nt          j        j        }rD|t          j        j        u rdn|f}                    d| j        || j                  || j        <   n*	                    | j
        | j        |          || j        <   | j                                        || j                 _        n| j         dk    r                    | j
                  || j        <   | j                                        || j                 _        	}| j
                            d          D ]:}t          ||          st!          d| j
         d	          t#          ||          };||| j
        <   ||fS )
Nplaceholderr   r,   )args	type_expr)r;   default_valueget_attr.zNode target  not found!)oplenr:   inspect	Signatureemptycreate_noder   typer9   targetmetacopyr=   splithasattrAttributeErrorgetattr)
r5   r6   r7   r<   r:   attr_valatombase_mod_graphr2   r-   s
          r"   construct_graphz%split_module.<locals>.construct_graph   s   
 7m## #DI 2 2	!8I8O  ' *g.?.EEErrMK[*8*D*D]TXT]dhtxt}*D*~*~TY''*8*D*DK49M +E + +TY' ,09>>+;+;L#((W
""&4&=&=dk&J&JL#+/9>>+;+;L#(H))#.. 3 3x.. R()P)P)P)PQQQ"8T22*2N4;'^++r$   r   Ndef_nodeuse_nodec                 t   ddl m} t          | dd           }t          |dd           }t                              d| j        |||j        nd|           ||k    rW|C|         }|j                            | j                   ||j                            |           ||         }|j	                            | j                   | j
                            d          x}t           ||          t                    D ]}|         }	|j	                            |	j                   |         j        dk    rTt          |	dd           }
|
A|
         }|j                            |	j                   |j                            |           | |j                            |           d S d S d S d S )	Nr   )free_symbols_fx_partitionz*record_cross_partition_use %s (%s) %s (%s)-example_value)keyr9   )%torch.fx.experimental.symbolic_shapesrU   rM   logdebugr   r   
setdefaultr   r   rH   getsortedr+   r@   r   )rR   rS   rU   defineduseddef_partitionuse_partitiondef_valss_node	s_defineds_def_partition
partitionssymbol_to_nodes               r"   record_cross_partition_usez0split_module.<locals>.record_cross_partition_use   s    	GFFFFF(OT::x$77		8M7X5IHMMsTX	
 	
 	

 d??" *7 3%00???#!,77=== *4 0$//>>>  (}00AAAGN#LL$9$9sCCC L L!/!2%,77DDD)!,/=@@ )0(N(NI(42<Y2G / 7 B B6; O O O / : E Ed K K K&!.99'BBBBBA ?  0 '&r$   c                    t           |                     }t                              d| j        |                               |          }|t          |          x|<   }|j                            | j                   || _        d S )Nz*instantiate_node_partition_mapping %s (%s))	r+   r[   r\   r   r^   r   r   appendrV   )r5   partition_name	partitionri   r/   s      r"   "instantiate_node_partition_mappingz8split_module.<locals>.instantiate_node_partition_mapping   s    ^^D1122		>	>ZZZ NN>22	5>~5N5NNJ~&##DI...+r$   rX   )r9   r=   outputcall_function   c              3   B   K   | ]}t          |t                     V  d S N)
isinstancer   .0args     r"   	<genexpr>zsplit_module.<locals>.<genexpr>/  s/      JJz#t444JJJJJJr$   c              3      K   | ]}|d uV  	d S ru   r,   )rx   vs     r"   rz   zsplit_module.<locals>.<genexpr>?  s&      >>q}>>>>>>r$   zautocast must exitc                 4    i | ]\  }}|t          |          S r,   r_   rx   kr|   s      r"   
<dictcomp>z split_module.<locals>.<dictcomp>A  s$    JJJA6!99JJJr$   c                 4    i | ]\  }}|t          |          S r,   r~   r   s      r"   r   z split_module.<locals>.<dictcomp>B  s$    BBBTQAvayyBBBr$   zautocast_regions: %szgrad_regions: %s)r9   r=   rq   c                      | d           S ru   r,   )nrk   s    r"   <lambda>zsplit_module.<locals>.<lambda>U  s    (B(B1d(K(K r$   zRautocast or set_grad_enabled require monotonically increasing partitions:highest: z, this node's: c                      |           S ru   r,   rR   r5   rk   s    r"   r   zsplit_module.<locals>.<lambda>d  s    ,F,FxQU,V,V r$   c                      |           S ru   r,   r   s    r"   r   zsplit_module.<locals>.<lambda>g  s    .H.HSW.X.X r$   z cycle exists between partitions!c              3      K   | ]}|V  d S ru   r,   rw   s     r"   rz   zsplit_module.<locals>.<genexpr>  s"      88ss888888r$   )r@   rG   r:   kwargsr;   )r;   rV   c                     |          S ru   r,   r   r   s    r"   r   zsplit_module.<locals>.<lambda>  s    TU r$   c                     |          S ru   r,   r   s    r"   r   zsplit_module.<locals>.<lambda>  s    {1~ r$   )call_moduler=   r>   zOperator target r?   _)r@   rG   r:   r   r;   r   zMissing exit nodec                 (    g | ]}|j         d k    |S )r9   )r@   )rx   r5   s     r"   
<listcomp>z split_module.<locals>.<listcomp>  s$    QQQt=8P8Pd8P8P8Pr$   c              3   >   K   | ]}j         |                  V  d S ru   )r   )rx   r   
orig_nodesro   s     r"   rz   zsplit_module.<locals>.<genexpr>  sA       
 
8<I!*T"23
 
 
 
 
 
r$   c                 (    g | ]}|v|         S r,   r,   )rx   rY   orig_mod_envoriginal_orders     r"   r   z split_module.<locals>.<listcomp>
  s/     / / /&)NAZAZS!AZAZAZr$   c              3   (   K   | ]}|         V  d S ru   r,   )rx   r   r6   s     r"   rz   zsplit_module.<locals>.<genexpr>&  s(      BB,t$BBBBBBr$   c                     | j                  S ru   )r   )r   r6   s    r"   r   zsplit_module.<locals>.<lambda>5  s    |AF?S r$   zpost split_module)Nr[   r\   r   r   r   r+   r   r   graph_moduler   sympyr   amp_enter_autocast_exit_autocast_C_set_grad_enabledr	   setr   nodesrH   r^   rv   SymIntr5   exprSymbolr@   rG   rA   r:   boolalladdremovevaluesitems_LOGGERisEnabledForloggingDEBUGr   map_argr   listkeysr   rm   popr   RuntimeErrorr   rE   tuplerF   rI   r   r9   rK   rV   rJ   rL   rM   joinr    r   dictreversedr   r   rq   r   proxyProxy	enumeratenextiter)Br-   r.   r/   r0   r1   r2   rQ   r   rp   GLOBAL_STATE_NODESgrad_regionsautocast_regionsautocast_exitsactive_gradactive_autocastsvals0aassert_monotonically_increasinghighest_partitionpidoriginal_partition_orderroot_partitionsrn   sorted_partitionsroot_partition	dependentregions_mappingregionsrnew_nodeinpr9   gathered_argsgathered_kwargsrG   target_atomstarget_attrrO   qualnamer   	exit_noder7   construct_order_partitionsalready_constructed_attr_nodesoutput_valsnum_output_valsorig_mod_attr_nodesbased_mod_attrs
output_valnum_outputsoutput_val_proxyioutput_nameretr6   rP   r   r5   r   r   r   ro   ri   rk   rj   sB   ` `  `                                                 @@@@@@@@@@@r"   r   r   +   s   P II4	
 	
 	
  ,,39o, S%("7"CCD, , , , , , , ,8 LLL')J"$J/1N-C-C"*4.-C -C -C -C -C -C -C^
, 
, 
, 
, 
, 
, 		!	 " 1<L 5@MM13NKuu +: +: IMM/222S?sEL)) @SX]*rEL99 @ n$$,0N38=)7;;;**40007o%%$+9K*K*K{eh88849~~****!$)A,55555",/1E1E0F,G,G[))	 999JJ	JJJJJJJJ $$T***),nnT.B.B-C)D)D &'+t$$	 88849~~**** 1.22>>$3G3GHHH ''	!555/3ty|,"%))..*>*>???! 	: 	:AQ##NN4$8$89999	: >>n&;&;&=&=>>>>>TT@TTTTJJ1A1G1G1I1IJJJBB\-?-?-A-ABBBLGM** 8,.>???(,777&*+;&<&<&R\@R@R#    $
49 71117hHN""	!KKKK   * 	$ .&&C$+++E.E E?BE E ,++ !$ ;000HN""	VVVVV   HN""XXXXX    $JOO$5$566!#O%/%5%5%7%7 3 3!	9)** 	3"">222 $&
 2(,,..  000#N3> 	2 	2Iy!.22>BBBi(5 2&&y111  2 Z00=>>> -l; 7 7,2244 	7 	7MD'w<<!####<@Js71:'3D9QRR[ 
7 
7&s1vv.	$?66w;88di88888"i 7   !%	 0 0.6	%d++
7	7  , A A~.	# 	A 	AC#/55$S/. 6  K  *#388::K5@I!*S/22	A  *3 *34)) )	3"4#56I $/K!HN2249>V>V>V>VWWM#hn445555 O w999#{0055( = =D";55 Z,-X-X-X-XYYY")+t"<"<KK,//,7	!&)+ #,"7BB&BBH-1[L*mU33333ot44444 7A499TD 227"&) 3  H !INN,,HM*2I!$' -- 6 6_-- 	6 	6D%d+Gw<<!####SbS\ 6 6&s1vv.	*40	 ,,.A,,,$?66 |$+#/57'n 7   !* 3 3 5 56	6" %'L$&L+08>+?+?+A+ANCEN 	+GM 	 	D+:?lN, ,(L..	 GM 	+ 	+D&*L## "5R:R  &)UU" RQqw}QQQN4 7E 7E~.	  
 
 
 
 
@I@Q
 
 
 
 

 k**aO"";q>2222q  O"";/// 	9/ / / / /-6-=/ / / ' 9 99990?,1 1-o /2248888 , 9 9999/>,0 0,n /2248888050E0Q0Qy1
 1
y,-
 $//!BBBB1ABBBBB
 


 )+,,??$x~33J??"+I,=">"> E E;,<Q,?,D[))EA:DLd9#455667  7h!!&&ty|5S5S5S5STT   (

+
+NN
K
KCIId	
 	
 	
   Jr$   )NFF) rB   typingr   r   r   r   r   r   collectionsr	   r   r   torch.fx._compatibilityr
   torch.fx.graph_moduler   torch.fx.noder   torch.fx._utilsr   __all__	getLoggerr(   r[   r   r   nnModuleintr+   r   r   r,   r$   r"   <module>r      s    ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; # # # # # #   1 1 1 1 1 1 - - - - - -       2 2 2 2 2 2 
'!!(++ +gd+++
 
 
 
 
 
 
 ,+
2 d+++
 .2*/.3S SSHOS dVS[)S 4S>*	S
 "$S &d^S S S ,+S S Sr$   