
    קgR                    h   U 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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Zd dlZd dlmc mZ d dlmZ d dlmZmZ d dlm Z m!Z! d dl"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z( d dl)m*Z*m+Z+ d d	l,m-Z- d d
l.m/Z/m0Z0m1Z1 d dl2m3Z3m4Z4m5Z5m6Z6m7Z7 d dl8m9Z9 d dl:m;Z; d dl<m=Z=m>Z> d dl?m@Z@ d dlAmBZB d dlCmDZD d dlEmFZF d dlGmHZH d dlImJZJmKZK d dlLmMZM d dlNmOZOmPZPmQZQ d dlRmSZS d dlTmUZU d dlVmWZW d dlXmYZYmZZZm[Z[m\Z\ d dl]m^Z^m_Z_ d dl`maZa d dlmbZb d dlcmdZd dd lemfZf dd!lgmhZhmiZimjZjmkZkmlZl dd"lmmnZnmoZo  ejp        eq          Zre js         G d# d$                      Zte js         G d% d&                      Zu e js        d'(           G d) d*                      Zv et            Zwejx        ejy        ejz        ej{        ej|        ejr        ej}        e~ej        h	ew_        ed+             Zd, Zd- Zd.ej        j        fd/Zd0 Zd1 Zd2 Zd3 Zd4ej        j        d5ej        j        d6eeef         fd7Zd.ej        j        d8eod9eeej        f         d:eeeej        eHej        f         f         d;ee         f
d<Zd=e/d>eod:eeeej        eHej        f         f         d6dfd?Zd.ej        j        d>eod6dfd@Zd4ej        j        d5ej        j        d6dfdAZdBej        j        d6eeef         fdCZdDeeef         dEebdFebdGeeelf         d6eek         f
dHZ	 	 ddIdJdJd'd'd'dKdLedMeedNf         dOeeeef                  dPeeeeef         ee         ee         f                  dQeedNf         dRedSedTedUedVed6ej        j        fdWZ	 ddX dJdd'dJdYdBej        j        dZe/d6eufd[Zd\eKdBej        j        d6eeeej        ej        j        f         f         fd]Z	 ddBej        j        dMeedNf         dOeeeef                  d6ee         fd^ZdBej        j        d6ee         fd_Zd`eej                 daeeej        f         d>eod:eeeej        eHej        f         f         fdbZdcej        j        d>eod:eeeej        eHej        f         f         fddZdeej        j        d6dfdfZdeej        j        d6dfdgZd.ej        j        d8eofdhZdieid6eeef         fdjZdaeee                  edk<   daeeeef                  edl<   dm Zdn Zdo ZdpevdqebdQeedNf         drefdsZdpevdteeef         fduZd\eKfdvZedwej        j        fdx            Zedy             Z G dz d{ej        j                  Zdd|ZdBej        j        dMeedNf         dOeeef         dPeeeeef         ee         ee         f                  dQeedNf         d}ed~eeef         debdSeded6evfdZdBej        j        dMeedNf         dOeeef         dPeeeeef         ee         ee         f                  dQeedNf         d}ed~eeef         debdSededed6evfdZdd fdBej        j        dZe/d6eufdZ	 ddBej        j        dMeedNf         dOeeef         dPeeeeef         ee         ee         f                  dQeedNf         d}ed~eeef         debdSededed6evfdZeeh	 	 dd'dIddBej        j        dMeedNf         dOeeeef                  dPeeeeef         ee         ee         f                  dedQeedNf         d6eifd                        Zeeh	 	 dd'dIdJdJdJddBej        j        dMeedNf         dOeeeef                  dPeeeeef         ee         ee         f                  dedQeedNf         d}edSeded6eifd                        ZdS )    N)contextmanagernullcontext)AnyCallableDictListOptionalSetTupleUnion)enable_python_dispatcher)	UserErrorUserErrorType)exportdb_error_messageget_class_if_classified_error)_fakify_script_objects_gather_constant_attrs_NonStrictTorchFunctionHandlermake_constraintsmake_fake_inputs$produce_guards_and_solve_constraints)_node_metadata_hook_set_node_metadata_hook)CollectTracepointsPass)ConstantAttrMaplift_constants_passrewrite_script_object_meta)_collect_param_buffer_metadata_get_shape_env_from_gm)_populate_param_buffer_metadata_to_new_gmplaceholder_naming_passplaceholder_prefixes)SpecViolationError)_wrap_submodules)_graph_input_names_graph_output_names)create_functional_call)create_tree_flattened_fn)aot_export_module)detect_fake_mode)FakeScriptObject)
FakeTensorFakeTensorMode)log_export_usage)_check_dynamic_shapes_combine_args%_transform_shapes_for_default_dynamic)
OutputKind)#first_call_function_nn_module_stack)make_fx)ConstraintViolationErrorfree_unbacked_symbolsGuardOnDataDependentSymNodeShapeEnv)_PyTreeCodeGen_PyTreeInfo)insert_deferred_runtime_asserts)TreeSpec)ValueRangeError   )AutogradStateOpsFailSafeguard)_disable_prexisiting_fake_modeExportedProgram	InputKindModuleCallEntryModuleCallSignature)"_convert_to_export_graph_signatureExportGraphSignaturec                   ^    e Zd ZU dZdZeed<    ej        e	          Z
ee         ed<   dZdS )ExportDynamoConfigz:
    Manage Export-specific configurations of Dynamo.
    T	allow_rnn)default_factoryreorderable_logging_functionsN)__name__
