
    קg)                        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m	Z	m
Z
mZmZmZmZmZ d dl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mZ d d
lmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z) ej*        dej+        dej,        dej-        dej.        dej/        diZ0	 	 dMdZ1	 	 	 	 dNdZ2dej3        j4        de	e5ef         fdZ6de	e5ef         dej3        j4        ddddfdZ7dej3        j4        fdZ8	 dOde	e5e5f         d e5d!e5d"e9fd#Z:d$eej3        j;                 fd%Z<	 	 dPddddd&d'ee         d(ee          d)ee)         d*ee5         d+ee'         d,ee!         d-e9ddfd.Z=d/d0d1ej3        j;        de9fd2Z>d/d0d1ej3        j;        deej?        j                 fd3Z@d/d0d1ej3        j;        de9fd4ZAd/d0d1ej3        j;        deejB                 fd5ZCd/d0d1ej3        j;        de9fd6ZDd/d0d1ej3        j;        deejB                 fd7ZEdej3        j4        dej3        j4        fd8ZFd9eej3        j;                 deej3        j;                 fd:ZG	 dQd9eej3        j;                 deej3        j;                 fd;ZHd9eej3        j;                 deIfd<ZJd9eej3        j;                 deej3        j;                 fd=ZKd>ej3        j;        d?ej3        j;        ddfd@ZLdAej3        j;        ddfdBZMdej?        jN        fdCZOdD ZPdej3        j4        ddfdEZQdej3        j4        dFddej?        jN        dGe	e5ef         ddf
dHZRdIe	dJe9de	fdKZSdej3        j4        dejT        jU        jV        fdLZWdS )R    N)	Parameter)AnyDictIterableListOptionalTupleTypeTYPE_CHECKING)detect_fake_mode)
FakeTensorConstantAttrMap)ExportedProgram)ExportGraphSignature)	InputKind
OutputKind)_register_pytree_nodeContextFlattenFuncFromDumpableContextFn
GetAttrKeyKeyPathkeystr
MappingKeySequenceKeyToDumpableContextFntree_flatten_with_pathUnflattenFunc p_b_c_obj_tokenreturnr   c                 h   ddl m}  |            }d | j        D             }|                                D ]{\  }}||v r
|}|                    d          ^ }	}
|	D ]}t          ||          }|j                            |
d            t          ||
|           |	                    ||           ||S )Nr   r   c                 T    h | ]%}|j         t          j        k    |j        |j        &S  kindr   BUFFER
persistenttarget.0specs     O/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/_export/utils.py	<setcomp>z2_collect_and_set_constant_attrs.<locals>.<setcomp>:   ;       9	(((( 	(((    .)
(torch._export.passes.lift_constants_passr   input_specsitemssplitgetattr_bufferspopsetattradd)graph_signature	constantsmodr   constant_attrsnon_persistent_buffersnamevalue_modatomsattratoms               r2   _collect_and_set_constant_attrsrK   0   s     IHHHHH$_&&N #/  
 !(( ( (e)))zz# 	' 	'D4&&DD$%%%dE"""5$''''r5   old_sigr   new_sigc                     d | j         D             }|j         D ]'}|j        t          j        k    r|j        |v rd|_        (|S )Nc                 T    h | ]%}|j         t          j        k    |j        |j        &S r)   r*   r/   s     r2   r3   zB_overwrite_signature_for_non_persistent_buffers.<locals>.<setcomp>R   r4   r5   F)r8   r+   r   r,   r.   r-   )rL   rM   rD   r1   s       r2   /_overwrite_signature_for_non_persistent_buffersrP   N   sb     '   # $ $9	(((T[<R-R-R#DONr5   rB   c                    i }dt           j        j        dt          fd}| j        j        D ]I}|j        }|j        }|j        dk    r{ || |          }t          |t           j
        j                  rP|                    dd          D ]\  }}|||dz   |z   <   |                    dd          D ]\  }}|||dz   |z   <   |j        d	k    r0 || |          }t          |t           j        j                  s|||<   |j        d
