o
    Zh                    @   s  U d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlmZmZmZmZmZ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"m#Z# ddl$m%Z% ddl&m'Z' d	d
l(m)Z)m*Z*m+Z+mZ,m-Z-m.Z. d	dl/m0Z0m1Z1m2Z2m3Z3 d	dl4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z= d	dl>m>Z> d	dl?m@Z@ d	dl*mAZAmBZBmCZCmDZDmEZEmFZFmGZG d	dlHmIZI d	dlJmKZKmLZL d	dlMmNZNmOZO d	dlPmQZQmRZR d	dlSmTZTmUZU d	dlVmWZWmXZXmYZYmZZZm[Z[m\Z\m]Z] d	dl-m^Z^m_Z_ d	dl`maZambZbmcZcmdZdmeZemfZfmgZgmhZh d	dlimjZjmkZkmlZl d	dlmmnZnmoZo d	dlpmqZq d	dlrmsZs d	dltmuZumvZvmwZw d	dlxmyZymzZz d	dl{m|Z|m}Z}m~Z~mZmZmZmZ d	dlmZ d	d lmZ d	d!lmZmZmZmZmZ d	d"lmZmZmZmZmZmZ d	d#lmZ d	d$lmZmZmZ d	d%lmZmZ d	d&lmZmZmZmZmZ eeZejed'Zejed(Zejed)Zejed*Ze Zd+d, e D Zeeef ed-< eqejjZeqejjZd.d/ ed0< d1d/ ed2< d3Zejd4ed5ejfd6d7ZejG d8d9 d9ZejG d:d; d;ZejG d<d= d=ZejG d>d? d?ZG d@dA dAZeddBdC ZejdrdFdGZejdrdHdIZejG dJdK dKZG dLdM dMeȃZG dNdO dOeʃZG dPdQ dQeʃZdRejdSef fdTdUZdVdEdWejegef dXefdYdZZd[Zdsd]d^ZdWejegef dXefd_d`Zdadb ZG dcdd ddeՃZejG dedf dfZG dgdE dEedhZG didj dje؃Zejdkkrdldm ejD ZG dndo doe؃ZG dpdq dqe݃ZdS )ta  
Core module responsible for converting Python bytecode into TorchDynamo's symbolic execution format.

This module implements the bytecode-level tracing system that allows TorchDynamo to analyze
and transform Python code. It converts Python bytecode instructions into a symbolic format
that tracks the flow of tensors and other values through the program.

Key components:
- InstructionTranslatorBase: Base class for converting bytecode to symbolic execution
- InstructionTranslator: Main translator for function bytecode
- InliningInstructionTranslator: Handles inlining of called functions
- SpeculationLog: Manages state for speculative execution and rollback

The symbolic conversion process handles:
- Control flow (loops, conditionals, etc.)
- Function inlining and call stack management
- Tracking of program values and side effects
- Graph breaks and resumption points
- Exception handling and stack frame management

This is a core part of TorchDynamo's tracing system that enables ahead-of-time
optimization of PyTorch programs.
    N)AnyCallablecastNoReturnOptionalUnion)patch)TensorifyScalarRestartAnalysis)tracingTracingContext)
guard_bool)cache_method   )configexcgraph_break_hintsloggingtrace_rules	variables)get_indexofJUMP_OPNAMESlivevars_analysispropagate_line_nums)	cleaned_instructionscreate_call_functioncreate_instructioncreate_jump_absolutecreate_swapget_code_keysInstructionis_generator	unique_id)code_context)	PyCodegen)ArgsMismatchErrorBackendCompilerFailedcollapse_resume_framesformat_graph_break_messageget_stack_above_dynamounimplemented_v2Unsupported)get_funcname)GuardBuilderinstall_guard)GraphCompileReasonOutputGraph)DummyModuleExecutionRecorder)ContinueExecutionCacheReenterWith)
AttrSourceDictGetItemSourceGlobalSourceGlobalWeakRefSourceLocalCellSourceLocalSourceSource)is_builtin_constantis_forbidden)countersget_fake_valueget_instruction_source_311get_metrics_contextgraph_break_dup_warning_checkeristype
LazyStringproxy_args_kwargs)typestrValueMutationNewVariableTracker)FrameStateSizeEntrywrap_fx_proxy)BuiltinVariable)ConstantVariable)ContextWrappingVariableGenericContextWrappingVariableWithExitFunctionVariable)ConstDictVariableSetVariable)BaseUserFunctionVariableLocalGeneratorFunctionVariableLocalGeneratorObjectVariableNestedUserFunctionVariableSkipFunctionVariableUserFunctionVariableUserMethodVariable)MAX_ITERATOR_LIMIT)LazyVariableTracker)BaseListVariableListIteratorVariableListVariableSliceVariableTupleVariable)CellVariableExceptionVariableGetAttrVariableNullVariablePythonModuleVariableUnknownVariable)NNModuleVariable)supported_comparison_opsSymNodeVariableTensorVariable)SymbolicTorchFunctionStateTorchFunctionModeVariable)RemovableHandleVariableUserDefinedClassVariable!UserDefinedExceptionClassVariable"UserDefinedExceptionObjectVariableUserDefinedObjectVariableZgraph_breaksZ
trace_callZtrace_sourceZtrace_bytecodec                 C   s   i | ]
\}}|t |jqS  )rJ   call_function.0kvrp   rp   M/var/www/auris/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py
<dictcomp>   s    rw   compare_op_handlersc                 C   s   t | g t|i S N)handle_containsreversedtxargs_rp   rp   rv   <lambda>   s    r   inc                 C   s   t | t| g t|i gi S ry   )
handle_notrz   r{   r|   rp   rp   rv   r      s    znot inzihttps://github.com/pytorch/pytorch/issues/new?&labels=oncall%3A+pt2&projects=&template=pt2-bug-report.ymlnamereturnc                 C   s
   t | S )z
    Import the named module and cache the result. importlib.import_module()
    seems to do some filesystem checking to validate the name so not caching
    this can be slow.
    )	importlibimport_module)r   rp   rp   rv   _import_module   s   
r   c                   @   sR   e Zd ZU eed< eed< eed< eed< dZeed< dZ	e
e ed< d	d
 ZdS )SpeculationEntryfilenamelinenoinstruction_pointerinstFfailedNreasonc                 C   s*   d| _ | jdur| jj}nd}tj|d)z\
        Start tracing of the current frame over again, and don't take this branch.
        TNz!Unknown fail_and_restart_analysis)restart_reason)r   r   r   ZSpeculationRestartAnalysis)selfr   rp   rp   rv   fail_and_restart_analysis   s
   

z*SpeculationEntry.fail_and_restart_analysis)__name__
__module____qualname__str__annotations__intr   r   boolr   r   r.   r   rp   rp   rp   rv   r      s   
 r   c                   @   s\   e Zd ZU dZejedZee e	d< dZ
ee	d< dd Zdd	 Zd
ededefddZdS )SpeculationLoga3  
    SpeculationLog replaces the prior copy_graphstate/restore_graphstate
    checkpointing.  Rather than saving/restoring state, we restart the
    dynamo conversion process over from the beginning -- but when we
    hit the start of the speculation that failed, we instead generate
    a graph break.
    default_factoryentriesr   indexc                 C   s
   d| _ d S Nr   )r   r   rp   rp   rv   restart      
zSpeculationLog.restartc                 C   s   | j   d| _d S r   )r   clearr   r   rp   rp   rv   r         

zSpeculationLog.clearr   r   r   c                 C   s  t | j| jkr| jt|||| | j| j }d}| jdkr<| j| jd  }d|j d|j d|jj d|j	 d	}|j	|krK|j|krK|j|ks{t
d	| j d
t | j d|j d|j d|jj d|j	 d| d| d|j d| d| d|  jd7  _|S )z
        Lookup or create a SpeculationEntry() that is shared across
        RestartAnalysis calls.  Args are used only for debug checks.
         r   r   zPrevious instruction: :(z @ z)
z"
SpeculationLog diverged at index z
 (log had z entries):
- Expected:  (z at ip=z)
- Actual: a  
There are two usual reasons why this may have occured:
- When Dynamo analysis restarted, the second run took a different path than
  the first.  If this occurred, the previous instruction is the critical instruction that
  behaved differently.
- Speculation entries are only added under certain conditions (as seen in
  step()), e.g., there must exist operators in the graph; those conditions may
  have changed on restart.

If this divergence was intentional, clear the speculation log before restarting (do NOT
do this for graph breaks, you will infinite loop).

Otherwise, please submit a bug report, ideally including the contents of TORCH_LOGS=+dynamo
)lenr   r   appendr   r   r   r   opnamer   SpeculationLogDivergence)r   r   r   r   r   entryZprev_entry_msgZ
prev_entryrp   rp   rv   next   sX   



zSpeculationLog.nextN)r   r   r   __doc__dataclassesfieldlistr   r   r   r   r   r   r   r   r   rp   rp   rp   rv   r      s   
 r   c                   @   s8   e Zd ZU ejedZeeef e	d< defddZ
dS )
LocalStater   automatic_dynamicr   c                 C   s   d dd | j D S )N
c                 s   s&    | ]\}}| d |   V  qdS ): N)renderrr   rp   rp   rv   	<genexpr>)  s    
z$LocalState.render.<locals>.<genexpr>)joinr   itemsr   rp   rp   rv   r   (  s   
zLocalState.renderN)r   r   r   r   r   dictr   r   rH   r   r   rp   rp   rp   rv   r   "  s
   
 r   c                   @   s2   e Zd ZU eed< eed< dZeee  ed< dS )DistributedStateZ
compile_pgZlocal_stateN
all_states)	r   r   r   r   r   r   r   r   r   rp   rp   rp   rv   r   /  s   
 r   c                   @   sl   e Zd ZU e Zee ed< ededdfddZedede	fddZ
edd	d
Zede	fddZdS )TensorifyStateforce_specializationsr   r   Nc                 C      | j | d S ry   )r   addclsr   rp   rp   rv   
specialize<  s   zTensorifyState.specializec                 C   s
   || j v S ry   )r   r   rp   rp   rv   should_specialize@     
z TensorifyState.should_specializec                 C      | j   d S ry   )r   r   r   rp   rp   rv   r   D     zTensorifyState.clearc                 C   s   t | jdkS r   )r   r   r   rp   rp   rv   emptyH  r   zTensorifyState.emptyr   N)r   r   r   setr   r   r   classmethodr   r   r   r   r   rp   rp   rp   rv   r   6  s   
 r   c                   C   s
   t tS ry   )torchdynamo_loggingZget_step_loggerlogrp   rp   rp   rv   _step_loggerM  r   r   r}   InstructionTranslatorBasec                 c   sR    | j j}| j j}zg | j _d| j _d V  W || j _|| j _d S || j _|| j _w r   )speculation_logr   r   )r}   r   r   rp   rp   rv    save_and_restart_speculation_logR  s   
r   c                 c   s2    z| j j}d| j _d V  W || j _d S || j _w NF)outputshould_exit)r}   tmprp   rp   rv   (temporarely_allow_writes_to_output_graphd  s   r   c                   @   sV   e Zd ZU eed< eed< eed< dZeee	e
f  ed< dd Zdd	 Zd
d ZdS )BlockStackEntryr   targetstack_indexNwith_contextc                 C   s
   | j d uS ry   )r   r   rp   rp   rv   can_restorex  r   zBlockStackEntry.can_restorec                 C   sN   | j d usJ | jr t| jdr | jjr t| j d t| jjS t| j d S )Ntarget_valuesr   )r   r   hasattrr   r3   tupler   rp   rp   rv   	resume_fn{  s   
zBlockStackEntry.resume_fnc                 C   s0   | j d usJ |r| j  s|s| j |S d S ry   )r   Zexit_on_graph_breakexit)r   r}   Zis_graph_breakrp   rp   rv   r     s   zBlockStackEntry.exit)r   r   r   r   r   r   r   r   r   rL   rM   r   r   r   rp   rp   rp   rv   r   n  s   
 
r   c                   @      e Zd ZdS )r   Nr   r   r   rp   rp   rp   rv   r         r   c                   @   r   )ReturnValueOpNr   rp   rp   rp   rv   r     r   r   c                   @   s   e Zd ZdZdS )YieldValueOpzY
    Signal to the symbolic tracer to stop and return control flow to the
    caller
    N)r   r   r   r   rp   rp   rp   rv   r     s    r   fn.c                    s>   t t| jt|  t| dddtf fdd}|S )Nr   InstructionTranslatorr   c                    s   |   | | i  d S ry   )pushrq   popnr   r   Zfn_varnargsrp   rv   impl  s   zstack_op.<locals>.impl)r   inspect	signature
parametersrJ   	functoolswrapsr   )r   r   rp   r   rv   stack_op  s
   r   r   truth_fnr   c                 C   s   |t jus|r	dS t| jtsJ | j}| j| }|jdkr dS |d7 }d}| j| }|jdkr]t|jts8dS |j}|d7 }| j| }|jdvrKdS |d7 }|jdkrX|d7 }| j| }|jdkrddS | 	t
| d	S )
NFLOAD_ASSERTION_ERRORr   zassertion error
LOAD_CONST)CALL_FUNCTIONPRECALLCALLr   RAISE_VARARGST)operatortruth
isinstancer   r   instructionsr   argvalr   r   rK   create)r   r   r   Zcurrent_instruction_pointerr   	error_msgrp   rp   rv   &_detect_and_normalize_assert_statement  s4   








