
    Vh-                     L   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	m
Z
 d dlmZ d dlmZm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mZ d dlmZ d d	lmZ d d
lm Z  erd dl!m"Z" d dl#m$Z$ d dl%m&Z& d dl'm(Z( d dl'm)Z)m*Z*m+Z+ d dl,m-Z-m.Z. d dl/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z< e*jz                  de*j|                  de*j~                  de*j                  de*j                  de*j                  diZC	 	 drdZDdej                  j                  fdZG	 	 dsdZH	 	 	 	 dtdZIdej                  j                  deJeKef   fd ZLd!eJeKef   d"ej                  j                  ddddfd#ZMd"ej                  j                  fd$ZN	 dud&eJeKeKf   d'eKd(eKd)eOfd*ZPd+eQej                  j                     ddfd,ZS	 	 dvdddd%d-d.eTe   d/ee3   d0ee<   d1eeK   d2ee:   d3ee4   d4eOddfd5ZUd6d7d8ej                  j                  deOfd9ZVd6d7d8ej                  j                  deej                  j                     fd:ZXd6d7d8ej                  j                  deOfd;ZYd6d7d8ej                  j                  deej                     fd<Z[d6d7d8ej                  j                  deOfd=Z\d6d7d8ej                  j                  deej                     fd>Z]d"ej                  j                  d?eej                  j                  geej                  j                  eOf   f   dej                  j                  fd@Z^dAeQej                  j                     deQej                  j                     fdBZ_d"ej                  j                  fdCZ`	 dwdAeQej                  j                     deej                  j                     fdDZadAeQej                  j                     debfdEZcdAeQej                  j                     deQej                  j                     fdFZddGej                  j                  dHej                  j                  ddfdIZed"ej                  j                  dej                  j                  ddfdJZgdKej                  j                  deej                  j                     fdLZhdej                  j                  dej                  fdMZjdN Zkd"ej                  j                  ddfdOZld"ej                  j                  dPddej                  j                  dQeJeKef   ddf
dRZmdSeJdTeOdeJfdUZnd"ej                  j                  dej                  j                  j4                  fdVZqedej                  j                  fdW       ZrdXdYdeOfdZZsdXdYdeOfd[ZtdXdYdeOfd\ZudXdYdeOfd]Zvdxd^Zwd_ Zxdyd`Zy ej                  dab      de{dY   fdc       Z|ddeKde{dY   fdeZ}de{dY   fdfZ~dzdgZedh        Zdiedej                  j                  deJeKeej                  ej                  j                  f   f   fdjZd.eTej                  j                     ddfdkZd.eTej                  j                     ddfdlZdm Zdn Z G do dpej                  j                        Zdq Zy){    N)Iterable)contextmanager)ismethod	Parameter)AnyCallableOptionalTYPE_CHECKINGUnion)detect_fake_mode)
FakeTensorFakeTensorMode)FunctionalTensor)#first_call_function_nn_module_stack)insert_deferred_runtime_assertsConstantAttrMap)OperatorBase)ExportedProgram)ExportGraphSignature)CustomObjArgument	InputKind
OutputKind)_deregister_pytree_flatten_specregister_pytree_flatten_spec)_deregister_pytree_node_register_pytree_nodeContextFlattenFuncFromDumpableContextFn
GetAttrKeyKeyPathkeystr
MappingKeySequenceKeyToDumpableContextFntree_flatten_with_pathUnflattenFunc p_b_c_obj_tokenreturnc                    ddl m}  |       }| j                  D ch c]7  }|j                  t        j
                  k(  r|j                  s|j                  9 }}|j                         D ]n  \  }}||v r|}	|j                  d      ^ }
}|
D ]  }t        |	|      }	 |	j                  j                  |d        t        |	||       |j                  ||       p |S c c}w )Nr   r   .)(torch._export.passes.lift_constants_passr   input_specskindr   BUFFER
persistenttargetitemssplitgetattr_bufferspopsetattradd)graph_signature	constantsmodr   constant_attrsspecnon_persistent_buffersnamevalue_modatomsattratoms                C/home/dcms/DCMS/lib/python3.12/site-packages/torch/_export/utils.py_collect_and_set_constant_attrsrL   >   s     I$&N $//99	((( 	 
 !( (e))zz# 	'D4&D	' 	$%dE"5$'( #s   <CrA   c                 X   ddl m}m} t               }| j                  j
                  D ]  }|j                  dk(  st        j                  j                  j                  | |j                        }t        |t        j                        sb|j                  |vsq|j                  |vst        j                  j                  j                  | |j                          ||| |j                  |j                  d       |j!                  |j                          | j#                          |S )Nr   )_assign_attr	_AttrKindget_attrF)torch.export.unflattenrN   rO   setgraphnodesoptorchfxgraph_module	_get_attrr7   
isinstanceTensor	_del_attrr5   r>   	recompile)rA   
state_dictrD   rN   rO   temp_registered_constantsnoder7   s           rK   _register_constants_as_buffersra   \   s     ? #		 ?77j XX**44S$++FF&%,,/ KKz1KK'==HH))33CE dkk9;K;KUS-11$++>? MMO$$    r   c                 *   | j                   D ]-  }|j                  |v st        j                  |_        d |_        / | j                  D ]G  }|j                  t        j                  k(  s!|j                  |v s0t        d|j                   d       | S )Nz	Constant z< is mutated in the forward method. Pls register it as buffer)
r3   r7   r   CONSTANT_TENSORr4   r6   output_specsr   BUFFER_MUTATIONRuntimeError)sigr_   rC   s      rK   7_override_graph_signature_for_temp_registered_constantsri   v   s      #;;33!11DI"DO#
    II33388DKK=(de  Jrb   new_sigc                 2   | j                   D ch c]7  }|j                  t        j                  k(  r|j                  s|j
                  9 }}|j                   D ]6  }|j                  t        j                  k(  s!|j
                  |v s0d|_        8 |S c c}w )NF)r3   r4   r   r5   r6   r7   )old_sigrj   rC   rD   s       rK   /_overwrite_signature_for_non_persistent_buffersrm      s     ''99	((( 	  ## $99	(((T[[<R-R#DO$ Ns   <Bc                    i }dt         j                  j                  dt        fd}| j                  j
                  D ]  }|j                  }|j                  }|j                  dk(  ry || |      }t        |t         j                  j                        rL|j                  dd      D ]  \  }}|||dz   |z   <    |j                  dd      D ]  \  }}|||dz   |z   <    |j                  d	k(  r2 || |      }t        |t         j                  j                        s|||<   |j                  d
