
    a,j                    
   U d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlm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$m%Z% ddl&m'Z'm(Z( dd	l)m*Z*m+Z+m,Z, dd
l-m.Z. ddl/m0Z0 ddl1m2Z2 ddl3m4Z4m5Z5m6Z6mZ7m8Z8m9Z9 ddl:m;Z;m<Z<m=Z=m>Z> ddl?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZN ddlOmOZO ddlPmQZQ ddlRmSZS ddl5mTZTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\m]Z] ddl^m_Z_ ddl`maZambZb ddlcmdZdmeZemfZf ddlgmhZhmiZimjZjmkZkmlZl ddlmmnZnmoZo ddlpmqZqmrZrmsZs ddltmuZumvZvmwZwmxZxmyZymzZzm{Z{m|Z| ddl8m}Z}m~Z~ ddlmZmZmZmZmZmZmZmZmZ ddlmZmZmZ ddlmZmZmZ dd lmZmZ dd!lmZ dd"lmZmZmZmZ dd#lmZ dd$lmZmZmZmZmZmZmZ dd%lmZ dd&lmZ dd'lmZmZmZmZmZmZ dd(lmZmZmZmZmZmZmZ dd)lmZmZ dd*lmZ dd+lmZ dd,lmZmZmZ dd-lmZmZ dd.lmZmZmZmZmZ erdd/lmZmZmZ dd0lmZ dd1lmZ  ej        eԦ          Ze!j                            ed2          Ze!j                            ed3          Ze!j                            ed4          Ze!j                            ed5          Z ej                    Zd6  ej                    D             Zd7ed8<    eej                  j        Z eej                  j        Zd9 ed:<   d; ed<<   e9j        ez  ez  Zd=ed><   e	j        ddC            Zej         G dD dE                      Zej         G dF dG                      Zej         G dH dI                      Zej         G dJ dK                      Z G dL dM          Ze	j        ddO            Zej        ddS            Zej        ddT            Zej         G dU dV                      Z G dW dXe          Z G dY dZe          Z G d[ d\e          Zdd`ZdddZe	j        ddg            ZddlZdmZddpZdddvZddxZ dd~ZddZ G d de          Zej         G d d                      Z G d dQe          Z G d de          Zej        dk    rd ej	        D             Z
ej        dd            Z G d de          Z G d de          ZdS )a  
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.
    )annotationsN)deque)AnycastNoReturnTYPE_CHECKING	TypeAliasTypeVar)TypeIs)DynamoProfilerStateFunctionTraceTiming)ObservedExceptionTensorifyScalarRestartAnalysis)InlinedCodeCachetracingTracingContext)	dump_file)
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_binary_slicecreate_call_functioncreate_call_function_excreate_copycreate_dup_topcreate_instructioncreate_jump_absolutecreate_rot_ncreate_swapget_code_keysInstructionis_generatoris_jump_absolute	unique_id)code_context)	PyCodegen)%maybe_setup_comprehension_speculation)
!augment_exc_message_with_hop_nameBackendCompilerFailedcollapse_resume_framesformat_frame_infoget_stack_above_dynamoResumePrologueTracingErrorStepUnsupportedunimplementedUnsupported	UserError)get_funcname)GuardBuilderinstall_guard)GraphCompileReasonOutputGraphStackLocalsMetadata)impl_CONTAINS_OP_fallbackimpl_IS_MAPPINGimpl_MATCH_CLASSimpl_MATCH_KEYSimpl_MATCH_SEQUENCE)DummyModuleExecutionRecorder)ContinueExecutionCache"IS_TRACING_RESUME_PROLOGUE_VARNAMEReenterWith)
AttrSourceDictGetItemSourceGlobalSourceGlobalWeakRefSourceLocalCellSourceLocalSourceSkipGuardSourceSource)is_builtin_constantis_forbidden)	_get_error_on_graph_break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VariableBuilderwrap_fx_proxy)BuiltinVariableDictBuiltinVariable)ConstantVariable)ContextWrappingVariableGenericContextWrappingVariableWithEnterFunctionVariableWithExitFunctionVariableConstDictVariable)BaseUserFunctionVariableLocalGeneratorFunctionVariableLocalGeneratorObjectVariableNestedUserFunctionVariableSkipFunctionVariableUserFunctionVariableUserMethodVariable)MAX_ITERATOR_LIMIT)LazyVariableTracker)BaseListVariableIteratorVariableListIteratorVariableListVariableSliceVariableTupleVariable)CellVariableExceptionVariableGetAttrVariableNullVariablePythonModuleVariableTracebackVariableUnknownVariable)NNModuleVariableUnspecializedNNModuleVariable)SetVariable)SymbolicStreamState)supported_comparison_opsSymNodeVariableTensorVariable)SymbolicTorchFunctionStateTorchFunctionModeVariable)RemovableHandleVariableUserDefinedClassVariable!UserDefinedExceptionClassVariable"UserDefinedExceptionObjectVariableUserDefinedObjectVariable)Callable	GeneratorSequence)FakeTensorMode)CompilePackagegraph_breaks
trace_calltrace_sourcetrace_bytecodec                >    i | ]\  }}|t          |          j        S  )rf   call_function.0kvs      ^/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/torch/_dynamo/symbolic_convert.py
<dictcomp>r      s6     ' ' ',0AqAq'' ' '    dict[str, Any]compare_op_handlersc                B    t          | g t          |          i           S N)handle_containsreversedtxargs_s      r   <lambda>r      s#    (4..21 1 r   inc           
     b    t          | t          | g t          |          i           gi           S r   )
handle_notr   r   r   s      r   r   r      s2    J.x~~.	3	34b5 5 r   znot inr	   ExceptionValsnamestrreturntypes.ModuleTypec                *    t          j        |           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   s    r   _import_moduler      s     "4(((r   c                  r    e Zd ZU ded<   ded<   ded<   ded<   dZd	ed
<   dZded<   dZded<   ddZddZdS )SpeculationEntryr   filenameintlinenoinstruction_pointerr,   instFbool_failedNzbool | Noneerror_on_graph_breakzGraphCompileReason | Nonereasonr   Nonec                t    d| _         || _        | j        | j        j        }nd}t          j        |          )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   r   SpeculationRestartAnalysis)selfr   r   s      r   fail_and_restart_analysisz*SpeculationEntry.fail_and_restart_analysis   s@     $8!;"![/NN@N,NKKKKr   r   InstructionTranslatorBasec                B    | j         r| j        J | j        |_        dS dS NTF)r   r   )r   r   s     r   failedzSpeculationEntry.failed	  s0    < 	,888&*&?B#4ur   )r   r   r   r   )r   r   r   r   )	__name__
__module____qualname____annotations__r   r   r   r   r   r   r   r   r   r      s         MMMKKKG(,,,,,(,F,,,,
L 
L 
L 
L     r   r   c                      e Zd ZU dZ ej        e          Zded<   dZ	ded<   dZ
d	ed
<    ej        e          Zded<   dZd	ed<   ddZddZ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_factoryzlist[SpeculationEntry]entriesr   r   indexFr   graph_break_on_autograd_grad	list[str]autograd_grad_leaked_tensorsgraph_break_on_requires_grad_r   r   c                    d| _         d S Nr   )r   r   s    r   restartzSpeculationLog.restart'  s    


r   c                F    | j                                          d| _        d S r   )r   clearr   r   s    r   r   zSpeculationLog.clear*  s!    


r   r   r   r   r   r   r,   r   c                z   t          | j                  | j        k    r*| j                            t	          ||||                     | j        | j                 }d}| j        dk    r=| j        | j        dz
           }d|j         d|j         d|j        j         d|j	         d	}|j	        |k    r|j        |k    r|j        |k    sft          d	| j         d
t          | j                   d|j         d|j         d|j        j         d|j	         d| d| d|j         d| d| d          | xj        dz  c_        |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 occurred:
- 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   entryprev_entry_msg
prev_entrys           r   nextzSpeculationLog.next.  s    t|
**L 63FMM   TZ(:??dj1n5JS)< S Sz?P S SO*S S/9/MS S S 
 %)<<<(**&&*"&* 8;DL8I8I ^ #l .3j.? HMHa      !%  5H  	    ( 	

a

r   Nr   r   )
r   r   r   r   r   r   r   r,   r   r   )r   r   r   __doc__dataclassesfieldlistr   r   r   r   r   r   r   r   r   r   r   r   r   r     s           '8k&7&M&M&MGMMMMENNNN */ .....?k.?PT.U.U.U UUUU +0!////      - - - - - -r   r   c                  D    e Zd ZU  ej        e          Zded<   ddZdS )	
LocalStater   zdict[str, FrameStateSizeEntry]automatic_dynamicr   r   c                n    d                     d | j                                        D                       S )N
c              3  N   K   | ] \  }}| d |                                  V  !dS ): N)renderr   s      r   	<genexpr>z$LocalState.render.<locals>.<genexpr>e  sM       
 
%)Qq  AHHJJ  
 
 
 
 
 
r   )joinr   itemsr   s    r   r   zLocalState.renderd  sC    yy 
 
-1-C-I-I-K-K
 
 
 
 
 	
r   Nr   r   )	r   r   r   r   r   dictr   r   r   r   r   r   r   r   ^  s[         8I8I9 9 9    
 
 
 
 
 
r   r   c                  2    e Zd ZU ded<   ded<   dZded<   dS )DistributedStater   
compile_pgr   local_stateNzlist[LocalState] | None
all_states)r   r   r   r   r	  r   r   r   r  r  k  s8         OOO*.J......r   r  c                      e Zd ZU  e            Zded<   edd            Zedd	            Zedd
            Z	edd            Z
dS )TensorifyStatezset[str]force_specializationsr   r   r   r   c                :    | j                             |           d S r   )r  addclsr   s     r   
specializezTensorifyState.specializex  s    !%%e,,,,,r   r   c                    || j         v S r   )r  r  s     r   should_specializez TensorifyState.should_specialize|  s    111r   c                8    | j                                          d S r   )r  r   r  s    r   r   zTensorifyState.clear  s    !'')))))r   c                2    t          | j                  dk    S r   )r   r  r  s    r   emptyzTensorifyState.empty  s    3,--22r   N)r   r   r   r   )r   r   r   r   r   r   r   )r   r   r   setr  r   classmethodr  r  r   r  r   r   r   r  r  r  s          '*cee++++- - - [- 2 2 2 [2 * * * [* 3 3 3 [3 3 3r   r  Callable[..., None]c                 4    t          j        t                    S r   )torchdynamo_loggingget_step_loggerlogr   r   r   _step_loggerr     s    .s333r   r   r   Generator[None, None, None]c              #     K   | j         j        }| j         j        }	 g | j         _        d| j         _        d V  || j         _        || j         _        d S # || j         _        || j         _        w xY wr   )speculation_logr   r   )r   r   r   s      r    save_and_restart_speculation_logr$    s        (G$E)%'"#$ %,"#(    &-"#( ((((s   A A,c              #  ~   K   	 | j         j        }d| j         _        d V  || j         _        d S # || j         _        w xY wNF)outputshould_exit)r   tmps     r   (temporarely_allow_writes_to_output_graphr*    sN      $i# %	 #		####s   . <c                  T    e Zd ZU ded<   ded<   ded<   dZded	<   ddZddZddZdS )BlockStackEntryr,   r   zInstruction | Nonetargetr   stack_indexNz?ContextWrappingVariable | GenericContextWrappingVariable | Nonewith_contextr   r   c                    | j         d uS r   )r/  r   s    r   can_restorezBlockStackEntry.can_restore  s     ,,r   rL   c                    | j         J | j        rPt          | j        d          r;| j        j        r/t	          | j         dz
  t          | j        j                            S t	          | j         dz
            S )Ntarget_valuesr   )r.  r/  hasattrr3  rL   tupler   s    r   	resume_fnzBlockStackEntry.resume_fn  s    +++		5)?;;		5 !/		5
  1$eD,=,K&L&L   t/!3444r   r   r   is_graph_breakVariableTracker | Nonec                    | j         J |r| j                                         s|s| j                             |          S d S r   )r/  exit_on_graph_breakexit)r   r   r7  s      r   r;  zBlockStackEntry.exit  sX      ,,,	.#0DDFF	.	. $))"---tr   r  )r   rL   )r   r   r7  r   r   r8  )r   r   r   r   r/  r1  r6  r;  r   r   r   r,  r,    s          TXLXXXX- - - -5 5 5 5     r   r,  c                      e Zd ZdS )r   Nr   r   r   r   r   r   r   r             Dr   r   c                      e Zd ZdS )ReturnValueOpNr=  r   r   r   r@  r@    r>  r   r@  c                      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   r   r   r   rB  rB    s           r   rB  fnCallable[..., object]Callable[..., Any]c                    t          t          j        |           j                  t	          |           t          j        |           dfd            }|S )	Nr   InstructionTranslatorr   r,   r   r   c                    |                                          | |                               i                      d S r   )pushr   popn)r   r   fn_varnargss     r   implzstack_op.<locals>.impl  s9    		&&&tTYYu-=-=rBBCCCCCr   )r   rG  r   r,   r   r   )r   inspect	signature
parametersrf   	functoolswraps)rC  rM  rK  rL  s     @@r   stack_oprS    sr    !"%%011ER  F_RD D D D D D D Kr   modobjectr   c                    t          | t          j                  sdS | j                            d          d         t
          j        v S )NF.r   )
isinstancetypes
ModuleTyper   splitsysstdlib_module_names)rT  s    r   	is_stdlibr^    s?    c5+,, u<c""1%)@@@r   with_msgr   c                    | rdd}ndd}d t          j        |          D             }t          d t          |          D                       }|                    d	          }||d
z   |d
z            S )Nxr   r   r   c                    | s
J d            d S )Nmsgr   ra  s    r   rC  z(get_assert_bytecode_sequence.<locals>.fn  s    OOeOO1OOr   c                    | sJ d S r   r   rd  s    r   rC  z(get_assert_bytecode_sequence.<locals>.fn  s    HH1HHr   c                    g | ]	}|j         
S r   r   r   r   s     r   
<listcomp>z0get_assert_bytecode_sequence.<locals>.<listcomp>  s    >>>TT[>>>r   c              3  J   K   | ]\  }}|                     d           |V  dS )POP_JUMPN)
startswith)r   ir   s      r   r   z/get_assert_bytecode_sequence.<locals>.<genexpr>  s7      VV71d$//*:U:UVQVVVVVVr   RAISE_VARARGSr   )ra  r   r   r   )disget_instructionsr   	enumerater   )r_  rC  insts	begin_idxend_idxs        r   get_assert_bytecode_sequenceru    s     	 	 	 	 		 	 	 	 ?>S%9"%=%=>>>E VVi&6&6VVVVVIkk/**GQ1,--r   r   truth_fnCallable[[object], bool]rI  c                   |t           j        us|rdS t          | j        t                    sJ | j        }dD ]}t          |          }| j        ||t          |          z            }d |D             }||k    rZ|r+|                    d          }| j        ||z            j	        }nd}| 
                    t          j        | |                      dS dS )NF)FTc                    g | ]	}|j         
S r   rg  rh  s     r   ri  z:_detect_and_normalize_assert_statement.<locals>.<listcomp>   s    777TT[777r   
LOAD_CONSTzassertion errorT)operatortruthrX  r   r   ru  instructionsr   r   argvalrI  rb   build)	r   rv  rI  current_instruction_pointerr_  assert_insts	cur_instsload_const_idx	error_msgs	            r   &_detect_and_normalize_assert_statementr    s    	&&4&ud.44444"&":!  3H==%'*E,+   
	 87Y777	$$ .!-!3!3L!A!A -/.@ 	 .	IIo+D)<<===44 % 5r   Fntorch.fx.Nodec                   | j                             dd          pt          | dd          }|sdS |                                                    d          }d}t          |          D ].\  }}|                                                    d          r|}/|dk     rdS ||                                         }d}|dz   t          |          k     r||dz                                            }| |rd| ndz   S )	zIExtract the innermost user source location from an FX node's stack trace.stack_tracer   r   zFile r   r   z, code: )metagetgetattrstripr[  rq  rl  r   )r  sttrace_lineslast_file_idxrm  line	file_linecodes           r   get_node_source_infor  P  s   	
M2	&	&	G'!]B*G*GB r((**""4((KM[))  4::<<""7++ 	MqrM*0022IDq3{++++=1,-3355>$>....B??r      valuerb   	max_linesr   c                  	 t          | t                    sdS 	 | j        j        }g g t	                      ddd	fd
	 	|           ssdS g }t          t                              z   D ],}|                    |           |                    d           -dd	                    d |D                       z   S # t          $ r  t                              dd           Y dS w xY w)zWalk the FX graph backwards from a TensorVariable to show how it was
    computed, with user source locations. Graph inputs (placeholders) are always
    shown first, followed by operations in dataflow order ending at the branch
    condition.r   arU  r   r   c                l    t          | t          j        j                  r| j        nt          |           S r   )rX  torchfxNoder   repr)r  s    r   fmt_argz0format_tensor_computation_trace.<locals>.fmt_args  s'    '58=99F166tAwwFr   r  r  r   c                &   | j         
v rd S 
                    | j                    t          |           }| j        dk    rXg }|r|                    d|            |                    | j          d| j         d           	                    |           d S | j        dk    rt                    k     rt          | j        dt          | j                            }d	                    fd| j
        D                       }g }|r|                    d|            |                    | j          d	| d
| d                               |           | j
        D ]0}t          |t          j        j                  r |           -d S d S d S )Nplaceholderz# z: graph input ()r   r   z, c              3  .   K   | ]} |          V  d S r   r   )r   r  r  s     r   r   z@format_tensor_computation_trace.<locals>.walk.<locals>.<genexpr>  s+      $@$@AWWQZZ$@$@$@$@$@$@r   z = r   )r   r  r  opr   r-  r   r  r   r  r   rX  r  r  r  )r  sourceblocktarget_nameargs_strr  r  r  	op_blocksplaceholder_blocksvisitedwalks         r   r  z-format_tensor_computation_trace.<locals>.walkv  s   v  KK)!,,Ft}$$ 0LLf///BBqxBBBCCC"))%000t&&3y>>I+E+E%ah
CMMJJ99$@$@$@$@$@$@$@@@ 0LLf///DD;DDDDDEEE  '''    A!!UX]33  Q '&+E+E   r   z@

  The branch condition involves a tensor computed as follows:
r   c              3      K   | ]	}d | V  
dS )    Nr   )r   r  s     r   r   z2format_tensor_computation_trace.<locals>.<genexpr>  s(      <<$t<<<<<<r   z&format_tensor_computation_trace failedT)exc_infoN)r  rU  r   r   )r  r  r   r   )rX  r   proxynoder  r   r   extendr   r  	Exceptionr  debug)
r  r  r  	all_linesr  r  r  r  r  r  s
    `   @@@@@r   format_tensor_computation_tracer  c  sx   
 e^,, r9{ &(	.0EE	G 	G 	G 	G	  	  	  	  	  	  	  	  	  	  	 6 	T


 	!3 	2  "	'$x	/B/B*C*CC 	! 	!EU###R     Rii<<)<<<<<=	
    		:T	JJJrrs   <C A/C &C76C78Callable[[InstructionTranslatorBase, Instruction], None]c                :     dd	 ddfdd fd}|S )Nr  rb   r   r   c                
   t          |           }g }t          | t                    r	 | j        j        j                            d          }|W|                                dk    r?|j        t          j
        t          j        t          j        fv r|                    d           n# t          $ r Y nw xY w|                    t           j                   |                    d           t%          dd|  d|z   |           d S )	Nexample_valuer   zThe branch condition uses a scalar integer tensor. Consider rewriting the computation to use plain Python ints (e.g. use int attributes instead of tensor buffers) so the condition becomes a shape guard instead of data-dependent branching.z1Use `torch.cond` to express dynamic control flow.zData-dependent branchingzattempted to jump with zyDetected data-dependent branching (e.g. `if my_tensor.sum() > 0:`). Dynamo does not support tracing dynamic control flow.gb_typecontextexplanationhints)r  rX  r   r  r  r  r  dimdtyper  r   int32int64r   r  r  r   FUNDAMENTALr:   )r  
trace_infor  examples       r   raise_jump_graph_breakz,generic_jump.<locals>.raise_jump_graph_break  s,   4U;;
e^,, 	+*/33ODD'**
  LL4      &2333HIII.5e55DFPQ	
 	
 	
 	
 	
 	
s   A=B& &
B32B3r   r   r   r   r,   	extra_msgr   r   c           	     2   |                                  sJ d }	  |           n # t          t          f$ r}|}Y d }~nd }~ww xY w|J |                                 r|                     | j        |           |                     | j        t          |          |           | 	                    |           t                              d           | j                            | t          dt          |           | |                                 g          d          }|                                  |                     | j        |          }
r| 	                    |           |j        J |                     |j        |          }t*          j        dk    r(| j                            t1          d          g           t1          |j        |d         	          }	|	                    |           | j                            |	g|z   |z              d S )
Nr   r   zgeneric_jump triggered compilezgeneric_jump r   r   
stack_pops      TO_BOOLr   )r-  )should_compile_partial_graphr;   r<   maybe_has_backedgeraise_loop_graph_breakf_codelog_graph_breakcode_optionsr   rI  r  r  r'  compile_subgraphr@   r`   frame_summarypopcreate_call_resume_atnext_instructionr-  r\  version_infoadd_output_instructionsr'   r   copy_positions)r   r   r  r  r   eall_stack_locals_metadataif_nextif_jump	jump_instrI  r  s             r   jump_graph_breakz&generic_jump.<locals>.jump_graph_break  sQ    0022222	""5))))Y' 	 	 	CCCCCC	  ""$$ 	:''S999s88 	 	
 	
 	
 			%		2333$(K$@$@%;;	;;d>P>P>R>R=S   %A %
 %
! 	


,,!%
 
  	IIe{&&&,,K%
 

 w&&K//1CI1N1N0OPPP&t{71:FFF	  &&&++YK',AG,KLLLLLs   ' A?Ac                   |                                  }t          j        rkt          j        j        dk    rUt          |           rC|                                  }|                                rt          |	                                          r| 
                    |          S |                                 r | ||           d S t          dd| dg t          j        ddd           |                                rE | j        j        d	t$          j        gt)          ||fi           R   | 
                    |           d S t+          |t,                    r|j        }t+          |t$          j                  s|dk    }t$          j        j        j                            |          }|s,t          d
t;          |          dg t          j                   | 
                    |           d S  | j        j        d	t$          j        gt)          |fi           R  }tA          | |tC          |j"        |                     } | j        j        d	t$          j        gt)          ||fi           R   | 
                    |           d S |                                rt+          |tF                    r8|j$        r1tK          |j$        &                    tN          j(                              |	                                          r.r| )                    |           | 
                    |           d S d S |                                r#|                                 r | ||           d S t+          |tT                    rZ| j        +                    |j,                  } |          r.r| )                    |           | 
                    |           d S d S t+          |tZ                    rU	 |.                    | d          }	nl# t^          j0        $ rZ t_          j1        |            	 |.                    | d          }	n+# t^          j0        $ r t_          j1        |            d }	Y nw xY wY nw xY wt+          |	td          tf          f          ry|	4                    | g i           }tk          tk          |	dd           dd           }
