a
    h                     @  s  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- ddl.m/Z/m0Z0m1Z1m2Z2 ddl%m3Z3 ddl4m5Z5m6Z6m7Z7m8Z8 ddl9m:Z: ddl;m<Z<m=Z= ddl>m?Z@ ddlAmBZB ddlCmDZD ddlEmFZFmGZG ddlHmIZImJZJ ddlKmLZLmMZMmNZNmOZOmPZP ddlQmRZRmSZS ddlTmUZUmVZVmWZWmXZXmYZY ddlZm[Z[m\Z\m]Z]m^Z^ ddl_m`Z`maZambZbmcZc ddlNmdZdmeZemfZfmgZgmhZhmiZimjZjmkZkmlZlmmZmmnZnmoZompZp ddlqmrZrmsZsmtZt ddlumvZv dd lwmxZxmyZy dd!lzm{Z{ dd"l|m}Z} dd#l~mZmZmZmZmZ dd$lPmZ dd%lmZmZmZmZ dd&lmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ dd'lmZ d(ed)< zddlZW n ey   dZY n0 ejrdd*lmZ dd+lmZ dd,lmZ dd-lmZmZmZ dd.lmZ e
eZe$jed/Ze$jed0Ze Zed1Ze"d2ZG d3d4 d4ZG d5d6 d6Ze Ze Zdad7ed8< ee@dd9d:d;d<d=d>d?Zd@d@dAdBdCZecjdDdEdFdGdHZddJdKdLdEdMdNdOdPZdaeǡ ZdQedR< d@d@dSdTdUZd@d@dSdVdWZG dXdY dYZdd[dEdEd\d]dYd^d_d`ZddalmZ ddblmZ ejrHddclmZ e̓ Zddede< dfdgdhdidjZddddkdKdldldldmd[dEdEd\dndodpdLdqdrdsd]dtdudvdwZG dxdy dyZdd[dnd]dydzd{d|Zd9dMd}d~dZdKdsdddZG dd dejكZG dd dZddnddddZdS )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.
    )annotationsN)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_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ShortenTracebackSkipCodeRecursiveExceptionTorchRuntimeErrorUncapturedHigherOrderOpErrorunimplemented_v2Unsupported)CheckFunctionManager'get_and_maybe_log_recompilation_reasonsGuardedCode)Hooks)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)chromium_event_timedCleanupManagerCompileTimeInstructionCountercountersdynamo_timedformat_bytecodegen_record_file_name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bytecodeZgraph_breaks_T_Pc                   @  s   e Zd ZdS )TODO_UNKNOWNN)__name__
__module____qualname__ r~   r~   I/var/www/auris/lib/python3.9/site-packages/torch/_dynamo/convert_frame.pyrz      s   rz   c                   @  sH   e Zd ZddddZdddddZdd	d
ddZddddZdS )TrackerNonereturnc                 C  s   g | _ t | _d S N)seensetseen_idsselfr~   r~   r   __init__   s    zTracker.__init__r   )
strong_objr   c                   sD   t |  jvr@t| fdd}j| j  d S )Nc                   s   j  S r   )r   remove)_idxr   r~   r   <lambda>       zTracker.add.<locals>.<lambda>)idr   weakrefrefr   appendadd)r   r   objr~   r   r   r      s
    
zTracker.addbool)itemr   c                 C  s   t || jv S r   )r   r   )r   r   r~   r~   r   __contains__   s    zTracker.__contains__c                 C  s   | j   | j  d S r   )r   clearr   r   r~   r~   r   r      s    
zTracker.clearN)r{   r|   r}   r   r   r   r   r~   r~   r~   r   r      s   r   zOptional[GlobalStateGuard]initial_global_statestrzdict[str, Any]zOptional[dict[str, str]]r   )srcglobals	co_fieldsr   c                 C  s   t | ||}t|j |S r   )original_forward_from_srcr9   __code__)r   r   r   resultr~   r~   r   fx_forward_from_src_skip_result   s    
r   zCallable[_P, _T])fnr   c                   s*   t  dddd fd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
    _P.args	_P.kwargsrx   argskwargsr   c                    s  t  }t }tj  t ~ t T t }t }t	 }tj
 }t }t }	tj }