r  Fr   c                    s@  |d u r
t jj }z|d j|d jf}W n ty'   | d | d f}Y nw d}tjr9t	 }d
t|}n	t	 | }t|}d
t|}d|d  d|d  d	| d
tjrh| d| d7 nt|7 t jjddd  fddd ttjrtst|rt d S td|d |d | d S )Nco_filenameco_firstlinenor   zGraph break in user code at r   r   r   z
Graph Break Reason: z
User code traceback:
zQ
========== most recent `torch.compile` tracing attempt started here ==========

a}  
NOTE: the most recent `torch.compile` tracing attempt might not be where you applied `torch.compile`! This is due to how graph breaks are implemented - the optimized code object returned by Dynamo will call another Dynamo-generated resume function and tracing is re-enabled by calling the resume function as a normal Python function, which Dynamo intercepts as a top-level frame.
artifactc                   S   s
   dddS )NZdynamo_graph_break_reasonstring)r   encodingrp   rp   rp   rp   rv   r     s   z!log_graph_break.<locals>.<lambda>c                      s    d rt   S d S )Nr   r   )	traceback
format_excrp   exc_infoZuser_stack_tracerp   rv   r     s    )Zmetadata_fnZ
payload_fnzmGraph break (user stack suppressed due to duplicate graph break) in user code at %s:%s
Graph Break Reason: %s)torchZ_guardsr   extract_stackr   r   
IndexErrorr   verboser(   r   r
  format_listr&   r   _loggingZtrace_structuredgraph_break_logisEnabledForr   DEBUGexplainrA   r   debug)code_optionsr   r  
user_stackZ	frame_locZstack_above_dynamo_formattedZstack_above_dynamoZuser_stack_formattedrp   r  rv   log_graph_break  sb   


r  c                    sN   dd g t jdd fdd	ddd	tf fd
d}|S )NzData-dependent branchingzyDetected data-dependent branching (e.g. `if my_tensor.sum() > 0:`). Dynamo does not support tracing dynamic control flow.z1Use `torch.cond` to express dynamic control flow.r   c                    s   t | jtd|  dd |  stdddg d |  r1d|   }t| t	
|| | td | jj| td	t| | |  gd |   | | j}ra| | | |j}tjd
kru| jtdg t|j|d d}|| | j|g| |  d S )Nattempted to jump with gb_typecontextexplanationhintsr   z;Should not compile partial graph (data-dependent branching)r   zDynamo has determined when encountering data-dependent branching (e.g. `if my_tensor.item() > 0:`) that it should not compile the partial graph.BSkipping frame because there is a graph break in a for/while loop
zgeneric_jump triggered compilezgeneric_jump       TO_BOOLr   )r   )r  r  r'   should_compile_partial_graphr)   maybe_has_backedgeframe_summaryr   infor   	SkipFramer   r  r   compile_subgraphr.   rE   popcreate_call_resume_atnext_instructionr   sysversion_infoadd_output_instructionsr   r   copy_positions)r   r   value	extra_msgmsgZif_nextZif_jumpZ	jump_inst)_explanation_gb_type_hintsr   rp   rv   jump_graph_breakG  sR   		






z&generic_jump.<locals>.jump_graph_breakr   r   r   c                    s  |   }tjrt| r|   }| r&t| r | |S | || t|t	rC| j
jdtjgt||fi R   | | d S t|trs|j}t|tjsU|dk}tjjj|}|sltdt|dg tjd | | d S | j
jdtjgt|fi R  }t| |t|j| d}| j
jdtjgt||fi R   | | d S | rt|tr|jrt |j!t"j# | rψr| $| | | d S d S t|t	r| % r| || d S t|t&r| j
'|j(}|rr| $| | | d S d S t|t)rz|*| d}	W n+ t+j,y=   t+-|  z|*| d}	W n t+j,y:   t+-|  d }	Y nw Y nw t|	t.r|	/| g i }t|t0rqt|j1tt2frq|j1rorh| $| | | d S d S t|tr|3 rr| $| | | d S d S td	d
|	 d| dg d d S drr| $| | | d S d S t|t	s|4| rڈt5|6| r؈r| $| | | d S d S t|tr4zt|jtjr|3| j
}
nt7|jdk}
W n% t+j8y } z| % r| ||d| dW  Y d }~S  d }~ww |
r2r+| $| | | d S d S t|t9j:rQdrOrH| $| | | d S d S ddlm;} |jd ur{||jr{|< ryrr| $| | | d S d S td|  d d S )Nrq   r   z#Assertion failed on symbolic shapesr   r  )Zexample_value__bool____len__z3Data-dependent branching with non-constant __bool__zmethod: z
, result: z}Attempted to perform data-dependent branching on a user-defined object with a __bool__ method that did not return a constant.Tr   )r6  r   )is_constant_sourcer  )=r.  r   Z rewrite_assert_with_torch_assertr  is_python_constantr   as_python_constantjumpr   rh   r   Zcreate_proxyr  Z_assert_asyncrD   rg   Zsym_numZSymBoolfxZexperimentalZsymbolic_shapesZexpect_truer)   r   r   
USER_ERRORZscalar_tensorrI   r>   noderO   sourcer-   
make_guardr,   ZSEQUENCE_LENGTHr   r(  re   get_submodule
module_keyro   Zvar_getattrr   ZObservedAttributeErrorhandle_observed_exceptionrW   rq   rK   r5  r   Zevaluate_exprZhas_unpack_var_sequencer   unpack_var_sequencer   Z	UserErrorr   ZBackwardHookVariabler>  Zget_real_value)r   r   r5  r  Zsym_exprresultZscalar_to_tensor_proxyZscalar_to_tensormodxZeval_resulter>  r8  r9  r:  r;  r   r   rp   rv   innerz  s6  




















	


"





zgeneric_jump.<locals>.inner)r   )r   ZFUNDAMENTALr   )r   r   rP  rp   rO  rv   generic_jump;  s    3 .rQ  c                    s    fdd}|S )Nc                    s@   t dddtf fdd}dddtdtffdd |S )	Nr   r   r   c              
      s  |   }|jr|jd usJ  | ||jS z| |W S  ty } z]| jr;|  tdd| j ddgtj|d t	|t
jrB |  sG t| jdt||jd |  rid|   }t| t
|||  |d	 t|j|j|_W Y d }~nd }~ww |  d S )
Nz0Graph break under GenericContextWrappingVariablez!Active generic context managers: z]Attempted to graph break in an active context manager(s) that doesn't support graph breaking.zEMove the offending context manager(s) to outside the compiled region.)r  r  r   r!  Zfrom_excT)r  r   r  r#  graph_break)	speculater   r   r*   active_generic_context_managersremove_from_statsr)   r   ZCAUSED_BY_EARLIER_GRAPH_BREAKr   r   ZUncapturedHigherOrderOpErrorr(  r  r  r   Z
real_stackr)  r*  r   r+  r,  add_to_statsr.   r7  r   )r   r   speculationZexcpr7  handle_graph_breakinner_fnrp   rv   wrapper,  sT   


(z>break_graph_if_unsupported.<locals>.decorator.<locals>.wrapperr   c                    s  | j j| |d t| }g }| jD ]6}t|jtr&|| 	|j
| q|jd us-J t|jts5J |j| || |j
| q| j |  ~tjdkr|jdkr| jd ure| j nd}t|dkrtjdk stJ | j td|dg t|jd	}|d
 | | j | n|jd u sJ t|}d |_| j |g | j | tjdkrtjdk r|jdkrttjd |jttjd |j }	nt|j |j}	| ! |	  t" D ]}
| #t$  q| j | %| j& d S )Nr"  r%     r   rp   r   r$  KW_NAMESr   Fr  r%     r   )'r   r-  r#   block_stackr   r   rj   extend_outputr   Ztry_except_torch_function_moder  rL   reconstruct_typeZtry_finallyr3  get_instructionsr1  r2  r   kw_namesr@  r   r   r   argr4  r   copyexn_tab_entrydisstack_effectopmapopcoder   ranger   rd   r/  r0  )r   r   r   cgcleanupbrf  Z
call_instsZ	inst_copyrk  r   r   rp   rv   rY  ^  sd   







zIbreak_graph_if_unsupported.<locals>.decorator.<locals>.handle_graph_break)r   r   r   r.   )rZ  r[  rr  rX  rv   	decorator+  s   1Ez-break_graph_if_unsupported.<locals>.decoratorrp   )r   rs  rp   rr  rv   break_graph_if_unsupported*  s   zrt  c                       s"   e Zd ZdZd fddZ  ZS )BytecodeDistpatchTableMetazTInstalls a `cls.dispatch_table` on every subclass to speed up calls to self.OPCODE()r   Nc                    sN   t  ||| dd   fddtj D fddtdD _d S )Nc                 W   s6   t d|  d| d|  dd|  dgtjd d S )NzMissing bytecode handlerz with args z=Dynamo does not know how to handle the bytecode instruction ``.z%Do not trace code that produces the `z_` bytecode instruction (see https://docs.python.org/3/library/dis.html for bytecode semantics).r  )r)   r   SUPPORTABLE)r   r~   rp   rp   rv   _missing  s   


z5BytecodeDistpatchTableMeta.__init__.<locals>._missingc              
      s&   i | ]\}}|t |t |qS rp   )getattrr   partial)rs   r   op)rx  r   rp   rv   rw     s    z7BytecodeDistpatchTableMeta.__init__.<locals>.<dictcomp>c                       g | ]}  |qS rp   )getrs   i)dispatch_tablerp   rv   
<listcomp>      z7BytecodeDistpatchTableMeta.__init__.<locals>.<listcomp>   )super__init__rj  rl  r   rn  r  )r   r   basesdct	__class__)rx  r   r  rv   r    s   z#BytecodeDistpatchTableMeta.__init__r   )r   r   r   r   r  __classcell__rp   rp   r  rv   ru    s    ru  c                   @   s   e Zd ZU dZejedZee e	d< ejddZ
ee e	d< dd Zd	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZeZdS )ExceptionStackzU
    Exception stack that it is shared among all InstructionTranslator instances
    r   
_exc_stackN)default_current_exceptionc                 C   s
   d | _ d S ry   r  r   rp   rp   rv   clear_current_exception  r   z&ExceptionStack.clear_current_exceptionc                 C   s   |  | || _d S ry   )._set_context_and_break_context_reference_cycler  r   valrp   rp   rv   set_current_exception  r   z$ExceptionStack.set_current_exceptionc                 C   s&   | j d usJ | | j  |   d S ry   )r  r   r  r   rp   rp   rv   move_current_exception_to_stack  s   z.ExceptionStack.move_current_exception_to_stackc                 C   s   | j d usJ | j S ry   r  r   rp   rp   rv   get_current_exception  s   z$ExceptionStack.get_current_exceptionc                 C   sT   |j  }rt|tur|S t| j| dkr(| j| }| ||d  || |S Nr   r   )__context__typerK   r   r  _set_context_recursiveset_context)r   r  Zprev_idxctxprevrp   rp   rv   r    s   

z%ExceptionStack._set_context_recursivec                 C   s`   | }}d}	 |j }t|tu rd S ||u r|td  d S |}||u r'd S |r,|j }| }qr   )r  r  rK   r  )r   r  oZslow_oZslow_update_toggler  rp   rp   rv   _break_context_reference_cycle  s    z-ExceptionStack._break_context_reference_cyclec                 C   s$   |  |t| jd  | | d S )Nr   )r  r   r  r  r  rp   rp   rv   r    s   z=ExceptionStack._set_context_and_break_context_reference_cyclec                 C   
   | j  S ry   )r  r.  r   rp   rp   rv   r.    r   zExceptionStack.popc                 C   r   ry   )r  r   r  rp   rp   rv   r        zExceptionStack.appendc                 C   s
   t | jS ry   )r   r  r   rp   rp   rv   r=    r   zExceptionStack.__len__c                 C   s
   | j | S ry   )r  )r   r   rp   rp   rv   __getitem__  r   zExceptionStack.__getitem__c                 C   s   d| j d| jS )Nzself._exc_stack=z - self._current_exception=)r  r  r   rp   rp   rv   __str__     zExceptionStack.__str__)r   r   r   r   r   r   r   r  rG   r   r  r   r  r  r  r  r  r  r  r.  r   r=  r  r  __repr__rp   rp   rp   rv   r    s"   
 	r  c                #       sL
  e Zd ZU eed< eeef ed< eeef ed< eed< e	e ed< e
e ed< eed< e	e ed< eed	< e
e ed
< eed< e	e ed< eed< eed< e
e ed< e	e ed< eed< e
e ed< e
eegef  ed< e
e ed< dd Zdd Zdd Zdd Zdd Zdd  Zd!ed"e	e d#eeef fd$d%Zd&d' Zd(d) Zdmd+d,Zd-d. Zd/d0 Z d1d2 Z!e"j#d3krd4d5 Z$nd6d5 Z$e%d7d8 Z&d9d: Z'd;d< Z(d=d> Z)d?e
e fd@dAZ*dBe	e fdCdDZ+dEefdFdGZ,dHedEe	e fdIdJZ-dKdL Z.dMdN Z/dOdP Z0dQdR Z1dSdT Z2e.Z3dUdV Z4dWdX Z5dYdZ Z6e7j8d[d\ Z9d]d^ Z:d_d` Z;e<dadb Z=dcdd Z>dedf Z?dgdh Z@didj ZAdkdl ZBdmdn ZCdodp ZDeDZEeDZFeGeHjIdqZJeGeHjKdqZLeGeHjIdrZMeGeHjKdrZNdsdt ZOdudv ZPdwdx ZQdydz ZRd{d| ZSd}d~ ZTdd ZUdd ZVdd ZWdd ZXdd ZYdd ZZdEe[fddZ\dd Z]dd Z^dd Z_dd Z`dd Zadd Zbdd Zcdd Zddd Zedd Zfdd Zgdd Zhdd Ziejdddd Zkejdddd Zlejdddd Zmdd Zndd Zodd Zpdd Zqdd Zrdd Zsdd Ztdd Zudd Zvdd ZwdEefddĄZxejddddǄ ZyddɄ Zzdd˄ Z{dd̈́ Z|ddτ Z}ddф Z~efddӄZddՄ ZeZddׄ Zddل ZeZddۄ Zdd݄ Zdd߄ Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d Zdd Zdd Zdd Zdd	 Zd
d ZeZdd Zdd Zdd Zdd Zdd Zdd ZeeHjZeeHjZeeHjIZeeHjZeeHjZeeHjZeeHjZeeHjZeeHjZeeHjZeeHjZeeHjZeeHjZejddeeHjZeeHjZeeHjÃZeeHjŃZeeHjǃZeeHjɃZeeHj˃ZeeHj̓ZeeHjσZeeHjуZeeHjӃZeeHjՃZeeHjՃZeeHj؃ZeeHjڃZeeHj܃ZeeHjރZeeHjZeeHjZeeHjZdd Ze"j#d3krdd Zdd Zdd Zd d! Zdnd"d#Zejddd$d% Zd&d' Zd(d) ZeDZeDZeGeHjKdqZeGeHjKdqZeGeHjIdqZeGeHjIdqZd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Zd<d= Zd>d? Zd@dA Z dBdC ZejdddDdE ZdFdG ZdHdI ZdJdK ZdLdM ZdNdO ZdPdQ ZdmdRdSZ	dTdU Z
dVdW ZdXdY Ze%dZd[ Zejd\eegef fd]d^ZdEefd_d`Z	*dmdedae	e dbeeef dceeef ddeeef deeeef deeef deeef dedfejdgededhededie
e dje
eej  dEd*f" fdkdlZ  ZS (o  r   r   symbolic_localssymbolic_globalssymbolic_torch_function_statestackr   current_instructionrb  r   rf  accept_prefix_instprefix_instsinline_depthinconsistent_side_effectscurrent_speculationr  exn_vt_stackexec_recorderstrict_checks_fnstart_pointc                 C   s
   d| _ dS )z
        InstructionTranslator has encountered instructions which may cause
        dynamo to see a different version of history from eager
        See: https://github.com/pytorch/pytorch/issues/110765
        TN)r  r   rp   rp   rv   mark_inconsistent_side_effects7  s   
