
    Phd                        d dl Z d dlZd dl mZ d dlmZmZmZmZmZ d dl	Z	d dl
mc mZ d dlmc mZ d dlmZ d dlmZmZmZmZ d dlmZ ddlmZ d	e	j6                  d
e	j8                  j:                  dedefdZ  G d de	j                  j2                        Z!dede!fdZ"de	j                  jF                  ddfdZ$d Z%dededefdZ&de	j                  j2                  de	j                  j2                  fdZ'de	j                  j2                  defdZ(de	j                  j2                  de	j                  jR                  fdZ*de	j                  j2                  de	j                  jR                  fdZ+ G d  d!      Z,d"e	j                  jF                  d#e	j                  j2                  fd$Z-ded%eeef   d&ee   fd'Z.d( Z/y))    N)deepcopy)castDictListOptionalUnion)ExportedProgram)ConstantArgumentModuleCallSignatureSymIntArgumentTensorArgument)GraphModule   )!_check_input_constraints_pre_hookfrom_obj	to_moduletargetis_parameterc                    |j                  d      ^ }}|D ]>  }t        ||d       }|+t        j                  j	                         }t        |||       |}@ t        | t        j                        st        dt        |             |r0|j                  |t        j                  j                  |              y |j                  ||        y )N.z)Expected only parameters or buffers, got:)splitgetattrtorchnnModulesetattr
isinstanceTensor
ValueErrortyperegister_parameter	Parameterregister_buffer)r   r   r   r   prefixfielditemts           bC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/_export/unflatten.py_assign_attrr)      s     \\#&NVUItT*9!AItQ'	  h-Dd8nUU$$UEHH,>,>x,HI!!%2    c                   .     e Zd Zdef fdZ fdZ xZS )_UnflattenedModuleexport_modulec                 (   |j                   j                  t        d      t        	|   i t
        j                  j                         d       t        |j                        }t        |j                         | _         t        |j                        | _
        t        || j                          t        ||        |j                  | _        |j                  | _        |j                  }| j                   j                   D ]$  }||   j#                         }t%        || |d       & | j                   j&                  D ]$  }||   j#                         }t%        || |d       & i | j                   j(                  | j                   j*                  }t-        | |g        | j/                         D ]b  }t1        |t
        j                  j2                        s(|j                  j4                  D ]"  }|j6                  dk7  r|j8                  |vr"J  d y )Nz%Unflattening on JointExportModule NYIr,   T)r   Fplaceholder)graph_signaturebackward_signaturer   super__init__r   fxGraphr   graphmodule_call_graph_inplace_buffer_mutations_outline_submodulesrange_constraintsequality_constraints
