
    Vh                    R   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mZ ddlmZmZmZmZ ddlmZmZmZmZm Z  ddl!m"Z" ddlm#Z# ddl$Z$ddl%Z$dd	l&m'Z' dd
l(m)Z) ddl*m+Z+ ddl,m-Z-m.Z.m/Z/m0Z0 ddl%m1Z1 ddl2m3Z3m4Z4m5Z5m6Z6 ddl7m8Z8 ddl9m:Z:m;Z; ddl<m=Z> ddl?m@Z@ ddlAmBZB ddlCmDZDmEZE ddlFmGZGmHZH ddlImJZJmKZKmLZLmMZM ddlNmOZOmPZP ddlQmRZRmSZSmTZTmUZUmVZV ddlWmXZXmYZYmZZZm[Z[ ddl\m]Z]m^Z^m_Z_m`Z` ddlKmaZambZbmcZcmdZdmeZemfZfmgZgmhZhmiZimjZjmkZkmlZl ddlmmnZnmoZompZp ddlqmrZr ddlsmtZt dd lumvZv dd!lwmxZx dd"lymzZzm{Z{m|Z|m}Z}m~Z~ dd#lMmZ dd$lmZmZmZmZ dd%lmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ dd&lmZ d'ed(<   	 ddlZej:                  rdd)lmZ dd*lmZ dd+lmZmZmZ dd,lmZ  e
jN                  e      Ze$jT                  jW                  ed-      Ze$jT                  jW                  ed.      Z ej\                         Z ed/      Z e"d0      Z G d1 d2      Z G d3 d4      Z e       Z e       Zdad5ed6<    ejn                  e>      	 dT	 	 	 	 	 	 	 dUd7       ZdVd8Ze`jt                  dWd9       Z	 	 dX	 	 	 	 	 	 	 	 	 dYd:Zda ej|                         Zd;ed<<   dZd=ZdZd>Z G d? d@      Z	 	 	 d[	 	 	 	 	 	 	 	 	 d\dAZddBlmZ ddClmZ ej:                  rddDlmZ  eī       ZdEedF<   d]dGZ	 	 d^ddH	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d_dIZ G dJ dK      Zd`dLZdadMZdbdNZ G dO dPej                        Z G dQ dR      Z	 	 	 	 	 	 dcdSZy# e$ r dZY w xY w)da  
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.
    )annotationsN)Path)CellTypeCodeTypeFunctionType
ModuleType)AnyCallableOptionalTypeVarUnion)	ParamSpec)ReferenceType)GlobalStateGuard)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_torch_dispatch_mode)CapturedTracebackformat_traceback_short   )config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RecompileLimitExceededShortenTracebackSkipCodeRecursiveExceptionTorchRuntimeErrorUncapturedHigherOrderOpErrorunimplemented_v2Unsupported)CheckFunctionManager'get_and_maybe_log_recompilation_reasonsGuardedCode)Hooks)put_code_state)ExecutionRecord)TORCH_DYNAMO_RESUME_IN_PREFIX)DistributedStateExceptionStackInstructionTranslator
LocalStateSpeculationLog)is_numpy)ConvertFrameReturnFrameActionFrameExecStrategywrap_guarded_code)chromium_event_timedCleanupManagerCompileTimeInstructionCountercountersdynamo_timedformat_bytecodegen_record_file_nameget_metrics_contextincrement_frameis_namedtupleistype
LazyString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)WrapBackendDebug)BytecodeHook
CacheEntryDynamoFrameType)FrameStateSizeEntrybytecodegraph_breaks_T_Pc                      e Zd Zy)TODO_UNKNOWNN)__name__
__module____qualname__     K/home/dcms/DCMS/lib/python3.12/site-packages/torch/_dynamo/convert_frame.pyrv   rv      s    r{   rv   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   clearr   r   s    r|   r   zTracker.clear   s     		r{   N)returnNone)r   r   r   r   )r   r   r   bool)rw   rx   ry   r   r   r   r   rz   r{   r|   r~   r~      s    (#)r{   r~   zOptional[GlobalStateGuard]initial_global_statec                J    t        | ||      }t        |j                         |S r   )original_forward_from_srcr8   __code__)srcglobals	co_fieldsresults       r|   fx_forward_from_src_skip_resultr      s#     'sGY?FfooMr{   c                R     t        j                         d fd       } |_        |S )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                    t               }t        j                         }t        j                  j	                         5  t        j
                         }t        j                         }t        j                         }t        j                  j                         }t        j                         }t        j                         }	t        j                  j                         }