z8InstructionTranslatorBase.mark_inconsistent_side_effectsc                 C   s^   | j j}| jd usJ | j| jd  D ]}|jdv r dS |jtv r,|j}||k r, dS qdS )N)RETURN_VALUERETURN_CONSTFT)r  offsetr   r   r   r   r   )r   Z
cur_offsetr   Zjump_offsetrp   rp   rv   r)  ?  s   

z,InstructionTranslatorBase.maybe_has_backedgec                 C   B   t | dst| jd pg | _t| tr|  j| j 7  _| jS )N	_cellvarsco_cellvars)r   r   r  r  r   InliningInstructionTranslatorparentcellvarsr   rp   rp   rv   r  ^  
   

z"InstructionTranslatorBase.cellvarsc                 C   r  )N	_freevarsco_freevars)r   r   r  r  r   r  r  freevarsr   rp   rp   rv   r  g  r  z"InstructionTranslatorBase.freevarsc                 C   s"   t | ds|  |   | _| jS )N_cell_and_freevars)r   r  r  r  r   rp   rp   rv   cell_and_freevarsp  s   
z+InstructionTranslatorBase.cell_and_freevarsc                    s:   t | j| j  fdd| j D | _| jj|  d S )Nc                    s   i | ]\}}| v r||qS rp   rp   rr   readsrp   rv   rw   x  s    z?InstructionTranslatorBase.prune_dead_locals.<locals>.<dictcomp>)r   r   r  r  r   r   side_effectsZprune_dead_object_newr   rp   r  rv   prune_dead_localsu  s
   
z+InstructionTranslatorBase.prune_dead_localsr   r~   kwargsc                 C   s   t |tsJ t |tsJ t |tsJ tdd t|| D s&J d }t|dr0|j	}t|dr8|j
}|rIt|rIt|rItd| | || || d S )Nc                 s   s    | ]}t |tV  qd S ry   )r   rG   rs   rM  rp   rp   rv   r     s
    
z:InstructionTranslatorBase.call_function.<locals>.<genexpr>r5  r   z$Attempt to trace forbidden callable )r   rG   r   r   all	itertoolschainvaluesr   r5  r   callabler<   AssertionErrorr   rq   )r   r   r~   r  rZ  rp   rp   rv   rq   ~  s   

z'InstructionTranslatorBase.call_functionc                 C   s    t |ts	t|}|| ||S )zD
        Redirect the call to the generator "call_function"
        )r   rR   rq   r   r   r~   r  rp   rp   rv   inline_generator_function  s   
z3InstructionTranslatorBase.inline_generator_functionc                 C   s0   t jrt| r| |||S t| |||S )zF
        A call to some user defined function by inlining it.
        )r   "enable_faithful_generator_behaviorr    get_coder  r  inline_callr  rp   rp   rv   inline_user_function_return  s   z5InstructionTranslatorBase.inline_user_function_returnNc                 C   st   |d u r| j }| jdkrd| j dnd}t| jj|}|d u r"dnd| d}| jj d| d| jj | | S )Nr   z (inline depth: )r   r   r   z in )r   r  r+   f_coder  co_name)r   r   Zinline_depth_strfuncnameZfuncname_strrp   rp   rv   get_line_of_code_header  s   $z1InstructionTranslatorBase.get_line_of_code_headerc                 C   s8   d|    d}t| jj| j }|d| 7 }|S )NzTRACE starts_line r   z    )r  	linecachegetliner  r  r   rstrip)r   Zlog_strlinerp   rp   rv   get_log_starts_line_log_str  s   z5InstructionTranslatorBase.get_log_starts_line_log_strc                 C   sh   | j |krd S || _ t| jj|| jj ddlm} || jj t	t
jr2tdt| j d S d S )Nr   )	dump_file%s)r   r   Zset_current_locr  r  r  Ztorch._logging.structuredr  trace_source_logr  r   r  r  rC   r  )r   r   r  rp   rp   rv   starts_line  s   
z%InstructionTranslatorBase.starts_linec              
   C   sT  | j }|du r	dS | j|  | _}|d | _ |jr| |j | js8|  r8|  r8|  | _| jj	r8| 
|S ttjrItd|j|j| j | | z| j|j | | | jj W S  tye     tjy} } z| | W Y d}~dS d}~w ttfy   Y dS  ty   | jdu rtd  tjddd Y nw | j  dS )	z<Process exactly one instruction, return False we should exitNFr   zTRACE %s %s %sTzempty checkpointzstep triggered compiler  ) r   r   r  r  r  r(  is_non_empty_graphrS  r  r   step_graph_breaktrace_bytecode_logr  r   r  r  r   r   update_block_stackr  rm  r   r   r	   r   ObservedExceptionexception_handlerr   r   r*   r   r   )r   ipr   rN  rp   rp   rv   step  sN   






zInstructionTranslatorBase.stepr\  c                 C   s   |j }|r+t| jdkr%|j| jd jur'|j| jd ju r)| j  d S d S d S d S | jrC|jdvrEt| jdks<J | j  d S d S d S )N   r  )NOPJUMP_BACKWARDr   )ri  r   rb  r   r.  r   )r   r   r   rp   rp   rv   r    s   	z,InstructionTranslatorBase.update_block_stackc                 C      d S ry   rp   r   rp   rp   rv   r       c                 C   s   | j | j S ry   )r   r   r   rp   rp   rv   r0    s   z*InstructionTranslatorBase.next_instructionc                 C   sT   | j jrJ | jd usJ | j j| dtd|  gd | j t|g| j  d S )NTZstep_unsupported)Zpartial_convertr   )	r   Zoutput_instructionsr  r-  r.   r*  r3  r   r   )r   Zcontinue_instrp   rp   rv   r  !  s   z*InstructionTranslatorBase.step_graph_breakc                 C   s
   t d S ry   )r   current_framer   rp   rp   rv   run_ctx_mgr.  s   
z%InstructionTranslatorBase.run_ctx_mgrc                 C   sD  |    zyz| j|  | j| _|  r	 |  sW nR ty#     ty*     tyX } z#t	|drSd|j
v rStdtj| jj| jjjdddd tjd  d }~w tym } z
| jrh| j |_ d }~ww W | j  t| tr~| j  n| j  t| tr| j  w w W d    d S 1 sw   Y  d S )Nr7  zData-dependentr   FT)Zprint_outputZinclude_strideZinclude_device)file)r  r   Zpush_txr   r  r  r	   r%   RuntimeErrorr   r7  printr  rB  GraphModuleZ
nn_modulesgraphZprint_readabler1  stderr	Exceptionr  Z
get_recordZexec_recordZpop_txr   r   rp  )r   rN  rp   rp   rv   run4  sX   






