
    ק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Zd dlZd dlZd dl	Z	d dl
Z
d dlZd dlZd dlmZ d dlmZ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Z d dlZd dl m!c m"Z# d dlm$Z$ d dlm%Z%m&Z&m'Z' d dl(m)Z) d dl*m+Z+ d dl,m-Z- d d	l.m/Z/m0Z0m1Z1 d d
l2m3Z3 d dl4m5Z5 ddl6m7Z7m8Z8mZ9m:Z: ddl;m<Z<m=Z= ddl>m?Z?m@Z@mAZAmBZB ddlCmCZC ddlDmEZE ddlFmGZG ddl8mHZHmIZImJZJmKZKmLZL ddlMmNZNmOZO ddlPmQZQ ddlRmSZSmTZT ddlUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\m]Z]m^Z^m_Z_m`Z`maZambZb ddl!mcZcmdZdmeZemfZfmgZgmhZhmiZimjZjmkZkmlZlmmZmmnZnmoZompZpmqZqmrZrmsZsmtZt ddlumvZv ddlwmxZxmyZymzZzm{Z{m|Z| ddl}m~Z~ ddlmZ ddlmZ ddlmZmZmZmZ ddlmZ erd d lmZ  ej        e          Zej                            ed!          Zej                            ed"          Zej                            ed#          Zej                            ed$          Z ed%&           G d' d(                      Z G d) d*          Z ej        d          d+             Ze G d, d-                      Zd. Z G d/ d0ej        j                  Z G d1 d2          Zeeef         Z G d3 d4          Zd5Zd6 Z ej                    Z G d7 d8e$j                  ZdS )9    N)	dataclass)	AnyCallableDictListOptionalSetTupleTYPE_CHECKINGUnion)fx)GlobalContextCheckpointStateSourceTracingContext)signpost_event)_make_graph_module)BackwardState)free_symbolsis_symbolicShapeEnv)insert_deferred_runtime_asserts)is_traceable_wrapper_subclass   )configexclogging	variables)
CompiledFn
CompilerFn)create_call_functioncreate_instructionInstruction	unique_id)code_context)	PyCodegen)enter_new_scope)BackendCompilerFailed!exceptions_allowed_to_be_fallback	SkipFrameunimplementedunimplemented_with_warning)GuardBuilderinstall_guard)is_dynamic_nn_module)AttributeMutationExistingSideEffects)
AttrSourceBackwardStateSourceConstantSourceGetItemSourceGlobalStateSourceis_constant_sourceis_from_local_sourceLocalSourceParamBufferSourceShapeEnvSourceSyntheticLocalSourceTensorPropertyTensorPropertySource)_extract_tensor_dictcheckpoint_paramsCleanupHookclone_inputscount_callscountersdynamo_timedget_instruction_source_311get_locals_to_stealget_static_address_typeget_torch_function_mode_stackgraph_break_reasonsincrement_op_countlazy_format_graph_code
LazyStringnn_module_proxysameset_example_value)VariableTracker)BackwardStateGraphArgGraphArgTrackedFakeVariableBuilderwrap_fx_proxy)BaseListVariable)NullVariable)NNModuleVariable)NumpyNdarrayVariableSymNodeVariableTensorVariableUnspecializedPythonVariable)TensorWithTFOverrideVariable)InstructionTranslatorBasegraph
graph_codegraph_sizes
trace_callT)frozenc                   $    e Zd ZU eed<   eed<   dS )VariableTrackerCacheKeyvt_idsourceN)__name__
__module____qualname__int__annotations__r        V/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/_dynamo/output_graph.pyre   re   y   s$         JJJ NNNNNrn   re   c                   ,    e Zd Zd Zd Zd Zd Zd ZdS )VariableTrackerCachec                     i | _         d S N)cacheselfs    ro   __init__zVariableTrackerCache.__init__   s    


rn   c                 l    t          t          |          |          }|| j        vrd S | j        |         S rs   re   idrt   )rv   valuerg   keys       ro   lookupzVariableTrackerCache.lookup   s5    %bii88dj  4z#rn   c                 T    t          t          |          |          }|| j        |<   d S rs   ry   )rv   r{   rg   vtr|   s        ro   addzVariableTrackerCache.add   s&    %bii88
3rn   c                 `    t                      }|j                            | j                   |S rs   )rq   rt   update)rv   	new_caches     ro   clonezVariableTrackerCache.clone   s+    (**	tz***rn   c                 8    | j                                          d S rs   )rt   clearru   s    ro   r   zVariableTrackerCache.clear   s    
rn   N)rh   ri   rj   rw   r}   r   r   r   rm   rn   ro   rq   rq      s_                    rn   rq   c                  4    t          j        t                    S rs   )torchdynamo_loggingget_step_loggerlogrm   rn   ro   _step_loggerr      s    .s333rn   c                   R    e Zd ZU dZeed<   eej                 ed<   dZ	e
ed<   d ZdS )GraphCompileReasonzOStores why a given output graph was compiled; i.e. what caused the graph break.reason
user_stackTgraph_breakc                 @    | j         rt          j        |            d S d S rs   )r   rI   appendru   s    ro   __post_init__z GraphCompileReason.__post_init__   s.     	-&t,,,,,	- 	-rn   N)rh   ri   rj   __doc__strrl   r   	tracebackFrameSummaryr   boolr   rm   rn   ro   r   r      sZ         YYKKKY+,,,, K- - - - -rn   r   c                       fd}|S )Nc                      d  D             S )Nc                 &    g | ]\  }}} ||i |S rm   rm   ).0fnargskwargss       ro   
<listcomp>zE_get_gen_rand_values_fn.<locals>._gen_rand_values.<locals>.<listcomp>   s.    III(8D&D#F##IIIrn   rm   )random_callss   ro   _gen_rand_valuesz1_get_gen_rand_values_fn.<locals>._gen_rand_values   s    IILIIIIrn   rm   )r   r   s   ` ro   _get_gen_rand_values_fnr      s)    J J J J J rn   c                   R     e Zd ZdZdeeej        j        f         f fdZ	d Z
 xZS )FakeRootModulez'Trick the constructor of fx.GraphModule
nn_modulesc                     t                                                       |                                D ]\  }}t          | ||           d S rs   )superrw   itemssetattr)rv   r   kv	__class__s       ro   rw   zFakeRootModule.__init__   sW    $$&& 	  	 DAqD!Q	  	 rn   c                     dS )NzFakeRootModule(...)rm   ru   s    ro   __repr__zFakeRootModule.__repr__   s    $$rn   )rh   ri   rj   r   r   r   torchnnModulerw   r   __classcell__r   s   @ro   r   r      sg        11 4UX_(<#=            
% % % % % % %rn   r   c                   T    e Zd ZdefdZdej        j        deej	                 fdZ
dS )WrapperBackendbackendc                     || _         d S rs   )r   )rv   r   s     ro   rw   zWrapperBackend.__init__   s    #*rn   gmexample_inputsc                 x   t          |          | _        || _        t          j        | j                  }|                     ||          | _        | j        | j        | j        j        u r| j        j        S t          j	        s| j        S 	  | j        j        t          |           } | j        t          |           }t          ||          r| j        |                                  S t          d|            # t          $ r t                              d            w xY w# |                                  w xY w)Nzincorrect results of backend zerror in verify_correctness)r?   restorer   copydeepcopyr   	candidateforwardr   verify_correctnessrA   rN   RuntimeError	Exceptionr   	exception)rv   r   r   copy_gmcorrectresults         ro   __call__zWrapperBackend.__call__   s1   (,,-((g~>>>!T^tw%F%F7?"( 	">!	%dgo|N'C'CDG#T^\.%A%ABF GV$$ &~ LLNNNN EtEEFFF  	 	 	MM7888	 LLNNNNs   
A	C: (C: :&D  D# #D9N)rh   ri   rj   r   rw   r   r   GraphModuler   Tensorr   rm   rn   ro   r   r      s]        +
 + + + +58/ elAS      rn   r   c            
       l    e Zd ZdZdeeef         dee         de	de
de
f
 fdZd ZdQd	efd
Zd Zd Zd Zdeg ef         fdZd Zed             Zed             Zd Zed             Zej        d             Zed             Zed             Zd Zd Zd Ze j!        d             Z"ed             Z#ed             Z$ed             Z%ede&j'        j(        fd             Z)edeeef         fd!            Z*dRd#Z+d$ Z,d% Z-ed&             Z.d'e/fd(Z0d) Z1d* Z2d+ Z3dSd-Z4d. Z5e6d/             Z7d0e8e&j9        j:        e&j;        ef         fd1Z<d2 Z=	 dTd4ee>         fd5Z?d6 Z@d7 ZAd8 ZBd9efd:ZCe j!        d;             ZDd< ZEd= ZFedeGeHjI                 fd>            ZJedeGe/         fd?            ZKd@eHjL        deMfdAZNd@eHjL        deMfdBZOdeGe&j;                 fdCZPdUdDZQdEeGeR         dd"fdFZSdUdGZTdefdHZUdefdIZVdUdJZWdKe	dd"fdLZXdMeeHjL        gd"f         dd"fdNZYdOe&jH        jI        fdPZZ xZ[S )VOutputGrapha  
    Wrapper class to hold outputs of InstructionTranslator.  Mainly the
    generated fx.Graph.

    OutputGraph is 1:1 with a frame being processed. Each frame is associated
    with some root InstructionTranslator. When user code calls a function,
    we construct a InliningInstructionTranslator that continues to write into
    the root InstructionTranslator's OutputGraph.
    code_optionscompiler_fnexportlocal_scopeglobal_scopec
                    t                                                       t          | |          g| _        i | _        || _        || _        || _        i | _        g | _	        t          t                    | _        t                      | _        |	j        |	j        |	j        d| _        g | _        t                      | _        t+          | j        t,          j        t,          j        t,          j        t,          j        | j                  }