d }t        j                  j                         rt        j                  j                         }t        j                  j                         }t        j                   j"                  j$                  }t&        t        j                   j"                  _        t)               }t+        j,                         }|j/                  t        j                   j0                  j3                                |j/                  t4               	  | i ||j7                          t        j                  j9                         dk(  sJ d       |j7                          t        j                  j;                  |       t        j<                  j>                  jA                  |       t        jB                  ||       t        jD                  |       t        j                  jG                  |
       t        jH                  |	       t        j                  j                         }||k7  rt        j                  jK                          |t        j                  jG                  |       t        j                  jM                  |       |t        j                   j"                  _        |jO                         sJ d|jQ                          d       cd d d        S # |j7                          t        j                  j9                         dk(  sJ d       |j7                          t        j                  j;                  |       t        j<                  j>                  jA                  |       t        jB                  ||       t        jD                  |       t        j                  jG                  |
       t        jH                  |	       t        j                  j                         }||k7  rt        j                  jK                          |t        j                  jG                  |       t        j                  jM                  |       |t        j                   j"                  _        |jO                         sJ d|jQ                          d       w xY w# 1 sw Y   y xY w)Nr   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   torchis_grad_enabled_C_PreserveDispatchKeyGuard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_statecudais_available_get_cublas_allow_tf32fxgraph_moduler   r   re   
contextlib	ExitStackenter_context_symbolic_trace_maybe_revert_all_patchesri   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_cublas_allow_tf32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
allow_tf32prior_fwd_from_srccleanup
exit_stackcurr_mobile_allocator_statefns                    r|   _fnz"preserve_global_state.<locals>._fn   s   !#//1
 XX//1 1	#(#B#B#D "'"L"L"N#QQSO==? ) "??,L113K#ll88:O!Nzz&&(!&!9!9!;88:J!&!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,,^<//
;:L%%7||~ fmmo..ef~_1	 1	2 xx99;q@ g@   "**?;((>>?ST22'? -**?;''4HHAAC , 03NNHH@@B!-JJ,,^<//
;:L%%7||~ fmmo..ef~_1	 1	s&   F.U5-N74F9U57F;U22U55U>r   z_P.argsr   z	_P.kwargsr   rs   )	functoolswraps_torchdynamo_orig_callable)r   r   s   ` r|   preserve_global_stater      s0     __R8 8t &(C"Jr{   c                p   | 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 t        j!                  d| j                   j"                  | j                   j$                  | j                   j&                         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   rz   .0v
has_tensors     r|   	<genexpr>z:has_tensor_in_frame.<locals>.has_tensor.<locals>.<genexpr>J  s     ">Q:a=">   c              3  .   K   | ]  } |        y wr   rz   r   s     r|   r   z:has_tensor_in_frame.<locals>.has_tensor.<locals>.<genexpr>Q  s     "AQ:a="Ar   N_fieldsc              3  B   K   | ]  } t        |              y wr   )getattr)r   r   r   r   s     r|   r   z:has_tensor_in_frame.<locals>.has_tensor.<locals>.<genexpr>W  s     "T1:gc1o#>"Ts   )r   
isinstancer   TensornnModulera   type
issubclassr'   trace_numpyrj   ndarraygenericlisttupleanydictvaluesstrintfloatr   r`   hasattrr   )r   obj_idr   r   r   s   `  r|   r   z'has_tensor_in_frame.<locals>.has_tensor6  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{   z0skipping because no torch.* %s             %s %sF)r   objectr   r   )f_coder6   co_names	f_globalsr   r   rw   
startswithr   rj   r'   r   rR   f_localsr   logdebugco_nameco_filenameco_firstlineno)framer  r   valuer   r   s       @@r|   has_tensor_in_framer  !  s     ||33 <<(( 	eoo%//'*C#z*''1SE\f((cRi8C=	 !#H(V &&( e II	  ## r{   c                    d }t        | d      r)t        | |      }t        || j                         || _        t        | |       y )Nexec_recordexport)r  r]   rh   r  record_filenamer:   )ecoder  r  r  s        r|   exception_handlerr  p  s?     Oq- .q$7_amm<+&)r{   z/typing.Counter[Union[int, FrameStateSizeEntry]]FRAME_COMPILE_COUNTERc                <    t         j                  rt        |       S | S r   )r'   cprofilecprofile_wrapper)funcs    r|   maybe_cprofiler    s    %%Kr{   c                D     t        j                         d fd       }|S )Nc                 P   t        j                         }|sJ d       t        dj                   dt	        |      j                  dd       d      }t        j                         }|j                          t        j                         } |j                  g| i |}t        j                         |z
  }|j                          t        j                  dj                  ||       t        j                  |      }	 |j!                  |       t        j                  d|       |j'                  d	      }		 t)        j*                  d
dddddt	        |      gt(        j,                        }
t)        j.                  dddt	        |	      g|
j0                         t        j                  d|	       t?        t	        |            xr#t@        jB                  jE                  dfd       |S # t"        $ r t        j%                  d|       Y w xY w# t2        $ r t        j                  d       |j5                  t        j6                  j8                        j;                  d       |j5                  t        j6                  j<                        j;                  d       Y w xY w)NzTrace id is Nonez/tmp/r   /z.profilez7### 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)nameurlrz   )manifold_links   r|   r   z;cprofile_wrapper.<locals>.profile_wrapper.<locals>.<lambda>  s    !8O r{   )#r   current_trace_idr   rw   r   replacecProfileProfileenabletimeruncalldisabler
  warningr$  Stats
dump_statsPermissionError	exceptionwith_suffix
subprocessPopenPIPE
check_callr%  FileNotFoundError
sort_statsSortKeyTIMEprint_stats
CUMULATIVEr   r   _loggingtrace_structured)r   r   trace_idprofile_pathprofstart_tsretvalprofile_latencypssvg_pathgprof2dot_processr.  r  s              @r|   profile_wrapperz)cprofile_wrapper.<locals>.profile_wrapper  sH   !224+++xDMM?!CM$9$9#s$C#DHM
 !99;d4T4V4))+0EMM		
 \\$	>OOL) 	'6++F3	E * 0 087-% "! !!s8}5'.. KK:HE @
 
= 
 NN++O K  	>MM.=	>, ! 	EKKN MM&..--.::2>MM&..334@@D	Es&   G/ ;A9H /HHBJ%$J%r   )r   r   )r  rR  s   ` r|   r  r    s'    __T: :x r{   c                  d    e Zd Z	 	 	 d	 	 	 	 	 	 	 	 	 ddZed	d       Zdd	 	 	 	 	 	 	 	 	 	 	 d