|                                r|	                                }|
dk    rGt+          |t                    s2t_          j6        tn          | dtq          |          j9         g           t+          |t          tt          f          r9 |          r0r| )                    |           | 
                    |           d S d S d S t+          |t,                    rD|;                                r.r| )                    |           | 
                    |           d S d S t          dd|	 d| dg            d S  d          r.r| )                    |           | 
                    |           d S d S |                                sp|<                    |           r[ t{          |>                    |                               r.r| )                    |           | 
                    |           d S d S t+          |t,                    r	 t+          |j        t$          j                  r|;                    | j                  }nt          |j        dk              }nC# t^          j@        $ r1}|                                 r | ||d|           cY d }~S  d }~ww xY w |          r.r| )                    |           | 
                    |           d S d S t+          |t          jB                  r; d          r.r| )                    |           | 
                    |           d S d S ddl$mC} |j$        ] ||j$                  rM |D                                          r.r| )                    |           | 
                    |           d S d S  |           d S )Nr   z>Data-dependent assertion failed (cannot compile partial graph)zvalue: zuDynamo has determined when encountering a data-dependent assert failure that it should not compile the partial graph.zUse `torch._assert()` to raise a hard AssertionError when the check fails. This error will propagate back the user code that called the compiled function (i.e. Dynamo will not trace any exception handling).zRemove the assert statement.zMove the assert statement outside of any context managers in order to graph break with partial graph compilation (if fullgraph=False).r  r   z#Assertion failed on symbolic shapesr   )r  __bool____len__rC  r   z&__bool__ should return bool, returned r   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   )r  r   )is_constant_source)Er  r    rewrite_assert_with_torch_assertr\  flagsoptimizer  is_python_constantr   as_python_constantjumpr  r:   r   r  	is_tensorr'  create_proxyr  _assert_asyncr_   rX  r   sym_numSymBoolr  experimentalsymbolic_shapesexpect_truer   
USER_ERRORscalar_tensorre   rY   r  rn   r  r?   
make_guardr>   SEQUENCE_LENGTHrI  r   get_submodule
module_keyr   var_getattrr   ObservedAttributeErrorhandle_observed_exceptionr   ru   r   r  raise_observed_exception	TypeErrortyper   r   evaluate_exprhas_unpack_var_sequencer   unpack_var_sequencer   r<   r   BackwardHookVariabler  get_real_value)r   r   r  r  sym_exprresultscalar_to_tensor_proxyscalar_to_tensorrT  ra  method_nameresult_valueeval_resultr  r  r  rI  r  rv  s                  r   innerzgeneric_jump.<locals>.inner  s	   !%3N		"a''6tXtLL ( *.I'')) 002233 99T??*6688 $$T4777F! ` 1% 1 1%H.:u
 ;N   *    ((#' 'y'92>>   
 		$%11  !=!(EM:: -'1}H.>JJ8TT ! E #H$&= 1 <=	    		$%=T[%=!4&7H%SU7V7V& & &"  -&,-C-H$OO      %DK$# #$4i#@"EE   
 IIdOOOF##%% j	. %!233 U Uel55l6RSSTTTx002233   %IIe$$$		$    __ `	.4#D#D#F#F `	.T4//////00 ^	.+++E,<==Cx}}   %IIe$$$		$    899 W	.	%%dJ77-   -d333))$	::AA1   1$777AAA !o/ABCC #$r266%gat&<&<j$OO,,.. #)#<#<#>#>L"j00LRV9W9W04%  flI[I[Id f f"    ",s<< (,AWAW ( - IIe,,,		$( ( ( (  88 ++-- ( - IIe,,,		$( (
 " U @1 @ @ @ @%X       8D>> $ )		%(((IIdOOOOO$ $ "" &	.u'D'DT'J'J &	.xE55d;;<<==   %IIe$$$		$    // !	. emU];; A"'"5"5dk"B"BKK",U]a-?"@"@K=   4466 S++D$aRRRRRRRRR x$$   %IIe$$$		$    y=>> 	.x~~   %IIe$$$		$   
 322222|',>,>u|,L,L'8E002233 $ )		%(((IIdOOOOO$ $
 '&u-----s[   9P #Q94Q
Q9%Q30Q92Q33Q98Q9'A\: :]:	%]5.]:4]55]:)r  rb   r   r   )r   )
r   r   r   r,   r  rb   r  r   r   r   r   r   r   r,   r   r   r   )rv  rI  r  r  r  s   `` @@r   generic_jumpr    s    !
 !
 !
 !
N 	7M 7M 7M 7M 7M 7M 7M 7Mr|. |. |. |. |. |. |. |. |.| Lr   cgr1   cleanuplist[Instruction]r   c                :   g }| }||                     |           |j        }|t          |          D ]} | j        D ]}t	          |j        t                    rA|                    |                                	                    |j
        |                     ]|j        J t	          |j        t                    sJ |j                            |           |                    |                                                    |j
        |                     ݌dS )zsGenerates bytecode to restore the block stack for running the unsupported instruction
    in the compiled bytecode.N)r   parentr   block_stackrX  r/  r   extend_outputr6  try_except_torch_function_moder  ri   reconstruct_typetry_finally)r   r  r  all_txescur_txbs         r   _reconstruct_block_stackr!    s9    13H/1F

 
 x   R R 	R 	RA !.*CDD   KKMM@@   
 >---an/FHHHHHN++B///Q[[]]66rPPQQQQ	RR Rr   
msg_prefixYCallable[[Callable[..., None]], Callable[[InstructionTranslatorBase, Instruction], None]]c                     d fd}|S )Ninner_fnr  r   r  c                ^     t          j                   d fd            }dfd
|S )Nr   r   r   r,   r   r   c           
     l   | j         }| _         |                                 }|                    |           r|j        J  | ||j                  S 	  | |          || _         S # t          t
          f$ r}| j        ra| j        s| j        r t          |t                    r|
                                 t          dd| j         ddgt          j        |           t          |dd          r |                                 s |                                 r|                     | j        |           |                     | j         dt+          |           |	           t          |t                    r)|
                                 |                    d
           t/          t          |dt+          |                    t          |d|                                 g                    |_        Y d }~nd }~ww xY w	 || _         n# || _         w xY w|                    | j                   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  from_exc
skip_frameFz:

r  graph_breakrc  
real_stack)current_instruction_push	speculater   r   r;   r<   active_generic_context_managers	one_graphr   rX  remove_from_statsr:   r   CAUSED_BY_EARLIER_GRAPH_BREAKr  r  r  r  r  r  r  r   add_to_statsr@   r  r   )	r   r   	prev_pushspeculationexcphandle_graph_breakr%  r"  rI  s	        r   wrapperz>break_graph_if_unsupported.<locals>.decorator.<locals>.wrapper  s   5I,0D)..**K!!$'' J")555))$k6HIII.:xd++Z 1:--Y  + * * *7 ~ )B  "$44 1..000! R jDDh j j %Dc.L "&	 	 	 	 4u55 88:: **,, C//TBBB$$%(::s4yy:: %    dK00 5**,,,%%m444%7D%T33D,1C1C1E1E0FGG& &""""""O*N #
 1:--	-999911$2KLLLLLs+   A( (H:E>G=8H =HH 	Hr   r@   c                   t           j        dk    rqt           j        dk     ra|j        dk    rVt          j        t          j        d         |j                  t          j        t          j        d         |j                  z   }nt          j        |j        |j                  }t          	                    d|j                   | j
                            | |t          
          |z
            }t          | j
        j                  }g }t          | ||           | j
                            |                                           ~t           j        dk    r|j        dk    r| j        | j                                        nd}t)          |          dk    r<t           j        d	k     sJ | j
                            t+          d
|          g           |j        J t-          |j        d          }|d                             |           | j
                            |           n?|j        J t3          j        |          }	d |	_        | j
                            |	g           | j
                            |           |                     t          
          |z
             
r!|                     t;                                 | j
                            |                     | j        |                     d S )Nr     r     CALLPRECALLz%s triggered compiler  r   r   r  KW_NAMESr~  Fr  ) r\  r  r   ro  stack_effectopmapargopcoder  r  r'  r  r   r1   root_txr!  r  rp  kw_namesr  r   r'   r#   r  r-  copyexn_tab_entryrJ  rI  r   r  r  )r   r   r   rB  r  r  r  rG  
call_insts	inst_copyrI  s             r   r7  zIbreak_graph_if_unsupported.<locals>.decorator.<locals>.handle_graph_break)  s     G++$w..K6))  #/Ii($(   $SYv%6AA B  #/TXFFII,dk:::(,(D(DVD		L0H )E ) )% 4;.//B)+G$T2w777K//0C0C0E0EFFF7**t{f/D/D }0 M44666 
 x==1$$+g5555K77+JxHHHI   x+++1$(EBB
2--d33333J???? {*** IdOO	*.	'33YK@@@K//888IIc$ii,./// -		/++,,,K//**)-     r   r  )r   r   r   r,   r   r@   r   r   )rQ  rR  )r%  r8  r7  r"  rI  s   ` @r   	decoratorz-break_graph_if_unsupported.<locals>.decorator  sy     
	"	"7	M 7	M 7	M 7	M 7	M 7	M 7	M 7	M 
#	"7	Mr<	 <	 <	 <	 <	 <	| r   )r%  r  r   r  r   )rI  r"  rL  s   `` r   break_graph_if_unsupportedrM    s7    
{ { { { { { {z r   c                  $     e Zd ZdZd fdZ xZS )BytecodeDispatchTableMetazTInstalls a `cls.dispatch_table` on every subclass to speed up calls to self.OPCODE()r  r  r   r   basesr   dctr   r   c                     t                                          |||           dd fdt          j                                        D             fd	t          d
          D              _        d S )Nr   r   r   r   r   r   c                `    t          d|  d| d|  dd|  dgt          j                   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   s     r   _missingz4BytecodeDispatchTableMeta.__init__.<locals>._missingr  sr    2!44d44f\bfff_F _ _ _ '2		 	 	 	 	 	r   c                `    i | ]*\  }}|t          |t          j        |                    +S r   )r  rQ  partial)r   r   r  rV  r  s      r   r   z6BytecodeDispatchTableMeta.__init__.<locals>.<dictcomp>~  sH     
 
 
 VY%6x%H%HII
 
 
r   c                :    g | ]}                     |          S r   )r  )r   rm  dispatch_tables     r   ri  z6BytecodeDispatchTableMeta.__init__.<locals>.<listcomp>  s'    IIIn0033IIIr      )r   r   r   r   r   r   )super__init__ro  rC  r  rangerZ  )r  r   rP  rQ  rV  rZ  	__class__s   `   @@r   r]  z"BytecodeDispatchTableMeta.__init__o  s    uc***
	 
	 
	 
	
 
 
 
 
!ioo//
 
 
 JIIIU4[[IIIr   )
r  r  r   r   rP  r   rQ  r   r   r   )r   r   r   r   r]  __classcell__r_  s   @r   rO  rO  l  sM        ^^J J J J J J J J J Jr   rO  c                      e Zd ZU dZ ej        e          Zded<    ej        d          Z	d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   zlist[ExceptionVals]
_exc_stackN)defaultzExceptionVals | None_current_exceptionr   r   c                    d | _         d S r   rf  r   s    r   clear_current_exceptionz&ExceptionStack.clear_current_exception  s    "&r   valr   c                >    |                      |           || _        d S r   )._set_context_and_break_context_reference_cyclerf  r   rj  s     r   set_current_exceptionz$ExceptionStack.set_current_exception  s$    ;;C@@@"%r   c                t    | j         J |                     | j                    |                                  d S r   )rf  r   ri  r   s    r   move_current_exception_to_stackz.ExceptionStack.move_current_exception_to_stack  s>    &222D+,,,$$&&&&&r   c                "    | j         J | j         S r   rh  r   s    r   get_current_exceptionz$ExceptionStack.get_current_exception  s    &222&&r   prev_idxr   c                    |j         x}rt          |          t          ur|S t          | j                  |z   dk    r?| j        |         }|                     ||dz
             ||ur|                    |           |S Nr   r   )__context__r  rh   r   rd  _set_context_recursiveset_context)r   rj  rs  ctxprevs        r   rw  z%ExceptionStack._set_context_recursive  s     ?"C 	S		9I(I(IJt(*Q..?8,D''hl;;;3%%%
r   c                    |x}}d}	 |j         }t          |          t          u rd S ||u r)|                    t          j        d                      d S |}||u rd S |r|j         }| }ar&  )rv  r  rh   rx  create)r   rj  oslow_oslow_update_toggler  s         r   _break_context_reference_cyclez-ExceptionStack._break_context_reference_cycle  s    
 F"	8mGG}} 000#~~.5d;;<<<AF{{ ! ,+%7!7%	8r   c                    |                      |t          | j                  dz
             |                     |           d S Nr   )rw  r   rd  r  rm  s     r   rl  z=ExceptionStack._set_context_and_break_context_reference_cycle  sC     	##CT_)=)=)ABBB++C00000r   c                4    | j                                         S r   )rd  r  r   s    r   r  zExceptionStack.pop  s    ""$$$r   c                :    | j                             |           d S r   )rd  r   rm  s     r   r   zExceptionStack.append  s    s#####r   c                *    t          | j                  S r   )r   rd  r   s    r   r  zExceptionStack.__len__  s    4?###r   r   c                    | j         |         S r   )rd  )r   r   s     r   __getitem__zExceptionStack.__getitem__  s    u%%r   r   c                &    d| j         d| j        S )Nzself._exc_stack=z - self._current_exception=)rd  rf  r   s    r   __str__zExceptionStack.__str__  s    A$/AAt'>AAAr   r   )rj  r   r   r   )r   r   )rj  r   rs  r   r   r   )r   r   )r   r   r   r   r  )r   r   r   r   r   r   r   rd  r   rf  ri  rn  rp  rr  rw  r  rl  r  r   r  r  r  __repr__r   r   r   rc  rc    sZ          '8k&7&M&M&MJMMMM/@{/@/N/N/NNNNN' ' ' '& & & &' ' ' '
' ' ' '
 
 
 
8 8 8 861 1 1 1% % % %$ $ $ $$ $ $ $& & & &B B B B HHHr   rc  c                  T    e Zd ZU ded<   ded<   ded<   ded<   ded	<   d
ed<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded <   d!ed"<   d#ed$<   d%ed&<   d'ed(<   ded)<   ded*<   d+ed,<   ded-<   d.ed/<   d0ed1<   d2ed3<   dNd6ZdOd7ZdPd9ZdPd:ZdPd;ZdNd<Z	dQdAZ
dRdDZdSdFZdTdUdIZdVdJZdWdKZdOdLZej        dMk    rdXdOZndXdPZedYdQ            ZdZdSZd[dTZdNdUZd\dWZd]dYZd^dZZd_d\ZdXd]ZdXd^ZdXd_ZdXd`Z d`dcZ!dXddZ"eZ#dadeZ$dXdfZ%dXdgZ&e'j(        d^dh            Z)dXdiZ*dXdjZ+e,dbdm            Z-dcdpZ.dVdqZ/dXdrZ0e0Z1dXdsZ2e,dddu            Z3dXdvZ4dedxZ5e5Z6e5Z7 e8e9j:        dy          Z; e8e9j<        dy          Z= e8e9j:        dz          Z> e8e9j<        dz          Z?dXd{Z@dXd|ZAdXd}ZBdXd~ZCdXdZDdXdZEdfdZFdgdZGdhdZHdXdZIdXdZJdXdZKdidZLdXdZMdjdZNdXdZOdXdZPdOdZQdXdZRdXdZSdXdZTdXdZU eVdzd          dXd            ZW eVdzd          dXd            ZX eVdzd          dXd            ZYdXdZZdXdZ[dXdZ\dXdZ]dkdZ^dXdZ_ eVdyd          dXd            Z`dXdZadldZbecdmd            ZddndZedodZfecdpd            ZgdOdZh eVdyd          dXd            ZidXdZjdqdZkdXdÄZldXdĄZmdOdńZndXdƄZodXdǄZpeqfdrdʄZrdXd˄ZsesZtdXd̄ZudXd̈́ZvevZwdXd΄ZxdXdτZydXdЄZzdXdфZ{dXd҄Z|dXdӄZ}dXdԄZ~dXdՄZ eVdyd֬          dXdׄ            ZdXd؄ZdXdلZdXdڄZdXdۄZdXd܄ZdXd݄ZdXdބZdsdZdtdZdXdZdXdZdXdZdXdZdXdZdXdZdXdZdXdZeZdXdZdXdZdXdZdXdZdXdZdXdZdXdZdXdZ ee9j                  Z ee9j                  Z ee9j:                  Z ee9j                  Z ee9j                  Z ee9j                  Z ee9j                  Z ee9j                  Z ee9j                  Z ee9j                  Z ee9j                  Z ee9j                  Z ee9j                  Z  eVdzd           ee9j                            Z ee9j                  Z ee9j                  Z ee9j                  Z ee9j                  Z ee9j                  Z ee9j                  Z ee9j                  Z ee9j                  Z ee9j                  Z ee9j                  Z ee9j                  Z ee9j                  Z ee9j                  Z ee9j                  Z ee9j                  Z ee9j                  Z ee9j                  Z ee9j                  Z ee9j                  ZڐdXdZej        dMk    rdXdZܐdXdZݐdXdZސdXdZߐdudvdZ eVdzd          dXd            ZdXdZdXd Ze5Ze5Z e8e9j<        dy          Z e8e9j<        dy          Z e8e9j:        dy          Z e8e9j:        dy          ZdXdZdXdZdwdZecdxd            ZdXdZdXdZdXd	ZdXd
ZdXdZdXdZdXdZdXdZdXdZdXdZdXdZdXdZ eVdzd          dXd            ZdXdZdXdZdXdZdXdZdXdZeZ eZeZdZdXdZdXdZdZdXdZdOdZ	 dTdyd"Z	dzd$Z
dOd%Zd{d'Zed|d)            Zej        d}d,            Zd~d.Zdd2Zdd5Zdd:Zecdd>            Z	 	 	 dd fdMZ xZS (  r   rA   r'  dict[str, VariableTracker]symbolic_localssymbolic_globalsr   symbolic_torch_function_stater   symbolic_stream_statez!dict[str, VariableTracker] | Nonepost_prune_cell_and_freevarslist[VariableTracker]stack
int | Noner   r,   current_instructionr   r-  zlist[BlockStackEntry]r  r   r   zConstantVariable | NonerG  accept_prefix_instr  prefix_instsinline_depthinconsistent_side_effectszSpeculationEntry | Nonecurrent_speculationz	list[Any]rZ  rc  exn_vt_stackzExecutionRecorder | Noneexec_recorderz(Callable[[VariableTracker], bool] | Nonestrict_checks_fnstart_pointhas_no_inlined_callsz InstructionTranslatorBase | Noner  is_child_tracer_activez3list[tuple[VariableTracker, list[VariableTracker]]]debug_localsCompilePackage | Nonepackagez
deque[str]latest_bytecode_queuer   r   c                    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   s    r   mark_inconsistent_side_effectsz8InstructionTranslatorBase.mark_inconsistent_side_effects  s     *.&&&r   c                    | }|^|j         j        }|j        J |j        |j        d          D ]+}|j        dv r n|j        t
          v r|j        }||k     r dS ,|j        }|^dS )N)RETURN_VALUERETURN_CONSTTF)r  offsetr   r}  r   r   r~  r  )r   r  
cur_offsetr   jump_offsets        r   r  z,InstructionTranslatorBase.maybe_has_backedge  s    * 48 3:J-999+F,F,H,HI $ $;"BBBE;,.."&+K"Z//#tt]F   ur   r   c                    | j         d         S )Nco_cellvarsr  r   s    r   cellvarsz"InstructionTranslatorBase.cellvars9       //r   c                    | j         d         S )Nco_freevarsr  r   s    r   freevarsz"InstructionTranslatorBase.freevars<  r  r   c                    t          | d          s.|                                 |                                 z   | _        | j        S )N_cell_and_freevars)r4  r  r  r  r   s    r   cell_and_freevarsz+InstructionTranslatorBase.cell_and_freevars?  s<    t122 	H&*mmoo&GD#&&r   c                      fd j                                         D              _        t           j         j                  fd j                                         D              _         d S )Nc                H    i | ]\  }}|                                 v ||S r   r  )r   r   r   r   s      r   r   z?InstructionTranslatorBase.prune_dead_locals.<locals>.<dictcomp>F  s@     -
 -
 -