k    rlt          |j        t           j        j                  sH|j        D ]@}	|	j        d	k    r3t           j        j        j        D ]}
|
|v r||
         ||	j                 |
<   AK|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                     |                     d          ^ }}| }|D ]}t          ||d           }|J t          ||          S )Nr6   )r:   r;   )rR   rS   prefixfieldtitems         r2   _getattrz0_collect_param_buffer_metadata.<locals>._getattrg   sW    "-- 	! 	!D4&&A====q%   r5   call_moduleTF)recurseremove_duplicater6   get_attrcall_function)torchfxGraphModulestrgraphnodesr.   metaop
isinstancennModulenamed_parametersnamed_buffers_opsHigherOrderOperator_input_nodesproxy_COPY_META_FIELDS)rB   params_buffers_to_node_metarY   noder.   re   	submodulerE   _argentrys              r2   _collect_param_buffer_metadatarw   ^   s    #%!, ! ! ! ! ! 	 Y Yy7m## f--I)UX_55 	L(99 5  :     L LGD! HL/t0CDD(66 5  7     L LGD! HL/t0CDD7j   f--Ii)=>> ;6:+F3
 7o%%jK7/
 /
% ( Y Y6Z''!&!A Y Y D==MQRW[7
CEJ&&r5   rq   gmc                    |                                  D ].}|                    dd           |                    dd           /|j        j        D ]}|j        dk    r|j        |j        v r@|j        |j                 }|| v r*| |                                         D ]\  }}||j        |<   |j        |j	        v r@|j	        |j                 }|| v r*| |                                         D ]\  }}||j        |<   dS )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=   rc   rd   rf   r.   inputs_to_parametersr9   re   inputs_to_buffers)	rq   rx   rM   metadatarr   
param_namekvbuffer_names	            r2   )_populate_param_buffer_metadata_to_new_gmr      s.    06688 * *&---]D)))) ) )7m##{g:::$9$+F
!<<< ;J G M M O O ) )1'(	!{g777%7D"=== ;K H N N P P ) )1'(	!) )r5   c                     d | j         j        D             }t          |           }||j        S |D ]*}t	          |t
          j                  r|j        j        c S +d S )Nc                 ^    g | ]*}|j                             d d          |j         d          +S )valN)re   getr0   rr   s     r2   
<listcomp>z*_get_shape_env_from_gm.<locals>.<listcomp>   s?       9==%%1 		%111r5   )rc   rd   _detect_fake_mode_from_gm	shape_envrg   r_   SymIntrr   )rx   vals	fake_moder   s       r2   _get_shape_env_from_gmr      s     HN  D *"--I"" $ $a&& 	$6####	$$ $r5   Fname_map	orig_namerE   is_placeholderc                    ||                                  v rt          j        d|          }|r:|s8|                    d          t	          |                    d                    }}nd}| d|dz    x}|                                  v r%|dz  }| d|dz    x}|                                  v %|| |<   n|| |<   | |         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   rt   )r}   rematchgroupint)r   r   rE   r   r   ndup_names          r2   _rename_without_collisionsr      s     x     -- 	 	kk!nnc%++a..&9&9!DDA",,QU,,,x1B1BBBFA #,,QU,,,x1B1BBB&"Ir5   input_placeholdersc                    dt           dt          fd}dd l}ddlm} ddlm} t          |          t          |           k    r0t          dt          |            dt          |           d	          i }t          ||           D ]\  \  }}	}
|
j
                            d
          }t          |t                    rEt          |	t          j                  s+t          d ||           dt!          |	                     t          |j                  t          |	j                  k    r,t          d ||           d|j         d|	j         d	          t%          t          |	j        |j                            D ]\  }\  }}t          |t          j                  rt          |j        j        j                  dk    rt/          t1          |j        j        j                            }||v rJ|j        j                            |          }||k    r$t          d ||           d| d| d|           nt          |t          j                  r|j        j        j        snt          |j        j        |j                  rt9          |          ||<   nr ||                    |j        j        |          |          }|.t          d|