k(  st        |j                  t         j                  j                        r%|j                   D ][  }	|	j                  d	k(  st         j                  j"                  j$                  D ]"  }
|
dk(  r	|
|v s||
   ||	j                     |
<   $ ]  |S )a  
    Param/buffer metadata needs to be saved before lowering to aten IR
    because aten IR lifts them, as a result, automatic preservation doesn't work.
    This is intended to be called on the strict mode tracing right before lowering to
    aten IR OR run_decomposition pass.
    model	attr_namec                 v    |j                  d      ^ }}| }|D ]  }t        ||d       }|J  t        ||      S )Nr1   )r9   r:   )ro   rp   prefixfieldtitems         rK   _getattrz0_collect_param_buffer_metadata.<locals>._getattr   sO    "- 	!D4&A= =	! q%  rb   call_moduleTF)recurseremove_duplicater1   rP   call_functioncustom)rV   rW   GraphModulestrrS   rT   r7   metarU   rZ   nnModulenamed_parametersnamed_buffers_opsHigherOrderOperator_input_nodesproxy_COPY_META_FIELDS)rA   params_buffers_to_node_metarv   r`   r7   r~   	submodulerE   _argentrys              rK   _collect_param_buffer_metadatar      s    #%!,, ! ! 		 "Yyy77m# f-I)UXX__5(99 5  :   LGD! HL/t0CDL
  )66 5  7   LGD! HL/t0CDL
 77j  f-Ii)=)=>6:+F3
 77o%jKK77/
 (( Y66Z'!&!A!A Y H,$ D=MQRW[7

CEJYY7"YH '&rb   r   gmc                 L   | j                         D ]&  }|j                  dd       |j                  dd       ( |j                  j                  D ]  }|j                  dk(  s|j
                  |j                  v rG|j                  |j
                     }|| v r*| |   j                         D ]  \  }}||j                  |<    |j
                  |j                  v s|j                  |j
                     }|| v s| |   j                         D ]  \  }}||j                  |<     y)zq
    Given that we collected param'buffer metadata before, we put them back in
    newly traced graph module
    nn_module_stackNstack_traceplaceholder)
valuesr<   rS   rT   rU   r7   inputs_to_parametersr8   r~   inputs_to_buffers)	r   r   rj   metadatar`   
param_namekvbuffer_names	            rK   )_populate_param_buffer_metadata_to_new_gmr      s    0668 *&-]D)*  )77m#{{g:::$99$++F
!<< ;J G M M O )1'(		!){{g777%77D"== ;K H N N P )1'(		!))rb   c                 J   | j                   j                  D cg c]-  }|j                  j                  dd       |j                  d   / }}t	        |       }||j
                  S |D ]4  }t        |t        j                        s|j                  j
                  c S  y c c}w )Nval)
rS   rT   r~   get_detect_fake_mode_from_gm	shape_envrZ   rV   SymIntr`   )r   r`   vals	fake_moder   s        rK   _get_shape_env_from_gmr      s     HHNN99==%1 			%D  *"-I""" $a&66###$s   2B Fname_map	orig_namerE   is_placeholderc                 Z   || j                         v rt        j                  d|      }|r.|s,|j                  d      t	        |j                  d            }}nd}| d|dz    x}| j                         v r"|dz  }| d|dz    x}| j                         v r"|| |<   | |   S || |<   | |   S )a  
    Renames nodes to avoid name collisions, with suffixing.
    name_map: map from original name to new name
    orig_name: mapping key
    name: candidate name (potentially suffixed, e.g. mul_2)
    is_placeholder: if the node is a placeholder, avoid detecting suffix
    z
(.*)_(\d+)      r   r   )r   rematchgroupint)r   r   rE   r   r   ndup_names          rK   _rename_without_collisionsr      s     x   -kk!nc%++a.&9!DA"V1QUG,,x1BBFA #V1QUG,,x1BB& I #Irb   input_placeholdersc                    dt         dt        fd}dd l}ddlm} ddlm} t        |      t        |       k7  r$t        dt        |        dt        |       d	      i }t        ||       D ]   \  \  }}	}
|
j                  j                  d
      }t        |t              rxt        |	t        j                        s t        d ||       dt!        |	             t        |j"                        t        |	j"                        k7  r/t        d ||       d|j"                   d|	j"                   d	      t%        t        |	j"                  |j"                              D ]  \  }\  }}t        |t        j&                        r!|j(                  j*                  j,                  sEt        |t        j&                        rt        |j(                  j*                  j.                        dk(  rt1        t3        |j(                  j*                  j.                              }||v rG|j(                  j*                  j5                  |      }||k7  rt        d ||       d| d| d|       t        |j(                  j*                  |j6                        rt9        |      ||<   ny ||j;                  |j(                  j*                  |      |      }|9t        d|
j<                   d| d| d|j(                  j*                   d| d      t9        |d         ||<   |j(                  j*                  |v s |||j(                  j*                           \  }}|dkD  r"||k  rt        d ||       d| d| d|       |t>        j@                  k  s4||kD  s;t        d ||       d| d| d|       t        |t        j&                        r"|j(                  j*                  j,                  s||k7  st        d ||       d| d| d|        t        |t8        tB        t        f      st!        |	      t!        |      k7  s|	|k7  st        d ||       d| d|	        y )Nkey_pathr/   c                     | d   }t        |t              sJ |j                  dk(  rdt        | dd        S | d   }t        |t              sJ t        |      dd }| t        | dd        S )zFor a given index into the flat_args, return a human readable string
        describing how to access it, e.g. "*args["foo"][0].bar"
        r   z*argsr   Nr   )rZ   r%   idxr#   r$   r}   )r   args_kwargs_key_path	kwarg_keyrE   s       rK   
get_keystrz6_check_input_constraints_for_graph.<locals>.get_keystr  s      ({.<<<##q(6(12,/011 Ii444y>!B'DVF8AB<0122rb   r   )_convert_range_to_int)	try_solvez&Unexpected number of inputs (expected z, got )r   zExpected input at z to be a tensor, but got z,Unexpected number of dimensions in input at z.shape (expected r   z.shape[z] to be equal to z
, but got zExpected input z] = z to be of the form z, where z is an integerr   z] to be >= z] to be <= z to be equal to )"r"   r}   sympy@torch._export.passes.add_runtime_assertions_for_constraints_passr   torch.utils._sympy.solver   lenrg   zipr~   r   rZ   r   rV   r[   typeshape	enumerater   r`   expr	is_numberfree_symbolsnextitersubsSymbolr   EqrE   mathinffloat)r   flat_args_with_pathrange_constraintsr   r   r   r   unification_mapr   r   r`   node_valjarg_dimnode_dimsymbolexisting_dimsolutionmin_valmax_vals                       rK   "_check_input_constraints_for_graphr     s{   3W 3 3"  3
3'9#::/01<O8P7QQRT
 	
 02O!$%8:L!M W399=='h
+c5<<0"(H)=(>>WX\]`XaWbc  8>>"c#))n4"B:hCWBX Y!!) 0syykD 
 +4C		8>>4R*S D&&GXw5#LL--77
 x6HMM..;;<A!$x}}'9'9'F'F"GHF0'/}}'9'9'>'>'O"l2"."4Z5I4J'RSQTTe#/.