1D**,,,, q,,,r   c                $    i | ]\  }}|v 	||S r   r   )r   r   r   readss      r   r   z?InstructionTranslatorBase.prune_dead_locals.<locals>.<dictcomp>M  s+      
  
  
QQ%ZZAqZZZr   )r  r  r  r   r}  r  )r   r  s   `@r   prune_dead_localsz+InstructionTranslatorBase.prune_dead_localsD  s    -
 -
 -
 -
,2244-
 -
 -
) "$"3T5MNN 
  
  
  
!17799 
  
  
r   rC  rb   r   kwargsc                (   t          |t                    sJ t          |t                    sJ t          |t                    sJ t	          d t          j        ||                                          D                       sJ d }t          |d          r|j	        }t          |d          r|j
        }|r0t          |          r!t          |          rt          d|           |                     |                    | ||                     d S )Nc              3  @   K   | ]}t          |t                    V  d S r   )rX  rb   )r   ra  s     r   r   z:InstructionTranslatorBase.call_function.<locals>.<genexpr>Z  s@       
 
 q/**
 
 
 
 
 
r   r  rC  z$Attempt to trace forbidden callable )rX  rb   r   r  all	itertoolschainvaluesr4  r  rC  callablerV   AssertionErrorrI  r   )r   rC  r   r  r%  s        r   r   z'InstructionTranslatorBase.call_functionQ  s5    "o.....$%%%%%&$''''' 
 
_T6==??;;
 
 
 
 
 	
 	
 
 2w 	 xH2t 	uH 	T** 	T|H/E/E 	T !R!R!RSSS		"""4v6677777r   ro   Sequence[VariableTracker]c                x    t          |t                    st          |          }|                    | ||          S )zD
        Redirect the call to the generator "call_function"
        )rX  rp   r   r   rC  r   r  s       r   inline_generator_functionz3InstructionTranslatorBase.inline_generator_functiong  s<     "<== 	4/33BdF333r   r   c                    t           j        r8t          |                                          r|                     |||          S t
                              | |||          S )zF
        A call to some user defined function by inlining it.
        )r   "enable_faithful_generator_behaviorr-   get_coder  InliningInstructionTranslatorinline_callr  s       r   inline_user_function_returnz5InstructionTranslatorBase.inline_user_function_returnt  sZ     4 	Ubkkmm9T9T 	U11"dFCCC0<<T2tVTTTr   Nr   c                    || j         }| j        dk    rd| j         dnd}t          | j        j        |          }|dnd| d}| j        j         d| d| j        j         | | S )Nr   z (inline depth: r  r   r   r   z in )r   r  r=   r  co_filenameco_name)r   r   inline_depth_strfuncnamefuncname_strs        r   get_line_of_code_headerz1InstructionTranslatorBase.get_line_of_code_header  s    >[F7;7H17L7L3t03333RT 	   7@@%-rr3C3C3C3C+)mmFmm8Km\m[kmmmr   c                    d|                                   d}t          j        | j        j        | j                                                  }|d| z  }|S )NzTRACE starts_line r   r  )r  	linecachegetliner  r  r   rstrip)r   log_strr  s      r   get_log_starts_line_log_strz5InstructionTranslatorBase.get_log_starts_line_log_str  sX    It'C'C'E'EIII !8$+FFMMOO=$== r   c                    | j         |k    rd S || _         t          j        | j        j        || j        j                   | j        r/t                              dt          | j
                             d S d S )N%s)r   r   set_current_locr  r  r  is_trace_source_log_enabledtrace_source_logr  r^   r  )r   r   s     r   starts_linez%InstructionTranslatorBase.starts_line  s    ;&  F&K#VT[-@	
 	
 	
 + 	W""4D4T)U)UVVVVV	W 	Wr   c                ~   t                      | _        | j        }|dS | j        |         x| _        }|dz   | _        |j        r|                     |j                   | j        sr|                                 r^|                                 rJ| 	                                | _
        | j
                            |           r|                     |           dS | j        r9t                              d|j        |j        t%          | j                             t&          j        rb	 t%          | j                  }n# t*          $ r d}Y nw xY w| j                            d|j         dt%          |j                   d|            |                     |           	  | j        |j                 | |           | j        j         S # t:          $ r  t<          j        $ r }|                      |           Y d}~dS d}~wtB          tD          f$ r Y dS tF          tH          tJ          f$ r}| j&        s| j        s| j'        stQ          |d	d          r7tS          |tJ                    r!tU          d
ddddgtV          j,                    | j
        YtZ                              d           tS          |tJ                    r"tU          dddddgtV          j,        d           d|_.         dt_          |           }| 0                    | j1        ||           Y d}~nd}~ww xY w| j
        2                    | j                   dS )z<Process exactly one instruction, return False we should exitNFr   zTRACE %s %s %sz0<self.stack repr truncated due to large integer>zTRACE  Tr*  z3cannot resume from torch._dynamo.step_unsupported()r   ztraced torch._dynamo.step_unsupported(), but Dynamo is instructed to error on graph break. This graph break is used for debugging only.z1Remove the torch._dynamo.step_unsupported() call.z9Make sure fullgraph=False and error_on_graph_break=False.r  z1empty checkpoint - cannot resume from graph breakz6torch._dynamo.step_unsupported() with empty checkpointztraced torch._dynamo.step_unsupported(), but there is no checkpoint to step_graph_break from. This graph break is used for debugging only.zInclude at least one checkpoint: (1) include at least 2 ops and (2) make sure there is some line of code that is not in a try/with block, and has an empty Python stack.)r  r  r  r  r*  zEncountered graph break that we cannot resume from. Compiling up to the previous resumable state, then skipping the rest of the function. Graph break encountered:

r  )3rW   r   r   r}  r  r  r  r  is_non_empty_graphr.  r  r   step_graph_breakis_trace_bytecode_log_enabledtrace_bytecode_logr  r   r~  r  r   verbose
ValueErrorr  r   update_block_stackrZ  rE  r'  r(  r   r   r   exception_handlerr@  rB  r;   r<   r9   r0  is_tracing_resume_prologuer  rX  r:   r   
DYNAMO_BUGr  r*  r   r  r  r   )r   ipr   
stack_reprr  r   s         r   stepzInstructionTranslatorBase.step  s   $=$?$?!%:5*.*;B*?? 4#%6  	/T-... 
	1133	 ''))	
 (,~~'7'7D$'..t44 %%d+++u- 	$$ $+t{D<L<L   > 	P!$*--

 P P PO


P &--GGGtDK'8'8GG:GG   	%%%A	,D,T4888{...- 	 	 	$ 	 	 	""1%%%44444|, 	 	 	55Y8 7	 7	 7	 , 2 1lE22	 a11 ! U "%` PW /9
 
 
 
 '/		MNNNa11 ! X "%a Pk /9	 $(     $8 031vv8 8    ! !        g7	r 	 ::4;TUUUus=   $D9 9EE(G LG88LL%C,LLr:  r   c                |   |j         }|rkt          | j                  dk    rM|j        | j        d         j        ur6|j        | j        d         j        u r| j                                         d S d S d S d S | j        r>|j        dvr7t          | j                  dk    sJ | j                                         d S d S d S )N   r  )NOPJUMP_BACKWARD	NOT_TAKENr   )rI  r   r  r-  r  r   )r   r   r   s      r   r  z,InstructionTranslatorBase.update_block_stack  s     &E  + ())Q..D,<R,@,GGG(8(<(CCC $((***** /.GGCC # 	+ < ) ) t/00A5555$((*****	+ 	+ ) )r   c                    d S r   r   r   r   s     r   r  z,InstructionTranslatorBase.update_block_stack=  s    Dr   c                8    | j         J | j        | j                  S r   )r   r}  r   s    r   r  z*InstructionTranslatorBase.next_instruction@  s"    '333 !9::r   continue_instc           	     n   | j         j        rJ | j        J | j        rJ t                              d           | j                             | t          d|                                 g                    }| j	        rddl
m} t          j        sJ t          | j         j                  }|                    g t#          d          |                    d          |                                t)          dd	          t#          d          |                    d          |                                t)          dd	                     |                     d||d         g |d
d          \  }} ||           g }t-          | j	        ||           |                     |g|g|           |                    |           |                    g t#          d          |                    d          t)          d          t#          d          |                    d          t)          d                     |d         j        }|                    g t#          d          |                    d          |                                t3          d|                     |                    g t5          d          t)          dd	                     | j	                            t9                                 |d         xj        dz  c_        |dk    r|                    g t#          d          |                    d          |                                t3          |d           t#          d          |                    d          t)          d                     |                    t)          d|dz   	          t)          d|dz   	          t)          d|dz   	          g           | j                             |                                | j	                            | j	        j         |dd                    z              d S | j                             g t5          d          t)          d                     t          | j         j                  }| j                             |                    d          |                                g           |d         j!        "                                D ]g\  }	}
| j                             tG                      |                    |
          |                                |$                    |	          g           h| j                             t)          d          tK          |          g| j&                   d S )Nzstep triggered compilestep_unsupported)r   r   )	skip_coder  r   