j         d| d| d|j        j         d| d          t9          |d                   ||<   |j        j        |v r |||j        j                           \  }}|dk    r*||k     r$t          d ||           d| d| d|           |t>          j         k     r*||k    r$t          d ||           d| d| d|           (||k    rQt          |t          j                  r|j        j        j        s[t          d ||           d| d| d|           ~t          |t8          tB          t          f          rGt!          |	          t!          |          k    s|	|k    r!t          d ||           d| d|	           d S )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   )rg   r   idxr   r   rb   )r   args_kwargs_key_path	kwarg_keyrE   s       r2   
get_keystrz6_check_input_constraints_for_graph.<locals>.get_keystr   s      ({.<<<<<#q((16(122,//111 Ii44444y>>!B$'D2F8ABB<00222r5   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   rb   sympy@torch._export.passes.add_runtime_assertions_for_constraints_passr   torch.utils._sympy.solver   lenRuntimeErrorzipre   r   rg   r   r_   Tensortypeshape	enumerater   rr   exprfree_symbolsnextitersubs	is_numberSymbolr   EqrE   mathinffloat)r   flat_args_with_pathrange_constraintsr   r   r   r   unification_mapr   ru   rr   node_valjarg_dimnode_dimsymbolexisting_dimsolutionmin_valmax_vals                       r2   "_check_input_constraints_for_graphr      s?   3W 3 3 3 3 3" LLL      322222
3'9#:#:::T/00T T8;<O8P8PT T T
 
 	
 02O!$%8:L!M!M [ [39==''h
++ Y	c5<00 "cH)=)=ccX\]`XaXacc   8>""c#)nn44"D::hCWCW D D!)D D7:yD D D  
 +4C	8>4R4R*S*S H H&&GX x66EHM.;<<AA!$x}'9'F"G"GHHF00'/}'9'>'>'O'O"l22".!EZZ5I5I !E !ERS !E !E#/!E !E;B!E !E# #  3 'w==O$+L$5$?O !)(-*<elKK O
 ;>g,, 7 7+49$)HHX]-?$I$I6," ," $,#3*6)j$) )j )jA )j )jSZ )j )j7?}7I)j )jSY)j )j )j+& +& %&
 ?B(1+>N>NOF$;})->>>+@+@-hm.@A, ,( #Q;;&00&2%DH9M9M %D %DVW %D %D'.%D %D:A%D %D'" '" !" #TX--&00&2%DH9M9M %D %DVW %D %D'.%D %D:A%D %D'" '" !"
 (**&x>>%$,M$6$@% %*=H1E1E = =a = ='= =3:= =   +{HR 3s"344 	CyyDNN**cXoo"hH)=)=hhxhhcfhh  s[ [r5   )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           |||	||           d S )Nz7Only dataclasses can be registered with this function: objr&   c                 
   g }g }g }t          j        |           D ]b}|j        t          | |j                  }}|r+|                    |           |                    |           M|                    |           c|||gfS N)dataclassesfieldsrE   r;   append)r   	flattened
flat_names
none_namesfrE   r   r   s          r2   default_flatten_fnz=register_dataclass_as_pytree_node.<locals>.default_flatten_fnl  s    	