d }tj rtj }tj }tjjj}ttjj_t }t }|tjj  |t zʈ | i |W |  tj dksJ d|  tj| tj j!"| tj#||d t$| tj%|
 t&|	 tj
 }||krjtj'  |d urtj%| tj(| |tjj_|) sJ d|*  dW  d    W  d    W  d    S |  tj| tj j!"| tj#||d t$| tj%|
 t&|	 tj
 }||krNtj'  |d urdtj%| tj(| |tjj_|) stJ d|*  dn|  tj dksJ d|  tj| tj j!"| tj#||d t$| tj%|
 t&|	 tj
 }||kr(tj'  |d ur>tj%| tj(| |tjj_|) srJ d|*  d0 W d    n1 s0    Y  W d    n1 s0    Y  W d    n1 s0    Y  d S )Nr   zQTorch function mode stack state changed while dynamo tracing, please report a bug)Z	warn_onlyzGlobal z7state changed while dynamo tracing, please report a bug)+r   torchZis_grad_enabledZ_CZ_PreserveDispatchKeyGuardrf   rg   Zis_inference_mode_enabledZ$are_deterministic_algorithms_enabledZ-is_deterministic_algorithms_warn_only_enabledZ$_is_default_mobile_cpu_allocator_setrandomgetstateZget_default_dtypeZget_rng_stateZcudaZis_availableZ_get_cublas_allow_tf32ZfxZgraph_moduler   r   rj   
contextlib	ExitStackenter_contextZ_symbolic_traceZ_maybe_revert_all_patchesrn   closeZ_len_torch_function_stackZ_set_grad_enabledZautogradZ	grad_modeZ_enter_inference_modeZuse_deterministic_algorithmssetstateZset_rng_stateZset_default_dtypeZ#_unset_default_mobile_cpu_allocatorZ_set_cublas_allow_tf32checkreason)r   r   guardsZprior_grad_modeZprior_inference_modeZprior_deterministicZprior_warn_onlyZprior_mobile_allocator_stateZpy_rng_stateZprior_dtypeZtorch_rng_stateZcuda_rng_stateZ
allow_tf32Zprior_fwd_from_srccleanupZ
exit_stackZcurr_mobile_allocator_stater   r~   r   _fn   s    













,    











z"preserve_global_state.<locals>._fn)	functoolswraps_torchdynamo_orig_callable)r   r   r~   r   r   preserve_global_state   s    	?r   ru   r   )framer   c                   s   | j tv rdS | j jD ]\}|| jv r| j| }t|trR|jdsL|tu rR dS t	rt
jr|t	u slt|r dS qi ddd fdd | j D ]} |r dS qtd| j j| j j| j j d	S )
z+Check if the frame has torch.* related bitsTztorch.objectr   )r   r   c                   sf  t  }|v r| S d|< t tjtjjfsLt tr\t tjjr\d|< | S t	j
rtrt tjs~t tjrd|< | S t ttfrtfdd D |< | S t t rt  }tfdd|D |< | S t ttttdtfr"d|< | S t r^t dr^t fdd jD |< | S dS dS )	z)Recursively check if the obj has a tensorFTc                 3  s   | ]} |V  qd S r   r~   .0v
has_tensorr~   r   	<genexpr>T  r   z:has_tensor_in_frame.<locals>.has_tensor.<locals>.<genexpr>c                 3  s   | ]} |V  qd S r   r~   r   r   r~   r   r   [  r   N_fieldsc                 3  s   | ]} t |V  qd S r   )getattrr   )r   r   r~   r   r   a  r   )r   
isinstancer   ZTensornnModulerd   type
issubclassr'   trace_numpyro   ZndarrayZgenericlisttupleanydictvaluesr   intfloatr   rc   hasattrr   )r   Zobj_idr   r   r   )r   r   r   @  sF    

z'has_tensor_in_frame.<locals>.has_tensorz0skipping because no torch.* %s             %s %sF)f_coder7   co_names	f_globalsr   r   r{   
startswithr   ro   r'   r   rU   f_localsr   logdebugco_nameco_filenameco_firstlineno)r   r   r   valuer~   r   r   has_tensor_in_frame+  s2    