dd lmc m} |                    d          5  t<          j                             |
| j        rdnd| j                  }d d d            n# 1 swxY w Y   tC          |          | _"        | #                                 tI          j%        tL                    | _'        i | _(        tS                      | _*        tW                      | _,        t[          j.                    | _/        ta          |          | _1        g | _2        d| _3        g | _4        || _5        || _6        || _7        || _8        i | _9        g | _:        g | _;        d| _<        i | _=        t<          j>        ?                                | _@        t<          j>        A                                | _B        t                      | _D        d| _E        t          i           | _F        t          i           | _G        | H                                 i | _I        g | _J        d | _K        g | _L        i | _M        d | _N        d | _O        | P                                | _Q        t                      | _R        d S )	N)export_root)co_nameco_filenameco_firstlineno)tracked_fakesallow_scalar_outputsallow_dynamic_output_shape_ops+prefer_deferred_runtime_asserts_over_guards'allow_complex_guards_as_runtime_asserts	co_fieldsr   F(fake_tensor_allow_unsafe_data_ptr_accessT)	shape_envallow_non_fake_inputsr   )Sr   rw   SubgraphTracertracersinput_source_to_varr   export_constraintsframe_stateinput_source_to_sizes_stridescleanup_hooksnext_compile_id_counter
compile_idsetinstalled_globalsr   r   r   r   r   bound_symbolsr   r   capture_scalar_outputs capture_dynamic_output_shape_opsr   r   torch._functorch.config
_functorchpatchr   _subclassesFakeTensorModer   tracing_contextinit_ambient_guardscollectionsdefaultdictlisttracked_fakes_id_to_sourceparam_name_to_sourcer0   side_effectsrq   variable_tracker_cache	itertoolscountunique_var_iddictr   output_instructions	timestampregister_finalizer_fnsr   r   r   root_txsource_to_user_stacks_current_txcleanupsshould_exitunspec_variable_map_C_is_torch_function_enabledtorch_function_enabled_is_torch_function_mode_enabledtorch_function_mode_enabledrH   torch_function_mode_stack!has_user_defined_allowed_in_graphnon_compliant_opscompliant_custom_opssave_global_state dynamo_flat_name_to_original_fqnr   random_values_varpregraph_bytecodebackward_statebackward_state_proxybackward_state_var!install_builtins_dict_in_fglobals%name_of_builtins_dict_key_in_fglobalsguard_on_key_order)rv   r   r   r  r   r   r   r   r   f_coder   _config	fake_moder   s                ro   rw   zOutputGraph.__init__   s    	&t@@@A CE "4&KM*68#$788+.55
 ~!-$3
 
 13 14 ,!'!>+1+R8>8j4:4bn
 
 
	$ 	211111111]]E]JJ 	 	)88#.2k&Dddu{	 9  I	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 0>i/H/H  """ #D)) 	' BD!'MM ';&<&<#&_.. ..68   OQ# 2=(& RT"<>+- KM  ',h&I&I&K&K#+08+S+S+U+U()F)H)H& 27. >AWW ADB! 	    AC-  	 "& 57 ;=>B!15 2244 	2 -0EEs   )1E&&E*-E*c                     | j         d         }t          |t                    s|j        }|                     d|          S )N__builtins____builtins_dict__)r   
isinstancer  __dict__install_global)rv   
f_builtinss     ro   r  z-OutputGraph.install_builtins_dict_in_fglobals  sB     &~6
*d++ 	-#,J""#6
CCCrn   hookc                     | t          | j                   }|| j        vsJ || j        |<   ||                                 fS rs   )lenr  get_backward_state_proxy)rv   r(  prefixnames       ro   add_backward_state_hookz#OutputGraph.add_backward_state_hook  sU    4#d122444.....$(D!T224444rn   c                 n   | j         | j        rt          d           | j                            dt
          t                                | _         t                      | j         j        j	        d<   t          | j         j        t                                 |                                 | _        | j         S )Nz&backward_state does not support exportdynamo_backward_state)rg   grapharg)r  r   r*   root_tracercreate_graph_inputr   r2   rQ   nodemetarO   new_varr  ru   s    ro   r+  z$OutputGraph.get_backward_state_proxy  s    $,{ HFGGG(,(8(K(K'?R?T?T )L ) )D% ?T>U>UD%*/
;d7<mooNNN&*llnnD#((rn   c                 x   | j                             t                                          t          j                             | j                             t                                          t          j                             | j                             t                                          t          j                             | j                             t                                          t          j	                             | j                             t                                          t          j
                             t          j        j                                        }|E| j                             t                                          t          j                             d S d S rs   )guardsr   r:   
make_guardr,   	SHAPE_ENVr5   DETERMINISTIC_ALGORITHMS	GRAD_MODEDEFAULT_DEVICETORCH_FUNCTION_STATEr   r  r   peek_interpreter_stackFUNCTORCH_STACK_MATCH)rv   cis     ro   r   zOutputGraph.init_ambient_guards  sM    	((33L4JKKLLL**<+PQQ	
 	
 	
 	)++66|7MNNOOO)++66|7RSSTTT**<+LMM	
 	
 	
 X 7799>KOO!##..|/QRR     >rn   c                 |    | }|                                  }t          | j                                      fd                               t          t          j        j        |                     	                    t          |          d                               |           | j                                                                       t          |          } t!          | j        |          |          }t#          j                    j        j                            |           |S )z]
        call fn(*args) before the graph runs and turn the result into a fake input.
        c                  D                          j        j                  S rs   )load_import_fromri   rh   )cgr   s   ro   <lambda>z3OutputGraph.synthetic_graph_input.<locals>.<lambda>  s!    B''  rn   F)r6  r%   r  add_push_nullforeachmapr   ConstantVariablecreatecall_functionr*  storer  extendget_instructionsr;   rT   r   getguards_contextdynamo_guardsremove_guards_with_source)rv   r   r   example_valuevarnamerg   r   rE  s    `     @ro   synthetic_graph_inputz!OutputGraph.synthetic_graph_input  s,    D	,,..t|$$
    	
 	
 	
 	

3y18$??@@@
TE***
%%b&9&9&;&;<<<%g..6v66}EE+9SS	
 	
 	
 rn   r   c                 :    | j                             |           d S rs   )r   r   )rv   r   s     ro   add_cleanup_hookzOutputGraph.add_cleanup_hook  s    !!"%%%%%rn   c                 z    t          | j                  D ]} |             | j                                         d S rs   )reversedr   r   )rv   r(  s     ro   call_cleanup_hookszOutputGraph.call_cleanup_hooks  sD    T/00 	 	DDFFFF  """""rn   c                     | j         d         S Nr   r   ru   s    ro   r2  zOutputGraph.root_tracer  s    |Arn   c                     | j         d         S Nr^  ru   s    ro   current_tracerzOutputGraph.current_tracer  s    |Brn   c                 2    t          | j                  dk    S )Nr   )r*  r   ru   s    ro   is_root_tracerzOutputGraph.is_root_tracer  s    4<  A%%rn   c                     | j         j        S rs   rb  r_   ru   s    ro   r_   zOutputGraph.graph  s    "((rn   c                     || j         _        d S rs   rf  )rv   r{   s     ro   r_   zOutputGraph.graph  s    $)!!!rn   c                     | j         j        S rs   )rb  input_name_to_proxyru   s    ro   ri  zOutputGraph.input_name_to_proxy  s    "66rn   c                     | j         j        S rs   )rb  real_value_cacheru   s    ro   rk  zOutputGraph.real_value_cache  s    "33rn   c                 &     | j         j        |i |S rs   )rb  create_proxyrv   r   r   s      ro   rm  zOutputGraph.create_proxy&  s    /t"/@@@@rn   c                 &     | j         j        |i |S rs   )rb  create_nodern  s      ro   rp  zOutputGraph.create_node)      .t".????rn   c                 &     | j         j        |i |S rs   )rb  remove_nodern  s      ro   rs  zOutputGraph.remove_node,  rq  rn   c              #     K   t                      }	 |r|j        | j        u sJ |                                 |r|nt	          | | j        |          }| j                            |           |V  |                    d d d            | j                                         d S # |                    d d d            | j                                         w xY w)N)parentsource_target)	r&   ru  rb  	__enter__r   r   r   __exit__pop)rv   rv  prior_tracernew_scope_ctxtracers        ro   	subtracerzOutputGraph.subtracer/  s      '))	 B#*d.AAAAA##%%%  #!4M    L'''LLL""4t444L ""4t444Ls   AB# #2Cc                     | S rs   rm   ru   s    ro   outputzOutputGraph.outputD  s    rn   c                     | j         j        S rs   )r   r   ru   s    ro   r   zOutputGraph.fake_modeH  s    #--rn   c                 $    | j         j        j        S rs   )r   r   r   ru   s    ro   r   zOutputGraph.shape_envL  s    #-77rn   returnc                 $    | j         j        j        S rs   )r   rQ  rR  ru   s    ro   r8  zOutputGraph.guardsP  s    #2@@rn   c                 $    | j         j        j        S rs   )r   module_contextr   ru   s    ro   r   zOutputGraph.nn_modulesT  s    #2==rn   Nc                    ||n| j         j        j        }| j        | j        f|d<   t
          j        t          j                    f|d<   t          j	        t
          j
        d          t          j        d          f|d<   t          j	        t
          j
        d          t          j        d          f|d<   t          j	        t
          j        d          t          j        d          f|d<   t          j	        t
          j        d          t          j        d          f|d	<   t
          j        t          j                    f|d
<   dS )zc
        Saves to out if it is provided. Else saves to the tracing context's global_state.
        Nr  grad_enabledcudaautocast_enabledcpuautocast_cpu_enabledautocast_gpu_dtypeautocast_cpu_dtypeautocast_cache_enabled)r   global_contextglobal_stateset_torch_function_stater  r   set_grad_enabledis_grad_enabled	functoolspartialset_autocast_enabledis_autocast_enabledset_autocast_dtypeget_autocast_dtypeset_autocast_cache_enabledis_autocast_cache_enabled)rv   outr  s      ro   r  zOutputGraph.save_global_stateX  s<   
 ?CC(<(K(X 	 )'2
-. ).(>@U@W@W'X^$ e8&AA%f--,
'(
 e8%@@%e,,0
+,
 e6??$V,,.
)*
 e6>>$U++.
)*
 ,+--2
-...rn   c                 :    | j                             |           d S rs   )r  r   )rv   txs     ro   push_txzOutputGraph.push_tx  s    #####rn   c                 4    | j                                         S rs   )r  ry  ru   s    ro   pop_txzOutputGraph.pop_tx  s    ##%%%rn   c                 8    | j         s| j        n| j         d         S r`  )r  r  ru   s    ro   
current_txzOutputGraph.current_tx  s    #'#3Mt||9I"9MMrn   argc                 r      j         rd S |j        J  fdfd |j        |j                   d S )Nc                    t          |           r$t          | j        j        t          j                  sd S | j        j        }|j        v rd S j                            |           t          	                    d| |
                                           j                            t          |          t          j        d|          }t!          |j        |            t#          || dd d          |j        j        d<   d S )Nzbind_symint %s %sT)beforerg   F)pass_arg_as_tensorfake_tensor	is_tensorr1  )r   r$  r4  exprsympySymbolr   r   r   debugr-  r2  r3  r   r   SymIntrO   rR   r5  )sprops0proxyrv   s       ro   bind_symintz4OutputGraph.add_symbol_bindings.<locals>.bind_symint  s    NN z!&+u|'L'L BT'''""2&&&II)1diikk::: $77B	 8  E ej!,,,*2#( + + +EJOJ'''rn   c           	         t          |                                           D ]*\  }} |t          |t          j        |                     +| j        t          j        u rt          |                                           D ]*\  }} |t          |t          j	        |                     + | 
                                t          |t          j                             n| j        t          j        u r= 	|                                 |            	|                                 |           n| j        t          j        t          j        hv r= 	|                                 |            	|                                 |           n[| j        t          j        t          j        hv r< 	|                                 |            	|                                 |           t/          |           rF|                                 \  }}|D ].}t3          | |          } 	|t5          ||                     -d S d S rs   )	enumeratesizer=   r<   SIZElayoutr   stridedstrideSTRIDEstorage_offsetSTORAGE_OFFSET
