
    ^j                   
   U d Z ddlm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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ZddlmZ ddlmZ ddlmZm Z m!Z!m"Z" ddlm#Z#m$Z$m%Z%m&Z& ddl'm(Z( dd	lm)Z) ddl*Z*ddl+Z*dd
l,m-Z- ddl.m/Z/ ddl0m1Z1 ddl2m3Z3 ddl4m5Z5m6Z6m7Z7m8Z8 ddl+m9Z9 ddl:m;Z;m<Z<m=Z= ddl>m?Z? ddl@mAZAmBZB ddlCmDZE ddlFmGZG ddlHmIZI ddlJmKZKmLZLmMZMmNZN ddlOmPZPmQZQ ddlRmSZSmTZTmUZUmVZVmWZW ddlXmYZY ddlZm[Z[m\Z\ ddl]m^Z^m_Z_m`Z`maZambZb ddlcmdZdmeZemfZfmgZg ddlhmhZh ddlimjZjmkZkmlZlmmZmmnZnmoZompZpmqZqmrZrmsZs dd lUmtZtmuZumvZvmwZwmxZxmyZymzZzm{Z{m|Z|m}Z}m~Z~mZmZ dd!lmZ dd"lmZmZmZ dd#lmZ dd$lmZmZmZ dd%lmZmZmZ dd&lmZ dd'lmZ dd(lmZmZmZmZmZ dd)lWmZ dd*lmZmZmZmZ dd+lmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ dd,lmZ d-ed.<   	 ddlZejz                  r.dd/lmZ dd0lmZ dd1lXmZ dd2lmZ dd3lmZ dd4lmZmZmZ dd5lmZ  ej                  eͫ      Ze*j                  j                  ed6      Ze*j                  j                  ed7      Z ej                         Z e&d8      Z e(d9      Z G d: d;      Z	 	 	 	 dd<Z	 	 	 	 	 	 dd=Z G d> d?      Z eګ       Z eګ       Zdad@edA<    ej                  eE      	 d	 	 	 	 	 	 	 ddB       ZdddCZddDZesj                  ddE       Z	 	 d	 	 	 	 	 	 	 	 	 ddGZda ej                         ZdHedI<   ddJZddKZe G dL dM             Z	 	 	 	 ddNZ G dO dP      Z	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 ddRZddSlmZ ddTlmZ  e       ZdUedV<   dWZ	 	 	 	 ddXZddYZddZZdd[ZedFddddd\	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd]       Ze G d^ d_             Ze G d` da             Z edQb       G dc dd             ZddeZe G df dg             Ze G dh di             ZddjZddkZ	 d	 	 	 	 	 	 	 ddmZ 	 dddFdn	 	 	 	 	 	 	 	 	 	 	 ddoZe G dp dl             ZddFdn	 	 	 	 	 	 	 ddqZddsZdFddddd\	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddtZ	 	 dddddu	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddvZ G dw dx      Z	 	 d	 	 	 	 	 	 	 	 	 ddyZddzZ	dd{Z
 G d| drej                        Z G d} d~      Z	 	 	 	 	 	 ddZy# e$ r dZY Qw xY w)a  
This module implements TorchDynamo's core frame conversion functionality, transforming Python
frames into FX graphs. It handles:

- Frame analysis and bytecode transformation
- Guard creation and management for dynamic behaviors
- Cache management for recompilation
- Error handling and fallback mechanisms

Key classes:
- ConvertFrame: Main entry point for frame conversion with error handling
- ConvertFrameAssert: Implements core frame to graph conversion logic
- Tracker: Tracks input/output code objects during conversion
- CatchErrorsWrapper: Provides error handling and suppression logic

The conversion process preserves program semantics while enabling optimizations
through torch.compile() and related systems.

NOTE: _torchdynamo_orig_backend is used for convert frame wrappers to identify the inner wrapped function.
By going down the _torchdynamo_orig_backend chain, one can recover the original unwrapped backend,
which is checked for during the Dynamo cache lookup.
    )annotationsN)	dataclass)Path)CellTypeCodeTypeFunctionType
ModuleType)AnycastNoReturnTypeVar)	ParamSpec)ReferenceType)GlobalStateGuard)CallbackTrigger)get_compile_pg)TensorifyState)compile_contextCompileContext	CompileIdtracing)
structured)compile_time_strobelight_meta#maybe_upload_prof_stats_to_manifoldsignpost_event)_use_lazy_graph_module)ConstraintViolationErrorGuardOnDataDependentSymNode)_forward_from_src)_WaitCounter)DistributedDataParallel)_disable_current_modes any_torch_dispatch_mode_on_stack/is_in_any_mode_without_ignore_compile_internalsis_traceable_wrapper_subclass)CapturedTracebackformat_traceback_short   )config
decoratorsexcgraph_break_hintstrace_rules)_is_registered_backend)remove_dead_coderemove_pointless_jumps) check_inst_exn_tab_entries_validInstructionis_generator propagate_inst_exn_table_entriestransform_code_object)CacheSizeRelevantForFramecompute_cache_sizeexceeds_recompile_limitis_recompilation)code_context)
_get_cache_entries_for_region_get_total_cache_entry_countadd_skip_reasonalways_optimize_code_objects
Constraint
dynamo_tls$get_eval_frame_isolate_recompiles_idinnermost_backend	skip_codeTorchPatcher)augment_exc_messageBackendCompilerFailedFailOnRecompileLimitHitformat_error_msgInternalTorchDynamoErrorPackageErrorResumePrologueTracingErrorShortenTracebackTorchRuntimeErrorUncapturedHigherOrderOpErrorunimplementedUnsupported	UserError)reset_user_object_tracking)CheckFunctionManager'get_and_maybe_log_recompilation_reasonsGuardedCode)Hooks)CodeOptionsDynamoTracerOutputOutputGraphCommon)_log_size_mismatch_recompilelog_frame_dynamic_whitelistput_code_state)ExecutionRecord)TORCH_DYNAMO_RESUME_IN_PREFIX)DistributedStateExceptionStackInstructionTranslator
LocalStateSpeculationLog)is_numpy)ConvertFrameReturnFrameActionFrameExecStrategywrap_guarded_code)_get_error_on_graph_breakchromium_event_timedCleanupManagerCompileTimeInstructionCountercountersdynamo_timedformat_bytecodegen_record_file_name#get_hook_for_recompile_user_contextget_metrics_contextincrement_frameis_namedtupleistype
LazyStringmaybe_disable_inference_mode*maybe_disable_inference_mode_for_fake_proporig_code_mapreset_graph_break_dup_checkersetup_compile_debug	to_int_ustroubleshooting_urlwrite_record_to_file)#torch_function_mode_stack_state_mgrzModuleType | Nonenp)Callable)WeakIdKeyDictionary)
CompilerFn)CompilePackage)WrapBackendDebug)BytecodeHook
CacheEntryDynamoFrameType)FrameStateSizeEntrybytecodegraph_breaks_T_Pc                      e Zd Zy)TODO_UNKNOWNN)__name__
__module____qualname__     f/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/_dynamo/convert_frame.pyr   r      s    r   r   c                    | y| j                   j                  j                  }|j                  j	                          |j
                  j	                          y)z:Clear WeakIdRef entries from a FakeTensorMode's describer.N)fake_tensor_convertermeta_converter	describerlookup_tensorclearlookup_storage)	fake_moder   s     r   _clear_fake_mode_weakrefsr      sH     //>>HHI!!#""$r   c                2   t         j                  }|t        t        |            }|r| sy| j                  }|y|j
                  }|j                  j                          t        |j                         t        |d      rt        |j                         yy)zBClear WeakIdRef entries that can block swap_tensors after compile.N_old_fake_mode)r)   #invalidate_compile_context_weakrefsr.   rB   output_graph_for_cleanuptracing_contexttensor_to_contextr   r   r   hasattrr   )tracer_outputcompiler_fnshould_clearoutput_graphtcs        r   clear_compile_context_weakrefsr      s    
 ==L-.?.LM} !99L		%	%B bll+|-.!,"="=> /r   c                  ,    e Zd ZddZddZddZddZy)	Trackerc                0    g | _         t               | _        y N)seensetseen_idsselfs    r   __init__zTracker.__init__   s    35	"%%r   c                     t        |       j                  vrQt        j                  | fd      } j                  j                  |        j                  j                         y y )Nc                :    j                   j                        S r   )r   remove)_idxr   s    r   <lambda>zTracker.add.<locals>.<lambda>   s    DMM4H4H4M r   )idr   weakrefrefr   appendadd)r   