dZy)ConvertFrameAssertNc                P    t                || _        || _        || _        || _        y r   )rd   r   
_one_graph_export_export_constraints)r   compiler_fn	one_graphr  export_constraintss        r|   r   zConvertFrameAssert.__init__  s(     	&'*5'##5 r{   c                      fdS )Nc                \    t        | j                  j                  j                        S r   )convert_frame_assertrV  rW  rX  backendr   s    r|   r   z8ConvertFrameAssert._clone_with_backend.<locals>.<lambda>  s'    3OOLL$$	 
 r{   rz   r   s   `r|   _clone_with_backendz&ConvertFrameAssert._clone_with_backend  s    
 	
r{   r   skipc                  t                |j                  }t        ||      }t        j	                  |       |t
        v r
t               S t        j                  j                  d      r6t        j                  j                  d      |j                  k7  r
t               S |j                  dk(  r%|j                  j                  d      r
t               S |j                  dk(  r
t               S |j                  dk(  rZ|j                  j                  t        j                  j                  t         j"                  j$                              r
t               S |j                  dk(  r|j                  dk(  r
t               S |j                  dk(  r.|j                  dk(  rt'        |j(                        s
t               S t+        |      r t-        d	d
ddgt.        j0                         t3        |      s
t               S t5               ad|vrt8        |d<   t8        dz  a|d   }t;        |t<              sJ t>        |   }	t>        |xx   dz  cc<   d }
tA        jB                         x}r|jD                  }
tG        |
||	      }tI        dd|j                  |tK        |      |j                  |jL                  |jN                  |jP                  d       |j                  j                  tR              sG|j                   d|j                   d|jL                   }tT        jV                  jY                  |       t[        tA        |            5  t]        |j                  |j^                  |j`                  |j(                  |jb                  | jd                  | jf                  | jh                  | jj                  |||||||dz         cd d d        S # 1 sw Y   y xY w)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hints_idr&   )compiled_autograd_idframe_idframe_compile_iddynamoz_convert_frame_assert._compile)r  rp  
compile_idr  r  
cache_sizeaccumulated_cache_size :)frame_staters  rc  )6r_   r  r3   input_codesr   output_codesrS   osenvirongetr  r  endswithr  pathdirnamer   optim__file__r   
f_builtinsr/   rD   r)   FUNDAMENTALr  r   r   FRAME_COUNTERr   r   r  r   current_compile_idro  r   r   r   r  +num_cache_entries_with_same_id_matched_objsnum_cache_entriesrL   r7   traced_frame_infosr   r   _compiler  r	  closurer   rV  rW  rX  )r   r  cache_entryhooksrx  rc  r  rt  rp  rq  ro  priorrs  infos                 r|   __call__zConvertFrameAssert.__call__  sS    	||'{;
<%''JJNN78

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

 &''
 <<:%$*:*:j*H%'' LLJ&  J.))* &''4Z5 '22		 #5)%''  01 #!.KQMu%(C(((0:h'1,'#"557757#(#=#= !5-

 	,<<$!*o#//"&"5"5(TT*4*F*F	
 ||&&'DEll^1T%5%5$6a8K8K7LMD))006^J78 	  //(('%AX!	 	 	s   A8O		OTFN)
rY  rk   rZ  r   r  r   r[  Optional[typing.Never]r   r   )r   z*Callable[[CompilerFn], ConvertFrameAssert]r  ro   r  Optional[CacheEntry]r  rI   rx  *dict[str, Union[int, FrameStateSizeEntry]]rc  r   r   rS   rw   rx   ry   r   propertyra  r  rz   r{   r|   rT  rT    s     5966 6 	6
 36 
6 
 
 CC *C 	C
 @C C 
Cr{   rT  c                    t        | |||      S )z&Fully convert a frame into an FX graph)rT  )rY  rZ  r  r[  s       r|   r^  r^  j  s     k9f>PQQr{   )OrderedDict)RemovableHandle)OutputGraphzdict[int, BytecodeHook]_bytecode_hooksc                J    t        t              }| t        |j                  <   |S )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{   rb  c               Z   
-./01234567 ddl m.m}m7m} d1d 2d 6t
        j                  j                         5t        	 	 	 	 	 	 d>. 024567fd       }t        d      	 	 	 	 	 	 	 	 	 	 d?-fd       }t        	 	 	 	 	 	 	 	 	 	 d?