7)!E# 
 &hmm&8&8%,,G
 7:'lOF3'0 %););W Ev(H  (/&2&5dii[s$wi X33;==3E3E2FhvhVd%f'" !"
 ;>hqk:J 7}}))->>+@-hmm.@.@A,( #Q;&0&2&8H9M8NgVWUXXc'.iz'%D'" !" #TXX-&0&2&8H9M8NgVWUXXc'.iz'%D'" !"
 x6$MM..88 (&,Z-A,B'!L]#*Jwi9 CDJ 3s"34CyDN*cXo"(H)=(>>NxjXbcfbgh kWrb   )serialized_type_nameto_dumpable_contextfrom_dumpable_contextreturn_none_fieldscls
flatten_fnunflatten_fnr   r   r   r   c          	          t        j                         s
J d         dt        dt        t        t           t
        f   ffd}dt        t           dt
        dt        f fd}dt        dt        t        t           t
        f   ffd}	n|||n|}|d u |d u z  rt        d	  d
      t         |||	||       y )Nz7Only dataclasses can be registered with this function: objr/   c                    g }g }g }t        j                  |       D ]\  }|j                  t        | |j                        }}|r#|j	                  |       |j	                  |       L|j	                  |       ^ |||gfS N)dataclassesfieldsrE   r:   append)r   	flattened
flat_names
none_namesfrE   r   r   s          rK   default_flatten_fnz=register_dataclass_as_pytree_node.<locals>.default_flatten_fn  s    	

##C( 	(AQVV 4#D"4  %!!$'!!$'	( :z222rb   r   contextc           
      n    |\  }} di t        t        ||             t         j                  |      S )N )dictr   fromkeys)r   r   r   r   r   s       rK   default_unflatten_fnz?register_dataclass_as_pytree_node.<locals>.default_unflatten_fn  s5    !(
JPT#j&12PdmmJ6OPPrb   c                      |       \  }\  }}t        ||      D cg c]  \  }}t        |      |f c}}|fS c c}}w r   r   r$   )r   r   r   _none_namesr   r   r   s         rK   default_flatten_fn_with_keyszGregister_dataclass_as_pytree_node.<locals>.default_flatten_fn_with_keys  sC    /9#,	,J/2:y/IJtq!A"JJVVJs   <z7Both to_dumpable_context and from_dumpable_context for z must be None or registered.r   flatten_with_keys_fnr   r   )	r   is_dataclassr   tuplelistr   r   
ValueErrorr   )
r   r   r   r   r   r   r   r   r   r   s
   ``    `   rK   !register_dataclass_as_pytree_noder    s    ## G	@FG 3 3d3i.@(A 3QXc] QW Q QW# W%S	78J2K W  *5;MJ#/#;<AULt#(=(EFEcU K% %
 	

 19/3rb   programr   r`   c                 F    |j                   | j                  j                  v S )zM
    Checks if the given node is a parameter within the exported program
    )rE   r?   r   r  r`   s     rK   is_paramr    s    
 99//DDDDrb   c                     t        | |      r2| j                  j                  |j                     }| j                  |   S y)z
    Returns the parameter associated with the given node in the exported program.
    Returns None if the node is not a parameter within the exported program
    N)r  r?   r   rE   r^   )r  r`   parameter_names      rK   	get_paramr    s<      00EEdiiP!!.11rb   c                 F    |j                   | j                  j                  v S )zJ
    Checks if the given node is a buffer within the exported program
    )rE   r?   r   r  s     rK   	is_bufferr
    s    
 99//AAAArb   c                     t        | |      rY| j                  j                  |j                     }|| j                  j                  v r| j
                  |   S | j                  |   S y)z
    Returns the buffer associated with the given node in the exported program.
    Returns None if the node is not a buffer within the exported program
    N)r
  r?   r   rE   rD   r@   r^   )r  r`   r   s      rK   
get_bufferr    sa     $--??		J'11HHH$$[11%%k22rb   c                 F    |j                   | j                  j                  v S )zZ
    Checks if the given node is a lifted tensor constant within the exported program
    )rE   r?   !inputs_to_lifted_tensor_constantsr  s     rK   is_lifted_tensor_constantr    s     99//QQQQrb   c                     t        | |      r2| j                  j                  |j                     }| j                  |   S y)z
    Returns the lifted tensor constant associated with the given node in the exported program.
    Returns None if the node is not a lifted tensor constant within the exported program
    N)r  r?   r  rE   r@   )r  r`   lifted_tensor_names      rK   get_lifted_tensor_constantr    sC     !$/$44VVII
   !344rb   node_call_backc                     ddl m} i d}| j                  j                  D ]  } ||      r|dz  }||<     || | fddd      }| j                  j                  |j                  _        |j                          |S )a;  
    sequential_split creates a new graph module that splits the input graph module into multiple submodules
    based on the node_call_back. It doesn't mutate the input graph module. The node_call_back should return
    True if the node is a delimiter.  Delimiter will be the first node in the next submodule.
    r   )split_moduler   c                     |    S r   r   )r`   	split_maps    rK   <lambda>z"sequential_split.<locals>.<lambda>5  s    Yt_ rb   T)keep_original_orderkeep_original_node_name)torch.fx.passes.split_moduler  rS   rT   _codegenr]   )r   r  r  split_idr`   new_gmr  s         @rK   sequential_splitr     s     :IH #$MH"	$#
 

