
    Phf)                       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Zd dlmc mZ d dl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*m+Z+m,Z,m-Z- d d	l.m/Z/ d d
l0m1Z1 d dl2m3Z3 d dl4m5Z5 ddl6m7Z7mZ8m9Z9 ddl:m;Z;m<Z< ddl=m>Z>m?Z?m@Z@mAZA ddlBmBZB ddlCmDZD ddlEmFZF ddlGmHZHmIZImJZJmKZKmLZL ddlMmNZNmOZO ddlPmQZQ ddlRmSZS ddlTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\m]Z]m^Z^ ddlm_Z_m`Z`maZambZbmcZcmdZdmeZemfZfmgZgmhZhmiZimjZjmkZkmlZl ddlmmnZn ddlompZpmqZqmrZrmsZs ddltmuZu ddlvmwZwmxZxmyZymzZz ddl{m|Z|  ej                  e~      Zej                   j                  e~d      Zej                   j                  e~d       Zej                   j                  e~d!      Zej                   j                  e~d"      Z G d# d$e      Z ej                  d      d%        Ze G d& d'             Zd( Z G d) d*ej                  j                        Z G d+ d,      Zeeef   Z G d- d.e!e         Zd/Zd0 Z G d1 d2e j*                        Zy)3    N)	dataclass)	AnyCallableDictList
NamedTupleOptionalSetTupleUnion)fx)CheckpointableGlobalContextCheckpointStateGuardsCheckpointStateSourceTracingContext)signpost_event)SymNode)free_symbolsis_symbolicShapeEnv)is_traceable_wrapper_subclass)sympy_interp)PythonReferenceAnalysis)WeakTensorKeyDictionary   )config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)SideEffects)

AttrSourceConstantSourceGlobalStateSourceis_constant_sourceis_from_local_sourceLocalSourceParamBufferSourceShapeEnvSourceTensorPropertyTensorPropertySource)checkpoint_paramsCleanupHookclone_inputscount_callscountersdynamo_timedget_instruction_source_311get_static_address_typegraph_break_reasonsincrement_op_countlazy_format_graph_codelazy_format_graph_tabular
LazyStringsame)VariableTracker)GraphArgTrackedFakeVariableBuilderwrap_fx_proxy)NNModuleVariable)NumpyNdarrayVariableSymNodeVariableTensorVariableUnspecializedPythonVariable)TensorWithTFOverrideVariablegraph
graph_codegraph_sizes
trace_callc                      e Zd ZU eeef   ed<   ee   ed<   e	ed<   e
eeej                  j                  f      ed<   eeej"                  gdf      ed<   e
eeef      ed<   e
eeef      ed<   eed	<   eed
<   eej,                  j.                     ed<   eej,                  j.                     ed<   dddd dede
e   fdZed        Zy)OutputGraphStateinput_source_to_vartracked_fakesguard_state
nn_modulesNregister_finalizer_fnsglobal_stateparam_name_to_sourceside_effects	timestampnon_compliant_opscompliant_custom_ops )prefixotherrg   returnc                @   | j                   D ]  }|dk(  r,| j                  j                  |j                        }||c S 4|dk(  r,| j                  j                  |j                        }||c S et	        | |      }t	        ||      }||k7  s| | d| d| c S  y )Nr]   rb   z mismatch: z != )_fieldsr]   diffrb   getattr)selfrh   rg   krsvovs          eC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/_dynamo/output_graph.pyrl   zOutputGraphState.diffx   s    AM!$$))%*;*;<=Hn$%%**5+=+=>=Hq!B"BRx !Kt4t<<       c                 .    | j                   j                  S N)r]   dynamo_guardsrn   s    rs   guardszOutputGraphState.guards   s    ---rt   )__name__
__module____qualname__r   r   rJ   __annotations__r   rL   r   r	   strtorchnnModuler   r   GraphModuleboolr1   intr
   _ops
OpOverloadrl   propertyry    rt   rs   rZ   rZ   k   s    fo566$$&&c588??2344 2>>*:D*@!ABB4T	?++"4V#455N5::0011ejj3344?A ,  hsm ( . .rt   rZ   c                  4    t        j                  t              S rv   )torchdynamo_loggingget_step_loggerlogr   rt   rs   _step_loggerr      s    ..s33rt   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 rv   )r   rD   appendrx   s    rs   __post_init__z GraphCompileReason.__post_init__   s    &&t, rt   N)rz   r{   r|   __doc__r~   r}   r   	tracebackFrameSummaryr   r   r   r   rt   rs   r   r      s,    YKY++,, K-rt   r   c                       fd}|S )Nc            
      N    D  cg c]  \  } }} | |i | c}}} S c c}}} w rv   r   )fnargskwargsrandom_callss      rs   _gen_rand_valuesz1_get_gen_rand_values_fn.<locals>._gen_rand_values   s,    <HIL(8D&D#F#LIIIs    r   )r   r   s   ` rs   _get_gen_rand_values_fnr      s    J rt   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.GraphModuler^   c                 l    t         |           |j                         D ]  \  }}t        | ||        y rv   )super__init__itemssetattr)rn   r^   ro   v	__class__s       rs   r   zFakeRootModule.__init__   s1    $$&DAqD!Q 'rt   c                      y)NzFakeRootModule(...)r   rx   s    rs   __repr__zFakeRootModule.__repr__   s    $rt   )rz   r{   r|   r   r   r~   r   r   r   r   r   __classcell__r   s   @rs   r   r      s)    1 4UXX__(<#=  
%rt   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 rv   )r   )rn   r   s     rs   r   zWrapperBackend.__init__   s	    #*rt   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_correctnessr>   rI   RuntimeError	Exceptionr   	exception)rn   r   r   copy_gmcorrectresults         rs   __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)rz   r{   r|   r!   r   r   r   r   r   Tensorr   r   rt   rs   r   r      s4    +
 +588// ellAS rt   r   c            
       <    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d	 Zd