"zInstructionTranslatorBase.runr  c                 C   s4   |d u st |tsJ dt| | j| d S )Nz"push expects VariableTracker, got )r   rG   rE   r  r   r  rp   rp   rv   r   [  s   zInstructionTranslatorBase.pushvalsc                 C   s   |D ]}|  | qd S ry   rr  )r   r  r  rp   rp   rv   	push_manya  s   z#InstructionTranslatorBase.push_manyr   c                 C   r  ry   )r  r.  r   rp   rp   rv   r.  e  r   zInstructionTranslatorBase.popnc                    s   g t  fddt|D S )Nc                    s   g | ]}   qS rp   r.  )rs   r   r   rp   rv   r  i      z2InstructionTranslatorBase.popn.<locals>.<listcomp>)r{   rn  )r   r  rp   r   rv   r   h  s   zInstructionTranslatorBase.popnc                 C   s   |j }| jr|| jv r| j|| j|  z| | j|   W nM tyn   |drZz|	dd}| | j|  W n. tyY   t
dd| d| ddgtjd Y nw t
d	d| d
| dg tjd Y nw |dr|| j| d S d S )N.Zimplicitz5Attempted to read undefined local variable (implicit)z
LOAD_FAST z5Could not find an implicit local variable with name ``z(This happens in dict/list comprehensionsr  z*Attempted to read undefined local variablez+Could not find a local variable with name `Z___stack)r   r  f_localsadd_local_varr   r  unwrapKeyError
startswithreplacer)   r   rC  r.  )r   r   r   new_namerp   rp   rv   	LOAD_FASTk  s@   




z#InstructionTranslatorBase.LOAD_FASTc                 C   sl   |j |  v s	J | j|j  }| jj|}| | | jr2|j | jv r4| j	|j | j|j   d S d S d S ry   )
r   r  r  r   r  Z	load_cellr   r  r  r  )r   r   cellcontents_varrp   rp   rv   
LOAD_DEREF  s   
z$InstructionTranslatorBase.LOAD_DEREFc                 C   s&   |j }|  }|| || j|< d S ry   )r   r.  set_name_hintr  )r   r   r   Z	loaded_vtrp   rp   rv   
STORE_FAST  s   
z$InstructionTranslatorBase.STORE_FASTc                 C   s   | j |j= d S ry   )r  r   r   rp   rp   rv   DELETE_FAST     z%InstructionTranslatorBase.DELETE_FASTc                 C   sb   |j |  v s	J | j|j  }|  }| jj|| t|ts"J |j	d ur/|
|j	 d S d S ry   )r   r  r  r.  r   r  
store_cellr   r_   
local_namer  )r   r   r  r  rp   rp   rv   STORE_DEREF  s   
z%InstructionTranslatorBase.STORE_DEREFc                 C   sF   |j }|d u rtj|jdS | j| }|s!tj|jd | j|< }|S )Nr5  )rg  rK   r  r   _constants_cache)r   r   r  r  rp   rp   rv   _load_const  s   
z%InstructionTranslatorBase._load_constc                 C   s   |  | | d S ry   )r   r  r   rp   rp   rv   r     r  z$InstructionTranslatorBase.LOAD_CONSTc                 C   s   |j }| jr&|| jv r| j|| j|  n|| jv sJ | j| | jj|< || jv rA| jj| j|  }| 	| jj
|| d S z| j| }W n tyV   | | Y S w | 	t| |t| d S ry   )r   r  	f_globalsZadd_global_var
f_builtinsbuiltinsr  r   r  r   load_globalr  load_builtinrG   buildr6   )r   r   r   variabler5  rp   rp   rv   _load_global  s    

z&InstructionTranslatorBase._load_globalc                 C   s$   d}|  |}t|}t| ||S )Nztorch.nn.modules.module)import_sourcer   rG   r  )r   module_namemodule_sourcefglobals_valuerp   rp   rv   nn_modules_globals_vt  s   
z/InstructionTranslatorBase.nn_modules_globals_vtc                 C   s\   t jdkrt jdk r|jd r| | | | t jdkr*|jd r,| | d S d S d S )Nr\  r$  r  )r1  r2  rg  	PUSH_NULLr   r   rp   rp   rv   LOAD_GLOBAL  s   

z%InstructionTranslatorBase.LOAD_GLOBALc                 C   sr   |   }|j}t|}|| jvrt | j|< | jj|| j| }t|t	r.t
d|dg d | jj||| d S )Nz%Storing Tensor hook handle in globalsThis is not supported.r  )r.  r   r6   r  objectr   r  Ztrack_global_existingr   rk   r)   Zstore_global)r   r   r5  r   rE  r  rp   rp   rv   STORE_GLOBAL  s    


z&InstructionTranslatorBase.STORE_GLOBALc                 C   s   d|v rt jjj| }|dddddd}nt|}d|dd }| jj}||vs7|| |u s7J |||< | j| t	|S )z-Create an alias to a module for use in guardstorch_package>r   <r  Z_dot_Z	__import_)
r  packagepackage_importer_package_imported_modulesr	  r   r   global_scopeZupdate_co_namesr6   )r   r"  r5  aliasr  rp   rp   rv   r!    s   z'InstructionTranslatorBase.import_sourcec                 C   sB   | d|d }t||k rtd|d }|r| d| S |S )z
        Copied from the Cpython implementation of __import__
        Resolve a relative module name to an absolute one.
        https://github.com/python/cpython/blob/5a094f0255eea1db58fb2cf14c200971e64ec36e/Lib/importlib/_bootstrap.py#L902
        r  r   z2attempted relative import beyond top-level packager   )rsplitr   ImportError)r   r   r.  levelbitsbaserp   rp   rv   resolve_name  s
   z&InstructionTranslatorBase.resolve_namec                 C   s   | j d}| j d}|dur%|dur#||jkr#tjd||jdd |S |dur,|jS tjddd | j d }d	| j vrD|d
d }|S )z
        Copied from the Cpython implementation of __import__
        https://github.com/python/cpython/blob/5a094f0255eea1db58fb2cf14c200971e64ec36e/Lib/importlib/_bootstrap.py#L1090
        __package____spec__Nz)__package__ != __spec__.parent (%r != %r)r%  )
stacklevelzYcan't resolve package from __spec__ or __package__, falling back on __name__ and __path__r   __path__r  r   )r  r}  r  r   warning
rpartition)r   r.  specrp   rp   rv   calc_package  s*   

z&InstructionTranslatorBase.calc_packagec           
      C   sH  |  d\}}| }| }|j}tj d| d| d| }|| jv r/| j| }t|}nLzt|||| jd}W n tyW   t	dd| d| d| dg t
jd	 Y nw |d
krg|  }| |||}|sv|dd
 }	| |	}n| |}| jr| j|| t|tjtfr| t||d d S t	dt|dg d	 d S )Nr  r   )fromlistr5  globalszImport failurezmodule_name: z, fromlist: z, level=z"Failure when attempting to import.r  r   r  rE  zBad import resultz%Import result is not a Python module.)r   r@  r   r1   ZLOCAL_MOD_PREFIXr  r6   
__import__r4  r)   r   rC  r@  r8  	partitionr!  r  Zadd_local_modrB   types
ModuleTyper0   r   rc   rE   )
r   r   r5  rA  r"  Zrecorded_namer5  rE  pkgZtop_level_module_namerp   rp   rv   IMPORT_NAME,  sR   






z%InstructionTranslatorBase.IMPORT_NAMEc                 C   s   |  | | | d S ry   )DUP_TOP
_load_attrr   rp   rp   rv   IMPORT_FROMe     
z%InstructionTranslatorBase.IMPORT_FROMc                 C   s|   || j vrtd| d| j | }t|r-t| jj}t||}| t	| || d S t
|s3J | tj|d d S )Nzname 'z' is not definedr  )r  r*   r  r6   r   Z%name_of_builtins_dict_key_in_fglobalsr5   r   rG   r  r;   rK   r  )r   r   r  Zbuiltins_sourceZ
var_sourcerp   rp   rv   load_builtin_from_argvali  s   


z2InstructionTranslatorBase.load_builtin_from_argvalc                 C   s   |  |j d S ry   )rN  r   r   rp   rp   rv   r  x  r  z&InstructionTranslatorBase.load_builtinc                 C   sL   | j d usJ | jd usJ t d| j | j  | j|j | _ | j | _d S )Nir_count)r   r  r@   	incrementindexofr   r   rp   rp   rv   rA  {  s   zInstructionTranslatorBase.jumpFTc                 C       | j t||jt| j d S ry   rb  r   r   r   r   r  r   rp   rp   rv   
SETUP_LOOP      z$InstructionTranslatorBase.SETUP_LOOPc                 C   rR  ry   rS  r   rp   rp   rv   SETUP_EXCEPT  rU  z&InstructionTranslatorBase.SETUP_EXCEPTc                 C   r   ry   )rb  r.  r   rp   rp   rv   	POP_BLOCK  r  z#InstructionTranslatorBase.POP_BLOCKc                 C      |  | d S ry   setup_or_before_withr   rp   rp   rv   
SETUP_WITH  r  z$InstructionTranslatorBase.SETUP_WITHc                 C   rR  ry   rS  r   rp   rp   rv   SETUP_FINALLY  s    z'InstructionTranslatorBase.SETUP_FINALLYc                 C   s   |  d  d S ry   rr  r   rp   rp   rv   BEGIN_FINALLY  r  z'InstructionTranslatorBase.BEGIN_FINALLYc                 C   sH   |  d\}}|d u sJ | | | || td gd i  d S )Nr  r%  )r   r   rq   rK   r  )r   r   r   r   rp   rp   rv   WITH_CLEANUP_START  s   
$z,InstructionTranslatorBase.WITH_CLEANUP_STARTc                 C   s   |  d | d  d S )Nr  )r   r   r   rp   rp   rv   WITH_CLEANUP_FINISH  rM  z-InstructionTranslatorBase.WITH_CLEANUP_FINISHc                 C   s*   | j | j }| t| | | dS )z|
        pushes the address of the next instruction onto the stack and increments
        bytecode counter by delta
        N)rQ  r0  r   rK   r  rA  )r   r   addrrp   rp   rv   CALL_FINALLY  s   z&InstructionTranslatorBase.CALL_FINALLYc                 C   s&   |   }t|tr| | _d S 	 d S ry   )r.  r   rK   r@  r   r   r   tosrp   rp   rv   END_FINALLY  s   
z%InstructionTranslatorBase.END_FINALLYc                 C   s0   |j }|r	|  }|  }|r| | d S d S ry   )r   r.  r   )r   r   Zpreserve_tosrc  r   rp   rp   rv   POP_FINALLY  s   z%InstructionTranslatorBase.POP_FINALLYc              
   C   s   |    }z|| }| | | | W d S  ttjfyP } z)t|tjr.t|  t	j
dkr@| | | td  | | W Y d }~d S d }~ww Nr`  )r.  realizenext_variabler   StopIterationr   ObservedUserStopIterationr   rI  r1  r2  rK   r  rA  )r   r   itr  rN  rp   rp   rv   FOR_ITER  s   




z"InstructionTranslatorBase.FOR_ITERc                 C   s   t |tjtfr|| g i }t| jr)t |tjr)|jt	u r)tt
| g i }| j| | |rAt|j}|d| tdttdg tjd d S )Nzraised exception zFailed to raise exceptionz.Attempted to raise a non-Exception type/value.r  )r   r   rJ   rm   rq   r    r  r`   exc_typeri  r  r  r  _isinstance_exceptionr   Zget_dynamo_observed_exceptionr)   r   r   rC  )r   r  Zobserved_exception_typerp   rp   rv   _raise_exception_variable  s(   




z3InstructionTranslatorBase._raise_exception_variablec                 C   s   |j dkr!t| jsJ | jd }| |sJ || | d S |j dkr2| jd }| | d S |  }t|trm|j	d u rm|  }z| | W | j
 }|| tdtd  n| j
 }|| tdtd  w tdt|dg d d S )Nr   r  r   	__cause__zRe-raise with 2 argumentsz3Dynamo does not support `raise ... from [not-None]`r  )rg  r   r  rn  ro  r  r.  r   rK   r5  r  Zcall_setattrr)   r   )r   r   r  Zfrom_vtZcurr_excrp   rp   rv   r     s6   






z'InstructionTranslatorBase.RAISE_VARARGSc                 C   sD   | j d }t|tsJ |jtu rtdddg d d S | | d S )Nr  z CLEANUP_THROW with StopIterationr   zRReceived StopIteration when handling generator.throw/close. This is not supported.r  )r  r   r`   rm  ri  r)   RERAISErb  rp   rp   rv   CLEANUP_THROW  s   


z'InstructionTranslatorBase.CLEANUP_THROWc                 C   sl   t jdkr#|  }|jr|  }| | d S | | | | d S |  }|  }|  }| | d S )Nr\  )r1  r2  r.  r   ro  r   )r   r   r  r   Z_excZ_tbrp   rp   rv   rq  ,  s   

z!InstructionTranslatorBase.RERAISEc                 C   s   t |tjttfS ry   )r   r   r`   rm   rn   r  rp   rp   rv   rn  C  s   z/InstructionTranslatorBase._isinstance_exceptionc                 C   s   t jdkr)t| jdksJ | jd }| jd }| |sJ t|j}td }n#t| jdks2J | jd }| jd }| |sCJ t|j}td }| ||||gi  d S )Nr\     r     ir  )	r1  r2  r   r  rn  rJ   rm  rK   rq   )r   r   r   r  typtbrp   rp   rv   WITH_EXCEPT_STARTM  s   







z+InstructionTranslatorBase.WITH_EXCEPT_STARTc                 C   s  d}t jdkrZ| jj}|r=t| j|jkr!|   t| j|jks|jr.| 	t
| jj | 	| j  | | d S | j  t| tu rXtdt||g tjtjd |t| jr+| j }|jjdkr| d | j  t| jdkr| j  t| tu rtdt||d	 g tjd || j }|jjdksk| j }| j  t| j|jkr|   t| j|jkstd
dd d}| j t!|d t| j t| jdkr| jd }| 	t
"  | 	| | 	t
#|j$ n| 	t
d  | 	t
d  | 	t
d  | 	t
"  | 	| | 	t
#|j$ | | d S | j  t| tu rGtdt||g tjtjd |)NzDynamo found no exception handler at the top-level compiled function when encountering an exception. Exception will propagate outside the compiled region.r\  zObserved exceptionr  EXCEPT_HANDLERr%  r   z#Observed exception (EXCEPT_HANDLER)z  This graph break is unexpected.g    .Ar  r  )%r1  r2  r  ri  r   r  depthr.  lastir   r   rK   r  r  r  rA  r   r  r   r)   r   r   rC  rw  rb  r   r   r   
DYNAMO_BUGr  r   r   r   r   rd   rJ   rm  )r   Zraised_exceptionZobserved_exn_gb_explanationri  Zblock_stack_entryZexception_varZexcept_handler_instZold_exceptionrp   rp   rv   r  f  s   

	










	z+InstructionTranslatorBase.exception_handlerc                 C   sL   |   }t| jdkrtd }n| jd }| | | | | j  d S )Nr   r  )r.  r   r  rK   r   r  )r   r   r  Zprev_excrp   rp   rv   PUSH_EXC_INFO  s   



z'InstructionTranslatorBase.PUSH_EXC_INFOc                 C   s   t jdkr|  }t| jsJ | j  d S t| jdks J | jd jjdkr-td| j  | 	d t| js>J | j  d S )Nr\  r   r  ry  zYBug in Dynamo tracing of exception handling.Top of the block stack is not EXCEPT_HANDLER.r%  )
r1  r2  r.  r   r  rb  r   r   r  r   )r   r   r   rp   rp   rv   
POP_EXCEPT  s   


z$InstructionTranslatorBase.POP_EXCEPTc                 C   s:  t | jdks	J |  }tjdkr| jd }n| j }t|tttt	fs7t
dt|d| dg tjd tjdkrR| |sRt
dt|d	| dg tjd t|tr[|j}n|g}|D ]:}t|tt	tfs{t
d
t|d| dg tjd | |rt|j|jr dS t|tjrt|j|jr dS q`dS )Nr  r\  r  z Exception with bad expected typez"`except ...` has unsupported type r  r  zCaught non-Exception valuezCExcept expects to recieve an object of Exception type but received z#Exception with non-type expectationz!`except ...` expects a non-type: TF)r   r  r.  r1  r2  r   rJ   r^   rm   rn   r)   r   r   rC  rn  r   
issubclassrm  r   r   )r   Zexpected_exc_typesZexc_instanceZexpected_typesexpected_typerp   rp   rv   check_if_exc_matches  sp   

	





z.InstructionTranslatorBase.check_if_exc_matchesc                 C   s   |  t|   d S ry   )r   r   rK   r  r   rp   rp   rv   CHECK_EXC_MATCHd     z)InstructionTranslatorBase.CHECK_EXC_MATCHc                 C   s   |   s| | d S d S ry   )r  rA  r   rp   rp   rv   JUMP_IF_NOT_EXC_MATCHg  s   z/InstructionTranslatorBase.JUMP_IF_NOT_EXC_MATCHc                 C   s:   |j dkr| | d S | t|j  | | di  d S )Nzexception matchr  )r   r  r   rx   r   r   rp   rp   rv   
COMPARE_OPk  s   
"z$InstructionTranslatorBase.COMPARE_OPc                 C   s   |  tt|  gi  d S ry   )rq   rJ   iterr.  r   rp   rp   rv   GET_ITERq     z"InstructionTranslatorBase.GET_ITERr   rr  c                 C   s&   |  |j}|  }| ||i  d S ry   r   r   r.  rq   )r   r   r~   r   rp   rp   rv   r   t  s   z'InstructionTranslatorBase.CALL_FUNCTIONc                 C   s  |j dkrti }|  }n!|j dkr|  }|  }ntdd|j  d|j  g tjd tjdkr?|  }t|t	s?J |  }tjdkrXtjdk rX|  }t|t	sXJ t|t
rt|jtr| }|jd	kryt|ttfryt|g}nK|jd
krt|trt|jdkrt|trtd|v rtdddg d n%|jdkrt|trt|jdkrt|trtd|v rtdddg d t|ts|| rt|| }t|trt| t|}t|trt|tstddt| dt| dg tjd | }| ||j| d S )Nr   r   z%Variadic function call with bad flagszflags: zHAttempted to call a variadic function (CALL_FUNCTION_EX) with bad flags r  r$  r\  viewZrandom_fromz,Tensor.random_ op called with `from` keywordr   r(  Zuniform_z-Tensor.uniform_ op called with `from` keywordz0Variadic function call with bad args/kwargs typezargs type: z, kwargs type: z2Expected args to be a list and kwargs to be a dict)r   rO   r.  r)   r   r|  r1  r2  r   rb   ra   objrh   rg  r   rK   r^   r   r   r  rZ   has_force_unpack_var_sequenceforce_unpack_var_sequencero   rJ   Zcall_custom_dictr   rE   rC  Zkeys_as_python_constantrq   )r   r   Z
kwargsvarsZargsvarsnullr   rp   rp   rv   CALL_FUNCTION_EXz  s   