$  $F HH--FLL
Mrb   rT   c                 >    | D cg c]  } ||      s| c}S c c}w )z:Returns the nodes that match the node_call_back as a list.r   rT   r  r`   s      rK   nodes_filterr"  ?  s    ";TnT&:D;;;s   c           	      v   ddl m}m} ddlm} t
        j                  j                  j                  s[d} || t        j                  ||            5  t        |       }|r%t        | |dt        | j                         d       d d d        | j                           ||       |_        | |fS # 1 sw Y   *xY w)	Nr   )_node_metadata_hook_set_node_metadata_hook)_graph_output_nameszUFile "torch/fx/passes/runtime_assert.py", line 24, in insert_deferred_runtime_asserts)r   zexported program: T)export)(torch._export.passes._node_metadata_hookr$  r%  4torch._functorch._aot_autograd.input_output_analysisr&  rV   _dynamoconfigdo_not_emit_runtime_asserts	functoolspartialr   r   r   rS   r]   user_outputs)r   r?   r$  r%  r&  r   r   s          rK   apply_runtime_assertion_passr0  D  s     Y==;;1 	 %	!!"5;O
 
	 /r2I/()LRXX)V(WX	
	 LLN#6r#:O 
	 
	s   3B//B8c                 L    t        | |r|nd       }t        |      dkD  r|d   S y)z
    Returns the first node that matches the node_call_back. If no node matches, returns None.
    When node_call_back is None, returns the first node in the node list.
    c                      yNTr   r`   s    rK   r  znodes_first.<locals>.<lambda>h  s    rb   r   N)r"  r   )rT   r  rets      rK   nodes_firstr6  a  s,     unDU
VC
3x!|1vrb   c                 ,    t        t        | |            S )z:Returns the number of nodes that match the node_call_back.)r   r"  )rT   r  s     rK   nodes_countr8  n  s    |E>233rb   c                 $    | D ]
  } ||        | S )z
    Sequentially visit the nodes list and invoke node_call_back on each element.
    Returns the nodes list after the node_call_back is invoked on each element.
    r   r!  s      rK   	nodes_mapr:  s  s     
  tLrb   old_nodenew_nodec                     | j                  |       | j                  j                          | j                  j	                  |        y)z5
    Replace all uses of old_node with new_node.
    N)replace_all_uses_withusersclearrS   
erase_node)r;  r<  s     rK   node_replace_rB  }  s4     ""8,NNNNh'rb   c                     t        |t        j                  j                        rGt	        |d      r:d|j
                  v r+| j
                  j                  d|j
                  d   i       y y y y )Nr~   r{   )rZ   rV   rW   r|   hasattrr~   update)r   rA   s     rK   _update_gm_meta_if_possiblerF    sY    3,,-C  
#((8"456 ! ! 	.rb   call_mod_nodec           
         | j                   dk(  sJ | j                  j                  }|J t        | j                  t
              sJ t        || j                        }d |j                  j                  D        }d |j                  j                  D        }|j                  j                  D cg c]  }|j                   dk(  s| }}t        || j                        D ]7  \  }}t        |t        j                  j                        sJ t        ||       9 |j                  j                  |       5  |D ]  }|j                  j                  |      }	|j                   dk(  rt|	j                  }
t!        ||
      r*d}d| }
t!        ||
      r|dz  }d| }
t!        ||
      r|
|	_        t#        ||	j                  t        ||j                               t        ||	        t%        |      d	kD  r4t%        |      dk(  rt%        |d	   j                        dk(  sJ |d	   j                  d	   t        t        j                  j                        r'j&                  j)                          t        |        nt        t*        t,        f      r~D ]   }|j&                  j/                  |d	          " t1        t+        | j&                  j3                               d
       }t5        |fd       | j                  j7                  |        n3t9        dt;               d      | j                  j7                  |        ddd       |j=                          |j?                          |S c c}w # 1 sw Y   0xY w)z
    Inline the submodule of the given node into the parent module.
    Note: we only support the case where submodule takes tensors inputs.
    rw   Nc              3   @   K   | ]  }|j                   d k(  s|  yw)r   NrU   .0r`   s     rK   	<genexpr>znode_inline_.<locals>.<genexpr>  s     
KD$'']2J4
Ks   c              3   >   K   | ]  }|j                   d vs|  yw))r   outputNrJ  rK  s     rK   rM  znode_inline_.<locals>.<genexpr>  s!      tww>W/Ws   rO  rP   r   submod_r   c                 ^    | j                   dk(  xr | j                  t        j                  k(  S )Nrz   )rU   r7   operatorgetitemr4  s    rK   r  znode_inline_.<locals>.<lambda>  s)    O!; "8x'7'77 rb   c                 <    t        | | j                  d            S Nr   )rB  args)get_item_node
new_outputs    rK   r  znode_inline_.<locals>.<lambda>  s"    -%"=#5#5a#89+ rb   zUnsupported output type z2. Expect it to be a Node or a list/tuple of Nodes.) rU   rS   owning_modulerZ   r7   r}   r:   rT   r   rV  rV   rW   NoderB  inserting_before	node_copyrD  r=   r   r?  r@  r   r   r<   r"  keysr:  rA  NotImplementedErrorr   delete_all_unused_submodulesr]   )rG  r   sub_gmphsbodyr`   rO  phr   r<  new_target_nameiget_item_usersrX  s                @rK   node_inline_rg    s   
 },,,				*	*B>>m**C000R--.F
KFLL..
KC++D  &||11ItTWW5HdIFIsM../ C#uxx}}---b# 
	"	"=	1 2: 	*Dxx))$/Hww*$"*//2/A(/smO!"o6Q,3A3- ""o6 #2HOOWVT[[-IJ$)	* v;?v;!#F1INN(;q(@@@*J*ehhmm4   &&(mZ8Ju6& .DJJNN6!9-. ".,,11348" " ##..}=).tJ/?.@@rs  **=9e2:h ##%LLNIy J2: 2:s    1M1M17A1M6)FM66M?c                    t        j                  | j                        }|j                  d   }dt        j
                  i}g }|j                         D ]d  \  }}t        |j                  |      D cg c]  }|j                   }}|D ]-  }	|	dk(  r	|j                  t        j                  |	|             / f t        j                  |      S c c}w )z
    Get source code and parse argument names using AST. The function returns
    a signature of the forward() function.

    # TODO: Directly provide inspect.signature compatible TS-d module.
    r   rV  self)