strong_objobjr   s   `  @r   r   zTracker.add   sR    ndmm#++j*MNCIIS!MMc" $r   c                0    t        |      | j                  v S r   )r   r   )r   items     r   __contains__zTracker.__contains__  s    $x4==((r   c                l    | j                   j                          | j                  j                          y r   )r   r   r   r   s    r   r   zTracker.clear  s     		r   NreturnNone)r   r   r   r   )r   r   r   bool)r   r   r   r   r   r   r   r   r   r   r   r      s    (#)r   r   zGlobalStateGuard | Noneinitial_global_statec                J    t        | ||      }t        |j                         |S r   )original_forward_from_srcrC   __code__)srcglobals	co_fieldsresults       r   fx_forward_from_src_skip_resultr     s#     'sGY?FfooMr   c           	        t        j                  t              t        j                  d|z         j                         }t        j                  |      }t        t        j                  fd|            | j                  | j                  t        j                  | j                        dgz   t        j                  j                  dfd       |D cg c]:  }|j                   k7  r)d|j"                   d|j$                   d	|j                    < c}d| j                   d| j                   d	| j                   gz   }|S c c}w )
N   skipc                    | d   k7  S )Nfilenamer   )fconvert_frame_interns    r   r   z"log_dynamo_start.<locals>.<lambda>$  s    a
m';; r   )linenamer   dynamo_startc                     d iS )Nstackr   )r   s   r   r   z"log_dynamo_start.<locals>.<lambda>1  s    %  r   zLine: z, Name: z, Filename: )r   intern_string__file__r&   extractsummaryfrom_tracebacklist	itertools	takewhileco_firstlinenoco_nameco_filenametorch_loggingtrace_structuredr   linenor   )coder   captured_tbframes_internedframestack_stringsr   r   s         @@r   log_dynamo_startr     s6   %33H=#++T:BBDK //<O;	
 ''LL"001A1AB	
	E 
NN##  !>>11 huzzl,u~~>NO
 $$%Xdll^<HXHXGYZ		M s   ?E	c                R     t        j                         d fd       } |_        |S )a@  
    Context manager to:
        1) Save/restore torch.is_grad_enabled() state
        2) Save/restore torch random state
        3) Monkey patch torch.fx.graph_module._forward_from_src

    NOTE: Python random state is preserved in eval_frame_cpp.cpp instead,
    so that it wraps more of the compilation pipeline.
    c                    t               }t        j                         }t        j                  j	                         5  t               5  t               5  t        j                         }t        j                         }t        j                         }t        j                  j                         }t        j                         }t        j                  j                         }	d }
t        j                  j                         rEt        j                  j!                         5  t        j                  j                         }
d d d        t        j                  j#                  dd      }t        j$                  j&                  j(                  }t*        t        j$                  j&                  _        t-               }t/        j0                         }|j3                  t        j$                  j4                  j7                                t9                	  | i ||j;                          |j;                          t        j                  j=                  |       t        j>                  j@                  jC                  |       t        jD                  ||       t        j                  jG                  |	       t        jH                  |       t        j                  j                         }||k7  rt        j                  jK                          |
Ft        j                  j!                         5  t        j                  jG                  |
       d d d        t        j                  jM                  dd|       |t        j$                  j&                  _        |jO                         stQ        d|jS                          d      cd d d        cd d d        cd d d        S # 1 sw Y   xY w# 1 sw Y   xY w# |j;                          |j;                          t        j                  j=                  |       t        j>                  j@                  jC                  |       t        jD                  ||       t        j                  jG                  |	       t        jH                  |       t        j                  j                         }||k7  rt        j                  jK                          |
St        j                  j!                         5  t        j                  jG                  |
       d d d        n# 1 sw Y   nxY wt        j                  jM                  dd|       |t        j$                  j&                  _        |jO                         stQ        d|jS                          d      w xY w# 1 sw Y   nxY wd d d        n# 1 sw Y   nxY wd d d        y # 1 sw Y   y xY w)Ncudamatmul)	warn_onlyzGlobal z7state changed while dynamo tracing, please report a bug)*r   r   is_grad_enabled_C_PreserveDispatchKeyGuardrw   rx   is_inference_mode_enabled$are_deterministic_algorithms_enabled-is_deterministic_algorithms_warn_only_enabled$_is_default_mobile_cpu_allocator_setget_default_dtyperandomget_rng_stater   is_availableDisableTorchFunction_get_fp32_precision_getterfxgraph_moduler   r   r{   
contextlib	ExitStackenter_context_symbolic_trace_maybe_revert_all_patchesrR   close_set_grad_enabledautograd	grad_mode_enter_inference_modeuse_deterministic_algorithmsset_rng_stateset_default_dtype#_unset_default_mobile_cpu_allocator_set_fp32_precision_settercheckAssertionErrorreason)argskwargsguardsprior_grad_modeprior_inference_modeprior_deterministicprior_warn_onlyprior_mobile_allocator_stateprior_dtypetorch_rng_statecuda_rng_statecuda_matmul_fp32_precprior_fwd_from_srccleanup
exit_stackcurr_mobile_allocator_statefns                   r   _fnz"preserve_global_state.<locals>._fnJ  s\   !#//1 HH..07	(*7	 787	
 $)#B#B#D "'"L"L"N#QQSO==? )  113K#ll88:O!Nzz&&(XX224 @%*ZZ%=%=%?N@$)HH$G$G%! "'!6!6!H!H6UEHH!!3)+G#--/J$$((BBD '(4*6*  "**?;((>>?ST22'? **?;''4HHAAC , 03NNHH@@B!-668 A

00@A33H&; ;M%%7||~(!&--/!22ij k7	 7	 7	 7	@ @>A A   "**?;((>>?ST22'? **?;''4HHAAC , 03NNHH@@B!-668 A

00@A A A33H&; ;M%%7||~(!&--/!22ij  &i7	 7	 7	 7	 7	 7	 7	 7	s   W	WCV-O	>CV-O2DV-( O&A5V-=	W	WO#V-&O/+V-2DV*	 T0'	V*	0T95A5V*	*V--V62W9	WW	WW r  z_P.argsr  z	_P.kwargsr   r   )	functoolswraps_torchdynamo_orig_backend)r,  r-  s   ` r   preserve_global_stater2  ?  s0     __R? ?B %'C!Jr   c                   | j                   t        v ry| j                   j                  D ]  }|| j                  v s| j                  |   }t	        |t
              r%|j                  j                  d      s|t        u r yt        s]t        j                  sn|t        u st        |      s y i dfd| j                  j                         D ]  } |      s y y)z+Check if the frame has torch.* related bitsTztorch.c           	         t               }|v r|   S d|<   t         t        j                  t        j                  j
                  f      s4t         t              r.t         t        j                  j
                        r
d|<   |   S t        j                  rDt        r>t         t        j                        st         t        j                        r
d|<   |   S t         t        t        f      rt!        fd D              |<   |   S t         t"              r5t         j%                               }t!        fd|D              |<   |   S t         t&        t(        t*        t        d      t,        f      r
d|<   |   S t/               r3t1         d      r't!         fd j2                  D              |<   |   S y)z)Recursively check if the obj has a tensorFTc              3  .   K   | ]  } |        y wr   r   .0v
has_tensors     r   	<genexpr>z:has_tensor_in_frame.<locals>.has_tensor.<locals>.<genexpr>  s     ">Q:a=">   c              3  .   K   | ]  } |        y wr   r   r6  s     r   r:  z:has_tensor_in_frame.<locals>.has_tensor.<locals>.<genexpr>  s     "AQ:a="Ar;  N_fieldsc              3  B   K   | ]  } t        |              y wr   )getattr)r7  r8  r9  r   s     r   r:  z:has_tensor_in_frame.<locals>.has_tensor.<locals>.<genexpr>  s     "T1:gc1o#>"Ts   )r   
isinstancer   TensornnModuleru   type
issubclassr)   trace_numpyr   ndarraygenericr   tupleanydictvaluesstrintfloatr   rt   r   r=  )r   obj_idrL  r9  r   s   `  r   r9  z'has_tensor_in_frame.<locals>.has_tensor  s   CXF## cELL%((//:;3*S%((//"B#HVF##RZZ(JsBJJ,G#HVF##C$'"">#">>HVF##C #**,'F""A&"AAHVF##C#sE4:t<=$HVF##3GC$;""T"TTHVF## r   F)r   objectr   r   )f_coder>   co_names	f_globalsr@  r	   r   
startswithr   r   r)   rF  rd   f_localsrL  )r   r   r   valuer9  r   s       @@r   has_tensor_in_framerX    s     ||33 <<(( 	eoo%//'*C#z*''1SE\f((cRi8C=	 !#H(V &&( e r   Fc                    d }t        | d      r)t        | |      }t        || j                         || _        t        | |       y )Nexec_recordexport)r   rp   r~   rZ  record_filenamerE   )er   r   r\  r]  s        r   exception_handlerr_    s?     Oq- .q$7_amm<+&)r   z)typing.Counter[int | FrameStateSizeEntry]FRAME_COMPILE_COUNTERc                <    t         j                  rt        |       S | S r   )r)   cprofilecprofile_wrapper)funcs    r   maybe_cprofilere    s    %%Kr   c                D     t        j                         d fd       }|S )Nc                    t        j                         }|st        d      t        t        j
                  j                  t        j                         j                   dt        |      j                  dd       d            }t        j                         }	 t        j                         } |j                  g| i |}t        j                         |z
  }t"        j'                  dj                  ||       t)        j*                  |      }	 |j-                  |       t"        j'                  d	|       |j1                  d
      }		 t3        j4                  ddddddt        |      gt2        j6                        5 }
t3        j8                  dddt        |	      g|
j:                         t"        j'                  d|	       d d d        tI        t        |            xr#tJ        jL                  jO                  dfd       |S # t         $ r# t"        j%                  d       d} | i |}Y Yw xY w# t.        $ r t"        j%                  d|       Y 6w xY w# 1 sw Y   xY w# t<        $ r t"        j'                  d       |j?                  t(        j@                  jB                        jE                  d       |j?                  t(        j@                  jF                        jE                  d       Y -w xY w)NzTrace id is Noner   /z.profilezfailed to enable cProfiler   z7### Cprofile for %s trace id [%s] took %.3f seconds ###zCannot write to %szRaw profile at %sz.svg	gprof2dotz-fpstatsz"--node-label=total-time-percentagez!--node-label=self-time-percentagez--node-label=total-time)stdoutdotz-Tsvgz-o)stdinz Generated SVG from profile at %szxFailed to generate SVG from profile -- dumping stats instead.Try installing gprof2dot and dot for a better visualization   linkc                     d dS )Ncprofile_manifold_url)r   urlr   )manifold_links   r   r   z;cprofile_wrapper.<locals>.profile_wrapper.<locals>.<lambda>5  s    !8O r   )(r   current_trace_idr  r   ospathjointempfile
gettempdirr   rM  replacecProfileProfiletimeruncall
ValueErrorlog	exceptionwarningrj  Stats
dump_statsOSErrorwith_suffix
subprocessPopenPIPE
check_callrk  FileNotFoundError
sort_statsSortKeyTIMEprint_stats
CUMULATIVEr   r   r   r   )r  r  trace_idprofile_pathprofstart_tsretvalprofile_latencypssvg_pathgprof2dot_processrs  rd  s              @r   profile_wrapperz)cprofile_wrapper.<locals>.profile_wrapper  s   !224 !344GGLL##%==/3x=#8#8c#B"C8L
 !	+yy{H
 "T\\$888F"iikH4O
 	EMM		
 \\$	>OOL) 	'6++F3	E!!87-% " J #%%GT3x=9+22 >I!J2 @
 
= 
 NN++O e  	+MM56O4*6*F	+  	>MM.=	>
J J" ! 	EKKN MM&..--.::2>MM&..334@@D	EsQ   AH H; 5I- AI!	I- (H87H8;II!I*&I- -BK?>K?r.  )r/  r0  )rd  r  s   ` r   rc  rc    s)    __TE EN r   c                      e Zd ZU dZded<   y)ConvertFrameBoxNzbool | Noneerror_on_graph_break)r   r   r   r  __annotations__r   r   r   r  r  <  s    (,+,r   r  c                   d| vrt         | d<   t         dz  a | d   }t        |t              st        dt	        |             t
        |   }t
        |xx   dz  cc<   d }t        j                         x}r|j                  }t        |||      S )N_idr(   zframe_id must be an int, got )compiled_autograd_idframe_idframe_compile_id)