z*InstructionTranslatorBase.CALL_FUNCTION_EXc                 C   s   |   }| |j}|   }t|tr| sJ | }|d t|  |t| d  }}tt	||}t|t|ksAJ | 
||| d S ry   )r.  r   r   r   r^   r?  r@  r   r   ziprq   )r   r   argnamesr~   r   kwargs_listr  rp   rp   rv   CALL_FUNCTION_KW  s   &z*InstructionTranslatorBase.CALL_FUNCTION_KWc                 C   sf   |  tj|dd |jd }| jd | }tjdk r'| tj||d d S | tj||d d S )Nr  r_  r   co_namesr\  )	r   r   r	  r   r  r1  r2  rK  LOAD_METHODr   r   rg  r   rp   rp   rv   LOAD_METHOD_SUPER  s   

z+InstructionTranslatorBase.LOAD_METHOD_SUPERc                 C   sD   |  tj|dd |jd }| jd | }| tj||d d S )Nr  r_  r   r  )r   r   r	  r   r  rK  r  rp   rp   rv   LOAD_ATTR_SUPER  s   
z)InstructionTranslatorBase.LOAD_ATTR_SUPERc                 C   sn   |  | |  }tjdkr| | | | d S tjdkr+| | | | d S | | | d  d S )Nr$  r\  )rK  r.  r1  r2  r   r&  r   r   r  rp   rp   rv   r    s   





z%InstructionTranslatorBase.LOAD_METHODc                 C   s:   |  |j}|  }|d u sJ |  }| ||i  d S ry   r  )r   r   r~   dummyr   rp   rp   rv   CALL_METHOD	  s
   z%InstructionTranslatorBase.CALL_METHODc                 C   s4   |   }tt| |t|jgi }| | d S ry   )r.  rJ   ry  rq   rK   r  r   r   )r   r   r  rK  rp   rp   rv   rK  	  s   z$InstructionTranslatorBase._load_attrc                 C   s0   t jdkr|jd r| | d S | | d S )Nr`  r  )r1  r2  rg  r  rK  r   rp   rp   rv   	LOAD_ATTR	  s
   


z#InstructionTranslatorBase.LOAD_ATTRc              
   C   s   |   }|jr| |S | d\}}t|tr)t|ts)| jr)J d|j dzt	t
| |t|j|gi  W d S  tyc } z|  sI tjddd |  |d W Y d }~nd }~ww |  d S )Nr  zMutating module attribute z during export.zSTORE_ATTR triggered compileTr  rR  )rS  r   store_attr_graph_breakr   r   re   rK   exportr   rJ   setattrrq   r  r*   r(  r   r  rU  rV  r   )r   r   rW  r  r  rN  rp   rp   rv   
STORE_ATTR	  s0   
z$InstructionTranslatorBase.STORE_ATTRc                 C   sx   t | jdd |  stdddg d | jj| td|  gd | jt		|g | 
d | j| | j d S )	NzSTORE_ATTR-caused graph breakr"  z-Should not compile partial graph (STORE_ATTR)r   zDynamo has determined when encountering an unsupported STORE_ATTR instruction (i.e. `obj.attr = val`) that it should not compile the partial graph.r  
store_attrr  )r  r  r(  r)   r   r-  r.   r*  r3  rh  r   r/  r0  r   rp   rp   rv   r  :	  s    

z0InstructionTranslatorBase.store_attr_graph_breakc                 C   s*   |   }tt| |t|jgi  d S ry   )r.  rJ   delattrrq   rK   r  r   r  rp   rp   rv   DELETE_ATTRM	  s   z%InstructionTranslatorBase.DELETE_ATTRc                 C      t dt|  )Nz1create_call_resume_at not overridden by subclass r  r  r   r  rp   rp   rv   r/  U	     z/InstructionTranslatorBase.create_call_resume_atc                 C   r  )Nz8should_compile_partial_graph not overridden by subclass r  r   rp   rp   rv   r(  Z	  r  z6InstructionTranslatorBase.should_compile_partial_graphr   c                 C   s(   |  d\}}}|| d||gi  d S )Nr%  __setitem__r   call_method)r   r   r  r  keyrp   rp   rv   STORE_SUBSCR_	  s   z&InstructionTranslatorBase.STORE_SUBSCRc                 C   s$   |  d\}}|| d|gi  d S )Nr  __delitem__r  )r   r   r  r  rp   rp   rv   DELETE_SUBSCRd	  s   z'InstructionTranslatorBase.DELETE_SUBSCRc                 C      |  |j}| t| d S ry   )r   r   r   r^   r   r   r   rp   rp   rv   BUILD_TUPLEh	     z%InstructionTranslatorBase.BUILD_TUPLEc                 C   r  ry   )r   r   r   r]   r  rp   rp   rv   BUILD_SLICEl	  r  z%InstructionTranslatorBase.BUILD_SLICEc                 C   s$   |  |j}| t|t d d S NZmutation_type)r   r   r   r\   rF   r  rp   rp   rv   
BUILD_LISTp	  s   z$InstructionTranslatorBase.BUILD_LISTc                 C   s>   t jrtdddg d | |j}t|t d}| | d S )Nzmissing BUILD_SET handlerr   z:Missing BUILD_SET bytecode handler (for testing purposes).r  r  )r   Z+inject_BUILD_SET_unimplemented_TESTING_ONLYr)   r   r   rP   rF   r   )r   r   r   Znew_setrp   rp   rv   	BUILD_SETt	  s   z#InstructionTranslatorBase.BUILD_SETc              
   C   sx   |  |j}g }|D ]%}z
|||  W q
 ty/   tdt|| dg tjd Y q
w | 	||t
 d d S )Nz-Failed to unpack object for BUILD_LIST_UNPACKzU cannot be unpacked into a list for the BUILD_LIST_UNPACK bytecode (`[*x, *y, ...]`).r  r  )r   r   extendr  NotImplementedErrorr)   r   r   rC  r   rF   )r   r   r   Zseqsr   seqrp   rp   rv   BUILD_LIST_UNPACK	  s   
z+InstructionTranslatorBase.BUILD_LIST_UNPACKc                 C   s   | j |td d S )Nr   )r  r^   r   rp   rp   rv   BUILD_TUPLE_UNPACK	  s   z,InstructionTranslatorBase.BUILD_TUPLE_UNPACKc                 C   sJ   |  |jd }tt|d d d |dd d }| t|t d d S )Nr  r   r  )r   r   r   r  r   rO   rF   )r   r   r   drp   rp   rv   	BUILD_MAP	  s   "z#InstructionTranslatorBase.BUILD_MAPc                    s^     |j} fdd|D }i }|D ]}t|tsJ ||j q t|t d d S )Nc                    s    g | ]}t t |gi qS rp   )rJ   r   rq   r  r   rp   rv   r  	  s     z>InstructionTranslatorBase.BUILD_MAP_UNPACK.<locals>.<listcomp>r  )r   r   r   rO   updater   r   rF   )r   r   r   rK  rM  rp   r   rv   BUILD_MAP_UNPACK	  s   z*InstructionTranslatorBase.BUILD_MAP_UNPACKc                 C   sn   |   }| |j}t|tsJ | sJ || }t|t|ks&J | t	t
t||t d d S r  )r.  r   r   r   r^   r?  r  r   r   rO   r   r  rF   )r   r   keysr  rp   rp   rv   BUILD_CONST_KEY_MAP	  s   
z-InstructionTranslatorBase.BUILD_CONST_KEY_MAPc                 C   sT   |  d\}}|jdksJ | j|j   }t|tsJ || d||fi  d S )Nr  r   r  )r   r   r  rg  rg  r   rO   r  )r   r   rt   ru   r  rp   rp   rv   MAP_ADD	  s
   z!InstructionTranslatorBase.MAP_ADDc                 C   sP   |   }|jdksJ | j|j  }t|tsJ | sJ || d|gi S )Nr   r   r.  r   r  rg  r   rP   
is_mutabler  r   r   ru   r  rp   rp   rv   SET_ADD	  s   z!InstructionTranslatorBase.SET_ADDc                 C   T   |   }|jdksJ | j|j  }t|tsJ | sJ || d|gi  d S Nr   r  r  r  rp   rp   rv   
SET_UPDATE	     z$InstructionTranslatorBase.SET_UPDATEc                 C   s`   |   }|jdksJ | j|j   }t|tsJ | s!J | jj	
| |j| d S r   )r.  r   r  rg  rg  r   r\   r  r   r  Zmutationr   r   r  rp   rp   rv   LIST_APPEND	  s   z%InstructionTranslatorBase.LIST_APPENDc           	   
   C   s   |j }tjdk r|  }|  }tjdkr%t|jdsJ tj|jjd}d }d }d }d }tjdk rR|d@ r:|  }|d@ rB|  }|d@ rJ|  }|d@ rR|  }| 	t
||| j|||| d S )	Nr\  co_qualnamer  r$     rs  r  r   )rg  r1  r2  r.  r   r5  rK   r  r  r   rT   r  )	r   r   flagsfn_namecodedefaultsclosureannotations
kwdefaultsrp   rp   rv   MAKE_FUNCTION	  s>   


z'InstructionTranslatorBase.MAKE_FUNCTIONc                    s     }t|tr|jt|jd}n=t|tr6t|jtr6t|j	 |j
  fddt|jD }n|rA|}ntdt|| dg tjd t||jkrotdd|j d	t| | d
g tjd t|D ]}| qsd S )N)Zidxesc                    s   g | ]	}t  | qS rp   )rI   r~  proxyr   rp   rv   r  
  s    z=InstructionTranslatorBase.UNPACK_SEQUENCE.<locals>.<listcomp>z+Failed to unpack object for UNPACK_SEQUENCEzV cannot be unpacked into a list for the UNPACK_SEQUENCE bytecode (i.e. `a, b, c = d`).r  z9Length mismatch when unpacking object for UNPACK_SEQUENCEzexpected length: z
, actual: za unpacked to a list for the UNPACK_SEQUENCE bytecode (i.e. `a, b, c = d`) with unexpected length.)r.  r   rh   rJ  rn  r   ra   r  ry  Zas_proxyr   r  r  r)   r   r   rC  r   r|  r{   r   )r   r   r  r  r  rp   r  rv   UNPACK_SEQUENCE
  s0   

z)InstructionTranslatorBase.UNPACK_SEQUENCEc           
      C   s   d|j   krdksJ  J |j d@ }|j d? }|  }|| rnt|| }t||| ks3J |d | }||t||  }|t|| d  }t|D ]}	| |	 qQ| t| t|D ]}	| |	 qdd S t	dt
|| dg tjd d S )Nr   i     r  z%Failed to unpack object for UNPACK_EXz; cannot be unpacked into a list for the UNPACK_EX bytecode.r  )r   r.  r  r   r  r   r{   r   r^   r)   r   r   rC  )
r   r   prefixsuffixr  r  Zvals_prefixZ	vals_listZvals_suffixitemrp   rp   rv   	UNPACK_EX
  s,   



z#InstructionTranslatorBase.UNPACK_EXc                 C   r  ry   rp   r   rp   rp   rv   r  6
  r  zInstructionTranslatorBase.NOPc                 C      |    d S ry   r   r   rp   rp   rv   POP_TOP9
     z!InstructionTranslatorBase.POP_TOPc                 C   s(   |   }|   }| | | | d S ry   r.  r   r   r   arq  rp   rp   rv   ROT_TWO<
  s   
z!InstructionTranslatorBase.ROT_TWOc                 C   s:   |   }|   }|   }| | | | | | d S ry   r  )r   r   r  rq  crp   rp   rv   	ROT_THREEB
  s   

z#InstructionTranslatorBase.ROT_THREEc                 C   sL   |   }|   }|   }|   }| | | | | | | | d S ry   r  )r   r   r  rq  r  r  rp   rp   rv   ROT_FOURJ
  s   


z"InstructionTranslatorBase.ROT_FOURc                 C   s    |   }| | | | d S ry   r  )r   r   r  rp   rp   rv   rJ  T
  s   
z!InstructionTranslatorBase.DUP_TOPc                 C   s<   |   }|   }| | | | | | | | d S ry   r  r  rp   rp   rv   DUP_TOP_TWOY
  s   


z%InstructionTranslatorBase.DUP_TOP_TWOc                 C   sX   |dkrt t| |gi S |dkrt t| |gi S |dkr*t t| |gi S |S )Nr   r  r%  )rJ   r   rq   reprascii)r   r5  flagrp   rp   rv   _convert_valuea
  s   z(InstructionTranslatorBase._convert_valuec                 C   s   |   }t|tr#ddlm}m} |j||||jd}| | d S | 	||d@ }t
d|  d }| ttj||gi  d S )Nr   )LazySymNodeFormatStringrY   rC  r%  z{:})r.  r   rg   Ztorch._dynamo.variables.lazyr  rY   r  rE  r   r  rK   r@  rq   rJ   r   format)r   fmt_specr  r5  r  rY   Zfmt_varrp   rp   rv   _format_valuej
  s   

