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

3rn   c                 d    t               }|j                  j                  | j                         |S rs   )rq   rt   update)rv   	new_caches     ro   clonezVariableTrackerCache.clone   s&    (*	tzz*rn   c                 8    | j                   j                          y rs   )rt   clearru   s    ro   r   zVariableTrackerCache.clear   s    

rn   N)rh   ri   rj   rw   r}   r   r   r   rm   rn   ro   rq   rq      s    rn   rq   c                  4    t        j                  t              S rs   )torchdynamo_loggingget_step_loggerlogrm   rn   ro   _step_loggerr      s    ..s33rn   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 rs   )r   rI   appendru   s    ro   __post_init__z GraphCompileReason.__post_init__   s    &&t, rn   N)rh   ri   rj   __doc__strrl   r   	tracebackFrameSummaryr   boolr   rm   rn   ro   r   r      s,    YKY++,, K-rn   r   c                       fd}|S )Nc            
      N    D  cg c]  \  } }} | |i | c}}} S c c}}} w rs   rm   )fnargskwargsrandom_callss      ro   _gen_rand_valuesz1_get_gen_rand_values_fn.<locals>._gen_rand_values   s+    <HII(8D&D#F#IIIs    rm   )r   r   s   ` ro   _get_gen_rand_values_fnr      s    J rn   c                   `     e Zd ZdZdeeej                  j                  f   f fdZ	d Z
 xZS )FakeRootModulez'Trick the constructor of fx.GraphModule
nn_modulesc                 l    t         |           |j                         D ]  \  }}t        | ||        y rs   )superrw   itemssetattr)rv   r   kv	__class__s       ro   rw   zFakeRootModule.__init__   s6    $$& 	 DAqD!Q	 rn   c                      y)NzFakeRootModule(...)rm   ru   s    ro   __repr__zFakeRootModule.__repr__   s    $rn   )rh   ri   rj   r   r   r   torchnnModulerw   r   __classcell__r   s   @ro   r   r      s)    1 4UXX__(<#=  
%rn   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 rs   )r   )rv   r   s     ro   rw   zWrapperBackend.__init__   s	    #*rn   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)r?   restorer   copydeepcopyr   	candidateforwardr   verify_correctnessrA   rN   RuntimeError	Exceptionr   	exception)rv   r   r   copy_gmcorrectresults         ro   __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)rh   ri   rj   r   rw   r   r   GraphModuler   Tensorr   rm   rn   ro   r   r      s4    +
 +588// ellAS rn   r   c            
       Z    e Zd ZdZdeeef   dee   de	de
de
f
 fdZd ZdOd	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j4                  d        Zed        Zed        Zd Zd Zd Ze jB                  d        Z"ed        Z#ed        Z$ed        Z%ede&jN                  jP                  fd        Z)edeeef   fd!       Z*dPd#Z+d$ Z,d% Z-ed&        Z.d'e/fd(Z0d) Z1d* Z2d+ Z3dQd,Z4d- Z5e6d.        Z7d/e8e&jr                  jt                  e&jv                  ef   fd0Z<d1 Z=	 dRd2ee>   fd3Z?d4 Z@d5 ZAd6 ZBd7efd8ZCe jB                  d9        ZDd: ZEd; ZFedeGeHj                     fd<       ZJedeGe/   fd=       ZKd>eHj                  deMfd?ZNd>eHj                  deMfd@ZOdeGe&jv                     fdAZPdSdBZQdCeGeR   dd"fdDZSdSdEZTdefdFZUdefdGZVdSdHZWdIe	dd"fdJZXdKeeHj                  gd"f   dd"fdLZYdMe&j                  j                  fdNZZ xZ[S )TOutputGrapha  
    Wrapper class to hold outputs of InstructionTranslator.  Mainly the
    generated fx.Graph.

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

3y1188$?@
TE*
%%b&9&9&;<%g.6v6}E++99SS	
 rn   r   c                 :    | j                   j                  |       y rs   )r   r   )rv   r   s     ro   add_cleanup_hookzOutputGraph.add_cleanup_hook  s    !!"%rn   c                 z    t        | j                        D ]	  } |         | j                  j                          y rs   )reversedr   r   )rv   r%  s     ro   call_cleanup_hookszOutputGraph.call_cleanup_hooks  s3    T//0 	DF	  "rn   c                      | j                   d   S Nr   r   ru   s    ro   r/  zOutputGraph.root_tracer  s    ||Arn   c                      | j                   d   S Nr[  ru   s    ro   current_tracerzOutputGraph.current_tracer  s    ||Brn   c                 2    t        | j                        dk(  S )Nr   )r'  r   ru   s    ro   is_root_tracerzOutputGraph.is_root_tracer  s    4<< A%%rn   c                 .    | j                   j                  S rs   r_  r_   ru   s    ro   r_   zOutputGraph.graph  s    ""(((rn   c                 &    || j                   _        y rs   rc  )rv   r{   s     ro   r_   zOutputGraph.graph  s    $)!rn   c                 .    | j                   j                  S rs   )r_  input_name_to_proxyru   s    ro   rf  zOutputGraph.input_name_to_proxy  s    ""666rn   c                 .    | j                   j                  S rs   )r_  real_value_cacheru   s    ro   rh  zOutputGraph.real_value_cache  s    ""333rn   c                 :     | j                   j                  |i |S rs   )r_  create_proxyrv   r   r   s      ro   rj  zOutputGraph.create_proxy&  s     /t""//@@@rn   c                 :     | j                   j                  |i |S rs   )r_  create_noderk  s      ro   rm  zOutputGraph.create_node)       .t""..???rn   c                 :     | j                   j                  |i |S rs   )r_  remove_noderk  s      ro   rp  zOutputGraph.remove_node,  rn  rn   c              #     K   t               }	 |r|j                  | j                  u sJ |j                          |r|nt	        | | j                  |      }| j
                  j                  |       | |j                  d d d        | j
                  j                          y # |j                  d d d        | j
                  j                          w xY ww)N)parentsource_target)	r&   rr  r_  	__enter__r   r   r   __exit__pop)rv   rs  prior_tracernew_scope_ctxtracers        ro   	subtracerzOutputGraph.subtracer/  s     ')	#**d.A.AAAA##%   #!4!4M  LL'L""4t4LL ""4t4LLs   CA'B# 5.C#/CCc                     | S rs   rm   ru   s    ro   outputzOutputGraph.outputD  s    rn   c                 .    | j                   j                  S rs   )r   r  ru   s    ro   r  zOutputGraph.fake_modeH  s    ##---rn   c                 B    | j                   j                  j                  S rs   )r   r  r   ru   s    ro   r   zOutputGraph.shape_envL  s    ##--777rn   returnc                 B    | j                   j                  j                  S rs   )r   rN  rO  ru   s    ro   r5  zOutputGraph.guardsP  s    ##22@@@rn   c                 B    | j                   j                  j                  S rs   )r   module_contextr   ru   s    ro   r   zOutputGraph.nn_modulesT  s    ##22===rn   Nc                    ||n| j                   j                  j                  }| j                  | j                  f|d<   t
        j                  t        j                         f|d<   t        j                  t
        j                  d      t        j                  d      f|d<   t        j                  t
        j                  d      t        j                  d      f|d<   t        j                  t
        j                  d      t        j                  d      f|d<   t        j                  t
        j                  d      t        j                  d      f|d	<   t
        j                  t        j                         f|d
