
    9i                   p	   % S r SSKJr  SSKrSSKrSSKrSSKrSSKrSSKrSSK	r	SSK
r
SSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKJr  SSKJr  SSKJrJrJrJr  SSKJrJrJ r J!r!J"r"  SSK#J$r$  SS	KJ%r%  SSK&r&SSK'r&SS
K(J)r)  SSK*J+r+  SSK,J-r-  SSK.J/r/  SSK0J1r1J2r2J3r3J4r4  SSK'J5r5  SSK6J7r7J8r8J9r9J:r:  SSK;J<r<  SSK=J>r>J?r?  SSK@JArB  SSKCJDrD  SSKEJFrF  SSKGJHrHJIrIJJrJ  SSKKJLrLJMrM  SSKNJOrOJPrPJQrQJRrRJSrS  SSKTJUrUJVrV  SSKWJXrXJYrYJZrZJ[r[J\r\  SSK]J^r^J_r_J`r`Jara  SSKbJcrcJdrdJereJfrf  SSKQJgrgJhrhJiriJjrjJkrkJlrlJmrmJnrnJoroJprpJqrqJrrrJsrsJtrt  SSKuJvrvJwrwJxrx  SS KyJzrz  SS!K{J|r|  SS"K}J~r~Jr  SS#KJr  SS$KJr  SS%KJrJrJrJrJr  SS&KSJr  SS'KJrJrJrJr  SS(KJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJrJr  SS)KJr  S*\S+'    SSKr\GRX                  (       a"  SS,KJr  SS-KJr  SS.KJr  SS/KJrJrJr  SS0KJr  \
GRp                  " \5      r\&GRv                  GRy                  \S15      r\&GRv                  GRy                  \S25      r\GR~                  " 5       r\!" S35      r\$" S45      r " S5 S65      r " S7 S85      r\" 5       r\" 5       rSqS9\S:'   \GR                  " \B5       Sj       SkS; jj5       rSlSmS< jjrSnS= jr\fGR                  SoS> j5       r  Sp         SqS@ jjrSq\GR                  " 5       rSA\SB'   SrSC jrSrSD jr\ " SE SF5      5       r    SsSG jr " SH SI5      r    St           SuSJ jjrSSKKJr  SSLKJr  \" 5       rSM\SN'   SvSO jr\S?SSSSSP.                                 SwSQ jj5       r\ " SR SS5      5       r\ " ST SU5      5       r\ " SV SW5      5       r\ " SX SY5      5       rS?SZ.     SxS[ jjrS?SSSSSP.                           SyS\ jjr  SzSSSS].                                     S{S^ jjjr " S_ S`5      r Sj       S|Sa jjrS}Sb jrS~Sc jr " Sd Se\GR                  5      rSSf jr " Sg Sh5      r      SSi jrg! \ a    Sr GNf = f)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CallableOptionalTypeVarUnion)	ParamSpec)ReferenceType)GlobalStateGuard)CallbackTrigger)get_compile_pg)TensorifyState)compile_contextCompileContext	CompileIdtracing)
structured)compile_time_strobelight_metajustknobs_check#maybe_upload_prof_stats_to_manifoldsignpost_event)_use_lazy_graph_module)ConstraintViolationErrorGuardOnDataDependentSymNode)_forward_from_src)_WaitCounter)DistributedDataParallel)_disable_current_modes/is_in_any_mode_without_ignore_compile_internalsis_in_torch_dispatch_mode)CapturedTracebackformat_traceback_short   )config
decoratorsexcgraph_break_hintstrace_rules)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)always_optimize_code_objects
dynamo_tls	skip_codeTorchPatcher)augment_exc_messageBackendCompilerFailedFailOnRecompileLimitHitformat_error_msgInternalTorchDynamoErrorPackageErrorRecompileLimitExceededResumePrologueTracingErrorShortenTracebackSkipCodeRecursiveExceptionTorchRuntimeErrorUncapturedHigherOrderOpErrorunimplemented_v2Unsupported)CheckFunctionManager'get_and_maybe_log_recompilation_reasonsGuardedCode)Hooks)DynamoTracerOutput)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Optional[ModuleType]np)
CompilerFn)CompilePackage)WrapBackendDebug)BytecodeHook
CacheEntryDynamoFrameType)FrameStateSizeEntrybytecodegraph_breaks_T_Pc                      \ rS rSrSrg)TODO_UNKNOWN    N)__name__
__module____qualname____firstlineno____static_attributes__r       [/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/torch/_dynamo/convert_frame.pyr   r      s    r   r   c                  <    \ rS rSrSS jrS	S jrS
S jrSS jrSrg)Tracker   c                0    / U l         [        5       U l        g N)seensetseen_idsselfs    r   __init__Tracker.__init__   s    35	"%%r   c                   ^ ^ [        U5      mTT R                  ;  aS  [        R                  " UUU 4S j5      nT R                  R                  U5        T R                  R                  T5        g g )Nc                :   > TR                   R                  T5      $ r   )r   remove)_idxr   s    r   <lambda>Tracker.add.<locals>.<lambda>   s    DMM4H4H4Mr   )idr   weakrefrefr   appendadd)r   
strong_objobjr   s   `  @r   r   Tracker.add   sR    ndmm#++j*MNCIIS!MMc" $r   c                2    [        U5      U R                  ;   $ r   )r   r   )r   items     r   __contains__Tracker.__contains__   s    $x4==((r   c                l    U R                   R                  5         U R                  R                  5         g r   )r   clearr   r   s    r   r   Tracker.clear   s     		r   )r   r   NreturnNone)r   r   r   r   )r   r   r   bool)	r   r   r   r   r   r   r   r   r   r   r   r   r   r      s    (#)r   r   zOptional[GlobalStateGuard]initial_global_statec                H    [        XU5      n[        UR                  5        U$ r   )original_forward_from_srcr<   __code__)srcglobals	co_fieldsresults       r   fx_forward_from_src_skip_resultr      s!     'sY?FfooMr   c           	       ^^ [         R                  " [        5      m[        R                  " SU-   S9R                  5       n[         R                  " U5      n[        [        R                  " U4S jU5      5      U R                  U R                  [         R                  " U R                  5      S./-   m[        R                  R                  SU4S j5        U Vs/ s H>  nUR                   T:w  d  M  SUR"                   SUR$                   S	UR                    3PM@     snSU R                   SU R                   S	U R                   3/-   nU$ s  snf )
N   skipc                   > U S   T:g  $ )Nfilenamer   )fconvert_frame_interns    r   r   "log_dynamo_start.<locals>.<lambda>   s    a
m';;r   )linenamer   dynamo_startc                    > ST 0$ )Nstackr   )r   s   r   r   r      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      s>   %33H=#++T:BBDK //<O;	
 ''LL"001A1AB	
	E 
NN##  ! E>>11 	P&huzzl,u~~>NO 
 $$%Xdll^<HXHXGYZ		M s   E6-Ec                X   ^  [         R                  " T 5      SU 4S jj5       nT Ul        U$ )z
Context manager to:
    1) Save/restore torch.is_grad_enabled() state
    2) Save/restore python random state
    3) Save/restore torch random state
    4) Monkey patch torch.fx.graph_module._forward_from_src
c            
       > [        5       n[        R                  " 5       n[        R                  R	                  5          [        5          [        5          [        R                  " 5       n[        R                  " 5       n[        R                  " 5       n[        R                  R                  5       n[        R                  " 5       n[        R                  " 5       n	[        R                  R                  5       n
S n[        R                  R!                  5       (       a  [        R                  R                  5       n[        R                  R#                  SS5      n[        R$                  R&                  R(                  n[*        [        R$                  R&                  l        [-        5       n[.        R0                  " 5       nUR3                  [        R$                  R4                  R7                  5       5        UR3                  [8        5         T" U 0 UD6UR;                  5         [        R                  R=                  5       S:X  d   S5       eUR;                  5         [        R                  R?                  U5        [        R@                  RB                  RE                  U5        [        RF                  " XVS9  [        RH                  " U5        [        R                  RK                  U
5        [        RL                  " U	5        [        R                  R                  5       nUU:w  a  [        R                  RO                  5         Ub  [        R                  RK                  U5        [        R                  RQ                  SSU5        U[        R$                  R&                  l        URS                  5       (       d   SURU                  5        S35       esS S S 5        sS S S 5        sS S S 5        $ ! UR;                  5         [        R                  R=                  5       S:X  d   S5       eUR;                  5         [        R                  R?                  U5        [        R@                  RB                  RE                  U5        [        RF                  " XVS9  [        RH                  " U5        [        R                  RK                  U
5        [        RL                  " U	5        [        R                  R                  5       nUU:w  a  [        R                  RO                  5         Ub  [        R                  RK                  U5        [        R                  RQ                  SSU5        U[        R$                  R&                  l        URS                  5       (       d   SURU                  5        S35       ef = f! , (       d  f       O= f S S S 5        O! , (       d  f       O= fS S S 5        g ! , (       d  f       g = f)Ncudamatmulr   zQTorch function mode stack state changed while dynamo tracing, please report a bug)	warn_onlyzGlobal z7state changed while dynamo tracing, please report a bug)+r   r   is_grad_enabled_C_PreserveDispatchKeyGuardrm   rn   is_inference_mode_enabled$are_deterministic_algorithms_enabled-is_deterministic_algorithms_warn_only_enabled$_is_default_mobile_cpu_allocator_setrandomgetstateget_default_dtypeget_rng_stater   is_available_get_fp32_precision_getterfxgraph_moduler!   r   rq   
contextlib	ExitStackenter_context_symbolic_trace_maybe_revert_all_patchesru   close_len_torch_function_stack_set_grad_enabledautograd	grad_mode_enter_inference_modeuse_deterministic_algorithmssetstateset_rng_stateset_default_dtype#_unset_default_mobile_cpu_allocator_set_fp32_precision_settercheckreason)argskwargsguardsprior_grad_modeprior_inference_modeprior_deterministicprior_warn_onlyprior_mobile_allocator_statepy_rng_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   _fn"preserve_global_state.<locals>._fn  sC   !#//1 HH..0(*68#(#B#B#D "'"L"L"N#QQSO==? ) "??,L113K#ll88:O!Nzz&&((!&!9!9!;$)HH$G$G%! "'!6!6!H!H6UEHH!!3)+G#--/J$$((BBD $$%HI4*6*xx99;q@ g@   "**?;((>>?ST22' -**?;''4HHAAC , 03NNHH@@B!-JJ,,^<33H&; ;M%%7||~~ fmmo..ef~i 98 +* 10< xx99;q@ g@   "**?;((>>?ST22' -**?;''4HHAAC , 03NNHH@@B!-JJ,,^<33H&; ;M%%7||~~ fmmo..ef~i 988 +** 100s\   W2
WF;V=O6GV=	W#	W26GV:	:V==
WW	W2
W&	"W22
X r  z_P.argsr  z	_P.kwargsr   r   )	functoolswraps_torchdynamo_orig_backend)r  r  s   ` r   preserve_global_stater  	  s2     __RA AF %'C!Jr   c                  ^^ U R                   [        ;   a  gU R                   R                   H  nXR                  ;   d  M  U R                  U   n[	        U[
        5      (       a+  UR                  R                  S5      (       d	  U[        L a    g[        (       d  Mp  [        R                  (       d  M  U[        L d  [        U5      (       d  M    g   0 mSUU4S jjmU R                  R                  5        H  nT" U5      (       d  M    g   [        R!                  SU R                   R"                  U R                   R$                  U R                   R&                  5        g)z+Check if the frame has torch.* related bitsTztorch.c           	       >^  [        T 5      nUT;   a  TU   $ STU'   [        T [        R                  [        R                  R
                  45      (       d>  [        T [        5      (       a3  [        T [        R                  R
                  5      (       a
  STU'   TU   $ [        R                  (       aS  [        (       aH  [        T [        R                  5      (       d  [        T [        R                  5      (       a
  STU'   TU   $ [        T [        [        45      (       a  [!        U4S jT  5       5      TU'   TU   $ [        T ["        5      (       a6  [        T R%                  5       5      n[!        U4S jU 5       5      TU'   TU   $ [        T [&        [(        [*        [        S5      [,        45      (       a
  STU'   TU   $ [/        T 5      (       a9  [1        T S5      (       a(  [!        UU 4S jT R2                   5       5      TU'   TU   $ g)z)Recursively check if the obj has a tensorFTc              3  4   >#    U  H  nT" U5      v   M     g 7fr   r   .0v
has_tensors     r   	<genexpr>:has_tensor_in_frame.<locals>.has_tensor.<locals>.<genexpr>  s     ">#Q:a==#   c              3  4   >#    U  H  nT" U5      v   M     g 7fr   r   r"  s     r   r&  r'    s     "A&Q:a==&r(  N_fieldsc              3  H   >#    U  H  nT" [        TU5      5      v   M     g 7fr   )getattr)r#  r$  r%  r   s     r   r&  r'    s     "T1:gc1o#>#>s   ")r   
isinstancer   TensornnModulerk   type
issubclassr*   trace_numpyrv   ndarraygenericr   tupleanydictvaluesstrintfloatr   rj   hasattrr*  )r   obj_idr9  r%  r   s   `  r   r%  'has_tensor_in_frame.<locals>.has_tensoro  s   CXF## cELL%((//:;;3*S%((//"B"B#HVF##RZZ((JsBJJ,G,G#HVF##C$''"">#">>HVF##C #**,'F""A&"AAHVF##C#sE4:t<==$HVF##3GC$;$;""T"TTHVF## r   z0skipping because no torch.* %s             %s %sF)r   objectr   r   )f_coder:   co_names	f_globalsr-  r	   r   
startswithr   rv   r*   r3  rZ   f_localsr9  logdebugr   r   r   )r   r   r   valuer%  r   s       @@r   has_tensor_in_framerI  Z  s    ||33 <<((oo%//'*C#z**''11SE\rf(((cRi8C== ) !#H( (V &&(e ) II	  ## r   Fc                    S n[        U S5      (       a&  [        X5      n[        X@R                  5        X@l        [        XS9  g )Nexec_recordexport)r=  rf   rt   rK  record_filenamer>   )er   r   rM  rN  s        r   exception_handlerrP    s:     Oq-  .q7_mm<+)r   z/typing.Counter[Union[int, FrameStateSizeEntry]]FRAME_COMPILE_COUNTERc                F    [         R                  (       a  [        U 5      $ U $ r   )r*   cprofilecprofile_wrapper)funcs    r   maybe_cprofilerV    s    %%Kr   c                J   ^  [         R                  " T 5      SU 4S jj5       nU$ )Nc                   >^ [         R                  " 5       nU(       d   S5       e[        STR                   S[	        U5      R                  SS5       S35      n[        R                  " 5       n UR                  5         [        R                  " 5       nUR                  " T/U Q70 UD6n[        R                  " 5       U-
  nUR                  5         [        R                  STR                  UU5        [         R"                  " U5      n UR%                  U5        [        R                  S
U5        UR)                  S5      n	 [*        R,                  " SSSSSS[	        U5      /[*        R.                  S9n
[*        R0                  " SSS[	        U	5      /U
R2                  S9  [        R                  SU	5        [A        [	        U5      5      =m(       a$  [B        RD                  RG                  SU4S j5        U$ ! [         a#    [        R                  S5        SnT" U 0 UD6n GNTf = f! [&         a    [        R                  S	U5         GN1f = f! [4         a    [        R                  S5        UR7                  [         R8                  R:                  5      R=                  S5        UR7                  [         R8                  R>                  5      R=                  S5         GN*f = f)NzTrace id is Nonez/tmp/r   /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                    > ST S.$ )Ncprofile_manifold_url)r   urlr   )manifold_links   r   r   ;cprofile_wrapper.<locals>.profile_wrapper.<locals>.<lambda>  s    !8Or   )$r   current_trace_idr   r   r:  replacecProfileProfileenabletimeruncalldisable
ValueErrorrF  	exceptionwarningr[  Stats
dump_statsOSErrorwith_suffix
subprocessPopenPIPE
check_callr\  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_processrd  rU  s              @r   profile_wrapper)cprofile_wrapper.<locals>.profile_wrapper  s~   !224+++xDMM?!CM$9$9#s$C#DHM
 !		+KKMyy{H\\$888F"iikH4OLLN
 	EMM		
 \\$	>OOL) 	'6++F3	E * 0 087-% "! !!s8}5'.. KK:HE @
 
= 
 NN++O e  	+MM56O4*6*F	+  	>MM.=	>, ! 	EKKN MM&..--.::2>MM&..334@@D	Es9   1A#G< H, A5I <)H)(H), IIBK&%K&r  )r  r  )rU  r  s   ` r   rT  rT    s'    __T? ?B r   c                  $    \ rS rSr% SrS\S'   Srg)ConvertFrameBoxi
  NzOptional[bool]error_on_graph_breakr   )r   r   r   r   r  __annotations__r   r   r   r   r  r  
  s    +/./r   r  c                   SU ;  a  [         U S'   [         S-  q U S   n[        U[        5      (       d   e[        U   n[        U==   S-  ss'   S n[        R
                  " 5       =n(       a  UR                  n[        UUUS9$ )N_idr)   )compiled_autograd_idframe_idframe_compile_id)FRAME_COUNTERr-  r;  rQ  r   current_compile_idr  r   )frame_stater  r  r  priors        r   get_compile_idr    s     K*E5!Hh$$$$,X6(#q(#1133u3$991) r   c                  |    \ rS rSr    S	           S
S jjr\SS j5       rSS.           SS jjrSrg)ConvertFrameAsserti&  Nc                r    [        5         Xl        X l        X0l        X@l        XPl        [        5       U l        g r   )rp   r  
_one_graph_export_export_constraints_packager  _box)r   compiler_fn	one_graphrM  export_constraintspackages         r   r   ConvertFrameAssert.__init__'  s/     	&')4&##5 #%	r   c                   ^  U 4S j$ )Nc                \   > [        U TR                  TR                  TR                  5      $ r   )convert_frame_assertr  r  r  backendr   s    r   r   8ConvertFrameAssert._clone_with_backend.<locals>.<lambda>:  s%    3OOLL$$	 
r   r   r   s   `r   _clone_with_backend&ConvertFrameAssert._clone_with_backend8  s    
 	
r   r   r   c               	   [        5         UR                  n[        X5      n[        R	                  U5        U[
        ;   a
  [        5       $ [        R                  R                  S5      (       a7  [        R                  R                  S5      UR                  :w  a
  [        5       $ UR                  S:X  a*  UR                  R                  S5      (       a
  [        5       $ UR                  S:X  a
  [        5       $ UR                  S:X  a_  UR                  R                  [        R                  R                  [         R"                  R$                  5      5      (       a
  [        5       $ UR                  S:X  a  UR                  S:X  a
  [        5       $ UR                  S:X  a4  UR                  S:X  a$  ['        UR(                  5      (       d
  [        5       $ [+        U5      (       a  [-        S	S
SS/[.        R0                  QS9  [3        U5      (       d
  [        5       $ [4        R6                  " 5       nU(       aI  SUR                  R                  ;   a/  UR8                  nU(       a  SUR                  R                  ;   a  M/  U(       a&  UR                  [:        R<                  L a	  [        SS9$ [?        5       q [C        U5      n	U	RD                  n
[G        SSUR                  U
[I        U	5      UR                  URJ                  URL                  URN                  S.5        UR                  R                  [P        5      (       dG  UR                   SUR                   SURJ                   3n[R        RT                  RW                  U5        [Y        [[        U	5      5         []        UR                  UR^                  UR`                  UR(                  URb                  U Rd                  U Rf                  U Rh                  U Rj                  UUUUUU	US-   U Rl                  U Rn                  S9nS S S 5        [p        Rr                  (       a.  U Rl                  b!  SSK:J;n  URy                  U Rl                  5        W$ ! , (       d  f       NS= f)NTORCHDYNAMO_DEBUG_FUNCTIONz	<genexpr>)ztransformers/file_utils.pyztransformers/utils/generic.pyzdiffusers/utils/outputs.py__setattr__r   z<module><string>z<lambda>z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torch/_dynamo/convert_frame.pyF)apply_to_codedynamoz_convert_frame_assert._compile)r   r  
compile_idr   r   
cache_sizeaccumulated_cache_size :r)   )r  r  r   r  convert_frame_box)DynamoCache)=ri   rA  r7   input_codesr   output_codesr[   osenvirongetr   r   endswithrD  pathdirnamer   optimr   r   
f_builtinsr3   rJ   r-   FUNDAMENTALrI  sys	_getframef_backr+   "_nonrecursive_disable_wrapper_coder   r   r  r  r   r:  r   +num_cache_entries_with_same_id_matched_objsnum_cache_entriesrT   r;   traced_frame_infosr   r   r   _compilerC  rE  closurer  r  r  r  r  r  r*   caching_precompiler  r  record_package)r   r   cache_entryhooksr  r   r   r  
prev_framer  r  infor   r  s                 r   __call__ConvertFrameAssert.__call__A  s    	||';
<%''JJNN788

;<L%''<<;&4+;+;+D+D,
 ,
 &''<<=( &''<<:%$*:*:*E*EGGOOEKK001+
 +

 &''
 <<:%$*:*:j*H%'' LLJ&  J.))** &''4Z5 '22		 #5))%'' ]]_
0J4E4E4Q4QQ#**J 0J4E4E4Q4QQ !!Z%R%RR%E::  01#K0
&&,<<$!*o#//"&"5"5(TT*4*F*F	
 ||&&'DEEll^1T%5%5$6a8K8K7LMD))006^J78  ..(('%AX"&))%F 9, $$)B, &&t}}57 98s    BQ99
R)r  r  r  r  r  r  TFNN)r  rw   r  r   rM  r   r  Optional[typing.Never]r  Optional[CompilePackage]r   r   )r   z*Callable[[CompilerFn], ConvertFrameAssert]r   r|   r  Optional[CacheEntry]r  rO   r  *dict[str, Union[int, FrameStateSizeEntry]]r   r;  r   r[   	r   r   r   r   r   propertyr  r  r   r   r   r   r  r  &  s     59,0&& & 	&
 3& *& 
&" 
 
 II *I 	I
 @I I 
I Ir   r  c                    [        XX#U5      $ )zHFully convert a frame into an FX graph, raising an exception if we fail.)r  )r  r  rM  r  r  s        r   r  r    s     G r   )OrderedDict)RemovableHandlezdict[int, BytecodeHook]_bytecode_hooksc                J    [        [        5      nU [        UR                  '   U$ )zRegister hooks for bytecode generated by Dynamo. The hook can do some
logging, as well as return a new code object to be used. Please refer
to `BytecodeHook` for the hook signature.
)r  r  r   )hookhandles     r   register_bytecode_hookr    s!    
 _-F!%OFIIMr   rM  r  r  distributed_stater  c                 ^^^^ SSK JmJm  TR                  5         [	        5       n[        U	U UUUUUU
UUUUUTUUUS9mSUUUU4S jjn U" 5         [        T5      nUR                  nUc   eUR                  (       d   eUR                  U	S S & U
R                  UR                  5        [        U	5        [        U	5        [        [        U	5      5      U	S S & U$ ! [         a  n[        TSS9Ul        e S nAff = f)Nr   )bisecttranslation_validation_enabled)r  speculation_logexn_vt_stackr  r  c                   >  TR                   R                  5         [        TR                   R                  5         TR	                  5          TR                  5         S S S 5        S S S 5        TR                   R                  5         g ! , (       d  f       N1= f! , (       d  f       N:= f! [        R                   a    TR                  5         e [        R                  [        R                  [        R                  4 a    e [         a*    T" 5       (       a  T " TR                   R                  5        e f = f! TR                   R                  5         f = fr   )outputmark_bytecode_tracing_startr   tracing_contextset_current_txrunr,   UnspecializeRestartAnalysisr   SpeculationRestartAnalysisTensorifyScalarRestartAnalysis	SkipFrame	Exception	shape_envcall_cleanup_hooks)r  r  tracerr  s   r   
run_tracertrace_frame.<locals>.run_tracer  s    	/MM5576679N9N9P

 :Q7  MM,,.! :Q9P77.. 	!!#**..MM
 	
  	-//v}}../	
 MM,,.sS   9B+ BB	B&B+ 	
B	B
B($B+ 'D9 (B+ +BD66D9 9ET)errorr   )torch.fx.experimental.validatorr  r  restartrV   rW   rP   output_graphoutput_instructionsupdatecode_optionsr4   r1   r0   r/   r  _torch_dynamo_tracer_output)r   r   localsbuiltinsr  r  tf_mode_stackr  r  instructionsr	  rM  r  r  r  r  r  r  tracer_outputr  rO  r  r   r  s           `            @@@r   trace_framer    s   ( W!#L"'!+#F(/ /**62++!!!)))) 44QF//0(6(601A,1OPQ
 	  (:6(N%s   BC 
C:%C55C:c                  d    \ rS rSr% SrS\S'   S\S'   S\S'       S           SS
 jjrSrg	)DynamoOutputi@  aS  
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.
rP   r  types.CodeTyper~   zOptional[float]last_attempt_start_timeNc           
         U R                   R                  c   e[        UU R                   R                  UU(       a  UR                  OS U(       a  UR                  OS UUS9$ )N)save_guardsstrict_error)r  r  rL   guard_fail_fnguard_filter_fn)r   r   r  saver  r  s         r   build_guardsDynamoOutput.build_guardsO  s^     !!..:::#++#(Ed%*E!!%
 	
r   r   )NFNF)r   r  r  zOptional[Hooks]r  r   r  r  r  r   r   rL   )r   r   r   r   __doc__r  r  r   r   r   r   r  r  @  sm     &%,,
 "&,0"

 
 	

 *
 
 

 
r   r  c                  B    \ rS rSr% SrS\S'   S\S'   S\S'   S	\S
'   Srg)BackendInputic  a  
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.
r:  
backend_idtorch.fx.GraphModuler   r
   example_inputsz,torch._subclasses.fake_tensor.FakeTensorMode	fake_moder   Nr   r   r   r   r  r  r   r   r   r   r  r  c  s      O&&;;r   r  c                  .    \ rS rSr% SrS\S'   S\S'   Srg)	CaptureOutputit  a  
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.
r  dynamo_outputr  backend_inputr   Nr$  r   r   r   r&  r&  t  s      r   r&  c                  H    \ rS rSr% S\S'   S\S'   S\S'   S\S'   S\S	'   S
rg)	FrameInfoi  r  r   dict[str, object]r   r  r  tuple[CellType]r  r   N)r   r   r   r   r  r   r   r   r   r*  r*    s     
r   r*  ) _is_export_deprecated_do_not_usec                 ^^ SSK Jm  Sm      SUU4S jjn [        U R                  U R                  U R
                  U R                  U R                  UUS[        5       S9	nTc   e[        UT5      $ ! [         a  n[        U5        [        R                  (       a  e UnUR                  b6  UR                  R                  S5        UR                  nUR                  b  M6  UR                  S5      UR                  eSnAff = f)a  
A standalone function which takes a frame and returns dynamo captured graph
plus other important compile information. This should serve as the common
interface for different torch compiler AOT frontengs (e.g. precompile, export).
Note that this function doesn't apply context managers like metrics context
or compile id, and the expectation is that the caller will apply them depending
on the use case.

The CaptureOutput is separated into two parts:
1. Dynamo specific information from DynamoOutput, which includes:
    - guards
    - generated bytecode
    - other information tracked by OutputGraph.
2. Backend specific information (indexed by unique backend id) such as:
    - fx graph
    - example inputs
r   )TracingContextNc                   > TR                  5       R                  nUc   e[        U R                  S   [        5      (       d   e[        U R                  S   XU5      mU $ )Nr   )r  r#  r-  metar:  r  )gmr"  r#  r/  r(  s      r   fullgraph_compiler-fullgraph_capture.<locals>.fullgraph_compiler  sb     #&&(22	$$$"'',/5555$GGL!2y
 	r   T)r  rM  r  restart_reasons)r2  r!  r"  zlist[torch.Tensor]r   r!  )torch._guardsr/  compile_framer   r   r  r  r  r   rK   r>   r*   verbose	__cause__with_tracebackr&  )r   r-  r3  r'  rO  cur_exnr/  r(  s         @@r   fullgraph_capturer<    s   ( -,0M
 
2D
	
 
6%JJMMLLNNMM*3E

. $$$66  	6A>>!"+,,T2''G + t$!++5	6s   AA7 7
DA$D'DDc               @  ^ ^^^^^^^^	^
^^^ [        5       m      SUUU UUUU	U
UUUUU4S jjnSn[        R                  " 5        HT  nU[        R                  " 5       l         [        SU 3SS9   [        T U5      u  nnUc   e[        UUUS9sSSS5        s  $    g! , (       d  f       Mh  = f! [        R                   a  n[        U[        R                  5      (       d  [        R                  " 5         [        R!                  S[#        [$        UR&                  5      5        UR)                  UR*                  =(       d    [-        [/        U5      5      5        [0        R0                  " 5       nUS:  a  [3        S	S
S/ S9   SnAGME   SnAGML  SnAf[        R4                   aq  n[        U[        R                  5      (       d  [        R                  " 5         [        R7                  SUT R8                  T R:                  T R<                  5        e SnAff = f)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                ~   > [         R                  R                  5       n[        TTTTTTUTTU UT	T
TTTS9nUc   eU$ )Nr  )r   	overrides _get_current_function_mode_stackr  )r  r	  r  r  r  r  r   r  r  rM  r  r  r   r  r  r  r  s       r   	transform compile_frame.<locals>.transform  sj     OO<<> 	 $1#/!
& (((r   Ncompile_attempt_Tlog_pt2_compile_event)r  r~   r  zRestarting analysis due to %sd   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  z*Skipping frame %s %s                 %s %s)r  list[Instruction]r	  r+  r   rP   )rY   r   countr   r  attemptrd   r5   r  r,   RestartAnalysisr-  r  r   r   rF  r  rl   r(   __traceback__r   restart_reasonr:  r1  rk  rJ   r  rG  r   r   r   )r   r   r  r  r  r  r  r5  rM  r  r  r  r  rA  r  rI  r~   r  rO  r  s   ``````` `````      @r   r7  r7    s   . %&O'7H	  8 #??$'.$(	 0	:RVW*?i*P'-$000#"/%,C XW	 % XW "" 	a!C!CDD$$&HH/11??C
  0 0 @CQLA&*iik#} ?!2    }} 	a!C!CDD$$&II  ## 	sD   'B44B"	B4"
B1	,B41B44HB?FH,A,HH)r   r  r  c                 ^^^^^^^^
^^^^=^>^?^@ SSK Jn  SSKJnJn  Sm?[        SS9        SGU=4S jj5       n[                SHUU
UUUU>U?UUUUUUU@4S jj5       m=[        5       nTb  TR                  U 5      O[        R                  " 5       n[        [        R                  5         [        [        T5      5         U" 5          [!        S	S
S
S9   [#        S5      R%                  5          U   ['        SSSS9   U   [)        5       m@[+        5       =n(       a  [-        U[/        5       5      m>OS m>S n[1        U5      (       a!  U(       a  [3        T
U5      nU(       d  SOUS   nSn[        R4                  (       d+  U(       a$  [3        T
US
S9nU(       d  S OUS   nUb  SU;   a  S
nUR7                  UUS.5        [9        5       nU(       a,  U Vs1 s H  nU" 5       S S iM     n nUR)                  SU 5        [;        UT5      u  n!n"U!(       a  SIS jn#[<        R?                  SU"[A        [        U"5      U#" U 5      U[B        5        [        RD                  (       a  [G        U" S35      eU(       a  [G        U" S35      e[I        S5      (       a  [K        U" S35      e[M        SSU" 3S U" S!3/ S"9  [<        RO                  S#U RP                  U RR                  U RT                  US$-   S%RW                  [X        RZ                  " S$U-   S&9R]                  5       5      5        [_        X5      n$[`        Rb                  " 5       n%S n&S n'S n(S n)S n*[d        Rf                  Rh                  Rj                  Rm                  5         S n+S n, U" XU	5      u  n+n,[o        5         U,(       a3  U,Rp                  =n-(       a   U-Rs                  5       (       a  [u        U 5        U+[d        Rf                  R                  Rv                  (       a>  ['        S'S(S)9   [<        Ry                  S*5        [z        R|                  " S+5        S S S 5        S n.U,(       a  U,Rp                  n.U.(       a!  0 U.l?        U.R                  =n/(       a  0 U/lA        S+S,K4JBn0  [        U05      n1U'Gc  U.Gb  [        U.R                  5      n2[        U.R                  R                  5      n3U.R                  5       n4[        U.R                  R                  5      n5U.R                  5       n6[        U.R                  5      n7U.R                   V8s1 s H  n8U8R                  iM     n9n8U.R                   V8s1 s H  n8U8R                  iM     n:n8[d        Rf                  Rh                  Rj                  R=                  5         OGS n2S n3S n4S n50 n6S n7[)        0 5      n9[)        0 5      n:[)        5       m@[`        Rb                  " 5       U%-
  S--  m?0 S.U1_S/U RP                  _S0U RR                  _S1U RT                  _S2UR                  _S3UR                  _S4U2_S5U3_S6U4_S7U5_S8U7_S9U&_S:U'_S;U)_S<U*_S=U9_S>U:_T@T?U+S L[        R                  S
[        T?5      U$[        U65      U(S?.	En;UR7                  U;5        U(       a"  U,(       a  U,R                  O	[        5       UlS        sS S S 5        sS S S 5        sS S S 5        sS S S 5        sS S S 5        sS S S 5        sS S S 5        sS S S 5        $ s  snf ! , (       d  f       GN= fs  sn8f s  sn8f ! [         Ga  n<[        U<5      R                  n&[        U<5      n'[        R                  " 5       /n([        U<XTS@9  [d        R                  R                  SASB SC SD9  [        R                  " U<T5      u  n)n*[A        U<SES 5      n,[        U<[        [        [        [        [        [        U[        U[        [        [        45      (       a  e [        [        U<5      R                   SF[        U<5       35      R                  U<R                  5      S eS n<A<ff = f! [d        Rf                  R                  Rv                  (       aP  ['        S'S(S)9   [<        Ry                  S*5        [z        R|                  " S+5        S S S 5        O! , (       d  f       O= fS n.U,(       a  U,Rp                  n.U.(       a!  0 U.l?        U.R                  =n/(       a  0 U/lA        S+S,K4JBn0  [        U05      n1U'Gc  U.Gb  [        U.R                  5      n2[        U.R                  R                  5      n3U.R                  5       n4[        U.R                  R                  5      n5U.R                  5       n6[        U.R                  5      n7U.R                   V8s1 s H  n8U8R                  iM     Os  sn8f n9n8U.R                   V8s1 s H  n8U8R                  iM     Os  sn8f n:n8[d        Rf                  Rh                  Rj                  R=                  5         OGS n2S n3S n4S n50 n6S n7[)        0 5      n9[)        0 5      n:[)        5       m@[`        Rb                  " 5       U%-
  S--  m?0 S.U1_S/U RP                  _S0U RR                  _S1U RT                  _S2UR                  _S3UR                  _S4U2_S5U3_S6U4_S7U5_S8U7_S9U&_S:U'_S;U)_S<U*_S=U9_S>U:_T@T?U+S L[        R                  S
[        T?5      U$[        U65      U(S?.	En;UR7                  U;5        U(       a#  U,(       a  U,R                  O	[        5       UlS        f f = f! , (       d  f       O= f S S S 5        O! , (       d  f       O= fS S S 5        O! , (       d  f       O= fS S S 5        O! , (       d  f       O= fS S S 5        O! , (       d  f       O= fS S S 5        O! , (       d  f       O= fS S S 5        O! , (       d  f       O= fS S S 5        g ! , (       d  f       g = f)JNr   )async_compile_pool_manager)BisectValidationExceptionValidationExceptiong        compile_inner)
phase_namec           
       > [         R                  " 5        nUR                  [        R                  R
                  R                  [        R                  [        [        R                  " 5       5      5      5        UR                  [        R                  " 5       5        T" XU5      sS S S 5        $ ! , (       d  f       O= f[        5       S 4$ r   )r   r   r   r   _dynamocallback_handlerinstall_callbacksr   DYNAMOr:  r   r  rb   recordr[   )r   r  r  r   _compile_inners       r   rQ  _compile.<locals>.compile_innerR  s     !!#u..@@#**C0Q0Q0S,T
  = D D FG!$59 $##  
 	
s   BB11
B?c                x  > [         R                   " 5       =p4            SS jnU" SU R                  U R                  U R                  U 5        S n [	        U T T!TTTUT#TTTTT"S9nTb  TR                  c   S5       eUR                  nUR                  n	UR                  b  UR                  nUc   eU" SU R                  U R                  U R                  U5        [        [         R#                  5       5       H(  u  p[%        SU
 3SS	9   U" X5      nUb  UnS S S 5        M*     U [&        U'   [(        R+                  U5        X4-
  mU	R,                  c   eU	R,                  nSS
 jnUc   eU" U 5      nU" U5      nSnUSU R.                  S U  S3-  nUSUR.                  S U  3-  nU R.                  S U UR.                  S U :X  d   U5       eSnUSU R0                   S3-  nUSUR0                   3-  nU R0                  UR0                  :X  d   U5       eSnUSU R2                   S3-  nUSUR2                   3-  nU R2                  UR2                  :X  d   U5       eUR4                  (       a!  UR7                  5       (       a  [        5       U	4$ UR8                  c   eUR:                  [<        R>                  U'   [%        SSS	9   URA                  U UT"S LTS9nS S S 5        T"bP  WRB                  c   eT"RE                  URB                  U5        T"RG                  URH                  RJ                  5        Tb  [M        T5      OSnSU-   n[O        UWRP                  TU5      nUR7                  5       (       d(  URR                  b  URS                  UR8                  5        [U        U5      U	4$ ! [
        R                   aK  nU(       a  [        R                  S5        UR                  c   e[        5       UR                  4s S nA$ S nAff = f! , (       d  f       GM(  = f! , (       d  f       GNN= f)Nc           	         [         R                  [        R                  5      (       a!  [         R	                  [        XX#U5      5        g g r   )bytecode_logisEnabledForloggingDEBUGrG  re   )prefixr   r   line_nor   s        r   log_bytecode6_compile.<locals>._compile_inner.<locals>.log_bytecodem  s8     ((77""#F(TJ 8r   zORIGINAL BYTECODEr  z,No graph captured with export/fullgraph=Truez;compiler collective wasn't run before compilation completedzMODIFIED BYTECODEbytecode_hooks_TrD  c                    SS K nU R                  U R                  -   [        U R                  UR
                  -  5      -   [        U R                  UR                  -  5      -   $ )Nr   )inspectco_argcountco_kwonlyargcountr   co_flags
CO_VARARGSCO_VARKEYWORDS)r   rg  s     r   
count_args4_compile.<locals>._compile_inner.<locals>.count_args  s]       (()t}}w'9'99:; t}}w'='==>?r   zarg mismatch: zold code object has args z, 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 r  )r  r  r  UnknownzTorch-Compiled Region: )ra  r:  r   r:  r   r:  rb  r;  r   r   r   r   r   r   r   r;  )+rk  r   r   r   r7  r,   r  rF  rG  r
  r[   
all_statesr~   r  r  	enumerater  r9  rd   ro   r  r   r  co_varnamesco_freevarsco_cellvarsrM  is_empty_graphr  cleanupsra   instancer  guards_stateadd_guarded_codeadd_inlined_sourcer  traced_coder:  rN   guard_managerguard_export_fnr^   )$r   r  r  r  
start_timerc  out_coder'  rO  r  r   r  hook_outputr  rm  total_argcount_oldtotal_argcount_newmsgcheck_fncompile_id_strannotation_strguarded_coder  r  r  r  r  r  dynamo_time_before_restartrM  r  r  r   r  r  r5  s$                         r   rY   _compile.<locals>._compile_innerd  s    04yy{:		"	.1	<?	GO		 	LL	
 	G)#5'"3M* !(,=,H,H,T 	
I	
T !))%3300<&3&K&K####LL	
 #?#9#9#;<ICu5TR"42**H SR = #'h"%<%I"))555++	 ###'-'1*4+;+;<O=O+P*QQSTT*8+?+?@SAS+T*UVV001##$7%789	 	9 $.t/?/?.@CC.x/C/C.DEE8#7#77<<7#.t/?/?.@CC.x/C/C.DEE8#7#77<<7 ==V2244%'66}}(((,2OO).E$11D('	 2 H F ((444$$X%:%:HE&&v'='='I'IJ,6,BZ	2^C"""	
 $$&&5+@+@+L !!&--0 .==e }} 	G		HI00<<<%')F)FFF		G0 SRx FEs7   N5 PP*5P	A P	PP
P'	*
P9r  T)reset_event_log_on_exitrE  z+pytorch.wait_counter.entire_forward_compilez_compile.compile_innerentire_frame_compile!dynamo_cumulative_compile_time_us)rR  dynamo_compile_column_usz$Unable to find recompilation reasonsF)skip_loggingz%[inline-inbuilt-nn-modules-candidate])recompile_reason#inline_inbuilt_nn_modules_candidate   recompile_user_contextsc                V    SU R                    SU R                   SU R                   S3$ )N'z' (r  ))r   r   r   )r   s    r   format_func_info"_compile.<locals>.format_func_infoI  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 %szK reached, because fail_on_recompile_limit_hit = True this is a HARD failurea1   reached with fullgraph=True. Excessive recompilations can degrade performance due to the compilation overhead of each recompilation. To monitor recompilations, enable TORCH_LOGS=recompiles. If recompilations are expected, consider increasing torch._dynamo.config.cache_size_limit to an appropriate value.z;pytorch/compiler:skip_code_recursive_on_recompile_limit_hitz reachedzDynamo cache limit exceededzLimit type: zLDynamo attempted to recompile the code object too many times, exceeding the zo cache size limit.Giving up on compiling as the compile time tradeoff is likely not worth the performance gain.r  zBtorchdynamo start compiling %s %s:%s, stack (elided %s frames):
%s   r  r   gc
gc_time_us)r  z run_gc_after_compile: running gcr)   )
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)	r5  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_tracerL  artifactc                     SSS.$ )Ndynamo_errorstringr   encodingr   r   r   r   r   _compile.<locals>.<lambda>  s    * (%r   c                 ,    [         R                  " 5       $ r   	traceback
format_excr   r   r   r   r    s    9#7#7#9r   metadata_fn
payload_fnr
  z: )r   r   r  r   r  rO   r   z7tuple[ConvertFrameReturn, Optional[DynamoTracerOutput]])r   r   r  r   r  rO   r   z-tuple[ConvertFrameReturn, DynamoTracerOutput])r   r   r   r:  )ltorch._inductor.async_compilerN  r  rO  rP  r   rV  rh   code_contextr   nullcontextr   r*   use_lazy_graph_moduler   r   r`   r"   guardrd   r   r   rU   rX   r9   rM   inline_inbuilt_nn_modulesupdate_outerrg   r8   rF  rp  r,  rs   fail_on_recompile_limit_hitr@   r   rD   rJ   rG  r   r   r   joinr'   r   formatr   rk  time_nsr   rT  utilsReinplaceCountersr   rR   r  has_outputsrQ   run_gc_after_compiler  r  collectlocal_scoperoot_txrE  r  r:  lenr  r  count_callsgraphnodesget_graph_sizes_structuredplaceholdersr  r   r  r  r  r  rr   r  r_   r  r1  r  r  rP  r   r   r,   get_exc_messager-  rK   rH   r?   AssertionErrorr   r    rI   rF   rC   rE   rB   r:  rK  )Ar   r   r  r  r  r  r  rM  r  r  r  r  r   r  r  r   r  r  rN  rO  rP  rQ  metrics_contextr  
compile_pgr  reasonsr  inbuilt_nn_reasonsinbuilt_nn_recompile_reasonr  user_contextuser_contexts_msgexceeded
limit_typer  r  start_time_nsr  r  r  r  r  r  r  r  r  r   r  r  r  r  r  r  r  r  opr  r  metricsrO  rY  r  r  r5  sA    ````` `` `  `` `                                            @@@@r   r  r  1  s3   . I ),"o>

#'
05
	@
 ?
" [>[>[> [> 
7	[> [> [> [>z *+O&-&9T"z?U?U?W  	v;;<z23"$d$	
 	BCIIK$-%H	

 	$'E')):) 0Z\ J $ +/J''E=k5QG>E67ST:  /4+//E!HU" /4Fq4I (
 ,7;./ 7;3 	$$$47Z	
 #F"G" :Q!9Pt$9P  !  9;LM6z:N*V
 KK;
 
+ & # 11-!l"mn  -!l #` `  !M  -
|8-DEE !9*:,7!%%/L 12!2  			QLL1HGG%--1t8<CCEF	
4 't2#'	%)5926 04--335^	*7*O'L- %2%?%??\? ,,..+D1f }}##88!$NHH?@JJqM O 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?WR__?W!$X.4.I.I(.IBOO.I % ( ##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 		
 	
 	 	LK	
 	
 	%$ 	43 	=<t!@ ON. %Y(W  +	< Q,,Ia&K%.%9%9%;$<!aV<NN++ : ,  @C?R?R:@<$&< $A'DdKM%)",/'0-$ . "  /Aw++,Bs1vh7 .1t<S+	<d }}##88!$NHH?@JJqM ONN 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?WR__?W$X!$X.4.I.I(.IBOO.I($ ( ##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 !} 		
 	
 	
 	 	LKK	
 	
 	
 	%$$ 	433 	=<<s  .o2on>n$	1n
4m0 mCl;[4(Fl; A\3l;,[90C4l;$\:l;
\ El;+	m4	m0=	n
	n$		n>	o!	o24l;9
\l;`: D`55`::`==4l81,b&	l8&
b40C3l8#f:9l8g&%El88l;;
m	m	m0
m$ m0'	n
0
m>:n
	n$	

nn$		n>$
n2.n>5	o>
oo	o2
o&	"o22
p c                  l    \ rS rSr S       SS jjr\S	S j5       r S
           SS jjrSrg)ConvertFramei:  Nc                <    Xl         [        USUS9U l        X l        g )NF)r  r  )r  r  _inner_convert_hooks)r   r  r  r  s       r   r   ConvertFrame.__init__;  s%     *5&25'
 r   c                   ^  U 4S j$ )Nc                0   > [        U TR                  5      $ r   )convert_framer  r  s    r   r   2ConvertFrame._clone_with_backend.<locals>.<lambda>I  s    }KK 
r   r   r   s   `r   r   ConvertFrame._clone_with_backendG  s    
 	
r   c                  ^ [         R                  UR                  5        [        S   S==   S-  ss'    U R	                  XX4US-   S9n[        S   S==   S-  ss'   U$ ! [
         Ga  n[        U[        5      (       a  e U R                  R                  R                  S LnUc   eU R                  R                  R                  (       a  e [        U[        5      (       a  e [        U[        5      n	U	(       a  [        R                  [        R                  5      (       a  [!        US5      (       a  [!        US5      (       a  [#        [%        UR&                  5      5         UR(                  n
SR+                  [,        R.                  " U
5      5      nS	U 3m[0        R2                  R5                  S
S U4S jS9  [        R7                  TSS9  S S S 5        O! , (       d  f       O= f[8        R:                  (       d  U	(       d  e [=        USS 5      nUR                  n[?        X}X5      nU	(       a  [@        RC                  USS9  O[@        RE                  USS9  [        U[F        5      (       a4  [I        [K        [L        RN                  [L        RN                  5      S9s S nA$ [        U[P        5      (       a4  [I        [K        [L        RR                  [L        RR                  5      S9s S nA$  S nA[I        5       $ S nAff = f)Nframestotalr)   r   okr  
real_stackr  z&Graph break: skip: from user code at:
r  c                     SSS.$ )Ndynamo_graph_break_reasonr  r  r   r   r   r   r   'ConvertFrame.__call__.<locals>.<lambda>  s    (C,41r   c                 8   > T  S[         R                  " 5        3$ )N
r  )user_stack_traces   r   r   r    s    2B1C2iFZFZF\E]/^r   r  T)exc_inforN  )frame_exec_strategy)*r  r   rA  rc   r  r  r-  rE   r  r  rI   rK   graph_break_logr^  r_  r`  r=  r   r   r  r  r  r  format_listr   r   r   rG  r*   suppress_errorsr,  rA   rF  r  rp  rG   r[   r]   r\   SKIPrD   RUN_ONLY)r   r   r  r  r  r   r   rO  r  	soft_fail
user_stackuser_stack_formattedrN  r   	error_msgr  s                  @r   r  ConvertFrame.__call__N  s    	%7#q(#a	((ETAX ) F Xt$)$M [	!788 ##((==TI ! (333""''<<   !9::"1k2I _99'--HH 1l++<0H0H()EF%&\\
/1ww%11*=0, .UUiTj+k(77&) (_ 8  (--,%) .  GFF& ))) &a):DAO<<D(/IIT2I5!788)(9#((+*:*:) 
 A566)(9#,,k.B.B)  7 "##{[	sE   )A   L+C8K>#A/G	K>
G)	%B<K>!L'AK>*L>L)r  r  r  r   )r  rw   r  rO   r  r  r   r   )r   z*Callable[[WrapBackendDebug], ConvertFrame]r   r  r  r   r   r   r  r  :  s    
 -1	

 
 *	

 

 
 
 m$m$ *m$ 	m$
 @m$ m$ 
m$ m$r   r  c                    [        XUS9$ )zHTry to convert a frame into an FX graph, if error leave frame unmodified)r  )r  )r  r  r  s      r   r  r    s     G<<r   c                   SSK Jn  [        R                  nS[        l        [	        U S5       n[
        R                  " U5      nS S S 5        [        [        R                  " WR                  R                  5       [        5       R                  5       5      5      Ul
        [        R                  " S5          [        UR                  UR                  UR                   UR"                  UR$                  USSS ['        5       [)        SS5      S S 0 [+        SSS9S	9  U[        l         S S S 5        g ! , (       d  f       N= f! U[        l        f = f! , (       d  f       g = f)
Nr)   )eagerFrbr   *   i  )r  r  )
r  r  rM  r  r  r  r  r   r  r  )backends.debuggingr
  r*   replay_record_enabledopenrS   loadr8  r   chainr   itemsr+   r  r  r   r  r  r  rO   r6   r   )r   r
  original_replay_valin_filerX  s        r   replayr    s   ) 66#(F 	h	 %%g. 
)//&..*>*>*@')//BSTUFN		(	(	/	?!#'g4Q: $b3G$ ,?F() 
0	/	 
	0 ,?F() 
0	/s0   D06E8A#EE0
D>EE
Ec                    [         R                  S:  a  g[        R                  " U 5       H$  nUR                  S:X  d  M  UR
                  S-  s  $    [        S5      e)N)      r   RESUMEr  z$RESUME instruction not found in code)r  version_infodisget_instructionsopnameoffsetRuntimeError)r   insts     r   first_real_inst_idxr!    sQ    
'!$$T*;;(";;!## + =
>>r   c                  <    \ rS rSrSS.           SS jjrSrg)ConvertFrameProtocoli  r   r   c                   g r   r   )r   r   r  r  r  r   s         r   r  ConvertFrameProtocol.__call__  s     !r   r   Nr  )r   r   r   r   r  r   r   r   r   r#  r#    sL     !! *! 	!
 @! ! 
! !r   r#  c                     [        5       $ r   )r%   r   r   r   &should_skip_due_to_torch_dispatch_moder'    s    :<<r   c                  8    \ rS rSrSS jr        SS jrSrg)CatchErrorsWrapperi  c                T    [         R                  " U5      " U 5        Xl        X l        g r   )r  r  r  r  )r   callbackr  s      r   r   CatchErrorsWrapper.__init__  s    !$')1&
r   c           
        Uc   e[         R                  UR                  5        [        R                  " UR                  5      n[
        R                  S:  a#  UR                  [        UR                  5      :  nO"UR                  [        UR                  5      :  nU(       dG  U(       d@  [        R                  (       d+  [        5       (       a  [        U R                  SS5      (       d  [        R                  [         R"                  5      (       a  U(       a  SnO;[        R                  " UR                  5      (       a  SnO[%        SS9(       a  SnOSn[        R'                  S	UR                  R(                  UUR                  R*                  5        [-        5       $ UR                  R*                  S
:X  a  UR                  R(                  S:X  dD  UR                  R*                  R/                  S5      (       a$  UR                  R(                  S:X  a
  [-        5       $ [0        R2                  R4                  R7                  5       S:X  a  [8        R:                  " 5       nU(       a  [<           SSKJ n  U" URB                  U R                  R                  S9n	[E        U R                  S5      (       d   S5       eU R                  RG                  U	RH                  5      n
U
" XU RJ                  U5      sS S S 5        $ [<           [M        5          U R                  XU RJ                  USS9nUsS S S 5        sS S S 5        $ ! , (       d  f       NP= f! , (       d  f       O= fS S S 5        g ! , (       d  f       g = f)N)r     r  Fztraced frame alreadyzin skipfiles)include_infra_modeszSnon-infra torch dispatch mode present, this is not supported today in torch.compilezdynamo tracing is disabledz#skipping: %s (reason: %s, file: %s)r  __new__zcollections/__init__.py_makeddp_optimizerr   )DDPOptimizer)bucket_bytes_capbackend_compile_fnr  zJDDPOptimizer only supports callback fns that know how to clone themselves.r)   r   )'r  r   rA  r.   r  r  r  f_lastir!  r*   rm  r'  r,  r  rF  r^  r_  r`  r&   rG  r   r   r[   r  r   rT  r  get_optimize_ddp_moder#   _get_active_ddp_modulecompile_lock"torch._dynamo.backends.distributedr3  r4  r=  r  
compile_fnr  r$   )r   r   r  r  is_skipfilehas_started_executionskip_reason
ddp_moduler3  r2  hijacked_callbackr   s               r   r  CatchErrorsWrapper.__call__  s    &&&%!''5w&$)MM4G4U$U!$)MM5H5V$V! "~~688 > >	5QQ ..("8K &&u||44"0K.5I"wK">K		9LL((LL,,	 &'' LL$$
2u||7K7Ky7XLL$$--.GHH$$/ &''==446/I0GGIJ!O$0)3)D)D+/+I+I+c+c%M #668M   e  66JJ)44 &
 -DJJ# "\* 1333DJJ! 4 F  43\\+ "\* 43\\\s1   3BM	M0M2	M0
M
M$	 M00
M>)r  r  N)r+  r#  r  rO   r   r   )r   r|   r  r  r  r  r   r[   )r   r   r   r   r   r  r   r   r   r   r)  r)    s8    
OO *O @	O
 
Or   r)  c                    [        X5      $ r   )r)  )r+  r  s     r   catch_errors_wrapperrC  Y  s     h..r   r   )r   r:  r   zdict[str, Any]r   zOptional[dict[str, str]]r   r   r  )r   r   r   r;  r   z	list[str])r  Callable[_P, _T]r   rD  )r   r|   r   r   )NF)
rO  r  r   r   r   Optional[DynamoFrameType]rM  r   r   r   )rU  rD  r   rD  )r  r  r   r   r  )r  rw   r  r   rM  r   r  r  r  r  r   r  )r  rz   r   r  )"r   r  r   r+  r  r+  r  r+  r  r,  r  rw   r  z'list[torch.overrides.TorchFunctionMode]r  r   r  rY   r  rG  r	  r+  rM  r   r  r  r  4Optional[dict[str, Union[int, FrameStateSizeEntry]]]r  Optional[DistributedState]r  r  r   rP   )r   r*  r-  r   r   r&  )r   r  r   r+  r  r+  r  r+  r  r,  r  rw   r  r   r5  zset[str]rM  r   r  r  r  rF  r  rG  r  r  r   r  )NN)&r   r   r   r+  r  r+  r  r+  r  r,  r  rw   r  r   rM  r   r  r  r  rO   r  r  r  r6   r   rE  r  rF  r  r   r   r;  r  r  r  zOptional[ConvertFrameBox]r   r[   )r  rw   r  rO   r  r  r   r  )r   r:  r   r   rp  )r   r   )r+  r#  r  rO   r   r)  )r  
__future__r   collectionsr   rh  r  r  r  r   r_  r  r[  r   ru  r  	threadingrk  r  typestypingr   dataclassesr   pathlibr   r   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   r6  r   r   r   r   r   torch._utils_internalr   r   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&   torch.utils._tracebackr'   r(   r  r*   r+   r,   r-   r.   bytecode_analysisr/   r0   bytecode_transformationr1   r2   r3   r4   r5   r  r6   r7   r8   r9   
eval_framer:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   r  rL   rM   rN   r  rO   r  rP   pgorQ   rR   replay_recordrS   resume_executionrT   symbolic_convertrU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r  r_   r`   ra   rb   rc   rd   re   rf   rg   rh   ri   rj   rk   rl   rm   rn   ro   rp   rq   rr   rs   rt   variables.torch_functionru   r  numpyrv   ModuleNotFoundErrorTYPE_CHECKINGbackends.registryrw   r  rx   repro.after_dynamory   rz   r{   r|   variables.builderr}   	getLoggerr   rF  r   getArtifactLoggerr]  r  RLockr9  r   r   r   r   r  r  r   r  r   r   r  #suppress_torch_distributed_warningsrI  rP  r  CounterrQ  rV  rT  r  r  r  r  r  torch.utils.hooksr  r  r  r  r  r  r&  r*  r<  r7  r  r  r  r  r!  Protocolr#  r'  r)  rC  r   r   r   <module>rr     s  . #    
  	   	    
       !  > > : : ' !   4 2 4 9 M M %  ? Q & A 
 M E E G        
  , < * ;  " X X     0 J  
 
-'4@@6 !~~//*E..228^L  T]t_	 	 ( iy37 0 7 *+MQ	%2J ,DNb 11K 2Kb (,	**
* %* 	*
 
*  F 
CL 0 0 0;.d dR 15(,


 
 /	

 &
 
 $ - ,7=( 8  15HL48(,#O
OO O  	O
 O O ;O O $O $O $O O /O FO  2!O" &#O$ %O Od 
 
 
D < < <           CH=7=7;?=7=7T 15HL48(,a
aa a  	a
 a a a a a /a Fa 2a &a ab (,HLF" (, 48+F
FF F  	F
 F F F F /F F &F *F %F FF  !F" #F$ &%F* 1+F, -FRA$ A$N )-=== &= 	=?@?	!6?? 	!=U Up/"/+0//e5  	Bs   R) )R54R5