z'InstructionTranslatorBase._format_valuec                 C   s2   |j }|d@ dkr|  }ntd}| ||S )Nrs  r   )rg  r.  rK   r  r  )r   r   r  r  rp   rp   rv   FORMAT_VALUE~
  s
   

z&InstructionTranslatorBase.FORMAT_VALUEc              	   C   s   g }g }i }|  |jD ]Z}t|tr|d || qt|tjrY||j ||j	 t
| t
|j @ rRtdd| d| d|j dg tjd ||j qtdt|d	g tjd q| tjd
||| d S )Nz{}zBUILD_STRING key conflictzformat_string_parts: z
, kwargs: z, part.sym_kwargs: z1Failed to build format string due to key conflictr  zBUILD_STRING type errorzLFormat string part type is not correct - expected constant or format string.r   )r   rg  r   rK   r   r   ZStringFormatVariableformat_stringr  Zsym_argsr   r  Z
sym_kwargsr)   r   rC  r  r   r   r  r   )r   r   Zformat_string_partsr~   r  partrp   rp   rv   BUILD_STRING
  s:   

z&InstructionTranslatorBase.BUILD_STRINGc                 C   sF   |j dks|j dksJ |j dkrd}nd}td|d}| | d S )Nr   r   iszis notr  r_  )r   r   r  )r   r   Z
new_argvalZnew_instrp   rp   rv   IS_OP
  s   
zInstructionTranslatorBase.IS_OPc                 C   s^   |j dks|j dksJ | d\}}|j }| || d|gi  |dkr-| | d S d S )Nr   r   r  __contains__)r   r   r   r  	UNARY_NOT)r   r   leftrightr{  rp   rp   rv   CONTAINS_OP
  s   z%InstructionTranslatorBase.CONTAINS_OPc                 C   r  )Nr   r  )r.  r   r  rg  r   r\   r  r  r  rp   rp   rv   LIST_EXTEND
  r  z%InstructionTranslatorBase.LIST_EXTENDc                 C   s"   |  tt| |  gi  d S ry   )r   rJ   r   rq   r.  r   rp   rp   rv   LIST_TO_TUPLE
  s   "z'InstructionTranslatorBase.LIST_TO_TUPLEc                 C   sH   | j d }| |sJ |jtu r"tt| g i }|| j d< d S d S Nr  )r  rn  rm  ri  r   rJ   r  rq   )r   r   r  new_valrp   rp   rv   STOPITERATION_ERROR
  s   


z-InstructionTranslatorBase.STOPITERATION_ERRORc                 C   sX   |   }|jdksJ | j|j   }t|tsJ | s!J || d|gi  d S r  )	r.  r   r  rg  rg  r   rO   r  r  r  rp   rp   rv   
DICT_MERGE
  s   z$InstructionTranslatorBase.DICT_MERGEc                 C   r  ry   r   r   rp   rp   rv   	GEN_START
  r  z#InstructionTranslatorBase.GEN_STARTc                 C   sH   | j d }| r| tt|  d S | || dg i  d S )Nr  r=  )r  r?  r   rK   r  r   r@  r  rb  rp   rp   rv   GET_LEN
  s   
z!InstructionTranslatorBase.GET_LENc                 C   sP   | j d }t|tsJ t|jtjjr| t	d d S | t	d d S Nr  TF)
r  r   rO   r   collectionsabcMappingr   rK   r  rb  rp   rp   rv   MATCH_MAPPING
  s
   
z'InstructionTranslatorBase.MATCH_MAPPINGc                 C   sd   | j d }| sJ | }t|tjjr(t|ttt	fs(| 
td d S | 
td d S r  )r  r?  r@  r   r  r	  Sequencer   bytes	bytearrayr   rK   r  )r   r   rc  Z	tos_valuerp   rp   rv   MATCH_SEQUENCE
  s   

z(InstructionTranslatorBase.MATCH_SEQUENCEc                    s    j d } j d ttsJ tfdd|D r< t fdd|D  tjdk r: t	d d S d S  t	d  tjdk rS t	d	 d S d S )
Nr  r  c                 3   s    | ]}| v V  qd S ry   rp   rs   rt   )tos1rp   rv   r   
      z7InstructionTranslatorBase.MATCH_KEYS.<locals>.<genexpr>c                    s   g | ]}  |qS rp   )Zgetitem_constr  r   r  rp   rv   r         z8InstructionTranslatorBase.MATCH_KEYS.<locals>.<listcomp>r\  TF)
r  r   rO   r  r   r^   r1  r2  rK   r  rb  rp   r  rv   
MATCH_KEYS
  s   



z$InstructionTranslatorBase.MATCH_KEYSc                 C   s   |  d d S )Nr  )rN  r   rp   rp   rv   r     r  z.InstructionTranslatorBase.LOAD_ASSERTION_ERRORc                 C   s,   |j dkr| | d| _d S | jrJ d S )Nr   F)rg  append_prefix_instr  r   rp   rp   rv   RESUME0  s   


z InstructionTranslatorBase.RESUMEc                 C   s   t |j | |S ry   )_binary_op_lookuprg  r   rp   rp   rv   	BINARY_OP9  r  z#InstructionTranslatorBase.BINARY_OPc                 C   r  ry   rp   r   rp   rp   rv   r   <  r  z!InstructionTranslatorBase.PRECALLc                 C   sV   | j d |j }t|tsJ |D ]	}t|tsJ q| jd u s"J tj|d| _d S )N	co_constsr  )r  rg  r   r   r   rf  rK   r  )r   r   rf  r   rp   rp   rv   r^  ?  s   z"InstructionTranslatorBase.KW_NAMESc                 C   s   |  t  d S ry   )r   rb   r   rp   rp   rv   r&  G  r  z#InstructionTranslatorBase.PUSH_NULLc           	      C   sB  |rt jdks	J |  }t|tr| sJ | }n	| jr$| jjnd}| 	|j
d }t jdkrF|d }t|d tr@g n|d g}nt|d trT|d }g }n	|d }|d g}|r||dt|   }|t| d  }tt||}t|t|ksJ n
||dd   }i }z| ||| W d | _d S d | _w )Nr$  rp   r  r   r   )r1  r2  r.  r   r^   r?  r@  rf  r5  r   rg  rb   r   r   r  rq   )	r   r   call_kwrf  contentsr   r~   r  r  rp   rp   rv   _callJ  s2   


zInstructionTranslatorBase._callc                 C   rX  ry   r  r   rp   rp   rv   r   s  r   zInstructionTranslatorBase.CALLc                 C   s   |  | j|j   d S ry   )r   r  rg  r   rp   rp   rv   COPYw  r  zInstructionTranslatorBase.COPYc                 C   s.   | j |j  | j d | j d< | j |j < d S r  )r  rg  r   rp   rp   rv   SWAPz  s   .zInstructionTranslatorBase.SWAPc                 C   r  ry   rp   r   rp   rp   rv   CACHE  r  zInstructionTranslatorBase.CACHEc                 C   rX  ry   rY  r   rp   rp   rv   BEFORE_WITH  r  z%InstructionTranslatorBase.BEFORE_WITHc                 C   s  |   }t|ttfstdd| d|  dddgd t|tr-| s-| j| t|ttfs6J t	||j
}tjdkrZ|jrT| jrQ|jj
| jd	 j
urTd }n	| jjj
}n|j
}| | |rt| trx| jt||t| j| n| jt||t| j | ||  d S )
NzUnsupported context managerz$Attempted SETUP_WITH/BEFORE_WITH on z%Dynamo does not know how to enter a `z` context manager.z,Avoid using the unsupported context manager.zFile an issue to PyTorch. Simple context managers can potentially be supported, but note that context managers can't be supported in generalr  r\  r  )r.  r   rL   rM   r)   Zpython_type_nameZsupports_graph_breaksrT  r   rN   r   r1  r2  ri  rb  r0  r   r   r   r   r  Zenter)r   r   r  r   r   rp   rp   rv   rZ    sP   


z.InstructionTranslatorBase.setup_or_before_withc                 C   s   | j sJ | j| d S ry   )r  r  r   r   rp   rp   rv   r    s   
z,InstructionTranslatorBase.append_prefix_instc                 C   sL   t jdkr| jst| j|j tsJ | jj	 | j|j< d S | 
| d S rf  )r1  r2  r  r   r  r   rb   r   r  track_cell_newr  r   rp   rp   rv   	MAKE_CELL  s
   
z#InstructionTranslatorBase.MAKE_CELLc                 C   rX  ry   r  r   rp   rp   rv   COPY_FREE_VARS  r  z(InstructionTranslatorBase.COPY_FREE_VARSc                 C   rX  ry   r%  r   rp   rp   rv   RETURN_GENERATOR  r  z*InstructionTranslatorBase.RETURN_GENERATORc                 C   s$   t jdkr|   d S | d d S )Nr$  r  )r1  r2  r.  r   r   rp   rp   rv   END_FOR  s   
z!InstructionTranslatorBase.END_FORc                 C   s@   t | j|j trtd|jd|j g tjd | | d S )Nz)LOAD_FAST_CHECK on uninitialized variablez/Attempted to load uninitialized local variable r  )r   r  r   rb   r)   r   rC  r  r   rp   rp   rv   LOAD_FAST_CHECK  s   
z)InstructionTranslatorBase.LOAD_FAST_CHECKc                 C   s6   |j | jvr| t  n| | t | j|j < d S ry   )r   r  r   rb   r  r   rp   rp   rv   LOAD_FAST_AND_CLEAR  s   
z-InstructionTranslatorBase.LOAD_FAST_AND_CLEARc                 C   s:   |  tj|dd |jd@ r| | d S | | d S )Nr  r_  r   )r   r   r	  rg  r  rK  r   rp   rp   rv   LOAD_SUPER_ATTR  s   
z)InstructionTranslatorBase.LOAD_SUPER_ATTRc                 C   s   |j dkr| | d S |j dkr| | d S |j dkr+| t|  |  d S tdd|j  d|j  dg tj	d d S )	Nr%        z Missing CALL_INTRINSIC_1 handlerzCALL_INTRINSIC_1 operand: z,No handler implemented for CALL_INTRINSIC_1 z instruction.r  )
r   r  UNARY_POSITIVEr   r^   r.  r  r)   r   rw  r   rp   rp   rv   CALL_INTRINSIC_1  s   




z*InstructionTranslatorBase.CALL_INTRINSIC_1c                 C   s   |   }|    | | d S ry   r  rb  rp   rp   rv   END_SEND  s   z"InstructionTranslatorBase.END_SENDc                 C   s   | j |dd d S )NT)r  r  r   rp   rp   rv   CALL_KW  s   z!InstructionTranslatorBase.CALL_KWc                 C   s   | j jdv sJ d S )N)POP_JUMP_IF_TRUEPOP_JUMP_IF_FALSEr  )r0  r   r   rp   rp   rv   r'    s   z!InstructionTranslatorBase.TO_BOOLc                 C   sp   |j }|  }t|tsJ |  }|d@ r||_n|d@ r"||_n|d@ r*||_n|d@ r1||_| | d S )Nr  rs  r  r   )	rg  r.  r   rT   r  r  r  r  r   )r   r   r  r   attrrp   rp   rv   SET_FUNCTION_ATTRIBUTE%  s   z0InstructionTranslatorBase.SET_FUNCTION_ATTRIBUTEc                 C   s   |  | |  |j d S ry   )r   r  r.  r   r   rp   rp   rv   CONVERT_VALUE6  r  z'InstructionTranslatorBase.CONVERT_VALUEc                 C   s   |  tdd d S )Nr   r   )r  rK   r  r   rp   rp   rv   FORMAT_SIMPLE9  s   z'InstructionTranslatorBase.FORMAT_SIMPLEc                 C   s   |  |  d d S r   )r  r.  r   rp   rp   rv   FORMAT_WITH_SPEC<  r  z*InstructionTranslatorBase.FORMAT_WITH_SPECc                 C   s    | j  dkrdd | _dS dS )Nr   c                   S      dS NTrp   rp   rp   rp   rv   r   B      z>InstructionTranslatorBase.is_non_empty_graph.<locals>.<lambda>TF)r   count_callsr  r   rp   rp   rv   r  ?  s   
z,InstructionTranslatorBase.is_non_empty_graphc                 C   s.   |d u rg }d t|  gtt| S )Nr   )r   r
  r  r*  r   r{   )r   Zadditional_stack_framesrp   rp   rv   format_frame_summaryF  s   z.InstructionTranslatorBase.format_frame_summaryc                 C   s(   t jt| jdd| jt| jddddS )Nr  	<unknown>r  F)lookup_line)r
  FrameSummaryry  r  r   r   rp   rp   rv   r*  O  s   z'InstructionTranslatorBase.frame_summaryc                 C   s&   t | jdd}td}||d uS )Nr  r>  z.*torch/nn/modules.*)ry  r  recompilematch)r   r   Znn_modules_patternrp   rp   rv   is_co_filename_from_nn_modulesW  s   
z8InstructionTranslatorBase.is_co_filename_from_nn_modulesc                 C   s,   | j |t|}tt|tj |S ry   )	r   install_global_by_idweakrefrefr-   r7   rF  r,   ZWEAKREF_ALIVE)r   r  r5  global_namerp   rp   rv   store_global_weakref_by_id\  s
   z4InstructionTranslatorBase.store_global_weakref_by_idc                 C   s
   | j jjS ry   )r   tracing_context	fake_moder   rp   rp   rv   rK  c  r   z#InstructionTranslatorBase.fake_modecheck_fnc                 c   s*    | j }|| _ z	dV  W || _ dS || _ w )zx
        Strict mode is enabled on a per-VariableTracker level depending on the return value of check_fn(node).
        N)r  )r   rL  priorrp   rp   rv   strict_translation_modeg  s   z1InstructionTranslatorBase.strict_translation_modec                 C   sF   | j d usJ | j dksJ | j| jj| j| j d | j| j d  S r  )r   r   r   r  r  r   r   r   rp   rp   rv   rS  s  s   z#InstructionTranslatorBase.speculater   r  r  r  r  r  r  r   distributed_stater  c                    sd  t    || _|| _|| _|| _|| _|	| _g | _d| _	d | _