+r   F	Exceptionr   zOptional[DynamoFrameType]r   )ecoder   exportr   c                 C  s:   d }t | dr*t| |}t|| j || _t| |d d S )Nexec_recordr   )r   r`   rm   r   record_filenamer;   )r   r   r   r   r   r~   r~   r   exception_handlerz  s    

r   z/typing.Counter[Union[int, FrameStateSizeEntry]]FRAME_COMPILE_COUNTER)funcr   c                 C  s   t jrt| S | S r   )r'   Zcprofilecprofile_wrapperr   r~   r~   r   maybe_cprofile  s    r   c                   s$   t  dddd fdd}|S )Nr   r   rx   r   c               	     s  t  }|sJ dtdj dt|dd d}t }z@|  t		 }|j
g| R i |}t		 | }|  W n. ty   td d}| i |}Y n0 tdj|| t|}z|| W n ty   td	| Y n0 td
| |d}	zNtjddddddt|gtjd}
tjdddt|	g|
jd td|	 W nF ty   td |tjjd |tjjd Y n0 t t|  rt!j"#d fdd |S )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.svgZ	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
   d dS )NZcprofile_manifold_url)nameurlr~   r~   Zmanifold_linkr~   r   r     r   z;cprofile_wrapper.<locals>.profile_wrapper.<locals>.<lambda>)$r   Zcurrent_trace_idr   r{   r   replacecProfileZProfileenabletimeZruncalldisable
ValueErrorr   	exceptionwarningr   ZStatsZ
dump_statsOSErrorwith_suffix
subprocessPopenPIPE
check_callr   FileNotFoundErrorZ
sort_statsZSortKeyZTIMEZprint_statsZ
CUMULATIVEr   r   _loggingtrace_structured)r   r   Ztrace_idZprofile_pathZprofstart_tsretvalZprofile_latencyZpsZsvg_pathZgprof2dot_processr   r   r   profile_wrapper  sx    


	

z)cprofile_wrapper.<locals>.profile_wrapper)r   r   )r   r  r~   r   r   r     s    Ar   c                   @  sV   e Zd Zdddddddd	d
dZeddddZdddddddddddZdS )ConvertFrameAssertTFNrp   r   Optional[typing.Never]Optional[CompilePackage]r   compiler_fn	one_graphr   export_constraintspackager   c                 C  s(   t   || _|| _|| _|| _|| _d S r   )ri   r   
_one_graph_export_export_constraints_package)r   r  r  r   r  r  r~   r~   r   r     s    	zConvertFrameAssert.__init__z*Callable[[CompilerFn], ConvertFrameAssert]r   c                   s    fddS )Nc                   s   t |  j j jS r   )convert_frame_assertr  r  r  backendr   r~   r   r     s
   z8ConvertFrameAssert._clone_with_backend.<locals>.<lambda>r~   r   r~   r   r   _clone_with_backend  s    z&ConvertFrameAssert._clone_with_backendr   skipru   Optional[CacheEntry]rK   *dict[str, Union[int, FrameStateSizeEntry]]r   rV   r   cache_entryhooksframe_stater  r   c                C  s  t   |j}t||}t| |tv r.t S tj	drRtj	d|j
krRt S |j
dkrn|jdrnt S |j
dkr~t S |j
dkr|jtjtjjrt S |j
dkr|jdkrt S |j
dkr|jdkrt|jst S t|rtd	d
ddgtjd t|st S t }|r<d|jjv r<|j}q|rZ|jtju rZtddS t a d|vrzt!|d< t!d7 a!|d }	t"|	t#sJ t$|	 }
t$|	  d7  < d }t%&  }r|j'}t(||	|
d}t)dd|j
|	t*||j|j+|j,|j-d |j
t.s.|j
 d|j d|j+ }t/j01| t2t%|R t3|j|j4|j5|j|j6| j7| j8| j9| j:|||||||d | j;dW  d    S 1 s0    Y  d S )NZ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.Zgb_typecontextZexplanationhintsztorch/_dynamo/convert_frame.pyF)Zapply_to_codeZ_idr&   )compiled_autograd_idframe_idframe_compile_iddynamoz_convert_frame_assert._compile)r   r,  
compile_idr   r   
cache_sizeaccumulated_cache_size :)r$  r/  r  r  )<rb   r   r4   input_codesr   output_codesrV   osenvirongetr   r   endswithr   pathdirnamer   Zoptim__file__r   
f_builtinsr0   rF   r*   ZFUNDAMENTALr   sys	_getframef_backr(   Z"_nonrecursive_disable_wrapper_coder   r   FRAME_COUNTERr   r   r   r   current_compile_idr+  r   r   r   r   +num_cache_entries_with_same_id_matched_objsnum_cache_entriesrO   r8   Ztraced_frame_infosr   r   _compiler   r   closurer   r  r  r  r  )r   r   r"  r#  r$  r  r   r0  Z
prev_framer,  r-  r+  Zpriorr/  infor~   r~   r   __call__  s    	










zConvertFrameAssert.__call__)TFNNr{   r|   r}   r   propertyr  rH  r~   r~   r~   r   r    s       r  Trp   r  r  r  c                 C  s   t | ||||S )z&Fully convert a frame into an FX graph)r  )r  r  r   r  r  r~   r~   r   r    s    
r  )OrderedDict)RemovableHandle)OutputGraphzdict[int, BytecodeHook]_bytecode_hooksrs   rL  )hookr   c                 C  s   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.
    )rL  rN  r   )rO  handler~   r~   r   register_bytecode_hook  s    
rQ  )r  r  dict[str, object]ztuple[CellType]rK   r  r3   z4Optional[dict[str, Union[int, FrameStateSizeEntry]]]r   r   rV   )r   r   localsbuiltinsrF  r  r  r   r  r#  r"  r0  r   r$  r/  r  r  r   c          .   '     s	  ddl mm}mm} d
d d tj tdddd	fdd	}t	d
ddddddd fdd}t
dddddd	
fdd t }d urЈnt }ttj tt` tdddd. td  | tdddd || t t t  }r`t|t 	nd 	d }t|r|rt|}|sdn|d }|d|i t |\}}|rJdd d!d"d#}t!"d$|t#t|||t$ tj%rt&| d%nNrt&| d&n8t'd'r,t(| d(nt)d)d*| d+| d,g d- t!*d.j+j,j-d/ d0.t/j0d/ d11  t23t4tj56d2fd3d4 t78 }d }d }d } d }!tj9j:j;<  d }"zRz||	|}"t=  t> |"W W tj9jj?rDtd5d6d7$ t!@d8 tABd9 W d    n1 s:0    Y  rXi jC_Di _Ed9d:l:mF}# tG|#}$|d u rd urtHjI}%tHjJjI}&K }'tHjLjM}(tHjN})d;d< jOD }*d=d< jPD }+tj9j:j;!  n:d }%d }&d }'d }(d })ti }*ti }+t t78 | d> 
|$j+j,j-|jQ|jR|%|&|'|(|)||| |!|*|+
|"d utjStjTtjUdtV
d?},||, W  d    W  d    W  d    W  d    W  d    W  d    W  d    S  tWy }- ztX|-jY}tG|-}tZ|-|d@ tj5j6dAdBd4 dCd4 dD t[\|-\} }!t]|-t^t_t`tatbtc|td|tetffr^ n&tgtX|-jY dEtG|- h|-jid W Y d }-~-n
d }-~-0 0 W tj9jj?rtd5d6d7$ t!@d8 tABd9 W d    n1 s0    Y  ri jC_Di _Ed9d:l:mF}# tG|#}$|d u rd urtHjI}%tHjJjI}&K }'tHjLjM}(tHjN})d;d< jOD }*d=d< jPD }+tj9j:j;!  n:d }%d }&d }'d }(d })ti }*ti }+t t78 | d> 
|$j+j,j-|jQ|jR|%|&|'|(|)||| |!|*|+
|"d utjStjTtjUdtV
d?},||, ntj9jj?rptd5d6d7$ t!@d8 tABd9 W d    n1 sf0    Y  ri jC_Di _Ed9d:l:mF}# tG|#}$|d u rd urtHjI}%tHjJjI}&K }'tHjLjM}(tHjN})d;d< jOD }*d=d< jPD }+tj9j:j;!  n:d }%d }&d }'d }(d })ti }*ti }+t t78 | d> 
|$j+j,j-|jQ|jR|%|&|'|(|)||| |!|*|+
|"d utjStjTtjUdtV
d?},||, 0 W d    n1 s0    Y  W d    n1 s0    Y  W d    n1 s0    Y  W d    n1 	s0    Y  W d    n1 	s<0    Y  W d    n1 	s\0    Y  W d    n1 	s|0    Y  d S )FNr   )bisectBisectValidationExceptiontranslation_validation_enabledValidationExceptiong        zlist[Instruction]rR  r   )instructionscode_optionsr   c                   s     t }t| 
	||dzzjj  tjj@     W d    n1 sz0    Y  W d    n1 s0    Y  W nd t	j