eg ef   fdZd Zed        Zed        Zd Zed        Zej,                  d        Zed        Zed        Zd Zd Zd Zej:                  d        Zed        Zed        Z ed        Z!ede"jF                  jH                  fd       Z%edeeef   fd       Z&dGdZ'd  Z(d! Z)ed"        Z*de+fd#Z,d$e+fd%Z-d&e.fd'Z/d( Z0d) Z1d* Z2dHd+Z3d, Z4e5d-        Z6d.e7e"jp                  jr                  e"jt                  ef   fd/Z;	 	 	 dId0ee<   fd1Z=d2 Z>d3 Z?ej:                  d4        Z@e"jF                  j                  j                         d5        ZCedeDeEj                     fd6       ZGedeDe.   fd7       ZH eId89      d:eEj                  deKfd;       ZLdeDe"jt                     fd<ZMdJd=ZNdJd>ZOd?eDeP   ddfd@ZQdJdAZRdJdBZSdCe	ddfdDZTdEeeEj                  gdf   ddfdFZU xZVS )KOutputGrapha  
    Wrapper class to hold outputs of InstructionTranslator.  Mainly the
    generated fx.Graph.

    OutputGraph is 1:1 with a frame being processed. Each frame is associated
    with some root InstructionTranslator. When user code calls a function,
    we construct a InliningInstructionTranslator that continues to write into
    the root InstructionTranslator's OutputGraph.
    code_optionscompiler_fnexportlocal_scopeglobal_scopec
                 T   t         |           t        | |      g| _        i | _        || _        || _        || _        t               | _	        g | _
        |	j                  |	j                  |	j                  d| _        g | _        t!               | _        t%        | j                  t&        j(                  t&        j*                  | j                        }
t,        j.                  j1                  |
| j
                  rdnd      }t3        |      | _        | j7                          t9        j:                  t<              | _        tA               | _!        tE               | _#        tA        |      | _$        g | _%        d| _&        g | _'        || _(        || _)        || _*        || _+        ddl,m-} i | _.        g | _/        g | _0        d| _1        d | _2        i | _3        t,        jh                  jk                         | _6        d| _7        t!        i       | _8        t!        i       | _9        | ju                          y )	N)export_root)co_nameco_filenameco_firstlineno)r\   allow_scalar_outputsallow_dynamic_output_shape_ops	co_fieldsTF)	shape_envallow_non_fake_inputsr   )InstructionTranslatorBase);r   r   SubgraphTracertracersr[   r   export_constraintsframe_stater   tensor_weakref_to_sizes_stridescleanup_hooksr   r   r   r   r\   setbound_symbolsr   r   capture_scalar_outputs capture_dynamic_output_shape_opsr   _subclassesFakeTensorModer   tracing_contextinit_ambient_guardscollectionsdefaultdictlisttracked_fakes_id_to_sourcedictra   r1   rb   r   output_instructionsrc   r_   r   r   r   root_txtorch._dynamo.symbolic_convertr   source_to_user_stacks_current_txcleanupsshould_exitrandom_values_varunspec_variable_map_C_is_torch_function_enabledtorch_function_enabled!has_user_defined_allowed_in_graphrd   re   save_global_state)rn   r   r   r   r   r   r   r   r   f_coder   	fake_moder   r   s                rs   r   zOutputGraph.__init__   s    	&t@A CE "4&/F/H,68
 ~~!--$33
 13 14 ,,!'!>!>+1+R+Rnn
	  %%44*.++$5 5 
	
 0>i/H  " ##D) 	' BF!'M .68   OQ# 2=(&L RT"<>+- !%KM &+hh&I&I&K# 27. >AW ADB! 	 rt   c                    | 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                               | j                   j                  t               j                  t        j                               | j                   j                  t               j                  t        j                               y rv   )ry   addr9   