BUILD_LISTrD  TFDELETE_SUBSCRr  LIST_APPENDSTORE_SUBSCRUNPACK_SEQUENCEPOP_TOPr  )'r'  output_instructionsr  r  r  r  r  r@   r  r  
eval_framer  r   nested_graph_breaksr1   rF  r  r%   create_load_constcreate_binary_subscrr'   create_resumer!  codegen_call_resume	num_stackr"   r*   rI  r   r  rp  r  r  locals_namesr  r&   create_storer(   r}  )r   r  r  r  r  leaf_resume_codeleaf_resume_namer  r  localidxs              r   r  z*InstructionTranslatorBase.step_graph_breakE  sw   ;2222'333 :~ 			*+++$(K$@$@%&84;M;M;O;O:PQQ %A %
 %
! ; i	------ ----4;.//B 	 ^^	((++	 ++--	 '|;;;		
 !^^	 ((++	 ++--	 '|;;;	   261C1C=";A">Be2 2.. I&''')+G$T["g>>> $$&6%7:J9KRPPPW%%%  ^^((++ '77 !^^	
 ((++ '77	 	 	 2!4>I ^^((++ ++-- )I66	    ^^&}!<<<   K_..///%a(22a722
 1}}  $Q,,Q// //11 -Y==	
 %Q ,,Q// +>::    &'8i!mLLL&|QGGG&'8i!mLLL  " K//##%%+33K02KABB2O      K// ^^&y11   4;.//BK//((,,++--   8;HNNPP  
s33&((,,S11//11..	    K//&y11(77 &    r   c                *    t          j        d           S r   )r   current_framer   s    r   run_ctx_mgrz%InstructionTranslatorBase.run_ctx_mgr  s     +D111r   c           
        |                                  5  t          | j        j                   	 | j                            |            | j        | _        	 |                                 r	 |                                 ng# t          $ rZ}| j
        rMt          dt          |          j         dt          |                                         |j                  d  d }~ww xY wn# t"          $ r  t$          $ r  t&          $ r}t)          |t*          t,          f          rRt)          | t.                    r=| j        s6| j        s/dt          |           }|                     | j        ||           t)          |t8          j                  s| j        j                                         tA          |d          r[d|j!        v rRtD          j#        $                    | j        j%        | j        j&                  '                    ddd	          }||_(          d }~wt          $ rc}| j)        r| j)        *                                |_+        t)          |t8          j                  s| j        j                                          d }~ww xY w| j        ,                                 t)          | t.                    r2| j        -                                 | j        .                                 nf# | j        ,                                 t)          | t.                    r3| j        -                                 | j        .                                 w w xY wd d d            d S # 1 swxY w Y   d S )
NzError while tracing through a Dynamo-generated resume function prologue. Errors are not allowed when tracing resume function prologues.
r   ztFailed to handle graph break gracefully. Skipping the function and falling back to eager. Graph break encountered:

r  rc  zData-dependentFT)print_outputinclude_strideinclude_device)/r  r   r  r  r'  push_txr   r  r  r  r  r8   r  r   r   with_traceback__traceback__r   r4   RuntimeErrorrX  r;   r<   rG  r   r0  r  r  r   RestartAnalysisside_effectslog_side_effects_summaryr4  rc  r  r  GraphModule
nn_modulesgraphprint_readablepartial_fx_graphr  
get_recordexec_recordpop_txr  mark_bytecode_tracing_stop)r   r  r   readable_graphs       r   runzInstructionTranslatorBase.run  s    L	= L	=dk-...J=##D)))#'#; 
))++  ))++     6 D8@#Aww3@ @7:1vv@ @  ).99t	D
  2   (    # # # q;	":;;"4)>?? !5 !N	@7:1vv@ @ 
 (()% )    "!S%899 HK,EEGGG1e$$ )9QU)B)B%*X%9%9.0A& &$n%*4PT %   #
 *8A&   % D$($6$A$A$C$CAM!!S%899 HK,EEGGG ""$$$ d$9:: =K'')))
 K::<<< ""$$$ d$9:: =K'')))
 K::<<<<=ML	= L	= L	= L	= L	= L	= L	= L	= L	= L	= L	= L	= L	= L	= L	= L	= L	= L	=s{   M"&C*)B C*
C%AC  C%%C*(K/*J	DHJ(AJJK/A!M"/A#MM""M&)M&rj  c                    t          |t                    sJ dt          |                       | j                            |           d S )Nz"push expects VariableTracker, got )rX  rb   r`   r  r   rm  s     r   rI  zInstructionTranslatorBase.push]  sU    #// 	
 	
???	
 	
/ 	
#r   valsc                :    |D ]}|                      |           d S r   )rI  )r   r/  rj  s      r   	push_manyz#InstructionTranslatorBase.push_manyc  s,     	 	CIIcNNNN	 	r   c                4    | j                                         S r   )r  r  r   s    r   r  zInstructionTranslatorBase.popg  s    z~~r   r  c                X     g t           fdt          |          D                       S )Nc                8    g | ]}                                 S r   r  )r   r   r   s     r   ri  z2InstructionTranslatorBase.popn.<locals>.<listcomp>k  s!    888!488::888r   )r   r^  )r   r  s   ` r   rJ  zInstructionTranslatorBase.popnj  s1    :8888uQxx88899::r   c           	        |j         }| j        r/|| j        v r&| j                            || j        |                    	 |                     | j        |                                                    n# t          $ r |                    d          ro	 |	                    dd          }|                     | j        |                    n]# t          $ r* t          dd| d| ddgt          j                   Y n*w xY wt          d	d| d
| dg t          j                   Y nw xY w|                    d          r| j                            |           d S d S )NrW  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 `__stack)r~  r  f_localsadd_local_varrI  r  unwrapKeyErrorrl  replacer:   r   r  r  )r   r   r   new_names       r   	LOAD_FASTz#InstructionTranslatorBase.LOAD_FASTm  s   { 	H$$-"7"7,,T4=3FGGG	IId*407799:::: 	 	 	s## #||C<<HIId28<==== 	 	 	! W 3T 3 3$c\`$c$c$cF.9	     	 H/// Ud U U U9-89	   #	2 ??9%% 	+ $$T*****	+ 	+s5   2A2 2D)6C	D)	1C=:D)<C==)D)(D)c                ^   |j         |                                 v sJ | j        |j                  }| j        j                            |          }|                     |           | j        r@|j         | j        v r4| j        	                    |j         | j        |j                             d S d S d S r   )
r~  r  r  r'  r!  	load_cellrI  r  r:  r;  )r   r   cellcontents_vars       r   
LOAD_DEREFz$InstructionTranslatorBase.LOAD_DEREF  s    {d44666666#DK0{/99$??		, 	V$+">">,,T[$-:TUUUUU	V 	V">">r   c                    |j         }|                                 }|                    |           || j        |<   |t          k    r5|                                }t          |          t          u sJ || _        d S d S r   )	r~  r  set_name_hintr  rK   r  r  r   r  )r   r   r   	loaded_vtrj  s        r   
STORE_FASTz$InstructionTranslatorBase.STORE_FAST  s    {HHJJ	%%%%.T"555..00C99$$$$.1D+++ 65r   c                    | j                             |j                  }t          |t                    r|                     |           | j         |j        = d S r   )r  r  r~  rX  r   _maybe_emit_sync_dealloc)r   r   vars      r   DELETE_FASTz%InstructionTranslatorBase.DELETE_FAST  sS    "&&t{33c>** 	/))#... ---r   rL  r   c                   ddl m}m} |j        }|	|j        dvrd S |j        j        }|j                            di                               dd           }t          |j
                                                  }|sd S d }| j        j        j        D ]}	|	|v r|	}	||j        dk    rd S |j                            di                               dd           }
||
k    rd S | ||          }|
 ||          }
 |            }| j                            dt"          j        j        j        ||
fi            | j                            dt"          j        j        j        |||                                fi            d S )Nr   )get_current_stream	new_event)cudaxpucustomstreamr'  r   )variables.streamsrO  rP  devicer  r  r  r  r  r  userskeysr'  r%  nodesr  r  r  opsstreamsrecord_eventsync_deallocas_proxy)r   rL  rO  rP  rV  r  alloc_streamrW  	last_userr  last_use_stream	event_idxs               r   rK  z2InstructionTranslatorBase._maybe_emit_sync_dealloc  s   DDDDDDDD>V[??Fy~y}}Xr2266xFFDJOO%%&& 	F	"( 	 	AEzz		 8 8F#.,,Xr::>>xNN?**F--f55L"0088OIKK	  I*(		
 	
 	
 	  I*cllnn5		
 	
 	
 	
 	
r   c                @   |j         |                                 v sJ | j        |j                  }|                                 }| j        j                            ||           t          |t                    sJ |j	        |
                    |j	                   d S d S r   )r~  r  r  r  r'  r!  
store_cellrX  r~   
local_namerG  )r   r   rC  rj  s       r   STORE_DEREFz%InstructionTranslatorBase.STORE_DEREF  s    {d44666666#DK0hhjj ++D#666$-----?&do..... '&r   c                    |j         }|t          j        |j                  S | j        |         }|s/t          j        |j                  | j        |<   | j        |         }|J |S )Nr  )rD  rh   r|  r~  _constants_cache)r   r   rm  rj  s       r   _load_constz%InstructionTranslatorBase._load_const  sq    H9#*====#A& 	+'7'>T['Q'Q'QD!!$'*C
r   c                V    |                      |                     |                     d S r   )rI  rj  r  s     r   rz  z$InstructionTranslatorBase.LOAD_CONST  s(    		$""4(()))))r   c           	     D   |j         }| j        rU|| j        v r'| j                            || j        |                    n%|| j        v sJ | j        |         | j        j        |<   || j        vr|                     |          S || j        v rR| j        j	        | j        |                  }| 
                    | j        j	                            ||                     d S | j        |         }| 
                    t          j        | |t          |                               d S r   )r~  r  	f_globalsadd_global_var
f_builtinsbuiltinsload_builtinr  r'  r!  rI  load_globalrb   r  rO   )r   r   r   variabler  s        r   _load_globalz&InstructionTranslatorBase._load_global  s   { 	Jt~%%"11$t8LMMMMt....48OD4I"+D1t~%%$$T***4((({/0Ed0KLHIIdk.::8TJJKKKFt$		/'e\$5G5GHHIIIIIr   c                z    d}|                      |          }t          |          }t          j        | ||          S )Nztorch.nn.modules.module)import_sourcer   rb   r  )r   module_namemodule_sourcefglobals_values       r   nn_modules_globals_vtz/InstructionTranslatorBase.nn_modules_globals_vt  s<    /**;77'44$T>=IIIr   c                &   |j         J t          j        dk    r/t          j        dk     r|j         dz  r|                     |           |                     |           t          j        dk    r!|j         dz  r|                     |           d S d S d S )Nr:  r  r  )rD  r\  r  	PUSH_NULLrt  r  s     r   LOAD_GLOBALz%InstructionTranslatorBase.LOAD_GLOBAL
  s    x###w&&3+;g+E+E$(UV,+ENN4   $w&&48a<&NN4      '&&&r   c                   |                                  }|j        }t          |          }|| j        vrt	                      | j        |<   | j        j                            || j        |                   }t          |t                    rt          d|dg            | j        j                            |||           d S )Nz%Storing Tensor hook handle in globalsThis is not supported.r  )r  r~  rO   r  rU  r'  r!  track_global_existingrX  r   r:   store_global)r   r   r  r   r  rs  s         r   STORE_GLOBALz&InstructionTranslatorBase.STORE_GLOBAL  s    

{d##t,,,*0((D!$';+AAD)$/
 
 e455 	?4	    	 --heDDDDDr   rw  rO   c                   d|v r[t           j        j        j        |         }|                    dd                              dd                              dd          }n(t          |          }d|                    dd           }| j        | j                            ||           || j        j        |<   | j        j	        }||vs||         |u sJ |||<   | j        
                    |           t          |          S )z-Create an alias to a module for use in guardstorch_package>r   <rW  _dot_	__import_)r  r  package_importer_package_imported_modulesr>  r   add_import_sourcer'  import_sourcesglobal_scopeupdate_co_namesrO   )r   rw  r  aliasrm  s        r   rv  z'InstructionTranslatorBase.import_source&  s    k))M2LE ##C--55c3??GGWUU E #;//EC 3 3C A ACCE<#L**5+>>>,7"5)K,	I%%5)9U)B)B)BB 	%##E***E"""r   r   levelc                    |                     d|dz
            }t          |          |k     rt          d          |d         }|r| d| n|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
        rW  r   z2attempted relative import beyond top-level packager   )rsplitr   ImportError)r   r   r  r  bitsbases         r   resolve_namez&InstructionTranslatorBase.resolve_name=  sb     ~~c519--t99uRSSSAw#'1$T1r   c                   | j                             d          }| j                             d          }|2|.||j        k    r#t                              d||j        d           |S ||j        S t                              dd           | j         d         }d	| j         vr|                    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__rW  r   )rm  r  r  r  warning
rpartition)r   r  specs      r   calc_packagez&InstructionTranslatorBase.calc_packageI  s    
 .$$]33~!!*--Gt{$:$:?K 	     N;KK8    
 nZ0G//!,,S11!4r   c           
        |                      d          \  }}|                                }|                                }|j        }t          j         d| d| d| }|| j        v r| j        |         }t          |          }n	 t          |||| j                  }n8# t          $ r+ t          dd| d| d| dg t          j        	           Y nw xY w|d
k    r+|                                 }|                     |||          }|s1|                    d          d
         }	|                     |	          }n|                     |          }| j        r| j                            ||           t%          |t&          j        t*          f          r&|                     t/          ||                     d S t          dt1          |          dg 	           d S )Nr  r   )fromlistr  globalszImport failurezmodule_name: z, fromlist: z, level=z"Failure when attempting to import.r  r   rW  r  zBad import resultz%Import result is not a Python module.)rJ  r  r~  rI   LOCAL_MOD_PREFIXrm  rO   
__import__r  r:   r   r  r  r  	partitionrv  r  add_local_modrX  rY  rZ  rH   rI  r   r`   )
r   r   r  r  rw  recorded_namer  r  pkgtop_level_module_names
             r   IMPORT_NAMEz%InstructionTranslatorBase.IMPORT_NAMEf  sA   ))A,,x((**..00k !1TTETTHTT{TT 	 DN**N=1E!-00FF"% N	      ,^K^^X^^W\^^ D9-89	      zz''))"//S%HH  9(3(=(=c(B(B1(E%++,ABB++K88 	C,,]EBBB ee.<== 
	II*5@@@AAAAA+C     s   B 2CCc                d    |                      |           |                     |j                   d S r   )DUP_TOP
_load_attrr~  r  s     r   IMPORT_FROMz%InstructionTranslatorBase.IMPORT_FROM  s.    T$$$$$r   r~  c                \   || j         vr#t          ddd| dg t          j                   | j         |         }t	          |          r?t          | j        j                  }t          ||          }t          j
        | ||          S t          |          sJ t          j
        | |          S )Nz%failed to find name in frame builtinsr   zFailed to find name `z` in frame's builtins.r  )ro  r:   r   r  r  rO   r'  %name_of_builtins_dict_key_in_fglobalsrN   rb   r  rU   )r   r~  rj  builtins_source
var_sources        r   load_builtin_from_argvalz2InstructionTranslatorBase.load_builtin_from_argval  s    ((?RFRRR&1	    of%C== 	4*A O +?FCCJ"(sJ???&s+++++"(s333r   c                `    |                      |                     |j                             d S r   )rI  r  r~  r  s     r   rq  z&InstructionTranslatorBase.load_builtin  s*    		$//<<=====r   Instruction | BlockStackEntryc                    | j         J | j        J |j        J t                                          d| j         | j        z
             | j        |j                 | _         | j         | _        d S )Nir_count)r   r  r-  r[   	incrementindexofr  s     r   r  zInstructionTranslatorBase.jump  s    '333+++{&&&''043CC	
 	
 	
 $(<#< 3r   FTc           	         |j         J | j                            t          ||j         t	          | j                                       d S r   r-  r  r   r,  r   r  r  s     r   
SETUP_LOOPz$InstructionTranslatorBase.SETUP_LOOP  C    {&&&dk3tz?? S STTTTTr   c           	         |j         J | j                            t          ||j         t	          | j                                       d S r   r  r  s     r   SETUP_EXCEPTz&InstructionTranslatorBase.SETUP_EXCEPT  r  r   c                8    | j                                          d S r   )r  r  r  s     r   	POP_BLOCKz#InstructionTranslatorBase.POP_BLOCK  s    r   c                0    |                      |           d S r   setup_or_before_withr  s     r   
SETUP_WITHz$InstructionTranslatorBase.SETUP_WITH      !!$'''''r   c           	         |j         J | j                            t          ||j         t	          | j                                       d S r   r  r  s     r   SETUP_FINALLYz'InstructionTranslatorBase.SETUP_FINALLY  sC    {&&&dk3tz?? S STTTTTr   c                8   |                                                                  }|                     |           	 |                    |           }|                     |           d S # t          t
          j        f$ r}t          |t
          j                  rt          j        |            t          j
        dk    r(|                     t          j        d                      n|                                   |                     |           Y d }~d S d }~ww xY wNr<  )r  realizerI  next_variableStopIterationr   ObservedUserStopIterationrX  r  r\  r  rh   r|  r  )r   r   itrj  r  s        r   FOR_ITERz"InstructionTranslatorBase.FOR_ITER  s    XXZZ!!		"	""4((CIIcNNNNNs<= 	 	 	!S:;; 4-d3337**
 		*1$778888 


IIdOOOOOOOOO	s   *A) )D?BDDc                v    t          |t          j        t          f          r|                    | g i           }|S r   )rX  r   rf   r   r   rm  s     r   _create_exception_typez0InstructionTranslatorBase._create_exception_type	  s@    )+-NO
 
 	2
 ##D"b11C
r   r   r   c                   |                                  } |j        | d          }t          |t          t          f          sJ t	          j        ||          } |j        | dt          j        | d          |gi            d S )Nr  __setattr__)	r  r  rX  rh   r   from_frame_summarycall_methodrb   r  )r   r   r  tbnew_tbs        r   _attach_traceback_to_exceptionz8InstructionTranslatorBase._attach_traceback_to_exception
	  s    **,,S_
 

 !#45
 
 	
 	
 
 #5mRHH"4996B	
 	
 	
 	
 	
r   r   c                   |                      |          }t          | j                  rWt          |t          j                  r=|j        t          u r/t          j	        | t                                        | g i           }|                     |          r9t          |dd           (t          j        j                                        |_        |                     |          rf| j                            |           t+          j        |j                  }t          |dd           } |d|                                 |          t+          j        t2          | d|                                 g           d S )Npython_stackraised exception )r,  z/exceptions must derive from BaseException, not r  )r  r-   r  rX  r   r   exc_typer  rb   r  r  r   _isinstance_exceptionr  r  _guardsr   extract_stackr  r  rn  r   get_dynamo_observed_exception
debug_reprr  r  python_type_name)r   rj  observed_exception_typer  s       r   _raise_exception_variablez3InstructionTranslatorBase._raise_exception_variable	  s    ))#..
 %%	X3	 ;<<	X --!'l;;II$PRTVWWC &&s++	L^T22:$};IIKKC %%c** 		33C888&)&G&U&U#"3==L))6CNN$4$466<    	$Z#BVBVBXBXZZ	
 	
 	
 	
 	
 	
r   c           
        |j         dk    rt          | j                  st          j        t
          | dg           t          | j                  sJ | j        d         }|                     |          s
J |            |                     |           d S |j         dk    r| j        d         }	 |                     |           | j        	                                }| 
                    |           d S # | j        	                                }| 
                    |           w xY w|                                 }|                                 }	 |                     |           | j        	                                }| 
                    |           |                     |          }|                    | dt          j        | d          |gi            d S # | j        	                                }| 
                    |           |                     |          }|                    | dt          j        | d          |gi            w xY w)Nr   zNo active exception to reraiser  r  r   r  	__cause__)rD  r   r  r   r  r  r  r  r  rr  r  r  r  r  rb   r  )r   r   rj  curr_excfrom_vtcauses         r   rn  z'InstructionTranslatorBase.RAISE_VARARGSN	  se   8q==t()) , $.N-O    t()))))#B'C--c2277C772**3/////X]]*R.C>..s333  ,BBDD33H=====  ,BBDD33H==== hhjjG((**C..s333  ,BBDD33H===33G<<$$!$*4==uE	      ,BBDD33H===33G<<$$!$*4==uE	   s   /C4 40D$G A2I	c                    | j         d         }t          |t                    sJ |j        t          u rt          dddg            d S |                     |           d S )Nr  z CLEANUP_THROW with StopIterationr   zRReceived StopIteration when handling generator.throw/close. This is not supported.r  )r  rX  r   r  r  r:   RERAISEr   r   toss      r   CLEANUP_THROWz'InstructionTranslatorBase.CLEANUP_THROWv	  sx    jn#011111<=((:p	      LLr   c                   t           j        dk    rr|                                 }|j        r+|                                 }|                     |           d S |                     |           |                     |           d S |                                 }|                                 }|                                 }|                     |           d S )Nr:  )r\  r  r  r~  r  rI  )r   r   rj  r   _exc_tbs         r   r  z!InstructionTranslatorBase.RERAISE	  s     w&&((**C{ 4HHJJ..s33333 		#..s3333388::D((**C((**C**3/////r   TypeIs[ExceptionVals]c                ,    t          |t                    S r   )rX  r   rm  s     r   r  z/InstructionTranslatorBase._isinstance_exception	  s    #}---r   c                   g }t           j        dk    rt           j        dk     rdnd}t          | j                  |k    sJ | j        |          }| j        d         }|                     |          sJ t          |j                  }|                    | d          }t           j        dk    r@t          | j        d         t                    s |
                    | j        d                    nut          | j                  dk    sJ | j        d	         }| j        d
         }|                     |          sJ t          |j                  }|                    | d          }||||gz  }|                     ||i            d S )Nr:  r           r  r     ir  )r\  r  r   r  r  rf   r  r  rX  r   r   r   )r   r   r   fn_locrC  rj  typr  s           r   WITH_EXCEPT_STARTz+InstructionTranslatorBase.WITH_EXCEPT_START	  s{   &(w&&*W44QQ!F tz??f,,,,VG$B*R.C--c22222!#,//C B
 7**!$*R.,?? 0KK
2///tz??a''''BB*R.C--c22222!#,//C77Bc22tR(((((r   raised_exceptionr   c           	     
   	 d	d	 fd}t           j        dk    rA j        j        }|rt	           j                  |j        k    r1                                  t	           j                  |j        k    1|j        r2 	                    t          j          j        j                              	                     j                                                                        |           d S  j                                          j                                        }                     |           t%                     t&          u r
 |             t	           j                  rS j                                        }|j        j        dk    rЉ                     d            j                                         t	           j                  dk    ra j                                         t%                     t&          u r0t1          d	t3                    	d
z   g t4          j                    j                                        }|j        j        dk    Љ j                                        } j                                         t	           j                  |j        k    r1                                  t	           j                  |j        k    1t=          t?          d          dd d          } j                             tC          |d t	           j                                       t	           j                  dk    ru j        d         } 	                    tE          j#                                	                    |            	                    tE          j$        |j%                             nu 	                    tM          j'        d                       	                    tM          j'        d                       	                    tM          j'        d                       	                    tE          j#                                	                    |            	                    tE          j$        |j%                                                  |           d S  j                                         t%                     t&          u r
 |             )NzDynamo found no exception handler at the top-level compiled function when encountering an exception. Exception will propagate outside the compiled region.r   r   c                 ,   j                                         } t          j        |                                           }t          |          sJ t          dd|                                  g t          j	        t          j
                   d S )NzObserved exceptionr  r(  )r  rr  r   r  python_typerX  r:   r  r   r  rU  )r  
dynamo_excobserved_exn_gb_explanationr   r   s     r   bubble_exception_to_interpreterzTInstructionTranslatorBase.exception_handler.<locals>.bubble_exception_to_interpreter	  s    (>>@@H:8;O;O;Q;QRRJ.
;;;;;,CH,?,?,A,ACC7&1&2 *	 	 	 	 	 	r   r:  EXCEPT_HANDLERr  r   z#Observed exception (EXCEPT_HANDLER)z  This graph break is unexpected.r(  g    .Ar  r  r   )(r\  r  r  rI  r   r  depthr  lastirI  rb   r  r  r  rr  r  r   r  r  rG  r  r   r   rJ  r:   r   r   r  rp  r.  r,   r   r   r,  r   r   rf   r  rh   r|  )
r   r   r  rI  r  block_stack_entryexception_varexcept_handler_instold_exceptionr  s
   ``       @r   r  z+InstructionTranslatorBase.exception_handler	  s   d 	$
	 	 	 	 	 	 	 	  w&& 4BM '
 $*oo(;;;HHJJJ $*oo(;;; !& II'-dD4L4STT  
 		$+AACCDDD 		-((((( 
  """  ,BBDD33H===::!66633555&&4#$$ I' %)$4$8$8$:$:!',37GGG IIaLLL%))+++4+,,11 
((***::)>>>)(M(+,<(=(=,G"D-E&E(9(D&E)9    /.(,(8(<(<(>(>%) (,37GGG, !% 1 G G I I!AACCC $*oo(9(EEEHHJJJ $*oo(9(EEE
 '2#c((<LdTU&V&V# ''#$7s4:OO  
 t())Q..$($5b$9M IIi799:::IIm,,,IIi78NOOPPPP II.5d;;<<<II.5d;;<<<II.5d;;<<< 		)355666		-(((		)3M4JKKLLL 		+,,,,, 
  """::!66633555&&r   c                (   |                                  }t          | j                  dk    rt          j        d           }n| j        d         }|                     |           |                     |           | j                                         d S )Nr   r  )r  r   r  rh   r|  rI  rp  )r   r   rj  prev_excs       r   PUSH_EXC_INFOz'InstructionTranslatorBase.PUSH_EXC_INFOG
  s    ( hhjjt !!Q&&(8(?(E(EHH(,H		(		#99;;;;;r   c                   t           j        dk    rE|                                 }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  r  zYBug in Dynamo tracing of exception handling.Top of the block stack is not EXCEPT_HANDLER.r  )
r\  r  r  r   r  r  r   r   r  rJ  )r   r   r   s      r   
POP_EXCEPTz$InstructionTranslatorBase.POP_EXCEPTd
  s    w&&

At()))))!!#####t'((1,,,,#(/3CCC$D     """IIaLLL t()))))!!#####r   c                   t          | j                  dk    sJ |                                 }t          j        dk    r| j        d         }n| j                                        }t          |t          t          t          t          f          s0t          dt          |          d| dg t          j                   t          j        dk    rE|                     |          s0t          dt          |          d	| dg t          j                   t          |t                    r|j        }n|g}|D ]}t          |t          t          t          f          s0t          d
t          |          d| dg t          j                   |                     |          rt!          |j        |j                  r dS t          |t&          j                  rt!          |j        |j                  r dS dS )Nr  r:  r  z Exception with bad expected typez"`except ...` has unsupported type rW  r  zCaught non-Exception valuezCExcept expects to receive an object of Exception type but received z#Exception with non-type expectationz!`except ...` expects a non-type: TF)r   r  r  r\  r  rX  rf   r}   r   r   r:   r   r   r  r  r  
issubclassr  rC  r   )r   expected_exc_typesexc_instanceexpected_typesexpected_types        r   check_if_exc_matchesz.InstructionTranslatorBase.check_if_exc_matchesy
  sJ   4:!####!XXZZw&&  :b>LL
  :>>++L 12	
 
 	 :.//VASVVV5)45	    w&&--l;; 8-- ufr u u u9-89	    (-88 	/5NN #N , 	 	M#65   A.. TM T T T9-89	    )),77 
J% = = 
 ttL)*CDD  J J 
 ttur   c                z    |                      t          j        | |                                                      d S r   )rI  rb   r  r  r  s     r   CHECK_EXC_MATCHz)InstructionTranslatorBase.CHECK_EXC_MATCH
  s3    		/'d.G.G.I.IJJKKKKKr   c                \    |                                  s|                     |           d S d S r   )r  r  r  s     r   JUMP_IF_NOT_EXC_MATCHz/InstructionTranslatorBase.JUMP_IF_NOT_EXC_MATCH
  s3    ((** 	IIdOOOOO	 	r   c                    |j         dk    r|                     |           d S |                     t          |j                  | |                     d          i                      d S )Nzexception matchr  )r~  r  rI  r   rJ  r  s     r   
COMPARE_OPz$InstructionTranslatorBase.COMPARE_OP
  s`    ;+++  &&&&&II)$+6tTYYq\\2NNOOOOOr   c                    |                      t          j        | t                    |                                 gi            d S r   )r   rb   r  iterr  r  s     r   GET_ITERz"InstructionTranslatorBase.GET_ITER
  s7    ?0t<<txxzzlBOOOOOr   zjEncountered graph break when attempting to trace CALL_FUNCTION: a call to a regular function, e.g. f(x, y))rI  r"  c                    |                      |j                  }|                                 }|                     ||i            d S r   )rJ  r~  r  r   )r   r   r   rC  s       r   CALL_FUNCTIONz'InstructionTranslatorBase.CALL_FUNCTION
  sB    
 yy%%XXZZ2tR(((((r   zjEncountered graph break when attempting to trace CALL_FUNCTION_EX: a variadic function call, e.g. f(*args)c           	        |j         dk    r$t          i           }|                                 }n|j         dk    st          j        dk    rM|                                 }t          |t                    rt          i           }|                                 }n/t          dd|j          d|j          g t          j	                   t          j        dk    r+|                                 }t          |t                    sJ |                                 }t          j        d	k    r;t          j        dk     r+|                                 }t          |t                    sJ t          |t                    s7|                    |           r"t          |                    |                     }t          |t                    rt          j        | t"          |          }t          |t                    rt          |t                    s?t          d
dt%          |           dt%          |           dg t          j                   |                                }|                     ||j        |           d S )Nr   r   r  z%Variadic function call with bad flagszflags: zHAttempted to call a variadic function (CALL_FUNCTION_EX) with bad flags r  r  r:  z0Variadic 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~  rn   r  r\  r  rX  r   r:   r   r  rx   has_force_unpack_var_sequencer}   force_unpack_var_sequencer   rg   call_custom_dictr  r`   r  keys_as_python_constantr   r  )r   r   
kwargsvarsargsvarsnullrC  s         r   CALL_FUNCTION_EXz*InstructionTranslatorBase.CALL_FUNCTION_EX
  si    ;!*2..JxxzzHH[A!1W!<!<J*l33 3.r22
xxzzHH?/$+//tgkgrtt5)45	    w&&88::DdL11111XXZZw&&3+;g+E+E88::DdL11111
 
 	O 44T::	O %X%G%G%M%MNNH j";<< 	V,=dD*UUJ ($455 	Z>
 >
 	
 J]gh&7&7]]PZH[H[]]P5)45     7799
2x~z:::::r   zyEncountered graph break when attempting to trace CALL_FUNCTION_KW: a function call with keyword arguments, e.g. f(x=True)c                   |                                  }|                     |j                  }|                                  }t          |t                    r|                                sJ |                                }|d t          |                    |t          |           d          }}t          t          ||                    }t          |          t          |          k    sJ | 
                    |||           d S r   )r  rJ  r~  rX  r}   r  r  r   r  zipr   )r   r   argnamesr   rC  kwargs_listr  s          r   CALL_FUNCTION_KWz*InstructionTranslatorBase.CALL_FUNCTION_KW%  s     88::yy%%XXZZ(M22Tx7R7R7T7TTTT..00 !1CMM>!12D#h--9I9I4Jkc(K00116{{c(mm++++2tV,,,,,r   c                8   |                      t          j        |d                     |j        d         }| j        d         |         }t
          j        dk     r|                     |           d S |                     t          j        ||                     d S )Nr  rA  r   co_namesr:  )	r$  r   r>  r~  r  r\  r  r  LOAD_METHODr   r   rD  r~  s       r   LOAD_METHOD_SUPERz+InstructionTranslatorBase.LOAD_METHOD_SUPER5  s    ;.tA>>>???k!n":.s3g%%OOF#####[0fEEEFFFFFr   c                    |                      t          j        |d                     |j        d         }| j        d         |         }|                     |           d S )Nr  rA  r   r4  )r$  r   r>  r~  r  r  r6  s       r   LOAD_ATTR_SUPERz)InstructionTranslatorBase.LOAD_ATTR_SUPER>  s[    ;.tA>>>???k!n":.s3r   c                l   |                      |j                   |                                 }t          j        dk    r,|                     |           |                     |           d S t          j        dk    r,|                     |           |                     |           d S t          d          )Nr  r:  zPLOAD_METHOD should have been rewritten to LOAD_ATTR. We should never reach here.)r  r~  r  r\  r  rI  r|  r  r   r   objs      r   r5  z%InstructionTranslatorBase.LOAD_METHODD  s    $$$hhjjw&&IIcNNNNN4     (( NN4   IIcNNNNN b  r   c                     t          d          )Nz^CALL_METHOD should have been rewritten to CALL_FUNCTION. This function should never be called.)r  r  s     r   CALL_METHODz%InstructionTranslatorBase.CALL_METHODU  s    l
 
 	
r   attrc                    |                                  }t          j        | t                                        | |t          j        | |          gi           }|                     |           d S r   )r  rb   r  r  r   rI  )r   r?  r<  r  s       r   r  z$InstructionTranslatorBase._load_attrZ  sf    hhjj &tW55CC/'d334
 

 			&r   c                    t           j        dk    r!|j        dz  r|                     |           d S |                     |j                   d S )Nr<  r  )r\  r  rD  r5  r  r~  r  s     r   	LOAD_ATTRz#InstructionTranslatorBase.LOAD_ATTRc  sQ    w&&x!|   &&&$$$$$r   zkEncountered graph break when attempting to trace STORE_ATTR: storing an object's attribute, e.g. x.attr = yc                    |                      d          \  }}t          j        | t                                        | |t          j        | |j                  |gi            d S )Nr  )rJ  rb   r  setattrr   r~  )r   r   rj  r<  s       r   
STORE_ATTRz$InstructionTranslatorBase.STORE_ATTRk  se    
 99Q<<SdG,,::/'dk::C@	
 	
 	
 	
 	
r   c                    |                                  }|                     ||j                   t          j        | t
                                        | |t          j        | |j                  gi            d S r   )r  _maybe_sync_dealloc_attrr~  rb   r  delattrr   r;  s      r   DELETE_ATTRz%InstructionTranslatorBase.DELETE_ATTRw  ss    hhjj%%c4;777dG,,::/'dk::;	
 	
 	
 	
 	
r   r<  c                    | j         j                            ||          rL| j         j                            ||          }t	          |t
                    r|                     |           d S d S d S r   )r'  r!  has_pending_mutation_of_attr	load_attrrX  r   rK  )r   r<  r   attr_vars       r   rG  z2InstructionTranslatorBase._maybe_sync_dealloc_attr  s{     ;#@@dKK 	8{/99#tDDH(N33 8--h77777	8 	88 8r   r  c                .   g }|dk    sJ |dk    r"|                     t          d                     | j        dk    rt          d          nt          d| j                  }|                     t          d          t          d          |g           |S )a  
        Debug CPython expects the stack to be empty after the return.
        Calling compile_subgraph will push cells and frame values to TOS.
        This function will pop those 2 values from the stack before actually returning.

        Expects the stack to be:
            cells, frame values, current frame stack (0 or 1 values)

        Pops cells and frame values, leaving the current frame stack as TOS.
        A return instruction is included.
        r   r  r  r  rA  r  )r  r*   r   r'   r~  )r   r  rr  return_insts       r   codegen_return_with_popsz2InstructionTranslatorBase.codegen_return_with_pops  s      A~~~~>>LLQ((( {n,, ~...#N4;GGG 	
 		**,>y,I,I;W	
 	
 	
 r   r  resume_instr  rB   resume_codeslist[types.CodeType]r  r1   is_leafhandle_inactive_ctxtuple[types.CodeType, str]c                    |rt          |j        |j                  D ]\  \  }}	}
t          t           j        |
                   }|                    t                                 |                    |           |	                    g t          d          |                    |          |                                |                    |          t          d                     |j        D ]\  }}	t          t           j        |                   }|                    t                                 |                    |           |	                    g t          d          |                    |          |                                |                    |j        |j        |         z             t          d                     t%          |          r|j        sJ |j        }t)          d|j                   }|rt-           j        |          t1           fd j        D                       }t3          |j                  t1          fd|D                       }t1          fd|D                       }|	                    t                      |                    |          |                                t                      g           |D ]m}|	                    t                      |                    |j        |j        |         z             |                                gt          d                     n|	                    t          d          t          dt7          |          	          gt          d          t9          |j        d
d                     n:t1          |j                                                  }t1          |j                  }t<          j        dk     rt7          |          dk    s
J d            t7           j                  t7          |j                   z
  } j!        j        J tE          j#         j$         j%         j!        j        |j        t1          d  j&        D                       |||t1          d  j&        D                       |t1          |j                  t1          |j                  t1          |j                   t1          |           j'                   } tQ          j)         j$                  *                    dd                       }|)tW          j,        |          tQ          j)        |          d<    j-        .                    || j/                    j0        ' j0        1                    | j/        d         |           td          d         |j3        xx         dz  cc<   ||fS )a.  
        Creates the resume function for the frame corresponding to `self`.

        Expects the TOS to be:
            [frame N cells, ..., frame 1 cells],
            [
                frame N stack + locals,
                ...,
                frame 1 stack + locals
            ]

        Some additional codegen may happen to prepare the frame stack + locals values for the generated resume function:
        - inactive context variables in the stack and locals will be replaced by their types
        - if the frame is a leaf frame, prune dead locals

        Regardless of codegen, the stack will be left in the same state as before.

        Args:
            - idx: depth of this frame: 0 corresponds to the leaf frame (frame N), N-1 to the root frame (frame 1).
            - resume_inst: the instruction that this frame should resume at
            - meta: metadata for this frame returned from OutputGraph.compile_subgraph
            - resume_codes: nested resume code objects generated from previous create_resume calls.
            - cg: codegen object to output to
            - is_leaf: True if `self` corresponds to the leaf frame.
            - handle_inactive_ctx: If True, handles inactive context variables as described above. This is necessary
                iff the resume function is traced
        r  r  __resume_at_c              3  P   K   | ] }|v |                                 v|V  !d S r   r  )r   r   r  r   s     r   r   z:InstructionTranslatorBase.create_resume.<locals>.<genexpr>  sN       ! !::!4+A+A+C+C"C"C "C"C"C"C! !r   c              3  $   K   | ]
}|v|V  d S r   r   r   r   argnames_null_sets     r   r   z:InstructionTranslatorBase.create_resume.<locals>.<genexpr>
  s.      SS1AR8R8RQ8R8R8R8RSSr   c              3  $   K   | ]
}|v |V  d S r   r   r[  s     r   r   z:InstructionTranslatorBase.create_resume.<locals>.<genexpr>  s.      !T!TQBS=S=S!=S=S=S=S!T!Tr   r  r   r  NTr<  r   z&variables should not be NULL in < 3.12c              3  .   K   | ]}|j         j        V  d S r   )r-  r  r   r   s     r   r   z:InstructionTranslatorBase.create_resume.<locals>.<genexpr>?  s&      <<a!(/<<<<<<r   c              3  >   K   | ]}|                                 V  d S r   )r6  r_  s     r   r   z:InstructionTranslatorBase.create_resume.<locals>.<genexpr>C  s*      ::A!++--::::::r   orig_graphmodulec                     d S r   r   r   r   r   r   z9InstructionTranslatorBase.create_resume.<locals>.<lambda>O  s     r   r   resumesr   )4r/  stack_ctx_argsstack_ctx_idxes_origr   ri   r  append_outputr&   r  r  r*   r
  r  r'   locals_ctx_argsr  r  r  r.   r-  r/   r  r   r}  r5  r  locals_null_keysr   r"   rX  r\  r  stack_null_idxesr  rJ   lookupr  r   r  r-  r0   get_contextr  weakrefrefr'  install_resume_function_globalrm  r  add_resume_functionrX   r  )r   r  rQ  r  rR  r  rT  rU  jr   j_origry  r   resume_nameall_argnamesr0  argnames_nullrD  	stack_lennew_codeorig_graphmodule_mayber\  r  s   `                    @@r   r  z'InstructionTranslatorBase.create_resume  sO   T  	"%d&94;T"U"U  A2DJv4FGG  !1!1222$$R(((  $Q,,S11 //11 ,,Q//	
 +>::     /  a2D4H4NOO  !1!1222$$R(((  $Q,,S11 //11 ,,T^d>OPT>U-UVV	
 +>::    K(( 	-%%%%%,K C{/A C CDD  -	9%d&7EEE  ! ! ! ! !-! ! !  L
 !$D$9 : :SSSSSSSSSH!!T!T!T!T\!T!T!TTTM "$$((--++--"$$	     	 	  &((,,T^d>OPS>T-TUU//11 %Q	    &y11&|XGGG !^^
 )tDD	 	 	 	 T.335566H!$"788Mg%%}%%***,T*** 
OOc$*?&@&@@	'.:::#9#@KK$+<<4+;<<<<<::)9:::::$%&&$&''$'((,--!$
 $
*"
!9$+!F!F!J!J"
 "
 " " "-EL[&F FL$X../AB
 	224>	
 	
 	
 <#L,,$.4k   	H,---2---$$r   r  list[StackLocalsMetadata]c           
      	   d| _         t          | j        j                  }g }g }g }| }d}|f|| u r|}	n|j        }	|	j        dk    r?|                    |           |                    |           |                    |	           |j        }|dz  }|ft          | j	                  t          |d         j
                  z
  }
|s|
dk    sJ |                    t          |
dz                        t          |
dz             D ]$}|                    t          d                     %|                    t          d                     |                                S |d         | u r4|                    t          d|
                     d}|
|d         _        n|                    t          |
                     t          |
dz
            D ]$}|                    t          d                     %|                    t          dd                     ||d                  j        }||d                  xj        dz  c_        |d                             t'                                 |                    g t)          d          |                    |d                   |                                t/          ||d	                     g }|D ]Z}|                    t3                      |                    |          |                                gt          d                     [|                    t          d          t          dt          |                    g           |                    |           |                    g t          d          t5          j        |          t          d                     g }g }t9          |          D ]a\  }}|                    |||         |||                  |||| u d	          \  }}|                    |           |                    |           b|                     |||           |                    t          d                     |                                S )
aP  
        Codegen all resume function(s) from the frame stack starting at `self`, call them,
        and return the result.
        Assumes that the unsupported instruction has already been run.

        Expects the TOS to be:
            [
                frame N locals,
                frame N-1 stack + locals,
                ...,
                frame 1 stack + locals
            ], *(frame N stack (post-unsupported instruction))

        Leaves the result of calling the resume functions on the stack and returns it
        (empty stack after return).

        Args:
            - inst: the instruction of the current (deepest) frame to resume at
            - all_stack_locals_metadata: metadata returned from OutputGraph.compile_subgraph - contains
                metadata such as local names, NULL positions, stack length, etc.
        Nr   r  r   r  r  r   r  T)r   r1   r'  rF  r  r   r   r  r   r  ri  r  r*   r^  rf  r'   rp  r  rI  r   r%   r
  r  r"   r  r&   rH  deepcopyrq  r  r  )r   r   r  r  txesidxesresume_instsr  r  rQ  current_num_stackr   stack_insert_idxfilter_instsrR  resume_namesrm  resume_coderr  s                      r   r  z/InstructionTranslatorBase.create_call_resume_atc  s
   6 $( t{*++ 13*,37 ~~"$5!^33F###S!!!##K000]F1HC    
OOc%a(9/
 /
 

  	)
 %))))[):Q)>??@@@,q011 @ @  !3I!>!>????/??@@@&&((( 7d?? /BSTTTUUU 5F%a(22
 [):;;<<<,q011 @ @  !3I!>!>????/!DDDEEE8qBL%eAh/99Q>99GLL**+++ 	Q $$U1X.. ''))	
 %%57GNN
	
 
	
 
	
  	 	C"$$((--++-- !^^	    	"9--"<SZZ@@@	
 	
 	
 	&&& 	Q|,, Q	
 	
 	
 .0"4 	- 	-IAv'-';';Q)%(3$( ($K ,,,,,,,  |R@@@
+N;;<<<""$$$r   r  c           	        |                     t          d                     t          t          ||                     D ]	\  }\  }}|t	          |          dz
  k    r n|j        r|                     t                      |                    |          |                                g           |                     |	                    |d          gt          d          t          dd                     n*|                     |                    |dd                     |                     t          d                     |                     t          d          t          d	t	          |           dz
  
          g           |                     t          d                     | d         j        r|                     |                    d          |                                g           |                     |	                    |d         d          gt          d          t          dd                     |                     g t          d                     nN|                     t          d          g|                    |d         d          t          d                     |                     t                      t                      |                    d          |                                gt          d          |                    d          t          d                     |                     g t          d          t          d	d
          t          d          t          dd
                     |                     t          dd                     dS )a  
        Calls the provided resume functions.

        Expects the TOS to be in the state:
            [frame N cells, ..., frame 1 cells],
            [
                frame N stack + locals,
                frame N-1 stack + locals,
                ...,
                frame 1 stack + locals
            ]

        Pops the cells and frame values, leaving the result of calling the resume functions on TOS.

        Args:
            - resume_codes: list of resume function code objects to call
            - resume_names: list of the corresponding names of the resume functions
            - cg: PyCodegen object to output instructions to
        r  r   T)r  )	push_nullFr   r  r   r  r  r  r  LIST_EXTENDN)r  r%   rq  r/  r   r  r&   r
  r  create_load_globalr*   r#   load_function_namer'   r)   r$   )rR  r  r  rm  r   r  s         r   r  z-InstructionTranslatorBase.codegen_call_resume  s   4 	Q((((\<)H)HII 	- 	-OA|dC%%))) H  &((,,Q////11     --d-==$Q .a4@@@      !6!6tUA!F!FGGG[^^,,,,
"9--"<S5F5F5JKKK	
 	
 	
 	Q(((' 	((,,++--   )),r*:)EE ^^ *!t<<<   !!__    &y11**<+;UCC "!__  ( 	    $$R(('')) Q $$R(( #?33	
 	
 	
  	a"<Q777 Q
 #=a888	
 	
 	
 	0==>>>>>r   c                2   t           j        dk    r0| j        j        }|r"| j        r|j        | j        d         j        urdS t          d | j        D                       o9| j         o1| j        p| j	         o"| j
         o| j         o| j        j        j        d u S )Nr:  r  Fc              3  >   K   | ]}|                                 V  d S r   )r1  r_  s     r   r   zIInstructionTranslatorBase.should_compile_partial_graph.<locals>.<genexpr>  s*      ::A::::::r   )r\  r  r  rI  r  r-  r  r0  r  r   r  r/  r'  current_tracerr  )r   r   s     r   r  z6InstructionTranslatorBase.should_compile_partial_graph  s    w&&,:E $(-D<LR<P<W(W(Wu::)9::::: :N": ,MD4M0M: 33	:
 88: *1T9		
r   ziEncountered graph break when attempting to trace STORE_SUBSCR: trying to store subscript, e.g. x[key] = yc                l    |                      d          \  }}}|                    | d||gi            d S )Nr  __setitem__)rJ  r  )r   r   rj  r<  keys        r   r  z&InstructionTranslatorBase.STORE_SUBSCR  s;    
 		!S#mc3Z<<<<<r   c                    |                      d          \  }}|                     ||           |                    | d|gi            d S )Nr  __delitem__)rJ  _maybe_sync_dealloc_subscrr  )r   r   r<  r  s       r   r  z'InstructionTranslatorBase.DELETE_SUBSCR  sM    99Q<<S 	''S111mcUB77777r   r  c                6   ddl m} ddlm} d }	 t	          ||          r|                    | |          }n%t	          ||          r|                    |          }n# t          $ r Y nw xY wt	          |t                    r| 	                    |           d S d S )Nr   rm   )rx   )
variables.dictsrn   variables.listsrx   rX  getitem_constmaybe_getitem_constr  r   rK  )r   r<  r  rn   rx   item_vars         r   r  z4InstructionTranslatorBase._maybe_sync_dealloc_subscr  s     	766666555555		#/00 8,,  C!233 822377 	 	 	D	h// 	4))(33333	4 	4s   AA 
A*)A*c                ~    |                      |j                  }|                     t          |                     d S r   )rJ  r~  rI  r}   r   r   r  s      r   BUILD_TUPLEz%InstructionTranslatorBase.BUILD_TUPLE  s5    		$+&&		-&&'''''r   c                    |                      |j                  }|                     t          ||                      d S )N)r   )rJ  r~  rI  r|   r  s      r   BUILD_SLICEz%InstructionTranslatorBase.BUILD_SLICE  s:    		$+&&		-$///00000r   c                    dS )zCheck if comprehension speculation is allowed in nested context.

        For the base class (non-inlined), this always returns False.
        Fr   r   s    r   #_can_speculate_comprehension_nestedz=InstructionTranslatorBase._can_speculate_comprehension_nested  s	    
 ur   c                    t          | |          rd S |                     |j                  }|                     t	          j        | |                     d S r   )r2   rJ  r~  rI  rb   r  r  s      r   r   z$InstructionTranslatorBase.BUILD_LIST  sQ    0t<< 	F		$+&&		/'e4455555r   c                    t           j        rt          dddg            |                     |j                  }t          |t                                }|                     |           d S )Nzmissing BUILD_SET handlerr   z:Missing BUILD_SET bytecode handler (for testing purposes).r  mutation_type)r   +inject_BUILD_SET_unimplemented_TESTING_ONLYr:   rJ  r~  r   ra   rI  )r   r   r  new_sets       r   	BUILD_SETz#InstructionTranslatorBase.BUILD_SET  sv    = 	3X	    		$+&&e3C3E3EFFF		'r   r  r  c           	     p   |                      |j                  }g }|D ]j}	 |                    |                    |                      ,# t          $ r2 t          dt          |          | dg t          j                   Y gw xY w| 	                     ||t                                           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  )rJ  r~  r  r'  NotImplementedErrorr:   r   r   r  rI  ra   )r   r   r  seqsr  seqs         r   BUILD_LIST_UNPACKz+InstructionTranslatorBase.BUILD_LIST_UNPACK  s    yy%% 
	 
	C	S::4@@AAAA&   KHH#& !2 !2 !29-89      			##e+;+=+=>>>?????s   (A9BBc                >    |                      |t                     d S )Nr  )r  r}   r  s     r   BUILD_TUPLE_UNPACKz,InstructionTranslatorBase.BUILD_TUPLE_UNPACK  s!    t77777r   c           	        t          | |          rd S |                     |j        dz            }t          t	          |d d d         |dd d                             }|                     t          j        | |                     d S )Nr  r   )r2   rJ  r~  r  r/  rI  rb   r  )r   r   r  ds       r   	BUILD_MAPz#InstructionTranslatorBase.BUILD_MAP  s    0t<< 	F		$+/**U33Q3Zqt!t--..		/'a0011111r   c                                          |j                  } fd|D             }i }|D ]3}t          |t                    sJ |                    |j                   4                     t          j         |                     d S )Nc                p    g | ]2}t          j        t                                        |gi           3S r   )rb   r  r  r   )r   ra  r   s     r   ri  z>InstructionTranslatorBase.BUILD_MAP_UNPACK.<locals>.<listcomp>	  sJ     
 
 
 !$--;;D1#rJJ
 
 
r   )	rJ  r~  rX  rn   updater  rI  rb   r  )r   r   r  r  ra  s   `    r   BUILD_MAP_UNPACKz*InstructionTranslatorBase.BUILD_MAP_UNPACK  s    		$+&&
 
 
 

 
 
 "$ 	# 	#Aa!233333MM!'""""		! 	
 	
 	
 	
 	
r   c                   |                                  }|                     |j                  }t          |t                    sJ |                                sJ |                    |           }t          |          t          |          k    sJ |                     t          j
        | t          t          ||                                         d S r   )r  rJ  r~  rX  r}   r  r'  r   rI  rb   r  r  r/  )r   r   rX  r  s       r   BUILD_CONST_KEY_MAPz-InstructionTranslatorBase.BUILD_CONST_KEY_MAP  s    xxzz4;''$.....&&(((((--d334yyCKK''''		!Sv&&'' 	
 	
 	
 	
 	
r   c                   |                      d          \  }}|j        dk    sJ |j        J | j        |j                                                  }t          |t                    sJ |                    | d||fi            d S )Nr  r   r  )rJ  r~  rD  r  r  rX  rn   r  )r   r   r   r   r<  s        r   MAP_ADDz!InstructionTranslatorBase.MAP_ADD*  s    yy||1{Qx###j$(#++--#011111maVR88888r   c                   |                                  }|j        dk    sJ |j        J | j        |j                  }t	          |t
                    sJ |                                sJ |                    | d|gi            d S )Nr   r  r  r~  rD  r  rX  r   
is_mutabler  r   r   r   r<  s       r   SET_ADDz!InstructionTranslatorBase.SET_ADD2  s    HHJJ{Qx###j$(##{+++++~~eaS"-----r   c                   |                                  }|j        dk    sJ |j        J | j        |j                  }t	          |t
                    sJ |                                sJ |                    | d|gi            d S Nr   r  r  r  s       r   
SET_UPDATEz$InstructionTranslatorBase.SET_UPDATE;  s    HHJJ{Qx###j$(##{+++++~~hR00000r   c                p   |                                  }|j        dk    sJ |j        J | j        |j                                                  }t          |t                    sJ |                                sJ | j        j	        
                    |           |j                            |           d S r   )r  r~  rD  r  r  rX  r{   r  r'  r!  mutationr  r   r  s       r   r  z%InstructionTranslatorBase.LIST_APPENDD  s    HHJJ{Qx###j$(#++--#|,,,,,~~ ))#...	r   c                   |j         }t          j        dk     r|                                 }|                                 }t          j        dk    r6t	          |j        d          sJ t          j        | |j        j                  }d }d }d }d }t          j        dk     rf|d|dz  r|                                 }|dz  r|                                 }|dz  r|                                 }|dz  r|                                 }t          ||| j
        |||          }	|rt          |t                    sJ |j        }
t          t          t!          |
d d d         |
dd d         d	                    t#                      
          }|	                    |                               d|           |                     |	           d S )Nr:  co_qualnamer     r  r  r   Tstrictr  r   )rD  r\  r  r  r4  r  rb   r  r  rr   rm  rX  r}   r  rn   r  r/  ra   get_dict_vtsetitemrI  )r   r   r  fn_namer  defaultsclosurer   
kwdefaultsrC  r  anns               r   MAKE_FUNCTIONz'InstructionTranslatorBase.MAKE_FUNCTIONN  s   g%%hhjjGxxzzw&& 4:}55555%+D$*2HIIG
g%% 4< )"hhjjG4< -"&((**K4< ,!%J4< *#xxzzH'N
 
  	k=99999  %E#SsssU14a4[>>>??.00  C NN4  ((!3   			"r   c           	     L                                      }|                                r*|                     t          |j                            }nt          |t                    rg|j                                        rNt          |j        	                                |j
                   fdt          |j                  D             }nZ|                               r|                               }n/t          dt          |          | dg t          j                   t#          |          |j        k    r:t          dd|j         dt#          |           | d	g t          j                   t'          |          D ]}                     |           d S )
N)r|  c                <    g | ]}t          |                   S r   )re   )r   rm  r  r   s     r   ri  z=InstructionTranslatorBase.UNPACK_SEQUENCE.<locals>.<listcomp>  s'    MMMQ=uQx00MMMr   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  r  r^  r~  rX  r   r<  r  r^  r   r&  r'  r:   r   r   r  r   r  r   rI  )r   r   r  rj  rm  r  s   `    @r   r  z)InstructionTranslatorBase.UNPACK_SEQUENCE  s   hhjj==?? 	))$eDK6H6H)IICC_-- 	#'2C2C2E2E 	CG,,..99EMMMMM%:L:LMMMCC..t44 		//55CCEC" ( ( (5)45    s88t{""SMDKMM3s88MM" ? ? ?5)45    # 	 	AIIaLLLL	 	r   c                   d|j         cxk    rdk    sn J |j         dz  }|j         dz	  }|                                 }|                    |           rt          |                    |                     }t          |          ||z   k    sJ |d |         }||t          |          |z
           }|t          |          |z
  d          }t          |          D ]}	|                     |	           |                     t          |                     t          |          D ]}	|                     |	           d S t          dt          |          | dg t          j                   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   rI  r}   r:   r   r   r  )
r   r   prefixsuffixr  r/  vals_prefix	vals_listvals_suffixitems
             r   	UNPACK_EXz#InstructionTranslatorBase.UNPACK_EX  s   DK))))6))))))t#!hhjj,,T22 	55d;;<<Dt99////ww-KVc$ii&&889Is4yy61334K --    		$IImI../// --    		$    ?C"___5)45	     r   z-Encountered intentional debugging graph breakc                @    | j         rt          ddddg           d S d S )Nz#Forced graph break on leaf functionr   zSForced graph break on non-inlining function for nested graph break testing purposeszGSet torch._dynamo.config.debug_force_graph_break_on_leaf_return = Falser  )r  r:   r  s     r   graph_break_on_leaf_functionz6InstructionTranslatorBase.graph_break_on_leaf_function  sP     $ 		=6 ^     		 		r   c                J    |j         dk    r|                     |           d S d S )NGRAPH_BREAK_IF_LEAF)r~  r  r  s     r   r  zInstructionTranslatorBase.NOP  s2    ;///--d33333 0/r   c                .    |                                   d S r   r5  r  s     r   r  z!InstructionTranslatorBase.POP_TOP      




r   c                    |                                  }|                                  }|                     |           |                     |           d S r   r  rI  r   r   r  r   s       r   ROT_TWOz!InstructionTranslatorBase.ROT_TWO  s>    HHJJHHJJ		!		!r   c                    |                                  }|                                  }|                                  }|                     |           |                     |           |                     |           d S r   r  )r   r   r  r   cs        r   	ROT_THREEz#InstructionTranslatorBase.ROT_THREE  sZ    HHJJHHJJHHJJ		!		!		!r   c                N   |                                  }|                                  }|                                  }|                                  }|                     |           |                     |           |                     |           |                     |           d S r   r  )r   r   r  r   r  r  s         r   ROT_FOURz"InstructionTranslatorBase.ROT_FOUR  sv    HHJJHHJJHHJJHHJJ		!		!		!		!r   c                    |                                  }|                     |           |                     |           d S r   r  )r   r   r  s      r   r  z!InstructionTranslatorBase.DUP_TOP  s1    HHJJ		!		!r   c                    |                                  }|                                  }|                     |           |                     |           |                     |           |                     |           d S r   r  r  s       r   DUP_TOP_TWOz%InstructionTranslatorBase.DUP_TOP_TWO  s\    HHJJHHJJ		!		!		!		!r   r  flagc                J   |dk    r0t          j        | t                                        | |gi           S |dk    r0t          j        | t                                        | |gi           S |dk    r0t          j        | t
                                        | |gi           S |S )Nr   r  r  )rb   r  r   r   r  ascii)r   r  r  s      r   _convert_valuez(InstructionTranslatorBase._convert_value  s    199"(s33AA$QSTTTQYY"(t44BB4%RTUUUQYY"(u55CCD5'SUVVVr   fmt_specr  c                   |                                  }t          |t                    r@ddlm}m}  |j         |||          |j                  }|                     |           d S | 	                    ||dz            }t          j        | d|                                z   dz             }|                     t          t          j                  ||gi            d S )Nr   )LazySymNodeFormatStringrw   r  r  z{:})r  rX  r   torch._dynamo.variables.lazyr  rw   r|  r  rI  r  rb   r  r  r   rf   r   format)r   r  r  r  r  rw   fmt_vars          r   _format_valuez'InstructionTranslatorBase._format_value  s   

e_-- 
	       
 /'.''x88  E IIeF##E54<88!'$44666<
 
 	?3:66%8H"MMMMMr   c                    |j         }|J |dz  dk    r|                                 }nt          j        | d          }|                     ||          S )Nr  r   )rD  r  rb   r  r  )r   r   r  r  s       r   FORMAT_VALUEz&InstructionTranslatorBase.FORMAT_VALUE  s[       DLT!!xxzzHH&,T266H!!(E222r   c           
        g }g }i }|j         J |                     |j                   D ]M}|                                r+|                    d           |                    |           Bt	          |t
          j                  r|                    |j                   |                    |j	                   t          |                                          t          |j                                                  z  r-t          dd| d| d|j         dg t          j                   |                    |j                   !t          dt#          |          d	g t          j                   O|                     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   )rD  rJ  r  r   rX  r   StringFormatVariableformat_stringr  sym_argsr  rX  
sym_kwargsr:   r   r  r  r   rI  r|  r  )r   r   format_string_partsr   r  parts         r   BUILD_STRINGz&InstructionTranslatorBase.BUILD_STRING  s   )+&(-/x###IIdh'' 	 	D&&(( #**4000D!!!!D)"@AA #**4+=>>>DM***v{{}}%%DO,@,@,B,B(C(CC ! ; !D8K  !D  !DW]  !D  !Drv  sB  !D  !D$W= 1 <=	    do....5II n9-89	     			*11+,,dF 	
 	
 	
 	
 	
r   c                    |j         dk    s|j         dk    sJ |j         dk    rd}nd}t          d|          }|                     |           d S )Nr   r   iszis notr  rA  )r~  r'   r  )r   r   
new_argvalnew_insts       r   IS_OPzInstructionTranslatorBase.IS_OP9  sd    {a4;!#3#3#33;!JJ!J%l:FFF!!!!!r   c           	     :   |j         dk    s|j         dk    sJ |                     d          \  }}|j         }	 |                     |                    | d|gi                      n# t          j        t          f$ r}t          |t                    r|j        r |	                                 |                     | 
                    t          j        | t                    ||gi                      Y d }~nd }~ww xY w|dk    r|                     |           d S d S )Nr   r   r  __contains__)r~  rJ  rI  r  r   ObservedTypeErrorr;   rX  r*  r1  r  rb   r  rC   	UNARY_NOT)r   r   leftrightr  r6  s         r   CONTAINS_OPz%InstructionTranslatorBase.CONTAINS_OPB  sS   {a4;!#3#3#33iille[	IIe''ntfbIIJJJJ ! 
 	 	 	 $,, )? &&(((II00#)$0IJJ5M        	( 77NN4      7s   ,A& &C;<A5C66C;c                   |                                  }|j        dk    sJ |j        J | j        |j                  }t	          |t
                    sJ |                                sJ |                    | d|gi            d S )Nr   r  )r  r~  rD  r  rX  r{   r  r  r  s       r   r  z%InstructionTranslatorBase.LIST_EXTEND_  s    HHJJ{Qx###j$(##|,,,,,~~hR00000r   c                    |                      t          j        | t                                        | |                                 gi                      d S r   )rI  rb   r  r5  r   r  r  s     r   LIST_TO_TUPLEz'InstructionTranslatorBase.LIST_TO_TUPLEh  sO    		!$..<<TDHHJJ<QSTT	
 	
 	
 	
 	
r   c                   | j         d         }|                     |          sJ |j        t          u rt	          j        | t                                        | t	          j        | d          gi           }|                    | t	          j        | d          |           |                    | t	          j        | d          |           || j         d<   d S d S )Nr  zgenerator raised StopIterationrv  r  )	r  r  r  r  rb   r  r  r   call_setattr)r   r   rj  new_vals       r   STOPITERATION_ERRORz-InstructionTranslatorBase.STOPITERATION_ERRORm  s     jn))#.....<=((%+D,??MM &t-MNNO G
   '<T='Q'QSVWWW  '<T;'O'OQTUUU$DJrNNN )(r   c                0   |                                  }|j        dk    sJ |j        J | j        |j                                                  }t          |t                    sJ |                                sJ |                    | d|gi            d S r  )	r  r~  rD  r  r  rX  rn   r  r  r  s       r   
DICT_MERGEz$InstructionTranslatorBase.DICT_MERGE  s    HHJJ{Qx###j$(#++--#011111~~hR00000r   c                .    |                                   d S r   r5  r  s     r   	GEN_STARTz#InstructionTranslatorBase.GEN_START  r  r   c           	     0   | j         d         }|                                rI|                     t          j        | t          |                                                               d S |                     |                    | dg i                      d S )Nr  r  )r  r  rI  rb   r  r   r  r  r  s      r   GET_LENz!InstructionTranslatorBase.GET_LEN  s    jn!!## 	@IIo+D#c6L6L6N6N2O2OPPQQQQQIIcoodIr2>>?????r   c                    | j         d         }|                     |                     t          j        | t
                    |gi                      dS )zr
        If STACK[-1] is an instance of collections.abc.Mapping, push True.
        Otherwise, push False
        r  N)r  rI  r  rb   r  rD   r  s      r   MATCH_MAPPINGz'InstructionTranslatorBase.MATCH_MAPPING  sZ    
 jn		,,%dO<< 	
 	
 	
 	
 	
r   c                    | j         d         }|                     |                     t          j        | t
                    |gi                      d S Nr  )r  rI  r  rb   r  rG   r  s      r   MATCH_SEQUENCEz(InstructionTranslatorBase.MATCH_SEQUENCE  sY    jn		,,%d,?@@ 	
 	
 	
 	
 	
r   c           	        |                      d          \  }}}t          j        | |j                  }|                     |                     t          j        | t                    ||||gi                      t          j        dk     rK| j	        d         }|                     t          j        | t          |t                                          d S d S )Nr  r:  r  )rJ  rb   r  rD  rI  r  rE   r\  r  r  r]   rh   )r   r   subjectr  namesrD  r  s          r   MATCH_CLASSz%InstructionTranslatorBase.MATCH_CLASS  s    "iille#D$(33		,,%d,<==#sE* 	
 	
 	
 g%%*R.CIIo+DfSBR6S6S2STTUUUUU &%r   c           	        | j         d         }| j         d         }t          |t                    sJ |                     |                     t          j        | t                    ||gi                      t          j	        dk     rK| j         d         }|                     t          j        | t          |t                                          d S d S )Nr  r  r:  )r  rX  r}   rI  r  rb   r  rF   r\  r  r]   rh   )r   r   rX  r<  r  s        r   
MATCH_KEYSz$InstructionTranslatorBase.MATCH_KEYS  s    z"~jn$.....		,,%dO<<sDk2 	
 	
 	
 g%%*R.CIIo+DfSBR6S6S2STTUUUUU &%r   c                V    |                      |                     d                     d S )Nr  rI  r  r  s     r   LOAD_ASSERTION_ERRORz.InstructionTranslatorBase.LOAD_ASSERTION_ERROR  s)    		$//0@AABBBBBr   c                V    |                      |                     d                     d S )N__build_class__r&  r  s     r   LOAD_BUILD_CLASSz*InstructionTranslatorBase.LOAD_BUILD_CLASS  s)    		$//0ABBCCCCCr   z`Encountered graph break when attempting to trace BINARY_SUBSCR: a binary subscript, e.g. x[attr]c                j    |j         dk    r|                     |           d| _        d S | j        rJ d S )Nr   F)rD  append_prefix_instr  r  s     r   RESUMEz InstructionTranslatorBase.RESUME  sD    8q==##D)))&+D###......r   c                J    |j         J t          |j                  | |          S r   )rD  _binary_op_lookupr  s     r   	BINARY_OPz#InstructionTranslatorBase.BINARY_OP  s'    8'''$TX.tT:::r   c                    d S r   r   r  s     r   r?  z!InstructionTranslatorBase.PRECALL	      r   c                    | j         d         |j                 }t          |t                    sJ |D ]}t          |t                    sJ | j        J t          j        |          | _        d S )N	co_constsrh  )r  rD  rX  r5  r   rG  rh   r|  )r   r   rG  r   s       r   r@  z"InstructionTranslatorBase.KW_NAMES  s{    $[1$(;(E***** 	) 	)DdC((((((}$$$(/h???r   c                H    |                      t                                 d S r   )rI  r   r  s     r   r|  z#InstructionTranslatorBase.PUSH_NULL  s    		,..!!!!!r   call_kwc                   |rft           j        dk    sJ |                                 }t          |t                    r|                                sJ |                                }n| j        r| j        j        nd}|j	        J | 
                    |j	        dz             }t           j        dk    r/|d         }t          |d         t                    rg n|d         g}n7t          |d         t                    r|d         }g }n|d         }|d         g}|rs||dt          |                    z   }|t          |           d          }t          t          ||                    }t          |          t          |          k    sJ n||dd          z   }i }	 |                     |||           d | _        d S # d | _        w xY w)Nr  r   r  r   r   )r\  r  r  rX  r}   r  r  rG  r  rD  rJ  r   r   r  r/  r   )	r   r   r6  rG  contentsrC  r   r1  r  s	            r   _callzInstructionTranslatorBase._call  s     	D#w....xxzzHh66X8;V;V;X;XXXX2244HH.2mCt}**Hx###99TX\**w&&!B#HQK>>Q22Xa[MDD(1+|44 %a[a[ } 	(1H~#566D"CMM>#3#34K#h4455Fv;;#h--/////(122,&DF	! r4000 DMMMDDM    s   F? ?	GzTEncountered graph break when attempting to trace CALL: a function call, e.g. f(x, y)c                0    |                      |           d S r   r9  r  s     r   r>  zInstructionTranslatorBase.CALLF  s    
 	

4r   c                d    |j         J |                     | j        |j                              d S r   )rD  rI  r  r  s     r   COPYzInstructionTranslatorBase.COPYM  s3    x###		$*dhY'(((((r   c                    |j         J | j        |j                   | j        d         c| j        d<   | j        |j          <   d S r  )rD  r  r  s     r   SWAPzInstructionTranslatorBase.SWAPQ  sE    x###04
DH90EtzRT~-
2
DH9---r   c                    d S r   r   r  s     r   CACHEzInstructionTranslatorBase.CACHE]  r2  r   c                0    |                      |           d S r   r  r  s     r   BEFORE_WITHz%InstructionTranslatorBase.BEFORE_WITH`  r  r   ry  8ContextWrappingVariable | GenericContextWrappingVariablec           	        t          |t                    r.|                                s| j                            |           t
          j        dk    rO|j        r(| j        r|j        j	        | j        d         j	        urd }n'| j
        j        J | j
        j        j	        }n|j	        }|rt          | t                    st          j        r=| j                            t          ||t          | j                  |                     n;| j                            t          ||t          | j                                       |                    |           S )Nr:  r  )rX  rj   supports_graph_breaksr/  r   r\  r  rI  r  r-  r  rG  r   r	  r,  r   r  enter)r   ry  r   r-  s       r   	enter_ctxz#InstructionTranslatorBase.enter_ctxc  sV    s:;;	=--//	= 077<<<w&&
 ! D$D%,D4DR4H4OOO,:FFF.<C[F 	X$ 566 X&:T X ''#D&#dj//3GG     ''fc$*oo(V(VWWWyyr   c                b    t          dd|  d|                                  dg d           d S )NzUnsupported context managerz1Attempted SETUP_WITH/BEFORE_WITH/LOAD_SPECIAL on z%Dynamo does not know how to enter a `z` context manager.)z,Avoid using the unsupported context manager.aE  If the context manager seems like it should be supported (e.g. torch.set_grad_enabled), then it may be the case that it was created outside the compiled region, which Dynamo does not support. Supported context managers can cross graph break boundaries only if they are local non-closure variables, or are intermediate values.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  )ry  s    r   unsupported_ctx_graph_breakz5InstructionTranslatorBase.unsupported_ctx_graph_break  sZ    1MMMj@T@T@V@Vjjj  		
 	
 	
 	
 	
 	
r   c                n   |                                  }t          |t          t          f          s|                     |           t          |t          t          f          sJ |                     t          ||j                             |                     |                     ||                     d S r   )	r  rX  ri   rj   rJ  rI  rl   r-  rH  )r   r   ry  s      r   r  z.InstructionTranslatorBase.setup_or_before_with  s    hhjj)+IJ
 
 	2 ,,S111 )+IJ
 
 	
 	
 
 			*3<<===		$..d++,,,,,r   c                L    | j         sJ | j                            |           d S r   )r  r  r   r  s     r   r,  z,InstructionTranslatorBase.append_prefix_inst  s.    &&&&  &&&&&r   c                   t           j        dk    r[| j        sTt          | j        |j                 t                    sJ | j        j        	                                | j        |j        <   d S | 
                    |           d S r  )r\  r  r  rX  r  r~  r   r'  r!  track_cell_newr,  r  s     r   	MAKE_CELLz#InstructionTranslatorBase.MAKE_CELL  s    w&&t/F& d24;?NNNNN(7799  --- ##D)))))r   c                0    |                      |           d S r   r,  r  s     r   COPY_FREE_VARSz(InstructionTranslatorBase.COPY_FREE_VARS      %%%%%r   c                0    |                      |           d S r   rQ  r  s     r   RETURN_GENERATORz*InstructionTranslatorBase.RETURN_GENERATOR  rS  r   c                "   t           j        dk    r|                                  n|                     d           t           j        dk    rB| j        |         }|| j        v r.| j                            |           | xj        dz  c_        d S d S d S )Nr  r  r<  r   )r\  r  r  rJ  r  _comprehension_end_for_ipsdiscard_comprehension_depth)r   r   
current_ips      r   END_FORz!InstructionTranslatorBase.END_FOR  s    w&&HHJJJJIIaLLL w&&d+JT<<</77
CCC))Q.))))	 '&<<r   c                    t          | j                            |j        d           t                    r,t          d|j        d|j         g t          j                   |                     |           d S )Nz)LOAD_FAST_CHECK on uninitialized variablez/Attempted to load uninitialized local variable r  )	r]   r  r  r~  r   r:   r   r  r@  r  s     r   LOAD_FAST_CHECKz)InstructionTranslatorBase.LOAD_FAST_CHECK  s{    $&**4;==|LL 	C[dk[[5)45	    	tr   c                    |j         | j        vr"|                     t                                 n|                     |           t                      | j        |j         <   d S r   )r~  r  rI  r   r@  r  s     r   LOAD_FAST_AND_CLEARz-InstructionTranslatorBase.LOAD_FAST_AND_CLEAR  sV    ;d222IIlnn%%%%NN4   ,8NNT[)))r   c                    |                      t          j        |d                     |j        J |j        dz  r|                     |           d S |                     |j                   d S )Nr  rA  r   )r$  r   r>  rD  r5  r  r~  r  s     r   LOAD_SUPER_ATTRz)InstructionTranslatorBase.LOAD_SUPER_ATTR  sv    ;.tA>>>???x###8a< 	)T"""""OODK(((((r   c                h   |j         dk    r|                     |           d S |j         dk    r|                     |           d S |j         dk    rI|                     t	          |                                                     |                                d S |j         dk    r^|                                                                 }t          j	        t          |                    }|                     |           d S t          dd|j          d|j          dg t          j        	           d S )
Nr  r     r  z Missing CALL_INTRINSIC_1 handlerzCALL_INTRINSIC_1 operand: z,No handler implemented for CALL_INTRINSIC_1  instruction.r  )r~  r  UNARY_POSITIVErI  r}   r  r'  r  r   TypingVariabler
   r:   r   rU  )r   r   r   tvs       r   CALL_INTRINSIC_1z*InstructionTranslatorBase.CALL_INTRINSIC_1  s/   ;!$$T*****[A%%%%%[AIImDHHJJ$H$H$N$NOOPPPPP[A

--//A)'!**55BIIbMMMMM:BT[BBe4;eee6)56	     r   c                   |                                  }|                                  }|j        dk    rZt          |t                    sJ |                    | dt          j        d          |gi            |                     |           d S t          dd|j         d|j         dg t          j
                   d S )	Nr  r  __type_params__z Missing CALL_INTRINSIC_2 handlerzCALL_INTRINSIC_2 operand: z,No handler implemented for CALL_INTRINSIC_2 rd  r  )r  r~  rX  ro   r  rh   r|  rI  r:   r   rU  )r   r   arg2arg1s       r   CALL_INTRINSIC_2z*InstructionTranslatorBase.CALL_INTRINSIC_2  s    xxzzxxzz;! d$<=====!():;;TB	   IIdOOOOO:BT[BBe4;eee6)56	     r   c                    |                                  }|                                   |                     |           d S r   r  r  s      r   END_SENDz"InstructionTranslatorBase.END_SEND  s/    hhjj


		#r   zpEncountered graph break when attempting to trace CALL_KW: a function call with keyword arguments, e.g. f(x=True)c                4    |                      |d           d S )NT)r6  r;  r  s     r   CALL_KWz!InstructionTranslatorBase.CALL_KW  s      	

4
&&&&&r   c                &    | j         j        dv sJ d S )N)POP_JUMP_IF_TRUEPOP_JUMP_IF_FALSEr	  )r  r   r  s     r   r  z!InstructionTranslatorBase.TO_BOOL$  s0     $+ 0
 
 
 
 
 
 
r   c                    |j         }|J |                                 }t          |t                    sJ |                                 }|dz  rjt          j        dk    sJ |                    | t          j        | d          gi           }|	                    | dt          j        d          |gi            n|dz  r||_        n|dz  rt          |t                    sJ |j        }t          t!          t#          |d d d         |dd d         d	
                    t%                                }|                    |                               d|           n|dz  r||_        n|dz  r||_        |                     |           d S )N   r  r   r  r   r  r  r  Tr  r  )rD  r  rX  rr   r\  r  r   rb   r  r  rh   r|  r  r}   r  rn   r  r/  ra   r  r  r  r  rI  )r   r   r  rC  r?  r  r  s          r   SET_FUNCTION_ATTRIBUTEz0InstructionTranslatorBase.SET_FUNCTION_ATTRIBUTE.  s      XXZZ"899999xxzz4< 	#w.... %%d_-B4-K-K,LbQQDNN!():;;TB	    T\ 	BJJT\ 	dM22222 JE#SsssU14a4[>>>??.00  C NN4  ((!3    T\ 	 BMMT\ 	BK		"r   c                    |j         J |                     |                     |                                 |j                              d S r   )rD  rI  r  r  r  s     r   CONVERT_VALUEz'InstructionTranslatorBase.CONVERT_VALUEV  sB    x###		$%%dhhjj$(;;<<<<<r   c                X    |                      t          j        | d          d           d S )Nr   r   )r  rb   r  r  s     r   FORMAT_SIMPLEz'InstructionTranslatorBase.FORMAT_SIMPLEZ  s+    ?0r::A>>>>>r   c                V    |                      |                                 d           d S r   )r  r  r  s     r   FORMAT_WITH_SPECz*InstructionTranslatorBase.FORMAT_WITH_SPEC]  s&    488::q)))))r   )	__enter____exit__
__aenter__	__aexit__c                   t          |j        t                    s
J d            | j        |j                 }|dv r|                                 }t          |t
          t          f          s|                     |           t          |t
          t          f          sJ |dk    r9|                     t          |                     | 
                    |           d S |                     t          |d                      | 
                    |           d S |                     |           |                                 }|                     |           | 
                    |           d S )Nz*expected LOAD_SPECIAL arg to be set to int)r~  r  r~  )rX  rD  r   _load_special_namesr  ri   rj   rJ  rI  rk   r|  rl   r  )r   r   r?  ry  r<  s        r   LOAD_SPECIALz&InstructionTranslatorBase.LOAD_SPECIALo  sh   $(C((VV*VVV('1,,,((**C-/MN  6 00555 -/MN     {""		3C88999t$$$$$ 		23==>>>t$$$$$ OOD!!!((**CIIcNNNNN4     r   c                `    |                      t          j        | |j                             d S r   )rI  rb   r  r~  r  s     r   LOAD_SMALL_INTz(InstructionTranslatorBase.LOAD_SMALL_INT  s)    		/'dk::;;;;;r   )r  r  r5  r  anyc                    t          |j        t                    s
J d            |                     |                     | j        |j                                      d S )Nz2expected LOAD_COMMON_CONSTANT arg to be set to int)rX  rD  r   rI  r  _common_constantsr  s     r   LOAD_COMMON_CONSTANTz.InstructionTranslatorBase.LOAD_COMMON_CONSTANT  s]    $(C(( 	
 	
@	
 	
( 			$//0Ftx0PQQRRRRRr   c                T    | j                                         dk    r
d | _        dS dS )Nr   c                     dS NTr   r   r   r   r   z>InstructionTranslatorBase.is_non_empty_graph.<locals>.<lambda>  s    d r   TF)r'  count_callsr  r   s    r   r  z,InstructionTranslatorBase.is_non_empty_graph  s/    ;""$$q((&2lD#4ur   additional_stack_frameslist[Any] | Nonec           
         |g }d                     t          j        |                                 gt	          t          |                    z                       S )Nr   )r  	tracebackformat_listr  r   r   )r   r  s     r   format_frame_summaryz.InstructionTranslatorBase.format_frame_summary  s^     #*&(#ww!##%%&h7N.O.O)P)PP 
 
 	
r   traceback.FrameSummaryc           	         t          j        t          | j        dd          | j        t          | j        dd          d          S )Nr  	<unknown>r  F)lookup_line)r  FrameSummaryr  r  r   r   s    r   r  z'InstructionTranslatorBase.frame_summary  sF    %DK<<KDKK88	
 
 
 	
r   c                    t          | j        dd          }t          j        d          }|                    |          d uS )Nr  r  z.*torch/nn/modules.*)r  r  recompilematch)r   r   nn_modules_patterns      r   is_co_filename_from_nn_modulesz8InstructionTranslatorBase.is_co_filename_from_nn_modules  s?    4;{CCZ(?@@!''11==r   r  c                    | j                             |t          j        |                    }t	          t          |                              t          j                             |S r   )	r'  install_global_by_idrl  rm  r?   rP   r  r>   WEAKREF_ALIVE)r   r  r  global_names       r   store_global_weakref_by_idz4InstructionTranslatorBase.store_global_weakref_by_id  sY    k66vw{5?Q?QRR,,778RSS	
 	
 	
 r   FakeTensorMode | Nonec                $    | j         j        j        S r   )r'  tracing_context	fake_moder   s    r   r  z#InstructionTranslatorBase.fake_mode  s    {*44r   check_fn!Callable[[VariableTracker], bool]c              #  V   K   | j         }|| _         	 dV  || _         dS # || _         w xY w)zx
        Strict mode is enabled on a per-VariableTracker level depending on the return value of check_fn(node).
        N)r  )r   r  priors      r   strict_translation_modez1InstructionTranslatorBase.strict_translation_mode  sH       % (	*EEE$)D!!!ED!))))s    	(r   c                    | j         J | j         dk    sJ | j                            | j        j        | j        | j         dz
  | j        | j         dz
                     S ru  )r   r#  r   r  r  r   r}  r   s    r   r.  z#InstructionTranslatorBase.speculate  sk    '333'!++++#((K#K$q(d6:;	
 
 	
r   r   fallback_linenotuple[str, int]c                "    ||dk     r||fS ||fS r   r   )r   r   r   r  s       r   _make_frame_locz)InstructionTranslatorBase._make_frame_loc  s'     >VaZZo..&!!r   	frame_loctuple[tuple[str, int], ...]c                <   g }t           j        rW| j        }|N|                     |j        j        |j        |j        j                  }|                    |           |j        }|N|	                                 |                    |           t          |          S r   )r   r	  r  r  r  r  r   co_firstlinenor   reverser5  )r   r  frame_loc_chain_list
current_txparent_frame_locs        r   _get_frame_loc_chainz.InstructionTranslatorBase._get_frame_loc_chain  s     79% 		/;?;J(#'#7#7%1%%4$ $ 
 %++,<==='.
 ( 	$$&&&##I...)***r   r  r   r   )Unsupported | UserError | StepUnsupportedc                   |j         rd S t          |dd           }|#t          j        j                                        }	 t          j        r8| j        1| 	                    | j
        j        | j        | j
        j                  }n-| 	                    |d         j        |d         j        d          }n # t          $ r |d         |d         f}Y nw xY w|                     |          }d}t          j        r6t%                      }d                    t)          j        |                    }n t%                      |z   }t-          |          }d                    t)          j        |                    }	|t/          ||          }d|d          d|d	          d
| dt          j        r| d|	 dz  nt1          |	          z  t          j                            dd fd           t7          |t8                    r|j        nt=          |          }
t>                               tB          j"                  ritF          sbtI          j%        |
|f          rLt          j        r%dz  d                    | j&                  dz   z  t>          '                               n)t>          '                    d|d         |d	         |           d|_         d S )Nr,  r  r   r  r  r   zGraph break in user code at 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                     dddS )Ndynamo_graph_break_reasonstring)r   encodingr   r   r   r   r   z;InstructionTranslatorBase.log_graph_break.<locals>.<lambda>C  s    3$! ! r   c                 4      dt          j                     S )Nr   )r  
format_exc)user_stack_traces   r   r   z;InstructionTranslatorBase.log_graph_break.<locals>.<lambda>G  s    "2NNi6J6L6LNN r   )metadata_fn
payload_fnz4
Most recent bytecode instructions traced (max 20):
r   zmGraph break (user stack suppressed due to duplicate graph break) in user code at %s:%s
Graph Break Reason: %sT)(loggedr  r  r  r   r  r   r	  r  r  r  r  r   r  r   
IndexErrorr  r  r7   r  r  r  r5   r3   r   _loggingtrace_structuredrX  r;   r  r  graph_break_logisEnabledForr   DEBUGexplainr\   r  r  r  )r   r  r   r   
user_stackr  frame_loc_chainstack_above_dynamo_formattedstack_above_dynamouser_stack_formattedr  r  s              @r   r  z)InstructionTranslatorBase.log_graph_break  sX    : 	FS,55
5CCEEJ	) dk.E 00K+KK. 		 !00rN+rN) 	
  	 	 	 ]+-.III	 33I>>')$> 	<!7!9!9+-77%&899, ,(( 011J>J/
;;J!wwy'<Z'H'HII ?6sFCCF'9Q< ' ')A, ' '#)' ' ' 	 > 	:/ L L'L L L $8 9 99''  ONNN 	( 	
 	
 	
 ",C!=!=L#++499((77		 03Wo4NOO	 ~ Q L  !DIId.H$I$ID$PP !!     !! A!!	   


s   A1B4 4CCr  types.CodeTypeUnsupported | UserErrorc                h    t          ddt          |            dg t          j        |d           d S )Nzgraph break in loopzframe skipped: ztorch.compile detected a graph break in a for/while loop. Skipping the frame and falling back to eager, as graph breaks in loops are not supported.T)r  r  r  r  r)  r*  )r:   r6   r   r2  )r  r   s     r   r  z0InstructionTranslatorBase.raise_loop_graph_breakk  sT     	)?&7&=&=??hD%CD	
 	
 	
 	
 	
 	
r   r}  r:  rm  ro  r  exportr#  r   distributed_stateDistributedState | Noner  tuple[types.CellType] | Noner  dict[Instruction, int] | Nonec                4   t                                                       || _        || _        || _        || _        || _        |	| _        |
| _        d | _	        g | _
        d| _        d | _        t          d          | _        d| _        g | _        g | _        d| _        d | _        d| _        g | _        || _        t/          d          | _        d| _        t5                      | _        || _        ||nt;          | j                  | _        || _        || _         || _!        || _"        || _#        || _$        |$tJ          j&        rtO          |||          | _(        nd | _(        i | _)        i | _*        || _+        d| _,        d| _-        d| _.        d | _/        d | _0        d| _1        d | _2        d| _3        g | _4        || _5        d	d
l6m7}m8}m9}m:} |j;        ||z  |z  |z  z  r"| <                    t{          d                      || _>        d| _?        d gt          |jA                  z  | _B        t          D                    t          jF                  | _G        t          D                    t          jF                  | _I        t          jK        |jL        |           d S )Nr   r  Tr  r  )maxlen)r  r  r  Fr   )CO_ASYNC_GENERATORCO_COROUTINECO_GENERATORCO_ITERABLE_COROUTINE)Mr\  r]  r#  r  r'  r  r  r  r  r  r  r   r  r'   r  r-  r  r/  r   rG  r  r  r  r   r  rY  r  rW  r}  r   r  r:  rm  ro  r  r  r  r   replay_record_enabledrI   r  nn_module_stack	num_callsr  r0  r   r  r  r  r  r  r  r  r  resume_executionr  r  r  r  co_flagsrI  rf   r  r  r   r4  ri  r  r  r   r  r  r  r  r  	lazycacher  )r   r'  r}  r:  rm  ro  r  r  r  r  r  r  r  r  r#  r  r  r  r  r  r  r  r  r  r_  s                           r   r]  z"InstructionTranslatorBase.__init__y  s   0 	.!2 . 0-J*%:" -1),.
#$ #5e#<#< (,%UW,"&(%*"%5%5%5"$%!47EE' 0<*GGD<M0N0N 	  	 *3*4,8&, 6#?!2W<" " "D "&D BD)+ $)!*/'#'  $$(!&+#	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 ?<'*??BTT
 	- IIod++,,,().&P
 !!P"
 ++GM:: 	* 9I8U8UM9
 9
( 	F.	:::::r   r   r  )r   r   )rC  rb   r   r  r  r  r   r   )rC  ro   r   r  r  r  r   rb   )rC  ro   r   r  r  r  r   r   r   )r   r  r   r   r  )r   r   r   r   r   r,   r   r   )r   r,   )r  r,   r   r   r   r   )rj  rb   r   r   )r/  r  r   r   r   rb   )r  r   r   r  )rL  r   r   r   )r   r,   r   rb   )rw  r   r   rO   )r   r   r  r   r  r   r   r   )r~  r   r   rb   )r   r  r   r   )rj  rb   r   rb   )r   r   r   r   )rj  rb   r   r   )rj  rb   r   r  )r   r   r   r   )r?  r   r   r   )r<  rb   r   r   r   r   )r   r,   r  r   r   r  )r  r   rQ  r,   r  rB   rR  rS  r  r1   rT  r   rU  r   r   rV  r   r,   r  rx  r   r  )rR  rS  r  r   r  r1   r   r   )r<  rb   r  rb   r   r   )r   r,   r  r  r   r   )r  rb   r  r   r   rb   )r  rb   r  r   r   r   )F)r   r,   r6  r   r   r   )ry  rD  r   r,   r   rb   )ry  rb   r   r   )r  r  r   r   )r   r  )r  r   r  r   r   r   r   r  )r  r  r   r   )r   r   )r   r   r   r  r  r   r   r  )r  r  r   r  )r  r   r   r   r   r  r   r   )r  r  r   r  r   r   )NNN)(r'  rA   r}  r  r:  r   rm  r   ro  r   r  r   r  r  r  r  r  r   r  r   r  r  r  r   r  r   r#  r   r  rc  r  r  r  r  r  r  r  r  r   r   (  r   r   r   r   r  r  r  r  r  r  r   r  r  r  r  r  r  r\  r  r  propertyr  r  r  r-  rI  r1  r  rJ  r@  rE  rI  rM  rK  rf  LOAD_CLOSURErj  rz  rt  rQ  cached_propertyrz  r}  r  r   rv  r  r  r  EAGER_IMPORT_NAMEr  r  rq  r  JUMP_FORWARDJUMP_ABSOLUTEr  r{  not_rt  r|  rs  JUMP_IF_FALSE_OR_POPJUMP_IF_TRUE_OR_POPr  r  r  r  r  r  r  r  r  rn  r  r  r  r  r  r  r  r  r  r  r  r"  rM  r$  r-  r2  r7  r9  r5  r>  r  rB  rE  rI  rG  staticmethodrP  r  r  r  r  r  r  r  r  r  r  r   r  r{   r  r  BUILD_TUPLE_UNPACK_WITH_CALLr  r  BUILD_MAP_UNPACK_WITH_CALLr  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r  r  DICT_UPDATEr  r  r  r  r"  r$  r'  r*  rS  posre  negUNARY_NEGATIVEr	  invertUNARY_INVERTpowBINARY_POWERmulBINARY_MULTIPLYmatmulBINARY_MATRIX_MULTIPLYfloordivBINARY_FLOOR_DIVIDEtruedivBINARY_TRUE_DIVIDErT  BINARY_MODULO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_MODULOINPLACE_REMAINDERiaddINPLACE_ADDisubINPLACE_SUBTRACTilshiftINPLACE_LSHIFTirshiftINPLACE_RSHIFTiandINPLACE_ANDixorINPLACE_XORior
INPLACE_ORr-  r0  r?  r@  r|  r9  r>  r=  r?  r  JUMP_BACKWARD_NO_INTERRUPTPOP_JUMP_FORWARD_IF_TRUEPOP_JUMP_BACKWARD_IF_TRUEPOP_JUMP_FORWARD_IF_FALSEPOP_JUMP_BACKWARD_IF_FALSErA  rC  rH  rJ  r  r,  rO  rR  rU  r[  r]  r_  ra  rh  rm  ro  rq  r  rw  ry  r{  r}  LOAD_FAST_BORROWr  POP_ITERr  r  r  r  r  r  r  r  r  r  r  
contextlibcontextmanagerr  r.  r  r  r  r  r]  r`  ra  s   @r   r   r     sg         ////0000====....CCCC    ####$$$$""""&&&&KKK%%%%########0000    ++++>>>>,,,, !   EEEE""""%%%%. . . .! ! ! !F0 0 0 00 0 0 0' ' ' '

 
 
 
8 8 8 8,4 4 4 4U U U Un n n n n   	W 	W 	W 	Wn n n n` 7""*	+ *	+ *	+ *	+ *	+\	 	 	 	 ; ; ; X;A A A AF2 2 2 2M= M= M= M=^             ; ; ; ;!+ !+ !+ !+FV V V V2 2 2 2. . . .)
 )
 )
 )