#C(( 	( 	(AQV 4 4#D"4  %%%!!$''''!!$'''':z222r5   r}   contextc           
          |\  }} di t          t          ||                     t                               |          S )Nr)   )dictr   fromkeys)r}   r   r   r   r   s       r2   default_unflatten_fnz?register_dataclass_as_pytree_node.<locals>.default_unflatten_fny  sF    !(
JsPPT#j&1122PdmmJ6O6OPPPr5   c                 ^     |           \  }\  }}d t          ||          D             |fS )Nc                 6    g | ]\  }}t          |          |fS r)   )r   )r0   r   r   s      r2   r   z[register_dataclass_as_pytree_node.<locals>.default_flatten_fn_with_keys.<locals>.<listcomp>  s'    JJJtq!A"JJJr5   )r   )r   r   r   r   r   s       r2   default_flatten_fn_with_keyszGregister_dataclass_as_pytree_node.<locals>.default_flatten_fn_with_keys}  s=    .8joo+	+J
JJs:y/I/IJJJJVVr5   z7Both to_dumpable_context and from_dumpable_context for z must be None or registered.)r   flatten_with_keys_fnr   r   )	r   is_dataclassr   r	   r   r   r   
ValueErrorr   )
r   r   r   r   r   r   r   r   r   r   s
   ``    `   r2   !register_dataclass_as_pytree_noder   ^  s    #  G GFFFG G G3 3d3i.@(A 3 3 3 3 3 3QXc] QW Q Q Q Q Q Q QW# W%S	78J2K W W W W W W  *5;MJ#/#;<<AULt#(=(EF 
%c % % %
 
 	

 19/3     r5   programr   rr   c                 (    |j         | j        j        v S )zM
    Checks if the given node is a parameter within the exported program
    )rE   r@   r~   r   rr   s     r2   is_paramr     s    
 9/DDDr5   c                 n    t          | |          r$| j        j        |j                 }| j        |         S dS )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   
state_dict)r   rr   parameter_names      r2   	get_paramr     s;      2 0EdiP!.114r5   c                 (    |j         | j        j        v S )zJ
    Checks if the given node is a buffer within the exported program
    )rE   r@   r   r   s     r2   	is_bufferr     s    
 9/AAAr5   c                     t          | |          r?| j        j        |j                 }|| j        j        v r| j        |         S | j        |         S dS )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   rA   r   )r   rr   r   s      r2   
get_bufferr     sZ     $ 3-?	J'1HHH$[11%k224r5   c                 (    |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     r2   is_lifted_tensor_constantr     s     9/QQQr5   c                 n    t          | |          r$| j        j        |j                 }| j        |         S dS )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   rA   )r   rr   lifted_tensor_names      r2   get_lifted_tensor_constantr     sB     !$// 5$4VI
  !3444r5   c                     ddl m} i d}| j        j        D ]} ||          r|dz  }||<    || | fddd          }| 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)   )rr   	split_maps    r2   <lambda>z"sequential_split.<locals>.<lambda>  s    Yt_ r5   T)keep_original_orderkeep_original_node_name)torch.fx.passes.split_moduler   rc   rd   _codegen	recompile)rx   node_call_backr   split_idrr   new_gmr  s         @r2   sequential_splitr    s     :99999IH # #>$ 	MH"	$\

$$$$  $  F H-FL
Mr5   rd   c                      fd| D             S )z:Returns the nodes that match the node_call_back as a list.c                 *    g | ]} |          |S r)   r)   )r0   rr   r  s     r2   r   z nodes_filter.<locals>.<listcomp>  s(    ;;;TnnT&:&:;D;;;r5   r)   rd   r  s    `r2   nodes_filterr    s    ;;;;U;;;;r5   c                 f    t          | |r|nd           }t          |          dk    r|d         S dS )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                     dS )NTr)   rr   s    r2   r  znodes_first.<locals>.<lambda>  s    QU r5   r   N)r  r   )rd   r  rets      r2   nodes_firstr    s=     uUnnDUDU
V
VC
3xx!||1v4r5   c                 <    t          t          | |                    S )z:Returns the number of nodes that match the node_call_back.)r   r  r  s     r2   nodes_countr    s    |E>22333r5   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)   )rd   r  rr   s      r2   	nodes_mapr    s+    
   tLr5   old_nodenew_nodec                     |                      |           | j                                         | j                            |            dS )z5
    Replace all uses of old_node with new_node.
    N)replace_all_uses_withusersclearrc   
erase_node)r  r  s     r2   node_replace_r   "  sH     ""8,,,NNh'''''r5   call_mod_nodec                    | j         dk    sJ | j        j        }t          | j        t
                    sJ t          || j                  }d |j        j        D             }d |j        j        D             }d |j        j        D             }t          || j	                  D ]6\  }}t          |t          j        j                  sJ t          ||           7|j                            |           5  |D ],}|j                            |          }	t          ||	           -t!          |          dk    rSt!          |          dk    rt!          |d         j	                  dk    sJ |d         j	        d         t          t          j        j                  r*j                                         t          |            nt          t&          t(          f          rD ]"}|j                            |d                    #t-          t'          | j                                                  d           }