sparse_coo_indices_values
sparse_csr
sparse_bsrcrow_indicescol_indices
sparse_csc
sparse_bscccol_indicesrow_indicesr   __tensor_flatten__getattrr1   )
tsrcir  attrsctxattrinner_tr  handle_tensors
           ro   r  z6OutputGraph.add_symbol_bindings.<locals>.handle_tensor  sD   !!&&((++ R R1A3C9LaPPQQQQx5=((%ahhjj11 X XDAqK#7^=RTU#V#VWWWW$$&&(n.KLL    U---ajjllC000aiikk3////e.0@AAAann..444ammoos3333e.0@AAAann..444ammoos333,Q// B1133
s! B BD%a..G!M':c4+@+@AAAA	B BB Brn   )r   r  rg   )rv   r  r  r  s   ` @@ro   add_symbol_bindingszOutputGraph.add_symbol_bindings  s     ; 	F***	 	 	 	 	2	B 	B 	B 	B 	B 	B2 	cosz22222rn   c                 *    t          | j                  S rs   )rB   r_   ru   s    ro   rB   zOutputGraph.count_calls  s    4:&&&rn   c                 V    t          t          | j        j                            dk    S r]  )r*  r   r_   nodesru   s    ro   is_empty_graphzOutputGraph.is_empty_graph  s"    4
())**a//rn   c                     |sJ | j         }|                    d          D ]0}t          |t                    r	||         } t	          ||          }1|S N.)r   splitr$  r  r  )rv   keysobjr   s       ro   get_submodulezOutputGraph.get_submodule  s\    BF/C 	& 	&A#t$$ &!fc1oo
rn   tmpc                     t          | j        d                   }	 | dt          | j                   }||vr| j        dxx         |fz  cc<   |S 6)Nco_varnamesT_)r   r   r   r   )rv   r-  existingvars       ro   r6  zOutputGraph.new_var  sp    t(788	66D!34466C(""!-000SF:000
		rn   c                 T    || j         d         vr| j         dxx         |fz  cc<   dS dS )z/Ensure self.code_options.co_names contains nameco_namesN)r   )rv   r-  s     ro   update_co_nameszOutputGraph.update_co_names  sD    t(444j)))dW4))))) 54rn   c                     d                     t          t          |                     }t          j        dd|          }t          j        dd|          }t          j        dd|          }|r|d                                         sd|z   }|S )	Nr  z^[GL]\['?(.*?)'?\]$z\1z	\[(\d+)\]z_\g<1>z[^a-zA-Z0-9]r   sub)joinrI  r   rer  isalpha)namesr-  s     ro   module_key_namezOutputGraph.module_key_name  s     xxC((v,eT::vlIt44vosD11 	 47??,, 	 4<Drn   targetc                     t           j        j                  r t           j        fi           S t                    dv sJ d         t          t                    rJ t          t          j	                  r, j
                                         s j         fd}nt          t          j        j                  r]t          t          j        j                  sJ r3t                              t"          j                             fd}n;fd}n4t          t          j        t          j        f          r fd}n fd} j                                        D ]\  }}|u r ||          c S t/          j        | }t3          j                    D ]} j        vr j        <   t          t          j        j                  rq fd}	t7          d          r%                                D ]\  }
} |	|
           t7          d	          r%                                D ]\  }
} |	|
            |          c S | d
| t=          d          )Nrg   c           	         j         J j         | <   j        j        j        v rj        j        j                 S t	                    dk    r-t                              t          j                             n;t                    s,t                              t          j
                             t          j                            d| di           fdi}j        j        j                            |          }d|j        j        j        vsJ t#                    |j        j        j        d<   |S )Nguardedget_attrrm   rT  tensor_dict)r   r  r  r   rG   r-   r9  r,   ID_MATCHr6   TENSOR_MATCHrU   rm  track_object_existingr  r4  r5  r>   )
module_keyr   optionsrv   rg   r  r|  s     ro   	wrap_namez6OutputGraph.register_attr_or_module.<locals>.wrap_name  sF   0<<<8>)*5 T\0===<.;FCC*622i??!&"3"3L4I"J"JKKKK+F33 P!&"3"3L4M"N"NOOO"L''
JBGG  #) 	  \(5KKFTVWW$BHM,>>>>>4H4P4P"=1	rn   c                 <    t          t                    | fi S rs   )rX   typer  r  r  s    ro   r  z6OutputGraph.register_attr_or_module.<locals>.wrap_name=  s$    +DLL*fXXPWXXXrn   c                 (    t          j        fi S rs   )r   UnspecializedNNModuleVariabler  s    ro   r  z6OutputGraph.register_attr_or_module.<locals>.wrap_nameE  s    $B6UUWUUUrn   c           	      Z    t          j                            d| di           fdiS )Nr  rm   sym_num)rZ   rK  rm  )r  r  rv   r  s    ro   r  z6OutputGraph.register_attr_or_module.<locals>.wrap_nameP  sK    &-%%j*b"EE  # 	  rn   c                     j                             |            j        | <    t          t	          |                               S )N)source_name)r  r  r   rT   r3   )r  rv   r  s    ro   r  z6OutputGraph.register_attr_or_module.<locals>.wrap_name[  sR    ++J77706!*-Tt^
-S-S-STT  rn   c                     j         J t          |           } d|  }|j         |<   t          t                    r6| j        t
                              |                                          <   d S d S r  )r   r9   r$  r8   r  r   r  r-  )	leaf_name