make_guardr.   	SHAPE_ENVr4   DETERMINISTIC_ALGORITHMS	GRAD_MODEDEFAULT_DEVICETORCH_FUNCTION_STATEBACKEND_MATCHCONFIG_HASH_MATCHrx   s    rs   r   zOutputGraph.init_ambient_guardsp  s    	(33L4J4JKL**<+P+PQ	
 	)+66|7M7MNO)+66|7R7RST**<+L+LM	
 	)+66|7Q7QRS)+66|7U7UVWrt   c                     | j                   j                  t               j                  t        j
                               y rv   )ry   r   r4   r   r.   HAS_GRAPH_BREAKrx   s    rs   guard_has_graph_breakz!OutputGraph.guard_has_graph_break  s'    )+66|7S7STUrt   r   c                 :    | j                   j                  |       y rv   )r   r   )rn   r   s     rs   add_cleanup_hookzOutputGraph.add_cleanup_hook  s    !!"%rt   c                 z    t        | j                        D ]	  } |         | j                  j                          y rv   )reversedr   clear)rn   hooks     rs   call_cleanup_hookszOutputGraph.call_cleanup_hooks  s.    T//0DF 1  "rt   c                      | j                   d   S Nr   r   rx   s    rs   root_tracerzOutputGraph.root_tracer  s    ||Art   c                      | j                   d   S Nr  rx   s    rs   current_tracerzOutputGraph.current_tracer  s    ||Brt   c                 2    t        | j                        dk(  S )Nr   )lenr   rx   s    rs   is_root_tracerzOutputGraph.is_root_tracer  s    4<< A%%rt   c                 .    | j                   j                  S rv   r  rU   rx   s    rs   rU   zOutputGraph.graph  s    ""(((rt   c                 &    || j                   _        y rv   r  )rn   values     rs   rU   zOutputGraph.graph  s    $)!rt   c                 .    | j                   j                  S rv   )r  input_name_to_proxyrx   s    rs   r  zOutputGraph.input_name_to_proxy  s    ""666rt   c                 .    | j                   j                  S rv   )r  real_value_cacherx   s    rs   r  zOutputGraph.real_value_cache  s    ""333rt   c                 :     | j                   j                  |i |S rv   )r  create_proxyrn   r   r   s      rs   r   zOutputGraph.create_proxy  s     /t""//@@@rt   c                 :     | j                   j                  |i |S rv   )r  create_noder!  s      rs   r#  zOutputGraph.create_node       .t""..???rt   c                 :     | j                   j                  |i |S rv   )r  remove_noder!  s      rs   r&  zOutputGraph.remove_node  r$  rt   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(   r(  r  	__enter__r   r   r   __exit__pop)rn   r)  prior_tracernew_scope_ctxtracers        rs   	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 rv   r   rx   s    rs   outputzOutputGraph.output  s    rt   c                 .    | j                   j                  S rv   )r   r   rx   s    rs   r   zOutputGraph.fake_mode  s    ##---rt   c                 B    | j                   j                  j                  S rv   )r   r   r   rx   s    rs   r   zOutputGraph.shape_env  s    ##--777rt   ri   c                 B    | j                   j                  j                  S rv   )r   guards_contextrw   rx   s    rs   ry   zOutputGraph.guards  s    ##22@@@rt   c                 B    | j                   j                  j                  S rv   )r   module_contextr^   rx   s    rs   r^   zOutputGraph.nn_modules  s    ##22===rt   Nc                 V   ||n| j                   j                  j                  }| j                  | j                  f|d<   t
        j                  t        j                         f|d<   t
        j                  t        j                         f|d<   t
        j                  t        j                         f|d<   t
        j                  t        j                         f|d<   t
        j                  t        j                         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autocast_enabledautocast_cpu_enabledautocast_gpu_dtypeautocast_cpu_dtypeautocast_cache_enabled)r   global_contextr`   set_torch_function_stater   r   set_grad_enabledis_grad_enabledset_autocast_enabledis_autocast_enabledset_autocast_cpu_enabledis_autocast_cpu_enabledset_autocast_gpu_dtypeget_autocast_gpu_dtypeset_autocast_cpu_dtypeget_autocast_cpu_dtypeset_autocast_cache_enabledis_autocast_cache_enabled)rn   outr`   s      rs   r   zOutputGraph.save_global_state  s   
 ?C(<(<(K(K(X(X 	
 ))''2
-. ).(>(>@U@U@W'X^$&&%%',
'(
 **))+0
+,
 ((((*.
)*
 ((((*.
)*
 ,,++-2
-.rt   c                 :    | j                   j                  |       y rv   )r   r   )rn   txs     rs   push_txzOutputGraph.push_tx  s    #rt   c                 6    | j                   j                         S rv   )r   r,  rx   s    rs   pop_txzOutputGraph.pop_tx
  s    ##%%rt   c                 P    | j                   s| j                  S | j                   d   S r  )r   r   rx   s    rs   
current_txzOutputGraph.current_tx  s$    #'#3#3t||M9I9I"9MMrt   c                 v   | j                   J | j                  j                  j                         }| j                  j                  j                         }| j                  j
                  j                         }t        t        | j                        t        | j                        ||t        | j                        |t        | j                         | j                  j                         | j                  t        | j                         t        | j"                              }| xj                  dz  c_        |S )z>Create a checkpoint of the current state by copying everythingr   )ra   r   r6  copy_graphstater8  r@  rZ   r   r[   r   r\   r_   rb   clonerc   r   rd   re   )rn   guards_graph_statemodule_stater`   states        rs   rW  zOutputGraph.copy_graphstate  s    ((444!11@@PPR++::JJL++::JJL ))*##$,,-**+##%NN&&'))*
 	!rt   r[  c                    |\  | _         | _        }}| _        }| _        | _        | _        | _        | _        | j                  j                  j                  |       | j                  j                  j                  |       | j                  j                  j                  |       d}t        t        | j                  j                               D ]|  }|j"                  d   | j
                  kD  s |j$                  dk7  s0d|j"                  v r|j"                  d= | j'                  |       | j(                  j+                  |d       |dz  }~ t,        j/                  d|       y)z6Restore a checkpoint created by self.copy_graphstate()r   creation_timestampplaceholderexample_valueNr   z$restore_graphstate: removed %s nodes)r[   r\   r_   ra   rb   rc   rd   re   r   r6  restore_graphstater8  r@  r  r   rU   nodesmetaopr&  r  r,  r   debug)rn   r[  guards_staterZ  r`   removed_nodesnodes          rs   r`  zOutputGraph.restore_graphstate'  s2    	
$'%N"%++>>|L++>>|L++>>|L T$**"2"234D		./$..@GG}, #dii/		/2  &%%))$5" 5 			8-Hrt   argc                       j                   ry |j                  J  fdfd |j                  |j                         y )Nc                 0   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|      }| |j                  j                   d<   t#        || dd d      |j                  j                   d<   y )Nzbind_symint %s %sT)beforesourcer_  F)is_unspecializedfake_tensor	is_tensorgrapharg)r   
isinstancerg  exprsympySymbolr   r   r   rd  namer  create_graph_inputr~   r   SymIntrb  rK   )sprops0proxyrn   s       rs   bind_symintz4OutputGraph.add_symbol_bindings.<locals>.bind_symintW  s    Nz!&&++u||'LBT'''""2&II)1diik: $$77B	 8 E 01EJJOOO,*2!& +EJJOOJ'rt   c           	         t        | j                               D ]'  \  }} |t        |t        j                  |             ) t        | j                               D ]'  \  }} |t        |t        j                  |             )  | j                         t        |t        j                               t        |       r:| j                         \  }}|D ]!  }t        | |      } 	|t        ||             # y y rv   )	enumeratesizer;   r:   SIZEstrideSTRIDEstorage_offsetSTORAGE_OFFSETr   __tensor_flatten__rm   r2   )
tsrcirx  attrsctxattrinner_tr|  handle_tensors
           rs   r  z6OutputGraph.add_symbol_bindings.<locals>.handle_tensorp  s    !!&&(+1A3C9L9LaPQ ,!!((*-1A3C9N9NPQRS .  "$S.*G*GH -Q/113
s!D%a.G!':c4+@A " 0rt   )r   rn  rl  )rn   rh  r|  r  s   ` @@rs   add_symbol_bindingszOutputGraph.add_symbol_bindingsK  s>     ;;***	2	B 	cooszz2rt   c                 ,    t        | j                        S rv   )r?   rU   rx   s    rs   r?   zOutputGraph.count_calls  s    4::&&rt   c                 X    t        t        | j                  j                              dk(  S r  )r  r   rU   ra  rx   s    rs   is_empty_graphzOutputGraph.is_empty_graph  s!    4

(()*a//rt   c                     |sJ | j                   }|j                  d      D ]$  }t        |t              r||   }t	        ||      }& |S N.)r^   splitrq  r   rm   )rn   keysobjro   s       rs   get_submodulezOutputGraph.get_submodule  sI    tBF//CA#t$!fc1o	 !
 
rt   c                     t        | j                  d         }t        j                         D ])  }| d| }||vs| j                  dxx   |fz  cc<   |c S  y )Nco_varnames_)r   r   	itertoolscount)rn   ru  existingr  vars        rs   new_varzOutputGraph.new_var  s\    t((78"AF!A3-C("!!-0SF:0
	 #rt   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   )rn   ru  s     rs   update_co_nameszOutputGraph.update_co_names  s2    t((44j)dW4) 5rt   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)joinmapr~   rer  isalpha)namesru  s     rs   module_key_namezOutputGraph.module_key_name  sm     xxC(vv,eT:vvlIt4vvosD147??,4<Drt   targetc                     t              rt        j                  fi S t              dv sJ d   t	        t
              rJ t	        t        j                        r j                   j                         s j                  t              s(t        j                  t        j                               t!              dk(  r(t        j                  t        j"                                fd}nt	        t        j$                  j&                        rUt	        t        j$                  j&                        sJ t        j                  t        j(                               fd}n8t	        t        j*                  t        j,                  f      r fd}n fd} j.                  j1                         D ]  \  }}|u s ||      c S  t3        j4                  | }t7        j8                         D ]  } j.                  vr j.                  <   t	        t        j$                  j&                        r_ fd}	t;        d      r j=                         D ]  \  }
} |	|
        t;        d	      r j?                         D ]  \  }
} |	|
         |      c S | d
|  tA        d      )Nrl  guardedc           	          j                   J j                   | <   t        j                  j                  d| t	               i       fdiS )Nget_attrr_  )ra   rN   r   r   tuple)
module_keyoptionsrn   rl  r  r/  s    rs   	wrap_namez6OutputGraph.register_attr_or_module.<locals>.wrap_name  sc    00<<<8>))*5$LL''
JL #) 	 rt   c                 0    t        t              | fi S rv   )rO   type)r  r  r  s    rs   r  z6OutputGraph.register_attr_or_module.<locals>.wrap_name  s    'VjLGLLrt   c           	      j    t        j                  j                  d| t               i       fdiS )Nr  sym_num)rQ   creater   r  )r  r  rn   r  s    rs   r  z6OutputGraph.register_attr_or_module.<locals>.wrap_name  sC    &--%%j*egrJ # 	 rt   c                     j                   j                  |        j                  | <    t        t	        |                   S )N)source_name)r2  r  r   rM   r3   )r  rn   r  s    rs   r  z6OutputGraph.register_attr_or_module.<locals>.wrap_name  sC    ++J706!!*-Tt^
-ST rt   c                 f    j                   J t        |       } d|  }|j                   |<   y r  )ra   r8   )	leaf_name
new_sourcenew_nameru  rn   rl  s      rs   register_leaf_namez?OutputGraph.register_attr_or_module.<locals>.register_leaf_name  sA    #88DDD%6vy%I
&*V1YK#8>H11(;rt   _parameters_buffersr  unreachable)!r0   r   UnspecializedNNModuleVariabler   rq  r8   r   r   r  r  r  r5   r/   r   r.   TENSOR_MATCHrC   DATA_PTR_MATCHr   r   	NN_MODULErw  SymFloatr^   r   r   r  r  r  hasattrnamed_parametersnamed_buffersAssertionError)rn   r  r  r  r  ro   r   baser  r  r  r  ru  rl  r/  s   `` `        @@@rs   register_attr_or_modulez#OutputGraph.register_attr_or_module  sL     '::6MWMMw-7""""f&7888fell+((F&&( ))%f-f//0I0IJK&v.);f//0K0KLM	 	 0fehhoo666&++L,B,BCDM u~~ >? OO))+DAqF{ |# ,
 **E2"A4??*(.%fehhoo6I v}5,2,C,C,ELIq.y9 -Fvz2,2,@,@,BLIq.y9 -C !&V1QC=D+ #. ]++rt   r   c                     |J ddl m} | _        | _        d _        |s j                          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(                  |               fd}t+        |j                        D ]  }|j-                  |         j/                          |j1                          t3        |j4                        }	t7         j8                        }