t1          |
fd           | j                            |            n:t5          d	t7                     d
          | j                            |            ddd           n# 1 swxY w Y   |                                 |                                 |S )z
    Inline the submodule of the given node into the parent module.
    Note: we only support the case where submodule takes tensors inputs.
    rZ   c              3   0   K   | ]}|j         d k    |V  dS )r|   Nrf   r   s     r2   	<genexpr>znode_inline_.<locals>.<genexpr>6  s.      
K
KD$']2J2J42J2J2J2J
K
Kr5   c              3   ,   K   | ]}|j         d v|V  dS ))r|   outputNr$  r   s     r2   r%  znode_inline_.<locals>.<genexpr>7  s8        tw>W/W/W/W/W/W/W r5   c                 (    g | ]}|j         d k    |S )r'  r$  r   s     r2   r   z node_inline_.<locals>.<listcomp>:  s$    IIItTW5H5Hd5H5H5Hr5   r   r   c                 B    | j         dk    o| j        t          j        k    S )Nr^   )rf   r.   operatorgetitemr  s    r2   r  znode_inline_.<locals>.<lambda>V  s"    O!; "8x'77 r5   c                 F    t          | | j        d                            S )Nr   )r   args)get_item_node
new_outputs    r2   r  znode_inline_.<locals>.<lambda>\  s$    -%"=#5a#89+ + r5   zUnsupported output type z2. Expect it to be a Node or a list/tuple of Nodes.N)rf   rc   owning_modulerg   r.   rb   r;   rd   r   r-  r_   r`   Noder   inserting_before	node_copyr   r  r  listtupler=   r  keysr  r  NotImplementedErrorr   delete_all_unused_submodulesr  )r!  rx   sub_gmphsbodyr'  phru   rr   r  get_item_usersr/  s              @r2   node_inline_r>  +  sG   
 },,,,			*Bm*C00000R-..F
K
KFL.
K
K
KC +  D JIv|1IIIFsM.//  C#ux}-----b#		"	"=	1	1 ': ': 	* 	*Dx))$//H$))))v;;??v;;!##F1IN(;(;q(@(@(@(@*J*ehm44   &&(((mZ8888Ju66 & . .DJNN6!9---- ".,1133448 8" " "      #..}====)stJ/?/?sss   **=999O': ': ': ': ': ': ': ': ': ': ': ': ': ': ':R ##%%%LLNNNIs   4F1J11J58J5c                 r   t          j        | j                  }|j        d         }dt          j        i}g }|                                D ]X\  }}d t          |j        |          D             }|D ]1}|dk    r	|	                    t          j        ||                     2Yt          j        |          S )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   r-  c                     g | ]	}|j         
S r)   )ru   )r0   as     r2   r   z:_get_torch_jit_trace_forward_signature.<locals>.<listcomp>~  s    MMM1MMMr5   self)
parameters)astparsecoder;  r   POSITIONAL_OR_KEYWORDr9   r;   r-  r   inspect	Signature)	rB   ast_modast_func_defarg_type_map
param_listarg_type
param_typearg_name_listarg_names	            r2   &_get_torch_jit_trace_forward_signaturerR  n  s     i!!G$+LOL I;<L J , 2 2 4 4 G G*MM0A8(L(LMMM% 	G 	GH6!!g/*EEFFFF	G
 
3333r5   c                 V   t          | t          j        j        t          j        j        f          rOt          |           }t          |j                  t          |          t          |          z   k    s
J d            nt          j	        | j
                  } |j        |i |j        S )NzyArguments other than POSITIONAL_OR_KEYWORD kinds in forward() are not supported in _get_torch_jit_trace_forward_signature)rg   r_   jitScriptModuleTracedModulerR  r   rC  rH  	signatureforwardbind	arguments)rB   	fake_argsfake_kwargssigs       r2   _bind_signature_to_inputsr^    s    #	.	0FGHH 	-4S99 3>""c)nns;7G7G&GGGGJ HGGG
 ,,38Y.+..88r5   c                 
   g }| j         j        D ]D}|j        dk    r5t          |j        t
          j        j                  r|j        j        dk    ra|j	        \  }}}}|
                    t          | |j                  |f           |
                    t          | |j                  |f           |j        j        dk    rG|j	        d         |j	        dd         }}|
                    t          | |j                  |f           |j        j        dk    r8|j	        \  }	}
}|
                    t          | |	j                  |
|z   f           F|D ]\  }}i }t          |j         j                  D ]g\  }}|t          |          k     r/||         j        ||j        <   ||         j        x|_        |_        Gt          ||j        |j                  |_        ht!          |           |                                 dS )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.
    r^   condwrap_with_set_grad_enabledr   r   Nmap_impl)rc   rd   rf   rg   r.   r_   rl   rm   _name_argsr   r;   r   r   rE   r   _name_hoo_subgraph_placeholdersr  )rx   subgraph_ph_tuplesrr   rt   
true_graphfalse_graph	cond_argssubgraphr:  
body_grapharrayr-  hoo_phsr   is                  r2   re  re    s    RT  7o%%*K7+
 +
% { F**8<
5:{I"))72z7H+I+I9*UVVV"))72{7I+J+JI*VWWWW"&BBB $
1tz!""~#"))72x+G+G*MNNNN"j00*.*'
E4"))R!233UT\B  
 0  '#% !566 	W 	WGAt3w<<&-ajo#*1!*/9	DKK6xDIVV		 	(111 r5   export_graph_signaturerA   c           
         d }d i }t          |||          }	t          |	          \  }
}d |j        D             }t          |
|          D ]T\  \  }}}|rJt	          ||t
          t          j                 d                    fd|D                       z   d           U|j        D ]}|j	        t          j        k    r|j	        t          j
        k    rd}n" ||j                                                  }t          j        d	d|          }t	          ||j        j        t
          |j	                 |z   d           | j        j        D ])}|j        d