V/ / / / L	 	 	 	* * * *J J J J* J J J J! ! ! !E E E E( # # # \#,
2 
2 
2 
2   :; ; ; ;| $% % % % 4 4 4 \4,> > > >4 4 4 4 LM$X]E::#|HNE::'<t<<&,x~t<<U U U U
U U U U
   ( ( ( (U U U U   *   
 
 
 
(.
 .
 .
 .
`& & & &P   0 0 0 0.. . . .$) $) $) $)LA' A' A' A'F< < < <:$ $ $ $*Q Q Q QfL L L L   P P P PP P P P    ) ) )	 )
    :; :; :;	 :;x  A  
	- 	- 	- 
	-G G G G          "
 
 
 

   % % % %   A  
 
 
	 

 
 
 
8 8 8 8    \>x% x% x% x%tg% g% g% g%R A? A? A? \A?F
 
 
 
&  ~  = = =	 =8 8 8 84 4 4 4(( ( ( (1 1 1 1   6 6 6 6
 
 
 
 @L @ @ @ @ @ 8 8 8 8 $6 2 2 2 2
 
 
 
$ "2
 
 
 
 9 9 9 9. . . .1 1 1 1   / / / /b   :   0  N  
 
 
 
4 4 4 4
               
      N N N N,3 3 3 3
 
 
 
B" " " "! ! ! !:1 1 1 1
 
 
 

% % % %$1 1 1 1 K   @ @ @ @
 
 
 

 
 
 
V V V V V V V V"C C C CD D D D Xhl++NXhl++N''I8HO,,L8HL))Lhx|,,O%Xho66"(8#455!("233HX\**Mx--(,''Jhx|,,O..u   hx  " "M HX_--MHX_--M(-((J&&I(,''JHX]++Mx..&hx'788#8H$677"(8#455Xhm,,N //(8=))Kx..Xh.//NXh.//N(8=))K(8=))K(,''J/ / / / 7""	; 	; 	; 	;   @ @ @ @" " " "-! -! -! -! -!^  i    	 ) ) ) )V V V V M!%+|HNEBB ,X^U C C ,X]E B B!-hmU!C!C   ( ( ( (# # # #J 
 
 
 \
 - - - -' ' ' '	* 	* 	* 	*& & & && & & &/ / / /   ; ; ; ;) ) ) )   .   ,     A  
' ' ' 
'
 
 
 
& & & &P= = = =? ? ? ?* * * * !IH
! ! ! !8< < < <S S S S    ;?	
 	
 	
 	
 	

 
 
 
> > > >
    5 5 5 X5 * * * *
 
 
 
" " " "+ + + +(l l l l\ 
 
 
 \
@ 15)-15-@; @; @; @; @; @; @; @; @; @; @;r   )	metaclassc                       e Zd Zed,d            Zej        d-d            Zd. fd Zd/d!Z	d0d$Z
d1d%Zd/d&Zd2d)Zd2d*Zd2d+Z xZS )3rG  r   c                     t           j        S r   )tlsr  r   r   r   r  z InstructionTranslator.current_tx  s
    ~r   r   c              #     K   t          t          dd           }| t          _        	 d V  |t          _        d S # |t          _        w xY w)Nr  )r  rB  r  )r   r  s     r   set_current_txz$InstructionTranslator.set_current_tx  sJ      \400	#EEE"CNNNUCN""""s	   8 Ar}  r  r  r  r:  r   rm  ro  r  tuple[Any, ...] | Nonetorch_function_mode_stackr  compiler_fnr0  r   r  export_constraintsframe_stater#  r   r  rc  r  r  r  r  r   c                J    t                      t          j        d|j         d|d          d|d                     t	                                          t          ||	| ||||||||
|          ||||||i i d d ||d||||           |                                  t          | j	        j
                  5  |                                 5  |
| _        || _        | j        r| j        s
J d	            i | _        t          |                                           }t#          j        |                              d
d           }|                                D ]q\  }}||vrhd }|r5t+          |                    |i                                                     }t-          j        |t1          |d|                    }|| j        |<   r| j	        j        }|                                 D ]}||v rZ||         }t1          |dd          }t-          j        ||          }|                                }|                    ||           n|                                }||_        || j        |<   |J t=          |                                 |          D ]\  }}tA          |          }t1          |d          }	 t-          j        |j!        |          }n## tD          $ r tG          j$                    }Y nw xY w|%                    |||          }||_        || j        |<   tM          |          | _'        tQ                      | _)        |r)tF          j        *                    | j                  | _        d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nztorchdynamo start tracing r  r  r   r  )local_scoper  r  rF  r0  r  r   )r'  r}  r:  rm  ro  r  r  r  r  r  r  r  r  r  r#  r  r  r  z4Export without one graph - something has gone wrong.dynamismT)is_inputrL  )rM  is_derefed_cell_contents)rN  )+r   r   INFOr  r\  r]  rA   _throw_if_in_functorchr   r'  r  rD  r0  r  r  r  r  r0   rk  r  r  	frozensetrw   r|  rR   r!  r  rN  rd  re  r/  r  rQ   cell_contentsr  r   DeletedVariabletrack_cell_existingr   r  r   r  realize_all)r   r}  r  r:  rm  ro  r  rF  r  rG  r0  r  rH  rI  r#  r  r  r  r  rL  r   r  local_dynamismrL  r!  contents_sourcerD  cell_varrC  cell_sourcer_  s                                 r   r]  zInstructionTranslator.__init__
  s   ( 	Lxxx,}:UxxXdeuXvxx	
 	
 	
 	"$&*C#   &!%*."&+%/A 	 !	
 !	
 !	
F 	##%%% T[011 d	 d	43F3F3H3H d	 d	#,DN DK{ ~  J ~ $&D *-d.D.D.F.F*G*G#/77;;JMMH'~~// 5 5e000%)N S)28<<b3I3I3O3O3Q3Q)R)R-4# %)%3   C 25D(.  ;3L %6 %68##$TNE* '2!%15' ' 'O
 5H4N5 5L  ,::<<H ++HlCCCC+::<<H&*#-5$T** &&&!$--//7;; 6 6
d-d33"-dT"R"R"R?#6#=*O$ $LL " ? ? ?#,#<#>#>LLL? (;;|  '+#-5$T**1K)2 2D. *=)>)>D&  (1'D'P'P(( ($Ed	 d	 d	 d	 d	 d	 d	 d	 d	 d	 d	 d	 d	 d	 d	 d	 d	 d	 d	 d	 d	 d	 d	 d	 d	 d	 d	 d	 d	 d	 d	 d	 d	s\   7NG.N ;KN K6	3N 5K6	6A>N 4N N	NN	NNNc                V   t           j                            d          }t          j        | j        j        d| j        j                  }t           j        j        	                                }t           j        j        j
        j        t           j        j        j
        j        t           j        j        j
        j        f}|f|                                |v rR||urP|                                j                                        }d| d| d| d}t#          dd|g 	           d S d S d S d S )
NeagerrG  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  _dynamolookup_backendrN  getattr_staticr'  rG  _C
_functorchpeek_interpreter_stackTransformTypeVmapGradJvpr  r   lowerr:   )r   r[  rG  ciforbidden_keysr   rc  s          r   rP  z,InstructionTranslator._throw_if_in_functorch  sF   ,,W55,K#]DK4K
 
 X 7799H-2H-2H-1
 >bffhh.88[PU=U=U6688=&&((DY(,Y Y +/Y Y !%Y Y Y  ?	      >88=U=Ur   r  rT   c                    t          |t                    r| j        |j                 S t          |t                    r| j        |j                 S t          r   )rX  rR   r:  re  rO   rm  r  r=  )r   r  s     r   get_example_valuez'InstructionTranslator.get_example_value  sK    fk** 	4=!233fl++ 	6>&"455r   c                    | j                                         D ]K}t          |t                    r4t	          |                                t          j        j                  r dS LdS r   )	r  r  rX  r   r  r  r  nnModule)r   r   s     r   $symbolic_locals_contain_module_classz:InstructionTranslator.symbolic_locals_contain_module_class  sh    %,,.. 	 	A!566 :$$&&< <  ttur   c                    t          | j                  r^| j        d         x}rQt          |t                    r>t	          |                    |           t                                | j        d<   d S d S d S d S )Nr  r  )r   r  rX  rq   rz   r'  ra   )r   r  s     r   "replace_tos_if_return_is_generatorz8InstructionTranslator.replace_tos_if_return_is_generator  s    
OO	
2&	 3 <==	
 2--d33.00  DJrNNN	 	 	 	 	 	r   r   r,   c                   |                                   | j        J | j        J t                                          d| j        | j        z
             t
          j        s}| j                                        dk    r`| j	        sY| 
                                sE| j        s>| j        s7| j        s0| j        s)t          j        dt#          | j                             d | _         t'                      t(          j        d| j        j         d|j         d           t0                              d           | j                            | t7          d|                                 gd	
          |j        dk    rdnd          }t;          |          dk    sJ |d         j        rJ | j                            |                      ||d         j!                             tD          )Nr  r   zhNo ops traced for the FX graph. `torch.compile` will skip the frame and fall back to eager.
Frame info: ztorchdynamo done tracing r   r  zreturn triggered compilereturn_valueF)r+  r  r   r  )#rp  r   r  r[   r  r   allow_empty_graphsr'  r  r  rn  r  r0  r   r  r   	SkipFramer6   r  r   r   rO  r  r   r  r  r  r@   r  r   ri  r  rP  r  r@  )r   r   r  s      r   _returnzInstructionTranslator._return  s1   //111'333+++''043CC	
 	
 	

 )	''))Q..2 /==?? / K / N	 /
 - / 3 / -@0==@ @  
 $( LM(;MMt{MMM	
 	
 	
 			,---$(K$@$@%!3!3!5!5 6E   !K>99qqq %A %
 %
! ,--2222,Q/@@@@++))$0I!0L0VWW	
 	
 	
 r   c                0    |                      |           d S r   ru  r  s     r   r  z"InstructionTranslator.RETURN_VALUE      Tr   c                0    |                      |           d S r   rw  r  s     r   r  z"InstructionTranslator.RETURN_CONST  rx  r   )r   rG  r  )$r}  r  r  r  r:  r   rm  r   ro  r   r  rE  rF  r   r  r   rG  r   r0  r   r  r   rH  r   rI  r   r#  r   r  rc  r  r  r  r  r   r   r   )r  rT   r   r   r  r  )r   r   r   r  r  r=  r>  rD  r]  rP  rj  rn  rp  ru  r  r  r`  ra  s   @r   rG  rG    s          \ # # # #c c c c c cJ   >      	 	 	 	, , , ,\          r   rG  r:  c           	     p    g | ]3\  }}t          t          d |v r