FRAME_COUNTERr@  rN  r  rD  r`  r   current_compile_idr  r   )frame_stater  r  r  priors        r   get_compile_idr  A  s     K*E5!Hh$<T(^<LMNN,X6(#q(#1133u3$991) r   c                  p    e Zd Z	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 ddZed	d       Zdd	 	 	 	 	 	 	 	 	 	 	 d
dZy)ConvertFrameAssertNc                    t                || _        || _        || _        || _        || _        || _        t               | _        y r   )	rz   r1  
_one_graph_export_export_constraints_package_recompile_limitr  _box)r   r   	one_graphr\  export_constraintspackagerecompile_limits          r   r   zConvertFrameAssert.__init__Z  sA     	&')4&##5  /#%	r   c                      fdS )Nc                t    t        | j                  j                  j                  j                        S Nr  )convert_frame_assertr  r  r  r  backendr   s    r   r   z8ConvertFrameAssert._clone_with_backend.<locals>.<lambda>o  s0    3OOLL$$ 11 
 r   r   r   s   `r   _clone_with_backendz&ConvertFrameAssert._clone_with_backendm  s    
 	
r   r   r   c               	   t                |j                  }t               }t        ||      }|dk\  r|t        |d      z   }	n|}	t	        |      }
t        |||
      }t        j                  |       |t        v rt        d      S t        j                  j                  d      r8t        j                  j                  d      |j                  k7  rt        d      S |j                  dk(  r'|j                  j                  d      rt        d	      S |j                  d
k(  rt        d      S |j                  dk(  r\|j                  j!                  t        j"                  j%                  t&        j(                  j*                              rt        d      S |j                  dk(  r|j                  dk(  rt        d      S |j                  dk(  r0|j                  dk(  r!t-        |j.                        st        d      S t1        |      r t3        ddddgt4        j6                         t9        |      st        d      S t;        j<                         }|r?d|j                  j                  v r'|j>                  }|rd|j                  j                  v r'|r)|j                  t@        jB                  u rt        dd      S tD        }tG               a"tI        |      }|jJ                  }tM        dd|j                  |tO        |      |j                  |jP                  |jR                  |jT                  d       |j                  j!                  tV              sG|j                   d |j                   d!|jP                   }tX        jZ                  j]                  |       	 t_        ta        |            }| jb                   te        jf                  | jb                  "      nti        jj                         }|5  |5  tm        |j                  |jn                  |jp                  |j.                  |jr                  | jt                  | jv                  | jx                  | jz                  ||||	|||||d#z   | j|                  | j~                  $      }d d d        d d d        |a"td        j                  r-| j|                  !d#d%lAmB} |j                  | j|                         S # 1 sw Y   RxY w# 1 sw Y   VxY w# |a"w xY w)&Nr   z&frame is already Dynamo-generated codeskip_reasonTORCHDYNAMO_DEBUG_FUNCTIONz>function name does not match TORCHDYNAMO_DEBUG_FUNCTION filterz	<genexpr>)ztransformers/file_utils.pyztransformers/utils/generic.pyzdiffusers/utils/outputs.pyz7skipping generator expression in transformers/diffusers__setattr__z#__setattr__ frames are not compiledr   z,torch.optim __init__ frames are not compiledz<module><string>zframe was generated by exec()z<lambda>z!frame is a namedtuple constructorzAttempt to trace generator z<Generators cannot be compiled directly with `torch.compile`.zbCall a generator from inside of a non-generator Python function and compile that function instead.gb_typecontextexplanationhintsz(no tensor-related objects found in frameztorch/_dynamo/convert_frame.pyFz2tracing is non-recursively disabled for this frame)apply_to_coder  dynamoz_convert_frame_assert._compile)r   r  
compile_idr   r   
cache_sizeaccumulated_cache_size :r  r(   )r  r  r   r  convert_frame_box)DynamoCache)Drs   rR  rA   r;   r<   r7   input_codesr   output_codesre   ru  environgetr   r   endswithrU  rv  dirnamer   optimr   r   
f_builtinsr3   rO   r,   FUNDAMENTALrX  sys	_getframef_backr*   "_nonrecursive_disable_wrapper_coder   r   r  r  r   rM  r   +num_cache_entries_with_same_id_matched_objsnum_cache_entriesr^   r@   traced_frame_infosr   r   r   r  r)   patchr
  nullcontext_compilerT  rV  closurer1  r  r  r  r  r  caching_precompiler  r  record_package)r   r   cache_entryhooksr  r   r   isolate_recompiles_idcache_entriescache_entries_for_reasonstotal_countr  
prev_frameprev_initial_global_stater  r  infocompile_ctxrecompile_ctxr   r  s                        r   __call__zConvertFrameAssert.__call__w  s~    	|| D F5d<QR !A%(58Ub9 )% )6%248'}kJ
<%D  JJNN78

;<L%\  <<;&4+;+;+D+D,
 &U  <<=(%2WXX<<:%$*:*:*E*EGGOOEKK001+
 &J  <<:%$*:*:j*H%2QRR LLJ&  J.))* &2UVV4Z5 '22		 #5)%F  ]]_
 0J4E4E4Q4QQ#**J 0J4E4E4Q4QQ !!Z%R%RR%#P  %9!/1#K0
&&,<<$!*o#//"&"5"5(TT*4*F*F	
 ||&&'DEll^1T%5%5$6a8K8K7LMD))006"	=).*DEK
 ((4 T-B-BC++- 
  m !LLOONN$$MM22OOLL,,!- +) MM&*ii) 2 $= $$)B, &&t}}5A   2 $= s>   !AS4 7S(:BSS(S4 S%	!S((S1-S4 4S8TFNNN)r   r   r  r   r\  r   r  
Any | Noner  CompilePackage | Noner  
int | Noner   r   )r   z*Callable[[CompilerFn], ConvertFrameAssert]r   r   r  CacheEntry | Noner  rV   r  $dict[str, int | FrameStateSizeEntry]r   rN  r   re   r   r   r   r   propertyr  r  r   r   r   r  r  Y  s     )-)-&*&& & 	&
 '& '& $& 
&& 
 
  qq 'q 	q
 :q q 
qr   r  Tc                "    t        | |||||      S )zHFully convert a frame into an FX graph, raising an exception if we fail.)r  )r   r  r\  r  r  r  s         r   r  r  +  s#      r   )OrderedDict)RemovableHandlezdict[int, BytecodeHook]_bytecode_hooksbytecode_hook_side_effectsc                    t        j                  |       syt        j                  |       j                  t              }|y|S )zReturn Dynamo's replayed Python side-effect sources for compiled code.

    Returns ``None`` when ``code`` was not produced by Dynamo or no metadata was
    attached to it.
    N)r:   has_contextget_contextr  '_BYTECODE_HOOK_SIDE_EFFECTS_CONTEXT_KEY)r   side_effectss     r   get_compiled_code_side_effectsr	  H  sD     ##D)++D155/L r   c                *    t        t        |             S )zNReturn whether Dynamo recorded replayed Python side effects for compiled code.)r   r	  r   s    r   compiled_code_has_side_effectsr  Z  s    .t455r   c                    t        j                  |       sy t        j                  |       }t        |v r$|t           t        j                  |      t        <   y y r   )r:   r  r  r  )src_codedst_codesrc_contexts      r   _copy_code_contextr  _  sO    ##H-**84K.+=?@ 	  *+RS >r   c                J    t        t              }| t        |j                  <   |S )a*  Register hooks for bytecode generated by Dynamo. The hook can do some
    logging, as well as return a new code object to be used. Hooks can query
    `get_compiled_code_side_effects(new_code)` for replayed Python side effect
    sources. Please refer to `BytecodeHook` for the hook signature.
    )r  r  r   )hookhandles     r   register_bytecode_hookr  i  s!     _-F!%OFIIMr   r\  r  r  distributed_stater  c                  ddl mm j                          t	               }t        |	| ||||||
||||||||      d	fd}	  |        t              }|j                  }|t        d      |j                  st        d      |j                  |	d d  |
j                  |j                         t        |	       t        |	       t        t        |	            |	d d  |S # t         $ r}t        d      |_         d }~ww xY w)
Nr   )bisecttranslation_validation_enabled)r  speculation_logexn_vt_stackr  r  c                    	 j                   j                          t        j                   j                        5  j	                         5  j                          d d d        d d d        j                   j                          i _        y # 1 sw Y   3xY w# 1 sw Y   7xY w# t        j                  $ r j                           t        j                  t        j                  t        j                  f$ r  t        $ r%         r  j                   j                          w xY w# j                   j                          i _        w xY wr   )outputmark_bytecode_tracing_startr   r   set_current_txrunr+   UnspecializeRestartAnalysisr   SpeculationRestartAnalysisTensorifyScalarRestartAnalysis	SkipFrame	Exception	shape_envcall_cleanup_hooksrV  )r  r  tracerr  s   r   
run_tracerztrace_frame.<locals>.run_tracer  s	   	!MM557667 9N9N9P 

   MM,,. FO#   .. 	!!#**..MM
 	
  	-/v}}../	
 MM,,. FOsG   9B( BBB&B( B	BB%!B( (BD++D. .#Ez$tracer output_graph must not be Nonez*output_graph must have output_instructionsT)errorr   )torch.fx.experimental.validatorr  r  restartr`   ra   rX   r   r  output_instructionsupdatecode_optionsr4   r1   r0   r/   r&  _torch_dynamo_tracer_output)r   r   localsbuiltinsr  r   tf_mode_stackr  r  instructionsr0  r\  r  r  r  r  r  r*  r   r  r^  r  r)  r  s           `            @@@r   trace_framer6  w  s   ( W!#L"'!+#F(! !,*62++> !GHH)) !MNN 44QF//0(6(601A,1OPQ   (:6(N%s   BC( (	D	1DD	c                  z    e Zd ZU dZded<   ded<   ded<   ded	<   	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 d	 	 	 	 	 ddZy
)DynamoOutputao  
    Represents the core data returned from a single dynamo run, including:
      - Guards, wrapped inside tracer_output.output_graph.guards
      - Generated bytecode
      - Other information needed for compilation.
    This data structure should capture all the "interesting" information dynamo
    produces on the frontend side before it enters user backend.
    rX   r   types.CodeTyper   list[list[str] | None]pycodezfloat | Nonelast_attempt_start_timeNc           	         | j                   j                  }|t        d      t        ||||r|j                  nd |r|j
                  nd ||      S )Nz2output_graph must not be None when building guardssave_guardsstrict_error)r   r   r  rS   guard_fail_fnguard_filter_fn)r   r   r  saver  r@  r   s          r   build_guardszDynamoOutput.build_guards  s]     ))66 !UVV##(Ed%*E!!%
 	