0123fd       -t               }t        t        j                        5  t        t!                    5  t#        d	d
d
      5  |5  t%               3t'               4t)               x}rt+        |t-                     0nd 0d }t/        |      r|rt1        
|      }|sdn|d   }|j3                  d|i       t5        |      \  }}|rd@d}t6        j9                  d|t;        t        |       |       |t<               t        j>                  rtA        | d      rtA        | d      tC        d      rtE        | d      tG        dd| d| dg        t6        jI                  d jJ                   jL                   jN                  dz   djQ                  tS        jT                  dz         jW                                      tY        jZ                  t\              /t
        j^                  ja                  d /fd       tc        jd                         }d }d }d }d }t
        jf                  jh                  jj                  jm                          d } 	  | |	|      } to                | t
        jf                  j                  jp                  r@ts        dd !      5  t6        ju                  d"       tw        jx                  d#       d d d        6ri 6jz                  _>        d#d$l4m?}! t        |!      }"|2t        2j                        }#t        2j                  j                        }$2j                         }%t        2j                  j                        }&t        2j                        }'2j                  D (ch c]  }(|(j                   })}(2j                  D (ch c]  }(|(j                   }*}(t
        jf                  jh                  jj                  j7                          nDd }#d }$d }%d }&d }'t%        i       })t%        i       }*t%               3tc        jd                         |z
  d%z  1i d&|"d' jJ                  d( jL                  d) jN                  d*|j                  d+|j                  d,|#d-|$d.|%d/|&d0|'d1|d2|d3|d4|d5|)d6|*31| d ut        j                  t        j                  t        j                  d
t        1      d7}+|j3                  |+       cd d d        cd d d        cd d d        cd d d        S # 1 sw Y   DxY wc c}(w c c}(w # t        $ r},t        |,      j                  }t        |,      }t        |, |8       t
        j^                  ja                  d9d: d; <       t        j                  |,      \  }}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# t
        jf                  j                  jp                  rMts        dd !      5  t6        ju                  d"       tw        jx                  d#       d d d        n# 1 sw Y   nxY w6ri 6jz                  _>        d#d$l4m?}! t        |!      }"|2t        2j                        }#t        2j                  j                        }$2j                         }%t        2j                  j                        }&t        2j                        }'2j                  D (ch c]  }(|(j                   nc c}(w })}(2j                  D (ch c]  }(|(j                   nc c}(w }*}(t
        jf                  jh                  jj                  j7                          nDd }#d }$d }%d }&d }'t%        i       })t%        i       }*t%               3tc        jd                         |z
  d%z  1i d&|"d' jJ                  d( jL                  d) jN                  d*|j                  d+|j                  d,|#d-|$d.|%d/|&d0|'d1|d2|d3|d4|d5|)d6|*31| d ut        j                  t        j                  t        j                  d
t        1      d7}+|j3                  |+       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        y # 1 sw Y   y xY w)ANr   )bisectBisectValidationExceptiontranslation_validation_enabledValidationExceptiong        c                   j                          t               }t        | |	
|      	 t        j                  j
                        5  j                         5  j                          d d d        d d d        j                  j!                          j                  J j"                  sJ j"                  | d d  |j%                  j&                         t)        |        t+        |        t-        t/        |             | d d  y # 1 sw Y   xY w# 1 sw Y   x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!                          w xY w)N)rx  speculation_logexn_vt_stackdistributed_state)restartrN   rO   r   outputtracing_contextset_current_txrunr(   UnspecializeRestartAnalysisr   SpeculationRestartAnalysisTensorifyScalarRestartAnalysis	SkipFrame	Exception	shape_envcall_cleanup_hooksoutput_instructionsupdatecode_optionsr0   r-   r,   r+   )instructionsr  r  r  builtinsr  r  rY  r  r  r[  rx  r   localsrZ  r  r  tf_mode_stacktracerr  s      r|   	transformz_compile.<locals>.transform  s    	!%'&#+%/!