__module____qualname____doc__rI   bool__annotations__dataclassesfieldsetrK   r
   r   do_not_emit_runtime_asserts     O/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/export/_trace.pyrH   rH   `   sh           It3D;3D4 4 4!3x=    #'rW   rH   c                   x    e Zd ZU ej        j        ed<   eed<   ee	e
ej        eej        f         f         ed<   dS )ATenExportArtifactgmsig	constantsN)rL   rM   rN   torchfxGraphModulerQ   rF   r   strr   Tensorr+   ScriptObjectrV   rW   rX   rZ   rZ   q   sm         	L 	
	     rW   rZ   T)frozenc                   b    e Zd ZU eed<   eed<   eed<   eeeee	j        f         f         ed<   dS )ExportArtifactatenout_spec	fake_modemodule_call_specsN)
rL   rM   rN   rZ   rQ   r<   r-   r   ra   pytreerV   rW   rX   rf   rf      sX         
Cc6?&:!;;<<<<<<rW   rf   c               #   n  K   t           j        j                            d          } t           j        j                            d          }	 d V  t          j        j        j        |   t          j        j        j        |  d S # t          j        j        j        |   t          j        j        j        |  w xY wNF)r^   backendsmkldnn	set_flagsnnpack)orig_mkldnn_flagorig_nnpack_flags     rX   _ignore_backend_decompsrt      s      ~,66u==~,66u==;')9::')9:::: 	')9::')9:::s   B 2B4c                 &    dt          |           z   S )N	L__self__)_strip_rootxs    rX   
_fixup_keyrz      s    Q''rW   c                     t          | t                    rM|                     d          r8| t          d          d          }|                    d          r
|dd          n|S | S )N_export_root.r>   )
isinstancera   
startswithlen)ry   strippeds     rX   rw   rw      sk    !S Fall>:: FS((**+'22377Ex||XEHrW   r[   c           
         | j         j        D ]}|j        t          j        j        j        k    rd|j        v rt          |j        d                   }| j         	                    |          5  | j         
                    dt          j        j        j        |j        ||j        d         d          }|j        |_        |                    |           | j                             |           ddd           n# 1 swxY w Y   dS )z
    In-place modifiy input graph module by replacing the export tracepoint with a new node
    that has the same target and args, but with the _export_root stripped from path.
    pathcall_functionkind)r   r   )argskwargsN)graphnodestargetr^   opshigher_order_export_tracepointr   rw   inserting_beforecreate_noder   metareplace_all_uses_with
erase_node)r[   noder   new_nodes       rX   _rewrite_tracepoint_noder      s>   
  . .;%)0CCC$$"4;v#677X..t44 . .!x33'	.A!Y$($(K$7   	  4    H %)IHM..x888H''---. . . . . . . . . . . . . . .	. .s   +BC88C<	?C<	c                 b   g g }| j         j        D ]}|j        dk    rHd|j        v r?|j        d         }|/t	          |t
          j                  r                    |           Ud|j        v r>|j        d         }|/t	          |t
          j                  r|                    |           t          |z             x}r|}nt          t                      d          }dfd}t          j        t
          j        ||          }	t          j        t
          j        ||          }
|	|
|fS )	z
    Given a graph module, extract fakified input tensors from the metadata of
    its placeholders, and map them to the structure of given args and kwargs.
    Also return the fake mode used to fakify those inputs.
    placeholdervalNexample_valueT)	shape_envexportr   c                 "             }dz  |S )Nr>   rV   )ry   r   count	fake_inpss     rX   lookup_fakez)_extract_fake_inputs.<locals>.lookup_fake   s    

rW   )r   r   opr   r~   r^   rb   appendr*   r-   r8   rk   tree_map_only)r[   r   r   	fake_valsr   fake_valdetected_fake_moderi   r   	fake_argsfake_kwargsr   r   s              @@rX   _extract_fake_inputsr      sP    %'I$&I + +7m##(:(:y'H#
8U\(J(J#  ***	))y1H#
8U\(J(J#  ***-i).CDDD F&		"XZZEEE	E      $U\;EEI&u|[&IIKk9,,rW   c                     |j         D ]3}|j        t          j        t          j        fv r| |j                 |_        4|j        D ]3}|j        t          j        t          j	        fv r| |j                 |_        4d S N)
input_specsr   rB   	PARAMETERBUFFERr   output_specsr2   BUFFER_MUTATIONGRADIENT_TO_PARAMETER)param_buffer_tabler\   specs      rX   _replace_param_buffer_namesr      s     : :9
 
 
 -T[9DK  : :9&,
 
 
 -T[9DK: :rW   c           	      .   t          |           t          |          t                    z   k    s;J dt          |            dt          |           dt                     d            fd| t          |          d          D             }g ||R S )Nz,Total number of arg names is expected to be z	 but got z positional args, z kwargs.c                      g | ]
}|         S rV   rV   ).0kw_namer   s     rX   
<listcomp>z/_convert_to_positional_args.<locals>.<listcomp>   s    SSSGwSSSrW   )r   )orig_arg_namesr   r   reordered_kwargss     ` rX   _convert_to_positional_argsr      s    ~#d))c&kk"9999	Fs>7J7J 	F 	Ft99	F 	F03F	F 	F 	F :99 TSSS~c$iikk7RSSS		  rW   c                   
 d}t          j        dd|          }|                                 D ](}t          |t          j        j                  s#|j        j        D ]}|j	        dv rd}|j
                            di           x}rt          t          |                                                    \  }}	t          j        |	          r,t#          |	t          j        j                  r||k    r|	|u rd}nt          |	t(                    sJ |rCd 
|||j        d	z   |j        z   fi|}
fd
|                                D             |j
        d<   *d S )Nz	L['self']z[^a-zA-Z0-9]_r   outputTnn_module_stackFc                     	 g  G fdd          }t          | dd |            ii           d                              S # t          $ r | cY S w xY w)Nc                   $    e Zd Z fdZ fdZdS )@_normalize_nn_module_stack.<locals>.normalize_path.<locals>.Pathc                 2                         |           | S r   )r   )selfnamepartss     rX   __getattr__zL_normalize_nn_module_stack.<locals>.normalize_path.<locals>.Path.__getattr__   s     %T 2 2 2'+rW   c                 L                         t          |                     | S r   )r   ra   )r   idxr   s     rX   __getitem__zL_normalize_nn_module_stack.<locals>.normalize_path.<locals>.Path.__getitem__$  s      %SXX 6 6 6'+rW   N)rL   rM   rN   r   r   )r   s   rX   Pathr     sG        , , , , ,, , , , , , ,rW   r   Lr   r}   )evaljoin	Exception)r   r   r   s     @rX   normalize_pathz2_normalize_nn_module_stack.<locals>.normalize_path  s    $ ", , , , , , , , , , TC&$$&&)9#:;;;"xx.$ $ $ $#$s   AA AAr}   c                 8    i | ]\  }\  }}| |          |fS rV   rV   )r   keyr   tyr   s       rX   
<dictcomp>z._normalize_nn_module_stack.<locals>.<dictcomp>1  sB     0 0 0'ZdB ....30 0 0rW   )resubmodulesr~   r^   r_   r`   r   r   r   r   getnextitervaluesinspectisclass
issubclassnnModulera   rM   rN   items)gm_torch_levelroot_clsrootroot_keyr[   r   add_rootr   r   r   r   s             @rX   _normalize_nn_module_stackr     s   DvosD11H$$&& , ,"eh233 	HN )	 )	Dw333H"&)--0A2"F"FF 	/_%;%;%=%= > >??b ?2&& /:b%(/+J+J /t||h#(%b#..... $ $ $& tX%83%>AV%VW#%#0 0 0 0+:+@+@+B+B0 0 0	+,M)	, ,rW   original_moduletraced_modulereturnc                    i }i }|                      d          D ]\  }}||t          |          <   |                     d          D ]\  }}||t          |          <   i }|                     d          D ]4\  }}	||vsJ t          |	          |v r|t          |	                   ||<   5|                    d          D ]4\  }}
||vsJ t          |
          |v r|t          |
                   ||<   5|S )z
    Returns a mapping of parameter/buffer names from the new module to the
    original model. This is to help with restoring the FQN for parameter/buffers
    of a traced module to what the original module contains.
    Fremove_duplicate)named_parametersidnamed_buffers)r   r   param_lookupbuffer_lookupr   parambufferr   dynamo_namedynamo_paramdynamo_buffers              rX   _get_param_buffer_mappingr   7  sf    $&L$&M&777OO ' 'e"&RYY'55u5MM ) )f$(bjj!!)+%2%C%C &D & & M M!\ "44444l|++.:2l;K;K.L{+&3&A&A 'B ' ' O O"] "44444m--.;B}<M<M.N{+rW   r\   fake_params_buffersr]   flat_fake_argsc                    d | j         j        D             }t          |j                  t          |          k    sJ d}t	          ||j                  D ]g\  }}|j        t          j        t          j        fv r,|j	        J ||j	                 j
        |j        d         _
        Q|j        t          j        k    r?||         }	t          |	t          j                  r|	j
        |j        d         _
        |dz  }|j        t          j        k    rz|j	        J ||j	                 }
t          |
t          j                  rH|j        d         j        r|
j
        |j        d         _
        |j        d         j
        |
j
        k    sJ 4|j        t          j        t          j        fv rUt)          |j                  d S )Nc                 (    g | ]}|j         d k    |S )r   )r   r   r   s     rX   r   z0_preserve_requires_grad_pass.<locals>.<listcomp>a  s$    PPPTtw-7O7OD7O7O7OrW   r   r   r>   )r   r   r   r   zipr   rB   r   r   r   requires_gradr   