new_sourcenew_namer-  rv   rg   s      ro   register_leaf_namez?OutputGraph.register_attr_or_module.<locals>.register_leaf_nameo  s    #8DDD%6vy%I%I
&*#8#8Y#8#8>H1(;%fk:: * !* !A + ; ;JOO<M<M N N  * *rn   _parameters_buffersr  unreachable)r.   r  r   rT   r  r  r$  r9   r   r   rb  rd  r2  r   r   r-   r9  r,   	NN_MODULEr  SymFloatr   r   r   r  r   r   hasattrnamed_parametersnamed_buffersAssertionError)rv   r  r  r  r  r   r   baser  r	  r  r  r-  rg   r|  s   `` `        @@@ro   register_attr_or_modulez#OutputGraph.register_attr_or_module  s     (;<< 	G ??4?>>g>>vFFFw--7"""""f&788888fel++ W	(F&&(( * )         < 00 )	feho66666 Vf//0FGGHHHY Y Y Y Y Y YV V V V V V V u~ >?? 	             O))++ 	$ 	$DAqF{{ y||###  *E2"" 	! 	!A4?**(.%feho66 :* * * * * * * v}55 :,2,C,C,E,E : :LIq..y9999vz22 :,2,@,@,B,B : :LIq..y9999 y&&&==Q==DD]+++rn   c                    | j                             d          }t          |          }|sg S g }i }g |j        |j                                        | j        j                                        }|r|	                                }t          |t                    r't          |j        t                    sJ ||j        z  }Rt          |t          t          f          rLt          |j        t"                    r2t          |j        j        t&                    r|j        j        j        |v s|j        j        j        }||vrg ||<   ||                             |           |i }	| j        D ]}
t          |
j        t0                    r(t          |
j        t&                    r|
j        j        |v sF|
j        j        }|| j        d         v sJ ||         D ]}|j        j        }||	vrp|                     | d          }||	|<   |                    t;          d|          t;          d|          t;          d          t;          d|          g           t'          |	|                   |_        
|S )	Nrv   r  _ref	LOAD_FASTargval
LOAD_CONSTBINARY_SUBSCR
STORE_FAST)r   rP  rF   stacksymbolic_localsvaluesr   store_attr_mutationsr  ry  r$  rV   r   r   rP   r/   rg   r4   r  r8   
local_namer   	graphargs_exampler   r   indexr6  rN  r!   )rv   r  maybe_gmstolen_list_namesalias_instsneeds_aliasqueuexstolen_namevisitedr  	list_namelist_idx
alias_names                 ro   handle_aliases_for_stolen_listsz+OutputGraph.handle_aliases_for_stolen_lists  s   #''///99  	I  	
X
&&((
 388::
  	/		A!-.. !!'400000  10IJKKqx77 qx}k:: HM,0AAA(-2K+--+-K($++A...%  	/( > 	: 	:C3<..sz;77 J)[88 
-I 1- @@@@@ + : :8>7**!%$***" "J )3GH%&&.{9MMM.|HMMM.??.|JOOO	   'wx'899':* rn   Fr   c           
          |J ddl m} | _        | _        d _        t
                              d|           t          d j        D                       st          d           g t          j        dk    rj        D ]}|j        d	k    r*                    t          d	|j        
                     7|j        dk    r=                    t          dt#          j        d                                                            t'          j        |                      j        r j        r
J d                                 j                                                                                  fd}t1          j                  D ]}|                                                                                                  t9          j                  }|D ]}	|	                                 d  j                                         D             }
tC          |
          }g }i }|rg ||d         <   j"                                         D ]\  }}tG          |j$        tJ                    r|j$        j&        |k    r0t          j        dk    r"tN          (                    tR          |          ran"tN          (                    tR          |          rJ ||vrg ||<   ||                             |           |*                                D ]I}|                    ||                    |                    |gt#          ||                   z             Jt#           j+                  dk    r |             g } ,                    d           _-         |t]           j+                            } /                    d|          }ta          |          }|                    |1                    |d                     |                    te          dd                     |                    |3                    j4        j-                              5                    |           |r7t          d |D                       rt          d |D                       rt#          tm          |                    t#          |          k    r։ j7        8                                rt#          j9                  dk    s j:        s |              5                     ;                    t9          t1          |                    |          t          dt#          |                    gz               5                    fdt1          |          D                        dS  ,                    d          }ta          ||          } <                    ||           ta          ||d |j=                                         D                       } <                    ||           d}g }t}           j?                  dk    st#          |j@                  dk    r|                     ;                    |A                                |                     t#          |j@                  dk    r+|                    |3                    |                     d}n8|                    t          d                     n B                                |              5                    ||C                                z               5                    fdt1          |          D                        |r8 5                    ta                    D                    |          g           dS dS ) zw
        Generate a subgraph to continue execution on user code.
        Automatically restore live variables.
        Nr   disableTzCOMPILING GRAPH due to %sc              3   >   K   | ]}|                                 V  d S rs   )can_restore)r   blocks     ro   	<genexpr>z/OutputGraph.compile_subgraph.<locals>.<genexpr>  s.      CC55$$&&CCCCCCrn   z&compile_subgraph with block_depth != 0      	MAKE_CELLr  COPY_FREE_VARSco_freevars)r  z)export does not support pregraph_bytecodec                  Z                                                                       d S rs   )add_output_instructionsr   )prefix_instsrv   s   ro   append_prefix_instsz9OutputGraph.compile_subgraph.<locals>.append_prefix_insts  s/    ((666     rn   c                 4    i | ]\  }}|t          |          S rm   )rM   )r   r-  mods      ro   
<dictcomp>z0OutputGraph.compile_subgraph.<locals>.<dictcomp>  s3     
 
 
+44D/#&&
 
 
rn   ra  )r9     r   random_values__gen_rand_valuesFc              3      K   | ]X}t          |t          t          t          f           o0t          |t                    o|                                t          u  V  Yd S rs   )r$  r\   rY   r]   rZ   python_typefloat)r   r   s     ro   r7  z/OutputGraph.compile_subgraph.<locals>.<genexpr>H  s          3,4   V $A77TAMMOOu<TU     rn   c              3   @   K   | ]}t          |t                    V  d S rs   )r$  r[   r   r*  s     ro   r7  z/OutputGraph.compile_subgraph.<locals>.<genexpr>T  s,      HHaJq.11HHHHHHrn   UNPACK_SEQUENCEc                 T    g | ]$}t                                        |          %S rm   r%   create_storer   r  r  s     ro   r   z0OutputGraph.compile_subgraph.<locals>.<listcomp>b  -    SSSS2++C00SSSrn   	graph_outc                 &    i | ]\  }}|d k    |dS )r   Nrm   )r   valr   s      ro   rD  z0OutputGraph.compile_subgraph.<locals>.<dictcomp>n  s$    VVV
UETUII#tIIIrn   )tempvarsPOP_TOPc                 T    g | ]$}t                                        |          %S rm   rO  rQ  s     ro   r   z0OutputGraph.compile_subgraph.<locals>.<listcomp>  rR  rn   )E
decoratorsr3  partial_convertcompile_subgraph_reasonr	  r   r  allblock_stackr*   sysversion_infor@  opnamer   r!   r  r*  r   r   r  r   rN  r0  rZ  exitcleanup_graphprune_dead_localsr   r  realizer   r   r   r  r$  rg   r8   r!  r  __instancecheck__rW   r  r   r6  r  r   r&  r%   load_function_namer    rP  r  r?  r   r   is_emptydebug_localsr  compile_and_call_fx_graphcodegen_suffixusesrB   r_   graph_outputsgraph_output_varsrun_compiler_collectiverO  create_delete)rv   r  rZ  r   r3  instrA  r6  stack_valuesr{   nn_modules_proxiesrootrestore_varsval_to_namesr   r   random_calls_instructionsrand_fnrand_fn_namecodegengraph_output_varpass1pass2stored_graph_output_varr  r@  s   ``                       @ro   compile_subgraphzOutputGraph.compile_subgraph  s    !!!''''''.'-$		-v666CCBNCCCCC 	DBCCC*,w&& 9 9;+-- ''*;t{KKK    [$444 ''*,#bom6T2U2U      !''	$8888"	7'+{	7 	76	7 	7 	7 	D2333D@@DDEEE	! 	! 	! 	! 	! 	! bn-- 	 	EJJrNNNN
BH~~ " 	 	EMMOOOO
 
8<8M8M8O8O
 
 
 0119; 	0-/Lb)* &,,.. 	& 	&DAq !(K00 QX5HA5M5M7**)),::   11,BBBBB$$"$QO""1%%%%""$$ 	< 	<AQ000c,q/&:&: :;;;; t !!A%%!!!(*%%)\\/%B%BD"g5d6GHHIIG../BGLLLD))G%,,**<>>   &,,-A!U-K-KLLL%,,$$RY%@AA   (()BCCC G	   &    G	 HH<HHHHHG	 C%%&&#l*;*;;;!**,, <((A--' .  !!!((..r48N8N3O3OQUVV%&7S=N=NOOOPQ  
 ((SSSSH\<R<RSSS      $||K88b$(899EL%888  VVej6F6F6H6HVVV	  E L%888&+#F4:&&!++s53F/G/G1/L/L222u7N7N7P7PRVWW   u*++q00MM%"4"45E"F"FGGG.2++MM"4Y"?"?@@@@ ,,R000!!!((%2H2H2J2J)JKKK ((SSSSH\<R<RSSS   ' ,,r]]001ABBC     rn   c                    | j         ru| j        rJ | j                                         D ]R\  }} |                                                   | j                                                 |           S| j                                       | j        	                               |j
        D ]\  }                    fd           |D ]} |                               t          t          |          d                                         t          d          g                               ||j                    | j                                       d S )Nc                                  S rs   rm   )rE  	debug_vars   ro   rF  z,OutputGraph.codegen_suffix.<locals>.<lambda>  s    RR	]] rn   FrW  )value_from_source)r  r   r   append_outputcreate_loadr  