td| _g | _g | _d| _d | _d| _g | _|| _|| _t| j| _|| _|| _|| _|| _|
| _|d urbtjrbt|
||d| _nd | _i | _ i | _!|| _"d| _#d | _$d | _%t&j'dkrdd	l(m)}m*}m+}m,} |
j-||B |B |B @ r| .t/d  || _0d| _1d gt2|
j3 | _4t56|
j7| d S )
Nr   r  r  T)r  r  r  F)r%  
   r   )CO_ASYNC_GENERATORCO_COROUTINECO_GENERATORCO_ITERABLE_COROUTINE)8r  r  r   rO  r   r  r  r  r  r   r  r   r  rb  rT  r   rf  r  r  r  r   r   rQ  r  r  r  r  r  r   Zreplay_record_enabledr1   r  nn_module_stack	num_callsr  	one_graphr  r  r1  r2  resume_executionrQ  rR  rS  rT  co_flagsr   rJ   r  r  r   r  r  r  	lazycacher  )r   r   r   r  r  r  r  r  r  r  r  r  r  r   r  rO  r  rQ  rR  rS  rT  r  rp   rv   r  }  sb   



z"InstructionTranslatorBase.__init__ry   )F(  r   r   r   r/   r   r   r   rG   ri   r   r   r   r   r   rK   r   r   r   r  r1   r   r  r)  r  r  r  r  rq   r  r  r  r  r  r  r1  r2  r  propertyr0  r  r  r  r   r  r.  r   r  r  r  r  r  LOAD_CLOSUREr  r   r   r   cached_propertyr%  r'  r*  r   r!  r8  r@  rI  rL  rN  r  rA  JUMP_FORWARDJUMP_ABSOLUTErQ  r   not_r3  r   r2  JUMP_IF_FALSE_OR_POPJUMP_IF_TRUE_OR_POPrT  rV  rW  r[  r\  r]  r^  r_  ra  rd  re  rl  r   ro  r   rr  rq  rn  rx  r  r}  r~  r  r  r  r  r  rt  r   r  r  r  r  r  r  rK  r  r  r  r  r/  r(  r  r  r  r  r  r  r\   r  r  ZBUILD_TUPLE_UNPACK_WITH_CALLr  r  ZBUILD_MAP_UNPACK_WITH_CALLr  r  r  r  r  r  r  r  r  r  r  r  r  rJ  r  r  r  r  r  r  r  r  r   r  r  DICT_UPDATEr  r  r  r  r  r   r   posr.  negUNARY_NEGATIVEr  invertUNARY_INVERTpowBINARY_POWERmulBINARY_MULTIPLYmatmulBINARY_MATRIX_MULTIPLYfloordivBINARY_FLOOR_DIVIDEtruedivBINARY_TRUE_DIVIDErL  BINARY_MODULOZBINARY_REMAINDERr   
BINARY_ADDsubBINARY_SUBTRACTgetitemBINARY_SUBSCRlshiftBINARY_LSHIFTrshiftBINARY_RSHIFTand_
BINARY_ANDor_	BINARY_ORxor
BINARY_XORipowINPLACE_POWERimulINPLACE_MULTIPLYimatmulINPLACE_MATRIX_MULTIPLY	ifloordivINPLACE_FLOOR_DIVIDEitruedivINPLACE_TRUE_DIVIDEimodINPLACE_MODULOZINPLACE_REMAINDERiaddINPLACE_ADDisubINPLACE_SUBTRACTilshiftINPLACE_LSHIFTirshiftINPLACE_RSHIFTiandINPLACE_ANDixorINPLACE_XORior
INPLACE_ORr  r  r   r^  r&  r  r   r  r   r  ZJUMP_BACKWARD_NO_INTERRUPTZPOP_JUMP_FORWARD_IF_TRUEZPOP_JUMP_BACKWARD_IF_TRUEZPOP_JUMP_FORWARD_IF_FALSEZPOP_JUMP_BACKWARD_IF_FALSEr!  r"  rZ  r  r$  r&  r'  r(  r)  r*  r+  r/  r0  r1  r'  r5  r6  r7  r8  r  r=  r*  rD  rI  rK  
contextlibcontextmanagerrN  rS  rF  CodeTyper   r   r   CellTyper  r  rp   rp   r  rv   r     s8  
 			

	

-
)
'#	
	

9	
		$ 
{Q

a
		
	&
		 	































);

	"





	
)	metaclassc                
       s   e Zd Zed"ddZejdd Zdee	 de
ded	ee dd
f
 fddZdd ZdefddZ fddZdd Zdd Zdd Zdd Zdd Zdd Zd d! Z  ZS )#r   r   c                   C   s   t jS ry   )tls
current_txrp   rp   rp   rv   r    s   z InstructionTranslator.current_txc                 c   s0    t tdd }| t_z	d V  W |t_d S |t_w )Nr  )ry  r  r  )r   rM  rp   rp   rv   set_current_tx  s   z$InstructionTranslator.set_current_txr   r   r  rO  Nc                    s  t  tjd|j d|d  d|d   t jdi dt||	| |||||||d
d|d	|d
|d|d|d|di di dd d|d|ddd|d|d| |   t| j	j
 |   |
| _|| _| jrx| jsxJ di | _t|  }t|dd }| D ]&\}}||vrd }|rt||i  }t|t|d|d}|| j|< q| j	j}|  D ].}||v r|| }t|ddd}t||}| }||| n| }||_|| j|< qt|  |D ]5\}}t |}t|dd}z	t|j!|}W n t"y   t#$ }Y nw |%|||}||_|| j|< qt&|| _'g | _(|r<t#j)| j| _W d    n1 sGw   Y  W d    d S W d    d S 1 s`w   Y  d S )Nztorchdynamo start tracing  r  r   r  r   )Zlocal_scoper1  r  torch_function_mode_stackr   r  r  r  r  r  r  r  r  r  r  r  r   r   r  rO  z4Export without one graph - something has gone wrong.dynamismT)is_inputr  )r  is_derefed_cell_contents)r  rp   )*r   r   INFOr  r  r  r/   _throw_if_in_functorchr
   r   rJ  r  rW  r  r  r   r  r"   get_contextr}  r   	frozensetrY   r  r9   r  r  r#  r  r  r  r  r8   cell_contents
ValueErrorr   ZDeletedVariableZtrack_cell_existingri   r  Zdebug_localsZrealize_all)r   r   r  r  r  r  r  r  r  compiler_fnrW  r  Zexport_constraintsZframe_stater   r  rO  r  r  r   r5  Zlocal_dynamismvarr  Zcontents_sourcer  Zcell_varr  Zcell_sourcer  rp   rv   r    s   
TzInstructionTranslator.__init__c                 C   s   t jd}t| jjd| jj}t jj	 }t jjj
jt jjj
jt jjj
jf}|d urS| |v rU||urW| j }d| d| d| d}tdd|g d	 d S d S d S d S )
Neagerr  zhIf you are reaching here, it means dynamo failed for one of the following reasons:
- Calling torch.func.zP(compiled_fn) function from eager mode is not supported. Ensure that torch.func.zp is also wrapped within a torch.compile function. For more information, see PyTorch issue #128711.
- torch.func.z2(fn) requires the function to be inlined by dynamoz%Unsupported functorch tracing attemptr   r  )r  _dynamoZlookup_backendr   getattr_staticr   r  Z_CZ
_functorchZpeek_interpreter_stackZTransformTypeZVmapZGradZJvpr  r   lowerr)   )r   r  r  ciZforbidden_keysr   r7  rp   rp   rv   r    s4   


	
z,InstructionTranslator._throw_if_in_functorchrE  c                 C   s0   t |tr| j|j S t |tr| j|j S try   )r   r9   r  r  r6   r  rH  r  )r   rE  rp   rp   rv   get_example_value  s
   

z'InstructionTranslator.get_example_valuec                    s   t    d S ry   )r  r  r   r  rp   rv   r    r  zInstructionTranslator.runc                 C   sV   t jdkr| jj}|r| jr|j| jd jurdS tdd | jD o*| j o*| j S )Nr\  r  Fc                 s       | ]}|  V  qd S ry   )r   rs   rq  rp   rp   rv   r     r  zEInstructionTranslator.should_compile_partial_graph.<locals>.<genexpr>)	r1  r2  r  ri  rb  r   r  rW  rT  )r   r   rp   rp   rv   r(    s   
z2InstructionTranslator.should_compile_partial_graphc                    s  d _ |jdkrtdgS |jdkrtd|jdgS tj|tfddj D }tfdd|D }tfdd|D }t	j
dk rTt|d	ksTJ d
t g }tjD ]<\}}tt|rtt|}|jd u rudnt|j}	|||	f |   ttj| d   td q_g }
|D ]3}ttj|  }rtt|}|jd u rdnt|j}	|
||	f |   td|d qg }t	j
dkr'tjD ]\}}tt|r|| qd	}ttjD ],\}}tt|r&td|d | D ]} td|d q    |d7 }qtjt| }|t| }td|j }t j!j"|jtdd j#D |||tdd j#D t|t|
t|}t$%j!&ddd  }|d urt'(|t$%|d< |j)rj*+||  ,||d| nj*+|t-.|j/|   0|d|   fdd|D   t1|d  td  2 S )Nr  r  r_  c                 3   s(    | ]}| v r|  vr|V  qd S ry   )r  r  )r  r   rp   rv   r     s    z>InstructionTranslator.create_call_resume_at.<locals>.<genexpr>c                 3   s&    | ]}t t j| s|V  qd S ry   r  __instancecheck__rb   r  r  r   rp   rv   r         
c                 3   s&    | ]}t t j| r|V  qd S ry   r  r  r   rp   rv   r     r  r`  r   z&variables should not be NULL in < 3.12rp   r   r  r  r\  r  r   )rg  Z__resume_at_c                 s   s    | ]}|j jV  qd S ry   )r   r  r  rp   rp   rv   r     r  c                 s   r  ry   )r   r  rp   rp   rv   r     r  orig_graphmodulec                   S   r  ry   rp   rp   rp   rp   rv   r   $  r;  z=InstructionTranslator.create_call_resume_at.<locals>.<lambda>Tc                    r|  rp   )Zcreate_loadr  )ro  rp   rv   r  6  r  z?InstructionTranslator.create_call_resume_at.<locals>.<listcomp>F)3r   r   r   r   r   r   r   r  r  r1  r2  r   r#   	enumerater  r  r  rL   r   r   r   rd  rc  r   Zappend_outputrb   r{   rn  Zpop_nullr!   r  r2   lookupr  r   rb  r"   r  r}  rF  rG  r  r   Zinstall_global_unsafeZmake_function_with_closurerF  FunctionTyper  Zload_function_namer   re  )r   r   Zall_argnamesr  Zargnames_nullZstack_ctx_varsr  r  r  r   Zargnames_ctx_varsr   Z
null_idxesZnull_cntjZ	stack_lenr   Znew_codeZorig_graphmodule_mayberp   )ro  r  r   rv   r/    s   









z+InstructionTranslator.create_call_resume_atc                 C   s6   | j  D ]}t|trt| tjjr dS qdS )NTF)	r  r  r   rl   r  r@  r  nnModule)r   ru   rp   rp   rv   $symbolic_locals_contain_module_class;  s   z:InstructionTranslator.symbolic_locals_contain_module_classc                 C   sL   t | jr | jd  }r"t|tr$t|| t d| jd< d S d S d S d S )Nr  r  )r   r  r   rS   r[   r  rF   )r   rc  rp   rp   rv   "replace_tos_if_return_is_generatorC  s   z8InstructionTranslator.replace_tos_if_return_is_generatorc                 C   s   |    | jd usJ | jd usJ t d| j| j  tjs9| j dkr9| j	s9| 
 s9| js9| js9tdd | _t tjd| jj d|j d td|j | jj| td|  gd	d
d |jdkrmtdntd|jd}| j|g t)NrO  r   z#because no content in function callztorchdynamo done tracing r   r  z%s triggered compilereturn_valueF)rR  r"  r  r  r_  )r  r   r  r@   rP  r   Zallow_empty_graphsr   r<  r  r  r  rW  r   r,  r   r   r  r  r  r   r   r  r-  r.   r*  r   r   r3  r   )r   r   Zreturn_instrp   rp   rv   _returnN  sH   

zInstructionTranslator._returnc                 C   rX  ry   r  r   rp   rp   rv   r  t  r  z"InstructionTranslator.RETURN_VALUEc                 C   rX  ry   r  r   rp   rp   rv   r  w  r  z"InstructionTranslator.RETURN_CONST)r   r   )r   r   r   staticmethodr  r  r  r  r   r   r   r  r   r   r  r  r:   r  r  r(  r/  r  r  r  r  r  r  rp   rp   r  rv   r     s6    
 &r   r\  c                 C   s<   g | ]\}}t td |v r|dd nd|dd  qS )ZINPLACEr%  NZBINARY_)ry  r   )rs   r   r   rp   rp   rv   r  |  s    $r  c                       s   e Zd ZU dZee ed< edd Ze	dd Z
e	dedee fd	d
Zdd Zdedejdeeef deeef dededdf fddZedd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Z fd%d&Z fd'd(Z  Z S ))r  z Trace and inline a called methodsymbolic_resultc                 C   sP   t tdtd i | ||||}| W  d    S 1 s!w   Y  d S )NZunimplementedr  )r   r   r=   build_inline_tracerinline_call_)r   r  funcr~   r  tracerrp   rp   rv   r    s   $z)InliningInstructionTranslator.inline_callc              
   C   s:  |   rtdt| dg d tj| dd}|jrxddlm} tt	| dd d	r4| j
j|fv r4td
dS t| dr=| j
jnd}d| dg}d|  vr[|d| d|   ddg7 }tdd| d|   d|   d|j d| d|d t| trt|  dd
rtdt|  d|   ddgd d S |S ) NzInline attempt with __self__zAttempted to inline a function with the `__self__` attribute. Dynamo is expected to decompose method calls into function calls with a `self` argument.r  T)Zis_inlined_callr   )!produce_trampoline_autograd_applyr   _originFz"allowlist in dynamo known functionr   zAvoid calling the function `rv  r  zRemove the function `z` or the file `zv` from torch/_dynamo/trace_rules.py. More graph breaks may occur as a result of attempting to trace into the function.z Please file an issue to PyTorch.z.Attempted to inline function marked as skippedz
qualname: z, name: z, filename: `z`, skip reason: z?Dynamo developers have intentionally marked that the function `z` should not be traced.Z_torchdynamo_disablez2Skip inlining `torch.compiler.disable()`d functionzSkip inlining function z3 since it was wrapped with `torch.compiler.disable`z(Remove the `torch.compiler.disable` call)Zhas_selfr)   r   r   Zcheck_verboseskippedZtorch._dynamo.variables.miscr  r   ry  r   r  Z
SkipResultr   get_filenameget_namer   r   rV   r   r  Zget_function)r  rK  r  Zfn_qualnamer!  rp   rp   rv   check_inlineable  sZ   

	


	z.InliningInstructionTranslator.check_inlineabler  r~   c                    sl  t |trtdd| dg d t |ttttfsJ t|}|j	du s'J z	|