r   c                   | j                   j                  }|t        d      t        t	        |j                         |j                  |j                  |j                  |j                        |j                  |j                  | j                  | j                  | j                   j                  ||| j                   j                  	      S )NzAoutput_graph must not be None when producing graph capture output)r   r   r  GraphCaptureOutputrY   dump_guards_stateimport_sourcesr'  export_metadatatracked_fakes_id_to_sourcetraced_coder   r;  r  rT  )r   argdefs
kwdefaultsr   s       r   graph_capture_outputz!DynamoOutput.graph_capture_output  s    
 ))66 S  "..0++&&,,77 ''$$MMKK&&((
 	
r   NFNFr   r9  r  zHooks | NonerC  r   r  zlist[CacheEntry] | Noner@  r   r   rS   NN)rL  tuple[Any, ...] | NonerM  dict[str, Any] | Noner   rF  )r   r   r   __doc__r  rD  rN  r   r   r   r8  r8    s     &%""))
 #15"

 
 	

 /
 
 

. +/,0
'
 *
 
	
r   r8  c                  D    e Zd ZU dZded<   ded<   ded<   ded	<   d
ed<   y)BackendInputa   
    Represents core data structure that dynamo will pass to a backend, including:
      - Graph module
      - Example inputs
      - The FakeTensorMode used for compiling graph.
    This data structure should capture all the information dynamo produces
    on for the user backend.
    rM  
backend_idtorch.fx.GraphModuler	  r
   example_inputsz,torch._subclasses.fake_tensor.FakeTensorModer   r   r   N)r   r   r   rT  r  r   r   r   rV  rV    s&     O&&;;**r   rV  )frozenc                      e Zd ZU ded<   ded<   ded<   ded<   d	ed
<   d	ed<   dZded<    ej                  e      Zded<   ddd	 	 	 	 	 	 	 	 	 ddZ	ddZ
	 	 	 	 	 	 ddZddZy)GraphRuntimeEnvr9  r   r:  r;  dict[str, str]rH  dict[str, Any]used_globalsrR  r  rL  NrS  rM  )default_factoryset[str]external_refsFextra_globalsuse_python_codegenc                  | j                   j                         D ci c]  \  }}|t        j                  |       }}}i || j                  |xs i ||i}| j                  |       |r| j                  ||      }	n| j                  }	t        j                  |	|| j                  | j                        }
| j                  r| j                  |
_        |
S c c}}w )N)r  rL  )rH  items	importlibimport_moduler_  _check_external_refs_build_python_wrapperr   typesr   r  rL  rM  __kwdefaults__)r   rW  compiled_fnrd  re  aliasmodule_namerH  rT  r   r,  s              r   forward_callablez GraphRuntimeEnv.forward_callable+  s     '+&9&9&?&?&A
"{ 9**;77
 



 "
 	
	 	!!),11)ZHH}}HLLLL	
 ?? $B	7
s    Cc                   | j                   D cg c]  }||	 }}|st        d      | j                  }|j                  }|j                  d| }dj                  |      }dg}|j                  d| d       |D ]  }|D ]  }	|j                  d|	          |j                  d       |j                  d	       |j                  d
       dj                  |      S c c}w )zCBuild Python source for the wrapper function from generated pycode.Nz%No Python code generated from Dynamo., zdef __generate_func__():z    def __dynamo_func__(z):z        z        return __retz    return __dynamo_func__z/__dynamo_generated_func__ = __generate_func__()
)r;  RuntimeErrorr   co_argcountco_varnamesrw  r   )
r   ppycode_parts	orig_codeargcountvarnamesargnames
func_linespartr   s
             r   _build_python_wrapper_sourcez,GraphRuntimeEnv._build_python_wrapper_sourceP  s    #';;@a!-@@FGGMM	(((((399X&01
4XJbAB  	5D 5!!HTF"345	5 	0167KLyy$$! As
   C C c                |    | j                         }|j                         }t        ||       |d   }|j                  S )z=Build a Python code object from the generated pycode strings.__dynamo_generated_func__)r  copyexecr   )r   rT  rW  func_source	namespacerd  s         r   rk  z%GraphRuntimeEnv._build_python_wrapperd  s?     779$-NN$4	 	[)$45}}r   c                x    g }| j                   D ]  }||vs|j                  |        |rt        d| d      y )Nz&Missing required external references: zM. Please load AOT compiled function with `f_globals=<enclosing global scope>`)rb  r   ru  )r   rT  missing_refsr   s       r   rj  z$GraphRuntimeEnv._check_external_refsr  s]    %% 	)C)###C(	) 8 G^ ^  r   )
rW  rM  rn  Callable[..., Any]rd  rS  re  r   r   r  )r   rM  )rT  r^  rW  rM  r   r9  )rT  r^  r   r   )r   r   r   r  rM  dataclassesfieldr   rb  rq  r  rk  rj  r   r   r   r\  r\     s    """"  ####(,J%,/k//DM8D 04#(## (#
 -# !# 
#J%('58	r   r\  c                    ddl }i }| j                         D ]  \  }}	 |j                  |       |||<    |S # t        $ r Y ,w xY w)zCFilter a builtins dict to only picklable entries for serialization.r   N)picklerg  dumpsr&  )builtins_dictr  r   kr8  s        r   _safe_builtins_dictr    sZ    F##% 1	LLOF1I M  		s   8	AAc                      e Zd ZU dZded<   ded<   ded<   ded	<   d
ed<   ded<   ded<   ded<   ded<   	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZddZedd       Zy)rF  z)
    Minimal version of DynamoOutput
    rY   r   r]  rH  zlist[CodeType]rK  r   r   r:  r;  rR  r  rL  rS  rM  r^  rT  Nc           	     r    t        || j                  ||r|j                  nd |r|j                  nd ||      S )Nr>  )rS   r   rA  rB  )r   r   r  rC  r  r@  s         r   rD  zGraphCaptureOutput.build_guards  s@     $#(Ed%*E!!%
 	
r   c           
     r   ddl m} i }| j                  j                  j                  j                         D ].  } ||      }||| j                  v s| j                  |   ||<   0 | j                  | j                        }dd l	}|D ]^  }||vs|j                  d      r*|| j                  v rt        | j                  |         ||<   Ct        ||      sPt        ||      ||<   ` t        | j                  | j                  | j                   || j"                  | j$                  | j&                  |      S )Nr   )get_global_source_name__builtins_dict__)r   r;  rH  r_  r  rL  rM  rb  )torch._dynamo.sourcer  r   rI  graph_input_idx_to_local_sourcerL  rT  _get_external_refsr   r3  rU  r  r   r?  r\  r;  rH  r  rL  rM  )r   r  r_  sourceglobal_namerb  	_builtinsr   s           r   get_runtime_envz"GraphCaptureOutput.get_runtime_env  s+   ? ..NNUUW	H08K"dnn,,0NN;,G[)	H //> 	%  	@C,&>>"563$..;P(;DNN3<O(PL%Y,(/	3(?L%	@ ]];;..%LLLL'	
 		
r   c                0   dd l }t               } |j                  |       D ]r  }|j                  dk(  r)|j                  s|j                  |j                         ;|j                  dk(  sK|j                  sX|j                  |j                         t |S )Nr   LOAD_GLOBAL	LOAD_NAME)disr   get_instructionsopnameargvalr   )r   r  rb  instructions       r   r  z%GraphCaptureOutput._get_external_refs  s    "%% 03//9 	:K!!]2%%!%%k&8&89##{2%%!%%k&8&89	: r   rO  rP  )r   r\  )r   r9  r   ra  )	r   r   r   rT  r  rD  r  staticmethodr  r   r   r   rF  rF    s     $#""""####%%
 #15"

 
 	

 /
 
 

$$
L  r   rF  c                  F    e Zd ZU dZded<   ded<   dddd	 	 	 	 	 	 	 d
d	Zy)CaptureOutputa  
    CaptureOutput should represent all the information produced from torch
    compiler for a single graph capture. This intends to be consumed by
    various compiler frontends so that we can share as much compiler internals
    as possible and avoid great divergence between different stacks.
    This data structure should eventually contain all the information compiler
    produces as more refactors happens to converge different compiler
    frontends.
    rF  rN  zBackendInput | Nonebackend_inputNF)rn  rd  re  c                   | j                   j                         }| j                  t        d      | j                  j                  }|xs | j                  j
                  }|j                  ||||      S )Nzbackend_input must not be Nonerc  )rN  r  r  r  rW  r	  rq  )r   rn  rd  re  runtime_envrW  s         r   rq  zCaptureOutput.forward_callable  s{     //??A% !ABB''22
!DT%7%7%D%D++'1	 , 
 	
r   )rn  zCallable[..., Any] | Nonerd  rS  re  r   r   r  )r   r   r   rT  r  rq  r   r   r   r  r    sM     -,&&
 26/3#(
 /
 -	

 !
 

r   r  c                :   ddl }t        | t        j                  j                        r+| j
                  }t        |d      r|j                  }| j                  }t        |d      r|j                  }t        | j                        dk(  rt        | j                        dk(  rtt        t        j                  j                  j                  j                        dk(  r9t        t        j                  j                  j                  j                        dk(  rt        | j                         dk(  rt        | j"                        dk(  rt        t        j                  j                  j                  j$                        dk(  rt        t        j                  j                  j                  j&                        dk(  r[|t        j                  j                  j
                  k7  r4|t        j                  j                  j                  k(  r| j
                  } n=t        | t        j(                  j*                        r| j,                  } n| j                  } t        | d      r| j                  | j.                  fS  |j0                  |       r| dfS t3        d|        )z
    Utility function to get the function to trace, and optionally a bound self
    object, from a callable (nn.Module, function, or method).
    r   N__self__zUnsupported model code type )inspectr@  r   rB  rC  forwardr   __func__r  len_forward_pre_hooks_forward_hooksmodulesmodule_global_forward_pre_hooks_global_forward_hooks_backward_pre_hooks_backward_hooks_global_backward_pre_hooks_global_backward_hooksr  GraphModule
_call_implr  
isfunctionru  )modr  resolved_forwardresolved_calls       r   get_traced_fnr    s   
 #uxx';;#Z0/88=*-)22M
 &&'1,C&&'1,EHH$$++EEF!KEHH$$++AABaGC++,1C''(A-EHH$$++FFG1LEHH$$++BBCqH EHHOO$;$;;!9!99 ++CUXX112..C,,CsJ||S\\))			C	 Dy9#?@@r   c                0    t        j                  | d      S )NF)follow_wrapped)r  	signature)r,  s    r   _get_signaturer  ?  s    R66r   	FrameInfoc           	        ddl }t        |       \  }}||f|z   }|i }t        |      } |j                  |i |}|j	                          |j
                  }|j                  xs d}	|j                  j                  }