state_dict
parameterscloner)   buffersinputs_to_parametersinputs_to_buffers_sink_paramsmodulesr   r   nodesopname)
selfr-   export_graphr<   rF   clonedinputs_to_statemodulenode	__class__s
            r(   r3   z_UnflattenedModule.__init__1   s   ((;;GDEEUXX^^-/CD 3 34'(E(EF!)-*I*I!J!,0D0DEL$/!.!@!@$1$F$F!"--
((33D%++-F!	 4 ((00D%++-F"	 1+
""77+
""44+

 	T?B/llnFfehh&:&:;**77m+yy777 + %r*   c                 V   t        j                  ||f      \  }}| j                  d   j                  dk(  sJ | j                  d   j                  }||j
                  k7  rt        d| dd|j
                         t        |    | }t        j                  ||j                        S )Nr    zInput treespec does not match with exported module's. Are you sure you are calling this with the right arguments? Input treespec: z. zExported module treespec: )pytreetree_flattenr7   fqn	signaturein_spec	TypeErrorr2   __call__tree_unflattenout_spec)rG   argskwargs	flat_argsrT   rS   tree_outrM   s          r(   rV   z_UnflattenedModule.__call__`   s    #00$@	7%%a(,,222**1-77	i'''##*)2/ -Y->->,?@	  7#Y/$$Xy/A/ABBr*   )__name__
__module____qualname__r	   r3   rV   __classcell__)rM   s   @r(   r,   r,   0   s    -8o -8^C Cr*   r,   rK   returnc                 F    t        |       } | j                  t               | S )zvUnflatten an ExportedProgram, producing a module with the same module
    hierarchy as the original eager module.
    )r,   register_forward_pre_hookr   )rK   s    r(   	unflattenrd   q   s"      'F
$$%FGMr*   r6   c                    t        t        t        | j                                    }|j                  dk(  rt        |j                        dk(  sJ |j                  d   }|j                  }|dt        |       }|j                  j                         D ci c]  \  }}||
 }}}| j                  D 	ci c]  }	|	j                  dk(  s|	j                  |	! }
}	|D ]  }||j                     }||   }|
|   }| j                  |      5  | j                  dt        j                  j                  j                   ||f      |j"                  j                         D ]  \  }}|j"                  |<    	 ddd       |j%                  fd        t'        |t        |      d       }|f|_        yc c}}w c c}	w # 1 sw Y   KxY w)a  Transform buffer mutations from their functionalized form into a copy_
    node in the graph.

    Functionalization represents buffer mutation by passing the buffer as an input and output. So for example, the eager code:
        def forward(self, x):
            self.buffer += x
            return x * x

    Will become a graph that looks like:
        def forward(self, buffer, x):
            mutated_buffer = aten.add(buffer, x)
            mul = aten.mul(x, x)
            return (mutated_buffer, mul)

    We want to inplace this into something that looks like the original eager code:
        def forward(self, buffer, x):
            mutated_buffer = aten.add(buffer, x)
            buffer.copy_(mutated_buffer)
            mul = aten.mul(x, x)
            return (mul,)
    outputr   r   Nr/   call_functionc                     | uS N )xnew_nodes    r(   <lambda>z+_inplace_buffer_mutations.<locals>.<lambda>   s
    1H;Lr*   )nextiterreversedrD   rE   lenrY   buffers_to_mutaterA   itemsrF   inserting_aftercreate_noder   opsatencopy_metareplace_all_uses_withtuple)r6   r0   output_nodereturn_argsmutation_node_to_buffer	mutationskvbuffers_to_inputsrL   input_name_to_nodemutationbuffer_name
input_name
input_nodeuser_outputsrl   s                   @r(   r8   r8   z   s   , tHU[[123K>>X%#k.>.>*?1*DDD""1%K-??:c"9:;I*9*K*K*Q*Q*ST*S$!QA*ST$)KK$/D477m3K		4K   -hmm<&{3
'
3
""8,((!5!5
H7MH !++-1#$a  .	 - 	&&x1LM " C/023L &(K3 U -,s   F46F:F:A)F??G	c                 V    t        |       t        |      k  xr |dt        |        | k(  S )z2Check whether `candidate` is a prefix of `target`.N)rq   )	candidater   s     r(   	is_prefixr      s+    y>CK'QF3CS^,D	,QQr*   
parent_fqn	child_fqnc                     | dk(  r|S | j                  d      }|j                  d      }|d t        |       |k(  sJ d| d|  d       dj                  |t        |      d        S )NrO   r   zChild module 'z(' is not a descendant of parent module '')r   rq   join)r   r   parent_splitchild_splits       r(   compute_accessorr      s    R##C(L//#&K 	'c,'(L8Y		{"J:,VWXY888KL 1 3455r*   rk   yc                     dt         j                  j                  dt        fd} || j                         ||j                        k(  sJ y )Nr6   ra   c                 V  
 g }i 
dt         f
fd}t        | j                        D ]  \  }}t        j                  ||j
                        D cg c]  }t        |       }}|t        j                  ||j                        j                         D cg c]  \  }}| d|  c}}z  }|j                  dk(  r|j                  nd}	|j                  | d|j                   d|	 dd	j                  |       d
       |