USER_INPUTr~   r^   rb   CONSTANT_TENSORis_leaf
CUSTOM_OBJTOKENAssertionError)r[   r\   r   r]   r   placeholdersir   r   fake_argconstants              rX   _preserve_requires_grad_passr  Z  s    QPRX^PPPLs3|#4#44444	A,88 , ,
d9
 
 
 ;***-@. Ie** Y)...%a(H(EL11 H191G	% .FAAY)333;*** -H(EL11 T9U#+ T5=5KDIe$229U+9X=SSSSSY9/AAA +++5, ,rW   orig_constant_attrsgraph_signaturec                 6   i }|                                 D ]\  }}|| v r| |         ||<   |j        D ]e}|j        t          j        t          j        fv rD|j        }|J |                    ||g          }|d         |_        ||         }	||= |D ]}
|	||
<   fdS )zXRewrite the graph signature and constants table to use the FQN from the original module.Nr   )r   r   r   rB   r  r  r   r   )r  r  r]   remap_tabler   valuer   orig_targettargetsr
  r   s              rX   _remap_constantsr    s     )+K (( ; ;e''' 3E :K+ - -9% 
 
 
 +K***!ookK=AAG!!*DK -H+&! - -$,	&!!- -rW   c                 z  
 d | j         j        D             

fd}t          t          j                 }t          t          j                 }i }|j        D ]}|j        t          j        k    r|j        j	        
                    |          s|j        j	        
                    |          r. |||j        j	        t          |          d         z             }n |||j        j	        z             }|||j        j	        <   ||j        _	        |j        D ],}|j        j	        |v r||j        j	                 |j        _	        -|                                 D ]g}t          |t          j        j                  s"|j         j        D ]$}	|	j	        |v r||	j	                 x|	_	        |	_        %|                                 hdS )z\
    For strict mode, rename constants nodes that were previously annotated as buffers.
    c                     h | ]	}|j         
S rV   )r   r   s     rX   	<setcomp>z*_rename_constants_nodes.<locals>.<setcomp>  s    777$)777rW   c                 x    | v rd}|  d| x}v r|dz  }|  d| x}v |}                      |            | S )Nr>   r   )add)r   ndup_name
node_namess      rX   rename_constantz0_rename_constants_nodes.<locals>.rename_constant  sn    :A"&}}}},8;;Q #'}}}},8;;DtrW   N)r   r   r"   rB   r   r  r   r   argr   r   r   r   r   r~   r^   r_   r`   r   	recompile)r[   r  r  buffer_prefixconst_prefixbuffer_to_constantr   c_namemodr   r  s             @rX   _rename_constants_nodesr$    s    87777J     ))9:M'	(ABL+ # #9	111$(-:R:R;
 ;
1 x}''66 G( 48=]1C1C1E1E#FF  ))EFF06tx}-"DHM, > >8=....tx}=DHM zz||  #ux344 	IO 	H 	HDy...*<TY*GG	DK rW   c                 p   t          | |          }|                                D ]\  }}|                    dd          ||<   |                                D ]\  }}t          ||          st	          ||          }t          |t          j                  r6t          |t          j        j	                  s|
                    ||           nt          |||           t          ||           |j        j        D ]%}|j        dk    r|j        }||v r||         |_        &|                                 dS )zV
    Restores the state dict of the traced module to that of the original module.
    r}   r   get_attrN)r   r   replacehasattrgetattrr~   r^   rb   r   	Parameterregister_buffersetattrdelattrr   r   r   r   r  )r   r   r   r   fqnattrr   	attr_names           rX   _restore_state_dictr1    sZ    3?MRR
 (--// 9 9	c#&;;sC#8#84   (--// 	% 	%	c}d++ 	}d++dEL)) 	.*T58CU2V2V 	.))#t4444M3---t$$$$ #) < <7j  I...0;rW   r#  c                 B    d |                      d          D             S )Nc                 >    i | ]\  }}|t          |          j        S rV   )typerL   )r   r   ms      rX   r   z)_get_module_hierarchy.<locals>.<dictcomp>  s4       #*4d1gg  rW   Fr   )named_modules)r#  s    rX   _get_module_hierarchyr7    s5     .1.?.?QV.?.W.W   rW   module_hierarchyin_specrh   module_call_signaturesc                     fd| D             }|d         j         dk    sJ t          g g ||          |d         _        |S )Nc                 X    g | ]&}t          |                    |                     'S ))r.  	signature)rC   r   )r   r.  r:  s     rX   r   z+_make_module_call_graph.<locals>.<listcomp>  sE        	C+A+E+Ec+J+JKKK  rW   r    )inputsoutputsr9  rh   )r.  rD   r=  )r8  r9  rh   r:  rets      ` rX   _make_module_call_graphrB    so       #  C q6:*2w  CF JrW   rV   F)preserve_module_call_signaturedisable_constraint_solver'allow_complex_guards_as_runtime_assertsrestore_fqn_log_export_usagesame_signaturefr   .r   dynamic_shapesrC  rD  rE  rF  rG  rH  c                (   |rt          ddh           t          |t                    s*t          t          j        dt          |                     |pi }t          | ||          }
t          |
|           t          |
|          }t          j        j                            t          j        t                               5  	 i }t#          | ||          5  t%                      5  t          j                            | |d|d|||	          |i |\  }}ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   ny# t(          t*          f$ r,}t          t          j        t/          |                    d}~wt0          $ r1}t          t          j        d	t/          |           d
          d}~ww xY wddd           n# 1 swxY w Y   ||j        d<   t          | t          j        j                  r|rt;          | |           |S )z
    Traces either an nn.Module's forward function or just a callable with PyTorch
    operations inside and produce a torch.fx.GraphModule in torch IR.
    zexport.private_api_export_to_torch_ireventflagsAExpecting `args` to be a tuple of example positional inputs, got symbolicT)rJ  tracing_moderD  +prefer_deferred_runtime_asserts_over_guardsrE  rG  rH  Nz5Consider annotating your code using torch._check*(). constrain_as_size_example)	case_namerj   )r.   r~   tupler   r   INVALID_INPUTr4  r0   r/   r1   r^   _dynamoconfigpatchrR   asdictDEFAULT_EXPORT_DYNAMO_CONFIGr$   rt   r   r5   r=   CONSTRAINT_VIOLATIONra   r7   ANTI_PATTERNr   r   r   r1  )rI  r   r   rJ  rC  rD  rE  rF  rG  rH  combined_argstransformed_dynamic_shapesrj   r   r   es                   rX   rL  rL    s!   $  T3<Q;RSSSSdE"" 
'\PTUYPZPZ\\
 
 	

 \rF!!T622M-888!F~" " 
		#	#K$67S$T$T	U	U  	GI!13D   &((  %*M$8$8#=!+.G AE<c&7#1 %9 % % % % %!                             $ )/: 	H 	H 	HM>AGGG* 	 	 	*PAPP5   	/              < 0AN+,!UX_%% /+ /A~...s   GED;(0D$D;$D((D;+D(,D;/E;D?	?ED?	EGF='E??F=,F88F==GGGc                     | S r   rV   rx   s    rX   <lambda>rc  X       rW   )	transformpre_dispatchdecomp_table_check_autograd_state_is_torch_jit_traceconstant_attrsc                0   t           j                                        }t                      }|	r|s|rt	                      }t
          d             }t           j        j        j        	                    | |ddd          5  |5  t                      5   |            5    |t                    | |d|||          \  }}d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   d } || |           |rM	  ||           n@# t          t          f$ r,}t          t          j        t#          |                    d }~ww xY wt%          j        ||f          }t           j        j        j        s|d}t/          |t1          j        t4          |                    5  t7          |          }|r(t9          ||d	t;          |j                   d
           d d d            n# 1 swxY w Y   |                                 tA          |          |_!        d}tE          |j#                  tE          |j$                  z   tE          |j%                  z   }|j        j&        D ]G}|j'        dk    r:||k    r/|||z
           }tQ          |t           j)                  s
||j*        d<   |dz  }HtW          ||tY          |                     }t[          |          }|.                    t_          |||                     |r*ddl0m1} ddl2m3}  |||          \  }} |||          \  }}|4                                D ]p}tQ          |t           j5        j6                  s"|j        j&        D ]A}|j'        dv r6|j*        7                    dd            |j*        7                    dd            Bqtq          ||| ||||           ts          |||||           tu          |||          S )Nc               3      K   t           j        j        } 	 dt           j        _        d V  | t           j        _        d S # | t           j        _        w xY wNTr^   compiler_is_compiling_flag	old_values    rX   _compiling_state_contextz4_export_to_aten_ir.<locals>._compiling_state_contextj  N      N5		:04EN-EEE09EN---	EN-9999	   = ATtie_weightsstrictstack_weightsF)trace_jointrf  decompositionsr   c                    t          | t          j        j                  rt	          | d          r|j                            | j                   t          | j        j	                  d         }t          |j        j	                  d         }|j
        dk    r|j
        dk    sJ t          |j                  dk    sJ |j                            |j                   d S d S )Nr   r   r   )r~   r^   r_   r`   r(  r   updatelistr   r   r   r   )old_gmnew_gmold_output_nodenew_output_nodes       rX   $_maybe_fixup_gm_and_output_node_metaz@_export_to_aten_ir.<locals>._maybe_fixup_gm_and_output_node_meta  s    feh233 	>vv&& 0""6;///"6<#566r:O"6<#566r:O"%11o6HH6T6T6T6T+,,1111 ''(<=====	> 	>rW   UFile "torch/fx/passes/runtime_assert.py", line 24, in insert_deferred_runtime_assertsstack_traceexported program: r   r   r   r   r>   )replace_autocast_with_hop_pass)replace_set_grad_with_hop_passr   r   r  );r^   _Cis_grad_enabledr   r?   r   r   utils	stateless_reparametrize_modulert   r)   r5   r=   r   r   r]  ra   rk   tree_leavesrX  rY  rU   r   	functoolspartialr   r   r;   r3   r   r  r&   user_outputsr   
parametersbuffersinput_tokensr   r   r~   rb   r   rE   _get_non_persistent_buffersr   r~  r   3torch._export.passes.replace_autocast_with_hop_passr  3torch._export.passes.replace_set_grad_with_hop_passr  r   r_   r`   popr!   r  rZ   )r#  r   r   r   rj  produce_guards_callbackre  rf  rg  rh  ri  r  grad_safe_guardrs  r[   r  r  ra  r   r  r   indextotal_non_user_inputsr   user_argexport_graph_signaturer]   r  r  _mods                                 rX   _export_to_aten_irr  P  sX   " h..00O!mmO  > 	> 	>;==O: : ^: 
	!	7	7 
8 
 
 
 
 
 
 011
 
 4L3K3M3M
 
 ;ii(9::%'
 
 
O
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 	> 	> 	> )(b111  H	H##B''''(/: 	H 	H 	HM>AGGG	H
 'K(@AAN=; 1 	 %	!"5;OOO
 
 
	 
	 /r22I /X)LRX)V)VXX	   
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 LLNNN#6r#:#:O  EO&''
o%
&
&	'
o*
+
+	, 
   7m##---)%2G*GH!(EL99 0'/DIe$QJE?8==  +2..I(-C^TTUUU 
	
 	
 	
 	
 	
 	
	
 	
 	
 	
 	
 	
 &D%C&&
 &