store_attrr   codegen_hookscodegen_save_tempvarsrh  rG  extend_outputr    r*  r!   restore_stackcodegen_update_mutated)	rv   r  rq  rE  r-  rU  r   r  r  s	      `    @ro   rj  zOutputGraph.codegen_suffix  s    	${"""!06688 $ $	c3  0G!H!HIIId####''+++//333  " 	> 	>OIt22222333  31#d))UCCDDD0;;<====
RYGGG0044444rn   c                 l   | j         sJ t          | j        j                  }|D ]}|j                            dd           t          j                    }t          ||dd                   D ]\  }}|j	        t          j
        j        u rt          |j                  | fk    r|j        s|j        d         }|j	        t          j
        j        u rbt          |j                  | fk    rH|j        sA|j        d         }| j                            |           | j                            |           dS )z
        Remove "creation_timestamp" from node meta

        Remove this pattern from the graph:
            torch._C._set_grad_enabled(False)
            torch._C._set_grad_enabled(True)
        creation_timestampNr   r   )r	  r   r_   r  r5  ry  r   r  zipr  r  _set_grad_enabledtupler   _erased
erase_node)rv   r  r4  r  node1node2s         ro   rb  zOutputGraph.cleanup_graph  sA    TZ%&& 	6 	6DIMM.5555,..uQRRy11 	1 	1LE5 :::%*%%l*:)<<< =  %z!}LEH$>>>ej)),.>-@@@!M A $):a=LJ))%000J))%000	1 	1rn   c                     i }| j         j        D ]d}|j                            dd           }t	          |t
          j        j                  r(|                                }d |D             ||j	        <   e|S )NrT  c                 Z    g | ](}t          |t                    r|nt          |          )S rm   )r$  rk   repr)r   r  s     ro   r   z:OutputGraph.get_graph_sizes_structured.<locals>.<listcomp>  s1    !U!U!U1z!S'9'9"F!!tAww!U!U!Urn   )
r_   r  r5  rP  r$  r   r   
FakeTensorr  r-  )rv   retr4  rT  r  s        ro   get_graph_sizes_structuredz&OutputGraph.get_graph_sizes_structured  s|    J$ 	V 	VD IMM/4@@M-):)EFF V$))++!U!UPT!U!U!UDI
rn   r-  c                 0   d}|d| dz  }| j         j        D ]}|j                            dd           }t	          |t
          j        j                  r|                                }||j	         dt          |           dz  }g }d}|D ]j}t	          |t                    r|                    |           -t	          |t
          j                  r"d}|                    |j        j                   i n|r||j	         d	t          |           dz  }|S )
NzTRACED GRAPH TENSOR SIZES
z===== z =====
rT  z: 
FTz (concrete): )r_   r  r5  rP  r$  r   r   r  r  r-  r  rk   r   r  r4  hint)	rv   r-  graph_sizes_strr4  rT  r  concrete_size
has_symintszs	            ro   get_graph_sizeszOutputGraph.get_graph_sizes  sF   72D2222J$ 	 	D IMM/4@@M-):)EFF $))++di#B#B5;;#B#B#BB ""
  B!"c** %,,R0000#B55 %)
%,,RW\::::! '#yOOu]7K7KOOO rn   c              #   x  K   | j         j                                        }i }|                     |           	 | j         j                            |           dV  | j         j                            t          |                     dS # | j         j                            t          |                     w xY w)zj
        Momentarily restores the global state to what it was prior to tracing the current output
        )r  N)r   r  copy_graphstater  restore_graphstater   )rv   prior_global_statecurrent_global_states      ro   restore_global_statez OutputGraph.restore_global_state  s      
 "1@PPRR<>#7888	 /BBCUVVVEEE  /BB,-ABB    D /BB,-ABB   s   #B .B9c                 Z   |j         xj        j        }t                              dj                   t          j                            dd fd           t          j	        
                    |                                t          j	                                        z            5  d g|                                z  }t          j        |j        |           |_        d d d            n# 1 swxY w Y   |j                                         t$          j        d S d S )Nzcompiler_collective %sartifactc                      dddS )Ncompiler_collectivejson)r-  encodingrm   rm   rn   ro   rF  z5OutputGraph.run_compiler_collective.<locals>.<lambda>  s    1 &% % rn   c                  Z    t          j        t          j         j                            S rs   )r  dumpsdataclassesasdictlocal_state)dss   ro   rF  z5OutputGraph.run_compiler_collective.<locals>.<lambda>   s#    4:&r~66$ $ rn   )metadata_fn
payload_fn)group)distributed_state
all_states
compile_pgr   infor  r   _loggingtrace_structuredr  devicerankdevice_countr  distall_gather_objectspeculation_logr   r    CompileCollectiveRestartAnalysis)rv   r  r  r  r  s       @ro   rn  z#OutputGraph.run_compiler_collective  se   &&B38MJHH-r~>>>N++     , 	 	 	 "":??#4#4uz7N7N7P7P#PQQ + +"Vjoo&7&77
&z2>TTTT *+ + + + + + + + + + + + + + + $$&&&66) 438M8Ms   1<C99C= C=c                 (    t           j        j                                        5  ddlm}  j        sJ                      |           t          d          }t          |t                    sJ t          |t                    sJ                      dd j                            t          d |D                                 fi           }|j        j                            ||           t$          j        s.t)          t+          j        | j                   j        |                                             t5           j                  }t6          d         dxx         |z  cc<    j                                         t=          | j                   j        D ]} |            j         _          j!        "                                j#        d<   tH          %                    d	tM          |d
d
d
                     t           j'        (                    d fdfd            )                                  j*        j+        }	 j,        shddl-m.c m}
 |
/                    d          5  t           j0        1                    |	j                  }ddd           n# 1 swxY w Y   | j*        _+         2                                5   3                              }ddd           n# 1 swxY w Y   ddl4m5} t          ||          s*t          tm          |dd          |          rP|j7        dk    rEt          ||          r|n|j8        }|9                    |           t          ||          s|j:        } ||          }t6          d         dxx         dz  cc<    ;                    ||           ty          |          }|=                    |           |>                                cddd           S # 1 swxY w Y   dS )zr
        Generate code from self.graph and return the Instruction()s to
        call that generated code.
        r   r2  __compiled_fnr  c              3   >   K   | ]}|                                 V  d S rs   )as_proxyrL  s     ro   r7  z8OutputGraph.compile_and_call_fx_graph.<locals>.<genexpr>   s*      5O5Oqajjll5O5O5O5O5O5Orn   statscalls_capturedr  %sT)include_strideinclude_devicecoloreddynamo_output_graphc                  0    d                                  iS )Nsizes)r  ru   s   ro   rF  z7OutputGraph.compile_and_call_fx_graph.<locals>.<lambda>D  s    $"A"A"C"CD rn   c                  4                          ddd          S )NFT)print_outputr  r  )print_readable)r   s   ro   rF  z7OutputGraph.compile_and_call_fx_graph.<locals>.<lambda>E  s#    2#4#4!&tD $5 $ $ rn   )r  r   NFr   )r   )_LazyGraphModule__self___lazy_forwardunique_graphs)?r   _guardsr   clear_framerY  r3  r	  rn  r#   r$  r   r   rp  rb  
create_argr  r  _maybe_preserve_original_metar   do_not_emit_runtime_assertsr   r   r   r_   r   remove_unused_graphargsrB   rC   rk  r   r   r  r[  r  r   r5  graph_code_logr  rK   r  r  r[  r   r   r   r   r   r   r   r   r  call_user_compilertorch.fx._lazy_graph_moduler  r  rh   r  force_recompiler   install_global_unsafer%   make_call_generated_coderO  )rv   r  rvrs  r3  r-  output_nodencallsregister_finalizerold_fake_moder  backend_fake_modecompiled_fnr  lazy_gmrE  r   s   `               @ro   ri  z%OutputGraph.compile_and_call_fx_graph  s\   
 ])5577 h	) h	)++++++####((,,,_--Db$'''''dN33333**$//5O5OB5O5O5O0O0OPPR	 K I$BB2{SSS5 /N444N   ((*** ,,FW.///69/// !'')))#D$*55B&*&A ' '"""2&&&&)-)EB& 5::<< G2   &"T$PT     N++%DDDD    ,    ##%%% 0:M; C999999999]]E]RR  (-(9(H(H"/"9 )I ) )%               2C$.**,, : :"55b99: : : : : : : : : : : : : : : EDDDDD+'788 27;