|dd         nd|dd                    4S )INPLACEr  NBINARY_)r  rG  )r   r   r   s      r   ri  ri    se       
 FA	 	!#v--F122JJ3IVABBZ3I3I	
 	
  r   r'  rA   r  r  get_inline_depthCallable[[], int]c              #    K   t           j        sdV  dS | j        t                      | _        | j                                        }| j                                        }| j                            |j        |j        |j	        t          j                               d}	 dV  d}| j                                        }t          j                    }|r| |            }||j        z
  }	|	|j        z
  }
t          |j        |j        |j        |	|
t'          |j                  ||r|d         nd|r|d         nd|r|d         nd|j        |          }| j                            |           | j                            |	           dS dS dS # | j                                        }t          j                    }|r| |            }||j        z
  }	|	|j        z
  }
t          |j        |j        |j        |	|
t'          |j                  ||r|d         nd|r|d         nd|r|d         nd|j        |          }| j                            |           | j                            |	           w w w xY w)a[  
    Context manager for profiling inline calls.

    Args:
        output: The OutputGraph containing profiler_state
        code: The code object being inlined (for timing metadata)
        get_inline_depth: Callable that returns inline_depth (called after work completes)

    Yields:
        None (profiling happens around the with block)
    NFTr   r   r  )	func_namer   firstlineno