<   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   global_contextglobal_stateset_torch_function_stater
  r   set_grad_enabledis_grad_enabled	functoolspartialset_autocast_enabledis_autocast_enabledset_autocast_dtypeget_autocast_dtypeset_autocast_cache_enabledis_autocast_cache_enabled)rv   outr  s      ro   r  zOutputGraph.save_global_stateX  sU   
 ?C(<(<(K(K(X(X 	 ))''2
-. ).(>(>@U@U@W'X^$ e88&A%%f-,
'(
 e88%@%%e,0
+,
 e66?$$V,.
)*
 e66>$$U+.
)*
 ,,++-2
-.rn   c                 :    | j                   j                  |       y rs   )r  r   )rv   txs     ro   push_txzOutputGraph.push_tx  s    #rn   c                 6    | j                   j                         S rs   )r  rv  ru   s    ro   pop_txzOutputGraph.pop_tx  s    ##%%rn   c                 P    | j                   s| j                  S | j                   d   S r]  )r  r  ru   s    ro   
current_txzOutputGraph.current_tx  s$    #'#3#3t||M9I9I"9MMrn   argc                       j                   ry |j                  J  fdfd |j                  |j                         y )Nc                 *   t        |       r.t        | j                  j                  t        j
                        sy | j                  j                  }|j                  v ry j                  j                  |       t        j                  d| |j                                j                  j                  t        |      t        j                  d|      }t!        |j                  |        t#        || dd d      |j                  j$                  d<   y )Nzbind_symint %s %sT)beforerg   F)pass_arg_as_tensorfake_tensor	is_tensorr.  )r   r!  r1  exprsympySymbolr   r   r   debugr*  r/  r0  r   r   SymIntrO   rR   r2  )sprops0proxyrv   s       ro   bind_symintz4OutputGraph.add_symbol_bindings.<locals>.bind_symint  s    Nz!&&++u||'LBT'''""2&II)1diik: $$77B	 8 E ejj!,*2#( +EJJOOJ'rn   c           	      6   t        | j                               D ]'  \  }} |t        |t        j                  |             ) | j
                  t        j                  u rtt        | j                               D ]'  \  }} |t        |t        j                  |             )  | j                         t        |t        j                               n | j
                  t        j                  u r/ 	| j                         |        	| j                         |       n| j
                  t        j                  t        j                   hv r/ 	| j#                         |        	| j%                         |       nZ| j
                  t        j&                  t        j(                  hv r. 	| j+                         |        	| j-                         |       t/        |       r:| j1                         \  }}|D ]!  }t3        | |      } 	|t5        ||             # y y rs   )	enumeratesizer=   r<   SIZElayoutr   stridedstrideSTRIDEstorage_offsetSTORAGE_OFFSET