g }i }|	rt3               ||	d   <   |j:                  j=                         D ]^  \  }}t?        |j@                  tB              r|j@                  jD                  |k(  r:||vrt3               ||<   ||   j                  |       ` |jG                         D ]7  }|jI                  ||          |	jI                  |gt%        ||         z         9 t%        |jJ                        dkD  r |        g } jM                  d       _'        tQ        d      } |tS        |jJ                              } jU                  ||       tW        ||
      }|jI                  |jY                  |d             |jI                  t[        dd             |j                  |j]                  |j^                  jN                                ja                  |       |	rt        d |	D              rt        d |	D              rt%        tc        |	            t%        |	      k(  ro jd                  jg                         rU |         ja                   ji                  |t3        t+        |	            |
      t!        dt%        |	            gz          n jM                  d      }tW        ||
|      } jd                  jk                  |        jd                  jm                  |       |jo                  |	        jd                  jq                  |       tW        ||
||jr                  j=                         D ci c]  \  }}|dkD  s|d c}}      } jd                  jk                  |        jd                  jm                  |       |jo                  |	        jd                  jq                  |       g }tu         jv                        dk7  st%        |jx                        dk7  r|jI                   ji                  ||j{                         |
             t%        |jx                        dk7  r!|j                  |j]                  |             n|j                  t!        d              |         ja                  ||j}                         z           ja                  t+        |      D cg c]  }tW        |      j]                  |       c}       y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rv   )can_restore).0blocks     rs   	<genexpr>z/OutputGraph.compile_subgraph.<locals>.<genexpr>9  s     CN55$$&N   z&compile_subgraph with block_depth != 0      	MAKE_CELL)argvalCOPY_FREE_VARSco_freevars)rh  c                  H    j                           j                          y rv   )add_output_instructionsr	  )prefix_instsrn   s   rs   append_prefix_instsz9OutputGraph.compile_subgraph.<locals>.append_prefix_instsM  s    ((6 rt   r  r   random_values__gen_rand_valuesFc              3   T   K   | ]   }t        |t        t        t        f        " y wrv   )rq  rS   rP   rT   )r  r   s     rs   r  z/OutputGraph.compile_subgraph.<locals>.<genexpr>  s7      
 &A 3,4  &s   &(c              3   <   K   | ]  }t        |t                y wrv   )rq  rR   r  xs     rs   r  z/OutputGraph.compile_subgraph.<locals>.<genexpr>  s     H<aJq.1<r  UNPACK_SEQUENCE	graph_out)tempvarsPOP_TOP)?
decoratorsr  partial_convertcompile_subgraph_reasonr   r  r   rd  allblock_stackr,   sysversion_infor  opnamer   r#   r  r  r   r   r  exitcleanup_graphprune_dead_localsr   stackr   r^   symbolic_localsr   rq  rl  r7   
local_namer  extendr   r  r   r%   r   install_globalr'   load_function_namer"   create_storer2  r  r   rb   is_emptycompile_and_call_fx_graphcodegen_hookscodegen_save_tempvarsrestore_stackcodegen_update_mutatedusesr?   rU   graph_outputsgraph_output_varsget_instructions)rn   rP  r  r   compile_return_valuer  instr  r  stack_valuesrootrestore_varsval_to_namesro   r   random_calls_instructionsrand_fn_namerand_fncodegengraph_output_varpass1valr  pass2r2  r  r  s   `                         @rs   compile_subgraphzOutputGraph.compile_subgraph   s    !!!'.'-$#&&(		-v6CBNNCCBC*,w&;;+- ''*;t{{K [[$44 ''*,#boom6T2U !''		$8 (	! bnn-EJJrN . 	
BHH~doo.9;-1VLb)* &&,,.DAq !((K0QXX5H5HA5M$"&&QO""1% / ""$AQ0c,q/&: :; %
 r!#!(*%%)\\/%BD"$%89L5booFGGg6D)G%,,**<> &,,-A!U-KL%,,$$RYY%@%@A (()BC  
 &
 
 H<HHC%&#l*;;!!**,!((..r48N3OQUV%&7S=NOPQ
  $||K8b$(89E++E233E:-44U;  6;jj6F6F6HV6H
UETUI#t)6HV	E ++E233E:-44U;F4::&!+s53F3F/G1/L222u7N7N7PRVW u**+q0MM%"4"45E"FGMM"4Y"?@!((%2H2H2J)JK 	$$8@8NO8NYr]'',8NO	
+ W, Ps   Y<-Y<!Z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)
        r]  Nr   r   )r   r   rU   ra  rb  r,  r   rC  zipr  r   _set_grad_enabledr  r   _erased