|
s|	rtt        |	      t        |
      k7  r$t        dt        |	       dt        |
       d      |j                  t        |
|	      D ci c]  \  }}||j                   c}}       t        |j                  |j                  ||j                   |j                  xs d|j"                  |j$                        S c c}}w )zL
    Create a frame to trace, given a model, args, and optional kwargs.
    r   Nr   zclosure length (z) must match freevars length ())r  rL  rM  )r3  r  r  bindapply_defaults	arguments__closure__r   co_freevarsr  r  r/  zipcell_contentsr  __globals____dict____defaults__rm  )r  r  r  r3  r,  self_optr  bound_argumentsrV  r  freevarsr   cells                r   
_get_framer  C  s@     %LB{T!~r"I$innd5f5O""$((Hnn"G{{&&H7w<3x=( "3w<.0NsS[}o]^_  	8;Hg8NO*$T4%%%O	
 

$"$$  Ps   E
constraints _is_export_deprecated_do_not_usec                   t        | ||      }t        t        t        i                   5  t	        |||      cddd       S # 1 sw Y   yxY w)a  
    This API captures a full graph for a model, given example inputs to trace with.

    Specifically, it takes a callable (nn.Module, method, or function), args, and
    optional kwargs, and returns Dynamo-captured graph along with other important
    compile-time information. This serves as the common graph-capture mechanism
    for different torch compiler AOT frontends (e.g. AOT precompile, export).

    Note that this API doesn't apply context managers like metrics context,
    and the expectation is that the caller will apply them depending
    on the use case.

    The CaptureOutput is separated into two parts:
    1. Frontend specific information, which includes:
        - guards
        - generated bytecode
        - other information tracked by OutputGraphCommon.
    2. Backend specific information (indexed by unique backend id) such as:
        - fx graph
        - example inputs
    r  N)r  r   r   r  _fullgraph_capture_frame)r  r  r  r  r  r   s         r   fullgraph_capturer  n  sJ    : sD&)E	r(:;	< 
'#-M

 
 
s   AAc                  T    e Zd ZU ded<   ded<   ded<   ded<   ded<   d	ed
<   ded<   y)r  r9  r   dict[str, object]r   r2  r3  tuple[CellType]r  rR  rL  rS  rM  N)r   r   r   r  r   r   r   r  r    s,    
##%%r   c               \   ddl m d 	 	 	 	 	 	 dfd}	 t        | j                  | j                  | j
                  | j                  | j                  |||dt               
      }t#        |j%                  | j&                  | j(                              S # t        t        t        f$ r}t        |       t        j                  r |}|j                  4|j                  j!                  d        |j                  }|j                  4|j!                  d       |j                  d }~ww xY w)Nr   )TracingContextc                ,   j                         }|j                  }|j                  }|t        d      t	        | j
                  d   t              s$t        dt        | j
                  d                t        | j
                  d   | |||      | S )Nzfake_mode must not be NonerW  z)gm.meta['backend_id'] must be a str, got )	r  r   r   r  r@  metarM  rD  rV  )gmrY  r   r   r   r  r  s        r   fullgraph_compilerz4_fullgraph_capture_frame.<locals>.fullgraph_compiler  s     ),,.#--	+== !=>>"'',/5 ;DAV<W;XY  %GGL!2~yBS
 	r   T)r   r\  r  r  restart_reasons)r  rX  rY  zlist[torch.Tensor]r   rX  )torch._guardsr  compile_framer   r   r2  r3  r  r   rP   rN   rQ   rE   r)   verbose	__cause__with_tracebackr  rN  rL  rM  )	r   r  r  r  dynamo_outputr^  cur_exnr  r  s	          @@r   r  r    s    -)-M 2D	$6%JJMMLLNNMM*3*E
2 **5==%:J:JK  5yA 
6A>>!"+,,T2''G + t$!++5
6s   AB D+,AD&
D&&D+ConvertFrameProtocolc                P     t         dd       }||S ddd fd}| _        |S )N_dynamo_fail_callbackc                     t        d      )NzDynamo: expected not to compile nested code - this happens because a Dynamo callback was triggered and succeeded in compiling when running fullgraph=True compiled code.)ru  )r  r  s     r   compile_frame_errorz.get_fail_callback.<locals>.compile_frame_error  s    9
 	
r   c                 z    t        j                  t        dz         5   | i |cd d d        S # 1 sw Y   y xY w)Nz.compile_frame)mockr  r   )r  r  callbackr  s     r   fail_callbackz(get_fail_callback.<locals>.fail_callback  s:    ZZ#335HI 	-T,V,	- 	- 	-s   1:)r  r
   r  r
   r   r   )r  r
   r  r
   r   re   )r?  r  )r  r  r  s   ` @r   get_fail_callbackr    s8    H&=tDM 
-
 &3H"r   c               z   	
 t               	 	 	 	 	 	 d 	
fd}d}t        j                         D ]  }|t        j                         _        	 t        d| d      5  t         |      \  }}|t        d      |j                  t        d      t        |||j                  j                  |      cddd       c S  y# 1 sw Y   nxY w# t        j                  $ r}t        |t        j                        st!        j"                          t$        j'                  d	t)        t*        |j,                               t/        |d
d      }|r|j1                          |j3                  |j4                  xs t7        t9        |                   t;        j:                         }|dkD  rt=        dddg        Y d}~d}~wt        j>                  $ rS}t        |t        j                        st!        j"                          t/        |d
d      }|r|j1                           d}~ww xY w)a  
    A helper function taking a frame and backend, then return the generated bytecode
    and guards as a common data structure.
    This is a shared interface for multiple compiler frontends (e.g. torch.compile,
    torch.export) that needs to capture a graph out of python code.
    c                    t        t        j                        }t        || t	        t
        |      	
      }|t        d      |S )Nr  z.tracer_output must not be None after transform)r   r   r   r6  r   rW   r  )r5  r0  r4  r   r3  r  r   r   r  r\  r  r  r   r2  r  r  r  s       r   	transformz compile_frame.<locals>.transform  sx     BF/55B
 $l+1#/!
&   !QRRr   Ncompile_attempt_Tlog_pt2_compile_eventz:tracer_output must not be None after transform_code_objectzGtracer_output.output_graph must not be None after transform_code_object)r   r   r;  r<  zRestarting analysis due to %sr1  d   z!Excessive RestartAnalysis() callsr  zDynamo attempted to trace the same frame 100+ times. Giving up on compiling as the compile time tradeoff is likely not worth the performance gain.r  )r5  list[Instruction]r0  r^  r   rX   ) rc   r   countr   r  attemptrn   r5   r  r   r8  output_pycoder+   RestartAnalysisr@  r$  r   r   r  r  rv   r'   __traceback__r?  _cleanup_output_graphr   restart_reasonrM  rD  r}  rO   r%  )r   r   r2  r3  r  r   r  r  r\  r  r  r  r  r  r<  r  r   r   r^  failed_tracer_outputr  s   ``````` `````       @r   r  r    s   0 %&O'7E	  : #??$ 3'.$0	 0	:RVW *?i*P'- ((T  !--5(a  $"/%(55CC,C	 	3    "" 	a!C!CD$$&HH/11??C
 $+1.KT#R #$::< 0 0 @CQLA&*iik#}?!2  }} 	a!C!CD$$&#*1.KT#R #$::<	s?   #C$3AC		C$C 	C$$H:7CGH:'AH55H:)r   r  r  c               @  
?@ABC ddl m}m} dAt        d      	 	 	 	 	 	 	 	 d@?fd       }t        	 	 	 	 	 	 	 	 dA
@ACfd       ?t               }j                  |       nt        j                         }t        t        j                        5  t        t                    5  t        dd	d	
      5  t        d      j!                         5  |5  t#        ddd      5  |5  t%               Ct'               x}rt)        |t+                     @nd @d }t-        |      r!|rt/        ||t1                    }|sdn|d   }|j3                  |dd       t5               }|r)|D ch c]  } |       d d  }}|j%                  d|       t7        |      \  }B|rDdBd} t8        j;                  dBt=        t        B       | |       |t>               dCBfd}!	  |!        t8        jO                  d| jP                  | jR                  | jT                  |dz   djW                  tY        jZ                  d|z         j]                                      t_        | |      }#ta        jb                         }$d }%d }&d }'d }(d })td        jf                  jh                  jj                  jm                          d }*d }+t        jn                  s t        jp                  st        jr                  rsddl:m;},m<}-m=}.  |,t        jn                        x}/rt}        |/       |-t        jp                        x}/rt}        |/       |.t        jr                        x}/rt}        |/      	  || ||	      \  }*}+t                |+r9|+j                  x}0r+|0j                         rt        |        |rd |v r
t                t        |+       |*td        jf                  j                  j                  r@t#        d!d"#      5  t8        j                  d$       t        j                  d       d d d        d }1|+r|+j                  }1|1ri |1_I        |1j                  x}2ri |2_K        dd%l4mL}3 t        |3      }4|&|1t        |1j                        }5t        |1j                  j                        }6|1j                         }7t        |1j                  j                        }8|1j                         }9t        |1j                        }:|1j                  D ;ch c]  };|;j                   }<};|1j                  D ;ch c]  };|;j                   }=};td        jf                  jh                  jj                  j9                          nFd }5d }6d }7d }8i }9d }:t%        i       }<t%        i       }=t%               Cta        jb                         |$z
  d&z  Ai d'|4d(| jP                  d)| jR                  d*| jT                  d+|j                  d,|j                  d-|5d.|6d/|7d0|8d1|:d2|%d3|&d4|(d5|)d6|<d7|=CA|*d ut        j                  d	t        A      |#t        |9      |'d8	}>|j3                  |>       |r|+r|+j                  n	t               |_]        |+Z|+j                  N|+j                  j                  s8|+j                  j                  j                  j                  jm                          t        |+       cd d d        cd d d        cd d d        cd d d        cd d d        cd d d        cd d d        S c c}w # t@        $ r]}"t        jB                  rtE        d      |"|rtE        d      |"tG        tH        jJ                  tH        jJ                        |"_&         d }"~"ww xY w# 1 sw Y   qxY wc c};w c c};w # t        $ r$}"t        |"      j                  }%t        |"      }&t        j                         g}'t        |"| |9       td        j                  j                  d:d; d< =       t        j                  |"      \  }(})t=        |"d>d       }+t        |"t@        t        t        t        t        t        t        t        |t        |t        t        t        f      r t        t        |"      j                   d?t        |"             j                  |"j                        d d }"~"ww xY w# td        jf                  j                  j                  rMt#        d!d"#      5  t8        j                  d$       t        j                  d       d d d        n# 1 sw Y   nxY wd }1|+r|+j                  }1|1ri |1_I        |1j                  x}2ri |2_K        dd%l4mL}3 t        |3      }4|&|1t        |1j                        }5t        |1j                  j                        }6|1j                         }7t        |1j                  j                        }8|1j                         }9t        |1j                        }:|1j                  D ;ch c]  };|;j                   nc c};w }<};|1j                  D ;ch c]  };|;j                   nc c};w }=};td        jf                  jh                  jj                  j9                          nFd }5d }6d }7d }8i }9d }:t%        i       }<t%        i       }=t%               Cta        jb                         |$z
  d&z  Ai d'|4d(| jP                  d)| jR                  d*| jT                  d+|j                  d,|j                  d-|5d.|6d/|7d0|8d1|:d2|%d3|&d4|(d5|)d6|<d7|=CA|*d ut        j                  d	t        A      |#t        |9      |'d8	}>|j3                  |>       |r|+r|+j                  n	t               |_]        |+Z|+j                  N|+j                  j                  s8|+j                  j                  j                  j                  jm                          t        |+       w xY w# 1 sw Y   nxY wd d d        n# 1 sw Y   nxY wd d d        n# 1 sw Y   nxY wd d d        n# 1 sw Y   nxY wd d d        n# 1 sw Y   nxY wd d d        n# 1 sw Y   nxY wd d d        y # 1 sw Y   y xY w)DNr   )BisectValidationExceptionValidationExceptiong        compile_inner)
phase_namec           
     r   t        j                         5 }s-|j                  t        j                  j                                |j                  t        j                  j                  j                  t        j                  t        t        j                                            |j                  t        j                                |j                  t                 | ||      }t        j"                  j%                         dk7  rt'        d      |cd d d        S # 1 sw Y   nxY wt)               d fS )Nr   zQTorch function mode stack state changed while dynamo tracing, please report a bug)r
  r  r  r   compiler_compile_session_context_dynamocallback_handlerinstall_callbacksr   DYNAMOrM  r   r  rl   recordr   r   _len_torch_function_stackr  re   )r   r  r  r   r   _compile_innerr\  s        r   r  z_compile.<locals>.compile_inner  s     !!# 	u ##ENN$K$K$MN..@@#**C0Q0Q0S,T
  = D D FG CD#D)U;Fxx113q8$g  +	 	 	0  
 	
s   DD!!D*c                     t        j                          x}}	 	 	 	 	 	 	 	 	 	 	 	 d.d} |d j                   j                   j                          d j                  v }|r(t        j
                  j                  d fd fd       d dd	lm}  | t        j                        }	 |rt        j                  |      nt        j                         5  t         '(!|*$%&")
      }	d d d        ""j,                  t)        d      	j.                  |	j0                  }|	j2                  |	j2                  }t)        d       |d j                   j                   j                         |r(t        j
                  j                  d fdfd       |j4                  t)        d      |j4                  }t7        |j9                               t;        j<                        t>        <   tA        tB        jE                               D ];  \  }}tG        d| d      5   |       }||urtI        |       |d d d        =  tJ        <   tL        jO                         ||z
  #ddl(m)} |jT                  v rddl+m,}  |       d/d}t)        d       |       } |      } jZ                  d | jZ                  d | k7  r8d}|d jZ                  d |  dz  }|d jZ                  d |  z  }t)        |       j\                  j\                  k7  r2d!}|d" j\                   dz  }|d#j\                   z  }t)        |       j^                  j^                  k7  r2d$}|d% j^                   dz  }|d&j^                   z  }t)        |      |j`                  r|jc                         rt+        d'      |fS |jd                  t)        d(      |jf                  th        jj                  <   t        jl                         }tn        jp                  r#|js                  to        jt                                tG        d)d      5  |5  |	jw                   |)d u*      }d d d        d d d        )}jx                  t)        d+      )j{                  |jx                         )j}                  |j~                  j                         )j                  |j                  j                          t               nd,}d-|z   }t        j                   |      }|jc                         s'|j                  |j                  |jd                         t        |      |fS # 1 sw Y   xY w# t        j                   $ rT}