DAACSTT2(O;; "+/?@@.KK$-  !00999!+/?@@ 2")/K!'+..KWo...!3...&&t[9992B''---&&((Qh	) h	) h	) h	) h	) h	) h	) h	) h	) h	) h	) h	) h	) h	) h	) h	) h	) h	)s[   IP8&J*P*J.	.P1J.	2#PK7+P7K;	;P>K;	?C;PPPc                 8    | j                             d          S )Nplaceholder)op)r_   
find_nodesru   s    ro   placeholderszOutputGraph.placeholders|  s    z$$$666rn   c                 $    d | j         D             S )Nc                 (    g | ]}|j         d          S )r1  r5  )r   r4  s     ro   r   z)OutputGraph.graphargs.<locals>.<listcomp>  s    DDD$	*%DDDrn   )r  ru   s    ro   r"  zOutputGraph.graphargs  s    DD$2CDDDDrn   r   c                     t          dd          5  |                     |          cd d d            S # 1 swxY w Y   d S )NOutputGraph.call_user_compilerbackend_compile)
phase_name)rD   _call_user_compiler)rv   r   s     ro   r  zOutputGraph.call_user_compiler  s    ,9J
 
 
 	0 	0 ++B//	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0s   488c           
         | j         J d}g }|j        j        D ]0}|j        dv r|dz  }|j        dk    r|                    |           1t          |           |D ]}|j        d         }|j        |_        | j	        |_
        | j        |_        	 t          | j         d          r| j         j        nd} t                      t           j        d|            | j         }t$          j        rt)          |          } |||                                           }	 t                      t           j        d	|            t-          |	          s
J d
            n# t.          $ rw}
| j        r.t3          | j         |
                              |
j                  d d| j                                         d}t=          |
| j        j        |           Y d }
~
n:d }
~
wt@          $ r}
|
d }
~
wtB          $ r}
t3          | j         |
          |
d }
~
ww xY wtE          ddi | j#        |tI          |j        j                  tI          |          d           |	S )Nr   rL  call_methodcall_moduler   r  r1  rh    zcalling compiler function zdone compiler function z#compiler_fn did not return callablez9Backend compiler failed with a fake tensor exception at 
zAdding a graph break.dynamor  )op_count
node_countinput_count)%r   r_   r  r  r   rJ   r5  rg   _dynamo_sourcer   _param_name_to_sourcer  _source_to_user_stacksr  rh   r   r   INFOr   r   r   r   callabler(   r  r'   with_traceback__traceback__r  format_frame_summaryr+   r  r)   r   r   r   r*  )rv   r   totr  r4  plr  r-  r   r  emsgs               ro   r  zOutputGraph._call_user_compiler  s   +++HN 	* 	*DwIIIqw-''##D)))3 	+ 	+B'*%C #
B#'#< $($>!	D 4+Z88 )) 
 LNN7<)Ld)L)LMMM*K( :,[99%+b$*=*=*?*?@@KLNN7<)I4)I)IJJJK((OO*OOOOO0 
	D 
	D 
	D5 +D,<a@@OOO (<4466( ( ( 
 'q$,*=sCCCCCCCC 	 	 	 G 	D 	D 	D'(8!<<!C	D 	,.!"(.11"<00	  		
 		
 		
 s2   CE 
H"A-GH!G##H0HHc                 R    g }| j         D ]}|                    |j                   |S rs   )r"  r   example)rv   r   r  s      ro   r   zOutputGraph.example_inputs  s4    > 	' 	'CMM#+&&&&rn   c                 b     j         sJ dt          j        j        fd}dt          j        j        fdfd}ddlm} t          t           j        j	                            D ]}t          t          |j                            dk    r|j        dk    sh|j        d	k    r|j        t          j        u sJ|j        d	k    r)|j        t           j        u r ||j        d                   s ||          s ||          r                     |           d
 } fd}t)                      dt*          t           j        t           j        f         fdg } j        D ]} ||          d u}|r|j        s|                    |           /|j        s,t5          |j        d         t8                    s ||           b|j        d         }	t5          |	t8                    rt5          |j        d         j        t           j                  r|j        d         j        }
|j        d         j        }t           j         j!        "                    |
          s}tG          |
$                                          }|%                                D ]G}tM          |j'        |          }tQ          j)        t           j        t           j        ffd|           Hv|	j*        |	j*        n|	j        } |           |D ]4} ||          }|%|vr ||           +                    |           5d S )Nb_nodec                     | du rdS t          | t          j                  sdS | j                            d          }|dS |du rdS t          |t
          j                  r|j                                        x}|S dS )NTFrT  )	r$  r   Noder5  rP  r   SymBoolr4  maybe_as_bool)r  brs      ro   is_static_truez;OutputGraph.remove_unused_graphargs.<locals>.is_static_true  s    ~~tfbg.. u00AyuDyyt1em,,&..000Q= 5rn   ac                     ddl m} t          | t          t          t
          f          rdS t          | t          j                  r(t          | j        	                    d          |          S dS )Nr   SymTypesTrT  F)