t        |      <    dj                  |      S c c}w c c}}w )Nra   c                     t        | t        j                  j                        rdt	        t        |                z   S t	        |       S )N%)r   r   r4   Nodestrid)arg	nodes_idxs    r(   arg_dumpz?_verify_graph_equivalence.<locals>.graph_dump.<locals>.arg_dump   s7    #uxx}}-S2c7!3444s8Or*   =rg   rO   z: [z](z, )
)r   	enumeraterD   rP   tree_maprY   rZ   rs   rE   r   appendr   r   )r6   retr   irL   r   	args_dumpkeyvaluer   r   s             @r(   
graph_dumpz-_verify_graph_equivalence.<locals>.graph_dump   s   $&		S 	
 !-GAt-3__Xtyy-QR-QcS-QIR"(//(DKK"H"N"N"P"PJC %q "P I %)GG$>T[[BFJJ!Btwwiq499Y3G2HJK"#Ibh . yy~ Ss   D D%
)r   r4   r5   r   r6   )rk   r   r   s      r(   _verify_graph_equivalencer      s?    %((.. S ( agg*QWW"5555r*   gmc                 v    d}t        | d|       r|dz  }t        | d|       rd| }t        | ||       |S )Nr   _spec_r   )hasattrr   )r   specr   rF   s       r(   	_add_specr      sN    	A
"qcl
#	Q "qcl
#A3<DBdKr*   c                     t        | |      }| j                  j                  |      }| j                  j                  t        j
                  ||f      S ri   )r   r6   get_attrrg   	fx_pytreetree_flatten_spec)r   rL   r   rF   	spec_nodes        r(   _generate_flattenr      sD    RD!!$'I88!!)"="=i?PQQr*   c                     t        | |      }| j                  j                  |      }| j                  j                  t        j
                  ||f      S ri   )r   r6   r   rg   rP   rW   )r   rD   r   rF   r   s        r(   _generate_unflattenr      sD    RD!!$'I88!!&"7"7%9KLLr*   c                   P    e Zd Z	 ddeeef   fdZd Zd Zd Z	d Z
d Zd	 Zd
 Zy)ModuleFrameNr7   c	                    || _         || _        || _        || _        || _        || _        || _        d| _        | j                  d   | _        ||| _	        n~t        j                  j                  i t        j                  j                         | j                        | _	        |j                  | j                        | j                  j                  d<   | j
                  | j                  v r| j                  | j
                     | _        n*d | _        | j                  | j                  | j
                  <   t#        | j                   j$                        | _        | j                  j&                  | _        i | _        i | _        d | _        |~t/        |j                  | j                        }	|j                  j1                  |	| j                   | j                  n| j                          |j&                  j3                  |	      | _        |j                  | j                        }
|
| j                  t5        |
j6                  j8                        dk(  sJ |
j6                  j8                  d   }|
j6                  j8                  d   }|j:                  J |j:                  J | j                  j&                  j=                  d       5  g }t?        t5        |j8                              D ]9  }|jA                  | j                  j&                  jC                  d|              ; i }|j:                  D ]*  }| j                  j&                  jC                  |      ||<   , tE        | j                  tG        |      |f|
j6                        }tI        |
jJ                        D ]  \  }}| j                  j&                  jM                  dtN        jP                  ||ftS        |tT              s|jV                  nd	| 
      }tS        |tT              rotY        jX                  | j                  |jV                     j                        |_        || j*                  | j                  |jV                     <    	 d d d        | j                  j&                  j[                  | j,                        5  g }|
