
    Ph/              	          d dl Z d dlmZmZmZmZmZ d dlZd dl	m
Z
 d dlmZ g dZ G d dej                  j                        Zdej                  j                  d	efd
Zdej                  j                  dedefdZ	 	 ddeej(                  j*                  ej                  j                  f   deeej                  j,                  gef      dedefdZy)    N)CallableDictOptionalSetUnion)map_arg)split_module)FoldedGraphModuleget_unique_attr_name_in_modulesplit_const_subgraphsc                        e Zd ZdZ	 	 	 d
dej
                  j                  dej                  j                  de	ej                  j                     de	e
   de
f
 fdZ fdZd	 Z xZS )r
   a  
    FoldedGraphModule is a GraphModule which also contains another
    `const_subgraph_module` representing a subgraph which has all const attr
    inputs and which can be run once before running the main standard
    `graph`. The `const_output_names` are the ordered list names of attrs which
    represent what each respective output from the const_subgraph should be set
    on which attrs.
    rootgraphconst_subgraphfx_const_folded_attrs_namedevice_for_folded_attrsc                     t         |   ||       |d nt        j                  j	                  ||      | _        d| _        || _        || _        y )NF)	super__init__torchfxGraphModuleconst_subgraph_modulehas_folding_been_runr   r   )selfr   r   r   r   r   	__class__s         kC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/fx/experimental/const_fold.pyr   zFoldedGraphModule.__init__   sW     	u% % %%dN; 	"
 %*!*D''>$    c                 T    | j                   s| j                          t        |   | S N)r   run_foldingr   __call__)r   argskwargsr   s      r   r"   zFoldedGraphModule.__call__'   s(    ((w&&r   c                 Z     j                    j                  y  j                  rJ d _         j                         } fd}t        |t              r3t
        j                  j                  |D cg c]
  } ||       c}      n ||      }t          j                  |       y c c}w )NTc                    t         j                  j                  t        | t              s| n/t        j
                  | g      j                  j                        t        | t         j
                        r| j                        S d      S )N)deviceF)requires_grad)	r   nn	Parameter
isinstanceintTensortor   r(   )ir   s    r   _create_paramz4FoldedGraphModule.run_folding.<locals>._create_param=   sz    88%%!!S) \\1#&))1M1M)N1;Au||1Laoo	 &   SX	 &  r   )	r   r   r   r+   tupler   r)   ParameterListsetattr)r   folded_attrsr0   r/   paramss   `    r   r!   zFoldedGraphModule.run_folding,   s     &&...6,,,,$(!
 113	 ,. HH""l#KlM!$4l#KL|, 	
 	d55v>	 $Ls   2B()NNcuda)__name__
__module____qualname____doc__r   r)   Moduler   Graphr   strr   r"   r!   __classcell__)r   s   @r   r
   r
      ss     4848'-?hhoo? xx~~? !0	?
 %-SM? "%?$'
?r   r
   gminline_mod_namec                    t        | j                               |   }t        |t        j                  j
                        sJ d}| j                  j                  D ]%  }|j                  dk(  s|j                  |k(  s#|} n |J |j                  }i d}fd}|j                  j                  D ]  }|j                  dk(  r||   |<   |dz  } |j                  dk(  r-|j                  d   }	t        |	|      }
|j                  |
       \| j                  j                  |      5  | j                  j                  ||      }ddd       |<    | j                  j                          y# 1 sw Y   +xY w)z
    Given `gm` and some graph module which is called with target name `inline_mod_name`,
    this helper will inline all of the nodes from that called graph module into `gm`.
    Ncall_moduler   c                 P    |    }| j                   j                         |_         |S r    )metacopy)nodenew_nodereplacement_mappings     r   replacement_fnz&_inline_module.<locals>.replacement_fnb   s$    &t,		(r   placeholder   output)dictnamed_modulesr+   r   r   r   r   nodesoptargetr#   r   replace_all_uses_withinserting_before	node_copyeliminate_dead_code)r?   r@   
inline_modcall_mod_node_to_replacerF   call_mod_argsph_countrI   inline_nodeoutputsoutput_replacementsrG   rH   s               @r   _inline_moduler]   M   sk    b&&()/:Jj%(("6"6777#77m#(F'+$  $/// -11M>@H
 "''-->>]*/<X/F,MH>>X%!&&q)G")'>"B$::;NOXX&&'?@xx))+~FH A+3K( .  HH  "	 A@s   *E11E:	
mod_tracednamereturnc                 "   t        j                  dd|      }|d   j                         rd| }t        | |      rSt        j                  d|      }||dz   }n(|j                  dd      \  }}| dt        |      dz    }t        | |      rS|S )zP
    Make sure the name is unique (in a module) and can represents an attr.
    z[^0-9a-zA-Z_]+_r   z(.*)_(\d+)$_1rK      )resubisdigithasattrmatchgroupr,   )r^   r_   ri   basenums        r   r   r   z   s    
 66"C.DAw4&z