parameters)astparsecoderb  r   POSITIONAL_OR_KEYWORDr8   r:   rV  r   r   inspect	Signature)
rA   ast_modast_func_defarg_type_map
param_listarg_type
param_typeaarg_name_listarg_names
             rK   &_get_torch_jit_trace_forward_signaturerz    s     ii!G$+LLOL I;;<L J , 2 2 4 G*(/0A0A8(LM1MM% 	GH6!g//*EF	GG 
33 Ns   1Cc                 t   t        | t        j                  j                  t        j                  j                  f      r?t        |       }t        |j                        t        |      t        |      z   k(  s&J d       t        j                  | j                        }i  |j                  | j                  |S )NzyArguments other than POSITIONAL_OR_KEYWORD kinds in forward() are not supported in _get_torch_jit_trace_forward_signature)rZ   rV   jitScriptModuleTracedModulerz  r   rj  ro  	signatureforwardbind_partial	arguments)rA   	fake_argsfake_kwargsrh   s       rK   _bind_signature_to_inputsr    s    #		..		0F0FGH4S9 3>>"c)ns;7G&GG 	
J	
G
 , Ec	*44DDDrb   c                    g }| j                   j                  D ]p  }|j                  dk(  st        |j                  t
        j                  j                        sC|j                  j                  dk(  r`|j                  \  }}}}|j                  t        | |j                        |f       |j                  t        | |j                        |f       |j                  j                  dk(  rG|j                  d   |j                  dd }}|j                  t        | |j                        |f       |j                  j                  dk(  s7|j                  \  }	}
}|j                  t        | |	j                        |
|z   f       s |D ]  \  }}i }t        |j                   j                        D ]q  \  }}|t        |      k  r8||   j                  ||j                  <   ||   j                  x|_        |_        Lt        ||j                  |j                        |_        s t!        |       |j#                           y)a0  
    Propagate placeholder names from the top-level graph into HigherOrderOp subgraphs,
    and handle collisions with non-placeholders by count suffixing.
    Different HOO subgraph types have different input schemas, so we first enumerate them
    and gather the top-level named placeholder nodes.
    rz   condwrap_with_set_grad_enabledr   r   Nmap_impl)rS   rT   rU   rZ   r7   rV   r   r   _name_argsr   r:   r   r   rE   r   _name_hoo_subgraph_placeholdersr]   )r   subgraph_ph_tuplesr`   r   
true_graphfalse_graph	cond_argssubgraphra  
body_grapharrayrV  hoo_phsr   re  s                  rK   r  r  
  s    RT 77o%*KK77+
 {{  F*8<

5:{I"))72z7H7H+I9*UV"))72{7I7I+JI*VW""&BB $

1tzz!"~#"))72x+G*MN""j0*.**'
E4"))R!2!23UT\B& 0 '#% !5!56 	WGAt3w<&-ajoo#*1!*//9	DK6xDIIV		W 	(1rb   export_graph_signaturer@   c           
         d }d i }t        |||      }	t        |	      \  }
}|j                  D cg c]5  }|j                  t        j
                  k(  r|j                  j                  7 }}t        |
|      D ]J  \  \  }}}|st        ||t        t        j
                     dj                  fd|D              z   d       L |j                  D ]  }|j                  t        j
                  k(  r!|j                  t        j                  k(  rd}n  ||j                        j                         }t        j                   dd|      }t        ||j                  j                  t        |j                     |z   d        | j"                  j$                  D ]3  }|j&                  d	k(  rt        ||j                  |j                         5 | j"                  j$                  D ]  }|j&                  d	k(  rh|j                  |v sJ ||j                     x|_        |_        t)        |j*                  d
   t,              s[|j                  |j*                  d
   _        z|j                  |v s||j                     |_         t/        |        | j1                          |j                  D ]  }|j                  j                  |v sJ ||j                  j                     |j                  _        |j                  t        j2                  k(  sc|j                  |v sr||j                     dd |_         |j4                  D ]  }|j                  j                  |v r(||j                  j                     |j                  _        |j                  t6        j8                  k(  sa|j                  |v sp||j                     |_         t;        |j=                               D ]o  }||   }||v st)        |t>        j@                        r(||   }||k7  s3t        jB                  d|      sJ|t        t        j2                     |z   k7  sh|||<   ||= q yc c}w )aQ  
    This pass is run at the end of _export_non_strict() to assign better placeholder node names:
        - User inputs:
            These follow the signature of mod.forward(), e.g. forward(x, y) produces nodes x, y.
            For nested inputs from dictionaries, lists, tuples, or dataclasses,
            the names are a concatenation of the path to the tensor.
                e.g. x = {
                    'a': torch.randn(),
                    'b': [torch.randn(), torch.randn()]
                }
            produces nodes x_a, x_b_0, x_b_1.
        - Parameters/buffers/constants/custom objects:
            These follow the FQN of the object, prefixed by "p", "b", "c", "obj" respectively.
                e.g. self.bar.l0.weight produces "p_bar_l0_weight".
        - Effect tokens:
            These are named token, token_1, ...
    c                     | j                  d      r| t        d      d  } n| j                  d      r| t        d      d  } t        j                  dd|       } | S )N
L__self___self_[^a-zA-Z0-9]r   )
startswithr   r   subxs    rK   _strip_namez,placeholder_naming_pass.<locals>._strip_nameO  sS    <<%#l#%&A\\'"#g,.!AFF?C+rb   c                 0   t        | t              r,t        j                  ddt	        | j
                              } | S t        | t              rt	        | j                        S t        | t              r| j                  S t        dt        |        d|        )Nr  r   zPytree key of type z not handled for )rZ   r$   r   r  r}   keyr%   r   r!   rE   rg   r   r  s    rK   _extract_pytree_keyz4placeholder_naming_pass.<locals>._extract_pytree_keyW  st    a$SZ8AH;'quu::&66M!4T!WI=NqcRSSrb   r   c              3   J   K   | ]  } |      j                           y wr   )lower)rL  r  r  s     rK   rM  z*placeholder_naming_pass.<locals>.<genexpr>u  s      La.q1779Ls    #T)r   r)   r  r   r      Nz