"" &D%C&&
 &
""
 

 3 3$ 455 	J$ 	3 	3Dw333	/666	mT222	3 
   !
"$7N   
  s   ?DC9C"#C	?C"CC"CC"C9"C&&C9)C&*C9-D9C=	=D C=	DDD-D9 9E6
'E11E6:HHHri   c                 |   i t          |                    d                    t          |                    d                    }i }i }|                                D ]Z\  }}t	          |          |v r|t	          |                   }n)|                     |d          }||t	          |          <   |||<   [|S )NFr   Tstatic_shapes)dictr   r   r   r   from_tensor)ri   r#  params_buffersfaked_params_buffersmemor   r  fake_tensors           rX   _fakify_params_buffersr    s    
s##U#;;
<
<
s  % 88
9
9N
 "$D$**,, 0 0
Ue99r%yy/KK#//T/JJK)DEO$/S!!rW   c                    t          j        | j                  } |j        | j        }g }|                                D ]n\  }|j                 j        t           j        j	        k    r/|
                    fdt          |          D                        Y|                               o|r1|
                    d |                                D                        |S )a  
    Gets the argument names to forward that are used, for restoring the
    original signature when unlifting the exported program module.
    - Positional args: retain the original argument names, and enumerate
        *args as args_0, args_1, ...
    - Keyword args: retain the original kwarg names in the order specified
        by the user. This order seems to matter for the current state of
        export lifted modules.
    c                 $    g | ]\  }} d | S )r   rV   )r   r  r   r   s      rX   r   z*_get_forward_arg_names.<locals>.<listcomp>/  s'    EEEDAqT--A--EEErW   c                     g | ]\  }}|S rV   rV   )r   kwargr   s      rX   r   z*_get_forward_arg_names.<locals>.<listcomp>4  s    ;;;qe;;;rW   )r   r=  forwardbind_partial	argumentsr   r  r   _ParameterKindVAR_POSITIONALextend	enumerater   )r#  r   r   r\   _argsnamesr  r   s          @rX   _get_forward_arg_namesr    s     
CK
(
(CCd#-EE{{}}  e>$$(>(MMMLLEEEEIe4D4DEEEFFFFLL =;;FLLNN;;;<<<LrW   c                     t                      }|                                 D ]-\  }}|j        D ] }|                    |r| d| n|           !.|S )zO
    Returns set of non-persistent buffers in a module and its submodules.
    r}   )rT   r6  _non_persistent_buffers_setr  )r#  resultr   r5  bs        rX   r  r  9  sp     UUF$$&& 5 5a. 	5 	5AJJ3$}}}}}!4444	5MrW   orig_mod_bufferstraced_mod_buffersc                     |j         D ]L}|j        t          j        k    r5|j        J ||j                 }|| vrt          j        |_        |||j        <   MdS )zt
    Dynamo erroneously marks tensor attributes on modules as buffers.
    Rewrite them to be tensor constants.
    N)r   r   rB   r   r   r  )r  r  r  r]   r   r  s         rX    _rewrite_dynamo_tensor_constantsr  D  so      + 	/ 	/9	(((;***&t{3E,,, &5	).	$+&	/ 	/rW   orig_modc                     |j         D ]T}|j        t          j        k    r=|j        s6|j        J |j        |vsJ |                     |j                  ||j        <   UdS )z;
    Moves non-persistent buffers to tensor constants.
    N)r   r   rB   r   
persistentr   
get_buffer)r  r  r]   r   s       rX   0_move_non_persistent_buffers_to_tensor_constantsr  Z  sy      + F F9	((((;***;i////%-%8%8%E%EIdk"	F FrW   graph_modulec           
      x   t          | gt          |                                           z             D ]\  }}t          |t          j        j                  s&|j        j        D ]}|j	        dv r|dk    r|j
                            dd          x}	 t          d| d|j	         d          t          d |                                D                       st          d| d|j	         d	|           |j	        d
v r6|j
                            dd          rt          d| d|j	         d          ӐdS )a  
    Perform nn_module_stack checks on the graph.
    Current constraints:
        For the top level graph:
        - populated for 'call_function', 'get_attr'
        - None for 'placeholder', 'output'
        For submodule graphs:
        - None for 'placeholder', output'

    TODO(pianpwk): make this a consistent node-level check once nn_module_stack is populated for cond submodules.
    r   r&  r   r   NNode 	 of type z$ is missing nn_module_stack metadatac              3      K   | ]]\  }}t          |t                    o@t          |t                    o+t          |          d k    ot	          d |D                       V  ^dS )   c              3   @   K   | ]}t          |t                    V  d S r   )r~   ra   )r   ry   s     rX   	<genexpr>z4_verify_nn_module_stack.<locals>.<genexpr>.<genexpr>  s,      >>q