jJ                  D ]  }tS        |tT              r|j\                  |jA                  d        1tS        |t^        t`        f      sJ |jA                  | j                  jc                  | j                  |jV                                   te        | j                  j                  ||
j6                        }| j                  j&                  jg                  tN        jP                  |df      }| j                  j&                  jg                  tN        jP                  |df      }t?        t5        |j8                              D cg c]8  }| j                  j&                  jg                  tN        jP                  ||f      : }}|j:                  D ci c]9  }|| j                  j&                  jg                  tN        jP                  ||f      ; }}d d d        | j,                  J tG              | j,                  _4        | j,                  _5        y y y # 1 sw Y   \xY wc c}w c c}w # 1 sw Y   \xY w)NFmodule_call_signature   r   r   _positional_arg_rg   
_constant_)rE   r   rY   rF   )6
flat_graph
seen_nodesseen_modulesparentmodule_stack	module_idr7   verboserR   graph_moduler   r4   r   r5   getry   cached_graph_modulelistrD   r6   node_mapnode_to_placeholderparent_call_moduler   add_submodulecall_modulerq   rT   children_specscontextrt   ranger   r/   r   r{   r   inputsru   operatorgetitemr   r
   rF   copyinserting_beforer   r   r   remap_inputr   rg   rY   rZ   )rG   r   r   r   r   r   r   r7   r   accessorrS   	args_speckwargs_spec	arg_nodesidxkwarg_nodesrF   r[   r   flat_arg_noderD   inputinputs_node	args_nodekwargs_noder   r   s                              r(   r3   zModuleFrame.__init__   s    %$(("!2$$R(# ,D !& 4 4 !D
 ?P>S>S?D""#:; >>T...'+'8'8'HD$'+D$040A0ADdnn-$////0
&&,,
 =?#% ;?'

DHH=H--++3 !!--	 '-ll&>&>x&HD#%))$((3	 T[[%<y((778A===!))88;I#++::1=K$$,,,&&222""((88>	 Y%=%=!>?C$$))//;;>Nse<TU @ !'//D(,(9(9(?(?(K(KD(QK% 0-%%9%{3%%	
 !*)*:*: ;HC$($5$5$;$;$G$G*'//'-)#/?@ !XX)#/ %H %M "#'78 )-4??3883L3Q3Q)RM&JWD,,T__SXX-FG !< ?8 ""33D4K4KL79&--E!%)9:u{{?RT*)%..1QRRR KK33DOOEJJ4OP . 2KK,,%% !KK--;;$${A&6	 #kk//==$${A&6
 #3y'?'?#@AA KK%%33H4D4DyRSnUA   )00	 1 t{{((66 ((;*:  1	  7 MB **666+0+;D##(-8D##*M &=  ?>f7 MLs8   F+[/E[)=[>[)>[$[)[
[))[2c                 `   |j                   | j                  u sJ | j                   j                  d       5  | j                   j                  |j                  |j
                        }d d d        t        j                  |j                        _        || j                  |<   y # 1 sw Y   =xY w)N)	type_expr)	r6   r   r   r/   rF   r    r   ry   r   )rG   rk   placeholder_nodes      r(   add_placeholderzModuleFrame.add_placeholder|  s    ww$//)))ZZ((.#zz55aff5O / !%		!&& 1&6  # /.s   2B$$B-c                 N   |j                   | j                  u sJ || j                  v r| j                  |   S || j                  vrR| j	                  |       | j
                  5| j
                  j                  d| j                  j                  |             | j                  |   S )Nr   )	r6   r   r   r   r   r   
