
    Vh&                       d 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 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% ddlm&Z&m'Z'm(Z(m)Z)m*Z* ddl+m,Z, dd	l-m.Z. dd
l/m0Z0 ddl1m2Z2 ddl3m4Z4m5Z5m6Z6m7Z7 ddl8m9Z9 ddl:m;Z; ddl<m=Z=m>Z>m?Z?mZ@mAZA ddlBmCZCmDZD ddlEmFZFmGZGmHZHmIZImJZJ ddlKmKZK ddlLmMZM ddlNmOZO ddl>mPZPmQZQmRZRmSZSmTZT ddlUmVZV ddlWmXZX ddlYmZZZm[Z[ ddl\m]Z] ddl^m_Z_m`Z` ddlambZbmcZcmdZdmeZemfZfmgZgmhZhmiZimjZjmkZkmlZlmmZmmnZnmoZo ddlmpZpmqZqmrZrmsZsmtZtmuZumvZvmwZwmxZxmyZymzZzm{Z{m|Z|m}Z}m~Z~mZmZmZ ddlmZ ddlmZmZmZmZ dd lmZ dd!lmZmZ dd"lmZ dd#lmZmZmZmZ dd$lmZ erdd%lmZ  ej2                  e      Zej8                  j;                  ed&      Zej8                  j;                  ed'      Zej8                  j;                  ed(      Zej8                  j;                  ed)      Z ed*+       G d, d-             Z G d. d/      Z ejH                  d      d0        Ze G d1 d2             Zd3 Z G d4 d5ejP                  jR                        Z G d6 d7      Zeeef   Z G d8 d9      Zd:Zd; Z ejf                         Z G d< d=      Z G d> d?e"jl                        Zy)@a  
Core graph building functionality for PyTorch's Dynamo system. This module contains
the essential components for constructing and managing FX graphs during compilation:

- OutputGraph: Manages the overall graph construction and compilation process. It owns
  a SubgraphTracer and handles graph compilation, execution, and state management.
  OutputGraph also manages features like graph deduplication, symbolic shape handling,
  and tracking of side effects.

- SubgraphTracer: Handles the actual FX graph construction by tracing Python code.
  It supports advanced features like higher-order operators through nested tracers,
  lifting of free variables, and handling of symbolic shapes.

The module supports key Dynamo features including:
- Higher-order operators through nested SubgraphTracers
- Graph deduplication for optimization
- Symbolic shape handling and propagation
- Side effect tracking and management
- Guard insertion and management
    N)	dataclass)AnyCallablecastOptionalTYPE_CHECKINGUnion)fx)ShortenTracebackTensorifyScalarRestartAnalysis)CompileContext	CompileIdGlobalContextCheckpointStateSourceTracingContext)
FakeTensor)signpost_event)_make_graph_module)BackwardState)free_symbolsguard_scalaris_symbolicShapeEnv)insert_deferred_runtime_asserts)is_traceable_wrapper_subclass   )configexcgraph_break_hintslogging	variables)
CompiledFn
CompilerFn)create_call_functioncreate_instructioncreate_load_constInstruction	unique_id)code_context)	PyCodegen)enter_new_scope)BackendCompilerFailed!exceptions_allowed_to_be_fallback	SkipFrameunimplemented_v2unimplemented_v2_with_warning)apply_graph_deduplication)GraphRegionTracker)GuardBuilderinstall_guard)is_dynamic_nn_module)AttributeMutationExistingSideEffects)
AttrSourceBackwardStateSourceConstantSourceGetItemSourceGlobalStateSourceis_constant_sourceis_from_local_sourceLocalSourceNumpyTensorSource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_unique_name_wrtgraph_break_reasonsincrement_op_countlazy_format_graph_code
LazyStringnn_module_proxysameset_example_value)VariableTracker)BackwardStateGraphArgGraphArgTrackedFakewrap_fx_proxy)BaseListVariable)CellVariable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<   y)VariableTrackerCacheKeyvt_idsourceN)__name__
__module____qualname__int__annotations__r        J/home/dcms/DCMS/lib/python3.12/site-packages/torch/_dynamo/output_graph.pyrm   rm      s    J Nrv   rm   c                   *    e Zd Zd Zd Zd Zd Zd Zy)VariableTrackerCachec                     i | _         y N)cacheselfs    rw   __init__zVariableTrackerCache.__init__   s	    
rv   c                 h    t        t        |      |      }|| j                  vry | j                  |   S r{   rm   idr|   )r~   valuero   keys       rw   lookupzVariableTrackerCache.lookup   s/    %bi8djj zz#rv   c                 L    t        t        |      |      }|| j                  |<   y r{   r   )r~   r   ro   vtr   s        rw   addzVariableTrackerCache.add   s    %bi8

3rv   c                 d    t               }|j                  j                  | j                         |S r{   )ry   r|   update)r~   	new_caches     rw   clonezVariableTrackerCache.clone   s&    (*	tzz*rv   c                 8    | j                   j                          y r{   )r|   clearr}   s    rw   r   zVariableTrackerCache.clear   s    

rv   N)rp   rq   rr   r   r   r   r   r   ru   rv   rw   ry   ry      s    rv   ry   c                  4    t        j                  t              S r{   )torchdynamo_loggingget_step_loggerlogru   rv   rw   _step_loggerr      s    ..s33rv   c                   T    e Zd ZU dZeed<   eej                     ed<   dZ	e
ed<   d Zy)GraphCompileReasonzOStores why a given output graph was compiled; i.e. what caused the graph break.reason
user_stackTgraph_breakc                 H    | j                   rt        j                  |        y y r{   )r   rQ   appendr}   s    rw   __post_init__z GraphCompileReason.__post_init__   s    &&t, rv   N)rp   rq   rr   __doc__strrt   list	tracebackFrameSummaryr   boolr   ru   rv   rw   r   r      s,    YKY++,, K-rv   r   c                       fd}|S )Nc            
      N    D  cg c]  \  } }} | |i | c}}} S c c}}} w r{   ru   )fnargskwargsrandom_callss      rw   _gen_rand_valuesz1_get_gen_rand_values_fn.<locals>._gen_rand_values   s+    <HII(8D&D#F#IIIs    ru   )r   r   s   ` rw   _get_gen_rand_values_fnr      s    J rv   c                   f     e Zd ZdZdeeej                  j                  f   f fdZ	defdZ
 xZS )FakeRootModulez'Trick the constructor of fx.GraphModule
nn_modulesc                 l    t         |           |j                         D ]  \  }}t        | ||        y r{   )superr   itemssetattr)r~   r   kv	__class__s       rw   r   zFakeRootModule.__init__   s6    $$& 	 DAqD!Q	 rv   returnc                      y)NzFakeRootModule(...)ru   r}   s    rw   __repr__zFakeRootModule.__repr__   s    $rv   )rp   rq   rr   r   dictr   torchnnModuler   r   __classcell__r   s   @rw   r   r      s0    1 4UXX__(<#=  
%# %rv   r   c                   j    e Zd ZdefdZdej                  j                  deej                     fdZ
y)WrapperBackendbackendc                     || _         y r{   )r   )r~   r   s     rw   r   zWrapperBackend.__init__   s	    #*rv   gmexample_inputsc                    t        |      | _        || _        t        j                  | j                        }| j                  ||      | _        | j                  "| j                  | j                  j                  u r| j                  j                  S t        j                  s| j                  S 	  | j                  j                  t        |       } | j                  t        |       }t        ||      r| j                  | j                          S t        d|        # t        $ r t        j                  d        w xY w# | j                          w xY w)Nzincorrect results of backend zerror in verify_correctness)rG   restorer   copydeepcopyr   	candidateforwardr   verify_correctnessrI   rV   RuntimeError	Exceptionr   	exception)r~   r   r   copy_gmcorrectresults         rw   __call__zWrapperBackend.__call__   s
   (,--(g~>>>!T^^tww%F77??"((>>!	%dggoo|N'CDG#T^^\.%ABF GV$~~ LLN !>tfEFF  	MM78	 LLNs   /AD D  D??E EN)rp   rq   rr   r#   r   r   r
   GraphModuler   Tensorr   ru   rv   rw   r   r      s4    +
 +588// ellAS rv   r   c            
           e Zd ZU dZeed<   deeef   de	e
   dededef
 fdZd	 ZdVd
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j8                  d        Zed        Zed        Zed        Zd Z d Z!d Z"e#jH                  d        Z%ed        Z&ed        Z'ed         Z(ed!e)jT                  jV                  fd"       Z,ed!eeef   fd#       Z-dWd%Z.d& Z/d' Z0ed(        Z1d) Z2d* Z3d+ Z4dXd,Z5d- Z6e7d.        Z8d/ed0ed!e9jt                  fd1Z;d2e<e)jz                  j|                  e)j~                  ef   fd3Z@d4 ZA	 dYd5e	eB   fd6ZCd7 ZDd8 ZEd9 ZFd:efd;ZGe#jH                  d<        ZHd= ZId> ZJed!eKe9j                     fd?       ZMed!eKeN   fd@       ZOdAe9j                  d!eQfdBZRdAe9j                  d!eQfdCZSdD ZTdE ZUd!eKe)j~                     fdFZVdZdGZWdZdHZXdZdIZYdJeKeZ   d!d$fdKZ[dZdLZ\d!efdMZ]d!efdNZ^dZdOZ_dPed!d$fdQZ`dRee9j                  gd$f   d!d$fdSZadTe)jr                  j                  fdUZb xZcS )[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.
    side_effectscode_optionscompiler_fnexportlocal_scopeglobal_scopec                    t         |           t        | |      g| _        i | _        || _        || _        || _        i | _        g | _	        t        t              | _        t               | _        |	j                  |	j                   |	j"                  d| _        t'               | _        g | _        t-        | j*                  t.        j0                  t.        j2                  t.        j4                  t.        j6                  | j$                        }dd lmc m} |j=                  d      5  t>        j@                  jC                  || j
                  rdnd| j
                        }d d d        tE              | _#        tI        jJ                         | _&        | jO                          tQ        jR                  tT              | _+        i | _,        t[        |       | _.        t_               | _0        tc        jd                         | _3        ti        |      | _5        g | _6        d| _7        g | _8        || _9        || _:        || _;        || _<        i | _=        g | _>        g | _?        d| _@        i | _A        t>        j                  j                         | _D        t>        j                  j                         | _F        |
| _G        d| _H        t        i       | _I        t        i       | _J        | j                          i | _L        g | _M        d | _N        g | _O        i | _P        d | _Q        d | _R        | j                         | _T        t               | _U        y # 1 sw Y   xY w)	N)	is_export)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   )Vr   r   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   r2   region_trackerr   r   r   capture_scalar_outputs capture_dynamic_output_shape_opsr   r   torch._functorch.config
_functorchpatchr   _subclassesFakeTensorModer   tracing_contextr   current_compile_iddynamo_compile_idinit_ambient_guardscollectionsdefaultdictr   tracked_fakes_id_to_sourceparam_name_to_sourcer7   r   ry   variable_tracker_cache	itertoolscountunique_var_idr   r   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_enabled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)r~   r   r   r  r   r   r   r   r   f_coder  r   _config	fake_moder   s                 rw   r   zOutputGraph.__init__  s
    	&tv>? CE "4&KM*68#$78+.5
 ~~!--$33
 12 13 ,,!'!>!>+1+R+R8>8j8j4:4b4bnn
	$ 	21]]E]J 	))88#.2kkdu{{	 9 I	 0>i/H--/ 	 	  " ##D) 	' BD!'- ';&<#&__. .68   OQ# 2=(& RT"<>+- KM  ',hh&I&I&K#+088+S+S+U()B& 27. >AW ADB! 	  AC-  	 "& 57 ;=>B!15 224 	2 -0Ee	 	s   ';L>>Mc                 |    | j                   d   }t        |t              s|j                  }| j	                  d|      S )N__builtins____builtins_dict__)r   