cumtime_ns
tottime_nsbytecode_countr  caller_func_namecaller_filenamecaller_firstlinenois_primitive_call
call_stack)r   dynamo_profilerprofiler_stater   get_current_callerget_call_stackrI  r  r  r  timetime_nsr  start_time_nschild_time_nsr   r  r   r  r   co_coder  record_timingadd_child_time)r'  r  r}  caller_infor  trace_successstack_entrytrace_end_nsr  r  r  timings               r   profile_inline_callr  '  s     " ! $ 3 5 5'::<<K&5577J
d&(;T\^^   M=+//11|~~ 	=[4++--L%(AAJ#k&??J(%/$-'3%%"4<00)3>!HQD2= GA45@#J;q>>d"-"?%  F !//777!00<<<<<)	= 	=44 +//11|~~ 	=[4++--L%(AAJ#k&??J(%/$-'3%%"4<00)3>!HQD2= GA45@#J;q>>d"-"?%  F !//777!00<<<<)	=4s   !F C%I3c                       e Zd ZU dZded<   ded<   ed4d            Zed5d            Zed6d            Z	d7dZ
d8 fdZed9d!            Zd:d"Zd;d$Zd; fd%Zd< fd+Zd=d,Zd=d-Zd>d1Zd= fd2Zd= fd3Z xZS )?r  z Trace and inline a called methodr8  symbolic_resultr   r  r   funcro   r   r  r  r  r   rb   c                    d }t          j        |                                fd          5  |                     |||          }|                                cd d d            S # 1 swxY w Y   d S )Nc                      j         dz   S r  r  )r  s   r   r   z;InliningInstructionTranslator.inline_call.<locals>.<lambda>v  s    F4G!4K r   )r  r'  r  build_inline_tracerinline_call_)r  r  r  r   r  tracers    `    r   r  z)InliningInstructionTranslator.inline_calll  s      M4==??,K,K,K,K
 
 	) 	) ,,VT4HHF&&((		) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)s   ,A((A,/A,trace_rules.SkipResultc                   |                                  r t          dt          |           dg            t          | t                    rt          j        |                                 dd          rtt          j        |                                 dd           }t          dt          |                                           d|                                  d	| d
dg           t          j	        | d          }|j
        rddlm} t          | d          r8t          | j        d          r#| j        j        |u rt          j        dd          S t          | d          r| j        j        nd}d| dg}d|                                 vr|d| ddgz  }t          dd| d|                                  d|                                  d|j         d| d |           |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  _torchdynamo_disableF_torchdynamo_disable_msgz2Skip inlining `torch.compiler.disable()`d functionzSkip inlining function z= since it was wrapped with `torch.compiler.disable` (reason: r  z(Remove the `torch.compiler.disable` callT)is_inlined_callr   )!produce_trampoline_autograd_applyrC  _originz"allowlist in dynamo known functionr   zAvoid calling the function `rT  r\  z:Apply `@torch._dynamo.dont_skip_tracing` to the function `zw` to force tracing into the function. 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.)has_selfr:   r   rX  rt   rN  r^  get_functionr   check_verboseskippedtorch._dynamo.variables.miscr  r4  rC  r  