| ||}W n. ty^ } z"tdjt|d|  d|  d	| j d
d |D |dd }~ww t| D ]}t |ts~tdd| d| dg tjd qf|   jdv r|rt |d tjstdd j d| d j dg d d}tjjj drdt!" !  }t#j$dkr| j%| j&| j'j(j)d fdd}	t*+dt,|	 t-+d  ||j. |rt |d t/r| j01|d j2}
t |
tj3j4rt56|
t78|
j9j:d!< t; r!t<|  || j=| j>|}|S t |tr)J t|  || j=| j>|}|S )"NzEAttempted to inline function marked as skipped (SkipFunctionVariable)z+Attempted to inline a SkipFunctionVariable zdAttempted to inline a function that was previously determined to be marked as intentionally skipped.r  Fz;{reason}.
  func = {func}, args = {args}, kwargs = {kwargs}'z' r   c                 S   s   g | ]}|  qS rp   )Zpython_type)rs   rg  rp   rp   rv   r    r  zEInliningInstructionTranslator.build_inline_tracer.<locals>.<listcomp>)r   r  r~   r  z:Encountered unconverted argument when attempting to inlinezfunc: z, arg: zWAn argument to an inlined function was not successfully converted to a VariableTracker.)r  __setattr__r   z2Unsupported __setitem__/__setattr__ inline attemptzcode name: z, args: zAttempted to inline z: where first argument (self) is not a user-defined object.r   bytecoder   r\  )r   c                     s&   t  } d j d d|  S )NzTRACE inlined call z from r   )r?   r  r  )r  r  Zcur_instheaderZparent_coderp   rv   get_trace_call_log_str  s   zQInliningInstructionTranslator.build_inline_tracer.<locals>.get_trace_call_log_strr  zINLINING %s%s, %sr  )?r   rU   r)   rV   rT   rR   rS   r  r  r  Z	bind_args	TypeErrorr$   r  r   r  r  r  r  r  r  r  rG   r   r|  r  r   ro   r  r  	_internalZ	log_stateZis_artifact_enabledrj  Bytecoder1  r2  r  r  r  Z	positionsr   trace_call_logr  rC   r   r   re   r   rG  rH  rB  r  rF  rG  r"   r  forward__code__r    &InliningGeneratorInstructionTranslatorr  r  )r  r  r~   r  rK  Z
sub_localsrN  ru   r  r  moduler  rp   r  rv   r    s   

	"



z1InliningInstructionTranslator.build_inline_tracerc              
   C   s  | j }| j}t }|jr| |j}z| |   W d    n1 s%w   Y  W nF tjyF } zd| d| }t	
|  d }~w tjyd } zd| d| }t	
| t||d }~w tyq   t	
d|  w | jd usyJ | j|ju r|j| j | j| jO  _t	
d| tjst| tr| jrt|rt| tr| jrt| tsJ tt|  d S | jS t|rt| tsJ | j d u sJ t| jt dS | jS )Nz!Observed exception DURING INLING z : zSKIPPED INLINING r   zFAILED INLINING %szDONE INLINING %sr  ) r  r  r  nullcontextr  rN  r  r   r  r   r  r,  r*   r  r  r  r  r  r  r   r  r   r  is_generator_from_ctx_managerr    generator_exhaustedZraise_observed_exceptionri  r@  r[   generated_itemsrF   )r   r  r  Z
strict_ctxrN  r7  rp   rp   rv   r  8  sf   



z*InliningInstructionTranslator.inline_call_r  r  r  r  r  funcvarr   Nc           
         s   |  }|d }t|ts|j}t }	t|	 t j|ji ||||||	 fddt	 D  |j
|jd |j|j|jd || _|| _|j| _d | _|j | _|j| _d S )N__builtins__c                    s   i | ]}|t  |qS rp   )ry  r  r  rp   rv   rw     r  z:InliningInstructionTranslator.__init__.<locals>.<dictcomp>r   )r   r  r  r  r  r  r  r   r  r  r  r  r   r  rO  )Zget_globalsr   r   __dict__r   r   r  r  r   r   r  r  r   r  rO  r  r  rV  r  rU  rh  rW  )
r   r  r  r  r  r  r  r  r  r   r  r  rv   r  q  s:   	
z&InliningInstructionTranslator.__init__c                 C   s   | j jS ry   )r  rK  r   rp   rp   rv   rK    s   z'InliningInstructionTranslator.fake_modec                 C   s   t | j S ry   )r   r  r  r*  r   rp   rp   rv   r    r  z)InliningInstructionTranslator.run_ctx_mgrc                 C   r9  r   rp   r   rp   rp   rv   r(    r  z:InliningInstructionTranslator.should_compile_partial_graphc                 C   s   t dddg d d S )NzGraph break in inlined functionr   z2Graph breaks in an inlined call are not supported.r  )r)   r  rp   rp   rv   r/    s   
z3InliningInstructionTranslator.create_call_resume_atc                 C   s   |   | _d | _try   )r.  r  r   r   r   rp   rp   rv   r    s   
z*InliningInstructionTranslator.RETURN_VALUEc                 C   s   |  || _d | _try   )r  r  r   r   r   rp   rp   rv   r    s   z*InliningInstructionTranslator.RETURN_CONSTc           	      C   s   d| j v r,| j d }| |}d|v rtjjj| }nt|}t| ||}t	||}n| j
d| j }t|}| j }t| ||}t||}|||fS )Nr   r+  Z___unnamed_scope)r  r!  r  r.  r/  r0  r   rG   r  r4   r   rE  r6   r5   )	r   r   r"  r#  r$  fglobals_vtglobal_sourceZglobals_nameZglobals_sourcerp   rp   rv   get_globals_source_and_value  s&   




z:InliningInstructionTranslator.get_globals_source_and_valuec                    s   | j j| ju rt | d S |j}| |\}}}| j j||r/| 	| j j
|| d S z| j| }W n tyD   | | Y S w | 	t| || d S ry   )r   r1  r  r  r   r   r  r  Zhas_pending_mutation_of_attrr   Z	load_attrr  r  rG   r  )r   r   r   r   r  r  r5  r  rp   rv   r     s   z*InliningInstructionTranslator._load_globalc                    sn   | j j| ju rt | d S |  }t|tr!td|j	dg d |j	}| 
|\}}}| j j||| d S )Nz3Storing Tensor hook handle in globals (inline call)r(  r  )r   r1  r  r  r*  r.  r   rk   r)   r   r  r  r  )r   r   r5  r   Z_fglobals_valuer  r   r  rp   rv   r*    s   
z*InliningInstructionTranslator.STORE_GLOBAL)!r   r   r   r   r   rG   r   r   r  r  r  r   r  r  r   rF  r  r   r   ri   rQ   r  r[  rK  r  r(  r/  r  r  r  r   r*  r  rp   rp   r  rv   r    sN   
 

:l9

'
r  c                       sj   e Zd ZU ee ed< d fddZdefddZd	d
 Z	 fddZ
 fddZdd Zdd Z  ZS )r  r  r   Nc                    s(   t  j|i | g | _d| _d| _d S r   )r  r  r  r  r  )r   r~   r  r  rp   rv   r    s   
z/InliningGeneratorInstructionTranslator.__init__r   c                 C   s\   |   }| j| t| jtkrtdt | t	
d  tjs'| jr,|| _td S )NzqToo many yield values in generator. Maybe you are inlining an infinite generator. If not, please report a bug at )r.  r  r   r   rX   r   ZInfiniteGeneratorErrorPT2_ISSUE_TRACKER_URLr   rK   r  r   r  r  r  r   )r   r   toprp   rp   rv   YIELD_VALUE  s    z2InliningGeneratorInstructionTranslator.YIELD_VALUEc                 C   sB   | j d }t|ts|   tt| |gi }| | d S d S r  )r  r   r[   r.  rJ   r  rq   r   )r   r   rc  resrp   rp   rv   GET_YIELD_FROM_ITER  s   

z:InliningGeneratorInstructionTranslator.GET_YIELD_FROM_ITERc                       d| _ t |S r:  )r  r  r  r   r  rp   rv   r       z3InliningGeneratorInstructionTranslator.RETURN_VALUEc                    r  r:  )r  r  r  r   r  rp   rv   r    r  z3InliningGeneratorInstructionTranslator.RETURN_CONSTc              
   C   s   t | jdks	J |  }| jd }t|tr|jd u s$tdddg d z|| }W n. tt	j
fyY } zt|t	j
rAt	|  |   | t|j W Y d }~d S d }~ww t| jtre| jdksgJ |  jd8  _| | | | d S )	Nr  r  Unreachable sub-generator coder   @Should only be encountered while implementing generator support.r  r   r   )r   r  r.  r   rK   r5  r)   rh  ri  r   rj  rI  r   r  r   r   r  r   r   r  rc  exrp   rp   rv   
YIELD_FROM  s2   

 


z1InliningGeneratorInstructionTranslator.YIELD_FROMc              
   C   s  t | jdks	J |  }| jd }t|ttfs&t|trvt|jtj	j
rvt|trl|jd u rlz|| }W n- ttjfyd } ztjdk rK|   | t|j | | W Y d }~d S d }~ww | | d S tdddg d d S tdd	t| d
t| dg d d S )Nr  r  r`  r  r   r  r  zSEND with bad typez
TOS type: z(Attempted to SEND with unsupported type r  )r   r  r.  r   r[   rS   ro   r5  r  r	  IteratorrK   rh  ri  r   rj  r1  r2  r   r  rA  r)   rE   r  rp   rp   rv   SENDB  s>   



z+InliningGeneratorInstructionTranslator.SENDr   )r   r   r   r   rG   r   r  r   r  r  r  r  r  r  r  rp   rp   r  rv   r    s   
 &r  )r}   r   )r   FN)r   r  collections.abcr  rh  r   rj  r   r   r   r  r  r   r   rA  r1  	threadingr
  rF  typingrF  r   r   r   r   r   r   Zunittest.mockr   r  Ztorch._loggingZtorch._dynamo.excr	   Ztorch._guardsr
   r   Z%torch.fx.experimental.symbolic_shapesr   Ztorch.utils._functoolsr   r   r   r   r   r   r   r   Zbytecode_analysisr   r   r   r   Zbytecode_transformationr   r   r   r   r   r   r   r    r!   r"   Zcodegenr#   r$   r%   r&   r'   r(   r)   r*   Zfuncname_cacher+   Zguardsr,   r-   Zoutput_graphr.   r/   Zreplay_recordr0   r1   rX  r2   r3   rE  r4   r5   r6   r7   r8   r9   r:   r;   r<   utilsr=   r>   r?   r@   rA   rB   rC   rD   Zvariables.baserE   rF   rG   Zvariables.builderrH   rI   Zvariables.builtinrJ   Zvariables.constantrK   Zvariables.ctx_managerrL   rM   rN   Zvariables.dictsrO   rP   Zvariables.functionsrQ   rR   rS   rT   rU   rV   rW   Zvariables.iterrX   Zvariables.lazyrY   Zvariables.listsrZ   r[   r\   r]   r^   Zvariables.miscr_   r`   ra   rb   rc   rd   Zvariables.nn_modulere   Zvariables.tensorrf   rg   rh   Zvariables.torch_functionri   rj   Zvariables.user_definedrk   rl   rm   rn   ro   	getLoggerr   r   r  ZgetArtifactLoggerr  r  r  r  localr  r   rx   r   r   r   containsrq   rz   r`  r   r  cacherG  r   	dataclassr   r   r   r   r   	lru_cacher   r  r   r   r   r  r   r  r   r   r)  r   r   r  r  r  rQ  rt  r  ru  r  r   r   r2  Z_nb_opsr  r  r  rp   rp   rp   rv   <module>   s    ,$	$	(
$	 
		C
	!
B
L p~
]                 M   #	  o