isinstancer   __dict__install_global)r~   
f_builtinss     rw   r#  z-OutputGraph.install_builtins_dict_in_fglobals  s=     &&~6
*d+#,,J""#6
CCrv   hookc                     | t        | j                         }|| j                  vsJ || j                  |<   || j                         fS r{   )lenr   get_backward_state_proxy)r~   r0  prefixnames       rw   add_backward_state_hookz#OutputGraph.add_backward_state_hook  sT    #d112344.....$(D!T22444rv   c                 f   | j                   | j                  rt        dddg        t               }| j                  j                  dt        |      |t                     | _         t               | j                   j                  j                  d<   | j                         | _        | j                   S )Nz&backward_state does not support export z3Compiled autograd doesn't work with `torch.export`.gb_typecontextexplanationhintsdynamo_backward_statero   grapharg)r!  r   r/   r   root_tracercreate_graph_inputtyper9   rY   nodemetanew_varr"  )r~   example_values     rw   r3  z$OutputGraph.get_backward_state_proxy  s    $$,{{ D U	 *OM(,(8(8(K(K']#*,	 )L )D% ?T>UD%%**//
;&*llnD#(((rv   c                 Z   | j                   j                  t               j                  t        j
                               | j                   j                  t               j                  t        j                               | j                   j                  t               j                  t        j                               | j                   j                  t               j                  t        j                               | j                   j                  t               j                  t        j                               t        j                  j                  j                         }|A| j                   j                  t               j                  t        j                               y y r{   )guardsr   rB   
make_guardr3   	SHAPE_ENVr<   DETERMINISTIC_ALGORITHMS	GRAD_MODEDEFAULT_DEVICETORCH_FUNCTION_STATEr   r  r   peek_interpreter_stackFUNCTORCH_STACK_MATCH)r~   cis     rw   r  zOutputGraph.init_ambient_guards  s    	(33L4J4JKL**<+P+PQ	
 	)+66|7M7MNO)+66|7R7RST**<+L+LM	
 XX  779>KKOO!#..|/Q/QR rv   c                 ^    | }| j                         }t        | j                        j                  fd       j	                  t        t        j                  j                  |             j                  t        |      d       j                  |       | j                  j                  j                                t        |      }t!        j"                  | j                  ||      }t%        j&                         j(                  j*                  j-                  |       |S )z]
        call fn(*args) before the graph runs and turn the result into a fake input.
        c                  P     j                  j                  j                        S r{   )load_import_fromrq   rp   )cgr   s   rw   <lambda>z3OutputGraph.synthetic_graph_input.<locals>.<lambda>  s     B'' rv   F)rF  r*   r  add_push_nullforeachmapr!   ConstantVariablecreatecall_functionr2  storer  extendget_instructionsrC   rX   buildr   getguards_contextdynamo_guardsremove_guards_with_source)r~   r   r   rG  varnamero   r   rV  s    `     @rw   synthetic_graph_inputz!OutputGraph.synthetic_graph_input  s     D	,,.t||$
	
 	

3y1188$?@
TE*
%%b&9&9&;<%g. &&t||]FK++99SS	
 rv   r   c                 :    | j                   j                  |       y r{   )r   r   )r~   r   s     rw   add_cleanup_hookzOutputGraph.add_cleanup_hook,  s    !!"%rv   c                 z    t        | j                        D ]	  } |         | j                  j                          y r{   )reversedr   r   )r~   r0  s     rw   call_cleanup_hookszOutputGraph.call_cleanup_hooks/  s3    T//0 	DF	  "rv   c                      | j                   d   S Nr   r   r}   s    rw   rA  zOutputGraph.root_tracer4  s    ||Arv   c                      | j                   d   S Nro  r}   s    rw   current_tracerzOutputGraph.current_tracer8  s    ||Brv   c                 2    t        | j                        dk(  S )Nr   )r2  r   r}   s    rw   is_root_tracerzOutputGraph.is_root_tracer<  s    4<< A%%rv   c                 .    | j                   j                  S r{   rs  rg   r}   s    rw   rg   zOutputGraph.graph@  s    ""(((rv   c                 &    || j                   _        y r{   rw  )r~   r   s     rw   rg   zOutputGraph.graphE  s    $)!rv   c                 .    | j                   j                  S r{   )rs  input_name_to_proxyr}   s    rw   rz  zOutputGraph.input_name_to_proxyI  s    ""666rv   c                 .    | j                   j                  S r{   )rs  real_value_cacher}   s    rw   r|  zOutputGraph.real_value_cacheM  s    ""333rv   c                 .    | j                   j                  S r{   )rs  bound_symbolsr}   s    rw   r~  zOutputGraph.bound_symbolsQ  s    ""000rv   c                 :     | j                   j                  |i |S r{   )rs  create_proxyr~   r   r   s      rw   r  zOutputGraph.create_proxy[  s     /t""//@@@rv   c                 :     | j                   j                  |i |S r{   )rs  create_noder  s      rw   r  zOutputGraph.create_node^       .t""..???rv   c                 :     | j                   j                  |i |S r{   )rs  remove_noder  s      rw   r  zOutputGraph.remove_nodea  r  rv   c              #     K   t               }	 |r|j                  | j                  u sJ |j                          |r|n,t	        | | j                  || j                  j
                        }| j                  j                  |       | |j                  d d d        | j                  j                          y # |j                  d d d        | j                  j                          w xY ww)N)parentsource_targetr   )
r+   r  rs  	__enter__r   r   r   r   __exit__pop)r~   r  prior_tracernew_scope_ctxtracers        rw   	subtracerzOutputGraph.subtracerd  s     ')	#**d.A.AAAA##%   #.."/"11;;	  LL'L""4t4LL ""4t4LLs   C*A<B8 
.C*8/C''C*c                     | S r{   ru   r}   s    rw   outputzOutputGraph.output|  s    rv   c                 .    | j                   j                  S r{   )r   r(  r}   s    rw   r(  zOutputGraph.fake_mode  s    ##---rv   c                 B    | j                   j                  j                  S r{   )r   r(  r   r}   s    rw   r   zOutputGraph.shape_env  s    ##--777rv   r   c                 B    | j                   j                  j                  S r{   )r   rc  rd  r}   s    rw   rI  zOutputGraph.guards  s    ##22@@@rv   c                 B    | j                   j                  j                  S r{   )r   module_contextr   r}   s    rw   r   zOutputGraph.nn_modules  s    ##22===rv   Nc                 `   t        t        t        t        t        dt
        f   t        f   f   ||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<   y)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   r   r   tupler   r   r   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)r~   outr  s      rw   r  zOutputGraph.save_global_state  s    eHS#X.4556 ? ))88EE
  ))''2
-. ).(>(>@U@U@W'X^$ e88&A%%f-,
'(
 e88%@%%e,0
+,
 e66?$$V,.
)*
 e66>$$U+.
)*
 ,,++-2
-.rv   c                 :    | j                   j                  |       y r{   )r  r   )r~   txs     rw   push_txzOutputGraph.push_tx  s    #rv   c                 6    | j                   j                         S r{   )r  r  r}   s    rw   pop_txzOutputGraph.pop_tx  s    ##%%rv   c                 P    | j                   s| j                  S | j                   d   S rq  )r  r  r}   s    rw   
current_txzOutputGraph.current_tx  s$    #'#3#3t||M9I9I"9MMrv   c                 ,    t        | j                        S r{   )rJ   rg   r}   s    rw   rJ   zOutputGraph.count_calls  s    4::&&rv   c                 X    t        t        | j                  j                              dk(  S rn  )r2  r   rg   nodesr}   s    rw   is_empty_graphzOutputGraph.is_empty_graph  s!    4