erase_node)rn   ra  rg  r:  node1node2s         rs   r  zOutputGraph.cleanup_graph  s    TZZ%%&DIIMM.5  ,,.uQRy1LE5 : ::%**%l*:)<<$zz!}LLEHH$>$>>ejj),.>-@@!MM#(::a=LJJ))%0JJ))%0 2rt   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 =====
r_  z: 
FTz (concrete): )rU   ra  rb  getrq  r   r   
FakeTensorr  ru  r  r   r   rw  rg  hint)	rn   ru  graph_sizes_strrg  r_  r  concrete_size
has_symintszs	            rs   get_graph_sizes_log_strz#OutputGraph.get_graph_sizes_log_str  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# %( rt   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
        )rN  N)r   r@  rW  r   r`  r   )rn   prior_global_statecurrent_global_states      rs   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           
      &    ddl m}  j                  sJ t        d      t	        |t
              sJ t	        |t              sJ  j                  dd j                  j                  t        d |D                    fi         j                  |        j                          t         j                        }t        d   dxx   |z  cc<    j                   j#                          t%        j&                  | j                        } j(                  D ]
  } ||         j*                  |_        t,        j/                  dt1        |             t2        j/                  dt5        |             t6        j/                  dt9         fd	              j;                           j<                  j>                  } j@                  s;tB        jD                  jG                  |jH                  
      }	|	 j<                  _         jK                         5   jM                  |      }
ddd        |
      }
t        d   dxx   dz  cc<    jO                  |
       tQ        |      }|jS                         |jU                         S # 1 sw Y   cxY w)zr
        Generate code from self.graph and return the Instruction()s to
        call that generated code.
        r   r  __compiled_fnr2  c              3   <   K   | ]  }|j                           y wrv   )as_proxyr  s     rs   r  z8OutputGraph.compile_and_call_fx_graph.<locals>.<genexpr>  s     1K1!**,r  statscalls_captured%sc                  &    j                         S rv   )r(  )ru  rn   s   rs   <lambda>z7OutputGraph.compile_and_call_fx_graph.<locals>.<lambda>0  s    T%A%A$%Grt   )r   Nunique_graphs)+r  r  r   r%   rq  r   r   r#  r  
create_argr  insert_deferred_runtime_assertsremove_unused_graphargsr?   rU   r@   r  r	  r   r   r_   r  graph_code_logrd  rF   graph_tabular_logrG   graph_sizes_logrH   r  r   r   r   r   r   r   r   r,  call_user_compilerr  r'   make_call_generated_coder  )rn   rP  rvr  r  ncallsr   register_finalizerold_fake_modebackend_fake_modecompiled_fncgru  s   `           @rs   r   z%OutputGraph.compile_and_call_fx_graph	  s3    	()"d###$///  ++E1K1K,KLN		
 	,,T48 	$$&TZZ(*+v5+ 	##%^^D$**-"&"="=r" #> &*%A%A"T#9$#CD&?b&IJ*GH	
 	!,,66{{ % 1 1 @ @'11 !A ! .?D  *&&(11"5K )k*/*a/*D+.r]
##D)""$$ )(s   JJc                     g }| j                   j                  D ]%  }|j                  dk(  r|j                  |       $ |S  |S )Nr^  )rU   ra  rc  r   )rn   rp   rg  s      rs   placeholderszOutputGraph.placeholdersI  sD    JJ$$Dww-' %
 rt   c                 Z    | j                   D cg c]  }|j                  d    c}S c c}w Nrp  )rG  rb  rn   rg  s     rs   	graphargszOutputGraph.graphargsS  s+    262C2CD2C$		*%2CDDDs   (backend_compile)