insert_argr   r   )rG   rk   s     r(   r   zModuleFrame.remap_input  s    ww$//)))==##D,,,  #&&2 ''221dkk6M6Ma6PQ''**r*   c                     g } j                   j                   j                        }|ԉ j                  |j                  D ]N  }t        |t        t        f      r)|j                   j                  |j                            Bt        d|        t         j                  t         fd|D              |j                        }t!         j                  j                   j"                  |j                        }|}ng } j$                  j'                         D ]\  }|j(                  D ]K  }|j                   j                  vs|j                  |       |j                   j$                  |           \ ^  j"                  }t+        |      dk(  r|d   }t        |t,        t.        j0                  j2                  f      sJ  j4                  j7                  |        j4                  j9                           j                  j;                          |y t+        |      dk(  r|| j                  j$                  |d   <   nXt=        |      D ]J  \  }	}
t.        j0                  j?                  |      |	   j@                  }| j                  j$                  |
<   L  jB                  !tE         jB                   j                         y y )Nz'Unsupported data type for output node: c              3   j   K   | ]*  }j                   j                  |j                         , y wri   )r   r   rF   ).0rf   rG   s     r(   	<genexpr>z/ModuleFrame.finalize_outputs.<locals>.<genexpr>  s/      ". MM$//&++">?".s   03r   r   )#r7   r   rR   r   outputsr   r   r   r   r   rF   RuntimeErrorr   r   r{   rX   r   r   r   keysusersrq   r   r   r4   r   r6   rf   lint	recompiler   ProxyrL   r   r   )rG   orig_outputsrS   rf   tree_out_node
parent_outgraph_outputs	orig_node	user_noder   orig_output	proxy_outs   `           r(   finalize_outputszModuleFrame.finalize_outputs  sc   **..txx8	 T[[%<#++f~~&FG ''(DE&A&J 	 , 0!! ".  ""M 3D(($*A*A9CUCU3J HUMM!]]//1	!*I ~~T__<$++I6%,,T]]9-EF "1 2 00J=!Q& -a 0-$)>???

-( 	

##% |!i&74>DKK  a1"+L"9;!HHNN:6q9>>	4=$$[1 #:
 ##/%d&>&>@Q@QR 0r*   c                     | j                  d|j                                | j                  j                  || j                        | j
                  |<   || j                  |j                  <   y )Ncopying)printformat_noder6   	node_copyr   r   r   rF   )rG   rL   s     r(   	copy_nodezModuleFrame.copy_node  sP    

9d..01"jj2249I9IJd%)		"r*   c                     d}| j                   j                  D ]A  }| j                  ||j                  j	                  d      |j                                |dz  }C d}| j                  |   }|j                  dk(  r5| j                  |       |dz  }| j                  |   }|j                  dk(  r5| j                  |       | j                   j                  D ]#  }|j                  dk(  s| j                  |       % y )Nr   nn_module_stackr   r/   rf   )	r   rD   r  ry   r   r  rE   r  run_from)rG   r   rL   node_idxs       r(   	run_outerzModuleFrame.run_outer  s    OO))DJJq$))--(9:D<L<L<NOFA *
 zz(#gg&NN4 MH::h'D gg&
 	h OO))Dww("t$ *r*   c                 4    | j                   rt        |i | y y ri   )r   r  )rG   rY   rZ   s      r(   r  zModuleFrame.print  s    <<4"6" r*   c                 >   d}|t        | j                        k  r|| j                  |   }|j                  dk7  sJ | j                          | j                  d||j	                                | j                  | j
                         |j                  dk(  r,t        | j
                        dk(  r|S | j                          |S d|j                  v r0|j                  d   j                         D cg c]  \  }}|	 c}}n| j
                  }|d t        | j
                         | j
                  k7  rI| j                          | j                  d| j                         | j                  | j                         |S |J t        | j
                  |      r|t        | j
                           }| j                  d|       t        | j                  | j                  | j                  | | j
                  |gz   t!        |j                  d   j#                               t        | j
                           | j$                        j'                  |      }|dz  }S|| j
                  k(  sJ | j)                  |       |dz  }|t        | j                        k  r{y y c c}}w )	Nr   r/   STEPrf   r   r  	outliningzCreating new stack frame for)rq   rD   rE   r  r  r   r   ry   valuesrR   r6   r   r   r   r   r   r   r   r7   r	  r  )rG   r
  
module_idxrL   pathtynode_module_stacknext_modules           r(   r	  zModuleFrame.run_from  sS   
TZZ(::h'D77m+++JJLJJvx)9)9);<JJt(()ww("t(()Q. $O %%' %		1 '+ii0A&B&I&I&KL&K($&KL&& 
 !!93t'8'8#9:d>O>OO %%'

;1

4::&$000**,=> 0D4E4E0FG

9;G 'OOOO%%%%5#45::<=c$BSBS>TU** (8$  a
 %(9(9999NN4 MHw TZZ(& Ms   1Jri   )r]   r^   r_   r   r   r   r3   r   r   r   r  r  r  r	  rj   r*   r(   r   r      sI     G9  %8 89G9R7
+=S~*
%*#>r*   r   
orig_graphroot_modulec                     i }i }t        | ||d dgd|j                  D ci c]%  }|j                  r|j                  |j                  ' c}|      j	                          y c c}w )NrO   )r   )r   r7   rS   rR   r  )r  r  r   r   entrys        r(   r9   r9   0  sr    +-J/1L	
 %66	
6 IIu&6	

 ! ik	
s   *A!rJ   scopec                    | j                   j                         D ]$  \  }}t        t        t        |      ||gz          & t        | t              sy| j                  }t        d |j                        }t        d |j                        }|D ])  }t        t        fd|j                              |_
        + |D ]  }|j                  vrt        |j                        dkD  r|j                     j                  d      }	|	dt        |       |k7  rZ|	t        |      d }
t        | |
      }t        |t         j"                        sJ |j%                  |      5  |j'                  ddj)                  |
            }ddd       |j+                  d	       |j-                  |        | j/                          y# 1 sw Y   @xY w)
aH  Sink params and buffers from graph inputs into get_attr nodes.

    Exported modules are purely functional, so they pass their parameters and
    buffers in as inputs to the graph.

    To replicate eager's semantics, we need to get them from the module state
    via get_attr instead.

    module: GraphModule, potentially containining nested submodules.
    inputs_to_state: mapping graph input names to the corresponding key in the state_dict.
    scope: tracks where we are in the module hierarchy, so that we can emit the
        right `getattr(self, "foo.bar")` calls, etc.
    Nc                      | j                   dk(  S )Nr/   rE   ns    r(   rm   z_sink_params.<locals>.<lambda>\  s    addm3r*   c                      | j                   dk(  S )Nr   r  r  s    r(   rm   z_sink_params.<locals>.<lambda>_  s    )>r*   c                      | j                   vS ri   )rF   )r  rJ   s    r(   rm   z_sink_params.<locals>.<lambda>a  s    166+Hr*   r   r   r   T)propagate_meta)_modulesrs   rB   r   r   r   r6   filterrD   r{   rY   rF   rq   r   r   _recursive_getattrr   r   rt   ru   r   rz   
erase_noder   )rK   rJ   r  rF   	submoduler6   r   call_module_nodesrL   
state_name	attr_path
state_attrrl   s    `           r(   rB   rB   C  s   $ "??002iT+y1?ETFNS 3 fk*LLE3U[[AF >L!&!H$))TU	 " 99O+tzz?Q(399#>J ,CJ'50"3u:<0I+FI>Jj%,,777&&t, ,,Z)9LM - &&x&E/ 0  -,s   "G  G		c                 ,    |D ]  }t        | |      }  | S ri   )r   )objr*  attrs      r(   r%  r%  ~  s    c4   Jr*   )0r   r   r   typingr   r   r   r   r   r   torch.fx._pytreer4   _pytreer   torch.utils._pytreeutilsrP   torch.exportr	   torch.export.exported_programr
   r   r   r   torch.fxr   r   r   r   r   r   boolr)   r,   rd   r5   r8   r   r   r   r   r   r   r   r   r9   rB   r%  rj   r*   r(   <module>r8     s      4 4  $ $ $ $ (  ! 4
3ll3xx3 3 	34>C-- >CBo *< 5)UXX^^ 5) 5)pR
6 6 6 66!5!5 6%((:N:N 60%((&&  R%((.. Ruxx}} RMEHH00 M%((-- M{ {|	EHHNN AUAU &88#s(^8 98vr*   