sparse_coo_indices_values
sparse_csr
sparse_bsrcrow_indicescol_indices
sparse_csc
sparse_bscccol_indicesrow_indicesr   __tensor_flatten__getattrr1   )
tsrcir  attrsctxattrinner_tr  handle_tensors
           ro   r  z6OutputGraph.add_symbol_bindings.<locals>.handle_tensor  s   !!&&(+ R1A3C9L9LaPQRxx5==(%ahhj1 XDAq#7^=R=RTU#VWX$$&(n.K.KL U---ajjlC0aiik3/e..0@0@AAann.4ammos3e..0@0@AAann.4ammos3,Q/113
s! BD%a.G!':c4+@AB 0rn   )r   r  rg   )rv   r  r  r  s   ` @@ro   add_symbol_bindingszOutputGraph.add_symbol_bindings  s>     ;;***	2	B2 	cooszz2rn   c                 ,    t        | j                        S rs   )rB   r_   ru   s    ro   rB   zOutputGraph.count_calls  s    4::&&rn   c                 X    t        t        | j                  j                              dk(  S rZ  )r'  r   r_   nodesru   s    ro   is_empty_graphzOutputGraph.is_empty_graph  s!    4

(()*a//rn   c                     |sJ | j                   }|j                  d      D ]$  }t        |t              r||   }t	        ||      }& |S N.)r   splitr!  r   r  )rv   keysobjr   s       ro   get_submodulezOutputGraph.get_submodule  sN    tBF//C 	&A#t$!fc1o		&
 
rn   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   )rv   r*  existingvars       ro   r3  zOutputGraph.new_var  s`    t((78F!D!3!3456C("!!-0SF:0
	 rn   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   )rv   r*  s     ro   update_co_nameszOutputGraph.update_co_names  s2    t((44j)dW4) 5rn   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)joinrF  r   rer  isalpha)namesr*  s     ro   module_key_namezOutputGraph.module_key_name  sm     xxC(vv,eT:vvlIt4vvosD147??,4<Drn   targetc                     t         j                  j                        r t         j                  fi       S t              dv sJ d   t        t              rJ t        t        j                        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  t/        j0                  | }t3        j4                         D ]  } j*                  vr j*                  <   t        t        j                  j                        r_ fd}	t7        d      r j9                         D ]  \  }
} |	|
        t7        d	      r j;                         D ]  \  }
} |	|
         |      c S | d
|  t=        d      )Nrg   c           	         j                   J j                   | <   j                  j                  j                  v r#j                  j                  j                     S t	              dk(  r)t        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guardedget_attrrm   rQ  tensor_dict)r   r  r|  r   rG   r-   r6  r,   ID_MATCHr6   TENSOR_MATCHrU   rj  track_object_existingr  r1  r2  r>   )
module_keyr   optionsrv   rg   r  ry  s     ro   	wrap_namez6OutputGraph.register_attr_or_module.<locals>.wrap_name  s=   00<<<8>))*5 T\\00===<<..;;FCC*62i?!&"3"3L4I4I"JK+F3!&"3"3L4M4M"NO"LL''
JBG #) 	 \\((55KKFTVW$BHHMM,>,>>>>4H4P""=1	rn   c                 2    t        t              | fi S rs   )rX   typer  r  r  s    ro   r  z6OutputGraph.register_attr_or_module.<locals>.wrap_name=  s    +DL*fXPWXXrn   c                 0    t        j                  fi S rs   )r   UnspecializedNNModuleVariabler  s    ro   r  z6OutputGraph.register_attr_or_module.<locals>.wrap_nameE  s    $BB6UWUUrn   c           	      Z    t        j                  j                  d| di       fdiS )Nr  rm   sym_num)rZ   rH  rj  )r  r  rv   r  s    ro   r  z6OutputGraph.register_attr_or_module.<locals>.wrap_nameP  sA    &--%%j*b"E # 	 rn   c                     j                   j                  |        j                  | <    t        t	        |                   S )N)source_name)r|  r  r   rT   r3   )r  rv   r  s    ro   r  z6OutputGraph.register_attr_or_module.<locals>.wrap_name[  sC    ++J706!!*-Tt^
-ST rn   c                     j                   J t        |       } d|  }|j                   |<   t        t              r1| j                  t
        j                  |j                               <   y y r  )r   r9   r!  r8   r  r   r  r*  )	leaf_name
new_sourcenew_namer*  rv   rg   s      ro   register_leaf_namez?OutputGraph.register_attr_or_module.<locals>.register_leaf_nameo  sz    #88DDD%6vy%I
&*V1YK#8>H11(;%fk: !* !AA + ; ;JOO<M N ;rn   _parameters_buffersr  unreachable)r.   r  r   rT   r  r   r!  r9   r   r   r_  ra  r/  r   r   r-   r6  r,   	NN_MODULEr  SymFloatr   r   r   r  r   r   hasattrnamed_parametersnamed_buffersAssertionError)rv   r  r  r  r  r   r   baser  r  r  r  r*  rg   ry  s   `` `        @@@ro   register_attr_or_modulez#OutputGraph.register_attr_or_module  s2     (;(;< ??4??>g>vFF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" 	!A4??*(.%fehhoo6* v}5,2,C,C,E :LIq.y9:vz2,2,@,@,B :LIq.y9: !&V1QC=D3	!6 ]++rn   c                 <   | j                   j                  d      }t        |      }|sg S g }i }g |j                  |j                  j                         | j                  j                  j                         }|r|j                         }t        |t              r,t        |j                  t              sJ ||j                  z  }Ot        |t        t        f      r`t        |j                   t"              rFt        |j                   j$                  t&              r"|j                   j$                  j(                  |v s|j                   j$                  j(                  }||vrg ||<   ||   j+                  |       |ri }	| j,                  D ]  }
t        |
j.                  t0              r2t        |
j                   t&              r|
j                   j(                  |v sQ|
j                   j(                  }|| j2                  d   v sJ ||   D ]  }|j                   j4                  }||	vrX| j7                  | d      }||	|<   |j9                  t;        d|      t;        d|      t;        d      t;        d|      g       t'        |	|         |_          |S )	Nrv   r  _ref	LOAD_FASTargval
LOAD_CONSTBINARY_SUBSCR
STORE_FAST)r   rM  rF   stacksymbolic_localsvaluesr   store_attr_mutationsr  rv  r!  rV   r   r   rP   r/   rg   r4   r  r8   
local_namer   	graphargs_exampler   r   indexr3  rK  r!   )rv   r  maybe_gmstolen_list_namesalias_instsneeds_aliasqueuexstolen_namevisitedr  	list_namelist_idx
alias_names                 ro   handle_aliases_for_stolen_listsz+OutputGraph.handle_aliases_for_stolen_lists  sc   ##''//9 I  	
XX
&&(
 3388:
 		A!-.!!''4000  10IJKqxx7qxx}}k:HHMM,,0AA((--22K+-+-K($++A.% ( >> 	:C3<<.szz;7JJ))[8 

--I 1 1- @@@@ + :88>>7*!%$+T*"J )3GH%&&.{9M.|HM.?.|JO	 'wx'89':	:@ rn   r   c                     |J ddl m} | _        | _        d _        t
        j                  d|       t        d |j                  D              st        d       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       j-                   j(                         j-                   j/                  |              fd}t1        |j                        D ]  }|j3                  |         j5                          |j7                          t9        |j:                        }|D ]  }	|	j=                            j>                  jA                         D 