arg(\d+)_1)"r  r'   r3   r4   r   
USER_INPUTr   rE   r   r   placeholder_prefixesjoinTOKENr7   r  r   r  rS   rT   rU   rZ   r~   r   r  r]   
CUSTOM_OBJre   r   USER_INPUT_MUTATIONr   r]  rV   r[   r   )r   r  rA   r  r  fake_params_buffersr@   r  r   combined_argsr   r   rC   user_input_namesarg_path_arguser_input_name	base_namer`   rE   constantnew_namer  s                         @rK   placeholder_naming_passr  4  s   6	T  "H .c9kJM3MB +6699	,,, 	  .11DFV-W )4/&$Y%9%9:((L8LLM# '22 
99	,,,99	'I#DKK0668IFF?C;	"HHMM +i7		

(  C77m#"8TYY		BC  	,77m#99(((&.tyy&99DI $))E*,=>(,				% %YY(" +DI	, $B' LLN '22 4xx}}((( /II---$++2I"4;;/3DK4 '33 088==H$$TXX]]3DHHM99
6664;;(;R"4;;/DK	0 Y^^%& $T?8Jell%
  ~HD HH]D1 4Y5I5I JT QQ&.	(#dO$]s   :Qr^   in_placec                    |r6| j                         D ]!  \  }}t        |d      st        | |   d       # | S i }| j                         D ]8  \  }}t        |d      r"|j                         j	                         ||<   4|||<   : |S )z
    If `in_place` is false, return a new copy of `state_dict` with "proxy" removed from `v.__dict__`.
    `v` is the values in the dictionary.
    If `in_place` is true, modify `state_dict` in place.
    r   )r8   rD  delattrdetachclone)r^   r  r   r   new_state_dicts        rK   remove_proxy_from_state_dictr    s     $$& 	0DAqq'"
1w/	0 $$& 	&DAqq'"$%HHJ$4$4$6q!$%q!		&
 rb   c                 D   g }g }| j                   j                  D ]  }|j                  dk(  rMd|j                  v r?|j                  d   }|2t	        |t
        j                        sM|j                  |       _t        |      dk(  snd|j                  v sd|j                  v sd}d|j                  v r|j                  d   }nd|j                  v r|j                  d   }|t	        |t
        j                        s|j                  |        t        ||z         S )a  
    For a given graph module, we look at the "val" of placeholder nodes to find the fake inputs.
    Additionally, if gm doesn't have placeholders, we further look at the "example_value" or "val" of other nodes.
    If no fake mode is found, we return None for fake_mode.
    r   r   Nr   example_value)
rS   rT   rU   r~   rZ   rV   r[   r   r   r   )r   	fake_inps	fake_valsr`   fake_vals        rK   r   r     s     %'I$&I +77m#(:yy'H#
8U\\(J  *^q tyy(ETYY,>H$))+99_5$))#99U+#
8U\\(J  *+  I	122rb   c              #     K   t        | j                        }t        | j                        }| j                  j                          | j                  j                          	 d  || _        || _        y # || _        || _        w xY wwr   )r   _state_dict_hooks_state_dict_pre_hooksr@  )rA   state_dict_hooksstate_dict_pre_hookss      rK   _disable_load_state_dict_hooksr    sw     ,01F1F,G04S5N5N0O!##%9 0$8! !1$8!s   AB"A5 &B5BBrU   r   c                    t         j                  j                  | j                         t         j                  j                  j
                        xs0 t         j                  j                  j
                  | j                  v S r   )rV   _C%_dispatch_has_kernel_for_dispatch_keyrE   DispatchKeyCompositeImplicitAutograd
py_kernelsrJ  s    rK   
_is_cia_opr    sX    66GGIuxx++EE	
 	K 8899R]]J	rb   c                 2    t        |       xr t        |       S r   )_check_valid_to_preserver  rJ  s    rK   _is_preservable_cia_opr    s    #B':JrN:rb   c                 L    | j                         j                  d      d   dk(  S )N::r   aten)rE   r9   rJ  s    rK   _is_aten_opr    s!    779??4 #v--rb   c                     t        |        S r   )r  rJ  s    rK   _is_custom_opr    s    2rb   c            	      x   t         j                  j                  d      } | D ]  }t        |j	                  d            \  }}|j	                  d      }t        |      dk(  st        |      dk(  sJ |d   }d}t        |      dk(  r|d   }t        t        t        t         j                  |      |      |      } y)	z
    Utility function to query C++ dispatcher to get the all
    possible CIA ops and populate them into torch.ops namespace
    r  r  r1   r   r   r   defaultN)rV   r  ,_dispatch_get_registrations_for_dispatch_keyr   r9   r   r:   ops)cia_opsrU   	namespaceop_name
split_listop_overload_namer   s          rK   _materialize_cpp_cia_opsr    s    
 hhCC#G
  
W"288D>2	7]]3'
:!#s:!';;;Q-$z?a)!}GGEIIy97CEUV
Wrb   c                      t         S )z]
    This is an special marker that tells our infra that we shouldn't decompose this op.
    )NotImplemented)rV  kwargss     rK   _special_op_to_preserve_ciar  3  s
     rb   c                 |   ddl m}  ||       ry| t        j                  v ryt	        | d      syt        | j                  j                  D cg c]  }|j                  | c}      }|dk7  xs | j                  j                  }|ryt        j                  j                  | j                               syyc c}w )Nr   )#_should_decompose_because_unsafe_opF_schemaT)torch._decompr  r   metadata_fnsrD  r   r  r  