*d
#.=$;DAq)ID#V1SX\N+D *d
# Kr   moduleskip_folding_node_fnr   c           	      	   t        | t        j                  j                        s t        j                  j	                  |       }n| }t               d}|j                  j                  D ]  j                  dv rj                  dk7  r%t        j                        j                        sF|r	 |      rQj                         rbj                         j                  dk7  sd} |st        ||j                        S dt        j                  j                  ffd}t        || |      }|j                   |j"                  }}d\  }	}
|j                  j                  D ]=  j                  dk(  st%        |j&                  t)        |j&                               ? |j                  j                  D ]=  j                  dk(  st%        |j&                  t)        |j&                               ? d	}|j                  j                  D ]/  j                  dk(  sj&                  |	k(  s#j*                  } n |J t        j                  j                  ||j                        }|j                  j                  D ]  j                  d
k(  rt        j*                  d   t,              }0j                  dk7  r@t/        fd|D              }|j                  dk(  sJ |j                  j1                        5  |j                  j3                  |j&                        }d	d	d	       j4                  j7                         _        j9                  |       |j                  j;                          dt=               v sJ t?        |d      }t%        ||rt        j@                  jC                         nt        j@                  jE                                |j                  j                  D ]  j                  dk(  sj&                  |	k(  s#j                  j1                        5  j                  j3                  |      }d	d	d	       j4                  j7                         _        j9                  |        n |j                  jG                          tI        ||
       t        ||j                  |j                  ||      S # 1 sw Y   xY w# 1 sw Y   xY w)aJ  
    Looks through `module` for any nodes that have all constant attribute inputs
    and separates them out into their own constant subgraph, and returns a
    FoldedGraphModule which runs that constant subgraph on the first run to set
    attributes on the module prior to running the non-constant portion of the
    graph.
    F>   rL   rJ   get_attrTrF   c                     | v rdS dS )Nr   rK    )rF   const_nodess    r   mod_partitionz,split_const_subgraphs.<locals>.mod_partition   s    K'q.Q.r   )submod_0submod_1rB   NrL   r   rJ   c              3   V   K   | ]   }|j                   j                  k(  s| " y wr    )r_   rQ   ).0nrF   s     r   	<genexpr>z(split_const_subgraphs.<locals>.<genexpr>   s#     N"4Q$++8Mq"4s   ))multiple_outputs_FX_CONST_FOLDED_ATTRS)%r+   r   r   r   symbolic_tracesetr   rO   rP   all_input_nodesissubset	is_impureaddr
   Noder	   ru   rv   r3   rQ   getattrr#   r1   nextrS   rp   rD   rE   rR   
erase_nodelocalsr   r)   r2   r*   rU   r]   )rm   rn   r   r^   found_const_foldingrt   splitconst_gmnon_const_gmconst_mod_namenon_const_mod_namecall_const_gm_argsroot_const_gmr{   in_noderG   r   r4   rs   rF   s                     @@r   r   r      s    fehh223XX,,V4

 '*eK  && 77// 77j T-A-A)B)K)K*
   $8$> >> 	77j "&1 '6  Z-=-=>>/EHHMM / V];E"^^U^^lH)?&N&
 ""((77m#E4;;dkk(JK ) $$77m#E4;;$++(FG % !!77m#{{n,%)YY"	 "
 ))) HH((?M##))77h)$))A,>77m#N"4NNzzZ'''  11$7$**33GNNCH 8		(""8,&&t, * ))) "@'" "$4 %((:L:L:N
 !!77m#~(E,,T2#zz223MN 3 $		 0L&&|4 " 
KK##%
 5,-" E 87* 32s   &S  S- S*	-S6	)Ncpu)re   typingr   r   r   r   r   torch.fxr   torch.fx.noder   torch.fx.passes.split_moduler	   __all__r   r   r
   r=   r]   r   r)   r;   r   boolr   rr   r   r   <module>r      s    	 7 7  ! 5 [??,, ??D*#uxx++ *#c *#Zuxx/C/C 3 SV , GK#(S%((//588#7#778S"8UXX]]OT,A#BCS !S 	Sr   