
    PhQ                     H   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gZ e
j&                  e      Z ed	
       G d d             Z ed	
      	 	 ddedej.                  j0                  deegef   deeeef      dee   f
d       Zy)    N)AnyCallableDictListOptionalSet)OrderedDict)compatibility)GraphModule)Node	Partitionsplit_moduleT)is_backward_compatiblec                   $    e Zd ZdefdZdefdZy)r   namec                     || _         d| | _        g | _        i | _        i | _        i | _        i | _        t        j                  j                  j                         | _	        i | _        i | _        y )Nsubmod_)r   submod_name
node_namesinputsoutputsdependencies
dependentstorchfxgraphGraphenvironmenttargets)selfr   s     gC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/fx/passes/split_module.py__init__zPartition.__init__   sc    	$TF+%'')(*-/+-+088>>+?+?+A
-/')    returnc                     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__   sb    TYYK  ' (} % '((,(9(9': ;&&*oo%68	
r#   N)__name__
__module____qualname__strr"   r&    r#   r!   r   r      s    
*S 
*
# 
r#   mroot_msplit_callbackqualname_mapkeep_original_orderc                 n   456789:; dt         dt        t        t         f   dt        t        t        j                  j
                  j                  f   f5 fd}i :i 8dt         dt        t            f:fd;:fd}t        j                  j                  t        j                  j                  t        j                  j                  g}t               }t               }	i }
d	}t               } j                  j                   D ]  77j"                  d
v r |7       7j"                  dk(  r7j$                  |v r7j$                  t        j                  j                  k(  rRt'        7j(                        dk(  sJ t+        7j(                  d   t,              sJ 7}t         7      h      ||<   n	7j$                  t        j                  j                  k(  rJt/        d 7j(                  D              sJ |j1                  7       t         7      h      |	7<   d	|
7<   n7j$                  t        j                  j                  k(  rqt'        7j(                        dk(  sJ |	7j(                  d      j1                   7             |j3                  7j(                  d          7|
7j(                  d   <   |||   j1                   7             |D ]  }|	|   j1                   7               t/        d |
j5                         D              sJ d       |	j7                         D ci c]  \  }}|t9        |       }	}}|j7                         D ci c]  \  }}|t9        |       }}}t:        j=                  t>        j@                        r,t:        jC                  d|	       t:        jC                  d|       t-        |	      xs t-        |      }d} j                  j                   D ]  7787jD                  <   7j"                  dv r"7j"                  dk(  r;t        j                  j                  jG                  7j(                  d   ;fd       l|r 7      }||k  sJ d| d|        |}7j$                  |vst        j                  j                  jG                  7j(                  7;fd       t        j                  j                  jG                  7jH                  7;fd        tK        :jM                               }g }:j7                         D ],  \  }9t'        9jN                        r|jQ                  |       . g }|rw|jS                         }|jQ                  |       :|   jT                  D ]A  }:|   jN                  jS                  |       :|   jN                  r1|jQ                  |       C |rwt'        |      t'        :      k7  rtW        d      |	|fD ]  }|j7                         D ]  \  7}t'        |      dkD  sJ 7:t        |d            jX                  7<   |dd	 D ]  }:t        |         99j                  j[                  7j"                  7j$                  t]        d 7j(                  D              i 7j^                        }7j`                  jc                         |_0        |9jX                  7<      |D ]v  }:|   99jd                  D ]`  }9j                  jg                  |8|   j^                        }8|   j`                  jc                         |_0        |9jX                  8|   <   b x  j                  j                   D ]  7ti        7d      s:7jj                     99jX                  6t        j                  j                  jG                  7j(                  6fd       }t        j                  j                  jG                  7jH                  6fd!      } 7j"                  d"vr7j$                  }!n7j$                  jm                  d#      }" }#|"D ]3  }$ti        |#|$      sto        d$7j$                   d%      tq        |#|$      }#5 d&js                  |"      }!|#9jt                  |!<   | 9jv                   d#|! }%7j$                  ||%<   t+        |t\              sJ t+        | tx              sJ 9j                  j[                  7j"                  |!|| 7j^                        }7j`                  jc                         |_0        |9jX                  7<    |	fD ]  }t{        |      D ]  7|7   }t'        |      dkD  sJ |d	d D ]  }:t        |         9|
7   }&|&J d'       9j                  j[                  |&j"                  |&j$                  9jX                  7   fi |&j^                        }|&j`                  jc                         |_0           i }'i 4t        j                  j                  j}                         5i }(|s) j                  j                   D ]  7 |74|(      \  4}( n* j                  j                   D ]  77|'7jD                  <    |s|n|})t               }*|)D ]  }:|   9t]        89fd(9j~                  D              }+t'        |+      },|,dk(  r9j                  j                  |+d          n |,dkD  r9j                  j                  |+       |rF9jd                  D -cg c]  }-|'|-   	 }.}-|.D ]%  77|*v r |74|(      \  4}(|*j1                  7       ' t        j                  j
                  j                  9jt                  9j                        |(9jv                  <   5j                  9jv                  t]        4fd)9jd                  D                    }/t'        9j~                        }0|0dkD  rZt        j                  j                  j                  |/      }1t        9j~                        D ]  \  }2}3|1|2   j                  4|3<    |0dk(  s|/4t        t        9j~                              <     j                  j                   D ][  77j"                  dk(  s5j                  t        j                  j                  jG                  7j(                  d   4fd*             ] t        j                  j
                  j                  |(5      S c c}}w c c}}w c c}-w )+a  
    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):
                    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
    nodebase_mod_envbase_mod_attrsc                     | j                   dk(  rt        | j                        dkD  r| j                  d   nt        j                  j
                  }j                  | j                  | j                  |      || j                  <   | j                  j                         || j                     _
        ||fS | j                   dk(  rj                  | j                        || j                  <   | j                  j                         || j                     _
        }| j                  j                  d      D ]3  }t        ||      st        d| j                   d      t!        ||      }5 ||| j                  <   ||fS )Nplaceholderr   )	type_exprdefault_valueget_attr.zNode target  not found!)oplenargsinspect	Signatureemptyr6   targettyper   metacopyr9   splithasattrAttributeErrorgetattr)r2   r3   r4   r8   attr_valatombase_mod_graphr,   s         r!   construct_graphz%split_module.<locals>.construct_graph   sG   
 77m# #DII 2		!8I8I8O8O  '5&@&@tyy 'A 'L# ,099>>+;L#( ^++ WW