alias_info
is_mutablerV   r  _dispatch_has_kernelrE   )op_overloadr  re  r  is_mutating_or_aliasings        rK   r  r  >  s    A*;7&333;	*''11NqQ\\5MNJ )AoO1D1D1O1O88(()9)9);< 	Os   B9B9r   )maxsizec                      t        d      S )Nr  )(_collect_all_valid_cia_ops_for_namespacer   rb   rK   -_collect_all_valid_cia_ops_for_aten_namespacer  X  s    3F;;rb   r  c                 <   t                t        t        j                  |       sJ t	        t        j                  |       }t               }|D ]L  }t	        ||      }|j                         D ]+  }t	        ||      }t        |      s|j                  |       - N |S r   )	r  rD  rV   r  r:   rR   	overloadsr  r>   )r  op_namespacer  rU   	op_packetoverloadr  s          rK   r  r  ]  s     599i(((599i0LeG )L"-	!++- 	)H!)X6K%k2K(	)) Nrb   c                      t               } t        j                  j                  D ]#  }|dk7  r| t	        |      z  } | t               z  } % | S )a  
    This is an util function that gets the all CIA functional ops.

    The algorithm is in 2 steps:
      1. We first query C++ dispatcher to get the list of CIA ops
         and then we call getattr on torch.ops.aten to lazily populate
         them.

      2. Sometimes, handful of ops have CIA registered in python dispatcher
         but not on the C++ side, these can't be caught at the first step.
         So we walk again to get the final list.

    Note that the output of this function should never be modified
    r  )rR   rV   r  _dirr  r  )r  op_namespace_names     rK   _collect_all_valid_cia_opsr  n  sS     eG"YY^^ G&?@QRRGDFFGG Nrb   c                    t         j                  j                  j                  }|| j                  v r@t        | j                  |   t         j                  j                        s| j                  |   S d }t        j                  ||       S )Nc                  D   |d   }|d= t         j                  j                  j                  }t         j                  j	                  |j                         t         j                  j                  j                        r |j                  |g| i |S t        d| d      )Nkernel	Expected z) to have CompositeImplicitAutograd kernel)rV   r  r  r  r  rE   _op_dkAssertionError)rV  r  r  dks       rK   _special_op_to_decompose_ciaz9_get_decomp_for_cia.<locals>._special_op_to_decompose_cia  s    !8 XX!!;;8899KKM588//II
 !6==5d5f55 F8#LM rb   )r  )rV   r  r  r  r  rZ   r-  r.  )rU   r  r  s      rK   _get_decomp_for_ciar    sk     
			7	7B	R]]:bmmB.?AUAU#V}}R   9"EErb   c               #     K   t         j                  j                  } t         j                  j                  }	 dt         j                  _        dt         j                  _        d  | t         j                  _        |t         j                  _        y # | t         j                  _        |t         j                  _        w xY wwr3  )rV   compiler_is_compiling_flag_is_exporting_flag)old_compiling_flagold_exporting_flags     rK   _compiling_state_contextr    sy     ::::?,0),0),>),>) -?),>)s   5C .B &+C ,B==C r   c                 2   i t        |j                  d            t        |j                  d            }i }i }|j                         D ]G  \  }}t	        |      |v r|t	        |         }n!| j                  |d      }||t	        |      <   |||<   I |S )NF)ry   T)static_shapes)r   r   r   r8   idfrom_tensor)r   rA   params_buffersfaked_params_buffersmemor  rF   fake_tensors           rK   _fakify_params_buffersr    s    
s##U#;
<
s  % 8
9N
 "$D$**, 0
Ue9r%y/K#//T/JK)DEO$/S!0  rb   c           	         
 t         t        j                  j                        sJ ddl} G 
 fdd|j
                        
dt        dt        t        t           t        f   f
fd}dt        t           d	t        dt        ffd
}dt        dt        t        t           t        f   ffd}||} j                  dz    j                  z   }d }
fd}t         |||||       dt        t           ffd}	t         |	       y)aB  
    Registers a module as a valid input type for :func:`torch.export.export`.

    Args:
        mod: the module instance
        serialized_type_name: The serialized name for the module. This is
        required if you want to serialize the pytree TreeSpec containing this
        module.

    Example::

        import torch

        class Module(torch.nn.Module):
            def __init__(self):
                super().__init__()
                self.linear = torch.nn.Linear(3, 3)

            def forward(self, x):
                return self.linear(x)

        torch._export.utils.register_module_as_pytree_node(InputDataClass)

        class Mod(torch.nn.Module):
            def forward(self, x, m):
                return m(x) + x

        ep = torch.export.export(Mod(), (torch.randn(3), Module()))
        print(ep)

    r   Nc                   2     e Zd Z fdZd ZfdZ xZS )=register_module_as_pytree_input_node.<locals>.PrototypeModulec                     t        |   |g|i | t        |t        j                  j
                        sJ t        | d      rJ | _        y )N
_proto_cls)super__init__rZ   rV   r   r   rD  r  )ri  mrV  r  	__class__r   s       rK   r  zFregister_module_as_pytree_input_node.<locals>.PrototypeModule.__init__  sI    GQ000a111t\222!DOrb   c                 4    | j                   |j                   k(  S r   )r  )ri  others     rK   __eq__zDregister_module_as_pytree_input_node.<locals>.PrototypeModule.__eq__  s    ??e&6&666rb   c                       |              S r   r   )ri  r  PrototypeModules     rK   __deepcopy__zJregister_module_as_pytree_input_node.<locals>.PrototypeModule.__deepcopy__  s    "46**rb   )__name__
__module____qualname__r  r!  r$  __classcell__)r  r#  r   s   @rK   r#  r    s    	"	7	+ 	+rb   r#  r   r/   c                     t        | j                               }t        | j                               }i ||}t        |j	                               t        |j                                |       gfS r   )r   r   r   r   r   r]  )r   r   r   r  r#  s       rK   r   z@register_module_as_pytree_input_node.<locals>.default_flatten_fn  ss     4 4 67S..01>,>>N))+,$$&'C /
 
 	
rb   r   r   c           	         |\  }}| |       t        d       |       }	J  	|      \  }}dt        j                  j                  ffdt	        d t        | |      D              r]t        j                  j                  j                  j                  |t        t        ||             dd      5   |      }d d d        |S |}|S # 1 sw Y   S xY w)Nz!Module has been garbage collectedrA   c                 F   t        j                   |       }| j                  j                         D ci c].  \  }}t        j                   |      t        j                   |      0 c}}|_        |j                         D ]  \  }}t	        || |              |S c c}}w r   )copy__dict__r8   named_childrenr=   )rA   r5  r   r   rE   childcopy_modules         rK   r0  zWregister_module_as_pytree_input_node.<locals>.default_unflatten_fn.<locals>.copy_module  s    ))C.CCF<<CUCUCWX41aDIIaL$))A,6XCL"113 7eT;u#567J Ys   3Bc              3   *   K   | ]  \  }}||u  y wr   r   )rL  r   os      rK   rM  zUregister_module_as_pytree_input_node.<locals>.default_unflatten_fn.<locals>.<genexpr>  s     =daqz=s   T)tie_weightsstrict)
rg   rV   r   r   anyr   utils	stateless_reparametrize_moduler   )
r   r   r   refr   r   r   r5  r0  r   s
           @rK   r   zBregister_module_as_pytree_input_node.<locals>.default_unflatten_fn   s    !