1c 2 2>>>>>>rW   N)r~   ra   rV  r   allr   kvs      rX   r  z*_verify_nn_module_stack.<locals>.<genexpr>  s        
 !Aq	 #1c** ?&q%00?FFaK?  >>A>>>>>	     rW   z[ has incorrect nn_module_stack metadata formatexpected Dict[str, Tuple[str, str]], but got r   z7 contains nn_module_stack metadata, this should be None)r  r  r   r~   r^   r_   r`   r   r   r   r   r   r#   r  r   )r  r  r#  r   r   s        rX   _verify_nn_module_stackr  i  s    \NT,2F2F2H2H-I-IIJJ  3#ux344 	IO 	 	Dw77766+/9==9JD+Q+QQ 1`D``47```     
 %4$9$9$;$;     
 1^D ^ ^47 ^ ^L[^ ^   5559==!2D99 ,oootwooo  -	 rW   c           
         t          | gt          |                                           z             D ]\  }}t          |t          j        j                  s%| j        j        D ]}|j	        
                    dd          }|j        dv r5|2t          |t                    st          d| d|j         d|           [|j        dv r|rt          d| d|j         d|           dS )	z
    Perform stack trace checks on the graph.
    Constraints:
        - None or non-empty str for 'call_function', 'get_attr'
        - None for 'placeholder', 'output'
    r  Nr  r  r  zP has invalid stack_trace metadata, expected a string or None but instead found: r   zA contains stack_trace metadata, expected None but instead found: )r  r  r   r~   r^   r_   r`   r   r   r   r   r   ra   r#   )r  r  r#  r   r  s        rX   _verify_stack_tracer    sT    \NT,2F2F2H2H-I-IIJJ  3#ux344 	 &, 	 	D)--t<<Kw777#+z+s/K/K+,V V Vtw V VHSV V   555 ,J J Jtw J J<GJ J  	 rW   c           
         d |j         D             }|                                 D ]}t          |t          j        j                  s"|j        j        D ]h}|j        dk    r[|j	        |vr||j	                 }t          |         }|j	                            |          st          d|j	         d| d|           idS )a  
    Performs a sanity check on the placeholder node names.
    - User input nodes: no restrictions, should match the original forward() signature
    - Params/buffers/constants/custom_obj/token nodes: should start with prefixes defined in <placeholder_prefixes>
    c                 2    i | ]}|j         j        |j        S rV   )r  r   r   )r   r   s     rX   r   z-_verify_placeholder_names.<locals>.<dictcomp>  s     IIIDHM49IIIrW   r   zPlaceholder node name z does not follow spec for z, name should have prefix: N)r   r   r~   r^   r_   r`   r   r   r   r   r"   r   r#   )r[   r\   name_to_kindr#  r   	node_kindprefixs          rX   _verify_placeholder_namesr    s    JIIIILzz||  #ux344 	IO 		 		Dw-''9L00(3	-i8y++F33 , E  E  EV_  E  E  }C  E  E  		 rW   epc                    d}t                      }| j                                        D ]}t          |t          j        j                  s"|j        j        D ]o}|j	        dk    r|dz  }t          |j        d          sJ t          |j        d          sJ |                    |j        j         d|j        j                    p||dS )Nr   r   r>   rM   rL   r}   )op_countop_set)rT   r  r   r~   r^   r_   r`   r   r   r   r(  r   r  rM   rL   )r  r  r  r5  r   s        rX   get_ep_statsr    s    HUUF_$$&& 	K 	K!UX122 	GM 	K 	KDw/))MH4;555554;
33333JJ$+0II4;3GIIJJJJ	K !F333rW   _EXPORT_FLAGS_EXPORT_MODULE_HIERARCHYc                 F     t          j                    fd            }|S )Nc                  @   	 t          j                     } 	| i |}t          j                     }t          dd||z
  t          dt          |           n# t          $ r}t          |          }|j        dz   |j        z   }t          |          }|Mt          
                    t          |                     t          d|t          |          t                     n%t          d|t          |          t                     |d }~ww xY w	 d ad an	# d ad aw xY w|S )Nzexport.time)rN  metricsrO  r}   zexport.error.classified)rN  r4  messagerO  zexport.error.unclassifiedrV   )timer.   r  r  r   r4  rM   rN   r   logerrorr   ra   r  )
r   r   startr  endra  t
error_typerU  fns
            rX   wrapperz$_log_export_wrapper.<locals>.wrapper  ss    	,IKKET$V$$B)++C #e#  r""	     	 	 	QA+an<J5a88I$		0;;<<< 3#FF'	     !5#FF'	    G'	6 !M'+$$ !M'+$++++	s+   AA D 
D!B&DDD D)r  wraps)r  r  s   ` rX   _log_export_wrapperr    s9    _R$ $ $ $ $L NrW   c                     t          | t          t          t          f          s| f} nKt          | t                    rt          |           } n&t          | t          j        t          f          r|| f} |i }| |fS r   )r~   rV  r  r  r^   rb   )example_inputsexample_kwarg_inputss     rX   $_process_jit_trace_inputs_for_exportr    s    nudD&9:: 
+(*	ND	)	) +~.. 	>EL$#788+ ((*#!///rW   c                 d   |                      d          }t          |t                    s*t          t          j        dt          |                     ||ni }t          j        ||f          \  }}t          |t          j
        j                  r|                    | ||          }|||||fS )NT)	keep_varsrP  )
state_dictr~   rV  r   r   rW  r4  rk   tree_flattenr^   r   ShapesCollectionrJ  )r#  r   r   rJ  original_state_dictr   original_in_specs          rX   _process_export_inputsr     s    ..4.88dE"" 
'\PTUYPZPZ\\
 
 	
 )VVrF -tVn==A.%,"?@@ J'66sD&II)+>NNrW   export_artifactr  strict_mode_exportc                    | j         j        }| j         j        }| j        }| j        }i }|                                D ]*\  }	}
|st          |	          n|	}t          dg g d|
||<   +t          |          dk    r5|st          |            t          ||          |          }|J |j        }t          J t          t          |||          }||fS )z
    In-place modify the graph module in export_artifact, remove _export_tracepoint nodes and
    return module_call_graph.
    )r?  r@  r   NrV   )rg   r[   r\   rj   rh   r   rw   rD   r   r   r   r  r  rB  )r  r  rC  r  r[   r  rj   rh   r:  r.  specsmod_fqnresmodule_call_graphs                 rX   _get_module_call_graphr  &  s/     /36B3B3G3K 	)  )1H  '--// 
 