phase_namer   c           
      8   | 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        $ r5}
t3        | j                   |
      j5                  |
j6                        d d }
~
ww xY w)Nr   call_functioncall_methodcall_moduler   r^  rp  rz   rf   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.dynamozOutputGraph.call_user_compiler)op_count
node_countinput_count)%r   rU   ra  rc  r   rE   rb  rl  _dynamo_sourcera   _param_name_to_sourcer   _source_to_user_stacksr  rz   r   r   INFOr   r   r   r   callabler*   r   r)   with_traceback__traceback__r   format_frame_summaryr-   r   r+   r   r   r   r  )rn   r   totrG  rg  plrh  ru  r   rD  emsgs               rs   r=  zOutputGraph.call_user_compilerW  sl   +++HHNNDwwIIqww-'##D)	 #
 	3B''*%C #

B 
 $(#<#< $($>$>!	 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 	'(8(8!<KK	s2   7CG 	J
A;I

JIJ$0JJc                 b    g }| j                   D ]  }|j                  |j                          |S rv   )rK  r   example)rn   r   rh  s      rs   r   zOutputGraph.example_inputs  s*    >>CMM#++& "rt   c                 *     j                   sJ t        t         j                  j                              D ]  }t        t        |j                              dk(  s%|j                  dk(  r j                  |       F|j                  dk(  sV|j                  t        j                  u ss j                  |        d } fd}t               }g } j                  D ]  } ||      d u}|r|j                  r|j                  |       .|j                  s	 ||       C|j                  d   }|j                   |j                   n|j"                  }|t%        |      z  } |D ]+  } ||      }	|	|	|vr	 ||       |j'                  |	       - y )Nr   r  rP  c                     | j                   d   }|j                  }t        |t        j                        rDt        |j
                  j                  t        j                        r|j
                  j                  S y rI  )	rb  rd  rq  r   rw  rg  rr  rs  rt  )rg  rh  rd  s      rs   placeholder_binds_symbolzEOutputGraph.remove_unused_graphargs.<locals>.placeholder_binds_symbol  sV    ))J'CkkG'5<<0Z!!5<<6 ||(((rt   c                     t         j                  d| j                  d   j                  j	                                | j                  d= j                  |        j                  j                  | d        y )NzREMOVE UNUSED GRAPHARG %srp  )r   rd  rb  rl  ru  r&  r  r,  rg  rn   s    rs   remove_unusedz:OutputGraph.remove_unused_graphargs.<locals>.remove_unused  s\    II1499Z3H3O3O3T3T3VW 		*%T"!!%%dD1rt   rp  )r   r  r   rU   ra  r  usersrc  r&  r  operatorgetitemr   rG  r   rb  rn  rd  r   remove)
rn   rg  rg  rj  used_symbolsrecheck_placeholdersbinds_symbolrh  fakesymbols
   `         rs   r9  z#OutputGraph.remove_unused_graphargs  sb   T$**"2"234D4

#$)77j($$T*WW/DKK8CSCS4S$$T* 5		2 u!%%D3D9ELzz(//5zz!$' ))J/C+.??+FCKK  !L$66L &$ )D-d3F!-!$' !''/ )rt   c           
           j                   j                  j                         }t        d |j	                         D              syt        j                  | j                        }t        j                  dt        d| |             i t               }d} j                  j                  D ]&  j                  dk7  r} n|j                         ( |J t               |j	                         D ]-  }|D ]&  }j                  t!        |j"                               ( / t$        j                  d        j                  j                  D ]   j                  j'                  |vrj(                  n|j(                        5  dj*                  vr
	 ddd       Rg fd}	 |	j*                  d   fd	       t-        j*                  d   x}
t.        j0                        ryt3        |
j5                               D ]  \  } |	| fd
        t3        |
j7                               D ]  \  } |	| fd         |	|
j9                          fd       D ]  }|j;                  |g       }|D ]d  }t$        j                  d|j"                         t!        |j"                        }|j=                         z
  }|rRt?        |      d   } j                   jA                  |      sJ |       |jC                  |g       jE                  |       tG        tH        |j"                        jJ                  } j                  jM                  t.        jN                  jP                  jR                  jT                  |f      } j                  jM                  t.        jN                  jP                  jV                  jX                  |d|j"                   f       g  	 ddd        y# 1 sw Y   xY w)a  
        During tracing, we may have discovered that some data-dependent values
        had runtime assert on them; e.g., torch.empty(x.item()) induces a runtime
        that x.item() >= 0.  This asserts can happen unpredictably during fake
        tensor propagation, so we cannot conveniently insert them into the FX graph
        when they occur.  Instead, we accumulate them in the ShapeEnv, and in this
        pass insert them into the graph as proper tests.
        c              3       K   | ]  }|  y wrv   r   )r  rass     rs   r  z>OutputGraph.insert_deferred_runtime_asserts.<locals>.<genexpr>  s     9"833"8s   Nr3  z$pre insert_deferred_runtime_asserts r^  zneeded_symbols = %sr_  c                 x   t        | t        j                        rt        | j                  t              rt        | j                  j
                  x}t        j                        rS|vrN|v rIt        j                   |             |<   t        j                  d||          j                  |       y y y y y y )Nzsymbol_to_proxy[%s] = %s)rq  r   rw  rg  r   rr  rs  rt  r   Proxyr   rd  r   )symintcbrx  defsneeded_symbolssymbol_to_proxys      rs   match_symbolzAOutputGraph.insert_deferred_runtime_asserts.<locals>.match_symbol  s    "65<<8&v{{G<&FKK,<,<'<qellK_4/-/XXbd^*		"<aQRASTA	 0 5 L = 9rt   c                       S rv   r   )rg  s   rs   r5  z=OutputGraph.insert_deferred_runtime_asserts.<locals>.<lambda>&  s    rt   c                  @    j                   j                  d f      S )Nr  rU   rQ  r  rg  rn   s   rs   r5  z=OutputGraph.insert_deferred_runtime_asserts.<locals>.<lambda>*  s    tzz'='=ftQi'Prt   c                  @    j                   j                  d f      S )Nr  r  r  s   rs   r5  z=OutputGraph.insert_deferred_runtime_asserts.<locals>.<lambda>.  s    tzz'='=hq	'Rrt   c                  >    j                   j                  d f      S )Nr  r  ri  s   rs   r5  z=OutputGraph.insert_deferred_runtime_asserts.<locals>.<lambda>2  s    

 6 67G$ Qrt   zinserting runtime assert %sr   z"Deferred runtime assertion failed )-r   deferred_runtime_assertsr   anyvaluesr   r   rU   r:  rd  rF   r   ra  rc  r   updater   rr  r   inserting_beforenextrb  rq  r   r   r~  r  r  r  r,  r  sortedis_unbacked_symint
setdefaultr   r   r   rg  rP  opsatenscalar_tensordefault_assert_asyncrb  )rn   r  ru  ras_by_symbolr   rG  last_placeholderrv  rar~  r  rx  i0fvsmissingi1resres2r{  r  r|  rg  r}  s   `                 @@@@@rs   r8  z+OutputGraph.insert_deferred_runtime_asserts  sd    ??DDF9-"6"6"899^^D$**-"%I$#PRTU	
 uJJ$$Dww-'#' T"	 %
  +++ -0E '')C%%l277&;<  * 			'8JJ$$D ,,!5		;K;P;P #$))3	   