ci c]  \  }
}|
tC        |       }}
}tE        |      }g }i }|rg ||d   <   |jF                  jA                         D ]  \  }}tI        |jJ                  tL              r|jJ                  jN                  |k(  r:t        j                  dk\  rtP        jS                  tT        |      rhtP        jS                  tT        |      rJ ||vrg ||<   ||   j                  |        |jW                         D ]7  }|j-                  ||          |j-                  |gt#        ||         z         9 t#         jX                        dkD  r |        g } j[                  d       _.         |t_         jX                              } ja                  d|      }tc        ||      }|j-                  |je                  |d             |j-                  tg        dd             |j                  |ji                  |jj                  j\                                jm                  |       |rt        d |D              rt        d |D              rt#        to        |            t#        |      k(  r҉ jp                  js                         rt#        |jt                        dk7  s jv                  s |         jm                   jy                  |t9        t1        |            |      t        dt#        |            gz           jm                  t1        |      D cg c]  }tc        |      ji                  |       c}       y j[                  d      }tc        |||      } j{                  |||       tc        ||||j|                  jA                         D ci c]  \  }}|dkD  s|d c}}      } j{                  |||       d}g }t         j                        dk7  st#        |j                        dk7  r|j-                   jy                  ||j                         |             t#        |j                        dk7  r#|j                  |ji                  |             d}n,|j                  t        d             n j                  |        |         jm                  ||j                         z           jm                  t1        |      D cg c]  }tc        |      ji                  |       c}       |r+ jm                  tc        |      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rs   )can_restore).0blocks     ro   	<genexpr>z/OutputGraph.compile_subgraph.<locals>.<genexpr>  s     C55$$&C   z&compile_subgraph with block_depth != 0      	MAKE_CELLr  COPY_FREE_VARSco_freevars)r  z)export does not support pregraph_bytecodec                  H    j                           j                          y rs   )add_output_instructionsr   )prefix_instsrv   s   ro   append_prefix_instsz9OutputGraph.compile_subgraph.<locals>.append_prefix_insts  s    ((6 rn   r^  )r7     r   random_values__gen_rand_valuesFc              3      K   | ]K  }t        |t        t        t        f       xr) t        |t              xr |j                         t        u   M y wrs   )r!  r\   rY   r]   rZ   python_typefloat)r2  r   s     ro   r4  z/OutputGraph.compile_subgraph.<locals>.<genexpr>H  s`        3,4  V $A7TAMMOu<TUVs   AAc              3   <   K   | ]  }t        |t                y wrs   )r!  r[   r2  r&  s     ro   r4  z/OutputGraph.compile_subgraph.<locals>.<genexpr>T  s     HaJq.1Hr5  UNPACK_SEQUENCE	graph_out)tempvarsPOP_TOP)F
decoratorsr/  partial_convertcompile_subgraph_reasonr  r   r  allblock_stackr*   sysversion_infor>  opnamer   r!   r  r'  r   r   r  r   rK  r,  rW  exitcleanup_graphprune_dead_localsr   r  realizer   r   rM   r   r  r!  rg   r8   r  r  __instancecheck__rW   r  r   r3  r  r   r#  r%   load_function_namer    create_storer|  r=  r   r   is_emptydebug_localsr  compile_and_call_fx_graphcodegen_suffixusesrB   r_   graph_outputsgraph_output_varsrun_compiler_collectiverL  create_delete)rv   r  rM  r   r/  instr?  r3  stack_valuesr{   r*  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   `                             @ro   compile_subgraphzOutputGraph.compile_subgraph  s    !!!'.'-$		-v6CBNNCCBC*,w& 9;;+- ''*;t{{K [[$44 ''*,#boom6T2U !''		$89 ""t{{	76	7 
 	D223D@@DE	! bnn- 	EJJrN	 	
BHH~ " 	EMMO	 9=8M8M8O
+44D/#&&
 
 019;-/Lb)* &&,,. 	&DAq !((K0QXX5H5HA5M7*)),:  11,BBB$"$QO""1%%	&& ""$ 	<AQ0c,q/&: :;	<
 t  !A%!(*%%)\\/%BD"5d6G6GHIG../BGLLD)G%,,**<> &,,-A!U-KL%,,$$RYY%@%@A (()BC   &  H<HHC%&#l*;;!!**,(A-''!((..r48N3OQUV%&7S=NOPQ
 ((<D\<RSS2++C0S  $||K8b$(89EL%8  6;jj6F6F6HV
UETUI#t)V	E L%8&+#F4::&!+s53F3F/G1/L222u7N7N7PRVW u**+q0MM%"4"45E"FG.2+MM"4Y"?@ ,,R0!((%2H2H2J)JK ((<D\<RSS2++C0S ',,r]001ABC 'u
d T W2 Ts   ] 0!]/]=]*!]c                    | j                   rs| j                  rJ | j                   j                         D ]H  \  }} |       j                  j	                  | 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 rs   rm   )rB  	debug_vars   ro   rC  z,OutputGraph.codegen_suffix.<locals>.<lambda>  s    R	] rn   FrK  )value_from_source)r  r   r   append_outputcreate_loadr  
store_attrr   codegen_hookscodegen_save_tempvarsr\  rD  extend_outputr    r'  r!   restore_stackcodegen_update_mutated)	rv   r  re  rB  r*  rq  r   r  rw  s	      `    @ro   r^  zOutputGraph.codegen_suffix  s2   {{"?!00668 $	c3  0G0G!HId#$ 	''+//3  " 	>OIt23 31#d)UCD0;<=	> 	RYYG004rn   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   r_   r  r2  rv  r   r  zipr  r  _set_grad_enabledtupler   _erased
erase_node)rv   r  r1  r  node1node2s         ro   rU  zOutputGraph.cleanup_graph  s    TZZ%%& 	6DIIMM.5	6 ,,.uQRy1 	1LE5 : ::%**%l*:)<<$zz!}LLEHH$>$>>ejj),.>-@@!MM#(::a=LJJ))%0JJ))%0	1rn   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 )NrQ  )r_   r  r2  rM  r!  r   r   
FakeTensorr  rk   reprr*  )rv   retr1  rQ  r  r  s         ro   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)r*  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 =====
rQ  z: 
FTz (concrete): )r_   r  r2  rM  r!  r   r   r  r  r*  r  rk   r   r  r1  hint)	rv   r*  graph_sizes_strr1  rQ  r  concrete_size
has_symintszs	            ro   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#	( rn   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   )rv   prior_global_statecurrent_global_states      ro   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                 t   |j                   xj                  j                  }t        j	                  dj
                         t        j                  j                  dd fd       t        j                  j                  |j                         t        j                  j                         z        5  d g|j                         z  }t        j                  |j
                  |       |_        d d d        |j                   j#                          t$        j&                  y y # 1 sw Y   5xY w)Nzcompiler_collective %sartifactc                      dddS )Ncompiler_collectivejson)r*  encodingrm   rm   rn   ro   rC  z5OutputGraph.run_compiler_collective.<locals>.<lambda>  s    1 &% rn   c                  h    t        j                  t        j                   j                              S rs   )r  dumpsdataclassesasdictlocal_state)dss   ro   rC  z5OutputGraph.run_compiler_collective.<locals>.<lambda>   s"    4::&&r~~6$ rn   )metadata_fn