torch.fx.experimental.sym_noder  r$  rk   rJ  r   r   r  r5  rP  )r  r  s     ro   is_symnode_argz;OutputGraph.remove_unused_graphargs.<locals>.is_symnode_arg  sj    ??????!c5$/00 t!RW%% I!!&**_"="=xHHH5rn   c                 .   ddl m} | j        dk    rdS t          | j                            d          |          sdS t          fd| j        D                       sdS t          fd| j        	                                D                       sdS dS )	Nr   r  rL  FrT  c              3   .   K   | ]} |          V  d S rs   rm   r   r  r  s     ro   r7  zWOutputGraph.remove_unused_graphargs.<locals>.is_symnode_compute_node.<locals>.<genexpr>  s-      <<Q~~a((<<<<<<rn   c              3   .   K   | ]} |          V  d S rs   rm   r!  s     ro   r7  zWOutputGraph.remove_unused_graphargs.<locals>.is_symnode_compute_node.<locals>.<genexpr>  s-      GGQ~~a((GGGGGGrn   T)
r  r  r  r$  r5  rP  r\  r   r   r  )r4  r  r  s     ro   is_symnode_compute_nodezDOutputGraph.remove_unused_graphargs.<locals>.is_symnode_compute_node  s    ??????w/))udimmO<<hGG u <<<<$)<<<<< uGGGG$+2D2D2F2FGGGGG u4rn   r   )is_accessor_noder  rL  c                     | j         d         }|j        }t          |t          j                  r0t          |j        j        t          j                  r|j        j        S d S Nr1  )	r5  r  r$  r   r  r4  r  r  r  )r4  r  r  s      ro   placeholder_binds_symbolzEOutputGraph.remove_unused_graphargs.<locals>.placeholder_binds_symbol  sX    )J'CkG'5<00 )Z!5<6 6 ) |((4rn   c                     t                               d| j        d         j                                                   | j        d=                     |            j                            | d            d S )NzREMOVE UNUSED GRAPHARG %sr1  )r   r  r5  rg   r-  rs  rk  ry  )r4  rv   s    ro   remove_unusedz:OutputGraph.remove_unused_graphargs.<locals>.remove_unused%  sm    II149Z3H3O3T3T3V3VWWW 	*%T"""!%%dD11111rn   fakec                 *    | t          |          z  } d S rs   )r   )used_symbolsr*  s     ro   update_used_symbolsz@OutputGraph.remove_unused_graphargs.<locals>.update_used_symbols/  s    L...LLLrn   r1  c                      |           S rs   rm   )r  r-  r,  s    ro   rF  z5OutputGraph.remove_unused_graphargs.<locals>.<lambda>P  s    .A.A,PQ.R.R rn   ),r	  r   r4  Argument%torch.fx.experimental.symbolic_shapesr$  rZ  r   r_   r  r*  usersr  r  operatorgetitemr   _checkr   rs  r   r   r  r   r  r   r$  r5  rQ   r  ScriptObjectexample_strong_ref_libraryfake_class_registrytracing_with_realr  __obj_flatten__r  r  wrapped_objpytreetree_map_onlyr  remove)rv   r  r#  r$  r4  r'  r)  recheck_placeholdersbinds_symbolr  real_script_objfake_script_obj	flat_dictr  fake_attr_valr*  symbolr  r-  r,  s   `                @@@ro   r  z#OutputGraph.remove_unused_graphargs  s    	27#3 	 	 	 	&	bg. 	 	 	 		 	 	 	 	" 	KJJJJJT$*"23344 	+ 	+D4
##$$))Gz))?22t{hFV7V7V?22 K5<77*N49Q<88 8 /.t44 8 ('--	 8 $$T***	 	 		2 	2 	2 	2 	2 +.%%	/E%,:T4U 	/ 	/ 	/ 	/  "% $	< $	<D33D99EL !<z 6(//555z <*Ij)+@+ + < "M$'''' )J/C!#'<== ! !$)J"7"?ASTT !*.)J*?*G*.)J*?*R$~ASS+    " )-_-L-L-N-N(O(OI(1(8(8 " "07$3$?1" 1" !' 4%*\5<$@$R$R$R$R$R$1!" !" !" !"
 !+.?+FCK  ('d;;;; ) 	0 	0D--d33F!--!M$'''' !''///	0 	0rn   r,  c                 H    | j                             |           d| _        dS )zt
        We call this on the creation of a new compiled subgraph that is inserted
        before user code.
        TN)r  rN  r	  )rv   r,  s     ro   r?  z#OutputGraph.add_output_instructionsc  s)    
 	 ''///rn   c                     || j         vsJ | j                             |           | j                            t	          j        | j        ||                     dS )a`  
        WARNING: prefer the safer `install_global_by_id/install_global`.
        torch.compile instances should be independent of each other;
        one footgun is to have one instance depend on the existence of
        a global installed by another instance. This can happen if we mangle
        a global the same way across both instances.
        N)r   r   r  r   r@   rK  r   )rv   r-  r{   s      ro   r  z!OutputGraph.install_global_unsafek  s]     411111""4((([/0A4OOPPPPPrn   c                     | dt          |           d| j         }|| j        v r|S |                     ||           |S )z
        Installs a global if it hasn't been installed already.
        This is determined by (prefix, id(value)) pair.

        Returns the name of the newly installed global.
        r  _c)rz   r   r   r  rv   r,  r{   r-  s       ro   install_global_by_idz OutputGraph.install_global_by_idw  sU     992e9999994)))K""4///rn   c                 P    t          |          }|                     ||           |S )z~
        Installs a global, generating a unique name for it.

        Returns the name of the newly installed global.
        )r#   r  rJ  s       ro   r&  zOutputGraph.install_global  s,       ""4///rn   c                    d | _         | j                                         d | _        | j        j        D ]}d|j        v r|j        d= | j                                         | j                                         | j	                                         | j
                                         | j                                         | j                                         | j                                         d S r&  )r  r   r   r   r_   r  r5  rk  ri  r   r   r  r  r   rv   r4  s     ro   cleanupzOutputGraph.cleanup  s     $(!J$ 	* 	*DTY&&Ij)##%%% &&(((!!!#))+++#))+++-33555""$$$$$rn   enabledc                     || _         d S rs   )r  )rv   rP  s     ro   r  z$OutputGraph.set_torch_function_state  s    &-###rn   r  c                 :    | j                             |           d S rs   )r  r   )rv   r  s     ro   add_graph_finalizerzOutputGraph.add_graph_finalizer  s"     	#**+=>>>>>rn   r4  c                 z    |j         dk    r|j        d         j        S |j         dk    sJ | j        |j                 S )z#Extract the non-fake example tensorr  r1  r  )r  r5  r  r   r  rN  s     ro   example_value_from_input_nodez)OutputGraph.example_value_from_input_node  sA    7m##9Z(00w*$$$$t{++rn   )r(  rs   )r  )FN)r  N)\rh   ri   rj   r   r   r   r   r   r   r   Scoperw   r  rP   r.  r+  r   rV  r   rX  r[  propertyr2  rb  rd  r_   setterri  rk  rm  rp  rs  
contextlibcontextmanagerr}  r  r   r   r   r  	GuardsSetr8  r   r  r  r  r  rR   r  rB   r  r  r6  r  staticmethodr  r   r   r   r   r  r0  r   r~  rj  rb  r  r  r  rn  ri  r   r   r  r  r"  r   r   r  r  r   r  r"   r?  r  rK  r&  rO  r  rS  rU  r   r   s   @ro   r   r      s        r238nr2 j)r2
 r2 r2 r2 r2 r2 r2 r2 r2hD D D&5 5O 5 5 5 5
) 
) 
)  .  0&8BG#4 & & & &# # #
   X     X & & & ) ) X) \* * \* 7 7 X7 4 4 X4A A A@ @ @@ @ @   (   X . . X. 8 8 X8 A/ A A A XA >DcN > > > X>'
 '
 '
 '
R$ $ $& & & N N XN>3x >3 >3 >3 >3@' ' '0 0 0     5 5 5
   \L,ehou|S89L, L, L, L,\G G GT QU} }19:L1M} } } }~5 5 5*1 1 1<  C    2   "7 7 7.m) m) m)^ 7d27m 7 7 7 X7 E4> E E E XE0R^ 0
 0 0 0 0<bn < < < < <|U\ 2    S0 S0 S0 S0j d;.?  D        
Q 
Q 
Q 
QS    	s 	 	 	 	% % % %$. . . . . .?"*BN+;T+A"B?	? ? ? ?
,%(- , , , , , , , ,rn   r   a  With the current config, we will graph break (and fall back to eager-mode PyTorch) on all ops that have do not have the 'pt2_compliant_tag'. Please see the following doc for how to mark this op as PT2 compliant https://pytorch.org/tutorials/advanced/custom_ops_landing_page.htmlc                     |dk    rd S  fd} fd}t          |t          j        j                  r7t          j        j        |j        v r ||           d S  ||d| d           d S t          |t          j        j                  rOt          |	                                          }t          |          dk    rMt          ||d                   }t          j        j        |j        v r ||           d S  ||d| d	           d S t          j        j                             j        ||fd
          \  }}	 t          j        j        |j        g|R i |}	n3# t&          $ r&}
t)          t+          |
                     Y d }
~
nd }
~
ww xY wt          ||	          }t          j        j        |j        v r ||           d S  ||d| d|	 d           d S d S )NrL  c                 R    | j         dv rd S j                            |            d S )N>   atenprimprims)	namespacer  r   )r  output_graphs    ro   encountered_compliant_opz8check_pt2_compliant_op.<locals>.encountered_compliant_op  s4    888F)--f55555rn   c                     j                             |            t          j        rt	          |dz   t
          z              d S d S )N )r  r   r   only_allow_pt2_compliant_opsr*   err_epilogue)r  r  rc  s     ro   encountered_non_compliant_opz<check_pt2_compliant_op.<locals>.encountered_non_compliant_op  sL    &**6222. 	4#)l233333	4 	4rn   z%Encountered the torch.ops.OpOverload z that is not PT2 compliant.r   r   z:Encountered the non-overloaded torch.ops.OpOverloadPacket z that is not PT2 compliant. Fz+Encountered the torch.ops.OpOverloadPacket z! which resolves to the overload (z) that is not PT2 compliant.)r$  r   _ops
OpOverloadTagpt2_compliant_tagtagsOpOverloadPacketr  	overloadsr*  r  _dynamoutilsget_fake_values_from_nodesr  r  _jit_resolve_packet_qualified_op_namer   r*   r   )rc  kindr  r   r   rd  ri  rp  r  overloadr  s   `          ro   check_pt2_compliant_oprx    s   6 6 6 6 6
4 4 4 4 4
 &%*/00 	9&&+55$$V,,,F$$*F * * *	
 	
 	

 	&%*566 $&**,,--	 y>>Q1..By*bg55((,,,((/.4/ / /   F}*EE#dF^U
 
f	"x3),0  4: HH  	" 	" 	"#a&&!!!!!!!!	" VX&&9&"'11$$R(((((((&f & &3;& & &    ?$ $s   <E 
F&FFc                   f     e Zd ZdZ	 d fd	Zd Z	 	 	 d fd	Z	 d fd	Zd Zdd	Z	d
 Z
d Z xZS )r   a  
    Holds an FX graph that is being traced. OutputGraph owns a SubgraphTracer
    and the separation of responsibilities is that SubgraphTracer is
    responsible for building the graph while OutputGraph is responsible for
    compiling and executing the graph.
    NFc                    t                                                       t          j        |          | _        t
          j                                        | _        |r|J || _	        i | _
        i | _        || _        i | _        d | _        d | _        d | _        d | _        d | _        | j        	g | _        d S | j        j        | j                            |          |fgz   | _        d S rs   )r   rw   weakrefr  rc  r   r   Graphr_   r   ri  rk  ru  lifted_freevars	prev_inst	_cur_code_orig_gm_meta_orig_gm_lineno_map_orig_gm_firstlinenosource_fn_stack_target_to_str)rv   rc  ru  r   rv  r   s        ro   rw   zSubgraphTracer.__init__  s     	#M,77X^^%%

  	">>>& 9; =?   "!#' $(!
 ;#%D   #';#>**=99=IB $D   rn   c                 @   | j         r| j        r| j        r|j        j        }d }|#| j                            || j        z
  d           }|Q| j         |         }t          j        j        D ]}||v r||         |j	        |<   d|v r|d         |j	        d<   d S d S d S d S d S d S )Nstack_trace)
r  r  r  current_instructionstarts_linerP  r   r  _COPY_META_FIELDSr5  )rv   r  r4  linenonode_idxr5  fields          ro   r  z,SubgraphTracer._maybe_preserve_original_meta<  s    	C(	C )	C
 +7FH!377T66  #)(3X7 7 7E}}+/;	%( D((/3M/BDIm,,,#	C 	C 	C 	C 	C 	C $#
 )(rn   c           	         | j         bt          j        ||f          \  }}	g }
|D ],}|                     |          }|
                    |           -t          j        |
|	          \  }}t                                          |||||||          | j        j	        }t          j        dk    r|dv r|j        | j        urqj        jj        j        ^|j        |                    j        j                  fd}t$                              dt)          |                     | _        d}|j        | j        ur t-          j        |j                                      dd                       }t3          |t4          j        j                  r@d	}d
 |j        j        D             | _        |j         | _!        |j"        j#        j$        | _%        nd | _        d | _!        d | _%        |j&        }|r!|'                                j(        j)        d<   |dv r%| j*        j(        j+        |fgz   j(        j)        d<   n\|dk    rV| j         tY          d           | j*        j(        j+        j(        j)        d         |         d         fgz   j(        j)        d<   | -                    |j(                   |sdj(        j)        vr*|j&        }|r!|'                                j(        j)        d<   dj(        j)        vr|dv r%| j*        j(        j+        |fgz   j(        j)        d<   n\|dk    rV| j         tY          d           | j*        j(        j+        j(        j)        d         |         d         fgz   j(        j)        d<   dj(        j)        vrg }|rN|.                                s'|                    |/                                           ta          |dd           }|N|1                                 td          j3        4                    |          5                                }d6                    |          j(        _7        S )Nr8  r  )r  c                  r    t                                                    } dj        j         d d|  S )NzTRACE FX call z from r  )rE   rstripr4  r-  )linecur_instheaderr  tx_codes    ro   get_trace_call_log_strz;SubgraphTracer.create_proxy.<locals>.get_trace_call_log_str  s@    5gxHHOOQQDPBGLPPPP$PPPrn   r  Forig_graphmodulec                      d S rs   rm   rm   rn   ro   rF  z-SubgraphTracer.create_proxy.<locals>.<lambda>  s    D rn   Tc                     g | ]	}|j         
S rm   r  )r   nds     ro   r   z/SubgraphTracer.create_proxy.<locals>.<listcomp>  s'     & & & "BG& & &rn   nn_module_stack>   r  rL  r  r  z0Invoking an nn.Module inside HigherOrderOperatorr   r  ru  r  )8ru  r<  tree_flatten#maybe_lift_tracked_freevar_to_inputr   tree_unflattenr   rm  rc  r  r^  r_  r  r~  	positionsr  r  get_line_of_code_headertrace_call_logr  rL   r  r$   get_contextrP  r$  r   r   r   r_   r  r  _lineno_mapr  r   __code__r   r  r  r   r4  r5  r  r-  r*   r  is_co_filename_from_nn_modulesframe_summaryr  reverser   StackSummary	from_listformatr  r  )rv   rv  r  r   r   r-  	type_exprproxy_factory_fn	flat_args	tree_specnew_flat_argsr  maybe_new_argr  r  is_retracingorig_graphmodule_mayber  frame_summariesmsgsr  r  r  r  r   s                       @@@@ro   rm  zSubgraphTracer.create_proxyP  s   V ;"#)#6f~#F#F IyM  4 4 $ H H M M$$]3333!0	JJLD&WW!!&$i9I
 

 ) w&&4 4
 ,
 ,

 -H..&2&-9)338;M;T3UUQ Q Q Q Q Q Q Q $$T:6L+M+MNNN!) 9DN**&\%=bi%H%H%L%L"LL& & & &" 0%(2FGG 1#& &&<&B&H& & &" ,B+M(*2;J )) &*"+/(,0), 	E.=.B.B.D.DBGL*+333.2.Bv&F /BGL*++ ]""{&PQQQ.2.BGLGL!23F;A>F /BGL*+ 	**2rw777 	 44"$"4" M6E6J6J6L6LBGL!23 44;;;6:6Jv.N 7BGL!233 ]**{.%N   7;6JGLGL):;FCAFN 7BGL!23 ,,<>O 1 88:: ?#**2+;+;+=+=>>>R400  1 ##%%% )33ODDKKMMD"$''$--BG	rn   c                 Z   t          | j        ||||           | j        Pt          j        |i |}|D ]<}t          |t          j        j                  s"|j	        | j	        k    s
J d            =t                                          ||||||          }	| j        j        |	j        d<   |	S )Nz2create_node using arg not from this SubgraphTracerr  )rx  rc  ru  r<  arg_tree_leavesr$  r   r   r  r_   r   rp  r  r5  )rv   r  r  r   r   r-  r  r  r  r4  r   s             ro   rp  zSubgraphTracer.create_node  s     	t0"fdFKKK;".???I  H H!#ux}55 I+++G ,+++ ww""2vtVT9MM*.*;*E	&'rn   c                    t          |j                  dk    rg }|j                                        D ]K}|j        | j        k    r9|                    t          t          |j        j                                       L|D ]}|j                            |           | j                            |           | j	        
                    |j        d            d S r]  )r*  r1  r  r_   rN  rZ  r   r  r  ri  ry  r-  )rv   r4  user_graph_nodesuserother_graph_nodes        ro   rs  zSubgraphTracer.remove_node  s    tz??Q46
)) N N :++ %++HT$*:J5K5K,L,LMMM$4 D D  &112BCCCC
d### $$TY55555rn   c                    t                               d|||                                nd           || j        
J d            | j        rUt          |d          sD| j        j                            |g           	                    t          j                               || j        v r*t          j                    D ]}| d| }|| j        vr|} n| j        rkt          t!          | j                            }| j        |         j        }|r| j                            |          }	n5| j                            |          }	n| j                            d           }	|	5  |                     d|di |	          }
| j        r3|r1| j                                        \  }}|
| j        |<   || j        |<   n
|
| j        |<   |
cd d d            S # 1 swxY w Y   d S )
Nzcreate_graph_input %s %sz(none)zByou are required to provide a source for inputs on the root tracerF)allow_cell_or_freevarr  r  rm   )r  )r   r  r-  ru  r   r7   rc  r  
setdefaultr   r   extract_stackri  r   r   r   rZ  r4  r_   inserting_beforeinserting_afterrm  popitem)rv   r-  r  r  rg   r  candidate_name	prev_namer4  r  r  r   r   s                ro   r3  z!SubgraphTracer.create_graph_input  sa   		&#/FKKMMMX	
 	
 	

 >''S (''  	'eLLL !7BB62NNUU"022  
 4+++_&&  $(1!)AAA)DE B # 	4Xd&>??@@I+I6;D 7j11$77j0066*--d33C 	 	%%mT2rY%WWE' 7F 7/7799116(../(++16(.	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   #A GGGc                 R   | j         
J d            || j        v r| j        |         S |                     |j        j                  }t          |j        |j        j        d                    || j        |<   | j         *|j        | j         k    r| j                             |           |S )NzIlift_tracked_freevar_to_input should not be called on root SubgraphTracerrT  )	ru  r}  r3  r4  r-  rO   r5  r|  lift_tracked_freevar_to_input)rv   r  	new_proxys      ro   r  z,SubgraphTracer.lift_tracked_freevar_to_inputP  s     K##V $##
 D((('..++EJO<<	).%*//*JKKK&/U#;"u|t{'B'BK55e<<<rn   c                     t          |t          j        j                  s|S |j        | k    r|S |                     |          S )z
        If arg is a free variable, then lift it to be an input.
        Returns the new lifted arg (if arg was a freevar), else the
        original arg.
        )r$  r   r   Proxyr|  r  )rv   r  s     ro   r  z2SubgraphTracer.maybe_lift_tracked_freevar_to_inputb  sE     #ux~.. 	JZ4J11#666rn   )NFN)NNN)NNNN)rh   ri   rj   r   rw   r  rm  rp  rs  r3  r  r  r   r   s   @ro   r   r     s          KO2 2 2 2 2 2jC C C4 ` ` ` ` ` `F HL     &6 6 6,2 2 2 2j  $
7 
7 
7 
7 
7 
7 
7rn   r   )r   rY  r   r  r  r   r  r   r2  r  r^  r   r{  r   typingr   r   r   r   r   r	   r
   r   r   r  torch._guardsr   torch._loggingtorch.distributeddistributedr  torch.nntorch.utils._pytreerr  _pytreer<  r   r   r   r   torch._utils_internalr   r  r   %torch.fx.experimental._backward_stater   r0  r   r   r   torch.fx.passes.runtime_assertr   torch.utils._python_dispatchr   r  r   r   r   r   backends.registryr   r   bytecode_transformationr    r!   r"   r#   r$   ry  r%   current_scope_idr&   r'   r(   r)   r*   r+   r8  r,   r-   mutation_guardr.   r   r/   r0   rg   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   variables.baserP   variables.builderrQ   rR   rS   rT   rU   variables.listsrV   variables.miscrW   variables.nn_modulerX   variables.tensorrY   rZ   r[   r\   variables.torch_functionr]   torch._dynamo.symbolic_convertr^   	getLoggerrh   r   r  getArtifactLoggergraph_tabular_logr  graph_sizes_logr  re   rq   	lru_cacher   r   r   r   r   r   r   r   objectrV  r   rh  rx  r   r   Tracerr   rm   rn   ro   <module>r     s                           				 



      ! ! ! ! ! ! X X X X X X X X X X X X X X X X X X X X X X                       $ $ $ $ $ $ $ $ $       N N N N N N N N N N 0 0 0 0 0 0 : : : : : : ? ? ? ? ? ? U U U U U U U U U U J J J J J J F F F F F F D D D D D D D D D D D D 5 5 5 5 5 5 5 5            ' & & & & &       - - - - - -              0 / / / / / / / 0 0 0 0 0 0 @ @ @ @ @ @ @ @                                                                    ( , + + + + +              . - - - - - ( ( ( ( ( ( 1 1 1 1 1 1            C B B B B B  IHHHHHH g!!N44XwGG 11(LII.228]KK11(LII $              0 T4 4 4 - - - - - - - -  	% 	% 	% 	% 	%UX_ 	% 	% 	%               F 	S&[I, I, I, I, I, I, I, I,Z.J = = =@ &io'' m7 m7 m7 m7 m7RY m7 m7 m7 m7 m7rn   