(()*a//rv   c                     |sJ | j                   }|j                  d      D ]$  }t        |t              r||   }t	        ||      }& |S N.)r   splitr,  r   getattr)r~   keysobjr   s       rw   get_submodulezOutputGraph.get_submodule  sN    tBF//C 	&A#t$!fc1o		&
 
rv   c                     t        | j                  d         }	 | dt        | j                         }||vr| j                  dxx   |fz  cc<   |S 9)Nco_varnames_)r   r   r   r	  )r~   r5  existingvars       rw   rF  zOutputGraph.new_var  s`    t((78F!D!3!3456C("!!-0SF:0
	 rv   c                 X    || j                   d   vr| j                   dxx   |fz  cc<   yy)z/Ensure self.code_options.co_names contains nameco_namesN)r   )r~   r5  s     rw   update_co_nameszOutputGraph.update_co_names  s2    t((44j)dW4) 5rv   c                     dj                  t        t        |             }t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }|r|d   j                         sd|z   }|S )	Nr  z^[GL]\['?(.*?)'?\]$z\1z	\[(\d+)\]z_\g<1>z[^a-zA-Z0-9]r   sub)joinrZ  r   rer  isalpha)namesr5  s     rw   module_key_namezOutputGraph.module_key_name  sm     xxC(vv,eT:vvlIt4vvosD147??,4<Drv   attr_prefix
attr_valuec                     t        || j                        }|| j                  |<   | j                  d|di       }t        |j                  |       |S )Nget_attrru   )rP   r   r  rW   rD  )r~   r  r  	attr_nameproxys        rw   %register_static_attr_and_return_proxyz1OutputGraph.register_static_attr_and_return_proxy  sM     (T__E	 &0	"!!*iR@%**j1rv   targetc                    
 t         j                  j                        r!t        j                   j
                  fi S t              dv sJ d   t        t              rJ t        t        j                        r2 j                   j                         s j                   fd}nt        t        j                  j                        r^t        t        j                  j                        sJ r/t!        j#                  t$        j&                               fd}n?fd}n8t        t        j(                  t        j*                  f      r fd}n fd} j,                  j/                         D ]  \  }}|u s ||      c S  t1        j2                  | 
t5        
 j,                   j6                        
 j,                  
<   t        t        j                  j                        r_
 fd}t9        d      r j;                         D ]  \  }}	 ||        t9        d	      r j=                         D ]  \  }}	 ||         |
      S )
Nro   c           	         j                   J j                   | <   j                  j                  j                  v r#j                  j                  j                     S t	              dk(  r9t        t              s)t        j                  t        j                               n3t              s(t        j                  t        j                               t        j                  j                  d| di       fdi}j                  j                  j                  j                  |      }d|j                   j"                  j$                  vsJ t'              |j                   j"                  j$                  d<   |S )Nguardedr  ru   rG  tensor_dict)r  r  r  r   rO   r,  r@   r4   rJ  r3   ID_MATCHr=   TENSOR_MATCHr\   r  track_object_existingr  rD  rE  rF   )
module_keyr   optionsr~   ro   r  r  s     rw   	wrap_namez6OutputGraph.register_attr_or_module.<locals>.wrap_name!  sM   00<<<8>))*5 T\\00===<<..;;FCC*62i?
-I "&"3"3L4I4I"JK+F3!&"3"3L4M4M"NO"LL''
JBG #) 	 \\((55KKFTVW$BHHMM,>,>>>>4H4P""=1	rv   c                 2    t        t              | fi S r{   )r`   rC  r  r  r  s    rw   r  z6OutputGraph.register_attr_or_module.<locals>.wrap_nameG  s    +DL*fXPWXXrv   c                 0    t        j                  fi S r{   )r!   UnspecializedNNModuleVariabler  s    rw   r  z6OutputGraph.register_attr_or_module.<locals>.wrap_nameO  s    $BB6UWUUrv   c           	      Z    t        j                  j                  d| di       fdiS )Nr  ru   sym_num)rb   r\  r  )r  r  r~   r  s    rw   r  z6OutputGraph.register_attr_or_module.<locals>.wrap_nameZ  sA    &--%%j*b"E # 	 rv   c                     j                   j                  |        j                  | <   t        j                  t        |             S )N)source_name)r  r  r   rX   ra  r:   )r  r~   r  s    rw   r  z6OutputGraph.register_attr_or_module.<locals>.wrap_namee  sD    ++J706!!*-&,,&.Z"H rv   c                     j                   J t        |       } d|  }|j                   |<   t        t              r1| j                  t
        j                  |j                               <   y y r  )r  rA   r,  r?   r  r   r  r5  )	leaf_name
new_sourcenew_namer5  r~   ro   s      rw   register_leaf_namez?OutputGraph.register_attr_or_module.<locals>.register_leaf_namev  sz    00<<<.vyA
"V1YK06@))(3fk2 " 99#33JOO4EF 3rv   _parameters_buffers)r5   r  r   rX   ra  r  r   r,  rA   r   r   rs  ru  rA  r   r   r4   rJ  r3   	NN_MODULESymIntSymFloatr   r   r   r  rP   r   hasattrnamed_parametersnamed_buffers)r~   r  r  r  r  r   r   r  r  r  r5  ro   r  s   `` `      @@@rw   register_attr_or_modulez#OutputGraph.register_attr_or_module  s	     (;(;< #((&LGLLw-7""""f&7888fell+((F&&( )) @ 0fehhoo666f//0F0FGHYV u~~ >? OO))+ 	$DAqF{ |#	$
 **E2"4$:K:KL &fehhoo." v}-$*$;$;$= 2LIq&y12vz*$*$8$8$: 2LIq&y12 rv   c                    | j                   j                  d      }t        |      }|sg i fS g }i }g |j                  |j                  j                         | j                  j                  j                         }|r |j                         }t        |t              r,t        |j                  t              sJ ||j                  z  }O|| j                  j                  vst        |j                  t              r`t        |j                   t"              rFt        |j                   j$                  t&              r"|j                   j$                  j(                  |v s|j                   j$                  j(                  }||vrg ||<   ||   j+                  |       |r i }	i }
| j,                  D ]  }t        |j.                  t              r2t        |j                   t&              r|j                   j(                  |v sQ|j                   j(                  }|| j0                  d   v sJ ||   D ]  }|j                   |
v r|j                   j2                  }||	vrV| j5                  | d      }||	|<   |j7                  t9        d|      t;        |      t9        d      t9        d|      g       |j                   }t'        |	|         |
|<    " ||
fS )Nr~   r  _ref	LOAD_FASTargvalBINARY_SUBSCR
STORE_FAST)r   rb  rN   stacksymbolic_localsvaluesr   store_attr_mutationsr  r  r,  r]   r   r   mutation_typer6   ro   r;   baser?   
local_namer   	graphargs_exampler   indexrF  r_  r%   r&   )r~   r  maybe_gmstolen_list_namesalias_instsneeds_aliasqueuexstolen_namevisitedoverridden_sourcesarg	list_namelist_idx
alias_name
old_sources                   rw   handle_aliases_for_stolen_listsz+OutputGraph.handle_aliases_for_stolen_lists  s   ##''//9 r6M8:
XX
&&(
 3388:
 		A!-.!!''4000  T..CCC!!//3LMqxx7qxx}}k:HHMM,,0AA((--22K+-+-K($++A.+ . 35>> %	PC3<<.szz;7JJ))[8 

--I 1 1- @@@@ + P8811 88>>7*!%$+T*"J )3GH%&&.{9M-h7.?.|JO	 XX
1<WX=N1O":.5P%	PV ...rv   r   c                 \   ! |J ddl m} | _        | _        d _        t
        j                  d|       t        d |j                  D              st        ddd	g t        j                  
       g !t        j                  dk\  r|j                  D ]  }|j                  dk(  r'!j!                  t#        d|j$                               9|j                  dk(  r3!j!                  t#        dt'        |j(                  d                      {!j!                  t+        j*                  |               j,                  r j.                  rJ d       !j1                   j,                          j3                  |      \  }}!j1                  |       ! fd}t5        |j                        D ]  }	|	j7                  ||j8                         !  j;                          |j=                          t?        |j@                        }
|
D ]  }|jC                            jE                         } jF                  jI                         D ci c]  \  }}|tK        |       }}}tM        |      }g }i }|jN                  jI                         D ]  \  }}tQ        |jR                  tT              r|jR                  jV                  |k(  r:tQ        |tX              r|jV                  |k(  rZt        j                  dk\  rtZ        j]                  t^        |      rtZ        j]                  t^        |      rJ ||vrg ||<   ||   j!                  |        |ja                         D ]7  }|j1                  ||          |
j1                  |gt'        ||         z         9 t'         jb                        dkD  r |        g } je                  d       _3         |ti         jb                              } jk                  d|      }tm        |||      }|j1                  |jo                  |d             |j1                  tq        dd             |j!                  |js                  |jt                  jf                                jw                  |       |
rt        d |
D              rt        d |
D              rt'        ty        |
            t'        |
      k(  rՉ jz                  j}                         rt'        |j~                        dk7  s j                  s |         jw                   j                  |t?        t5        |
            ||      t#        dt'        |
            gz           jw                  t5        |      D cg c]  }tm        ||      js                  |        c}       y je                  d      }tm        ||||      } j                  ||
|       tm        ||||j                  jI                         D ci c]  \  }}|dkD  s|d c}}|      } j                  ||
|       d}g } t         j                        dk7  st'        |j                        dk7  r| j1                   j                  ||j                         ||             t'        |j                        dk7  r#| j!                  |js                  |             d}n,| j!                  t#        d             n j                  |        |         jw                  | |j                         z           jw                  t5        |      D cg c]  }tm        ||      js                  |        c}       |r- jw                  tm        ||      j                  |      g       yyc c}}w c c}w c c}}w c c}w ) zw
        Generate a subgraph to continue execution on user code.
        Automatically restore live variables.
        Nr   disableTzCOMPILING GRAPH due to %sc              3   <   K   | ]  }|j                           y wr{   )can_restore).0blocks     rw   	<genexpr>z/OutputGraph.compile_subgraph.<locals>.<genexpr>  s     C55$$&C   z'Attempt to compile graph in a try blockr8  z9Dynamo cannot compile traced graphs while in a try block.r9        	MAKE_CELLr  COPY_FREE_VARSco_freevars)r  z)export does not support pregraph_bytecodec                  H    j                           j                          y r{   )add_output_instructionsr   )prefix_instsr~   s   rw   append_prefix_instsz9OutputGraph.compile_subgraph.<locals>.append_prefix_insts  s    ((6 rv   )is_graph_break)r$     r   random_values__gen_rand_values)r  Fc              3      K   | ]K  }t        |t        t        t        f       xr) t        |t              xr |j                         t        u   M y wr{   )r,  rd   ra   re   rb   python_typefloat)r  r   s     rw   r!  z/OutputGraph.compile_subgraph.<locals>.<genexpr>c  s`        3,4  V $A7TAMMOu<TUVs   AAc              3   <   K   | ]  }t        |t                y wr{   )r,  rc   r  r  s     rw   r!  z/OutputGraph.compile_subgraph.<locals>.<genexpr>o  s     HaJq.1Hr"  UNPACK_SEQUENCE	graph_out)tempvarsr  POP_TOP)K