payload_fn)group)distributed_state
all_states
compile_pgr   infor  r   _loggingtrace_structuredr  devicerankdevice_countr  distall_gather_objectspeculation_logr   r    CompileCollectiveRestartAnalysis)rv   r  r  r  r  s       @ro   rb  z#OutputGraph.run_compiler_collective  s    &&&B38MJHH-r~~>NN++ , 	 "":??#4uzz7N7N7P#PQ +"Vjoo&77
&&z2>>T *+ $$&666) 9N3+ +s   <>D..D7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                   j                  j#                  ||       t$        j&                  s5t)        t+        j,                  | j.                         j0                  |        j3                          t5         j.                        }t6        d   dxx   |z  cc<    j8                  j;                          t=        | j.                         j>                  D ]
  } |         j@                  _          jB                  jE                         jF                  d<   tH        jK                  d	tM        |d
d
d
             t         jN                  jQ                  d fdfd        jS                           jT                  jV                  }	 jX                  s_ddl-m.c m}
 |
j_                  d      5  t         j`                  jc                  |	j0                        }ddd        jT                  _+         je                         5   jg                        }ddd       ddl4m5} t        |      s&t        tm        |dd      |      rR|jn                  dk(  rCt        ||      r|n|jp                  }|js                  |       t        ||      s|jt                  } ||      }t6        d   dxx   dz  cc<    jw                  ||       ty        |      }|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rs   )as_proxyrG  s     ro   r4  z8OutputGraph.compile_and_call_fx_graph.<locals>.<genexpr>   s     5Oqajjl5Or5  statscalls_capturedr  %sT)include_strideinclude_devicecoloreddynamo_output_graphc                  (    d j                         iS )Nsizes)r  ru   s   ro   rC  z7OutputGraph.compile_and_call_fx_graph.<locals>.<lambda>D  s    $"A"A"CD rn   c                  ,     j                  ddd      S )NFT)print_outputr  r  )print_readable)r   s   ro   rC  z7OutputGraph.compile_and_call_fx_graph.<locals>.<lambda>E  s    2#4#4!&tD $5 $ rn   )r  r   NFr   )r   )_LazyGraphModule__self___lazy_forwardunique_graphs)?r   _guardsr   clear_framerL  r/  r  rb  r#   r!  r   r   rm  r_  
create_argr  r|  _maybe_preserve_original_metar   do_not_emit_runtime_assertsr   r   r   r_   r   remove_unused_graphargsrB   rC   rh  r   r   r  rN  r  r   r2  graph_code_logr  rK   r  r  rX  r   r  r   r   r   r   r   r   r  call_user_compilertorch.fx._lazy_graph_moduler  r  rh   r  force_recompiler   install_global_unsafer%   make_call_generated_coderL  )rv   r  rvrh  r/  r*  output_nodencallsregister_finalizerold_fake_moder  backend_fake_modecompiled_fnr  lazy_gmrB  r   s   `               @ro   r]  z%OutputGraph.compile_and_call_fx_graph  s   
 ]]))557 h	)+####((,_-Db$'''dN333**$$//5OB5O0OPR	K II$$BB2{S55/NN44NN ((* ,FW./69/ !!'')#D$**5B&*&A&A '""2&' *.)E)EB& 55::< GG2   &"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h	) h	)x : :Mh	) h	)s=   IO!<+O')O!O"CO!O	O!O	O!!O*c                 :    | j                   j                  d      S )Nplaceholder)op)r_   
find_nodesru   s    ro   placeholderszOutputGraph.placeholders|  s    zz$$$66rn   c                 Z    | j                   D cg c]  }|j                  d    c}S c c}w Nr.  )r  r2  rv   r1  s     ro   r  zOutputGraph.graphargs  s%    262C2CD$		*%DDDs   (r   c                 j    t        dd      5  | j                  |      cd d d        S # 1 sw Y   y xY w)NOutputGraph.call_user_compilerbackend_compile)
phase_name)rD   _call_user_compiler)rv   r   s     ro   r  zOutputGraph.call_user_compiler  s5    ,9J
 	0 ++B/	0 	0 	0s   )2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
       	 tE        ddi | jF                  |tI        |j                  j                        tI        |      d       	S # t.        $ r}
| j0                  r0t3        | j                   |
      j5                  |
j6                        d d| j8                  j;                          d}t=        |
| j8                  j>                  |       Y d }
~
d }
~
wt@        $ r}
|
d }
~
wtB        $ r}
t3        | j                   |
      |