|rt"        j%                  d       |
j&                  t)        d      d t+        d|
       |
j&                  fcY d }
~
S d }
~
ww xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w)0Nc           	         t         j                  t        j                        r#t         j	                  t        | ||||             y y r   )bytecode_logisEnabledForloggingDEBUGdebugro   )prefixr   r   line_nor   s        r   log_bytecodez6_compile.<locals>._compile_inner.<locals>.log_bytecode  s9     ((7""#FD(GTJ 8r   zORIGINAL BYTECODEtorch_dynamo_resume_inartifactc                 (     j                   dz   ddS )N_ORIGINAL_BYTECODEstringr   encodingr   r  s   r   r   z2_compile.<locals>._compile_inner.<locals>.<lambda>       LL+?? (% r   c                 J    t        j                         j                         S r   r  Bytecoder  s   r   r   z2_compile.<locals>._compile_inner.<locals>.<lambda>  s    3<<#5#9#9#; r   metadata_fn
payload_fnr(   ))get_dynamo_config_override_for_compile_idr  z,No graph captured with export/fullgraph=Truez=SkipFrame exception must have _torch_dynamo_tracer_output setz0Dynamo decided to skip the frame while tracing: r  z;compiler collective wasn't run before compilation completedz+out_code must not be None after compilationzMODIFIED BYTECODEc                 (     j                   dz   ddS )N_MODIFIED_BYTECODEr'  r(  r*  r  s   r   r   z2_compile.<locals>._compile_inner.<locals>.<lambda>  r+  r   c                 J    t        j                         j                         S r   r-  )out_codes   r   r   z2_compile.<locals>._compile_inner.<locals>.<lambda>  s    3<<#9#=#=#? r   z+tracer_output.output_graph must not be Nonebytecode_hooks_Tr  )BREAKPOINT_MARKERr   )register_breakpoint_codec                    dd l }| j                  | j                  z   t        | j                  |j
                  z        z   t        | j                  |j                  z        z   S )Nr   )r  rv  co_kwonlyargcountr   co_flags
CO_VARARGSCO_VARKEYWORDS)r   r  s     r   
count_argsz4_compile.<locals>._compile_inner.<locals>.count_args-  s]       (()t}}w'9'99:; t}}w'='==>?r   z8out_code must not be None after bytecode hook processingzarg mismatch: zold code object has args rs  znew code object has args zfree var mismatch: zold code object has free var znew code object has free var zcell var mismatch: zold code object has cell var znew code object has cell var z#export mode produced an empty graphzoutput.guards must not be NonerD  )r  rC  r  z&check_fn.guards_state must not be NoneUnknownzTorch-Compiled Region: )r   rM  r   rM  r   rM  r!  rN  r   r   r   r   r   r   r   rN  )Ir}  r   r   r   r   r   r   graph_id_filterr2  r)   debug_dynamo_config_overrider  r
  r  r  r+   r%  r  r  r1  r  re   
all_statesr   r   r<  r   rI  $get_replayed_side_effect_source_refsr:   r  r  	enumerater  rL  rn   r  ry   r  r   bytecode_debuggerr8  	co_conststorch._C._dynamo.eval_framer9  rw  r  co_cellvarsr\  is_empty_graphr  cleanupsrk   instancer  r   r   r  temp_restore_stackrD  guards_stateadd_guarded_codeadd_inlined_sourcer   rK  update_device_typecurrent_tracergraphrM  rU   guard_managerguard_export_fnrh   )+r   r  r  r<  
start_timer"  is_resumption_framer2  dynamo_config_overrider  r^  r   r  r   r  hook_outputr8  r9  r?  total_argcount_oldtotal_argcount_newmsgbuild_guards_ctxcheck_fncompile_id_strannotation_strguarded_coder6  r3  r  r  r  r  r   r  dynamo_time_before_restartr\  r  r  r   r2  r  r  s+   `                          @r   r  z _compile.<locals>._compile_inner  s    04yy{:*		"	.1	<?	GO		 	LL	
 7$,,FNN++ < ,  N!J;;"
!	 * 34++-
 !.#!'9 +&7#!D (->-I-I-Q M  !))%3300<&3&K&K# !NOOLL	
 NN++ @ ,  %%- !NOO++&==?@ 	  *+RS #?#9#9#;< 	+ICu5TR +"42*"(2*8[A*H+ +	+ #'h"%<z%I"8 2 22L$X.	  J  (-'1001##$7%789 #C.t/?/?@SAS/T.UUWXXC+H,@,@ATBT,U+VWC !%%x333'C243C3C2DBGGC283G3G2HIIC %%x333'C243C3C2DBGGC283G3G2HIIC %% ==V224"/TU 
 ==  !ABB,2OO) &//1.44**3FFH .E 	GW 	$11D(+	 2 H	 	 $$,$%MNN$$X%:%:HE&&v'='='I'IJ&&v'<'<'B'BC,6,BZ	2^C"""	
 $$&5+@+@+L !!&--0 .==A ( }} 			HI,,4$S #"RSTRU V --	 	h+ +p	 	 	 	sg   ,+U? U20U? +W)XW63X2U<7U? ?W&A	W!W&!W&)W3	6X 	;XXr  T)reset_event_log_on_exitr  z+pytorch.wait_counter.entire_forward_compilez_compile.compile_innerentire_frame_compile!dynamo_cumulative_compile_time_us)r  dynamo_compile_column_usz$Unable to find recompilation reasonsF)recompile_reason#inline_inbuilt_nn_modules_candidate   recompile_user_contextsc                V    d| j                    d| j                   d| j                   dS )N'z' (r  r  )r   r   r   r  s    r   format_func_infoz"_compile.<locals>.format_func_info  s0    4<<.D,<,<+=Qt?R?R>SSTUUr   ztorch._dynamo hit config.%s (%s)
   function: %s
   last reason: %s
To log all recompilation reasons, use TORCH_LOGS="recompiles".
To diagnose recompilation issues, see %sc                 p    t        dd  d  dt        t                dd  ddt         d	g
       y )NzDynamo recompile limit exceededzLimit type: zLDynamo attempted to recompile the code object too many times, exceeding the z$ cache size limit (currently set to zj). Excessive recompilations can degrade performance due to the compilation overhead of each recompilation.zTo monitor recompilations, enable TORCH_LOGS=recompiles. If recompilations are expected, consider increasing torch._dynamo.config.z to an appropriate value.zSee z) for tips on dealing with recompilations.r  )rO   r?  r)   r}   )