SkipResultr   get_filenameget_namer   )r  rc  r  r  fn_qualnamer  s         r   check_inlineablez.InliningInstructionTranslator.check_inlineable{  s    ==?? 	6D		k    d011 	g6L!77
 7
 	 (!!##%? C LD--//00Ad6G6G6I6I A A:=A A A ?    *4FFF>  	VVVVVV
 d##DGY// GO'HHH #-?   3:$2E2EM$'..2K>{>>>E  1 1 3 333hQ\ h h h 7	  HS[ S S$--// S S"//11S SCI=S S(^i ( ( (    r   c                	    t          |t          t          t          f          sJ |                                d } j        j        }t          j        s!|r|j	        
                    d           x}r|}|=t                              |          }|j        du sJ t          j        s|r
||j	        <   d }	 |                     ||          }n# t          $ r}t!          dd|                                 d|                                 d|                                j         dd |D              d| 
d	|                                 d
g t(          j        |           Y d }~nd }~ww xY w|J t-          j        |                                          D ]<}	t          |	t2                    s%t!          dd| d|	 dg t(          j                   =j        dv rI|r t          |d         t8          j                  s't!          ddj         d| dj         dg            d}
t<          j        j         j!        "                    d          r)dtG          j$                  #                                 }
tJ          j&        dk    r? j'         j(        d# fd}tR          *                    dtW          |                     tX          *                    d |
|j-                   |rr|d         }d!}t          |t\                    r\|/                                sHta          |1                                t<          j2        j3                  r|4                                }nd}|rt          |tj                    rr j        6                    |j7                  }t          |t<          j8        j9                  r3tu          j;        |          ty          j=        |j>        j?                  d"<   nnt          |t                    rY|jA        }t          |t<          j8        j9                  r3tu          j;        |          ty          j=        |j>        j?                  d"<   t          |t                    rJ t                    r%t           | jE         jF         jG        |          }n$t           | jE         jF         jG        |          }|S )$NFz2failed to bind arguments when attempting to inlinezfunc='z' r   z	; args = c                6    g | ]}|                                 S r   )r  )r   rD  s     r   ri  zEInliningInstructionTranslator.build_inline_tracer.<locals>.<listcomp>  s"    ===3??,,===r   z; kwargs = z4Argument mismatch when attempting to trace function rW  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  )r  r  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   r   c                                          j        j                  } t                                                    }dj         d|  d| S )N)r   zTRACE inlined call z from r   )r  	positionsr   rZ   r  r  )headerr  r  cur_instr  parent_codes     r   get_trace_call_log_strzQInliningInstructionTranslator.build_inline_tracer.<locals>.get_trace_call_log_str  sc    77#-4 8   2+xHHOOQQQT\QQQQ4QQQr   r  zINLINING %s%s, %sTra  r  )HrX  rt   rr   rp   r  r'  r  r   dont_skip_tracingpreviously_inlined_functionsr  r  r  r  	bind_argsr  r:   r  r  r  r   r  r  r  r  rb   r  r  r   r   r  r  	_internal	log_stateis_artifact_enabledro  Bytecoder\  r  r  r  trace_call_logr  r^   r  r   rw   is_realizedr  	peek_typerl  rm  r  r   r  r  r  r#  rl  rm  r0   rk  forward__code__r   r  rs   r-   &InliningGeneratorInstructionTranslatorr  r  r  )r  r  r   r  r  tracing_ctxprevious_result
sub_localsr  r   r  r  arg0should_checkmoduler  r  r  r  s   `               @@@r   r  z1InliningInstructionTranslator.build_inline_tracer  s    $*.
 
 	
 	
 
  $}}m3
 ' 	)K 	)"-"J"N"Nd# #  ) )>2CCDIIF>U****+ H HAG8>
	f==JJ 
	 
	 
	LS S SD4E4E4G4G S S$--//Jh S S=====S SJPS SeSWS`S`SbSbeee&1 	 	 	 	 	 	 	 	 	
	 %%%!2!2!4!455 	 	Aa11 X5T55!55 y9-89	    <999 :Q)LMM : LBdlBBDBB{4<{{{	     >#-AA*MM 	54#,t,,002244Fw&&1H -KR R R R R R R R R   z2H'I'IJJJ		%tVV]CCC  	07DL$ 344 )T=M=M=O=O )dnn..@@ )<<>>DD#(L 0d$455 0#]88IIF!&%(*>?? 0 $K// %01HII.  &CDD 0!ZF!&%(*>?? 0 $K// %01HII. d$899999 	;'4, FF 3'4, F s   :C 
E0B
E++E0c                   | j         }d|_        d|_        | j        }t	          j                    }|j        r|                     |j                  }	 |5  |                                  d d d            n# 1 swxY w Y   n{# t          j
        $ r(}d| d| }t                              |            d }~wt          t          f$ r}d|_         d }~wt           $ r t                              d|            w xY w| j        |_        d|_        n# | j        |_        d|_        w xY w| j        j        rt)          j        d           S | j        J | j        |j        u r|j                            | j                   |xj        | j        z  c_        t                              d|           | j        j        j                            |           t<          j        stA          | tB                    r| j"        rtG          |          rttA          | tB                    r_| j$        rXtA          | tB                    sJ g }| j        %                                s| j        g}t          j&        tN          | |           d S | j        S tG          |          rTtA          | tB                    sJ | j        %                                sJ tQ          | j)        tU                                S | j        S )	NFTz!Observed exception DURING INLING z : zFAILED INLINING %szDONE INLINING %sr  r  )+r  r  r  r  r=  nullcontextr  r  r-  r   r   r  r  r;   r<   r*  r  r   r'  r(  rh   r|  r  rm  r  r  r  r  traced_coder   r   r  rX  r  is_generator_from_ctx_managerr-   generator_exhaustedis_constant_noner  r  rz   generated_itemsra   )r   r  r  
strict_ctxr  rc  r   s          r   r  z*InliningInstructionTranslator.inline_call_J  sh   &+#(,%{$022
" 	O55f6MNNJ	2  


              $ 	 	 	BdBBqBBCIIcNNNY' 	 	 	 !AL 	 	 	II*D111	 +/*CF',1F)) +/*CF',1F)1111;" 	1#*4000#///>V---#**4+@AAA((D,JJ((		$d+++#/66t<<<4 	,tCDD	,2	,
 T"",t%KLL, ,,
 "$(NOOOOO +<<>> 2 01D,]DtLLLLLL++D!! ,!$(NOOOOO+<<>>>>>+("2"4"4   
 ++sZ   B A5)B 5A99B <A9=B  D C9#B33C9C*C99D D%r  r  r  r  r  r   r  r   funcvar7BaseUserFunctionVariable | LocalGeneratorObjectVariabler   c                   |                                 }|d         }	t          |	t                    s|	j        }	|j        j        }
|
r|
j                                      nd }||j        }|j	        }|j
        }nct                    }t          |           t          |          }fdt                      D             }|
rt          |||          |
j        <   t!                                          |j        i ||	|||||||j        |j        dz   |j        |j        |j        |j        |           || _        || _        |j        | _        d | _        |j                                        | _        |j        | _        d S )N__builtins__c                2    i | ]}|t          |          S r   )r  )r   r   r  s     r   r   z:InliningInstructionTranslator.__init__.<locals>.<dictcomp>  s%    IIIAAwtQ//IIIr   )r}  r  r  r   )r'  r:  rm  ro  r  r  r  r  r}  r  r  r  r  r#  r  r  r  r  )get_globalsrX  r  __dict__r'  r  inlined_code_cacher  r}  r  r  r!   r    r   r+   r   r\  r]  r  r  r#  r  r  r  r  r  r  r  r  rH  r0  )r   r  r  r  r  r  r  r  rm  ro  r  cachedr}  r  r  r_  s     `            r   r]  z&InliningInstructionTranslator.__init__  s    ''))	~.
*d++ 	-#,J m3=HR/33D999d!.LnG!.LL/55L---!,//GIIIIIIIL 7G!-#!-8 8 8.t4 	=!+-*G"7%%=,q0"2,$6N% 	 	
 	
 	
( )#%5::<<)r   r  c                    | j         j        S r   )r  r  r   s    r   r  z'InliningInstructionTranslator.fake_mode  s    {$$r   c                X    t          j        | j                                                  S r   )r   r  r  r  r   s    r   r  z)InliningInstructionTranslator.run_ctx_mgr  s!    +DK,E,E,G,GHHHr   r   c                    t           j        sdS | j                                        sdS | j                                        sdS dS )zCheck if comprehension speculation is allowed in this inlined context.

        Unlike should_compile_partial_graph(), this skips the exception table entry check.
        FT)r   r	  r   should_allow_nested_graph_breaksr  r  r   s    r   r  zAInliningInstructionTranslator._can_speculate_comprehension_nested  sO    
 ) 	5|<<>> 	5{7799 	5tr   c                    t           j        rV| j                                        sdS | j                                        sdS t                                                      S dS r&  )r   r	  r  r  r  r  r\  r   r_  s    r   r  z:InliningInstructionTranslator.should_compile_partial_graph  s_    % 	:<@@BB u;;;== u7777999ur   r   r,   r  rx  r  c                    t           j        r"t                                          ||          S t	          dddg            d S )NzGraph break in inlined functionr   z2Graph breaks in an inlined call are not supported.r  )r   r	  r\  r  r:   )r   r   r  r_  s      r   r  z3InliningInstructionTranslator.create_call_resume_at  sY    
 % 	R77007PQQQ5L		
 	
 	
 	
 	
 	
r   c                P    |                                  | _        d | _        t          r   )r  r  r   r@  r  s     r   r  z*InliningInstructionTranslator.RETURN_VALUE  s     #xxzz#' r   c                R    |                      |          | _        d | _        t          r   )rj  r  r   r@  r  s     r   r  z*InliningInstructionTranslator.RETURN_CONST  s&    #//55#' r   r   r   #tuple[Any, VariableTracker, Source]c                P   d| j         v r| j         d                             d          s|| j         d         }|                     |          }d|v rt          j        j        j        |         }nt          |          } t          | |          |          }t          ||          }n_| j
                            d| j                   }t          |          }| j         } t          | |          |          }t          ||          }t          |          rt          |          }|||fS )Nr   namedtuple_r  ___unnamed_scope)rm  rl  rv  r  r  r  r  r   rd   rM   r'  r  rO   rN   r^  rS   )	r   r   rw  rx  ry  fglobals_vtglobal_sourceglobals_nameglobals_sources	            r   get_globals_source_and_valuez:InliningInstructionTranslator.get_globals_source_and_value  s<    ''z0J0U0U1
 1
' .4K ..{;;M+--M2L#  "0!<!< ?/$>>~NNK&}d;;MM;;;"DN L *,77N!^N @/$??OOK-ndCCM^$$ 	; ,M::M{M99r   c                   |j         }|| j        vr|                     |          S | j        j        | j        u r#t                                          |           d S |                     |          \  }}}| j        j        	                    ||          r5| 
                    | j        j                            ||                     d S | j        |         }| 
                    t          j        | ||                     d S r   )r~  rm  rq  r'  r  r\  rt  r  r!  rK  rI  rL  rb   r  )r   r   r   r   r  r  r  r_  s          r   rt  z*InliningInstructionTranslator._load_global5  s    {t~%%$$T***;#t~55 GG  &&&&&,0,M,Md,S,S)A{M{'DD[RVWW M		$+2<<[$OOPPPPPt,		//e]KKLLLLLr   c                x   | j         j        | j        u r#t                                          |           d S |                                 }t          |t                    rt          d|j	        dg            |j	        }| 
                    |          \  }}}| j         j                            |||           d S )Nz3Storing Tensor hook handle in globals (inline call)r  r  )r'  r  rm  r\  r  r  rX  r   r:   r~  r  r!  
store_attr)r   r   r  r   _fglobals_valuer  r   r_  s          r   r  z*InliningInstructionTranslator.STORE_GLOBALF  s    ;#t~55 GG  &&&&&HHJJE%!899 Q K 8	    ;D.2.O.OPT.U.U+O[!K$//T5IIIIIr   )
r  r   r  ro   r   r  r  r  r   rb   )r  ro   r   r  )
r  r   r  ro   r   r  r  r  r   r  r  )r  r   r  r  r  r  r  r  r  r   r  r   r  r  r   r   r  r  r  r  r  )r   r   r   r  )r   r   r   r   r   r  r  r  r  r  r  r]  r  r  r  r  r  r  r  r  r  rt  r  r`  ra  s   @r   r  r  e  s        **++++%%%%) ) ) [) ? ? ? \?B J J J \JXK, K, K, K,Z=* =* =* =* =* =*~ % % % X%I I I I        
 
 
 
 
 
   
   
(: (: (: (:TM M M M M M"J J J J J J J J J Jr   r  c                  x     e Zd ZU ded<   d fdZd fd
ZddZddZddZd fdZ	d fdZ
ddZddZ xZS )r  r  r  r   r   r  r   r   c                d     t                      j        |i | g | _        d| _        d| _        d S r&  )r\  r]  r  r  r  )r   r   r  r_  s      r   r]  z/InliningGeneratorInstructionTranslator.__init__]  s=    $)&)))!#( -2***r   rb   c                     t           j         j         fd          5  t                                                      cd d d            S # 1 swxY w Y   d S )Nc                      j         S r   r  r   s   r   r   zEInliningGeneratorInstructionTranslator.inline_call_.<locals>.<lambda>d  s
    4CT r   )r  r'  r  r\  r  r  s   `r   r  z3InliningGeneratorInstructionTranslator.inline_call_c  s     dk;T;T;T;TUU 	* 	*77''))	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	*s    AAAr   c                    dS r&  r   r   s    r   r  zCInliningGeneratorInstructionTranslator.should_compile_partial_graphg  s    ur   r   r,   c                D   |                                  }| j                            |           t          | j                  t          k    rt
          j        |                     t          j	        d                      t          j        s| j        r|| _        t          d S r   )r  r  r   r   rv   r   InfiniteGeneratorErrorrI  rh   r|  r   r  r  r  rB  )r   r   tops      r   YIELD_VALUEz2InliningGeneratorInstructionTranslator.YIELD_VALUEk  s    hhjj##C(((t#$$'999,,		")$//0005	1	 $'D 	 	r   c                    | j         d         }t          |t                    s[|                                  t	          j        | t                                        | |gi           }|                     |           d S d S r  )	r  rX  rz   r  rb   r  r!  r   rI  )r   r   r  ress       r   GET_YIELD_FROM_ITERz:InliningGeneratorInstructionTranslator.GET_YIELD_FROM_ITERy  sp    jn#344 	HHJJJ!'d33AA$rRRCIIcNNNNN	 	r   c                T    d| _         t                                          |          S r  )r  r\  r  r   r   r_  s     r   r  z3InliningGeneratorInstructionTranslator.RETURN_VALUE  #    #' ww##D)))r   c                T    d| _         t                                          |          S r  )r  r\  r  r  s     r   r  z3InliningGeneratorInstructionTranslator.RETURN_CONST  r  r   c                   t          | j                  dk    sJ |                                 }| j        d         }|                                st	          dddg            	 |                    |           }t          | j        t                    r| j        dk    sJ | xj        dz  c_        | 	                    |           | 
                    |           d S # t          t          j        f$ ry}t          |t          j                  rt          j        |            |                                  | 	                    t          j        |j                             Y d }~d S d }~ww xY w)	Nr  r  Unreachable sub-generator coder   @Should only be encountered while implementing generator support.r  r   r   )r   r  r  r  r:   r  rX  r   r   rI  r   r  r   r  r  rh   r|  r  r   r   rj  r  exs        r   
YIELD_FROMz1InliningGeneratorInstructionTranslator.YIELD_FROM  s   4:!####hhjjjn##%% 	 8^	   	###D))C 43S99,q0001 $$)$$IIcNNNT"""""# s<= 	9 	9 	9"c;<< 4-d333 HHJJJII&-bh77888888888	9s   $C E+2A.E&&E+c                N   t          | j                  dk    sJ |                                 }| j        d         }t          |t          t
          f          s:t          |t                    rt          |j        t          j	        j
                  r|                                r	 |                    |           }|                     |           d S # t          t          j        f$ rp}t"          j        dk     r|                                  |                     t'          j        |j                             |                     |           Y d }~d S d }~ww xY wt-          dddg            d S t-          dd	t/          |           d
t/          |           dg            d S )Nr  r  r<  r	  r   r
  r  zSEND with bad typez
TOS type: z(Attempted to SEND with unsupported type rW  )r   r  r  rX  ry   rq   r   r  collectionsabcIteratorr  r  rI  r  r   r  r\  r  rh   r|  r  r:   r`   r  s        r   SENDz+InliningGeneratorInstructionTranslator.SEND  s   4:!####hhjjjnc,.JKLL &	s566&	39ko&>??&	 ##%% #++D11C IIcNNNNN &s'DE 
$ 
$ 
$ ''11


II.5bh??@@@IIdOOOOOOOOO
$& < b	      ,3WS\\33Vws||VVV	     s   'C E)A%EE)r   r   r  r   r   r   r  r  r  )r   r   r   r   r]  r  r  r   r  r  r  r  r  r`  ra  s   @r   r  r  Y  s
        ****3 3 3 3 3 3* * * * * *         * * * * * ** * * * * *$# $# $# $#L* * * * * * * *r   r  )r   r   r   r   )r   r  )r   r   r   r!  )rC  rD  r   rE  )rT  rU  r   r   )r_  r   r   r   )r   r   rv  rw  rI  r   r   r   )r  r  r   r   )r  )r  rb   r  r   r   r   )rv  rw  rI  r   r   r  )r   r   r  r1   r  r  r   r   )rI  r   r"  r   r   r#  )r'  rA   r  r  r}  r~  r   r!  (  r   
__future__r   r  collections.abcr=  rH  r   ro  rQ  r   rN  r  r  r   r{  r  r\  	threadingr  r  rY  rl  r   typingr   r   r   r   r	   r
   typing_extensionsr   r  torch._loggingtorch._dynamo.dynamo_profilerr   r   torch._dynamo.excr   r   torch._guardsr   r   r   torch._logging.structuredr   %torch.fx.experimental.symbolic_shapesr   torch.utils._functoolsr   r   r   r   r   r  r   r   bytecode_analysisr   r   r   r    bytecode_transformationr!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   codegenr1   comprehension_graph_breakr2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   funcname_cacher=   guardsr>   r?   output_graphr@   rA   rB   	polyfillsrC   rD   rE   rF   rG   replay_recordrH   rI   r  rJ   rK   rL   r  rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   utilsrW   rX   rY   rZ   r[   r\   r]   r^   r_   variables.baser`   ra   rb   variables.builderrc   rd   re   variables.builtinrf   rg   variables.constantrh   variables.ctx_managerri   rj   rk   rl   r  rn   variables.functionsro   rp   rq   rr   rs   rt   ru   variables.iterrv   variables.lazyrw   r  rx   ry   rz   r{   r|   r}   variables.miscr~   r   r   r   r   r   r   variables.nn_moduler   r   variables.setsr   rU  r   variables.tensorr   r   r   variables.torch_functionr   r   variables.user_definedr   r   r   r   r   r   r   r   torch._subclasses.fake_tensorr   r  r   	getLoggerr   r  r  getArtifactLoggerr  r  r  r  r  rB  r  r   r   containsr   r   r  r   r   cacher   	dataclassr   r   r   r  r  r   r>  r$  r*  r,  r  r   r  r@  rB  rS  r^  ru  r  r  r  r  r  r!  rM  r  rO  rc  r   rG  r  _nb_opsr/  r  r  r  r   r   r   <module>r>     s    0 # " " " " "                  



                    				 



                  I I I I I I I I I I I I I I I I $ $ $ $ $ $      R R R R R R R R O O O O O O O O C C C C C C C C C C / / / / / / < < < < < < / / / / / /                                                           " ' & & & & &       L L L L L L                        ) ( ( ( ( ( / / / / / / / / N N N N N N N N N N              : 9 9 9 9 9 9 9         
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ; : : : : : : :
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 G F F F F F F F F F R R R R R R R R R R C C C C C C C C 0 0 0 0 0 0            / . . . . .                  / . . . . . / / / / / /                                 Q P P P P P P P ' ' ' ' ' ' 2 2 2 2 2 2 W W W W W W W W W W                      (==========<<<<<<''''''g!!.228^LL11(LII>33HnMM ^55h@PQQ io' '4R4L4R4T4T' ' '      "/("344B_X]++9
  D ! ! H 
 '(()      ) ) ) )        < I I I I I I I IX 
 
 
 
 
 
 
 
 / / / / / / / /3 3 3 3 3 3 3 3. 4 4 4 4 ) ) ) )& $ $ $ $        D	 	 	 	 	~ 	 	 		 	 	 	 	I 	 	 	    9      A A A A . . . .&' ' ' 'T B@ @ @ @&@ @ @ @ @F] ] ] ]@	R R R RBB B B BJJ J J J J J J J2 ` ` ` ` ` ` ` `FP8; P8; P8; P8; P8;'P8; P8; P8; P8;fp^ ^ ^ ^ ^5 ^ ^ ^B	 w 
    := := := :=zqJ qJ qJ qJ qJ$= qJ qJ qJh    -J     r   