d }
~
ww xY w)Nr   rI  call_methodcall_moduler   r  r.  rh    zcalling compiler function zdone compiler function z#compiler_fn did not return callablez9Backend compiler failed with a fake tensor exception at 
zAdding a graph break.dynamor  )op_count
node_countinput_count)%r   r_   r  r  r   rJ   r2  rg   _dynamo_sourcer   _param_name_to_sourcer  _source_to_user_stacksr  rh   r   r   INFOr   r   r   r   callabler(   r  r'   with_traceback__traceback__r  format_frame_summaryr+   r  r)   r   r   r   r'  )rv   r   totr  r1  plr  r*  r   r  emsgs               ro   r  zOutputGraph._call_user_compiler  sd   +++HHNN 	*DwwIIqww-'##D)		*
 	3 	+B''*%C #

B	+
 $(#<#< $($>$>!	D 4++Z8   )) 
 LN7<<+EdV)LM**K((,[9%b$*=*=*?@KLN7<<+B4&)IJK(O*OO(& 	,..!"((..1"<0			
 ; 1 
	D55+D,<,<a@OOOO M<<4467'( 
 'q$,,*=*=sCC 	 G 	D'(8(8!<!C	Ds2   7CG 	J 
A;I

J IJ $I;;J c                 b    g }| j                   D ]  }|j                  |j                          |S rs   )r  r   example)rv   r   r  s      ro   r   zOutputGraph.example_inputs  s/    >> 	'CMM#++&	'rn   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)NTFrQ  )	r!  r   Noder2  rM  r   SymBoolr1  maybe_as_bool)r  brs      ro   is_static_truez;OutputGraph.remove_unused_graphargs.<locals>.is_static_true  sq    ~fbgg.0AyDy1emm,&&..00Q= rn   ac                     ddl m} t        | t        t        t
        f      ryt        | t        j                        r%t        | j                  j                  d      |      S y)Nr   SymTypesTrQ  F)
torch.fx.experimental.sym_noder  r!  rk   rE  r   r   r  r2  rM  )r  r  s     ro   is_symnode_argz;OutputGraph.remove_unused_graphargs.<locals>.is_symnode_arg  sD    ?!c5$/0!RWW%!!&&**_"=xHHrn   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  rI  FrQ  c              3   .   K   | ]  } |        y wrs   rm   r2  r  r  s     ro   r4  zWOutputGraph.remove_unused_graphargs.<locals>.is_symnode_compute_node.<locals>.<genexpr>  s     <Q~a(<   c              3   .   K   | ]  } |        y wrs   rm   r  s     ro   r4  zWOutputGraph.remove_unused_graphargs.<locals>.is_symnode_compute_node.<locals>.<genexpr>  s     GQ~a(Gr  T)
r  r  r  r!  r2  rM  rO  r   r   r  )r1  r  r  s     ro   is_symnode_compute_nodezDOutputGraph.remove_unused_graphargs.<locals>.is_symnode_compute_node  sg    ?ww/)diimmO<hG <$))<<G$++2D2D2FGGrn   r   )is_accessor_noder  rI  c                     | j                   d   }|j                  }t        |t        j                        rDt        |j
                  j                  t        j                        r|j
                  j                  S y r  )	r2  r  r!  r   r  r1  r  r  r  )r1  r  r  s      ro   placeholder_binds_symbolzEOutputGraph.remove_unused_graphargs.<locals>.placeholder_binds_symbol  sV    ))J'CkkG'5<<0Z!!5<<6 ||(((rn   c                     t         j                  d| j                  d   j                  j	                                | j                  d= j                  |        j                  j                  | d        y )NzREMOVE UNUSED GRAPHARG %sr.  )r   r  r2  rg   r*  rp  rh  rv  )r1  rv   s    ro   remove_unusedz:OutputGraph.remove_unused_graphargs.<locals>.remove_unused%  s\    II1499Z3H3O3O3T3T3VW 		*%T"!!%%dD1rn   fakec                      | t        |      z  } y rs   )r   )used_symbolsr  s     ro   update_used_symbolsz@OutputGraph.remove_unused_graphargs.<locals>.update_used_symbols/  s    L..Lrn   r.  c                      |       S rs   rm   )r  r"  r!  s    ro   rC  z5OutputGraph.remove_unused_graphargs.<locals>.<lambda>P  s    .A,PQ.R rn   ),r  r   r1  Argument%torch.fx.experimental.symbolic_shapesr  rW  r   r_   r  r'  usersr  r  operatorgetitemr   _checkr   rp  r   r   r  r   r  r   r!  r2  rQ   r  ScriptObjectexample_strong_ref_libraryfake_class_registrytracing_with_realr   __obj_flatten__r  r  wrapped_objpytreetree_map_onlyr  remove)rv   r  r  r  r1  r  r  recheck_placeholdersbinds_symbolr  real_script_objfake_script_obj	flat_dictr  fake_attr_valr  symbolr  r"  r!  s   `                @@@ro   r  z#OutputGraph.remove_unused_graphargs  s    	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!-!$' !''/	0rn   r)  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   rK  r  )rv   r)  s     ro   r=  z#OutputGraph.add_output_instructionsc  s     
 	  ''/rn   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   r@   rH  r   )rv   r*  r{   s      ro   r  z!OutputGraph.install_global_unsafek  sS     411111""4([//0A0A4OPrn   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)rz   r   r   r  rv   r)  r{   r*  s       ro   install_global_by_idz OutputGraph.install_global_by_idw  sK     2e9+R'894)))K""4/rn   c                 @    t        |      }| j                  ||       |S )z~
        Installs a global, generating a unique name for it.

        Returns the name of the newly installed global.
        )r#   r  r?  s       ro   r#  zOutputGraph.install_global  s#      ""4/rn   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                          y r  )r  r   r   r   r_   r  r2  rh  rf  r   r   r  r  r   r  s     ro   cleanupzOutputGraph.cleanup  s     $(!JJ$$ 	*DTYY&IIj)	* 	##%  &&(!##))+##))+--335""$rn   enabledc                     || _         y rs   )r
  )rv   rD  s     ro   r  z$OutputGraph.set_torch_function_state  s
    &-#rn   r  c                 :    | j                   j                  |       y rs   )r  r   )rv   r  s     ro   add_graph_finalizerzOutputGraph.add_graph_finalizer  s     	##**+=>rn   r1  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  r2  r  r   r  r  s     ro   example_value_from_input_nodez)OutputGraph.example_value_from_input_node  sH    77m#99Z(000ww*$$$t{{++rn   )r%  rs   )tmp)FN)r  N)\rh   ri   rj   r   r   r   r   r   r   r   Scoperw   r  rP   r+  r(  r   rS  r   rU  rX  propertyr/  r_  ra  r_   setterrf  rh  rj  rm  rp  
contextlibcontextmanagerrz  r|  r  r   r   r  	GuardsSetr5  r   r  r  r  r  rR   r  rB   r  r  r3  r  staticmethodr  r   r   r   r   r  r,  r   rt  r^  rU  r  r  r  rb  r]  r   r   r  r  r  r   r   r  r  r   r  r"   r=  r  r@  r#  rC  r  rG  rI  r   r   s   @ro   r   r      s   r238nr2 j)r2
 r2 r2 r2hD&5O 5
).0&8BG#4 &#
      & ) ) \\* * 7 7 4 4A@@  (   . . 8 8 A// A A >DcN > >'
R$& N N>3x >3@'05
  L,ehhoou||S89L,\GT QU}19:L1M}~5*1<C 2  "7.m)^ 7d277m 7 7 E4> E E0R^^ 0
 0<bnn < <|U\\ 2 S0j d;.?  D  