C;#%-BCCe%%%!#	1	UXX__ 	 =c&)&<==))??T#j&12T @  ' "#&' 
 C
' 
s   8	CCc                      |       \  }^}}t        ||      D cg c]  \  }}t        |      |f c}}|g|fS c c}}w r   r   )r   r   r   rV  r   r   r   s         rK   r   zJregister_module_as_pytree_input_node.<locals>.default_flatten_fn_with_keys  sW    )3C&	&J/2:y/IJtq!A"JM
M
 
 	
Js   >r1   c                 T    | ^}}t        j                  |gd gt        |      z        S r   )jsondumpsr   )r   r]  r   s      rK   r   zAregister_module_as_pytree_input_node.<locals>.to_dumpable_context(  s,    qzz44D6CF?455rb   c                     t        j                  |       } t        j                  j	                               |d<   |S rU  )r<  loadsrV   r   r   )dumpablesr#  s     rK   r   zCregister_module_as_pytree_input_node.<locals>.from_dumpable_context,  s/    JJx uxx01!rb   r   c                 @     |       \  }}||j                   k(  sJ |S r   )r   )r   rC   flatsr   r   s       rK   default_flatten_fn_speczEregister_module_as_pytree_input_node.<locals>.default_flatten_fn_spec;  s'    #Cw$,,&&&rb   )
issubclassrV   r   r   weakrefr9  r   r   r   r   r   r&  r'  r   r   )r   rF  r   r   r   r   r   r   r   rD  r#  r   s   `         @@rK   $register_module_as_pytree_input_noderG    s    @ c588??++++'++ +
 
d3i.@(A 
Xc] W  8
# 
%S	78J2K 
 $J'L>>C/#2B2BB6
 19/3d3i 
 !rb   c                 0    t        |        t        |        y r   )r   r   )r   s    rK   &deregister_module_as_pytree_input_noderI  F  s    C #C(rb   c                    t        | t        j                  j                        sJ d|  d       t        |t        j                  j                        sJ d| d       | j                  |_        | j
                  |_        y )Nr   z to be a nn.Module)rZ   rV   r   r   _parametersr;   )srcdsts     rK   _sync_staterN  K  s     + 
3%)*+   + 
3%)*+  ooCO<<CLrb   c                  8    | r| ^}}|D ]  }t        ||        yy)z
    Sync state between exported modules corresponding to wrapped methods.
    This might be necessary after serializing/deserializing due to copying.
    N)rN  )wrapped_method_modulesr  other_msother_ms       rK   
sync_staterS  \  s.    
 -H 	$G7#	$ rb   c                        e Zd Z fdZ xZS )_WrappedMethodc                 \    t         |           t        |j                  |        || _        y r   )r  r  rN  __self__r  )ri  methodr  s     rK   r  z_WrappedMethod.__init__h  s#    FOOT*rb   )r%  r&  r'  r  r(  )r  s   @rK   rU  rU  g  s     rb   rU  c                 D    t        |       sJ d|  d       t        |       S )z
    Wrap a method as a module so that it can be exported.
    The wrapped module's forward points to the method, and
    the method's original module state is shared.
    r   z to be a method)r   rU  )rX  s    rK   wrap_methodrZ  p  s7      +	6(/*+  &!!rb   )r/   r   )rh   r   )rl   r   rj   r   )F)NNr   )r/   N)r  r   )rU   r   )rk  r,  r   r-  ro  r<  r   rR  r   collections.abcr   
contextlibr   r   r   typingr   r   r	   r
   r   rV   torch._guardsr   torch._subclasses.fake_tensorr   r   #torch._subclasses.functional_tensorr   torch.fx._utilsr   torch.fx.passes.runtime_assertr   r2   r   
torch._opsr   torch.exportr   torch.export.graph_signaturer   r   r   r   torch.fx._pytreer   r   torch.utils._pytreer   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r  	PARAMETERr5   rd   r  r  r  rL   rW   r|   ra   ri   rm   r   r}   r   r   r   boolr   r   rZ  r   r   r  r  r   r  r
  r[   r  r  r  r  r"  r0  r6  r   r8  r:  rB  r   rF  rg  rp  rz  r  r  r  r  _subclassesr  r   r  r  r  r  r  r  r  r  	lru_cacherR   r  r  r  r  r  r  rG  rI  rN  rS  rU  rZ  r   rb   rK   <module>rl     s   
        	 $ % ' @ @  * D @ ? J H',A Q Q   $ "dt&OOW <%			%4	(#.D 6'(<(< 6'c3h 6'r)!%c3h)) $) 
	)8$uxx33 $& !	38n  	:{UXX]]+{	{@ )-,04
 +/9==A$4	c4%4 =)4
 #3-4 ""564 $$9:4 4 
4nE' Euxx}} E E
((-- ehh  ! B( B B$ B
((-- ell&RR
((--R 
R
((-- ell$ehhmm_eEHHMM44G.HHI XX><UXX]]+ <UXX]]@S <
UXX%9%9 < 04

ehhmm
4tEHHMM* 4s 4
T%((--( T%((--=P (EHHMM (UXX]] (t (7EHH$8$8 7uxx 7SW 7L L(588;O;O2P L^4 4GDUDU 42E&'(<(< ' 'TN$N$2N$ 
N$ CH~N$ 
N$bT T d *33
""113< 	9 	9 	9> d ;~ ;$ ;.N .t .n  W.4 Q<s>7J <  < N@S "C$7 2F: 	? 	?  	  
#uU\\588#5#556
67 *}d588??.C } }@)UXX__0E )$ )
 "$UXX__ 	"rb   