y      Y nH t	jt	jt	jfy    Y n* ty    r jj  Y n0 W j  nj  0 jd us8J jsDJ j| d d < |j t|  t|  tt| | d d < d S )N)r$  speculation_logexn_vt_stackdistributed_stater  )ZrestartrQ   rR   outputZmark_bytecode_tracing_startr   Ztracing_contextZset_current_txrunr)   ZUnspecializeRestartAnalysisr   ZSpeculationRestartAnalysisTensorifyScalarRestartAnalysis	SkipFramer   	shape_envZcall_cleanup_hooksZoutput_instructionsupdaterZ  r1   r.   r-   r,   )rY  rZ  r\  )rU  rT  rF  r   r  r]  r   r  r$  r   rS  r  r^  r  r[  tf_mode_stacktracerrW  r~   r   	transform  s^    
H

z_compile.<locals>.transformcompile_inner)
phase_namer   r   rK   z2Callable[[list[Instruction], dict[str, Any]], Any]rV   )r   r  r#  rf  r   c              	     sj   t  L}|tjjtjt	t
  |t   | |||W  d    S 1 sZ0    Y  t S r   )r   r   r   r   _dynamoZcallback_handlerZinstall_callbacksr   ZDYNAMOr   r   rB  r\   recordrV   )r   r  r#  rf  stack)_compile_innerr~   r   rg    s    