decoratorsr  partial_convertcompile_subgraph_reasonr  r   debugallblock_stackr/   r   CAUSED_BY_EARLIER_GRAPH_BREAKsysversion_infor+  opnamer   r%   r  r2  r   r   r  r   r_  r  rk  exitr   cleanup_graphprune_dead_localsr   r  realize
dedup_passr   r   rU   r   r  r,  ro   r?   r  r^   rC  __instancecheck__r_   r  r   rF  r  r   r.  r*   load_function_namer$   create_storer  r*  r   r   is_emptydebug_localsr   compile_and_call_fx_graphcodegen_suffixusesrJ   rg   graph_outputsgraph_output_varsrun_compiler_collectiver`  create_delete)"r~   r  r;  r   r  instr  r  r,  r   stack_valuesr   output_replacementsr5  modnn_modules_proxiesrootrestore_varsval_to_namesr   r   random_calls_instructionsrand_fnrand_fn_namecodegenr  graph_output_varpass1valr  pass2stored_graph_output_varr  r+  s"   `                                @rw   compile_subgraphzOutputGraph.compile_subgraph  s    !!!'.'-$		-v6CBNNCCAW&DD	 +-w& 9;;+- ''*;t{{K [[$44 ''*,#boom6T2U !''		$89 **t{{ 	
7	
; 	D223*.*N*Nr*R''K(	! bnn- 	>EJJr&*<*<J=	> 	
BHH~ " 	EMMO	 #oo/ 9=8M8M8O
+44D/#&&
 
 01"$9; &&,,. 	&DAq !((K0QXX5H5HA5M!\*q||q/@7*)),:  11,BBB$"$QO""1%)	&* ""$ 	<AQ0c,q/&: :;	<
 t  !A%!(*%%)\\/%BD"5d6G6GHIG../BGLLD=OPG%,,**<> &,,-A!U-KL%,,$$RYY%@%@A (()BC   &  H<HHC%&#l*;;!!**,(A-''!((..Xl34d<O &&7S=NOPQ ((
  (5	  b5GHUU  $||K8D*?QE L%8  6;jj6F6F6HV
UETUI#t)V#5E L%8&+#F4::&!+s53F3F/G1/L22E335t=P u**+q0MM%"4"45E"FG.2+MM"4Y"?@ ,,R0!((%2H2H2J)JK ((
  (5	  b5GHUU ',,!3E'-(89 'W
h& W8s   
^>#^^#^#>#^)c                    | j                   j                         | j                  rs| j                  rJ | j                  j	                         D ]H  \  }} |       j                  j                  | j                               j                  |       J | j                   j                         |j                  D ]h  \  }j                  fd       |D ]
  } |        j                  t        t        |      d             j                  t        d      g       j j!                  ||j                          | j                   j#                         y )Nc                              S r{   ru   )rV  	debug_vars   rw   rW  z,OutputGraph.codegen_suffix.<locals>.<lambda>  s    R	] rv   Fr9  )value_from_source)r   codegen_save_tempvarsr   r   r   append_outputcreate_loadr"  
store_attrcodegen_hooksrM  rX  extend_outputr$   r2  r%   restore_stackcodegen_update_mutated)	r~   r  rV  rV  r5  rc  r   r  ri  s	      `    @rw   rO  zOutputGraph.codegen_suffix  s4    	//3{{"?!00668 $	c3  0G0G!HId#$ 	''+  " 	>OIt23 31#d)UCD0;<=	> 	RYYG004rv   c                    | j                   sJ t        | j                  j                        }|D ]  }|j                  j                  dd         t        j                         }t        ||dd       D ]  \  }}|j                  t        j                  j                  u s-t        |j                        | fk(  sH|j                  rU|j                  d   }|j                  t        j                  j                  u st        |j                        | fk(  s|j                  r|j                  d   }| j                  j                  |       | j                  j                  |        y)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   rg   r  rE  r  r   r  zipr  r  _set_grad_enabledr  r   _erased
erase_node)r~   r  rD  r  node1node2s         rw   rE  zOutputGraph.cleanup_graph  s    TZZ%%& 	6DIIMM.5	6 ,,.uQRy1 	1LE5 : ::%**%l*:)<<$zz!}LLEHH$>$>>ejj),.>-@@!MM#(::a=LJJ))%0JJ))%0	1rv   c                 \   i }| j                   j                  D ]  }|j                  j                  dd       }t	        |t
        j                  j                        sD|j                         }|D cg c]  }t	        |t              r|n
t        |      ! c}||j                  <    |S c c}w )NrG  )rg   r  rE  rb  r,  r   r   r   sizers   reprr5  )r~   retrD  rG  r|  ss         rw   get_graph_sizes_structuredz&OutputGraph.get_graph_sizes_structured  s    JJ$$ 	VD IIMM/4@M-):):)E)EF$))+PT!U1z!S'9!tAw"F!UDII		V
 
 "Vs   1$B)r5  c                 V   d}|d| dz  }| j                   j                  D ]  }|j                  j                  dd       }t	        |t
        j                  j                        sE|j                         }||j                   dt        |       dz  }g }d}|D ]g  }t	        |t              r|j                  |       %t	        |t
        j                        r(d}|j                  |j                  j                         g  |s||j                   d	t        |       dz  } |S )
NzTRACED GRAPH TENSOR SIZES
z===== z =====
rG  z: 
FTz (concrete): )rg   r  rE  rb  r,  r   r   r   r|  r5  r  rs   r   r  rD  hint)	r~   r5  graph_sizes_strrD  rG  r|  concrete_size
has_symintszs	            rw   get_graph_sizeszOutputGraph.get_graph_sizes  s   7VD622JJ$$ 	D IIMM/4@M-):):)E)EF$))+dii[5;-r#BB ""
 B!"c*%,,R0#B5%)
%,,RWW\\: "'#yyku]7K6LBO#	( rv   c              #     K   | j                   j                  j                         }i }| j                  |       	 | j                   j                  j	                  |       d | j                   j                  j	                  t        |             y# | j                   j                  j	                  t        |             w xY w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   )r~   prior_global_statecurrent_global_states      rw   restore_global_statez OutputGraph.restore_global_state  s     
 "11@@PPR<>#78	  //BBCUV   //BB,-ABD  //BB,-ABs   9C)B %/C0CCc                    |j                   x2j                  $j                  }t        j	                  dj
                         t        j                  j                  dd fd       t        j                  j                  |j                         t        j                  j                         z        5  t        dd      5  d g|j                         z  }t        j                   |j
                  |	       |_        d d d        d d d        |j"                  j%                          t&        j(                  y y # 1 sw Y   =xY w# 1 sw Y   AxY w)
Nzcompiler_collective %sartifactc                      dddS )Ncompiler_collectivestring)r5  encodingru   ru   rv   rw   rW  z5OutputGraph.run_compiler_collective.<locals>.<lambda>/  s    1 (% rv   c                  8     j                   j                         S r{   )local_staterender)dss   rw   rW  z5OutputGraph.run_compiler_collective.<locals>.<lambda>3  s    2>>#8#8#: rv   )metadata_fn
payload_fnr  T)log_pt2_compile_event)group)distributed_state
all_states
compile_pgr   infor  r   _loggingtrace_structuredr  devicerankdevice_countrL   r|  distall_gather_objectspeculation_logr   r    CompileCollectiveRestartAnalysis)r~   r  r  r  r  s       @rw   rS  z#OutputGraph.run_compiler_collective)  s   &&&B38MJHH-r~~>NN++ ; ,  

!!*//"3ejj6M6M6O"OP+2$O+ #Vjoo&77
&&z2>>T *+ + $$&666+ 9N3+ + + +s$   <E
>EEE		EEc                     t         j                  j                  j                         5  ddlm}  j                  sJ  j                  |       t        d      }t        |t              sJ t        |t              sJ  j                  dd j                  j                  t        d |D                    fi       }|j!                         D ]  \  }}	|j#                  |	        |j$                  j                  j'                  ||       t(        j*                  sQ j-                          t/        t1        j2                  | j4                         j6                  | j8                          j;                          t=         j4                        }
t>        d   dxx   |
z  cc<    jA                           jB                  jE                          tG        | j4                         jH                  D ]
  } |         jJ                  _%         jL                  jO                         jP                  d	<    jR                  jP                  d
<   tT        jW                  dtY        |ddd             t         jZ                  j]                  d fdfd        j_                           j`                  jb                  } j8                  s_ddl2m3c m} |ji                  d      5  t         jj                  jm                  |j6                        }ddd        j`                  _1         jo                         5   jq                        }ddd       ddl9m:} t        |      s&t        tw        |dd      |      rR|jx                  dk(  rCt        ||      r|n|jz                  }|j}                  |       t        ||      s|j~                  } ||      }t>        d   dxx   dz  cc<    j                  ||       t        |      }|j                  |       |j                         cddd       S # 1 sw Y   #xY w# 1 sw Y   xY w# 1 sw Y   yxY w)zr
        Generate code from self.graph and return the Instruction()s to
        call that generated code.
        r   r  __compiled_fnr  c              3   <   K   | ]  }|j                           y wr{   )as_proxyr5  s     rw   r!  z8OutputGraph.compile_and_call_fx_graph.<locals>.<genexpr>U  s     5Oqajjl5Or"  )r   statscalls_capturedr  r   %sT)include_strideinclude_devicecoloreddynamo_output_graphc                  (    d j                         iS )Nsizes)r  r}   s   rw   rW  z7OutputGraph.compile_and_call_fx_graph.<locals>.<lambda>  s    $"A"A"CD rv   c                  ,     j                  ddd      S )NFT)print_outputr  r  )print_readable)r   s   rw   rW  z7OutputGraph.compile_and_call_fx_graph.<locals>.<lambda>  s    2#4#4!&tD $5 $ rv   )r  r   NFr   )r   )_LazyGraphModule__self___lazy_forwardunique_graphs)Dr   _guardsr   clear_framer:  r  r  rS  r(   r,  r   r   r  rs  
create_argr  r   replace_all_uses_withr  _maybe_preserve_original_metar   do_not_emit_runtime_assertsremove_unused_get_attr_nodesr   r
   r   rg   r   r   remove_unused_graphargsrJ   rK   &remove_tensorify_specialized_graphargsr|  r   r   r  r<  r  r   rE  r   graph_code_logr=  rS   r  r  rl  r   r(  r   r   r   r   r   r  call_user_compilertorch.fx._lazy_graph_moduler  r  rp   r  force_recompiler   install_global_unsafer*   make_call_generated_coder`  )r~   r  rvrZ  replaced_outputsr  r5  output_nodeold_nodenew_nodencallsregister_finalizerold_fake_moder'  backend_fake_modecompiled_fnr  lazy_gmrV  r   s   `                  @rw   rN  z%OutputGraph.compile_and_call_fx_graphA  s   
 ]]))557 x	)+####((,_-Db$'''dN333**$$//5OB5O0OPR	K '7&<&<&> 9"(..x89 II$$BB2{S55 113/NN44NN;;	 ((* ,FW./69/779 !!'')#D$**5B&*&A&A '""2&' *.)E)EB&55::< GG67 ,0+A+ABGG'(  &"T$PT NN++%D ,  ##% 00::M;;99]]E]R (-(9(9(H(H"/"9"9 )I )% 2C$$.**, :"55b9: E+'787;