QS 	s 	%$. . .?"*BNN+;T+A"B?	?
,%((-- ,rn   r   a  With the current config, we will graph break (and fall back to eager-mode PyTorch) on all ops that have do not have the 'pt2_compliant_tag'. Please see the following doc for how to mark this op as PT2 compliant https://pytorch.org/tutorials/advanced/custom_ops_landing_page.htmlc                     |d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)        t+        |
             Y d }
~
od }
~
ww xY w)NrI  c                 Z    | j                   dv ry j                  j                  |        y )N>   atenprimprims)	namespacer  r   )r  output_graphs    ro   encountered_compliant_opz8check_pt2_compliant_op.<locals>.encountered_compliant_op  s)    88))--f5rn   c                     j                   j                  |        t        j                  rt	        |dz   t
        z          y y )N )r  r   r   only_allow_pt2_compliant_opsr*   err_epilogue)r  r  rX  s     ro   encountered_non_compliant_opz<check_pt2_compliant_op.<locals>.encountered_non_compliant_op  s6    &&**62..#)l23 /rn   z%Encountered the torch.ops.OpOverload z that is not PT2 compliant.r   r   z:Encountered the non-overloaded torch.ops.OpOverloadPacket z that is not PT2 compliant. Fz+Encountered the torch.ops.OpOverloadPacket z! which resolves to the overload (z) that is not PT2 compliant.)r!  r   _ops
OpOverloadTagpt2_compliant_tagtagsOpOverloadPacketr  	overloadsr'  r  _dynamoutilsget_fake_values_from_nodesr  r  _jit_resolve_packet_qualified_op_namer   r*   r   )rX  kindr  r   r   rY  r^  re  r  overloadr  s   `          ro   check_pt2_compliant_oprm    s   6
4
 &%**//099&&&++5$V,$3F8 <) *	

 	&%**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;* =%&? 70  	"#a&!!	"s   -G   	G'	G""G'c                   f     e Zd ZdZ	 d
 fd	Zd Z	 	 	 d fd	Z	 d fd	Zd Zd
dZ	d Z
d	 Z xZS )r   a  
    Holds an FX graph that is being traced. OutputGraph owns a SubgraphTracer
    and the separation of responsibilities is that SubgraphTracer is
    responsible for building the graph while OutputGraph is responsible for
    compiling and executing the graph.
    c                    t         |           t        j                  |      | _        t
        j                  j                         | _        |r|J || _	        i | _
        i | _        || _        i | _        d | _        d | _        d | _        d | _        d | _        | j                  g | _        y | j                  j&                  | j                  j)                  |      |fgz   | _        y rs   )r   rw   weakrefr  rX  r   r   Graphr_   r   rf  rh  rr  lifted_freevars	prev_inst	_cur_code_orig_gm_meta_orig_gm_lineno_map_orig_gm_firstlinenosource_fn_stack_target_to_str)rv   rX  rr  r   rs  r   s        ro   rw   zSubgraphTracer.__init__  s     	#MM,7XX^^%

 >!>& 9; =?   "!#' $(!
 ;;#%D #';;#>#>**=9=IB $D rn   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)