' TYY7F499_#==au||L )!&&( 31$P !4 !*!((* 51$R !6 !((*Q
 B'++B3C!		"?I*2773"%(<(<(>">"!'!3B#'>>#D#DR#HL"L#H)44R<CCBG #/ 7"''#"d   $(::#;#; %		 < < D Dsf$D !JJ44 %		 < < @ @ %)&H	$R!"	% " c 	 % s   O>(I	O>>P	rg   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   )rn   rg   s     rs   r  z#OutputGraph.add_output_instructionsS  s     
 	  ''/rt   c                 x    | j                   j                  t        j                  | j                  ||             y rv   )r   r   r=   r  r   )rn   ru  r  s      rs   r  zOutputGraph.install_global[  s)    [//0A0A4OPrt   c                    d | _         | j                  j                          d | _        | j                  j
                  D ]  }d|j                  v s|j                  d=   | j                  j                          | j                  j                          | j                  j                          | j                  j                          y rI  )r   r^   r	  ra   rU   ra  rb  r  r  rb   r_   rJ  s     rs   cleanupzOutputGraph.cleanup^  s     $(!JJ$$DTYY&IIj) % 	##%  &&(!##))+rt   enabledc                     || _         y rv   )r   )rn   r  s     rs   rA  z$OutputGraph.set_torch_function_staten  s
    &-#rt   rA  c                 :    | j                   j                  |       y rv   )r_   r   )rn   rA  s     rs   add_graph_finalizerzOutputGraph.add_graph_finalizerq  s     	##**+=>rt   rv   )tmp)FNF)ri   N)Wrz   r{   r|   r   r   r~   r   r	   r!   r   Scoper   r   r  r   r  r  r   r  r  r  rU   setterr  r  r   r#  r&  
contextlibcontextmanagerr0  r2  r   r   r   _guards	GuardsSetry   r^   r   rQ  rS  rU  rZ   rW  r`  rK   r  r?   r  r  r  r  staticmethodr  r   r   r   r   r  r   r  r  r(  r,  r   clear_framer   r   r   NoderG  rK  rA   r   r    r=  r   r9  r8  r$   r  r  r  rA  r  r   r   s   @rs   r   r      s   C!38nC! j)C!
 C! C! C!NX*V&8BG#4 &#
      & ) ) \\* * 7 7 4 4A@@  (   . . 8 8 A// A A >DcN > > 
D$& N N!1 ,"I(8 "IH43x 43l'05
  p,ehhoou||S89p,j /3"^
 +,	^
@1<2  " ]]!!--/=% 0=%~ d277m   E4> E E ./>R^^ >
 > 0>@U\\ 2 60rzx d;.?  D  Q, . . .?"*BNN+;T+A"B?	?rt   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://docs.google.com/document/d/1W--T6wz8IY8fOI0Vm8BF44PdBgs283QvpelJZWieQWQc                     |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      \  }}	 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)NrP  c                 Z    | j                   dv ry j                  j                  |        y )N>   r  primprims)	namespacere   r   )r  output_graphs    rs   encountered_compliant_opz8check_pt2_compliant_op.<locals>.encountered_compliant_op  s)    88))--f5rt   c                     j                   j                  |        t        j                  rt	        |dz   t
        z          y y )N )rd   r   r   only_allow_pt2_compliant_opsr,   err_epilogue)r  rb  r  s     rs   encountered_non_compliant_opz<check_pt2_compliant_op.<locals>.encountered_non_compliant_op  s6    &&**62..#)l23 /rt   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. z+Encountered the torch.ops.OpOverloadPacket z! which resolves to the overload (z) that is not PT2 compliant.)rq  r   r   r   Tagpt2_compliant_tagtagsOpOverloadPacketr  	overloadsr  rm   _dynamoutilsget_fake_values_from_nodesrU  r   _jit_resolve_packet_qualified_op_namer   r,   r~   )r  kindr  r   r   r  r  r  rc  overloadra  s   `          rs   check_pt2_compliant_opr    s   6
4
 &%**//099&&&++5$V,$3F8 <) *	

 	&%**556&**,-	 y>Q1.Byy**bgg5(,(..4X 6./ }}**EE##dF^