DACST((O; "+/?@  $--  !009!+/?@")//K!+.KWo.!3.&&t[92B''-&&(qx	) x	)X : :mx	) x	)s=   J?Q*+P6)Q>QCQ6Q 	;QQ	QQc                 :    | j                   j                  d      S )Nplaceholderop)rg   
find_nodesr}   s    rw   placeholderszOutputGraph.placeholders  s    zz$$$66rv   c                 Z    | j                   D cg c]  }|j                  d    c}S c c}w Nr@  )r  rE  r~   rD  s     rw   r  zOutputGraph.graphargs  s%    262C2CD$		*%DDDs   (r   c                 n    t        dddd      5  | j                  |      cd d d        S # 1 sw Y   y xY w)NOutputGraph.call_user_compilerbackend_compileT'aot_autograd_cumulative_compile_time_us)
phase_namer  dynamo_compile_column_us)rL   _call_user_compiler)r~   r   s     rw   r  zOutputGraph.call_user_compiler  s;    ,("&%N	
 	0 ++B/	0 	0 	0s   +4c                    | j                   J d}g }|j                  j                  D ]6  }|j                  dv r|dz  }|j                  dk(  s&|j	                  |       8 t        |       |D ]"  }|j                  d   }|j                  |_        $ | j                  |_
        | j                  |_        t        | j                   d      r| j                   j                  nd}	  t               t         j"                  d|        | j                   }t$        j&                  rt)        |      } ||| j+                               }	 t               t         j"                  d	|        t-        |	      sJ d
       	 tO        ddi | jP                  |tS        |j                  j                        tS        |      d       	S # t.        t0        f$ r  t2        $ r}
| j4                  rCt7        | j                   |
t9        j:                               j=                  |
j>                        d tA        |
| jB                  jD                  dd| dtG        |
       d| jB                  jI                          d| dtG        |
       ddg       Y d }
~
d }
~
wtJ        $ r}
|
d }
~
wtL        $ rH}
t7        | j                   |
t9        j:                               j=                  |
j>                        d d }
~
ww xY w)Nr   r]  call_methodcall_moduler   r  r@  rp   z<unknown compiler_fn>zcalling compiler function zdone compiler function z#compiler_fn did not return callablezBackend compiler exceptionz	Backend: z
Exception:z
Traceback:
zBackend compiler `z` failed with z. Adding a graph break.z-Report an issue to the backend compiler repo.r9  dynamor  )op_count
node_countinput_count)*r   rg   r  r  r   rR   rE  ro   _dynamo_sourcer  _param_name_to_sourcer  _source_to_user_stacksr  rp   r   r    INFOr   r   r   r   callabler   r   r-   r  r,   inspectcurrentframewith_traceback__traceback__r0   r  r&  r   format_frame_summaryr.   r   r   r   r2  )r~   r   totr  rD  plr  r5  r   r  es              rw   r  zOutputGraph._call_user_compiler  s   +++HHNN 	*DwwIIqww-'##D)		*
 	3 	+B''*%C !$

B		+ $(#<#< $($>$>! t''4 %%( 	
 	8LN7<<+EdV)LM**K((,[9%b$*=*=*?@KLN7<<+B4&)IJK(O*OO(6 	,..!"((..1"<0			
 K /0@A 	0 	55+$$a)=)=)? .1t< *##4#D6c!fX^DLLLmLmLoKpq0nSVHLcdC	 	  	 G 	8'  !W%9%9%;nQ__-48	8s3   %BG K0B/JK0JK0(AK++K0c                 t    t         j                  j                  j                  rt	        |       S t               S r{   )r   _dynamor   use_graph_deduplicationr1   r   r}   s    rw   rH  zOutputGraph.dedup_pass  s'    ==77,T226Mrv   c                 z    t        || j                  d      }||_        d|_        | j	                  ||d        |S )NT)requires_suffixFr?  )rP   r   rp   torchdynamo_force_dynamicr  )r~   r5  sub_gm	next_names       rw   install_subgraphzOutputGraph.install_subgraph  s@    'dootT	#+0( 	$$VYt$Drv   c                 V    | j                   D cg c]  }|j                   }}|S c c}w r{   )r  example)r~   r  r   s      rw   r   zOutputGraph.example_inputs(  s'    )-8##++88 9s   &c                     t        | j                  j                  d      d      D ]5  }t        t	        |j
                              dk(  s%| j                  |       7 y )Nr  r  T)reverser   )sortedrg   r  r2  r   usersr  r  s     rw   r  z(OutputGraph.remove_unused_get_attr_nodes,  sN    4::00J0?N 	'D4

#$)  &	'rv   c                      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(  s%|j                  dk(  s||j                  d	k(  r|j                  t        j                  u sQ|j                  d	k(  r1|j                  t         j"                  u r ||j$                  d         s ||      s	 ||      s j'                  |        d
 } fd}t)               dt*        t         j,                  t         j.                  f   fdg } j0                  D ]  } ||      d u}|r|j                  r|j3                  |       /|j                  s&t5        |j6                  d   t8              s	 ||       a|j6                  d   }	t5        |	t8              rt5        |j6                  d   j:                  t         j<                        r|j6                  d   j:                  }
|j6                  d   j>                  }t         j@                  jB                  jE                  |
      s}tG        |
jI                               }|jK                         D ]Q  }tM        |jN                  |      }tQ        jR                  t         j,                  t         j.                  ffd|       S |	jT                  |	jT                  n|	j:                  } |        |D ]+  } ||      }||vr	 ||       jW                  |       - y )Nb_nodec                     | du ryt        | t        j                        sy| j                  j	                  d      }|y|du ryt        |t
        j                        r|j                  j                         x}|S y)NTFrG  )	r,  r
   NoderE  rb  r   SymBoolrD  maybe_as_bool)r  brs      rw   is_static_truez;OutputGraph.remove_unused_graphargs.<locals>.is_static_true>  sq    ~fbgg.0AyDy1emm,&&..00Q= rv   ac                     ddl m} t        | t        t        t
        f      ryt        | t        j                        r%t        | j                  j                  d      |      S y)Nr   SymTypesTrG  F)
torch.fx.experimental.sym_noder  r,  rs   r3  r   r
   r  rE  rb  )r  r  s     rw   is_symnode_argz;OutputGraph.remove_unused_graphargs.<locals>.is_symnode_argQ  sD    ?!c5$/0!RWW%!!&&**_"=xHHrv   c                    ddl m} | j                  dk7  ryt        | j                  j                  d      |      syt        fd| j                  D              syt        fd| j                  j                         D              syy)	Nr   r  r]  FrG  c              3   .   K   | ]  } |        y wr{   ru   r  r  r  s     rw   r!  zWOutputGraph.remove_unused_graphargs.<locals>.is_symnode_compute_node.<locals>.<genexpr>h  s     <Q~a(<   c              3   .   K   | ]  } |        y wr{   ru   r  s     rw   r!  zWOutputGraph.remove_unused_graphargs.<locals>.is_symnode_compute_node.<locals>.<genexpr>j  s     GQ~a(Gr  T)