&	/667 9N9N9P 

   MM,,.!!!)))) 44QF//0(6(601A,1OPQ3   .. 	!!#**..MM
 	
  	-/v}}../	
 MM,,.sH   D1 D%(D9D%D1 D"	D%%D.*D1 1BF44F7 7Gcompile_inner)
phase_namec                   t        j                         5 }|j                  t        ddd             |j                  t	        d      j                                |j                  t        j                  j                  j                                |j                  t        j                                 | |||      cd d d        S # 1 sw Y   t               S xY w)Nz_compile.compile_innerentire_frame_compile!dynamo_cumulative_compile_time_us)r  dynamo_compile_column_usz#pytorch.wait_counter.dynamo_compile)r   r   r   r[   r    guardr   _dynamocallback_handlerinstall_callbacksrY   recordrS   )r  rZ  r  r  stack_compile_inners        r|   r  z_compile.<locals>.compile_inner  s     !!# 	Eu,5-P BCIIK  > > P P RS = D D FG!$	5)D	E 	E 	E   	
s   B*C

Cc           	        t        j                          x}}	 	 	 	 	 	 	 	 	 	 	 	 dd} |d| j                  | j                  | j                  |        d }t	        j
                         D ])  }|t        j                         _        	 t        | |      } n j:                  J d       |J  |d| j                  | j                  | j                  |       t<        j?                         D ]  }
 |