f	"xx33)),04:H VX&99&&"''1$R((=fX F33;* =%&? 70  	"#a&!!	"s   -F? ?	G&G!!G&c                   `     e Zd ZdZ	 d	 f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 rv   )r   r   weakrefr{  r  r   r   GraphrU   r   r  r  r(  lifted_freevars	prev_inst	_cur_code_orig_gm_meta_orig_gm_lineno_map_orig_gm_firstlinenosource_fn_stack_target_to_str)rn   r  r(  r   r)  r   s        rs   r   zSubgraphTracer.__init__  s     	#MM,7XX^^%
 >!>& 9; =?   "!#' $(!
 ;;#%D #';;#>#>**=9=IB $D rt   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rt-        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@                  r| jD                  r| jL                  r|j                  jZ                  }d }|)| jD                  j1                  || jL                  z
  d       }|o| j@                  |   }t6        j\                  j^                  D ]#  }||v s||   jR                  j>                  |<   % d|v r|d   jR                  j>                  d<   |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	                  |ja                                tc        |dd       }|r/|je                          tf        jh                  jk                  |      jm                         }djo                  |      jR                  _8        S c c}w )Nr  rO  )linenoc                  v    t              j                         } dj                  j                   d d|  S )NzTRACE FX call z from r   )rB   rstriprg  ru  )linecur_instheaderr?  tx_codes    rs   get_trace_call_log_strz;SubgraphTracer.create_proxy.<locals>.get_trace_call_log_strE  s9    5gxHOOQD+BGGLL>xr$PPrt   r3  Forig_graphmoduleTnn_module_stack>   rQ  rP  r  rR  z0Invoking an nn.Module inside HigherOrderOperatorr   stack_tracer(  rf   )9r(  pytreetree_flatten#maybe_lift_tracked_freevar_to_inputr   tree_unflattenr   r   r  rU  r  r  current_instructionr  	positionsr  r   get_line_of_code_headertrace_call_logrd  rH   r  r&   get_contextr!  rq  r   r   r   rU   ra  rb  r  _lineno_mapr  r   __code__r   r  r  r   rg  r  ru  r,   starts_liner{  _COPY_META_FIELDSframe_summaryrm   reverser   StackSummary	from_listformatr  r  )rn   r  r  r   r   ru  	type_exprproxy_factory_fn	flat_args	tree_specnew_flat_argsrh  maybe_new_argrP  r  is_retracingorig_graphmodule_maybendr  r  node_idxrb  fieldframe_summariesmsgsr  r  r?  r  r   s                            @@@@rs   r   zSubgraphTracer.create_proxy  s5   V ;;"#)#6#6f~#F IyM  $ H H M$$]3 ! "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*%1%=%=bii%H%L%L"D&" 0%((2F2FG#&<&B&B&H&H&&HBGG&H&" ,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*+ (())++77FH!3377T666 #))(3XX77E}.25kU+ 8 !D(26}2EBGGLL/ 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&&r'7'7'9:R40  ##% ))33ODKKMD"$''$-BGG	m&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 SubgraphTracerr]  )r  r  r(  r  arg_tree_leavesrq  r   r   r  rU   r   r#  rc   rb  )rn   rc  r  r   r   ru  r  r  rh  rg  r   s             rs   r#  zSubgraphTracer.create_node  s     	t00"fdFK;;"..??I !#uxx}}5II+HGH+	 ! w"2vtVT9M*.*;*;*E*E		&'rt   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 r  )r  rk  r  rU   r  r  r   ra  r  r  r,  ru  )rn   rg  user_graph_nodesuserother_graph_nodes        rs   r&  zSubgraphTracer.remove_node  s    tzz?Q46

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

d#  $$TYY5rt   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^  r   )r  )r   rd  ru  r(  r   r6   r  r   r  r   r   extract_stackr  r  r  r  r  rg  rU   r  inserting_afterr   popitem)rn   ru  r  rk  rl  r  candidate_name	prev_namerg  r  r{  ro   r   s                rs   rv  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((. SSs   9A1G44G=c                    | j                   J d       || j                  v r| j                  |   S | j                  |j                  j                        }|j                  j
                  d   |j                  j
                  d<   || j                  |<   | j                   4|j                  | j                   k7  r| j                   j                  |       |S )NzIlift_tracked_freevar_to_input should not be called on root SubgraphTracerr_  )r(  r  rv  rg  ru  rb  r/  lift_tracked_freevar_to_input)rn   r{  	new_proxys      rs   r  z,SubgraphTracer.lift_tracked_freevar_to_input
  s     KK#	WV	W#
 D(((''..++EJJOO<	/4zz/O	O,&/U#;;"u||t{{'BKK55e<rt   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.
        )rq  r   r   rx  r/  r  )rn   rh  s     rs   r  z2SubgraphTracer.maybe_lift_tracked_freevar_to_input  s=     #uxx~~.JZZ4J11#66rt   )NFN)NNN)NNNN)rz   r{   r|   r   r   r   r#  r&  rv  r  r  r   r   s   @rs   r   r     sH     KO1r od HL&6,2j$
7rt   r   )r   r  r   	functoolsr  r   rl  r  r  r   r  dataclassesr   typingr   r   r   r   r   r	   r
   r   r   rs  torch._guardsr   torch._loggingtorch.nntorch.utils._pytreer  _pytreer  r   r   r   r   r   r   torch._utils_internalr   torch.fx.experimental.sym_noder   %torch.fx.experimental.symbolic_shapesr   r   r   torch.utils._python_dispatchr   torch.utils._sympy.interpr   torch.utils._sympy.referencer   torch.utils.weakr   rf   r   r   r   backends.registryr    r!   bytecode_transformationr"   r#   r$   r%   r&   r  r'   current_scope_idr(   excr)   r*   r+   r,   r-   ry   r.   r/   mutation_guardr0   rb   r1   rl  r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   variables.baserJ   variables.builderrK   rL   rM   rN   variables.nn_modulerO   variables.tensorrP   rQ   rR   rS   variables.torch_functionrT   	getLoggerrz   r   _logginggetArtifactLoggerr;  r:  r<  r  rZ   	lru_cacher   r   r   r   r   r   r   r~   objectr  r   r  r  Tracerr   r   rt   rs   <module>r.     s          	 
   ! U U U     $ $   1 2 U U F 2 @ 4 ? ? 5  '  -  0 0 %       , T T 1  Cg!NN44XwG 11(LI..228]K11(LI#.z #.L T4 4 - - -	%UXX__ 	%   F 	S&[V?.!12 V?t$V =@f7RYY f7rt   