r  r  r  r,  rE  rb  r>  r   r   r  )rD  r  r  s     rw   is_symnode_compute_nodezDOutputGraph.remove_unused_graphargs.<locals>.is_symnode_compute_node]  sg    ?ww/)diimmO<hG <$))<<G$++2D2D2FGGrv   r   )is_accessor_noder  r]  c                     | j                   d   }|j                  }t        |t        j                        rDt        |j
                  j                  t        j                        r|j
                  j                  S y r  )	rE  r  r,  r   r  rD  exprsympySymbol)rD  r  r  s      rw   placeholder_binds_symbolzEOutputGraph.remove_unused_graphargs.<locals>.placeholder_binds_symbol  sV    ))J'CkkG'5<<0Z!!5<<6 ||(((rv   c                     t         j                  d| j                  d   j                  j	                                | j                  d= j                  |        j                  j                  | d        y )NzREMOVE UNUSED GRAPHARG %sr@  )r   r=  rE  ro   r5  r  r|  r  )rD  r~   s    rw   remove_unusedz:OutputGraph.remove_unused_graphargs.<locals>.remove_unused  s\    II1499Z3H3O3O3T3T3VW 		*%T"!!%%dD1rv   fakec                      | t        |      z  } y r{   )r   )used_symbolsr)  s     rw   update_used_symbolsz@OutputGraph.remove_unused_graphargs.<locals>.update_used_symbols  s    L..Lrv   r@  c                      |       S r{   ru   )tr,  r+  s    rw   rW  z5OutputGraph.remove_unused_graphargs.<locals>.<lambda>  s    .A,PQ.R rv   ),r  r
   rD  Argument%torch.fx.experimental.symbolic_shapesr!  rk  r   rg   r  r2  r  r  r  operatorgetitemr   _checkr   r  r   r	   r  r   r  r   r,  rE  rY   r  ScriptObjectexample_strong_ref_libraryfake_class_registrytracing_with_realr   __obj_flatten__r  r  wrapped_objpytreetree_map_onlyfake_tensorremove)r~   r  r   r!  rD  r&  r(  recheck_placeholdersbinds_symbolr  real_script_objfake_script_obj	flat_dictattrfake_attr_valr)  symbolr  r,  r+  s   `                @@@rw   r  z#OutputGraph.remove_unused_graphargs1  s    	277#3#3 	&	bgg.. 		" 	KT$**"2"234 	+D4

#$)GGz)?2t{{hFVFV7V?2 KK5<<7*499Q<8.t4'-$$T*	+		2 +.%	/E%,,:T4U 	/  "%% $	<D3D9ELzz(//5zz*IIj)+@+ "$' ))J/C!#'<= !$))J"7"?"?ASAST*.))J*?*G*G*.))J*?*R*R$~~AASS+  )-_-L-L-N(OI(1(8 "07$3$?$?1" !' 4 4%*\\5<<$@$R$1!"	" !+.??+FCKK  (d;I$	<N ) 	0D-d3F!-!$' !''/	0rv   c                    ddl m} | j                  j                  D ]  }|j                  j                  d      }t        |t              s0|j                  =t        |j                  j                  j                  d      sht        d |j                  D              s|j                  |j                  j                  j                  j                        st!        |j                        D ]7  }|j#                  t%        |j                               | j'                  |       9 | j'                  |       ! y )Nr   )TensorifyStaterG  r5  c              3   :   K   | ]  }|j                   d k(    yw)itemN)r  )r  us     rw   r!  zEOutputGraph.remove_tensorify_specialized_graphargs.<locals>.<genexpr>  s     ?qF*?s   )torch._dynamo.symbolic_convertrH  rg   r  rE  rb  r,  r   	item_memor  rD  _exprr>  r  should_specializer5  r   r  r   r  )r~   rH  rD  rG  rK  s        rw   r  z2OutputGraph.remove_tensorify_specialized_graphargs  s     	BJJ$$ 	'D IIMM/:M=*5!++7M3388>>G?DJJ??"44!++0066;;
 djj) (A++L9P9P,QR$$Q'(   &	'rv   r4  c                 H    | j                   j                  |       d| _        y)zt
        We call this on the creation of a new compiled subgraph that is inserted
        before user code.
        TN)r
  r_  r  )r~   r4  s     rw   r*  z#OutputGraph.add_output_instructions  s     
 	  ''/rv   c                     || j                   vsJ | j                   j                  |       | j                  j                  t	        j
                  | j                  ||             y)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   rH   r\  r   )r~   r5  r   s      rw   r  z!OutputGraph.install_global_unsafe  sS     411111""4([//0A0A4OPrv   c                     | dt        |       d| j                   }|| j                  v r|S | j                  ||       |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)r   r   r   r  r~   r4  r   r5  s       rw   install_global_by_idz OutputGraph.install_global_by_id  sK     2e9+R'894)))K""4/rv   c                 @    t        |      }| j                  ||       |S )z~
        Installs a global, generating a unique name for it.

        Returns the name of the newly installed global.
        )r(   r  rT  s       rw   r.  zOutputGraph.install_global  s#      ""4/rv   c                    d | _         | j                  j                          d | _        | j                  j
                  D ]  }d|j                  v s|j                  d=   | j                  j                          | j                  j                          | j                  j                          | j                  j                          | j                  j                          | j                  j                          | j                  j                          | j                  j                          | j                  j                          | j                   j                          y r  )r  r   r   r  rg   r  rE  r|  rz  r   r  r  r  r   r   r  r   r  s     rw   cleanupzOutputGraph.cleanup  s
    $(!JJ$$ 	*DTYY&IIj)	* 	##%  &&(!##))+##))+--335""$  &&(  &&(!!#rv   enabledc                     || _         y r{   )r  )r~   rY  s     rw   r  z$OutputGraph.set_torch_function_state+  s
    &-#rv   r  c                 :    | j                   j                  |       y r{   )r  r   )r~   r  s     rw   add_graph_finalizerzOutputGraph.add_graph_finalizer.  s     	##**+=>rv   rD  c                     |j                   dk(  r|j                  d   j                  S |j                   dk(  sJ | j                  |j                     S )z#Extract the non-fake example tensorr  r@  r  )r  rE  r  r   r  r  s     rw   example_value_from_input_nodez)OutputGraph.example_value_from_input_node3  sH    77m#99Z(000ww*$$$t{{++rv   )r0  r{   )tmpFN)r   N)drp   rq   rr   r   r7   rt   r   r   r   r   r#   r   Scoper   r#  rX   r6  r3  r  rg  r   ri  rl  propertyrA  rs  ru  rg   setterrz  r|  r~  r  r  r  
contextlibcontextmanagerr  r  r(  r   r   r  	GuardsSetrI  r   r  r  r  r  rJ   r  r  rF  r  staticmethodr  r
   Proxyr  r	   r   r   r   r  r  r   rf  rO  rE  r  r  r  rS  rN  r   r  r  rZ   r  r   r"   r  r  rH  r  r   r  r  r  r'   r*  r  rU  r.  rX  r  r\  r^  r   r   s   @rw   r   r     s    t238nt2 j)t2
 t2 t2 t2lD&5O 5)*.0&8BG#4 &#
      & ) ) \\* * 7 7 4 4 1 1A@@  .   . . 8 8 A// A A >DcN > >,
\$& N N'05
  		,/			Hehhoou||S89HTT/n QU\19:L1M\|501<C 2  "70})~ 7d277m 7 7 E4> E E0R^^ 0
 0Fbnn F FPU\\ 2 '
S0j 'D d;.?  D  
QS 	s 	$*. . .?"*BNN+;T+A"B?	?
,%((-- ,rv   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k7  ry  fd} fd}t        |t        j                  j                        r=t        j                  j
                  |j                  v r	 ||       y  ||d| d       y t        |t        j                  j                        r't        |j                               }t        |      dk(  rLt        ||d         }t        j                  j
                  |j                  v r	 ||       y  ||d| d	       y t        j                  j                  j                   j                  ||fd
      \  }}	 t        j                   j"                  |j$                  g|i |}	t        |	      }t        j                  j
                  |j                  v r	 ||       y  ||d| d|	 d       y y # t&        $ r"}
t)        ddt+        |
      g        Y d }
~
sd }
~
ww xY w)Nr]  c                 Z    | j                   dv ry j                  j                  |        y )N>   atenprimprims)	namespacer  r   )r  output_graphs    rw   encountered_compliant_opz8check_pt2_compliant_op.<locals>.encountered_compliant_opH  s)    88))--f5rv   c                     j                   j                  |        t        j                  rt	        dd|dz   t
        z   g        y y )Nz Encountered non-PT2-compliant opr8   r9  )r  r   r   only_allow_pt2_compliant_opsr/   err_epilogue)r  msgro  s     rw   encountered_non_compliant_opz<check_pt2_compliant_op.<locals>.encountered_non_compliant_opM  sB    &&**62..:#I4	 /rv   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*Error when attempting to resolve op packetr8  r9  z+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  	overloadsr2  r  r  utilsget_fake_values_from_nodesr  r  _jit_resolve_packet_qualified_op_namer   r/   r   )ro  kindr  r   r   rp  rv  r}  r  overloadr  s   `          rw   check_pt2_compliant_opr  D  s   6
 &%**//099&&&++5$V,$3F8;VW	
 	&%**556&**,-	 y>Q1.Byy**bgg5(,(..4X 6./ }}**EE##dF^U
f
	xx33)),04:H VX&99&&"''1$R((=fX F33;* =%&I 70  	DF	 	s   -G   	G+	G&&G+c                       e Zd Zd Zd Zy)	LazyProxyc                 <    || _         || _        || _        || _        y r{   )r  r   r   r   )r~   r  r   r   r   s        rw   r   zLazyProxy.__init__  s    	rv   c                 N     | j                   | j                  i | j                  S r{   )r   r   r   r}   s    rw   r   zLazyProxy.__call__  s     tww		1T[[11rv   N)rp   rq   rr   r   r   ru   rv   rw   r  r    s    2rv   r  c                   *    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d
eeej                  j                   f   fdZdeej$                  ej&                  f   dee   fdZdej$                  deej2                     f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.
    c                 :   t         |           t        j                  |      | _        t
        j                  j                         | _        || _	        i | _
        i | _        || _        || _        i | _        i | _        d | _        d| _        d| _        d| _        ||j(                  dz   nd| _        d | _        d | _        d | _        d | _        | j                  g | _        y | j                  j2                  | j                  j5                  |      |fgz   | _        y )NFr   r   )r   r   weakrefr  ro  r   r
   Graphrg   r   rz  r|  r  r  lifted_freevarsr~  	prev_instunder_activation_checkpoint#allow_side_effects_under_checkpointis_reconstructing_generatordebug_level	_cur_code_orig_gm_meta_orig_gm_lineno_map_orig_gm_firstlinenosource_fn_stack_target_to_str)r~   ro  r  r   r  r   s        rw   r   zSubgraphTracer.__init__  s   #MM,7XX^^%
 # 9; =? *  " TV ,1(
 490 ,1(:@:L 2 2Q 6RS!#' $(!
 ;;#%D #';;#>#>**=9=IB $D rv   c                    | j                   r| j                  r| j                  r|j                  j                  }d }|)| j                  j                  || j                  z
  d       }|]| j                   |   }t        j                  j                  D ]  }||v s||   |j                  |<    d|v r|d   |j                  d<   y y y y y y )Nstack_trace)