| |      }||} | t@        |<   tB        j+                  |       ||z
  J dd}|J  ||       } ||      }d}|d| jD                  d |  dz  }|d|jD                  d |  z  }| jD                  d | |jD                  d | k(  sJ |       d}|d| jF                   dz  }|d|jF                   z  }| jF                  |jF                  k(  sJ |       d}|d| jH                   dz  }|d|jH                   z  }| jH                  |jH                  k(  sJ |       jJ                  rjM                         r
t9               S jN                  J jP                  tR        jT                  |<   tW        | |r|jX                  nd       }t/              nd}d|z   }t[        ||j\                  |      }jM                         s'|j^                  |j_                  jN                         ta        |      S # t        j                  $ r}	t        |	t        j                        st        j                          t         j#                  dt%        t&        |	j(                               j+                  |	j,                  xs t/        t1        |	                   t        j                          }|dkD  rt3        dddg        Y d }	~	_d }	~	wt        j4                  $ r}	t        |	t        j                        st        j                          t         j7                  d	|	| j                  | j                  | j                         |rt         j7                  d
       t9               cY d }	~	c S d }	~	ww xY w)Nc           	         t         j                  t        j                        r#t         j	                  t        | ||||             y y r   )bytecode_logisEnabledForloggingDEBUGr  r\   )prefixr,  filenameline_nor  s        r|   log_bytecodez6_compile.<locals>._compile_inner.<locals>.log_bytecode	  s9     ((7""#FD(GTJ 8r{   zORIGINAL BYTECODEzRestarting analysis due to %sd   z!Excessive RestartAnalysis() callsrh  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.ri  z.Skipping frame %s %s                     %s %sz%No graph captured with one_graph=Truez;compiler collective wasn't run before compilation completedzMODIFIED BYTECODEc                    dd l }| j                  | j                  z   t        | j                  |j
                  z        z   t        | j                  |j                  z        z   S )Nr   )inspectco_argcount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{   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 UnknownzTorch-Compiled Region: )r  r   r,  r   r  r   r  r   r  r   r   r   r  r   r   r   )1r4  r  r  r  	itertoolscountr   r}  attemptr1   r(   RestartAnalysisr   r  r   r   r
  r  rb   r%   __traceback__r   restart_reasonr   r   rD   r  r  rS   
all_statesr  r   rc   rz  co_varnamesco_freevarsco_cellvarsr  is_empty_graphr   cleanupsrX   instancerF   guard_fail_fnrH   guard_managerguard_export_fnrV   )r  rZ  r  r  last_attempt_start_time
start_timer  out_coder  r  r  hook_outputr  total_argcount_oldtotal_argcount_newmsgcheck_fncompile_id_strannotation_strguarded_coder  rs  r  dynamo_time_before_restartr  restart_reasonss                       r|   r  z _compile.<locals>._compile_inner  s    04yy{:*		"	.1	<?	GO		 	LL	
  ( &	,G+2N ($,0yA	&	,P !(,=,H,H,T 	
I	
T ###LL	
 $**, 	'DtX.K&&	'
 #'h"%<z%I"!!!	 ###'-'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%''}}(((,2OO)'#(Ed	
 -7,BZ	2^C"""	
 $$&5+@+@+L !!&--0 ..G && !!S%G%GH"((*35qG
  ##A$4$4$DDGE*.))+'S=$ C "%6 ! == ,!!S%G%GH"((*		LL$$'' IIEF)++,s,   K99Q/B5OQ/BQ*"Q/*Q/rr  T)reset_event_log_on_exitlog_pt2_compile_eventz$Unable to find recompilation reasonsrecompile_reasonc                V    d| j                    d| j                   d| j                   dS )N'z' (rw  ))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 %s.zK reached, because fail_on_recompile_limit_hit = True this is a HARD failurea1   reached with one_graph=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.ri  zBtorchdynamo start compiling %s %s:%s, stack (elided %s frames):
%s   rh  rb  dynamo_startc                 .   dt        t        j                  fdt        j                  t        j                  dz         j                                            j                   j                  t        j                   j                        dgz   iS )Nr  c                    | d   k7  S )Nr  rz   )fconvert_frame_interns    r|   r   z,_compile.<locals>.<lambda>.<locals>.<lambda>  s    !J-3G"G r{      rb  )liner,  r  )r   r  	takewhiler   from_tracebackr$   extractsummaryr  r  intern_stringr  )r  r
  rc  s   r|   r   z_compile.<locals>.<lambda>	  s    ''G"11-551t8DLLN !% 3 3 $$.$<$<T=M=M$N r{   gc
gc_time_us)r  z run_gc_after_compile: running gcr&   )
curr_frameg    eA	frame_keyr  r  r  rt  ru  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config_suppress_errors config_inline_inbuilt_nn_modulesspecialize_float
is_forward%dynamo_compile_time_before_restart_usr  artifactc                     dddS )Ndynamo_errorstringr,  encodingrz   rz   r{   r|   r   z_compile.<locals>.<lambda>;  s    * (% r{   c                 *    t        j                         S r   	traceback
format_excrz   r{   r|   r   z_compile.<locals>.<lambda>?  s    9#7#7#9 r{   metadata_fn
payload_fnz: )r  zlist[Instruction]r  dict[str, object]r   r   )
r  r   rZ  r   r  rI   r  z2Callable[[list[Instruction], dict[str, Any]], Any]r   rS   )r  r   r   r   )btorch.fx.experimental.validatorr  r  r  r  r   	overrides _get_current_function_mode_stackr   r   r  r^   r   r'   use_lazy_graph_moduler   r   rW   r   rQ   r   rM   rP   r5   rG   update_outerr4   r
  r7  r   rg   fail_on_recompile_limit_hitr<   r   r?   rD   r  r  r  r  joinr$   r  formatr   r  r  rG  rH  r4  time_nsr  utilsReinplaceCountersr   rJ   run_gc_after_compiler[   r  r  collectr  local_scoper  r   lenr   r  count_callsgraphnodesplaceholdersr  ry   r   r  r  suppress_errorsinline_inbuilt_nn_modulesr%  rf   r  r   r  r(   get_exc_messager   rE   rB   r;   AssertionErrorr   r   rC   r@   r>   with_tracebackr  )8r  r   r  r  r  rY  rZ  r  r[  r  r  rt  r  rx  rs  rc  r  r  r  r  metrics_context
compile_pgr   reasonsexceeded
limit_typer  start_time_nsr  r  r  r  r  r  r  r  r  r  r  r  opr  r   metricsr  r  r  r
  r  r  r  r  r  r  r  r  s8   ````````` `  ```                             @@@@@@@@@@@r|   r  r    s
   &  ),$(F.2F 	88:  4Q'4Q7H4Q	4Q 4Q 4Q 4Ql #o>


 
 F	

 

 ?
2 b/b/b/ b/ F	b/
 
b/ b/ b/H *+Ov;;<|2z23|2 	d$	
|2 	|2 %(E(*')):) 0Z\ J $ +/J'E=k5QG>E67ST:  	$$&8:J%KL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  *77A''	
( #'	%)26 04--335	2(y%KL ^ }}##88!$N "HH?@JJqM" ,.))JI"v'9!&--0(+F,<,<,C,C(D%!'!3!3!5#&v||'9'9#: $'(;(;$<!?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$<*0*@*@4:4T4T$*$;$;"9B.:3G@ ((1y|2 |2 |2 |2 |2v" "  %Y(A  '	< Q,,Ia&KauV<NN++ : ,  @C?R?R:@<$&< %)",/'0-$  /Aw++,Bs1vh7 .1t<K'	<\ }}##88!$N "HH?@JJqM" " " ,.))JI"v'9!&--0(+F,<,<,C,C(D%!'!3!3!5#&v||'9'9#: $'(;(;$<!?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$<*0*@*@4:4T4T$*$;$;"9B.:3G@ ((1y|2 |2 |2 |2 |2 |2 |2 |2 |2 |2 |2s   f!#f2e75He"	W1e"	+V:8B6e"	.We"	W$D1e"		e7	f'	f!:W?e"		[C)[[[2e=+\1(	e1\:6B5e+_?>e`('D8ee"	"e+'e7.	f7f <f	f!f	f!!f*c                  T    e Zd Z	 	 	 	 	 	 ddZedd       Z	 d	 	 	 	 	 	 	 	 	 	 	 ddZy)	ConvertFramec                D    || _         t        |d      | _        || _        y )NF)rZ  )r   r^  _inner_convert_hooks)r   rY  r  s      r|   r   zConvertFrame.__init__  s"    
 +6'2;%Pr{   c                      fdS )Nc                0    t        | j                        S r   )convert_framerZ  r_  s    r|   r   z2ConvertFrame._clone_with_backend.<locals>.<lambda>  s    }WdkkB r{   rz   r   s   `r|   ra  z ConvertFrame._clone_with_backend  s	    BBr{   c                   t         d   dxx   dz  cc<   	 | j                  |||||dz         }t         d   dxx   dz  cc<   |S # t        $ r}t        |t              r t        |t
              }|rt        j                  t        j                        rt        |d      rt        |d      rt        t        |j                              5  |j                  }	dj                  t!        j"                  |	            }
d	|
 t$        j&                  j)                  d
d fd       t        j+                  d       d d d        n# 1 sw Y   nxY wt,        j.                  s|s t1        |dd       }|j2                  }t5        ||||      }|rt6        j9                  |d       nt6        j;                  |d       t        |t<              r7t?        tA        tB        jD                  tB        jD                              cY d }~S t        |tF              r7t?        tA        tB        jH                  tB        jH                              cY d }~S Y d }~t?               S d }~ww xY w)Nframestotalr&   rb  okrs  
real_stackrh  z&Graph break: skip: from user code at:
r(  c                     dddS )Ndynamo_graph_break_reasonr+  r,  rz   rz   r{   r|   r   z'ConvertFrame.__call__.<locals>.<lambda>  s    (C,41 r{   c                 6      dt        j                          S )N
r/  )user_stack_traces   r|   r   z'ConvertFrame.__call__.<locals>.<lambda>  s    2B1C2iFZFZF\E]/^ r{   r2  T)exc_infor  )frame_exec_strategy)%rZ   rY  r  r   rC   rE   graph_break_logr  r  r  r  r   r   rs  rb  r<  r0  format_listr   rG  rH  r  r'   rI  r   r  r=   r
  r  r7  rA   rS   rU   rT   SKIPr?   RUN_ONLY)r   r  r  r  rx  rc  r   r  	soft_fail
user_stackuser_stack_formattedr  r  	error_msgrg  s                 @r|   r  zConvertFrame.__call__  s)    	7#q(#Q	(({E;TAX ) F Xt$)$M K	 !9:"1k2I _99'--H 1l+<0H()EF %&\\
/1ww%11*=0, .UUiTj+k(77&) (_ 8  (--,%) .   & ))) &a):DAO<<D(D/5IIT2I5!78)(9#((+*:*:) 
 A56)(9#,,k.B.B)  7 "##[K	sE   .A 
I A<IA3E?	IE	B3I I AII I N)rY  rk   r  rI   r   r   )r   z*Callable[[WrapBackendDebug], ConvertFrame])r   r  r  rz   r{   r|   rW  rW    s      
	 C C \$\$ *\$ 	\$
 @\$ \$ 
\$r{   rW  c                    t        | |      S )zHTry to convert a frame into an FX graph, if error leave frame unmodified)rW  )rY  r  s     r|   r]  r]    s    U++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                  |j                  |j                  |j                  |j                   |ddd t#               t%        dd      d d i t'        dd      	       |t        _        y # 1 sw Y   xY w# |t        _        w xY w)
Nr&   )eagerFrbr   *   i  )rp  rq  )
rY  rZ  r  r[  r  rt  r  r  rx  rs  )backends.debuggingrt  r'   replay_record_enabledopenrK   loadr   r  chainr   itemsr  r  r  r  r  rI   r2   r   )r  rt  original_replay_valin_filer  s        r|   replayr    s    ) 66#(F 	h	 / %%g./)//&..*>*>*@')//BSTUFN;KKNNMMOONN#'0A6 "sC	
$ (;$// /. (;$s   DA)D  D D-c                    t         j                  dk  ryt        j                  |       D ]"  }|j                  dk(  s|j
                  dz  c S  t        d      )N)      r   RESUMEr  z$RESUME instruction not found in code)sysversion_infodisget_instructionsopnameoffsetRuntimeError)r  insts     r|   first_real_inst_idxr  7  sV    
'!$$T* $;;(";;!##$ =
>>r{   c                  0    e Zd Zdd	 	 	 	 	 	 	 	 	 	 	 ddZy)ConvertFrameProtocolr   rb  c                    y r   rz   )r   r  r  r  rx  rc  s         r|   r  zConvertFrameProtocol.__call__A  s     !r{   Nr  )rw   rx   ry   r  rz   r{   r|   r  r  @  sG     !! *! 	!
 @! ! 
!r{   r  c                  ,    e Zd ZddZ	 	 	 	 	 	 	 	 ddZy)CatchErrorsWrapperc                V     t        j                  |      |        || _        || _        y r   )r   r   r   r  )r   callbackr  s      r|   r   zCatchErrorsWrapper.__init__M  s$    !	!$'*2'
r{   c           	        |J t        j                  |j                        }t        j                  dk\  r#|j
                  t        |j                        kD  }n"|j
                  t        |j                        k\  }|s5|s3t        j                  s#t        d      rt        | j                  dd      st        j                  t        j                        rx|rd}n3t        j                  |j                        rd}nt        d      rd}nd}t        j!                  d	|j                  j"                  ||j                  j$                         t'               S |j                  j$                  d
k(  r#|j                  j"                  dk(  r
t'               S t(        j*                  j,                  j/                         dk(  rt1        j2                         }|rt4        5  ddlm}  ||j:                  | j                  j                        }	t=        | j                  d      sJ d       | j                  j?                  |	j@                        }
 |
||| jB                  |      cd d d        S t4        5  tE               5  | j                  ||| jB                  |d      cd d d        cd d d        S # 1 sw Y   MxY w# 1 sw Y   nxY wd d d        y # 1 sw Y   y xY w)N)r     F)include_infra_modesrW  ztraced frame alreadyzin skipfileszSnon-infra torch dispatch mode present, this is not supported today in torch.compilezdynamo tracing is disabledz#skipping: %s (reason: %s, file: %s)rg  __new__ddp_optimizerr   )DDPOptimizer)bucket_bytes_capbackend_compile_fnra  zJDDPOptimizer only supports callback fns that know how to clone themselves.r&   rb  )#r*   r   r  r  r  f_lastir  r'   r6  r#   r   r   r
  r  r  r  r  r  r  rS   r   r  r?  get_optimize_ddp_moder!   _get_active_ddp_modulecompile_lock"torch._dynamo.backends.distributedr  r  r  ra  
compile_fnr  r"   )r   r  r  rx  is_skipfilehas_started_executionskip_reason
ddp_moduler  r  hijacked_callbacks              r|   r  zCatchErrorsWrapper.__call__R  sj    &&&!''5w&$)MM4G4U$U!$)MM5H5V$V! "~~)eD ? ?ER .("8K &&u||4"0K.5I"wK">K		9LL((LL,,	 &''<<##z1ell6J6Ji6W%''==446/I0GGIJ! O$0)3)D)D+/+J+J+e+e%M #779N  e  77KK)44 &
 -{DJJ# *  	13 	22{DJJ! 3 	 	 	+ *	 	 	 	 	s1   BJ.0K; J:	K.J7:K	?KKN)r  r  r  rI   r   r   )r  ro   r  r  rx  r  r   rS   )rw   rx   ry   r   r  rz   r{   r|   r  r  L  s8    
HH *H @	H
 
Hr{   r  c                    t        | |      S r   )r  )r  r  s     r|   catch_errors_wrapperr    s     h..r{   r   )r   r   r   zdict[str, Any]r   zOptional[dict[str, str]]r   r   )r   Callable[_P, _T]r   r  )r  ro   r   r   )NF)
r  r  r  r   r  Optional[DynamoFrameType]r  r   r   r   )r  r  r   r  r  )
rY  rk   rZ  r   r  r   r[  r  r   rT  )r  rm   r   r  )NN)"r  r   r   r5  r  r5  r  r5  r  ztuple[CellType]rY  rk   rZ  r   r  r   r[  r  r  rI   r  r  rt  r2   r  r  rx  z4Optional[dict[str, Union[int, FrameStateSizeEntry]]]rs  r   rc  r   r   rS   )rY  rk   r  rI   r   rW  )r  r   r   r   r  )r  r  r  rI   r   r  )__doc__
__future__r   collectionsr   r1  r  r   r  r  r  r{  r$  r   r=  r  	threadingr4  r0  typingr   pathlibr   typesr   r   r   r   r	   r
   r   r   r   typing_extensionsr   r   r   torch._loggingtorch._C._dynamo.guardsr   torch._dynamo.distributedr   torch._dynamo.symbolic_convertr   torch._guardsr   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#   torch.utils._tracebackr$   r%   rh  r'   r(   r)   r*   bytecode_analysisr+   r,   bytecode_transformationr-   r.   r/   r0   r1   rt  r2   r3   r4   r5   
eval_framer6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   r   rF   rG   rH   r  rI   pgorJ   replay_recordrK   resume_executionrL   symbolic_convertrM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   r?  rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   rf   rg   rh   variables.torch_functionri   __annotations__numpyrj   ModuleNotFoundErrorTYPE_CHECKINGbackends.registryrk   repro.after_dynamorl   rm   rn   ro   variables.builderrp   	getLoggerrw   r
  rG  getArtifactLoggerr  rj  RLockr  rs   rt   rv   r~   ry  rz  r   r   r   r   #suppress_torch_distributed_warningsr  r  r  Counterr  r  r  rT  r^  r  torch.utils.hooksr  output_graphr  r  r  r  rW  r]  r  r  Protocolr  r  r  rz   r{   r|   <module>r     si  & #    
  	   	    
       > > : : ' !   4 4 9 M M %  ? Q & A M 9 9 G       
   * ;  " X X    ( J  
 
-4@@6 g!~~//*E..228^L y T]t_	 	 ( iy37 0 7 *+MQ	%2J ,EP 11K 2Kb (,	**
* %* 	*
 
* K F 
>B[ [@ 15	RRR R /	R
 R $ - 
) ,7=( 8. (,HLW2" #W2
W2W2 W2  	W2
 W2 W2 W2 W2 /W2 W2 &W2 *W2 %W2 FW2  !W2" #W2$ %W2vj$ j$Z,;>?	!6?? 	!N Nb/"/+0//G(  	Bs   2N N&%N&