k    rt	          ||j        |j                   *| j        j        D ]M}|j        d
k    r%|j        |v sJ ||j                 x|_        |_        2|j        |v r||j                 |_        Nt)          |            |                                  |j        D ]f}|j        j        |v sJ ||j        j                 |j        _        |j	        t          j        k    r#|j        |v r||j                 dd         |_        g|j        D ]\}|j        j        |v r||j        j                 |j        _        |j	        t0          j        k    r|j        |v r||j                 |_        ]t5          |                                          D ]q}||         }||v rct9          |t:          j                  sI||         }||k    r;t          j        d|          r&|t
          t          j                 |z   k    r|||<   ||= rdS )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                     |                      d          r| t          d          d          } n,|                      d          r| t          d          d          } t          j        dd|           } | S )N
L__self___self_[^a-zA-Z0-9]rt   )
startswithr   r   subxs    r2   _strip_namez,placeholder_naming_pass.<locals>._strip_name  sp    <<%% 	"#l##%%&AA\\'"" 	"#g,,..!AF?C++r5   c                 N   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|            )Nrt  rt   zPytree key of type z not handled for )rg   r   r   rv  rb   keyr   r   r   rE   r   r   rw  s    r2   _extract_pytree_keyz4placeholder_naming_pass.<locals>._extract_pytree_key  s    a$$ 	TSZZ88AH;'' 	Tqu:::&& 	T6MRT!WWRRqRRSSSr5   c                 P    g | ]#}|j         t          j        k    |j        j        $S r)   )r+   r   
USER_INPUTru   rE   r/   s     r2   r   z+placeholder_naming_pass.<locals>.<listcomp>  s6       9	,,, 	,,,r5   rt   c              3   R   K   | ]!} |                                           V  "d S r   )lower)r0   rx  r|  s     r2   r%  z*placeholder_naming_pass.<locals>.<genexpr>  s;      LLa..q117799LLLLLLr5   T)r   r    rt  r|      Nz
arg(\d+)_1) r^  r   r8   r   r   placeholder_prefixesr   r~  joinr+   TOKENr.   r  r   rv  ru   rE   rc   rd   rf   re  r  
CUSTOM_OBJoutput_specsr   USER_INPUT_MUTATIONr4  r6  rg   r_   r   r   )rx   ro  rB   r[  r\  fake_params_buffersrA   ry  r   combined_argsr   rt   user_input_namesarg_pathru   user_input_namer1   	base_namerr   rE   constantnew_namer|  s                         @r2   placeholder_naming_passr    s   6  	T 	T 	T  "H .c9kJJM3MBB *6   -00CEU,V,V  (3 	&$Y%9:((LLLL8LLLLLM#    '2 
 
9	,,,9	''II#DK006688IF?C;;	"HM +i7		
 	
 	
 	
 	
  C C7m##"8TY	BBBB  , ,7m##9((((&.ty&99DIY("" +DI $B''' LLNNN '2 4 4x}(((( /I---$+2I2I"4;/3DK&3 0 08=H$$$TX]3DHM9
6664;(;R;R"4;/DK Y^^%%&& $ $T?8Jel%
 %
  ~HD  H]D11 ! 4Y5I JT QQQ&.	(#dO$ $r5   r   in_placec                 D   |rB|                                  D ]+\  }}t          |d          rt          | |         d           ,| S i }|                                  D ]D\  }}t          |d          r*|                                                                ||<   ?|||<   E|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.
    ro   )r9   hasattrdelattrclonedetach)r   r  r   r   new_state_dicts        r2   remove_proxy_from_state_dictr  M  s      $$&& 	0 	0DAqq'"" 0