"&4&=&=dkk&JL#+/99>>+;L#(H))#.x.(<}K)PQQ"8T2 / +3N4;;'^++r#   def_nodeuse_nodec                 n   t        | dd       }t        |dd       }||k7  r|G|   }|j                  j                  | j                         ||j                  j                  |       |I|   }|j
                  j                  | j                         ||j                  j                  |       y y y y )N_fx_partition)rI   r   
setdefaultr   r   r   r   )rN   rO   defineduseddef_partitionuse_partition
partitionss         r!   record_cross_partition_usez0split_module.<locals>.record_cross_partition_use   s     (OT:x$7d?" *7 3%%00?#!,,77= *4 0$$//>&!..99'B '   r#   c                     t         |             }j                  |      }|t        |      x|<   }|j                  j	                  | j
                         || _        y N)r*   getr   r   appendr   rQ   )r2   partition_name	partitionrW   r.   s      r!   "instantiate_node_partition_mappingz8split_module.<locals>.instantiate_node_partition_mapping   s\    ^D12 NN>2	5>~5NNJ~&##DII.+r#   N)r6   r9   outputcall_function   r   c              3   >   K   | ]  }t        |t                 y wrZ   )
isinstancer   .0args     r!   	<genexpr>zsplit_module.<locals>.<genexpr>   s     J	z#t44	s   c              3   $   K   | ]  }|d u 
 y wrZ   r+   )rf   vs     r!   rh   zsplit_module.<locals>.<genexpr>   s     >&=q}&=s   zautocast must exitzautocast_regions: %szgrad_regions: %s)r6   r9   r`   c                      | d       S rZ   r+   )nrX   s    r!   <lambda>zsplit_module.<locals>.<lambda>  s    (B1d(Kr#   zRautocast or set_grad_enabled require monotonically increasing partitions:highest: z, this node's: c                      |       S rZ   r+   rN   r2   rX   s    r!   rn   zsplit_module.<locals>.<lambda>%  s    ,FxQU,Vr#   c                      |       S rZ   r+   rp   s    r!   rn   zsplit_module.<locals>.<lambda>(  s    .HSW.Xr#   z cycle exists between partitions!c              3       K   | ]  }|  y wrZ   r+   re   s     r!   rh   zsplit_module.<locals>.<genexpr>H  s     8issis   )r<   rB   r>   kwargsr7   )r7   rQ   c                     |    S rZ   r+   rm   r   s    r!   rn   zsplit_module.<locals>.<lambda>a  s
    TUr#   c                     |    S rZ   r+   ru   s    r!   rn   zsplit_module.<locals>.<lambda>c  s	    {1~r#   )call_moduler9   r:   zOperator target r;   _zMissing exit nodec              3   B   K   | ]  }j                   |        y wrZ   )r   )rf   r   
orig_nodesr^   s     r!   rh   zsplit_module.<locals>.<genexpr>  s&      
@QI!!*T"23@Qs   c              3   (   K   | ]	  }|     y wrZ   r+   )rf   r   r3   s     r!   rh   zsplit_module.<locals>.<genexpr>  s     B1A,t$1As   c                 "    | j                      S rZ   )r   )rm   r3   s    r!   rn   zsplit_module.<locals>.<lambda>  s    |AFF?Sr#   )Hr   r   r*   r   r   graph_moduler   r   amp_enter_autocast_exit_autocast_C_set_grad_enabledr	   setr   nodesr<   rB   r=   r>   rd   boolalladdremovevaluesitemssorted_LOGGERisEnabledForloggingDEBUGdebugr   map_argrs   listkeysr   r\   popr   RuntimeErrorr   create_nodetuplerC   rD   rE   r   r6   rG   rQ   rF   rH   rI   joinr   r   dictreversedr   r   r`   rw   proxyProxy	enumerater2   nextiter)<r,   r-   r.   r/   r0   rM   r_   GLOBAL_STATE_NODESgrad_regionsautocast_regionsautocast_exitsactive_gradactive_autocastsakrj   assert_monotonically_increasinghighest_partitionpidoriginal_partition_orderroot_partitionsr]   sorted_partitionsroot_partition	dependentregions_mappingregionsrnew_nodeinpr6   gathered_argsgathered_kwargsrB   target_atomstarget_attrrK   qualname	exit_nodeorig_mod_envr4   construct_order_partitionsalready_constructed_attr_nodesoutput_valsnum_output_valskeyorig_mod_attr_nodes
output_valnum_outputsoutput_val_proxyioutput_namer3   rL   r   r2   rz   r^   rW   rX   s<   ` `                                                 @@@@@@@@r!   r   r   (   s   N,,39o, S%(("7"7"C"CCD,0 (*J"$JCC"*4.C$	, 			!!		  "" 1<L 5@M13NKu77;;*4077o%$++9K*K{{ehh888499~***!$))A,555",/1E0F,G[)		 9 99J		JJJJ $$T*),nT.B-C)D &'+t$		 8 88499~*** 1.22>$3GH ''		!5/3tyy|,"%)).*>?!AQ##N4$89 "7 < >n&;&;&=>>T@TT>1A1G1G1IJ1IA6!91IJ-9-?-?-AB-ATQAvayL-ALBGMM*,.>?(,7&*+;&<&R\@R#  $
499 771177hHHNN""		!K * &C$+ F./seEF+ !$ ;;00HHNN""		V HHNN""X5 <  $JOO$56!#O%/%5%5%7!	9))*"">2 &8
 $&
(,,.  0#N3>>Iy!..22>Bi(55&&y1 ?  Z0=>> -l;,224MD'w<!###<@Js71:'33D9QR[&s1v.	$??66ww;;8dii88"ii 7  !%		 0.6	%%d+ ! 5 <" ,~.	##C#//55$S/.. 6 K  *#3388:K5@I!!*S/2 $ , 4)"4#5#56I $//K!HHNN22499>VWM#hhnn445O ww99#{{005(D";5,/?}K-XYY")+t"<K )
 ,/,7	!!&)+ #,"7"7!8&BH-1[[L*mU333ot444 2277"&)) 3 H !IINN,HM*2I!!$'Q V --_-D%d+Gw<!###Sb\&s1v.	*40	 ,A.AA,$??66 ||$++#//57'nn 7  !* 3 3 5 " . .$ %'L$&L+088>>+?+?+ANCENGGMMD+:lN,(L. " GGMMD&*L# " "5:R  &)U"4~.	  
@I@Q@Q
 

 k*aOO"";q>2q OO"";/ .7-=-=/-=cS!-=   / ,99/>,0,n /2248 , 160E0E0Q0Qy1
y,,-
 $//!!B1A1ABB


 )++,?$xx~~33J?"+I,=,=">;,<Q,?,D,D[) #?A:DLd9#4#4567] 5` 77h!!&&tyy|5ST  88  ,,^^LLO KBB/s   :t&&t,.t2)NF)r?   typingr   r   r   r   r   r   collectionsr	   r   r   torch.fx._compatibilityr
   torch.fx.graph_moduler   torch.fx.noder   __all__	getLoggerr'   r   r   nnModuleintr*   r   r   r+   r#   r!   <module>r      s     ; ; #   1 - 
'
'

H
%d+
 
 ,
2 d+
 .2*/@M@MHHOO@M dVS[)@M 4S>*	@M
 "$@M ,@Mr#   