,z_compile.<locals>.compile_innerc                   sF  t    }}ddddddddd}|d| j| j| j|  d }t D ]P}|t _zHt	d	| d
d t
| |}W d    n1 s0    Y  W  qW qF tjy( }	 znt|	tjst  tdtt|	j |	jptt|	 t   }|dkrtdddg d W Y d }	~	qFd }	~	0  tjy }	 zTt|	tjsPt  td|	| j| j| j |rxtd t W  Y d }	~	  S d }	~	0 0 qFd u sjd usJ d|d usJ |d| j| j| j| t  D ]}
|
| |}|d ur|}q| t!|< t"| || d us,J ddddd}|d usJJ || }||}d}|d| j#d |  d7 }|d|j#d |  7 }| j#d | |j#d | ksJ |d}|d| j$ d7 }|d|j$ 7 }| j$|j$ksJ |d }|d!| j% d7 }|d"|j% 7 }| j%|j%ks.J |j&rF' rFt S j(d usVJ j)t*j+|< t	d#d
dF t,|  |r|j-nd |r|j.nd rd$nd d%}W d    n1 s0    Y  d ur|j/d usJ 0|j/| d ur tnd&}d'| }t1||j2|}' s>|j3d ur>|3j( t4|S )(Nr   r   r   r   )prefixr   filenameline_nor   r   c                 S  s&   t tjr"t t| |||| d S r   )bytecode_logisEnabledForloggingDEBUGr   r_   )rm  r   rn  ro  r   r~   r~   r   log_bytecode)  s    z6_compile.<locals>._compile_inner.<locals>.log_bytecodezORIGINAL BYTECODEZcompile_attempt_T)log_pt2_compile_eventz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 %sz%No graph captured with one_graph=Truez;compiler collective wasn't run before compilation completedzMODIFIED BYTECODEr   r   c                 S  s4   dd l }| j| j t| j|j@  t| j|j@  S )Nr   )inspectco_argcountco_kwonlyargcountr   co_flags
CO_VARARGSCO_VARKEYWORDS)r   rx  r~   r~   r   
count_args  s    z4_compile.<locals>._compile_inner.<locals>.count_argsz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 Zbuild_guardssave)Zguards_serialization_modeUnknownzTorch-Compiled Region: )5r   r   r   r   	itertoolscountr   r8  attemptr^   r2   r)   ZRestartAnalysisr   r`  r   r   r   rG  re   r%   __traceback__r   Zrestart_reasonr   r   rF   ra  r   rV   Z
all_statesrN  r   rh   r5  co_varnamesco_freevarsco_cellvarsr   Zis_empty_graphr   Zcleanupsr[   instancerH   Zguard_fail_fnZguard_filter_fnZguards_stateZadd_guarded_coderJ   Zguard_managerZguard_export_fnrY   )r   r  r#  rf  Zlast_attempt_start_time
start_timert  Zout_coder  r   rO  Zhook_outputr~  Ztotal_argcount_oldZtotal_argcount_newmsgZcheck_fnZcompile_id_strZannotation_strguarded_code)r"  r/  r]  dynamo_time_before_restartr^  r  restart_reasonsr~   r   rl    s    

(


"



&	
z _compile.<locals>._compile_innerr.  T)Zreset_event_log_on_exitru  z+pytorch.wait_counter.entire_forward_compilez_compile.compile_innerZentire_frame_compileZ!dynamo_cumulative_compile_time_us)rh  dynamo_compile_column_usz$Unable to find recompilation reasonsrecompile_reasonr   rw  c                 S  s   d| j  d| j d| j dS )N'z' (r3  ))r   r   r   )r   r~   r~   r   format_func_info  s    z"_compile.<locals>.format_func_infoztorch._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.r(  zBtorchdynamo start compiling %s %s:%s, stack (elided %s frames):
%s   r'  r  Zdynamo_startc                
     sL   dt tfddttjd d  j j	t
 jdg iS )Nrk  c                   s   | d  kS )Nrn  r~   )f)convert_frame_internr~   r   r   @  r   z,_compile.<locals>.<lambda>.<locals>.<lambda>   r  )liner   rn  )r   r  	takewhiler   from_tracebackr$   extractsummaryr   r   intern_stringr   r~   )r   r  r  r~   r   r   =  s    


z_compile.<locals>.<lambda>gcZ
gc_time_us)r  z run_gc_after_compile: running gcr&   )
curr_framec                 S  s   h | ]
}|j qS r~   r}   r   opr~   r~   r   	<setcomp>  r   z_compile.<locals>.<setcomp>c                 S  s   h | ]
}|j qS r~   r  r  r~   r~   r   r    s   g    eA)	frame_keyr   r   r   r0  r1  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_opsr  Zdynamo_time_before_restart_sZhas_guarded_codeZconfig_suppress_errorsZ config_inline_inbuilt_nn_modulesspecialize_floatZ
is_forwardZ%dynamo_compile_time_before_restart_usr   artifactc                   S  s
   dddS )NZdynamo_errorstringr   encodingr~   r~   r~   r~   r   r   p  s    c                   S  s   t  S r   	traceback
format_excr~   r~   r~   r   r   t  r   Zmetadata_fnZ
payload_fnz: )jZtorch.fx.experimental.validatorrU  rV  rW  rX  r   Z	overridesZ _get_current_function_mode_stackr   r   r   ra   code_contextr   nullcontextr   r'   Zuse_lazy_graph_moduler   r   rZ   r    Zguardr^   r   rT   r   rP   rS   r6   rI   Zupdate_outerr5   r   r   r   rl   Zfail_on_recompile_limit_hitr=   r   rA   rF   r   r   r   r   joinr$   r  formatr   r  r<  r  r	  r   time_nsri  utilsZReinplaceCountersr   rM   rL   Zrun_gc_after_compilerG  r  Zcollectr^  Zlocal_scoper   r  r   lenr   rb  Zcount_callsgraphnodesZplaceholdersr  r  rC  rD  suppress_errorsZinline_inbuilt_nn_modulesr  rk   r   r   r}   r   r)   Zget_exc_messager   rG   rD   r<   AssertionErrorr   r   rE   rB   r@   r?   with_tracebackr  ).r   r   rS  rT  rF  r  r  r   r  r#  r"  r0  r   r$  r/  r  r  rV  rX  rf  rg  Zmetrics_contextr  Z
compile_pgr  ZreasonsZexceededZ
limit_typer  Zstart_time_nsr  r  r  r  r  r  r  r  r  r  r  r  r  r  Zmetricsr   r~   )rl  rU  rT  r"  rF  r   r/  r  r  r]  r  r   r  r$  r   rS  r  r^  r  r  r  r[  rd  re  rW  r   rE    sr   	:8( /




0
*

 
d
	
*

 
*

 rE  c                   @  sN   e Zd ZddddddddZed	d
ddZddddddddddZdS )ConvertFrameNrp   rK   r  r   r  r#  r  r   c                 C  s    || _ t|d|d| _|| _d S )NF)r  r  )r   r  _inner_convert_hooks)r   r  r#  r  r~   r~   r   r     s
    zConvertFrame.__init__z*Callable[[WrapBackendDebug], ConvertFrame]r   c                   s    fddS )Nc                   s   t |  jS r   )convert_framer  r  r   r~   r   r     r   z2ConvertFrame._clone_with_backend.<locals>.<lambda>r~   r   r~   r   r   r    s    z ConvertFrame._clone_with_backendr   ru   r  r   r   rV   r!  c                   s  t |j td d  d7  < z2| j|||||d d}td d  d7  < |W S  ty } zvt|trr t|t}|r$t	
tjr$t|dr$t|dr$tt|j\ |j}	dt|	}
d	|
  tjjd
dd  fddd t	j dd W d    n1 s0    Y  tjs4|s4 t|dd }|j}t||||}|rjtj|dd ntj|dd t|t rt!t"t#j$t#j$dW  Y d }~S t|t%rt!t"t#j&t#j&dW  Y d }~S W Y d }~n
d }~0 0 t! S )Nframestotalr&   r  okr/  
real_stackr'  z&Graph break: skip: from user code at:
r  c                   S  s
   dddS )NZdynamo_graph_break_reasonr  r  r~   r~   r~   r~   r   r   "  s    z'ConvertFrame.__call__.<locals>.<lambda>c                     s     dt   S )N
r  r~   Zuser_stack_tracer~   r   r   &  r   r  T)exc_infor   )Zframe_exec_strategy)'r4  r   r   r]   r  r   r   rE   rG   graph_break_logrq  rr  rs  r   r   r   r/  r  r  r  format_listr   r  r	  r   r'   r  r   r>   r   rG  r   rC   rV   rX   rW   ZSKIPrA   ZRUN_ONLY)r   r   r"  r#  r$  r  r   r   Z	soft_failZ
user_stackZuser_stack_formattedr   r   	error_msgr~   r  r   rH    sd    



&(zConvertFrame.__call__)N)r   rI  r~   r~   r~   r   r    s    	 r  r  c                 C  s   t | ||dS )zHTry to convert a frame into an FX graph, if error leave frame unmodified)r  )r  )r  r#  r  r~   r~   r   r  M  s    r  )rn  r   c                 C  s   ddl m} tj}dt_t| d}t|}W d    n1 sB0    Y  tt	|j
 t
  |_
zJt|j|j
|j|j|j|ddd t tddd d i tdddd	 W |t_n|t_0 d S )
Nr&   )eagerFrbr   *   i  )r,  r-  )
r  r  r   r  r#  r0  r"  r   r$  r/  )Zbackends.debuggingr  r'   Zreplay_record_enabledopenrN   loadr   r  chainr   itemsrE  r   rS  rT  rF  rK   r3   r   )rn  r  Zoriginal_replay_valin_filerj  r~   r~   r   replayU  s2    (
r  rw  c                 C  sB   t jdk rdS t| D ]}|jdkr|jd   S qtdd S )N)      r   RESUMEr  z$RESUME instruction not found in code)r>  version_infodisget_instructionsopnameoffsetRuntimeError)r   instr~   r~   r   first_real_inst_idxt  s    

r  c                   @  s*   e Zd Zddddddddd	d
dZdS )ConvertFrameProtocolr   r  ru   r  rK   r   r   rV   r!  c                C  s   d S r   r~   )r   r   r"  r#  r$  r  r~   r~   r   rH  ~  s    zConvertFrameProtocol.__call__N)r{   r|   r}   rH  r~   r~   r~   r   r  }  s   r  c                   @  s2   e Zd ZddddddZddd	d
dddZdS )CatchErrorsWrapperr  rK   r   callbackr#  r   c                 C  s   t ||  || _|| _d S r   )r   r   r   r#  )r   r  r#  r~   r~   r   r     s    zCatchErrorsWrapper.__init__ru   r  r   rV   )r   r"  r$  r   c              	   C  s  |d usJ t |j t|j}tjdkr@|jt|jk}n|jt|jk}|sv|svt	j
svtddrt| jddsttjr|rd}n&t|jrd}ntddrd}nd}td	|jj||jj t S |jjd
kr|jjdkrt S tjj dkrt }|rtb ddlm} ||j| jjd}	t | jdsHJ d| j!|	j"}
|
||| j#|W  d    S 1 s|0    Y  t\ t$ 4 | j||| j#|ddW  d    W  d    S 1 s0    Y  W d    n1 s0    Y  d S )N)r     F)Zinclude_infra_modesr  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)r&  __new__ddp_optimizerr   )DDPOptimizer)bucket_bytes_capZbackend_compile_fnr  zJDDPOptimizer only supports callback fns that know how to clone themselves.r&   r  )%r4  r   r   r+   r   r>  r  f_lastir  r'   r   r#   r   r   r   rq  rr  rs  r   r   r   rV   r   ri  r  Zget_optimize_ddp_moder!   Z_get_active_ddp_modulecompile_lockZ"torch._dynamo.backends.distributedr  r  r   r  Z
compile_fnr#  r"   )r   r   r"  r$  Zis_skipfileZhas_started_executionskip_reasonZ
ddp_moduler  r  Zhijacked_callbackr~   r~   r   rH    st    



$zCatchErrorsWrapper.__call__N)r{   r|   r}   r   rH  r~   r~   r~   r   r    s   r  r  c                 C  s
   t | |S r   )r  )r  r#  r~   r~   r   catch_errors_wrapper  s    r  )N)NF)TFNN)NN)N)__doc__
__future__r   collectionsr   r   r  r   r  r  rr  r6  r   r   r  r>  	threadingr   r  typingr   pathlibr   typesr   r   r   r   r   r	   r
   r   r   Ztyping_extensionsr   r   r   Ztorch._loggingZtorch._C._dynamo.guardsr   Ztorch._dynamo.callbackr   Ztorch._dynamo.distributedr   Ztorch._dynamo.symbolic_convertr   Ztorch._guardsr   r   r   r   r   Ztorch._utils_internalr   r   r   r   Ztorch.fx._lazy_graph_moduler   Z%torch.fx.experimental.symbolic_shapesr   r   Ztorch.fx.graph_moduler   r   Ztorch.monitorr    Ztorch.nn.parallel.distributedr!   Ztorch.utils._python_dispatchr"   r#   Ztorch.utils._tracebackr$   r%   r'  r'   r(   r)   r*   r+   Zbytecode_analysisr,   r-   Zbytecode_transformationr.   r/   r0   r1   r2   r0  r3   r4   r5   r6   Z
eval_framer7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   r   rH   rI   rJ   r#  rK   ZpgorL   rM   Zreplay_recordrN   Zresume_executionrO   Zsymbolic_convertrP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   r  rZ   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   rf   rg   rh   ri   rj   rk   rl   rm   Zvariables.torch_functionrn   __annotations__numpyro   ModuleNotFoundErrorTYPE_CHECKINGZbackends.registryrp   r  rq   Zrepro.after_dynamorr   rs   rt   ru   Zvariables.builderrv   	getLoggerr{   r   r  ZgetArtifactLoggerrp  r  RLockr  rx   ry   rz   r   r4  r5  r   r   r   r   Z#suppress_torch_distributed_warningsr   r   rA  Counterr   r   r   r  r  rK  Ztorch.utils.hooksrL  Zoutput_graphrM  rN  rQ  rE  r  r  r  r  Protocolr  r  r  r~   r~   r~   r   <module>   s   <X

 
MQ  
F 2      4    3r 	S