r  r  r  current_instructionstarts_linerb  r
   r  _COPY_META_FIELDSrE  )r~   r  rD  linenonode_idxrE  fields          rw   r  z,SubgraphTracer._maybe_preserve_original_meta  s    (())++77FH!3377T666 #))(3XX77 7E}+/;		%(7 !D(/3M/BDIIm, ) $ * ) rv   c           	         | j                   ^t        j                  ||f      \  }}	g }
|D ]$  }| j                  |      }|
j	                  |       & t        j
                  |
|	      \  }}t        |   ||||||      | j                  j                  }t        j                  dk\  r|dv r|j                  | j                  urj                  vj                  j                  `|j                   |j#                  j                  j                        fd}t$        j'                  dt)        |             | _        d}|j                   | j*                  ur t-        j.                  |j                         j1                  dd              }t3        |t4        j6                  j8                        rld	}|j:                  j<                  D cg c]  }|j>                   c}| _         |jB                  | _"        |jF                  jH                  jJ                  | _&        nd | _         d | _"        d | _&        |jN                  }|r'|jQ                         jR                  j>                  d
<   |dv r>| jT                  jR                  jV                  fgz   jR                  j>                  d<   n|dk(  r| j                   tY        dd| jZ                   dg        | jT                  jR                  jV                  t]        fdjR                  j>                  d
   j_                         D              fgz   jR                  j>                  d<   | ja                  |jR                         |s!d
jR                  j>                  vr5|jN                  }|r'|jQ                         jR                  j>                  d
<   djR                  j>                  vr|dv r>| jT                  jR                  jV                  fgz   jR                  j>                  d<   nz|dk(  ru| j                   tY        dddg        | jT                  jR                  jV                  jR                  j>                  d
      d   fgz   jR                  j>                  d<   djR                  j>                  vrg }|r?|jc                         s|j	                  |je                                tg        |dd       }|r?|ji                          tj        jl                  jo                  |      jq                         }djs                  |      jR                  _:        t4        jv                  jx                  jz                  s$t4        jv                  jx                  j|                  rD| j                  j~                  j                  | j                  j                  jR                         S c c}w )Nr#  r  )r  c                  v    t              j                         } dj                  j                   d d|  S )NzTRACE FX call z from r  )rM   rstriprD  r5  )linecur_instheaderr  tx_codes    rw   get_trace_call_log_strz;SubgraphTracer.create_proxy.<locals>.get_trace_call_log_strF  s9    5gxHOOQD+BGGLL>xr$PPrv   r  Forig_graphmodulec                       y r{   ru   ru   rv   rw   rW  z-SubgraphTracer.create_proxy.<locals>.<lambda>Q  s    rv   Tnn_module_stack>   r  r]  r  r  z4Invoking an nn.Module inside a higher order operatorzHigher order op name: zThis is not supported.r9  c              3   \   K   | ]#  \  }\  }}|j                  d       d   k(  r| % yw)@r   N)r  )r  r   r  tyr  s       rw   r!  z.SubgraphTracer.create_proxy.<locals>.<genexpr>u  s5      &Aw2773<?f4 s   ),z2Invoking an nn.Module inside a HigherOrderOperatorr8  r   r  r  )Ar  r;  tree_flatten#maybe_lift_tracked_freevar_to_inputr   tree_unflattenr   r  ro  r  rA  rB  r  r  	positionsr  r&  get_line_of_code_headertrace_call_logr=  rT   r  r)   get_contextrb  r,  r   r
   r   rg   r  rE  r  _lineno_mapr  r   __code__r   r  r  r   rD  r  r5  r/   r  r   r   r  is_co_filename_from_nn_modulesframe_summaryr  r	  r   StackSummary	from_listformatr  r  r  r   r  track_nodes_for_deduplicationr   
track_node)r~   r  r  r   r   r5  	type_exprproxy_factory_fn	flat_args	tree_specnew_flat_argsr  maybe_new_argr  r  is_retracingorig_graphmodule_maybendr  frame_summariesmsgsr  r  r  r  r   s     `                  @@@@rw   r  zSubgraphTracer.create_proxy  s'   V ;;"#)#6#6f~#F IyM  4 $ H H M$$]34 "00	JLD&W!&$i9I

 )) w&4 4
 ,

 --H.&&2&&--9))338;M;M;T;T3UQ $$T:6L+MN!) 99DNN*&\%=%=bii%H%L%L"L& &" 0%((2F2FG#&<&B&B&H&H& "BGG&" ,B+M+M(*22;;JJ ) &*"+/(,0),,.=.B.B.DBGGLL*+33.2.B.Bv&F /BGGLL*+ ]"{{& R4T5G5G4HI 8	 /3.B.BGGLL *,'',,7H*I*O*O*Q 	F 	/BGGLL*+ 	**2rww7 4"$"4"4"6E6J6J6LBGGLL!23 4;;6:6J6Jv.N 7BGGLL!23 ]*{{.($X$&(@"$	 7;6J6JGGLLGGLL):;FCAFN 7BGGLL!23 ,<>O 88:#**2+;+;+=>R40  ##% ))33ODKKMD"$''$-BGG MM  88}}##AA,,77!!,,bgg 	y&s   W!c                    t        | j                  ||||       | j                  at        j                  |i |}|D ]G  }t        |t        j                  j                        s(|j                  | j                  k(  rBJ d        t        
| -  ||||||      }	| j                  j                  |	j                  d<   |	S )Nz2create_node using arg not from this SubgraphTracerrt  )r  ro  r  r;  arg_tree_leavesr,  r   r
   r  rg   r   r  r  rE  )r~   r  r  r   r   r5  r  r  r  rD  r   s             rw   r  zSubgraphTracer.create_node  s     	t00"fdFK;;"..??I  !#uxx}}5yyDJJ. H. w"2vtVT9M*.*;*;*E*E		&'rv   c                    t        |j                        dkD  rg }|j                  j                         D ]S  }|j                  | j                  k7  s|j	                  t        t        |j                  j                                     U |D ]  }|j                  j                  |        | j                  j                  |       | j                  j                  |j                  d        y rn  )r2  r  r  rg   r_  rk  r   r  rx  rz  r  r5  )r~   rD  user_graph_nodesuserother_graph_nodes        rw   r  zSubgraphTracer.remove_node  s    tzz?Q46

) N ::+ %++HT$**:J:J5K,LMN %5 D  &&112BCD

d#  $$TYY5rv   c                    t         j                  d|||j                         nd|| j                  |       || j                  J d| d| d       | j
                  r`| j                  Tt        |d      sG| j                  j                  j                  |g       j                  t        j                                t        || j                        }| j                  rqt        t!        | j                              }| j                  |   j"                  }|r| j$                  j'                  |      }n7| j$                  j)                  |      }n| j$                  j'                  d       }|5  | j+                  d|d	i |
      }	t-        |	j"                  |       | j                  r>|r<| j                  j/                         \  }
}|	| j                  |<   || j                  |
<   n|	| j                  |<   | j
                  }t0        j2                  j5                         }|s.|s,t7        |t0        j8                        r| j;                  ||       t7        |t0        j<                        rQt7        |j"                  j>                  t@        jB                        r#|	| jD                  |j"                  j>                  <   |	cd d d        S # 1 sw Y   y xY w)Nz7create_graph_input %s %s %s at debug_level %s before=%sz(none)z0you are required to provide a source for inputs z example_val z on the root tracerT)only_allow_inputr  ru   r  )#r   r=  r5  r  r  r   r>   ro  r  
setdefaultr   r   extract_stackrP   rz  r   rk  rD  rg   inserting_beforeinserting_afterr  rW   popitemr   compileris_compilingr,  r   _lift_basic_symbolsr  r#  r$  r%  r~  )r~   r5  r  rG  beforero   	prev_namerD  ctxr  r   r   is_strict_exportis_non_strict_exports                 rw   rB  z!SubgraphTracer.create_graph_input  sd    			E#/FKKMX	
 >;;* B4&VcUddwx* >>dkk1'F!!77BB62NUU"002 #4)A)AB##Xd&>&>?@I++I6;;Djj11$7jj006**--d3C /	%%mT2rY%WEejj-8''F//779116((../((+16((.2  $~~#(>>#>#>#@ $,}ell;((? -6:""''< ?D""=#5#5#:#:;_/	 /	 /	s   /EKKc                 n   | j                   J d       |j                  j                  d   }t        |t        j
                        rE|j                  j                  | j                  v r#| j                  |j                  j                     S || j                  v r| j                  |   S |j                  | j                   k7  r| j                   j                  |       |j                  j                  d   }| j                  |j                  j                  t        |      |      }|| j                  |<   |S )NzIlift_tracked_freevar_to_input should not be called on root SubgraphTracerrG  )r  rD  rE  r,  r   r  r#  r~  r  r  lift_tracked_freevar_to_inputrB  r5  rC  )r~   r  rG  	new_proxys       rw   r  z,SubgraphTracer.lift_tracked_freevar_to_input7	  s    {{& 	
W	
& 

8 }ell3""''4+=+==%%m&8&8&=&=>>
 D(((''..
 <<4;;&KK55e<

8++JJOOT-0-
	 '0U#rv   c                     t        |t        j                  j                        sDt        |t              r2t	         fd|j
                  |j                  |j                  fD         S |S |j                   k(  r|S  j                  |      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.
        c              3   @   K   | ]  }j                  |        y wr{   )r  )r  sub_argr~   s     rw   r!  zESubgraphTracer.maybe_lift_tracked_freevar_to_input.<locals>.<genexpr>l	  s$      # @@Is   )
r,  r   r
   rh  slicestartstopstepr  r  )r~   r  s   ` rw   r  z2SubgraphTracer.maybe_lift_tracked_freevar_to_input_	  sy     #uxx~~.
 #u%(+		388SXX'F  