ru  rv  rw  current_instructionstarts_linerM  r   r  _COPY_META_FIELDSr2  )rv   r  r1  linenonode_idxr2  fields          ro   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, ) $ * ) rn   c           	      T
   | 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<   nv|dk(  rq| j                   tY        d       | jT                  jR                  jV                  jR                  j>                  d
   |   d   fgz   jR                  j>                  d<   | j[                  |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<   nv|dk(  rq| j                   tY        d       | jT                  jR                  jV                  jR                  j>                  d
   |   d   fgz   jR                  j>                  d<   djR                  j>                  vrg }|r?|j]                         s|j	                  |j_                                ta        |dd       }|r?|jc                          td        jf                  ji                  |      jk                         }djm                  |      jR                  _7        S c c}w )Nr6  r  )r  c                  v    t              j                         } dj                  j                   d d|  S )NzTRACE FX call z from r  )rE   rstripr1  r*  )linecur_instheaderr  tx_codes    ro   get_trace_call_log_strz;SubgraphTracer.create_proxy.<locals>.get_trace_call_log_str  s9    5gxHOOQD+BGGLL>xr$PPrn   r  Forig_graphmodulec                       y rs   rm   rm   rn   ro   rC  z-SubgraphTracer.create_proxy.<locals>.<lambda>  s    rn   Tnn_module_stack>   r  rI  rx  r  z0Invoking an nn.Module inside HigherOrderOperatorr   r{  rr  r  )8rr  r1  tree_flatten#maybe_lift_tracked_freevar_to_inputr   tree_unflattenr   rj  rX  r  rQ  rR  r|  rs  	positionsr  r  get_line_of_code_headertrace_call_logr  rL   rt  r$   get_contextrM  r!  r   r   r   r_   r  r2  ru  _lineno_maprv  r   __code__r   rw  r  r   r1  rx  r*  r*   r  is_co_filename_from_nn_modulesframe_summaryr  reverser   StackSummary	from_listformatr  r{  )rv   rk  r  r   r   r*  	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                        @@@@ro   rj  zSubgraphTracer.create_proxyP  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*+ ]"{{&PQ.2.B.BGGLLGGLL!23F;A>F /BGGLL*+ 	**2rww7 4"$"4"4"6E6J6J6LBGGLL!23 4;;6:6J6Jv.N 7BGGLL!23 ]*{{.%N 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	O&s   T%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 SubgraphTracerr  )rm  rX  rr  r1  arg_tree_leavesr!  r   r   r  r_   r   rm  r   r2  )rv   r  r  r   r   r*  r  r  r  r1  r   s             ro   rm  zSubgraphTracer.create_node  s     	t00"fdFK;;"..??I  H!#uxx}}5II+HGH+	H w"2vtVT9M*.*;*;*E*E		&'rn   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 rZ  )r'  r&  r  r_   rK  rW  r   r  r  rf  rv  r*  )rv   r1  user_graph_nodesuserother_graph_nodes        ro   rp  zSubgraphTracer.remove_node  s    tzz?Q46

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

d#  $$TYY5rn   c                     t         j                  d|||j                         nd       || j                  J d       | j                  rTt        |d      sG| j                  j                  j                  |g       j                  t        j                                || j                  v r2t        j                         D ]  }| d| }|| j                  vs|} n | j                  rqt        t!        | j                              }| j                  |   j"                  }|r| j$                  j'                  |      }	n7| j$                  j)                  |      }	n| j$                  j'                  d       }	|	5  | j+                  d|di |	      }
| j                  r>|r<| j                  j-                         \  }}|
| j                  |<   || j                  |<   n|
| j                  |<   |
cd d d        S # 1 sw Y   y xY w)
Nzcreate_graph_input %s %sz(none)zByou are required to provide a source for inputs on the root tracerF)allow_cell_or_freevarr  r  rm   )r  )r   r  r*  rr  r   r7   rX  r  
setdefaultr   r   extract_stackrf  r   r   r   rW  r1  r_   inserting_beforeinserting_afterrj  popitem)rv   r*  r  r  rg   r  candidate_name	prev_namer1  r  r  r   r   s                ro   r0  z!SubgraphTracer.create_graph_input  s   		&#/FKKMX	

 >'TST' 'eL!!77BB62NUU"002
 4+++__& $(61#!)A)AA)D	 ##Xd&>&>?@I++I6;;Djj11$7jj006**--d3C 	%%mT2rY%WE''F//779116((../((+16((.	 	 	s   9A1G44G=c                    | j                   J d       || j                  v r| j                  |   S | j                  |j                  j                        }t        |j                  |j                  j                  d          || j                  |<   | j                   4|j                  | j                   k7  r| j                   j                  |       |S )NzIlift_tracked_freevar_to_input should not be called on root SubgraphTracerrQ  )	rr  rr  r0  r1  r*  rO   r2  ry  lift_tracked_freevar_to_input)rv   r  	new_proxys      ro   r  z,SubgraphTracer.lift_tracked_freevar_to_inputP  s     KK#	WV	W#
 D(((''..++EJJOO<	)..%**///*JK&/U#;;"u||t{{'BKK55e<rn   c                     t        |t        j                  j                        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.
        )r!  r   r   Proxyry  r  )rv   r  s     ro   r  z2SubgraphTracer.maybe_lift_tracked_freevar_to_inputb  s=     #uxx~~.JZZ4J11#66rn   )NFN)NNN)NNNN)rh   ri   rj   r   rw   r  rj  rm  rp  r0  r  r  r   r   s   @ro   r   r     sN     KO2jC4 `F HL&6,2j$
7rn   r   )r   rN  r   r  r  r   r  r   r'  r  rQ  r   rp  r   typingr   r   r   r   r   r	   r
   r   r   r  torch._guardsr   torch._loggingtorch.distributeddistributedr  torch.nntorch.utils._pytreerg  _pytreer1  r   r   r   r   torch._utils_internalr   r  r   %torch.fx.experimental._backward_stater   r%  r   r   r   torch.fx.passes.runtime_assertr   torch.utils._python_dispatchr   r  r   r   r   r   backends.registryr   r   bytecode_transformationr    r!   r"   r#   r$   rn  r%   current_scope_idr&   r'   r(   r)   r*   r+   r5  r,   r-   mutation_guardr.   r   r/   r0   rg   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   variables.baserP   variables.builderrQ   rR   rS   rT   rU   variables.listsrV   variables.miscrW   variables.nn_modulerX   variables.tensorrY   rZ   r[   r\   variables.torch_functionr]   torch._dynamo.symbolic_convertr^   	getLoggerrh   r   r  getArtifactLoggergraph_tabular_logr  graph_sizes_logr  re   rq   	lru_cacher   r   r   r   r   r   r   r   objectrK  r   r]  rm  r   r   Tracerr   rm   rn   ro   <module>r     sA            	 
   ! X X X       $ $  N N 0 : ? U U J F D D 5  '  -  0 0 @       ( ,  . ( 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&[I, I,Z.J =@ &ioo' m7RYY m7rn   