1w///$$&& 	& 	&DAqq'"" &$%GGII$4$4$6$6q!!$%q!!r5   c                    g }g }| j         j        D ]}|j        dk    rHd|j        v r?|j        d         }|/t	          |t
          j                  r|                    |           Ut          |          dk    rrd|j        v s	d|j        v r`d}d|j        v r|j        d         }nd|j        v r|j        d         }|/t	          |t
          j                  r|                    |           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)
rc   rd   rf   re   rg   r_   r   r   r   r   )rx   	fake_inps	fake_valsrr   fake_vals        r2   r   r   b  s    %'I$&I + +7m##(:(:y'H#
8U\(J(J#  ***^^q  ty((ETY,>,>H$)++9_5$)##9U+#
8U\(J(J#  ***I	1222r5   )r&   r   )rL   r   rM   r   )F)NNr   )XrD  r   rH  r   r*  r   r   typingr   r   r   r   r   r	   r
   r   r_   torch._guardsr   torch._subclasses.fake_tensorr   r7   r   torch.exportr   torch.export.graph_signaturer   r   r   torch.utils._pytreer   r   r   r   r   r   r   r   r   r   r   r   r~  	PARAMETERr,   CONSTANT_TENSORr  r  r  rK   rP   r`   ra   rb   rw   r   r   boolr   r1  r   r   r   rh   r   r   r   r   r   r   r  r  r  r   r  r  r   r>  ri   rR  r^  re  r  r  _subclassesfake_tensorFakeTensorModer   r)   r5   r2   <module>r     s   



        				       R R R R R R R R R R R R R R R R R R R R  * * * * * * 4 4 4 4 4 4  BHHHHHH,,,,,,AAAAAA > > > > > > > >                           " "dt&OW    <#.D    3'(< 3'c3h 3' 3' 3' 3'l)!%c3h)) $) 
	) ) ) )8$ux3 $ $ $ $& !	 38n  	   :UX]+   H )-,04
 +/9==A$4 4 4	c4%4 =)4
 #3-4 ""564 $$9:4 4 
4 4 4 4nE' Eux} E E E E E
(- eh !    B( B B$ B B B B
(- el   &RR
(-R 
R R R R
(- el   $- %(BV    8<UX]+ <UX]@S < < < < 04
 

ehm
 
 
 
4tEHM* 4s 4 4 4 4
T%(-( T%(-=P    (EHM (UX] (t ( ( ( (@ @$ @ @ @ @F4 4 4 4 429 9 9'(< ' ' ' ' 'TJ$J$2J$ 
J$ CH~J$ 
J$ J$ J$ J$ZT T d    *33
"13 3 3 3 3 3r5   