limit_types   r   (raise_unimplemented_cache_limit_exceededz:_compile.<locals>.raise_unimplemented_cache_limit_exceeded  sj    =*:,7!%%/L0TU\]ceoUpTq rY!Y
;;E,F_a 233\]	r   z/Hard failure due to fail_on_recompile_limit_hitz"Hard failure due to fullgraph=TruezBtorchdynamo start compiling %s %s:%s, stack (elided %s frames):
%s   r  r   r(   )_validate_backend_names_validate_dynamo_config_keys_validate_inductor_config_keyszsize mismatch at indexgc
gc_time_us)rg  z run_gc_after_compile: running gc)
curr_frameg    eA	frame_keyr   r   r   r  r  guard_countshape_env_guard_countgraph_op_countgraph_node_countgraph_input_count	fail_typefail_reasonfail_user_frame_filenamefail_user_frame_linenonon_compliant_opscompliant_custom_ops)	r  dynamo_time_before_restart_shas_guarded_codespecialize_float
is_forward%dynamo_compile_time_before_restart_usstack_tracegraph_node_shapesexception_stack_tracer[  r$  c                     dddS )Ndynamo_errorr'  r(  r   r   r   r   r   z_compile.<locals>.<lambda>X  s    * (% r   c                 *    t        j                         S r   	traceback
format_excr   r   r   r   z_compile.<locals>.<lambda>\  s    9#7#7#9 r   r/  r1  z: )r   r   r  r   r  rV   r   z4tuple[ConvertFrameReturn, DynamoTracerOutput | None])r   r   r  r   r  rV   r   z-tuple[ConvertFrameReturn, DynamoTracerOutput])r   r   r   rM  )r   r   ){r,  r  r  r   re  rr   r:   r
  r  r   r)   use_lazy_graph_moduler   r   rj   r    guardrn   r   r   r_   rb   r9   rT   rB   update_outerrq   r8   r  r  r?  r}   rP   fail_on_recompile_limit_hitrG   rg   rf   RUN_ONLYframe_exec_strategyr  r   r   r   rw  r&   r   formatr   r}  time_nsr   r  utilsReinplaceCountersr   debug_backend_overriderC  debug_inductor_config_overriderB  rs  rt  ru  r  r\   r   has_outputsr[   rZ   r   run_gc_after_compiler  rv  collectlocal_scoperoot_txrV  rx  rM  r  r  r'  count_callsrT  nodesget_graph_sizes_structuredplaceholdersr  r   r  r  r  r  r|   r  ri   r\  r   guards_contextdynamo_guardsr&  rD  r  r  r_  r   r   r+   get_exc_messager@  rQ   rM   rF   r  
IndexErrorr   r   rN   rL   rJ   rK   rI   r  r  )Dr   r   r2  r3  r  r   r  r\  r  r  r  r  r  r  r   r  r  r   r  r  r  r  r  metrics_contextpackage_code_context
compile_pgrh  reasonsrk  user_contextuser_contexts_msgexceededrn  rq  r^  r  start_time_nsr  r  r  r  r  rb  r   rs  rt  ru  errr   r  r)  rx  ry  rz  r{  r|  r}  r  r~  opr  r  metricsr  r  rc  rp  r  sD    ````` `` ``   `` `                                            @@@@@r   r  r  a  sn   2 ),"o>

#'
05
	=
 ?
> j>j>j> j> 
7	j> j> j> j>X *+O&-&9T"z?U?U?W  	v;;<RGz23RG 	d$	
RG 	BCIIKRG 	RG 	$-%H	
RG 	RG %(E')):) 0Z\ J $ (,J'E=)52CK2PG ?F67ST:  	$$$47<	
 #F"G" :Q!)5t$! !  9;LM6z:N*V
 KK;
 
+ & # 8:" 			QLL1HGG%--1t8<CCEF	
4 'tT2 $	"&26/3 -1--335 ))2244
  .f.K.KLLsL o%263V3VWWsW o%455 s  !o%q	G*7i*O'L- %2%?%??\? ,,.+D1#(@DT(T02*=+Fj }}##88!$N "HH?@JJqM" F&33%'"#^^+6+&(FO)JI"v'9!&--0(+F,<,<,C,C(D%!'!3!3!5#&v||'9'9#: $*$E$E$G!$'(;(;$<!?E?W?W$XR__$X!$X.4.I.I((*BOO($ ( ##5599;"(,%!%#' $&!$(!$'G!'*2w$"%%.2lln}.LPS-S*Y4<< t// !$"5"5	
 jTT )**F*F { ()> !. #$4 $%6 Y { +,D )*@  $%6!" '(<#$ $30J$0$<$*$;$;"9B.:  +%():%;)>9GB ((1 ! % "6624 "6 )!..:%2299**::IIWW]]_*=+Fe
RG RG RG RG RG RG RG RGR!X  551I 1<
 ->#,,k.B.B-A) r" "2 %Y(_  -	< Q,,Ia&K%.%9%9%;$<!auV<NN++ : ,  @C?R?R:@<$&< $A'DdKM%)",/'0-$ .&  /Aw++,Bs1vh7 .1t<W-	<h }}##88!$N "HH?@JJqM" " " F&33%'"#^^+6+&(FO)JI"v'9!&--0(+F,<,<,C,C(D%!'!3!3!5#&v||'9'9#: $*$E$E$G!$'(;(;$<!?E?W?W$XR__$X$X!$X.4.I.I((*BOO( ($ ( ##5599;"(,%!%#' $&!$(!$'G!'*2w$"%%.2lln}.LPS-S*Y4<< t// !$"5"5	
 jTT )**F*F { ()> !. #$4 $%6 Y { +,D )*@  $%6!" '(<#$ $30J$0$<$*$;$;"9B.:  +%():%;)>9GB ((1 ! % "6624 "6 )!..:%2299**::IIWW]]_*=+Fe
RG RG RG RG RG RG RG RG RG RG RG RG RG RG RG RG RG RG RG RGs  )r>q?q*'q	*q 9p+<B p<\%A!p/\*6E2p)A^*1p9+^$C#p^ p*^%=F(p%	p+.	q 7	q	 	q*		q?	r%p*	^3A^^p^p*
c4Dccc2p+e8	pe
C"p(h<;pi%$F/pppp+"	q +p40q 7	q	 q	q		q*qq*!	q?*q3/q?6	r?r	rrc                  `    e Zd Z	 	 d	 	 	 	 	 	 	 	 	 ddZedd       Z	 d	 	 	 	 	 	 	 	 	 	 	 d	dZy)
ConvertFrameNc                V    || _         t        |d||      | _        || _        || _        y )NF)r  r  r  )r1  r  _inner_convert_hooksr  )r   r   r  r  r  s        r   r   zConvertFrame.__init__  s6     *5&2+	
  /r   c                      fdS )Nc                H    t        | j                  j                        S r  )convert_framer  r  r  s    r   r   z2ConvertFrame._clone_with_backend.<locals>.<lambda>  s    }KK 11 
 r   r   r   s   `r   r  z ConvertFrame._clone_with_backend  s    
 	
r   c                   t         j                  |j                         t        d   dxx   dz  cc<   	 | j	                  |||||dz         }t        d   dxx   dz  cc<   |S # t
        $ re}t        |t              r | j                  j                  j                  d u}|t        d      d | j                  j                  j                  r t        |t              r t        |t        t        f      }	|j                  }
|	rt        |dd      st        j!                  t"        j$                        rt'        |d	      rt'        |d
      rt)        t+        |j,                              5  |j.                  }dj1                  t3        j4                  |            }t7        j8                  |
      }d| d| t:        j<                  j?                  dd fd       t        jA                  dtB        jD                         d d d        n# 1 sw Y   nxY wtB        jF                  s|	s t        |dd       }|j                  }
tI        ||
||      }|	rtJ        jM                  |d       ntJ        jO                  |d       t        |t6        jP                        r(|jR                  tU        |jR                  d      cY d }~S Y d }~nd }~ww xY wtU        d      S )Nframestotalr(   r   okz%error_on_graph_break must not be NoneloggedFr  
real_stackr  zGraph break: torch.compile cannot properly resume from this graph break, which results in a skip.
torch.compile will skip tracing the frame zN and fall back to eager.
The graph break occurred in the following user code:
r$  c                     dddS )Ndynamo_graph_break_reasonr'  r(  r   r   r   r   r   z'ConvertFrame.__call__.<locals>.<lambda>U	  s    (C,41 r   c                 6      dt        j                          S )Nrt  r  )user_stack_traces   r   r   z'ConvertFrame.__call__.<locals>.<lambda>Y	  s    2B1C2iFZFZF\E]/^ r   r/  T)exc_info
stack_infor]  )r  z9compilation failed with a custom frame execution strategy)r  r  z*an error was suppressed during compilationr  )+r  r   rR  rm   r  r&  r@  rK   r  r  r  rN   rP   rQ   r?  graph_break_logr  r  r  r   r   r   r  r  rw  r  format_listr+   format_frame_infor   r   r   r  r)   r  suppress_errorsrH   r  r  r  TorchDynamoExceptionr  re   )r   r   r  r  r  r   r   r^  r  	soft_failr   
user_stackuser_stack_formatted
frame_infor]  	error_msgr  s                   @r   r  zConvertFrame.__call__	  s    	%7#q(#k	(({E;TAX ) F Xt$)$M e	!78 ##((==TI ! $+$%LMSWW""''<<   !9:"1{I&>?I<<D 8U3#00?
 1l+<0H()EF %&\\
/1ww%11*=0, &)%:%:4%@
IIS UU346 ) 77&) (_ 8  (--,%)'-~~ . )  4 ))) &a):DAO<<D(D/5IIT2I5 1c667))5)()(=(= [ Ee	N "D
 	
s8   .A% %
K/C?K.BH	KH	B.KKKrQ  )
r   r   r  rV   r  r  r  r  r   r   )r   z*Callable[[WrapBackendDebug], ConvertFrame]r   r  r  r   r   r   r  r    s    
 *.&*00 0 '	0
 $0 
0" 
 
 y
y
 'y
 	y

 :y
 y
 
y
r   r  c                     t        | |||      S )zHTry to convert a frame into an FX graph, if error leave frame unmodified)r  r  )r  )r   r  r  r  s       r   r  r  	  s     '	 r   c                   ddl m} t        j                  }dt        _        t	        | d      5 }t        j                  |      }d d d        t        t        j                  j                  j                         t               j                                     |_
        t        j                  d      5  	 t        |j                  |j                  |j                   |j"                  |j$                  |ddd t'               d g g t)        dd      d i t+        dd      	       |t        _        	 d d d        y # 1 sw Y   xY w# |t        _        w xY w# 1 sw Y   y xY w)