U*<E+c"""#*= +
r+
 +
%*+
 +
w'' )**Q..! 	)$R(((T$%;=STTUWXX#//// 	     rW   r_  c                     | j         j        }| j         j        }| j        }t	          d t          |j                  D             t          |j                            }t          |||||          }|S )Nc              3   J   K   | ]\  }}|j         t          j        k    |V  d S r   )r   rB   r  )r   r  ss      rX   r  z)_get_range_constraints.<locals>.<genexpr>W  sB       	
 	
1v--- ----	
 	
rW   )	rg   r[   r\   ri   r   r  r   r   r   )r  r_  rJ  r[   r  ri   
num_liftedrange_constraintss           rX   _get_range_constraintsr  P  s      /36B3B3G3K / 9I	
 	
!"8"DEE	
 	
 	

 	".// J )
  rW   c                 d    | j         J d | j         j                                        D             S )Nc                 8    i | ]\  }}t          |          ||S rV   )r6   r  s      rX   r   z+_get_inline_constraints.<locals>.<dictcomp>j  s<       Aq ##	1  rW   )r   var_to_ranger   )ri   s    rX   _get_inline_constraintsr  h  sD    *** '4::<<   rW   objc              #      K   | j         }|                    | | j                  | _         	 dV  || _         dS # || _         w xY w)znHelper method to make it easier to cleanly torch.export() a method on a
    module that is not `forward`.
    N)r  __get__	__class__)r  
new_methodoriginal_methods      rX   patch_forwardr  q  sX       kO $$S#-88CK& &o%%%%s	   8 	Ac               #     K   t           j                                        } t           j                            d           	 d V  t           j                            |            d S # t           j                            |            w xY wrm   )r^   r  _jit_texpr_fuser_enabled_jit_set_texpr_fuser_enabled)original_states    rX   _temp_disable_texpr_fuserr    su      X6688N	H))%000>--n=====--n====s   A& &!Bc                   $     e Zd Z fdZd Z xZS )_WrapperModulec                 V    t                                                       || _        d S r   )super__init__rI  )r   rI  r  s     rX   r#  z_WrapperModule.__init__  s$    rW   c                      | j         |i |S r   )rI  )r   r   r   s      rX   r  z_WrapperModule.forward  s    tvt&v&&&rW   rL   rM   rN   r#  r  __classcell__)r  s   @rX   r   r     sG            ' ' ' ' ' ' 'rW   r   c           	      b   t                      5  ddlm} t          ||          \  }}t	          | |t
          j        j        f          r2t          | ||dd          	                                cd d d            S t	          | t
          j
                  rt	          |                                 t
          j        j        t
          j        j        f          rt          |                                 |           5  t          |                                 ||dd          	                                cd d d            cd d d            S # 1 swxY w Y   n?t          t          |           ||dd          	                                cd d d            S 	 d d d            d S # 1 swxY w Y   d S )Nr   )TopLevelTracedModuleFT)rx  ri  )r  torch.jit._tracer(  r  r~   r^   r  ScriptModule_exportmoduleScriptMethodownerr   r   r  r   )traced_callabler   r   r(  export_argsexport_kwargss         rX   "_convert_ts_to_export_experimentalr2    s   	"	$	$ ! !999999%I$PV%W%W"]o(<eh>S'TUU 	$(   fhh! ! ! ! ! ! ! ! );<< 	!!##eh&;UX_%MB
 B
 	 4466HH  #))++! (,   &((      #! ! ! ! ! ! ! !"         //$(   fhhC! ! ! ! ! ! ! !"#! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !s=   A F$<A>F$:8E2F$E	F$E	6F$$F(+F(rf  r  orig_in_specri  c
                 l    t          j        t          |          }
t          | |||||||||	|
          S )N)rf  )r#  r   r   rJ  rC  rf  r  r3  rE  ri  lower_to_aten_callback)r  r  r  _strict_export_lower_to_aten_ir)r#  r   r   rJ  rC  rf  r  r3  rE  ri  lower_to_atens              rX   _strict_exportr8    sR     %&8|TTTM*%'E!/!0W/,   rW   r5  c           
         t          | ||||d|d          }t          |||          \  }}}t          ||          }|j        j        D ]u}|j        dk    rhd|j        vr_t          ||j                  }t          |t          j        j                  s+|
J d            |                    |d          |j        d<   v|j        x}}|J |J |j        t           t"          fvrt%          j        t"          d |g          }|j        j        j        j        }t/          t1          ||j        |                    |j        _        |                                 t7          |t          |                      t9          |          }t;          |           }t=          | |          }t?          |           }d |                                 D             fd	|D             |_!        |5   |
|tE          |||          i ||          }d d d            n# 1 swxY w Y   |j#        }|j$        }|j%        }tM          |||           tO          tQ          | )                                          tU          |+                                          ||
           tY          ||           t[          | ||           t]          |||           t_          ||           ta          ||||j        d                   S )NF)rC  rF  rE  rG  r&  r   zbCannot find dynamo_fake_mode. This could be due to the exported graph module have no placeholders.Tr  c                     i | ]\  }}||	S rV   rV   )r   tracedorigs      rX   r   z3_strict_export_lower_to_aten_ir.<locals>.<dictcomp>.  s    WWWLFD4WWWrW   c                 (    h | ]}|v |         S rV   rV   )r   r   reverse_name_lookups     rX   r  z2_strict_export_lower_to_aten_ir.<locals>.<setcomp>/  s4     2 2 2&&& 	D!&&&rW   )r  r  r  r]   rj   rg   rh   ri   rj   )1rL  r   r  r   r   r   r   r)  r   r~   r^   r   r   r  	_out_specr4  r  rV  rk   r<   _codegenpytree_info	orig_argsr9   r:   _in_specr  r   r   r   r   r  r   r  r   r[   r\   r]   r    r  rT   r  r  r   r   r  r  r$  rf   )r#  r   r   rJ  rC  rf  r  r3  rE  ri  r5  r   r   r   dynamo_fake_moder   r   r/  rh   orig_out_specr   params_buffers_to_node_metarj  r   non_persistent_buffersaten_export_artifactr[   r  r]   r>  s                                @rX   r6  r6    s    )'E0W	 	 	N  	^T6::	 11A>RR $* 
 
7j  U$)%;%;>4;77DdEHO44 $00w 100#3#?#? $@ $ $	% 
  .77H} $$$ }T5M))?5$
;;#)2>HN$2#	
 	
% %N! ~tCyy999"@"P"P ,C00N)B3)W)W
 9==WW<N<T<T<V<VWWW2 2 2 2*2 2 2N.
 
 
 
55'	;OO 
  

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
	 B15$.I-#R)?   %S[[]]++ < < > >??.	      24JKKK 5#Y  
 ^%;YGGG B 6777!"(-.AB	   s   &HHHc                     | S r   rV   rx   s    rX   rc  rc  m  rd  rW   c           
         t           d             }d }t          j        j        j                            | |ddd          5  t                      5   |            5  t          t          | 	                    d                              }	t          t          | 
                    d                              }
|	|
z   }  ||          | |d|          \  }}t          | t          j        j                  r/t          | d          r|j                            | j                   d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   t#          j        ||f          }d	}|j        j        D ]G}|j        d
k    r:||k    r/|||z
           }t          |t          j                  s
||j        d<   |dz  }Ht/          ||t1          |                     }|rM	  ||           n@# t2          t4          f$ r,}t7          t8          j        t=          |                    d }~ww xY wt?          |          }t          j         j!        j"        spd}tG          |tI          j%        tL          |                    5  tO          ||j(        dtS          |j                   d           d d d            n# 1 swxY w Y   |*                                D ]p}t          |t          j        j                  s"|j        j        D ]A}|j        dv r6|j        +                    dd            |j        +                    dd            BqtY          |          }|                    t[          |||                     t]          |||||           t_          ||| ||||           ta          |||          S )Nc               3      K   t           j        j        } 	 dt           j        _        d V  | t           j        _        d S # | t           j        _        w xY wrm  rn  rq  s    rX   rs  z<_export_to_aten_ir_make_fx.<locals>._compiling_state_contexto  rt  ru  c                 ^   ddl m} |pi }t          |                     d                    }t          |                     d                    }i ||}t          j        |          \  }}	t          |          }t          |          }
t          |          }t          |          }t          | |	|d          }g }|
                    |           |
                    |           t          |||          \  }t          j        ||f          \  }}t          j                  fd            }t                      5   t          |dd          | }|j                                         d d d            n# 1 swxY w Y   t%          |          }t'          |          } |t)          |          t)          |          ||d          |t          t+          |d|
         |                    t          t+          ||
|
|z            |                    i i ||d g g 	          }||fS )
Nr   )GraphSignatureFr   T)store_orig_modc                  (    t           |            S r   )rV  )r   flat_fns    rX   
wrapped_fnzG_export_to_aten_ir_make_fx.<locals>._make_fx_helper.<locals>.wrapped_fn  s    $(((rW   )record_module_stackrf  )r  r  user_inputsr  inputs_to_parametersinputs_to_buffersbuffers_to_mutateuser_inputs_to_mutater9  rh   backward_signaturer  output_tokens)&torch._functorch._aot_autograd.schemasrN  r  r   r   rk   r  rV  r   r'   r  r(   r  r  r   r4   r   eliminate_dead_coder%   r&   r  r   )r#  r   r   rO  rN  r   r   params_and_buffersparams_and_buffers_flatparams_spec	param_len
buffer_len
params_lenfunctional_callparams_buffers_argsrh   	flat_argsr9  rR  r[   input_namesoutput_namesr\   rQ  s                          @rX   _make_fx_helperz3_export_to_aten_ir_make_fx.<locals>._make_fx_helperx  s   IIIIII2 4 4e 4 L LMMS...FFGGB 0BMB/5/BCU/V/V,"'(?"@"@())	''
+,,
0j
 
 
 *,""#:;;;""4(((40&
 
 $02Ev1NOO	7		!	!	) 	) 	) 	) 
"	!	) &'' 	+ 	+$(!   	B
 H((***	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ ),,*2..n,--''#JKK0%!%c+a	k*BDT&U&U!V!V"K	I
,B BC]SS  !"$#
 
 
" 3ws   /E??FFTrv  Fr   )rz  r   r   r   r   r   r>   r  r  r  r  r   r   r  )1r   r^   r   r  r  r  rt   r   r  r   r   r~   r_   r`   r(  r   r~  rk   r  r   r   r   rb   rE   r  r5   r=   r   r   r]  ra   r*   rX  rY  rU   r   r  r  r   r;   r   r3   r   r  r   r   r  r!   rZ   )r#  r   r   r   rj  r  re  rs  rh  r`  ra  rb  r[   r  re  r  r   r  r  ra  ri   r  r  r]   s                           rX   _export_to_aten_ir_make_fxri  f  sL    : : ^:= = =D 
	!	7	7 
8 
 
 % %   % % #;":"<"<% % S1151IIJJKK	c///GGHHII
+
8ii88	
 
 
O c58/00 	%WS&5I5I 	%GNN38$$$'% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %* "I{#;<<IE  7m##
""$UZ%78!(EL99 0'/DIe$QJE?8== 
  H	H##B''''(/: 	H 	H 	HM>AGGG	H !++I=; 1 	 %	!"5;OOO
 
 	 	 ,#T%H%R%RTT	   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 

 3 3$ 455 	J$ 	3 	3Dw333	/666	mT222	3
 +2..I(-C^TTUUU 
"$7I  
 
   
  s   E%ECD7+E7D;;E>D;?EE%E	E%E	E%%E),E)=H	 	I'II.KKK
aot_exportdispatch_tracing_modec                 (  	 |
dv sJ di fd}t          | |||	|          \  }}t          |           }	fd}5  t                      5  t          j        j                            d          5  t          | ||          5 \  }}}}|
dk    rt          nt          j
        t          |		          } ||||||||
          }fd|j                                        D             |_        ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   t          | |j        |j                   J t!          |          S )u   
    ``dispatch_tracing_mode`` can be either "make_fx” or “aot_export”, corresponding to
    _export_to_aten_ir_make_fx and _export_to_aten_ir, respectively.
    )r4   rj  Nc                      d fd	}|S )Nc                 T   |pi } G fddt           j        j                  } ||           }d D             }t          ||          5   ||fd|i|\  }}t                              d|           d d d            n# 1 swxY w Y   t          j        t          |j	                  |_	        t          j        t          |j
                  |_
        t          j        t          |j                  |_        t          j        t          |j                  |_        t          j        t          |j                  |_        |j        j        D ]V}	d|	j        v rK|	j        d         }
d t          j        t          |
                                          D             |	j        d<   W||fS )Nc                   *     e Zd Z fdZfdZ xZS )]_non_strict_export.<locals>._tuplify_outputs.<locals>._aot_export_non_strict.<locals>.Wrapperc                 V    t                                                       || _        d S r   )r"  r#  r|   )r   r#  r  s     rX   r#  zf_non_strict_export.<locals>._tuplify_outputs.<locals>._aot_export_non_strict.<locals>.Wrapper.__init__2  s'    GG$$&&&(+D%%%rW   c                    t          | j        t          j        j                  rjt          j        j                                        5  t          j                            | j                  j        |i |}d d d            n# 1 swxY w Y   n | j        |i |}t          j
        |          \  }t          |          S r   )r~   r|   r^   r_   r`   	tracebackpreserve_node_metaInterpreterrunrk   r  rV  )r   r   r   tree_out	flat_outsrh   s        rX   r  ze_non_strict_export.<locals>._tuplify_outputs.<locals>._aot_export_non_strict.<locals>.Wrapper.forward6  s    !$"3UX5IJJ F"X/BBDD  ',x';';D<M'N'N'R!%()/( (H              
 $54#4d#Ef#E#E*0*=h*G*G'Ix +++s   	/BBBr%  )r  rh   s   @rX   Wrapperrp  1  sR        , , , , ,
, 
, 
, 
, 
, 
, 
, 
, 
,rW   ry  c                     g | ]}d |z   S )z_export_root.rV   )r   r  s     rX   r   z`_non_strict_export.<locals>._tuplify_outputs.<locals>._aot_export_non_strict.<locals>.<listcomp>E  s*     - - -()!#- - -rW   r   z%Exported program from AOTAutograd:
%sr   c                 4    i | ]\  }}t          |          |S rV   )rz   )r   r   r   s      rX   r   z`_non_strict_export.<locals>._tuplify_outputs.<locals>._aot_export_non_strict.<locals>.<dictcomp>Y  s4     4 4 4$C #34 4 4rW   )r^   r   r   r$   r  debugrk   tree_maprw   r  r  rV  rU  rW  r   r   r   r   )r#  r   r   rO  ry  wrapped_modnew_preserved_call_signaturesr[   r\   r   r   rj  rj   rh   rC  s              rX   _aot_export_non_strictzL_non_strict_export.<locals>._tuplify_outputs.<locals>._aot_export_non_strict.  s   \rF, , , , , , ,%(/ , , ," "'#,,K- --K- - -) ":<M  H H %*[$OOvOOOC		BBGGG	H H H H H H H H H H H H H H H $_[#.IICN /+s{CCCK$*OKAV$W$WC!'-S5( (C$ %+OKAV$W$WC!  $	11&*i0A&BO4 4(.') )%''	4 4 4DI/0 s7Ns   +BBBr   rV   )rj  r  rj   rh   rC  s   ` rX   _tuplify_outputsz,_non_strict_export.<locals>._tuplify_outputs-  s:    2	 2	 2	 2	 2	 2	 2	 2	 2	h &%rW   )ri  rE  c                 .    t          |           S )N)ri   r[   rJ  equalities_inputsoriginal_signatureri  )r   )r[   ri  r  ri   r  r`  s    rX   _produce_guards_callbackz4_non_strict_export.<locals>._produce_guards_callbackv  s,    35/1 3
 
 
 	
rW   F)assume_static_by_defaultr4   )rf  ri  )r  re  c                 V    i | ]%\  }}|t          |t                    r|         n|&S rV   )r~   r+   )r   r.  r  map_fake_to_reals      rX   r   z&_non_strict_export.<locals>.<dictcomp>  sK     . . .C j>N.O.OX%c**UX. . .rW   r?  )r   r  r   r^   rX  rY  rZ  r   ri  r  r  r  r]   r   r  r\   rf   )r#  r   r   rJ  rC  rf  r  r3  rE  ri  rk  r  r   r   r   r  patched_modnew_fake_argsnew_fake_kwargsnew_fake_constant_attrs_to_aten_funcrI  r  ri   r  rj   r  rh   r`  s       `    `            @@@@@@@rX   _non_strict_exportr    sq   " !$=====#'H?A5& 5& 5& 5& 5& 5& 5&| 	/0W	 	 	" 1C@@
 
 
 
 
 
 
 
 
 
    244    em6J6P6P!& 7Q 7 7     $CKKK 	 P
# )I55 +*&&!-(;    $1=#'(@*$ $ $ . . . . 4 > D D F F. . . *5	 	 	 	 	 	 	 	 	 	 	 	 	 	 	                                                                                         D 5!%';'E   !+	   s   E!&E D)A,D	D)DD)DD)E )D--E 0D-1E 4E E	EE	EEE)rx  rC  rx  c                   t          |           at          | |||          \  }}}}}|r t          j        t
          t                    nt          j        t          d          } || ||||d||dd
  
        }	|	j        j	        }
t          | ||          }t          |	j                  }||	j        j        j        d<   t          |	t!          | ||d          |          }t#          |	|||          \  }}||j        d<   t%          |           t'          |           t)          ||
           d	d
lm} t/          ||j        |
|||||f|	j        j        |g	  	        }|S )N)r5  r4   )rk  F)
r#  r   r   rJ  rC  rf  r  r3  rE  ri  inline_constraintsri  forward_arg_namesr   )TrainingIRVerifier	r   r   r  r  r  r  r  r]   	verifiers)r7  r  r   r  r  r6  ri  r  rg   r\   r  r  ri   r[   r   r  r0   r  r  r  r  torch._export.verifierr  rA   r   r]   )r#  r   r   rJ  rx  rC  r3  r  export_funcr  r  r  r  r  r[   r  r  exported_programs                     rX   _export_for_trainingr    s     5S99 	sD&.AA 		
	+#=	
 	
 	
 	

 "+
 
 
  "k%'E/!05!  O -15.sD&AA01JKK :LO !56.c4UCCC  3'Ev B
 $5BG Bb"8999999999&h.&++f~!&0%&
 
 
 rW   )rx  rC  rf  rE  ri  c                *   t          |           at                      }	|	                    |rdnd           |	                    |rdnd           |	at          dt                     t          | |||          \  }}}
}}|rt          nt          } || |||||||
||
  
        }|j	        j
        }|st          | ||          nd}t          |j                  }||j	        j        j        d<   t!          |t#          | |||	          |          }t%          ||
||          \  }}||j        d
<   t'          |           t)          |           |st+          ||           t,          j        j                            |d           ddlm} t9          | t,          j        j                  r@t?          | d          r0d| j        v r'|j                             d| j        d         i           tC          ||j"        ||||||f|j	        j#        |g	  	        }|S )a
  
    Traces either an nn.Module's forward function or just a callable with PyTorch
    operations inside and produce a ExportedProgram.

    Args:
        f: the `nn.Module` to trace.

        args: example positional inputs.

        kwargs: optional example keyword inputs.

        dynamic_shapes:
         An optional argument where the type should either be:
         1) a dict from argument names of ``f`` to their dynamic shape specifications,
         2) a tuple that specifies dynamic shape specifications for each input in original order.
         If you are specifying dynamism on keyword args, you will need to pass them in the order that
         is defined in the original function signature.

         The dynamic shape of a tensor argument can be specified as either
         (1) a dict from dynamic dimension indices to :func:`Dim` types, where it is
         not required to include static dimension indices in this dict, but when they are,
         they should be mapped to None; or (2) a tuple / list of :func:`Dim` types or None,
         where the :func:`Dim` types correspond to dynamic dimensions, and static dimensions
         are denoted by None. Arguments that are dicts or tuples / lists of tensors are
         recursively specified by using mappings or sequences of contained specifications.

        preserve_module_call_signature: A list of submodule paths for which the original
            calling conventions are preserved as metadata.

        allow_complex_guards_as_runtime_asserts:
         With the current dynamic shapes language for dims and derived dims, we can run into constraints
         that are not expressible with the language. For example, flattening a matrix and adding to a vector,
         both fully dynamic (i.e. x.reshape([-1]) + y) emits a guard s0 * s1 = s2, which is not expressible.
         By default, we either raise a constraint violation error or specialize to static values.
         If this flag is set to True, we avoid erroring out and instead allow complex constraints to exist as runtime
         assertions in the graph. The sympy interpreter (torch/utils/_sympy/interp.py) will produce the math ops
         required to compute and assert the value of the guard (e.g. sym_size_int, eq, _assert_scalar).
         Additionally, if TORCH_DYNAMO_DO_NOT_EMIT_RUNTIME_ASSERTS=1 is specified, we will allow complex constraints
         while not emitting runtime asserts, returning a cleaner graph with lesser guarantees around dynamic shapes.

    Returns:
        An ExportedProgram containing the traced method.
    rx  
non_strictrf  aot_dispatchzexport.enterrM  Nr  r  r  T)in_placer   )Verifierr   customr  )$r7  r  rT   r  r  r.   r   r8  r  rg   r\   r  r  ri   r[   r   r  r0   r  r  r  r  r^   r+  r  remove_proxy_from_state_dictr  r  r~   r_   r`   r(  r~  rA   r   r]   )r#  r   r   rJ  rx  rC  rf  rE  ri  rO  r  r  r  r  r  r  r  r  r[   r  r  r  s                         rX   r+  r+  	  s   v  5S99EEE	II&2hhl333	II@nn.AAAM>???? 	sD&.AA %+B..0BK!k&/ O 4C3G3K :MVsD&111RV  11JKK :LO !56.c4=PQQQ 
 3)+I6 B
 $5BG B >!"&<=== 
M445HSW4XXX////// 	3,--7C  7   
#(8"45666&h.&++f~!&0*
 
 
 rW   )NNr   )rj  )rR   r  r   loggingr   r  warnings
contextlibr   r   typingr   r   r   r   r	   r
   r   r   r^   torch._dynamotorch.fxtorch.utils._pytreer  _pytreerk   torch._dispatch.pythonr   torch._dynamo.excr   r   torch._export.db.loggingr   r   torch._export.non_strict_utilsr   r   r   r   r   r   (torch._export.passes._node_metadata_hookr   r   -torch._export.passes.collect_tracepoints_passr   (torch._export.passes.lift_constants_passr   r   r   torch._export.utilsr   r   r    r!   r"   r  r#   torch._export.wrappersr$   4torch._functorch._aot_autograd.input_output_analysisr%   r&   9torch._functorch._aot_autograd.traced_function_transformsr'   $torch._functorch._aot_autograd.utilsr(   torch._functorch.aot_autogradr)   torch._guardsr*   "torch._library.fake_class_registryr+   torch._subclasses.fake_tensorr,   r-   torch._utils_internalr.   torch.export.dynamic_shapesr/   r0   r1   torch.export.exported_programr2   torch.fx._utilsr3   "torch.fx.experimental.proxy_tensorr4   %torch.fx.experimental.symbolic_shapesr5   r6   r7   r8   torch.fx.graphr9   r:   torch.fx.passes.runtime_assertr;   r<   torch.utils._sympy.value_rangesr=   
_safeguardr?   r  r@   rA   rB   rC   rD   r  rE   rF   	getLoggerrL   r  	dataclassrH   rZ   rf   r\  criticalr|  r  	exceptioninfowarningprintwarnrK   rt   rz   rw   r_   r`   r   r   r   r   r   r   r   ra   r   rb   rc   r  r  r$  r1  r7  rB  rP   rL  r  r*  r  r  r  r  r  r  r  r  r  r  rQ   r  r  r  r   r  r  r  r  r  r   r2  r8  r6  ri  r  r  r+  rV   rW   rX   <module>r     s              				   2 2 2 2 2 2 2 2 I I I I I I I I I I I I I I I I I I I I       $ $ $ $ $ $ $ $ $ ; ; ; ; ; ; 6 6 6 6 6 6 6 6                              Q P P P P P         
              6 5 5 5 5 5 3 3 3 3 3 3             J I I I I I ; ; ; ; ; ; * * * * * * ? ? ? ? ? ? D D D D D D D D 2 2 2 2 2 2         
 5 4 4 4 4 4 ? ? ? ? ? ? 6 6 6 6 6 6            7 6 6 6 6 6 6 6 J J J J J J ( ( ( ( ( ( ; ; ; ; ; ; 5 5 5 5 5 5              V U U U U U U U g!! ' ' ' ' ' ' ' '  
 
 
 
 
 
 
 
 d###= = = = = = = $#=  2133 MMLKO	M
>  : ; ; ;( ( (  .!5 . . . .0#- #- #-L: : :	 	 	0 0 0f X_ 8?  
#s(^       F$,$,	$, c5</0$, Cu|-=u?QQRRS	$,
 I$, $, $, $,N-(-)- Cu|-=u?QQRRS- 
	- - - -6..). 
. . . .b!X_!5:X5I!	! ! ! !Hux 4S>    38n  !&9!9:	
 
/   ( (,MQ	E 79&+49"E E EE
S/E T#s(^$E U4S>5:tCy#HIJ	E %*#s(OE  $E .2E E E E XE E E E\ !r kr r r	r
 $r r r r rj  	  
#uU\58#556
67       0 (, 	
S/ T#s(^$ 
#Y	   >UX_ S    /%,'/S%,.// */ Cu|-=u?QQRRS	/ / / /,FhoF)F Cu|-=u?QQRRSF F F F(%(*> (4 ( ( ( (Veh&: t    4%("6 =Q    ,4_ 4c3h 4 4 4 4  %)xC! ( ( (59 (4S>2 9 9 9( ( (V0 0 0$O O O"'!#'!'! %*#s(O'! 	'! '! '! '!T#48cN   0~     &ux & & & &" > > >' ' ' ' 'UX_ ' ' '" " " "J	
S/ cN U4S>5:tCy#HIJ	
 %*#s(O  c3h  .2     8L	L
S/L cNL U4S>5:tCy#HIJ	L
 %*#s(OL L c3hL L .2L L %L L L L Lj !kn n	n
 $n n n n nx ".V V	V
S/V cNV U4S>5:tCy#HIJ	V
 %*#s(OV V c3hV V .2V V V V V V Vt  (,MQ	S 68S S S	S
S/S T#s(^$S U4S>5:tCy#HIJ	S S %*#s(OS S S S   Sl  (,MQ	L 6849 %L L L	L
S/L T#s(^$L U4S>5:tCy#HIJ	L L %*#s(OL L .2L L L L L   L L LrW   