ZZ4J11#66rv   e_proxyc                     |j                   t        t              sJ dt        f fd}fd}t        |t        j
                        rt        |j                               D ]  \  }} ||      st        j                  d|||j                         t        ||dt        j                  j                  j                  j                  ||fi t!        |            } j#                  ||        |j$                  t        j&                  u rt        |j)                               D ]  \  }} ||      st        j                  d|||j                         t        ||dt        j                  j                  j*                  j                  ||fi t!        |            } j#                  ||        n6|j$                  t        j,                  u rA j#                  |j/                         |        j#                  |j1                         |       n|j$                  t        j2                  t        j4                  hv rA j#                  |j7                         |        j#                  |j9                         |       nl|j$                  t        j:                  t        j<                  hv r@ j#                  |j?                         |        j#                  |jA                         |       tC        |      rC|jE                         \  }}	|D ]*  }
tG        ||
      } j#                  |tG        ||
             , y y t        |t        jH                        r/ ||      r&|jJ                  jL                  }|jN                  |<   y y y )Nr   c                 >   ddl m}  ||       xr t        | j                  j                  t
        j                        xr] | j                  j                  j                  | j                  j                        xr" | j                  j                  j                  vS )Nr   )r   )
r0  r   r,  rD  r#  r$  r%  r   is_unbacked_symintr~  )r  r   r~   s     rw   	need_bindz8SubgraphTracer.track_unbacked_symbols.<locals>.need_bind	  sq    I A :qvv{{ELL9:FF$$77D: FFKKt'9'99	rv   c                 X     j                   |i |}t        |j                  |        |S r{   )r  rW   rD  )rG  r   r   r  r  s       rw   _proxy_with_example_valuezHSubgraphTracer.track_unbacked_symbols.<locals>._proxy_with_example_value	  s-    'F''88Eejj-8Lrv   z>_track_unbacked_symbols %s for %s.size()[%s] at debug_level %sr]  r  z@_track_unbacked_symbols %s for %s.stride()[%s] at debug_level %s)(r  r,  r   r   r   r   	enumerater|  r   r=  r  r  opsrk  sym_sizers   rC  track_unbacked_symbolslayoutstridedstride
sym_stride
sparse_coo_indices_values
sparse_csr
sparse_bsrcrow_indicescol_indices
sparse_csc
sparse_bscccol_indicesrow_indicesr   __tensor_flatten__r  r  rD  r#  r~  )r~   rG  r  r  r  ir  
lazy_proxyattrsr  rD  inner_tr#  r  s   `            @rw   r  z%SubgraphTracer.track_unbacked_symbols~	  s     &.111	D 		
 mU\\2!-"4"4"67 ?1Q<IIX** "+1'		//33 !"&q'	"J //:>'?* ##u}}4%m&:&:&<= CDAq |		^#".. &/"5+!IINN5599$aL&*1g	&
 33AzB'C* %%)9)99++M,B,B,DgN++M,A,A,CWM%%%*:*:E<L<L)MM++M,F,F,H'R++M,E,E,GQ%%%*:*:E<L<L)MM++M,F,F,H'R++M,E,E,GQ,];*==?
s! QD%mT:G//$9OPQ <
 u||4'$))..-4$$T* ( 5rv   rG  srcc           	          	 ddt         t        t        j                  f   dt        t
           dt        dd f fd}t        |t        j                        rnt        |j                               D ]-  \  }} |||t        |t        j                  |      nd d       / |j                  t        j                  u rt        |j!                               D ]-  \  }} |||t        |t        j"                  |      nd d       /  ||j%                         |t        |t        j&                        nd d       n6|j                  t        j(                  u rA j+                  |j-                         |        j+                  |j/                         |       n|j                  t        j0                  t        j2                  hv rA j+                  |j5                         |        j+                  |j7                         |       nl|j                  t        j8                  t        j:                  hv r@ j+                  |j=                         |        j+                  |j?                         |       tA        |      rG|jC                         \  }}|D ].  }tE        ||      }	 j+                  |	|tG        ||      nd        0 y y t        |t        j                        r
 |||       y y )	Nr  ro   r  r   c                    t        |       sy t        | t        j                        sJ j	                  |       }t        |      dk(  ry j                  ݉j                  j                  | |       |D ]  }j                  j                  |   }|j                  j                  d   }t        |t        j                        sJ j                  t        |      t        |      |||      }t        j                  d|||j!                         ndj"                         |j$                  |<    y t        |      dk(  sJ d| d|         |J d	|  d
|  d       t'        t)        |            }j                  t        |      t        |       | ||      }t        j                  d| ||j!                         ndj"                         t+        || dd d      |j                  j                  d<   y )Nr   rG  )r  ro   z4_lift_symbols_in_symint %s from %s at debug_level %szsubgraph inputsr   zyFor root tracer, we only expect to bind basic symbols (compound symbols should be cached before) but got unbound symbols z in zSource of 'z' is None when lifting it to input of top-level. If it's an unbacked symbol, this could be because it's not tracked with lazy_bind_unbacked_symbols. Otherwise, should provide a source when create_graph_input for `z` at root tracer.F)pass_arg_as_tensorr=  	is_tensorr@  )r   r,  r   r  lookup_unbound_symbolsr2  r  r  r~  rD  rE  rB  r   rC  r   r=  r5  r  r  r   iterrZ   )	r  ro   r  self_to_be_bounds0parent_proxyexample_valphr~   s	           rw   _lift_symbols_in_symintzCSubgraphTracer._lift_basic_symbols.<locals>._lift_symbols_in_symint	  s   
 q>a...#::1=#$) {{&//6:* <B#';;#<#<R#@L"."3"3"8"8"IK%k5<<@@@00B[)#%% 1 B IIN)/);AR((	 :<D((6#<( +,1 HHXGYY]^_]`b1 ) !! %WWXVYYjl)
 $/01,,GG!! -  		J%+%7FKKM=N$$	 ,4', $#,Z(rv   T)r  )F)$r	   rs   r   r  r   r   r   r,  r   r  r|  rE   rD   SIZEr  r  r  STRIDEstorage_offsetSTORAGE_OFFSETr  r  r  r  r  r  r  r  r   r  r  r  r   r  r  r8   )
r~   rG  r	  r  r  r  r  r  rD  r  s
   `         rw   r  z"SubgraphTracer._lift_basic_symbols	  s    !B	S%,,&'B	V$B	 B	 	B	H mU\\2!-"4"4"67 	1' ? -S.2E2EqI!	 ##u}}4%m&:&:&<= 	DAq+  # 1n6K6KQO!%#	 (!002 ? -S.2O2OP! %%)9)99(()?)?)A3G(()>)>)@#F%%%*:*:E<L<L)MM(()C)C)EsK(()B)B)DcJ%%%*:*:E<L<L)MM(()C)C)EsK(()B)B)DcJ,];*==?
s! D%mT:G,,#/C!6t < u||4# 5rv   r  r   c                    |j                   j                  j                  }t        |      dk(  rg S g }|D ]  }|| j                  vr|j                  |       #| j                  |   }t        |t              r |       }|| j                  |<   t        |t        j                  j                        r|j                  | u rJ d| d        t        |d       S )Nr   zThe proxy of symbol z" doesn't belong to current tracer.c                     | j                   S r{   )r5  )r  s    rw   rW  z7SubgraphTracer.lookup_unbound_symbols.<locals>.<lambda>v
  s
     rv   )r   )rD  r#  r   r2  r~  r   r,  r  r   r
   rh  r  r
  )r~   r  r   to_be_boundr  r  s         rw   r  z%SubgraphTracer.lookup_unbound_symbolsc
  s    vv{{//|!I 	B+++""2&&&r*E%+).""2&eUXX^^49M &rd*LMM	 k'788rv   )NFN)NNN)NNNNr`  )rp   rq   rr   r   r   r  r  r  r  rB  r  r  r	   r  r   r
   rh  r  r  r   r   r   r  r   r$  r%  r  r   r   s   @rw   r   r     s    BJC4 up HL&6. DHXv&P7>`5&+Iuxx~~,E&F`5F~"5<<#=>~EMfEU~D9 9ell9K 9rv   r   )r   r  rd  r   r  r  r  r    r1  r  rA  r   r  dataclassesr   typingr   r   r   r   r   r	   r$  torch._guardsr   torch._loggingtorch.distributeddistributedr  torch.nntorch.utils._pytreer~  _pytreer;  r
   torch._dynamo.excr   r   r   r   r   r   r   torch._subclasses.fake_tensorr   torch._utils_internalr   r  r   %torch.fx.experimental._backward_stater   r0  r   r   r   r   torch.fx.passes.runtime_assertr   torch.utils._python_dispatchr   r8  r   r   r   r   r!   backends.registryr"   r#   bytecode_transformationr$   r%   r&   r'   r(   r)   r`  r*   current_scope_idr+   r,   r-   r.   r/   r0   graph_deduplicationr1   graph_region_trackerr2   rI  r3   r4   mutation_guardr5   r   r6   r7   ro   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   variables.baserX   variables.builderrY   rZ   r[   r\   variables.listsr]   variables.miscr^   r_   variables.nn_moduler`   variables.tensorra   rb   rc   rd   variables.torch_functionre   rL  rf   	getLoggerrp   r   r  getArtifactLoggergraph_tabular_logr  graph_sizes_logr  rm   ry   	lru_cacher   r   r   r   r   r   r   r   r   objectra  r   rt  r  r  r   r  Tracerr   ru   rv   rw   <module>r@     sf  *         	 
   ! F F       $ $  N  5 0 : ?  K F W W 5  '  -  ; 4 / 0 @        ( ,  . 6 1  C H g!NN44XwG 11(LI..228]K11(LI $   0 T4 4 - - -	%UXX__ 	%   F 	S&[l, l,`1J FR &ioo' 2 2[9RYY [9rv   