Nr(   )eagerFrbr   *   i  )r  r  )r   r  r\  r  r  r  r  r  r  r   r  r  )backends.debuggingr  r)   replay_record_enabledopenr]   loadrK  r   chainr   rg  r*   r  r  r   r2  r3  r  rV   r6   r   )r   r  original_replay_valin_filer  s        r   replayr  	  s   ) 66#(F 	h	 / %%g./)//&..*>*>*@')//BSTUFN		(	(	/ ?	?!#'g  *,4Q:$b3G#( ,?F(-? ?	/ /4 ,?F(-? ?s0   D53E5A+E E5D>EEEc                    t         j                  dk  ryt        j                  |       D ]"  }|j                  dk(  s|j
                  dz  c S  t        d      )N)      r   RESUMErr  z$RESUME instruction not found in code)r  version_infor  r  r  offsetru  )r   insts     r   first_real_inst_idxr  	  sV    
'!$$T* $;;(";;!##$ =
>>r   c                  0    e Zd Zdd	 	 	 	 	 	 	 	 	 	 	 ddZy)r  r   r   c                    y r   r   )r   r   r  r  r  r   s         r   r  zConvertFrameProtocol.__call__	  s     !r   Nr  )r   r   r   r  r   r   r   r  r  	  sF     !! '! 	!
 :! ! 
!r   c                  @    e Zd ZddZ	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 ddZy)CatchErrorsWrapperc                V     t        j                  |      |        || _        || _        y r   )r/  r0  r1  r  )r   r  r  s      r   r   zCatchErrorsWrapper.__init__	  s$    !	!$')1&
r   c                    |j                   l|j                  `t        j                  d|j                  j
                  |j                   |j                  j                         t        |j                   |       |S )Nz#skipping: %s (reason: %s, file: %s))r  rb  r  r  rR  r   r   r=   )r   r   r   s      r   _handle_skipzCatchErrorsWrapper._handle_skip	  sd     )f.A.A.III5$$""((	 F..6r   c           	        |t        d      t        j                  |j                         t	        j
                  |j                  |      }t        j                  dk\  r#|j                  t        |j                        kD  }n"|j                  t        |j                        k\  }t        j                  rt               }n
t               }|s+|s)t        j                  s|rRt        | j                   dd      s;|rd}n|rd}nt        j                  rd}nd	}| j#                  t%        |
      |      S |j                  j&                  dk(  r|j                  j(                  dk(  s>|j                  j&                  j+                  d      r5|j                  j(                  dk(  r| j#                  t%        d
      |      S |j                  j(                  dk(  r|j                  j,                  dkD  rm|j                  j.                  rWt1        |j2                  j5                  |j                  j.                  d               r| j#                  t%        d
      |      S t6        j8                  j:                  j=                         dk(  rt?        j@                         }|rtB        5  ddl"m#}	  |	|jH                  | j                   j                         }
tK        | j                   d      st        d      | j                   jM                  |
jN                        }| j#                   |||| jP                  |      |      cd d d        S tB        5  tS               5  | j!                  ||| jP                  |d      }| j#                  ||      cd d d        cd d d        S # 1 sw Y   _xY w# 1 sw Y   nxY wd d d        y # 1 sw Y   y xY w)Nzframe_state must not be None)r   )r     r  Fz#frame has already started executingz%frame is in the Dynamo skipfiles listzDynamo tracing is disabledzSnon-infra torch dispatch mode present, this is not supported today in torch.compiler  r  __new__zcollections/__init__.py_makez/frame is a namedtuple __new__/_make constructorr   r   z9__init__ of a traceable wrapper subclass cannot be tracedddp_optimizer)DDPOptimizer)bucket_bytes_capbackend_compile_fnr  zJDDPOptimizer only supports callback fns that know how to clone themselves.r(   r   )*r  r  r   rR  r-   r  r  r  f_lastir  r)   #inline_torch_dispatch_torch_compiler#   r$   disabler?  r1  r  re   r   r   r  rv  rw  r%   rV  r  r   r  r  get_optimize_ddp_moder!   _get_active_ddp_modulecompile_lock"torch._dynamo.backends.distributedr  r  r   r  
compile_fnr  r"   )r   r   r  r  is_skipfilehas_started_executionshould_skip_for_dispatch_moder  
ddp_moduler  r  hijacked_callbackr   s                r   r  zCatchErrorsWrapper.__call__	  sP     !?@@%!''EBw&$)MM4G4U$U!$)MM5H5V$V! 55,L,N) @A * "~~- > >	5Q %CE:8  $$%7K%PRWXX LL$$
2u||7K7Ky7XLL$$--.GH$$/$$" Q 	  LL  J.((1,((-""5<<#;#;A#>? $$" [ 	  ==446/I0GGIJ! O$0)3)D)D+/+I+I+c+c%M #668M -h  66JJ)44 &
  ,,)%djj+V% .  	413 	433{DJJ! 4 F $$VU3	4 	4 	4/ .	4 	4 	4 	4 	4s1   %BNN?2N*	N?N'*N3	/N??ON)r  r  r  rV   r   r   )r   re   r   r   r   re   )r   r   r  r  r  r  r   re   )r   r   r   r   r  r  r   r   r   r  r  	  sT    
(1@	m4m4 'm4 :	m4
 
m4r   r  c                    t        | |      S r   )r  )r  r  s     r   catch_errors_wrapperr  J
  s     h..r   )r   z3torch._subclasses.fake_tensor.FakeTensorMode | Noner   r   )r   zDynamoTracerOutput | Noner   r   r   r   r   )r   rM  r   r^  r   zdict[str, str] | Noner   r   r  )r   r   r   rN  r   z	list[str])r,  Callable[_P, _T]r   r	  )r   r   r   r   )NF)
r^  r&  r   r   r   DynamoFrameType | Noner\  r   r   r   )rd  r	  r   r	  )r  r  r   r   r  )r   r   r  r   r\  r   r  r  r  r  r  r  r   r  )r   r9  r   ztuple[str, ...] | None)r   r9  r   r   )r  r9  r  r9  r   r   )r  r   r   r  )"r   r9  r   r  r2  r  r3  r  r  r  r   r   r4  z'list[torch.overrides.TorchFunctionMode]r  r   r  rc   r5  r  r0  rW   r\  r   r  r  r  +dict[str, int | FrameStateSizeEntry] | Noner  DistributedState | Noner  r  r   rX   )r  r^  r   r^  )r  r
   r   z"tuple[FunctionType, object | None])r,  r
   r   zinspect.Signature)r  r
   r  tuple[Any, ...]r  rS  r   r  )r  r
   r  r  r  rS  r  list[Constraint] | Noner  r   r   r  )r   r  r  r  r  r   r   r  )r  r  r   r  )r   r9  r   r  r2  r  r3  r  r  r  r   r   r  r   r  ra  r\  r   r  r  r  r  r  r  r  r  r   r8  rQ  )*r   r   r   r  r2  r  r3  r  r  r  r   r   r  r   r\  r   r  r  r  rV   r  r  r  list[CacheEntry]r  r  r  r6   r   r
  r  r  r  r   r   rN  r  r  r  zConvertFrameBox | Noner   re   )
r   r   r  rV   r  r  r  r  r   r  )r   rM  r   r   rA  )r  r  r  rV   r   r  (  rT  
__future__r   collectionsr
  r{  r  r  r/  rv  rh  r  r   r  ru  rj  r  r  r  rx  	threadingr}  r  rl  typingunittest.mockr  r   r   pathlibr   r   r   r   r	   r
   r   r   r   typing_extensionsr   r   r   torch._loggingtorch._C._dynamo.guardsr   torch._dynamo.callbackr   torch._dynamo.distributedr   torch._dynamo.symbolic_convertr   r  r   r   r   r   r   torch._utils_internalr   r   r   torch.fx._lazy_graph_moduler   %torch.fx.experimental.symbolic_shapesr   r   torch.fx.graph_moduler   r   torch.monitorr    torch.nn.parallel.distributedr!   torch.utils._python_dispatchr"   r#   r$   r%   torch.utils._tracebackr&   r'   r  r)   r*   r+   r,   r-   backends.registryr.   bytecode_analysisr/   r0   bytecode_transformationr1   r2   r3   r4   r5   r  r6   r7   r8   r9   r:   
eval_framer;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   graph_bytecode_inputsrR   r  rS   rT   rU   r  rV   r   rW   rX   rY   pgorZ   r[   r\   replay_recordr]   resume_executionr^   symbolic_convertr_   r`   ra   rb   rc   rd   re   rf   rg   rh   r  ri   rj   rk   rl   rm   rn   ro   rp   rq   rr   rs   rt   ru   rv   rw   rx   ry   rz   r{   r|   r}   r~   variables.torch_functionr   r  numpyr   ModuleNotFoundErrorTYPE_CHECKINGcollections.abcr   torch.utils.weakr   r   r  r   repro.after_dynamor   r   r   r   variables.builderr   	getLoggerr   r  r   getArtifactLoggerr  r  RLockr  r   r   r   r   r   r   r  r  r   r0  r   r   r2  #suppress_torch_distributed_warningsrX  r_  r  Counterr`  re  rc  r  r  r  r  r   torch.utils.hooksr  r  r  r	  r  r  r  r6  r8  rV  r\  r  rF  r  r  r  r  r  r  r  r  r  r  r  r  r  r  Protocolr  r  r  r   r   r   <module>r<     s  . #     
  	     	    
         !  > > / / ' !   4 2 4 9 M M % 
 ? Q & A  M E E 5 G   '      > 
  L L 
 + ;  " X X     0 J  
 
(4-'4@@6 g!~~//*E..228^L y T]t_	 	%B%	%?,?? 
?, ( iy04 - 4 *+JN	%2G ,DNb 11C 2CR %)	**
* "* 	*
 
* CV;CVCVCX @ XIX - - -50O Oh %)%)"&  #	
 #   & $ - ,7=( 8*F '
$6

  %)?C15%)#Q
QQ Q  	Q
 Q Q ;Q Q $Q $Q Q Q #Q =Q  /!Q" ##Q$ %Q Qh >
 >
 >
B + + +" $\ \ \~ X X Xv  
  
  
F.Ab7 %)(	(
( "( 	(\ %)$

 ,0-2$
	$

$
 "$

 )$
 '+$
 $
N & & & ,0-2	:: ): '+	:
 :|> %)?C15%)k
kk k  	k
 k k k k k #k =k /k #k  !kz %)?C!E
G& %) 15/E
G
E
GE
G E
G  	E
G
 E
G E
G E
G E
G #E
G E
G #E
G $E
G  0E
G *E
G "E
G  =!E
G$ %E
G& 'E
G( #)E
G. ./E
G0 1E
GPT
 T
t &*"&	 #  	
  ?D?	!6?? 	!@4 @4F/"/+0//eL  	Bs   U U&%U&