
    i*N                   
   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mZ ddlmZ ddlmZ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+ ddl,m-Z- ddl.m/Z/ ddl0m1Z1m2Z2m3Z3mZ4m5Z5m6Z6 ddl7m8Z8m9Z9m:Z:m;Z; ddl<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZK ddlLmLZL ddlMmNZN ddl2mOZOmPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZWmXZXmYZY ddlZm[Z[ ddl\m]Z]m^Z^ ddl_m`Z`maZambZb ddlcmdZd ddlemfZfmgZg ddlhmiZimjZjmkZk ddllmmZmmnZnmoZompZpmqZqmrZrmsZsmtZt ddl5muZumvZv ddlwmxZxmyZymzZzm{Z{m|Z|m}Z}m~Z~mZmZ ddlmZmZmZ ddlmZmZmZ ddlmZ dd lmZ dd!lmZmZmZmZ dd"lmZmZ dd#lmZmZmZmZmZmZmZ dd$lmZ dd%lmZ dd&lmZmZmZmZmZmZ dd'lmZmZmZmZmZmZ dd(lmZmZ dd)lmZ dd*lmZmZ dd+lmZmZ dd,lmZmZmZmZmZ erdd-lmZmZmZ dd.lmZ dd/lmZ  ej                  eȫ      Ze"j                  j                  ed0      Ze"j                  j                  ed1      Ze"j                  j                  ed2      Ze"j                  j                  ed3      Z ej                         Z ej                         D  ci c]  \  } }|  e|      j                   c}} Zd4ed5<    eej                        j                  Z eej                        j                  Zd6 ed7<   d8 ed9<   d:Zee6jT                  eef   Zd;ed<<   e	j                  dkd=       Zej                   G d> d?             Zej                   G d@ dA             Zej                   G dB dC             Zej                   G dD dE             Z G dF dG      Ze	j                  dldH       Zej                  	 	 	 	 dmdJ       Zej                  	 	 	 	 dmdK       Zej                   G dL dM             Z G dN dOe      Z G dP dQe      Z G dR dSe      ZdndTZdodUZe	j                  dpdV       Z	 	 	 	 	 	 	 	 dqdWZdXZ	 	 	 	 	 	 drdYZ	 	 	 	 	 	 dsdZZ G d[ d\e      Zej                   G d] d^             Z G d_ dIe`      Z G da dbe      Zej                  dck\  r6ej                  D cg c]  \  }} eedd|v r|ded ndf|ded         c}}Z G dg dhe      Z G di dje      Zyc c}} w c c}}w )ta  
Core module responsible for converting Python bytecode into TorchDynamo's symbolic execution format.

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

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

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

This is a core part of TorchDynamo's tracing system that enables ahead-of-time
optimization of PyTorch programs.
    )annotationsN)deque)StackSummary)AnycastNoReturnOptionalTYPE_CHECKING	TypeAliasUnion)TypeIs)ObservedExceptionTensorifyScalarRestartAnalysis)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)ArgsMismatchErrorBackendCompilerFailedcollapse_resume_framesformat_graph_break_messageformat_loop_skip_frame_messageformat_skip_frame_messageget_stack_above_dynamoResumePrologueTracingErrorStepUnsupportedunimplementedUnsupported)get_funcname)GuardBuilderinstall_guard)GraphCompileReasonOutputGraphStackLocalsMetadata)impl_CONTAINS_OP_fallback)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)ConstantVariable)ContextWrappingVariableGenericContextWrappingVariableWithEnterFunctionVariableWithExitFunctionVariable)ConstDictVariableSetVariable)BaseUserFunctionVariableLocalGeneratorFunctionVariableLocalGeneratorObjectVariableNestedUserFunctionVariableSkipFunctionVariableUserFunctionVariableUserMethodVariable)MAX_ITERATOR_LIMIT)LazyVariableTracker)BaseListVariableIteratorVariableListIteratorVariableListVariableSliceVariableTupleVariable)CellVariableExceptionVariableGetAttrVariableNullVariablePythonModuleVariableUnknownVariable)NNModuleVariableUnspecializedNNModuleVariable)SymbolicStreamState)supported_comparison_opsSymNodeVariable)SymbolicTorchFunctionStateTorchFunctionModeVariable)RemovableHandleVariableUserDefinedClassVariable!UserDefinedExceptionClassVariable"UserDefinedExceptionObjectVariableUserDefinedObjectVariable)Callable	GeneratorSequence)FakeTensorMode)CompilePackagegraph_breaks
trace_calltrace_sourcetrace_bytecodedict[str, Any]compare_op_handlersc                2    t        | g t        |      i       S N)handle_containsreversedtxargs_s      X/var/www/html/engine/venv/lib/python3.12/site-packages/torch/_dynamo/symbolic_convert.py<lambda>r      s    (4.21     inc           
     J    t        | t        | g t        |      i       gi       S r   )
handle_notr   r   r   s      r   r   r      s(    J.x~.	34b5 r   znot inzihttps://github.com/pytorch/pytorch/issues/new?&labels=oncall%3A+pt2&projects=&template=pt2-bug-report.ymlr   ExceptionValsc                ,    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)names    r   _import_moduler      s     ""4((r   c                  p    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y)SpeculationEntrystrfilenameintlinenoinstruction_pointerr+   instFbool_failedNzOptional[bool]error_on_graph_breakzOptional[GraphCompileReason]reasonc                    d| _         || _        | j                  | j                  j                  }nd}t        j                  |      )z\
        Start tracing of the current frame over again, and don't take this branch.
        Tz!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   sB     $8!;;"![[//N@N,,NKKr   c                \    | j                   r | j                  J | j                  |_        yyNTF)r   r   )r   r   s     r   failedzSpeculationEntry.failed  s/    <<,,888&*&?&?B#r   )r   r   returnNone)r   InstructionTranslatorBaser   r   )	__name__
__module____qualname____annotations__r   r   r   r   r    r   r   r   r      sC    MK
GT+/./+/F(/
Lr   r   c                  |    e Zd ZU dZ ej
                  e      Zded<   dZ	ded<   ddZ
dd	Z	 	 	 	 	 	 	 	 	 	 dd
Zy)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   indexc                    d| _         y Nr   )r   r   s    r   restartzSpeculationLog.restart  s	    
r   c                F    | j                   j                          d| _        y r   )r   clearr   r   s    r   r   zSpeculationLog.clear  s    
r   c                6   t        | j                        | j                  k(  r'| j                  j                  t	        ||||             | j                  | j                     }d}| j                  dk7  r]| j                  | j                  dz
     }d|j
                   d|j                   d|j                  j                   d|j                   d	}|j                  |k(  r|j
                  |k(  r|j                  |k(  st        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||

*LL 63FM TZZ(::?djj1n5J()<)<(=Qz?P?P>QJOO**+3z/M/M.NcS 
 %%)<<(*&*""&**ZDLL8I7J K^^Aell^2ejj.?.?-@HaHaGb c*AfXR}G4G3H I  	 ( 	

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   	  sa     '8k&7&7&MG#ME3N--%(-?B-JU-	-r   r   c                  F    e Zd ZU  ej                  e      Zded<   ddZy)
LocalStater   zdict[str, FrameStateSizeEntry]automatic_dynamicc                b    dj                  d | j                  j                         D              S )N
c              3  L   K   | ]  \  }}| d |j                            yw): N)render).0kvs      r   	<genexpr>z$LocalState.render.<locals>.<genexpr>T  s*      
%)QqcAHHJ< 
s   "$)joinr   itemsr   s    r   r   zLocalState.renderS  s/    yy 
-1-C-C-I-I-K
 
 	
r   Nr   r   )	r   r   r   r   r   dictr   r   r   r   r   r   r   r   M  s%    8I8I8I95 
r   r   c                  0    e Zd ZU ded<   ded<   dZded<   y)DistributedStater   
compile_pgr   local_stateNzOptional[list[LocalState]]
all_states)r   r   r   r   r   r   r   r   r   r   Z  s    O-1J*1r   r   c                  n    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
y)TensorifyStatezset[str]force_specializationsc                :    | j                   j                  |       y r   )r   addclsr   s     r   
specializezTensorifyState.specializeg  s    !!%%e,r   c                    || j                   v S r   )r   r   s     r   should_specializez TensorifyState.should_specializek  s    1111r   c                8    | j                   j                          y r   )r   r   r   s    r   r   zTensorifyState.clearo  s    !!'')r   c                2    t        | j                        dk(  S r   )r   r   r  s    r   emptyzTensorifyState.emptys  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   a  s_     '*e8+- - 2 2 * * 3 3r   r   c                 4    t        j                  t              S r   )torchdynamo_loggingget_step_loggerlogr   r   r   _step_loggerr  x  s    ..s33r   r   c              #  D  K   | j                   j                  }| j                   j                  }	 g | j                   _        d| j                   _        d  || j                   _        || j                   _        y # || j                   _        || j                   _        w xY wwr   )speculation_logr   r   )r   r   r   s      r    save_and_restart_speculation_logr  }  s        ((G$$E)%'"#$ %,"#(  &-"#( s   -B &A9 #B 9$BB c              #     K   	 | j                   j                  }d| j                   _        d  || j                   _        y # | j                   _        w xY wwNF)outputshould_exit)r   tmps     r   (temporarely_allow_writes_to_output_graphr    sA     $ii## %		 #				s   A+A AAAc                  ^    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y)BlockStackEntryr+   r   targetr   stack_indexNzHOptional[Union[ContextWrappingVariable, GenericContextWrappingVariable]]with_contextc                    | j                   d uS r   )r  r   s    r   can_restorezBlockStackEntry.can_restore  s      ,,r   c                *   | j                   J | j                  rbt        | j                  d      rL| j                  j                  r6t	        | j                   dz
  t        | j                  j                              S t	        | j                   dz
        S )Ntarget_valuesr   )r  r  hasattrr  rG   tupler   s    r   	resume_fnzBlockStackEntry.resume_fn  s    +++))?;!!//  1$eD,=,=,K,K&L  t//!344r   c                    | j                   J |r| j                   j                         s|s| j                   j                  |      S y r   )r  exit_on_graph_breakexit)r   r   is_graph_breaks      r   r%  zBlockStackEntry.exit  sG       ,,,t00DDF$$))"--r   r  )r   rG   )r   r   r&  r   r   zVariableTracker | None)r   r   r   r   r  r  r"  r%  r   r   r   r  r    sQ      	   -5+=A	r   r  c                      e Zd Zy)r   Nr   r   r   r   r   r   r   r         r   r   c                      e Zd Zy)ReturnValueOpNr(  r   r   r   r+  r+    r)  r   r+  c                      e Zd ZdZy)YieldValueOpzY
    Signal to the symbolic tracer to stop and return control flow to the
    caller
    N)r   r   r   r   r   r   r   r-  r-    s    r   r-  c                    t        t        j                  |       j                        t	        |       t        j                  |       dfd       }|S )Nc                h    | j                  j                  | | j                        i              y r   )pushcall_functionpopn)r   r   fn_varnargss     r   implzstack_op.<locals>.impl  s'    		&&&tTYYu-=rBCr   )r   InstructionTranslatorr   r+   r   r   )r   inspect	signature
parametersra   	functoolswraps)fnr5  r3  r4  s     @@r   stack_opr=    sL    !!"%001ER F__RD D Kr   c                    t        | t        j                        sy| j                  j	                  d      d   t
        j                  v S )NF.r   )
isinstancetypes
ModuleTyper   splitsysstdlib_module_names)mods    r   	is_stdlibrG    s:    c5++,<<c"1%)@)@@@r   c                    | rdd}ndd}t        j                  |      D cg c]  }|j                   }}t        d t	        |      D              }|j                  d      }||dz   |dz    S c c}w )Nc                    | sJ d       y )Nmsgr   xs    r   r<  z(get_assert_bytecode_sequence.<locals>.fn  s    OeO1r   c                    | sJ y r   r   rK  s    r   r<  z(get_assert_bytecode_sequence.<locals>.fn  s	    H1r   c              3  J   K   | ]  \  }}|j                  d       s|  yw)POP_JUMPN)
startswith)r   ir   s      r   r   z/get_assert_bytecode_sequence.<locals>.<genexpr>  s     V71d$//*:UQVs   ##RAISE_VARARGSr   )rL  r   r   r   )disget_instructionsr   r   	enumerater   )with_msgr<  r   insts	begin_idxend_idxs         r   get_assert_bytecode_sequencerZ    sr    		 &)%9%9"%=>TT[[>E> Vi&6VVIkk/*GQ1-- ?s   A/c                   |t         j                  us|ryt        | j                  t              sJ | j                  }dD ]  }t        |      }| j                  ||t        |      z    }|D cg c]  }|j                   }}||k(  sH|r.|j                  d      }| j                  ||z      j                  }	nd}	| j                  t        j                  |	              y yc c}w )NF)FT
LOAD_CONSTzassertion errorT)operatortruthr@  r   r   rZ  instructionsr   r   r   argvalr0  rb   create)
r   truth_fnr0  current_instruction_pointerrV  assert_insts	cur_instsr   load_const_idx	error_msgs
             r   &_detect_and_normalize_assert_statementrh    s     	&4d..444"&":":! 3H=%%'*E,+ 
	 .77TT[[7	7$!-!3!3L!A --/.@&  .	II&--i89!$  8s   -C"Fc                |     ddg t         j                  d	 d	 	 	 	 	 	 	 	 	 dfdd fd}|S )	NData-dependent branchingzyDetected data-dependent branching (e.g. `if my_tensor.sum() > 0:`). Dynamo does not support tracing dynamic control flow.1Use `torch.cond` to express dynamic control flow.c           	     8   | j                         sJ | j                  | j                  t        
d| 	             | j	                         rqt        | j                  dj                  t        j                  | j                         g                  }t        j                  |       t        j                  |      | j                  |       t        j!                  d       | j"                  j%                  | t'        dt)        |       | | j                         g      d      }| j+                          | j-                  | j.                  |      }r| j                  |       |j0                  J | j-                  |j0                  |      }t2        j4                  d	k\  r%| j"                  j7                  t9        d
      g       t9        |j:                  |d         }|j=                  |       | j"                  j7                  |g|z   |z          y )Nattempted to jump with gb_typecontextexplanationhints)r   r   zgeneric_jump triggered compilezgeneric_jump r   r   
stack_pops      TO_BOOLr   )r  )should_compile_partial_graphlog_graph_breakcode_optionsr4   maybe_has_backedger5   f_coder   	tracebackformat_listframe_summaryr  infor   	SkipFramer0  debugr  compile_subgraphr?   r[   popcreate_call_resume_atnext_instructionr  rD  version_infoadd_output_instructionsr&   r   copy_positions)r   r   value	extra_msgrJ  all_stack_locals_metadataif_nextif_jump	jump_inst_explanation_gb_type_hintsr0  s            r   jump_graph_breakz&generic_jump.<locals>.jump_graph_breakO  s    00222- 1%9(	 	 	
 ""$0	--t/A/A/C.DEFC HHSM--$$		%		23$(KK$@$@%/	{;d>P>P>R=S  %A %
! 	
,,!!%
 IIe{{&&&,,KK%

 w&KK//1CI1N0OP&t{{71:F	  &++YK',AG,KLr   c                   | j                         }t        j                  rBt        |       r4| j                         }|j	                         rmt        |j                               r| j                  |      S | j                         r | ||       n(t        dd| dg t        j                  ddd       |j                         rJ | j                  j                  dt        j                   gt#        ||fi         | j                  |       y t%        |t&              r|j(                  }t%        |t        j*                        s|d	k7  }t        j,                  j.                  j0                  j3                  |      }|s(t        d
t5        |      dg t        j6                         | j                  |       y  | j                  j                  dt        j8                  gt#        |fi        }t;        | |t=        |j>                  |             } | j                  j                  dt        j                   gt#        ||fi         | j                  |       y |j	                         rt%        |t@              r>|jB                  r2tE        |jB                  jG                  tH        jJ                                |j                               r%r| jM                  |       | j                  |       y y |j                         r| j                         r | ||       y t%        |tN              rS| j                  jQ                  |jR                        } |      r%r| jM                  |       | j                  |       y y t%        |tT              r	 |jW                  | d      }	t%        |	t^              rP|	ja                  | g i       }tc        tc        |	dd       dd       }
|j	                         r|j                         }|
dk(  rbt%        |t
              sRtd        jf                  ji                  dtk        |      jl                         }tY        jn                  tp        | |g       t%        |t
        tr        f      r |      rr| jM                  |       | j                  |       y t%        |t&              r5|ju                         rir| jM                  |       | j                  |       y t        dd|	 d| dg        y  d      r%r| jM                  |       | j                  |       y y y y y |j                         sW|jw                  |       rF ty        |j{                  |                   r%r| jM                  |       | j                  |       y y t%        |t&              r	 t%        |j(                  t        j*                        r|ju                  | j                        }nt}        |j(                  d	k7        } |      r%r| jM                  |       | j                  |       y y t%        |td        j                        r. d      r%r| jM                  |       | j                  |       y y ddl!mA} |jB                  N ||jB                        r< |j                               r%r| jM                  |       | j                  |       y y t        dd| g t        j                  d       y # tX        jZ                  $ r] tY        j\                  |        	 |jW                  | d      }	n0# tX        jZ                  $ r tY        j\                  |        d }	Y nw xY wY w xY w# tX        j~                  $ r*}| j                         r | ||d|       cY d }~S  d }~ww xY w)Nz>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).rn  r1  r   z#Assertion failed on symbolic shapesr   )example_value__bool____len__r<  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_sourcerj  rm  rk  )Cr  r    rewrite_assert_with_torch_assertrh  is_python_constantr   as_python_constantjumpry  r:   r   FUNDAMENTAL	is_tensorr  create_proxytorch_assert_asyncrZ   r@  r   sym_numSymBoolfxexperimentalsymbolic_shapesexpect_truer   
USER_ERRORscalar_tensorr`   rT   noderg   sourcer>   
make_guardr=   SEQUENCE_LENGTHr0  r~   get_submodule
module_keyr   var_getattrr   ObservedAttributeErrorhandle_observed_exceptionro   r1  getattrr   rb   ra  typer   raise_observed_exception	TypeErrorr   evaluate_exprhas_unpack_var_sequencer   unpack_var_sequencer   	UserErrorBackwardHookVariabler  get_real_value)r   r   r  rg  sym_exprresultscalar_to_tensor_proxyscalar_to_tensorrF  rL  method_nameresult_valuerJ  eval_resulter  r  r  r0  rb  s                   r   innerzgeneric_jump.<locals>.inner  s   !%336tXtL)-I'')002399T?*668$T47! `")% 1%H.::u
 ;N*  (((#'' 'y'92>
 		$%1 !==!(EMM:'1}H..>>JJ8T! E #H$&= 1 < <=	 		$%=T[[%=%=!4!4&7H%SU7V&"  -&,-C-H-H$O  %DKK$$## #$4i#@"E
 IIdO##% %!23ell55l6R6RST0023IIe$		$ 4 __4#D#D#FT4//0++++E,<,<=C}IIe$		$  89	%%dJ7 !/0r26%gat&<j$O,,.#)#<#<#>L"j0LRV9W'88??DT,EWE`E`Dab 44YC5Q!,s<,AW IIe,		$8++- IIe,		$! U"*1#Zx @%X  D>		%(IIdO " . BX<. "u'D'DT'JE55d;<=IIe$		$ > / emmU]];"'"5"5dkk"BK",U]]a-?"@K
 $IIe$		$ % y==>~IIe$		$ 
 3||',>u||,LE0023		%(IIdO 4
 65eW= ,*66K	c -- --d3))$	:A11 11$7Av == 446+D$2aSRRs[   \ ;A^ (]>7]
	]>
*]74]>6]77]>=]>^>^92^>8^99^>)r   )
r   r   r   r+   r  r]   r  r   r   r   r   r   r   r+   r   r   )r   r  )rb  r0  r  r  r  r  r  s   `` @@@@r   generic_jumpr  A  s     *H	@ 		&	&;F 	6M'6M6M 6M 	6M
 
6M 6Mp B Lr   c                      	 	 	 	 d fd}|S )Nc                j     t        j                         d fd       }	 	 	 	 	 	 	 	 dfd|S )Nc           
     |   | j                   }	| _         | j                         }|j                  |       r"|j                  J  | ||j                        S 	  | |      || _         S # t        $ r}| j
                  rW| j                  s| j                  r |j                          t        dd| j
                   ddgt        j                  |       t        |t        j                        r | j                         s | j!                  | j"                   dt%        |       |j&                         | j)                         rrt+        | j,                  dj/                  t1        j2                  | j5                         g                  }t6        j9                  |       t        j:                  |      ||j                          |j=                  d	       t?        |j@                  |j&                        |_        Y d }~nd }~ww xY w	 || _         n# || _         w xY w|jC                  | j                         y )
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.ro  rp  rq  rr  from_excz:

r   
user_stackr   graph_break)"current_instruction_push	speculater   r   r;   active_generic_context_managers	one_graphr   remove_from_statsr:   r   CAUSED_BY_EARLIER_GRAPH_BREAKr@  r   UncapturedHigherOrderOpErrorry  rz  r{  r   
real_stackr|  r5   r}  r   r~  r  r  r  r  r  add_to_statsr?   rJ  r   )
r   r   	prev_pushspeculationexcprJ  handle_graph_breakinner_fn
msg_prefixr0  s
         r   wrapperz>break_graph_if_unsupported.<locals>.decorator.<locals>.wrapperX  s   55I,0D)..*K!!$'"))555)$k6H6HII.:d+Z 1:-Y  *S77~~)B)B **,! R"CDDhDhCi j %Dc.LL "&	 dC$D$DE88:$$%%(\s4yk:# %  **,8	 5 5t7I7I7K6L MNC HHSM--,$6&&(!!-0%7$//%R""U*ST #09-	-11$2K2KLs+   A) )
H
3FH H H

H 	Hc                   t         j                  dk\  rt         j                  dk  rs|j                  dk(  rdt        j                  t        j
                  d   |j                        t        j                  t        j
                  d   |j                        z   }n*t        j                  |j                  |j                        }t        j                  d|j                         | j                  j                  | |t              |z
        }t        | j                  j                        }g }| j                  D ]  }t!        |j"                  t$              r:|j'                  |j)                         j+                  |j,                  |             W|j"                  J t!        |j"                  t.              sJ |j"                  j1                  |       |j'                  |j)                         j3                  |j,                  |              | j                  j5                  |j7                                ~t         j                  dk\  r|j                  dk(  r| j8                  | j8                  j;                         nd}t=        |      dkD  r<t         j                  d	k  sJ | j                  j5                  t?        d
|      g       |j                  J tA        |j                  d      }	|	d   jC                  |       | j                  j5                  |	       nF|jD                  J tG        jF                  |      }
d |
_$        | j                  j5                  |
g       | j                  j5                  |       | jK                  t              |z
         r| jM                  tO                      | j                  j5                  | jQ                  | jR                  |             y )Nrv     rv     CALLPRECALLz%s triggered compilers  r   r   ru  KW_NAMESr`  F)*rD  r  r   rS  stack_effectopmapargopcoder  r  r  r  r   r0   root_txblock_stackr@  r  r   extend_outputr"  try_except_torch_function_moder{  rc   reconstruct_typetry_finallyr  rT  kw_namesr  r   r&   r"   r  r  copyexn_tab_entryr2  r0  r}   r  r  )r   r   r   r  r  cgcleanupbr  
call_insts	inst_copyr0  s              r   r  zIbreak_graph_if_unsupported.<locals>.decorator.<locals>.handle_graph_break  s#      G+$$w.KK6)  #//IIi($(( $$SYYv%6A B  #//TXXFII,dkk:(,(D(DVD	L0H )E )% 4;;../B)+G%% V ann.GH$$DDOOW
 ~~111!!..3JLLL//3  !:!:2??G!TUV KK//0C0C0EF7*t{{f/D }}0 MM446 
 x=1$++g555KK77+JxHI xx+++1$((EB
2--d333J? {{*** IIdO	*.	'33YK@KK//8IIc$i,./		/+,KK//**))-r   r  )r   r   r   r+   r   r?   r   r   )r:  r;  )r  r  r  r  r0  s   ` @r   	decoratorz-break_graph_if_unsupported.<locals>.decoratorU  sY     
	"7	M 
#7	MrJ	+J	J	 'J	 	J	X r   )r  Callable[..., None]r   8Callable[[InstructionTranslatorBase, Instruction], None]r   )r0  r  r  s   `` r   break_graph_if_unsupportedr  P  s$    
I%I	AIV 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()c                @   t         	|   |||       dd}t        j                  j	                         D ci c]'  \  }}|t        | |t        j                  ||            ) }}}t        d      D cg c]  }|j                  |       c}| _
        y c c}}w c c}w )Nc                ^    t        d|  d| d|  dd|  dgt        j                         y )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).rn  )r:   r   SUPPORTABLE)r   r   s     r   _missingz4BytecodeDispatchTableMeta.__init__.<locals>._missing  sS    2!(+dV4[\b[ccef;F8 D_ _ '22		r      )r   r   r   r   r   r   )super__init__rS  r  r   r  r:  partialrangegetdispatch_table)
r   r   basesdctr  r   opr  rQ  	__class__s
            r   r  z"BytecodeDispatchTableMeta.__init__  s    uc*
	 "iioo/
 VY%6%6x%HII
 

 >C4[In003I

 Js   ,B4B)
r   r  r   r   r  r   r  r   r   r   )r   r   r   r   r  __classcell__r  s   @r   r  r    s    ^J Jr   r  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y)ExceptionStackzU
    Exception stack that it is shared among all InstructionTranslator instances
    r   zlist[ExceptionVals]
_exc_stackN)defaultzOptional[ExceptionVals]_current_exceptionc                    d | _         y r   r  r   s    r   clear_current_exceptionz&ExceptionStack.clear_current_exception  s
    "&r   c                4    | j                  |       || _        y r   )._set_context_and_break_context_reference_cycler  r   vals     r   set_current_exceptionz$ExceptionStack.set_current_exception  s    ;;C@"%r   c                v    | j                   J | j                  | j                          | j                          y r   )r  r   r  r   s    r   move_current_exception_to_stackz.ExceptionStack.move_current_exception_to_stack  s2    &&222D++,$$&r   c                6    | j                   J | j                   S r   r  r   s    r   get_current_exceptionz$ExceptionStack.get_current_exception  s     &&222&&&r   c                    |j                   x}rt        |      t        ur|S t        | j                        |z   dkD  r5| j                  |   }| j                  ||dz
         |j                  |       |S Nr   r   )__context__r  rb   r   r  _set_context_recursiveset_context)r   r!  prev_idxctxprevs        r   r*  z%ExceptionStack._set_context_recursive!  sm     ??"C"S	9I(IJt(*Q.??8,D''hl;OOD!
r   c                    |x}}d}	 |j                   }t        |      t        u ry ||u r|j                  t        d              y |}||u ry |r|j                   }| }Vr  )r)  r  rb   r+  )r   r!  oslow_oslow_update_togglerp  s         r   _break_context_reference_cyclez-ExceptionStack._break_context_reference_cycle,  sy    
 F"mmGG} 00#~.t45AF{ !++%7!7% r   c                v    | j                  |t        | j                        dz
         | j                  |       y )Nr   )r*  r   r  r3  r   s     r   r  z=ExceptionStack._set_context_and_break_context_reference_cycleG  s0     	##CT__)=)AB++C0r   c                6    | j                   j                         S r   )r  r  r   s    r   r  zExceptionStack.popN  s    ""$$r   c                :    | j                   j                  |       y r   )r  r   r   s     r   r   zExceptionStack.appendQ  s    s#r   c                ,    t        | j                        S r   )r   r  r   s    r   r  zExceptionStack.__len__T  s    4??##r   c                     | j                   |   S r   )r  )r   r   s     r   __getitem__zExceptionStack.__getitem__W  s    u%%r   c                :    d| j                   d| j                  S )Nzself._exc_stack=z - self._current_exception=)r  r  r   s    r   __str__zExceptionStack.__str__Z  s"    "$//##?t'>'>&@AAr   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*  r3  r  r  r   r  r9  r;  __repr__r   r   r   r  r    s     '8k&7&7&MJ#M2C+2C2CD2Q/Q'&'
'	 	,/			861 1	1%$$&B Hr   r  c                      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d0<   d1ed2<   dd3Zdd4Zdd5Zdd6Zdd7Zdd8Z		 	 	 	 	 	 	 	 dd9Z
	 	 	 	 	 	 	 	 dd:Z	 	 	 	 	 	 	 	 dd;Zddd<Zdd=Zdd>Zdd?Zej$                  d@k\  rddAZnddBZeddC       ZddDZddEZddFZddGZddHZddIZddJZddKZddLZddMZddNZ ddOZ!eZ"ddPZ#ddQZ$ddRZ%e&jN                  ddS       Z(ddTZ)ddUZ*e+ddV       Z,ddWZ-ddXZ.ddYZ/e/Z0ddZZ1e+d d[       Z2dd\Z3dd]Z4e4Z5e4Z6 e7e8jr                  d^      Z: e7e8jv                  d^      Z< e7e8jr                  d_      Z= e7e8jv                  d_      Z>dd`Z?ddaZ@ddbZAddcZBdddZCddeZDddfZEddgZFddhZGddiZHddjZIddkZJddlZKddmZLddnZMddoZNddpZOddqZPddrZQddsZRddtZSdduZTddvZUddwZV eWd_dxy      ddz       ZX eWd_d{y      dd|       ZY eWd_d}y      dd~       ZZddZ[ddZ\ddZ]ddZ^ddZ_ddZ` eWd^dy      dd       ZaddZbec	 	 	 	 	 	 dd       Zd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZe	 	 	 	 	 	 ddZfec	 	 	 	 	 	 	 	 d	d       ZgddZh eWd^dy      dd       ZiddZjddZkddZlddZmddZneofd
dZpddZqeqZrddZsddZtetZuddZvddZwddZxddZyddZzddZ{ddZ|ddZ} eWd^dy      dd       Z~ddZddZddZddZddZddZddZddZddZddZddZddZddZddZddZddZddZeZddZddZddZddZddZddZddZ ee8j2                        Z ee8j6                        Z ee8jr                        Z ee8j<                        Z ee8j@                        Z ee8jD                        Z ee8jH                        Z ee8jL                        Z ee8jP                        Z ee8jT                        Z ee8jT                        Z ee8jZ                        Z ee8j^                        Z  eWd_dy       ee8jb                              Z ee8jf                        Z ee8jj                        Z ee8jn                        Z ee8jr                        Z ee8jv                        Z ee8jz                        Z ee8j~                        Z ee8j                        Z ee8j                        Z ee8j                        Z ee8j                        Z ee8j                        Z ee8j                        Z ee8j                        Z ee8j                        Z ee8j                        Z ee8j                        Z ee8j                        Z ee8j                        ZddZej$                  d@k\  rddZddZddZddZܐdddZ eWd_dy      dd       ZddÄZddĄZe4Ze4Z e7e8jv                  d^      Z e7e8jv                  d^      Z e7e8jr                  d^      Z e7e8jr                  d^      ZddńZddƄZ	 	 	 	 	 	 ddǄZecddȄ       ZddɄZddʄZdd˄Zdd̄Zdd̈́Zdd΄ZddτZddЄZddфZdd҄ZddӄZ eWd_dԬy      ddՄ       ZddքZddׄZdd؄ZddلZddڄZeZeZeZdZdd܄Z dd݄ZdސZdd߄ZddZ	 d	 	 	 ddZddZddZddZedd       Z	e
j                  	 	 	 	 dd       ZddZ	 	 	 	 	 	 	 	 ddZ	 	 	 	 ddZ	 	 d	 	 	 	 	 	 	 ddZ	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fdZ xZS (  r   r@   r  dict[str, VariableTracker]symbolic_localssymbolic_globalsr   symbolic_torch_function_stater   symbolic_stream_statez$Optional[dict[str, VariableTracker]]post_prune_cell_and_freevarslist[VariableTracker]stackOptional[int]r   r+   current_instructionr   r  zlist[BlockStackEntry]r  r   r   zOptional[ConstantVariable]r  accept_prefix_instlist[Instruction]prefix_instsinline_depthinconsistent_side_effectszOptional[SpeculationEntry]current_speculationz	list[Any]r  r  exn_vt_stackzOptional[ExecutionRecorder]exec_recorderz+Optional[Callable[[VariableTracker], bool]]strict_checks_fnstart_pointis_leaf_tracerz#Optional[InstructionTranslatorBase]parentz3list[tuple[VariableTracker, list[VariableTracker]]]debug_localsOptional[CompilePackage]packagez
deque[str]latest_bytecode_queuec                    d| _         y)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)rL  r   s    r   mark_inconsistent_side_effectsz8InstructionTranslatorBase.mark_inconsistent_side_effects  s     *.&r   c                   | }||j                   j                  }|j                  J |j                  |j                  d  D ]8  }|j                  dv r n(|j                  t
        v s&|j                  }||k  s8 y |j                  }|y)N)RETURN_VALUERETURN_CONSTTF)rG  offsetr   r_  r   r   r`  rS  )r   cur_tx
cur_offsetr   jump_offsets        r   r|  z,InstructionTranslatorBase.maybe_has_backedge  s    * 7; 33::J--999++F,F,F,HI $;;"BB;;,."&++K"Z/#$ ]]F   r   c                     | j                   d   S )Nco_cellvarsr{  r   s    r   cellvarsz"InstructionTranslatorBase.cellvars        //r   c                     | j                   d   S )Nco_freevarsrc  r   s    r   freevarsz"InstructionTranslatorBase.freevars  re  r   c                ~    t        | d      s&| j                         | j                         z   | _        | j                  S )N_cell_and_freevars)r   rd  rh  rj  r   s    r   cell_and_freevarsz+InstructionTranslatorBase.cell_and_freevars  s2    t12&*mmo&GD#&&&r   c                V   | j                   j                         D ci c]  \  }}|| j                         v r|| c}}| _        t	        | j
                  | j                        }| j                   j                         D ci c]  \  }}||v s|| c}}| _         y c c}}w c c}}w r   )r?  r   rk  rC  r   r_  rG  )r   r   r   readss       r   prune_dead_localsz+InstructionTranslatorBase.prune_dead_locals  s     ,,224-
1D**,, qD-
) "$"3"3T5M5MN!11779 
QQ%ZAqD 
-
 
s   BB%B%c                   t        |t              sJ t        |t              sJ t        |t              sJ t	        d t        j                  ||j                               D              sJ d }t        |d      r|j                  }t        |d      r|j                  }|r$t        |      rt        |      rt        d|       | j                  |j                  | ||             y )Nc              3  <   K   | ]  }t        |t                y wr   )r@  r]   )r   rL  s     r   r   z:InstructionTranslatorBase.call_function.<locals>.<genexpr>  s      
 q/*
   r  r<  z$Attempt to trace forbidden callable )r@  r]   r   r   all	itertoolschainvaluesr   r  r<  callablerQ   AssertionErrorr0  r1  )r   r<  r   kwargsr  s        r   r1  z'InstructionTranslatorBase.call_function  s     "o...$%%%&$''' 
__T6==?;
 
 	
 
 2wxxH2tuuH*|H/E #Gz!RSS		"""4v67r   c                ^    t        |t              st        |      }|j                  | ||      S )zD
        Redirect the call to the generator "call_function"
        )r@  rj   r1  r   r<  r   rx  s       r   inline_generator_functionz3InstructionTranslatorBase.inline_generator_function  s.     "<=/3BdF33r   c                    d| _         t        j                  r,t        |j	                               r| j                  |||      S t        j                  | |||      S )zF
        A call to some user defined function by inlining it.
        F)rR  r   "enable_faithful_generator_behaviorr,   get_coder{  InliningInstructionTranslatorinline_callrz  s       r   inline_user_function_returnz5InstructionTranslatorBase.inline_user_function_return  sN     $44bkkm9T11"dFCC0<<T2tVTTr   c                    || j                   }| j                  dkD  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   z in )r   rK  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;7H7H17Lt0013RT 	   7 7@%-rRz3C++))*!F848K8K7L\N[kZlmmr   c                    d| j                          d}t        j                  | j                  j                  | j
                        j                         }|d| z  }|S )NzTRACE starts_line r   z    )r  	linecachegetliner}  r  r   rstrip)r   log_strlines      r   get_log_starts_line_log_strz5InstructionTranslatorBase.get_log_starts_line_log_str  sX    &t'C'C'E&FbI  !8!8$++FMMOT$= r   c                   | j                   |k(  ry || _         t        j                  | j                  j                  || j                  j
                         | j                  r*t        j                  dt        | j                               y y )N%s)r   r   set_current_locr}  r  r  is_trace_source_log_enabledtrace_source_logr  rY   r  )r   r   s     r   starts_linez%InstructionTranslatorBase.starts_line   sl    ;;& &&KK##VT[[-@-@	
 ++""4D4T4T)UV ,r   c                   t               | _        | j                  }|y| j                  |   x| _        }|dz   | _        |j
                  r| j                  |j
                         | j                  sb| j                         rR| j                         rB| j                         | _
        | j                  j                  |       r| j                  |       y| j                  r?t        j                  d|j                   |j"                  t%        | j                               t&        j(                  rW	 t%        | j                        }| j,                  j/                  d|j                    dt%        |j"                         d|        | j1                  |       	  | j2                  |j4                     | |       | j6                  j8                   S # t*        $ r d}Y w xY w# t:        $ r  t<        j>                  $ r}| jA                  |       Y d}~yd}~wtB        tD        f$ r Y ytF        tH        f$ r}| jJ                  s| j                  s| jL                  r2tO        |tH              r!tQ        d	d
dddgtR        jT                          | j                  GtV        j                  d       tO        |tH              r!tQ        dd
dddgtR        jT                          dtY        |       }| j[                  | j\                  ||j^                         Y d}~nd}~ww xY w| j                  ja                  | j                         y)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  Tz3cannot 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.rn  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.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  )1rR   r   r   r_  rG  r  rE  ry  is_non_empty_graphr  rM  r   step_graph_breakis_trace_bytecode_log_enabledtrace_bytecode_logr  r   r`  reprr   verbose
ValueErrorrW  r   update_block_stackr  r  r  r  r   r   r   exception_handlerr+  r-  r;   r9   r  is_tracing_resume_prologuer@  r:   r   
DYNAMO_BUGr  r   rz  r{  r  r   )r   ipr   
stack_reprr  r   s         r   stepzInstructionTranslatorBase.step  s   $=$?!%%:*.*;*;B*?? 4#%6 T--. 

113'')'+~~'7D$''..t4%%d+--$$ $++t{{D<L >>P!$**-
 &&--QtDKK'8&9:,G 	%>	,D,T48{{....  PO
P . 	$$ 	""1%|, 	_- 4	 ,,22a1! U "%` PW /99
 ''/		MNa1! X "%a Pk /99	 - .1VH6    !!<< !  a4	l 	  ::4;T;TUs=   0G 6G  GG L<HL&L4CLLr  c                   |j                   }|rt        | j                        dk\  rg|j                  | j                  d   j                  urA|j                  | j                  d   j                  u r| j                  j	                          y y y y | j                  rD|j
                  dvr5t        | j                        dk(  sJ | j                  j	                          y y y )N   r  )NOPJUMP_BACKWARD	NOT_TAKENr   )r  r   r  r  r  r   )r   r   r   s      r   r  z,InstructionTranslatorBase.update_block_stackz  s     &&E (()Q.D,<,<R,@,G,GG(8(8(<(C(CC $$((* D H /" ## < ) t//0A555$$((*)#r   c                     y r   r   r   r   s     r   r  z,InstructionTranslatorBase.update_block_stack  s    r   c                P    | j                   J | j                  | j                      S r   )r   r_  r   s    r   r  z*InstructionTranslatorBase.next_instruction  s+    ''333  !9!9::r   c           	     @
   | j                   j                  rJ | j                  J | j                  rJ t        j                  d       | j                   j                  | dt        d| j                         g            }| j                  rTddl
m} t        j                  sJ t        | j                   j                        }|j!                  g t#        d      |j%                  d      |j'                         t)        d	d
      t#        d      |j%                  d      |j'                         t)        d	d
             | j+                  d||d   g |dd      \  }} ||       | j-                  |g|g|       |j!                  g t#        d      |j%                  d      t)        d      t#        d      |j%                  d      t)        d             |d   j.                  }|j!                  t)        d	d
      gt#        d      |j%                  d      |j'                         t1        ||d             | j                  j3                  t5                      |d   xj.                  dz  c_        |j!                  t7               |j%                  d      |j'                         gt1        d|dz                |j!                  g t#        d      |j%                  d      |j'                         t7               t1        |dz   d       t9        d      |j%                  d      t)        d             |j!                  t)        d|dz   
      t)        d	|dz   
      t)        d|dz   
      g       | j                   j;                  |j=                         | j                  j?                  | j                  j@                  |dd        z          y | j                   j;                  g t9        d      t)        d             t        | j                   j                        }| j                   j;                  |j%                  d      |j'                         g       |d   jB                  jE                         D ]X  \  }}	| j                   j;                  t7               |j%                  |	      |j'                         |jG                  |      g       Z | j                   j;                  t)        d      tI        |      g| jJ                         y )Nzstep triggered compileTstep_unsupported)partial_convertr   r   )	skip_coder  r   
BUILD_LISTr  FDELETE_SUBSCRrv  STORE_SUBSCRUNPACK_SEQUENCEPOP_TOPr  )&r  output_instructionsrM  rE  r  r  r  r?   r  rS  
eval_framer  r   nested_graph_breaksr0   r  r  r$   create_load_constcreate_binary_subscrr&   create_resumecodegen_call_resume	num_stackr!   r0  r}   r%   r)   r  rT  r  r  locals_namesr   create_storer'   r_  )
r   continue_instr  r  r  leaf_resume_codeleaf_resume_namer  localidxs
             r   r  z*InstructionTranslatorBase.step_graph_break  sK   ;;2222''333 ::~ 			*+$(KK$@$@ %&84;M;M;O:PQ %A %
! ;;- ----4;;../B 	 ^	((+	 ++-	 '|;		
 !^	 ((+	 ++-	 '|;	 261C1C=";A">Be2.. &' $$&6%7:J9KRP  ^((+ '7 !^	
 ((+ '7	 2!4>>I&|; ^ ((+ ++-	
 )ItD KK_./%a(22a72
 "$((+++- )IM:	 	 ^	((+	 ++-	 #$		
 )Q=	 !^	 ((+	 '~6	" &'8i!mL&|QG&'8i!mL" KK//##%++33KK002KAB2O KK// ^&y1 4;;../BKK//((,++- 8;HHNNP 
s33&(,,S1//1.	 KK//&y1(7 &&r   c                ,    t        j                  d       S r   )r   current_framer   s    r   run_ctx_mgrz%InstructionTranslatorBase.run_ctx_mgri  s     ++D11r   c           
     ,   | j                         5  t        | j                  j                         	 | j                  j                  |        | j                  | _        	 | j                         r	 | j                         r| j                  jA                          tC        | tD              r5| j                  jG                          | j                  jI                          	 d d d        y # t        $ rY}| j                  rGt        dt        |      j                   dt        |             j                  |j                         d  d }~ww xY w# t"        $ r  t$        $ r  t&        $ r}t)        |d      rpd|j*                  v rbt,        j.                  j1                  | j                  j2                  | j                  j4                        j7                  ddd      }||_          d }~wt        $ r1}| j:                  r| j:                  j=                         |_         d }~ww xY w# | j                  jA                          tC        | tD              r5| j                  jG                          | j                  jI                          w w xY w# 1 sw Y   y xY w)NzError while tracing through a Dynamo-generated resume function prologue. Errors are not allowed when tracing resume function prologues.
r   rJ  zData-dependentFT)print_outputinclude_strideinclude_device)%r  r   r}  r  r  push_txr   rQ  r  	Exceptionr  r8   r  r   r   with_traceback__traceback__r   r2   RuntimeErrorr   rJ  r  r  GraphModule
nn_modulesgraphprint_readablepartial_fx_graphrO  
get_recordexec_recordpop_txr@  r6  r  mark_bytecode_tracing_stop)r   r  readable_graphs      r   runzInstructionTranslatorBase.runo  s*    1	=dkk--./=##D)#'#;#; 
))+ ))+> ""$ d$9:KK'')
 KK::<c1	= 1	= ! 668_#Aw334Bs1vh@ ).9t	D
  2 (  
1e$)9QUU)B%*XX%9%9..0A0A&$n%*4PT %  #
 *8A& %%$($6$6$A$A$CAM	 ""$ d$9:KK'')
 KK::< ;W1	= 1	=sh    J
,E!C)AJ
)	E2AEEEH#)A=G&&H#2,HH##H&&A!JJ

Jc                    |#t        |t              sJ dt        |              | j                  j	                  |       y )Nz"push expects VariableTracker, got )r@  r]   r[   rE  r   r   s     r   r0  zInstructionTranslatorBase.push  s?    {jo> 	
0?	
> 	

#r   c                4    |D ]  }| j                  |        y r   r0  )r   valsr!  s      r   	push_manyz#InstructionTranslatorBase.push_many  s     	CIIcN	r   c                6    | j                   j                         S r   )rE  r  r   s    r   r  zInstructionTranslatorBase.pop  s    zz~~r   c                p    g t        t        |      D cg c]  }| j                          c}      S c c}w r   )r   r  r  )r   nr   s      r   r2  zInstructionTranslatorBase.popn  s*    :uQx8!488:89::8s   3
c           	        |j                   }| j                  r7|| j                  v r)| j                  j                  || j                  |          	 | j	                  | j
                  |   j                                |j                  d      r| j
                  j                  |       y y # t        $ r |j                  d      rh	 |j                  dd      }| j	                  | 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 w xY w)Nr?  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 comprehensionsrn  z*Attempted to read undefined local variablez+Could not find a local variable with name `__stack)r`  rO  f_localsadd_local_varr0  r?  unwrapKeyErrorrP  replacer:   r   r  r  )r   r   r   new_names       r   	LOAD_FASTz#InstructionTranslatorBase.LOAD_FAST  sP   {{$$--"7,,T4==3FG	IId**40779:4 ??9%  $$T* &3  	s##||C<HIId228<= 	! W",TF 3&[\`[aab$cF.99		 H(/"MdVST U9-889	#	s6   ,B+ +E0C76E70D*'E)D**)EEc                   |j                   | j                         v sJ | j                  |j                      }| j                  j                  j                  |      }| j                  |       | j                  rW|j                   | j                  v r>| j                  j                  |j                   | j                  |j                             y y y r   )
r`  rk  r?  r  side_effects	load_cellr0  rO  r  r  )r   r   cellcontents_vars       r   
LOAD_DEREFz$InstructionTranslatorBase.LOAD_DEREF  s    {{d446666##DKK0{{//99$?		,$++">,,T[[$--:TU #?r   c                    |j                   }| j                         }|j                  |       || j                  |<   |t        k(  r+|j                         }t        |      t        u sJ || _        y y r   )	r`  r  set_name_hintr?  rF   r  r  r   r  )r   r   r   	loaded_vtr!  s        r   
STORE_FASTz$InstructionTranslatorBase.STORE_FAST  sj    {{HHJ	%%.T"55..0C9$$$.1D+ 6r   c                2    | j                   |j                  = y r   )r?  r`  r  s     r   DELETE_FASTz%InstructionTranslatorBase.DELETE_FAST  s      -r   c                R   |j                   | j                         v sJ | j                  |j                      }| j                         }| j                  j
                  j                  ||       t        |t              sJ |j                  |j                  |j                         y y r   )r`  rk  r?  r  r  r  
store_cellr@  rx   
local_namer  )r   r   r  r!  s       r   STORE_DEREFz%InstructionTranslatorBase.STORE_DEREF  s    {{d446666##DKK0hhj  ++D#6$---??&doo. 'r   c                   |j                   }| t        j                  |j                        S | j                  |   }|s<t        j                  |j                        | j                  |<   | j                  |   }|J |S )Nr  )r  rb   ra  r`  _constants_cache)r   r   rQ  r!  s       r   _load_constz%InstructionTranslatorBase._load_const  sv    HH9#**==##A&'7'>'>T[['QD!!!$''*C
r   c                D    | j                  | j                  |             y r   )r0  r  r  s     r   r\  z$InstructionTranslatorBase.LOAD_CONST  s    		$""4()r   c           	        |j                   }| j                  rn|| j                  v r*| j                  j                  || j                  |          n6|| j                  v sJ | j                  |   | j                  j
                  |<   || j                  vr| j                  |      S || j                  v r\| j                  j                  | j                  |      }| j                  | j                  j                  j                  ||             y | j                  |   }| j                  t        j                  | |t        |                   y r   )r`  rO  	f_globalsadd_global_var
f_builtinsbuiltinsload_builtinr@  r  r  r0  load_globalr]   buildrJ   )r   r   r   variabler  s        r   _load_globalz&InstructionTranslatorBase._load_global  s   {{t~~%""11$t8LMt...48OOD4I""++D1t~~%$$T**4((({{//0E0Ed0KLHIIdkk..::8TJKt$		/''e\$5GHIr   c                l    d}| j                  |      }t        |      }t        j                  | ||      S )Nztorch.nn.modules.module)import_sourcer   r]   r  )r   module_namemodule_sourcefglobals_values       r   nn_modules_globals_vtz/InstructionTranslatorBase.nn_modules_globals_vt  s6    /**;7'4$$T>=IIr   c                8   |j                   J t        j                  dk\  r3t        j                  dk  r |j                   dz  r| j                  |       | j	                  |       t        j                  dk\  r"|j                   dz  r| j                  |       y y y )Nr  ru  r  )r  rD  r  	PUSH_NULLr  r  s     r   LOAD_GLOBALz%InstructionTranslatorBase.LOAD_GLOBAL"  s~    xx###w&3+;+;g+E$((UV,NN4 $w&488a<NN4  ,8&r   c                   | j                         }|j                  }t        |      }|| j                  vrt	               | j                  |<   | j
                  j                  j                  || j                  |         }t        |t              rt        d|dg        | j
                  j                  j                  |||       y )Nz%Storing Tensor hook handle in globalsThis is not supported.rn  )r  r`  rJ   r@  objectr  r  track_global_existingr@  r   r:   store_global)r   r   r  r   r  r  s         r   STORE_GLOBALz&InstructionTranslatorBase.STORE_GLOBAL*  s    
{{d#t,,,*0(D!!$';;++AAD))$/
 e45?4	 	  --heDr   c                   d|v rZt         j                  j                  j                  |   }|j	                  dd      j	                  dd      j	                  dd      }n t        |      }d|j	                  dd       }| j                  | j                  j                  ||       || j                  j                  |<   | j                  j                  }||vs	||   |u sJ |||<   | j                  j                  |       t        |      S )z-Create an alias to a module for use in guardstorch_package>r   <r?  _dot_	__import_)r  rV  package_importer_package_imported_modulesr  r   add_import_sourcer  import_sourcesglobal_scopeupdate_co_namesrJ   )r   r  r  aliasr	  s        r   r  z'InstructionTranslatorBase.import_source>  s    k)MM22LLE ##C-55c3?GGWU  #;/E 3 3C ABCE<<#LL**5+>,7""5)KK,,	I%5)9U)BBB 	%##E*E""r   c                ~    |j                  d|dz
        }t        |      |k  rt        d      |d   }|r| d| S |S )z
        Copied from the Cpython implementation of __import__
        Resolve a relative module name to an absolute one.
        https://github.com/python/cpython/blob/5a094f0255eea1db58fb2cf14c200971e64ec36e/Lib/importlib/_bootstrap.py#L902
        r?  r   z2attempted relative import beyond top-level packager   )rsplitr   ImportError)r   r   rV  levelbitsbases         r   resolve_namez&InstructionTranslatorBase.resolve_nameU  sQ     ~~c519-t9uRSSAw#'$q1T1r   c                   | j                   j                  d      }| j                   j                  d      }|6|2||j                  k7  r#t        j	                  d||j                  d       |S ||j                  S t        j	                  dd       | j                   d   }d| j                   vr|j                  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__z)__package__ != __spec__.parent (%r != %r)rv  )
stacklevelzYcan't resolve package from __spec__ or __package__, falling back on __name__ and __path__r   __path__r?  r   )r	  r  rS  r  warning
rpartition)r   rV  specs      r   calc_packagez&InstructionTranslatorBase.calc_packagea  s    
 ..$$]3~~!!*-Gt{{$:?KK 	   N;;KK8  
 nnZ0G/!,,S1!4r   c           
     l   | j                  d      \  }}|j                         }|j                         }|j                  }t        j                   d| d| d| }|| j
                  v r| j
                  |   }t        |      }n{	 t        |||| j
                        }|d
k7  r#| j                         }| j                  |||      }|s&|j                  d      d
   }	| j                  |	      }n| j                  |      }| j                   r| j                   j#                  |       t%        t&        j(                  t*        f      r| j-                  t/        ||             y t        dt1        |      dg 	       y # t        $ r, t        dd| d| d| dg t        j                  	       Y w xY w)Nr  r   )fromlistr1  globalszImport failurezmodule_name: z, fromlist: z, level=z"Failure when attempting to import.rn  r   r?  r  zBad import resultz%Import result is not a Python module.)r2  r  r`  rD   LOCAL_MOD_PREFIXr	  rJ   
__import__r0  r:   r   r  r=  r4  	partitionr  rO  add_local_modrX   rA  rB  rC   r0  r|   r[   )
r   r   r1  r?  r  recorded_namer  r  pkgtop_level_module_names
             r   IMPORT_NAMEz%InstructionTranslatorBase.IMPORT_NAME~  s   ))A,x((*..0kk !112!E7!H:Q{mT 	 DNN*NN=1E!-0F"% NN	 z'')"//S%H (3(=(=c(B1(E%++,AB++K8,,]EB %%**K89II*5@A+CA  ,+K=XJhW\V]^ D9-889	 s   E> >1F32F3c                \    | j                  |       | j                  |j                         y r   )DUP_TOP
_load_attrr`  r  s     r   IMPORT_FROMz%InstructionTranslatorBase.IMPORT_FROM  s    T$r   c                b   || j                   vr#t        ddd| dg t        j                         | j                   |   }t	        |      rBt        | 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.rn  r  )r  r:   r   r  rv  rJ   r  %name_of_builtins_dict_key_in_fglobalsrI   r]   r  rP   rb   ra  )r   r`  r!  builtins_source
var_sources        r   load_builtin_from_argvalz2InstructionTranslatorBase.load_builtin_from_argval  s    (?3F8;QR&11	 oof%C=*AAO +?FCJ"((sJ??&s+++#**55r   c                X    | j                  | j                  |j                               y r   )r0  rR  r`  r  s     r   r  z&InstructionTranslatorBase.load_builtin  s    		$//<=r   c                   | j                   J | j                  J |j                  J t               j	                  d| j                   | j                  z
         | j
                  |j                     | _         | j                   | _        y )Nir_count)r   rQ  r  rV   	incrementindexofr  s     r   r  zInstructionTranslatorBase.jump  s    ''333+++{{&&&''0043C3CC	
 $(<<#< 33r   FTc           	         |j                   J | j                  j                  t        ||j                   t	        | j
                                     y r   r  r  r   r  r   rE  r  s     r   
SETUP_LOOPz$InstructionTranslatorBase.SETUP_LOOP  ;    {{&&&dkk3tzz? STr   c           	         |j                   J | j                  j                  t        ||j                   t	        | j
                                     y r   rY  r  s     r   SETUP_EXCEPTz&InstructionTranslatorBase.SETUP_EXCEPT  r[  r   c                8    | j                   j                          y r   )r  r  r  s     r   	POP_BLOCKz#InstructionTranslatorBase.POP_BLOCK  s    r   c                &    | j                  |       y r   setup_or_before_withr  s     r   
SETUP_WITHz$InstructionTranslatorBase.SETUP_WITH      !!$'r   c           	         |j                   J | j                  j                  t        ||j                   t	        | j
                                     y r   rY  r  s     r   SETUP_FINALLYz'InstructionTranslatorBase.SETUP_FINALLY   s;    {{&&&dkk3tzz? STr   c                &    | j                  d        y r   r  r  s     r   BEGIN_FINALLYz'InstructionTranslatorBase.BEGIN_FINALLY  s    		$r   c                    | j                  d      \  }}|J | j                  |       | j                  |j                  | t        j                  d       gdz  i              y )Nr  rv  )r2  r0  r1  rb   ra  )r   r   r%  r   s       r   WITH_CLEANUP_STARTz,InstructionTranslatorBase.WITH_CLEANUP_START  sX    IIaL	c{{		#		$$$T,<,C,CD,I+JQ+NPRSTr   c                H    | j                  d       | j                  d        y Nr  )r2  r0  r  s     r   WITH_CLEANUP_FINISHz-InstructionTranslatorBase.WITH_CLEANUP_FINISH  s    		!		$r   c                   | j                         j                         }| j                  |       	 |j                  |       }| j                  |       y # t        t
        j                  f$ r}t        |t
        j                        rt        j                  |        t        j                  dk\  r%| j                  t        j                  d              n| j                          | j                  |       Y d }~y d }~ww xY wNr  )r  realizer0  next_variableStopIterationr   ObservedUserStopIterationr@  r  rD  r  rb   ra  r  )r   r   itr!  r  s        r   FOR_ITERz"InstructionTranslatorBase.FOR_ITER  s    XXZ!		"	""4(CIIcNs<<= 	!S::;--d37*
 		*11$78 
IIdOO	s   "A C?-BC::C?c                l    t        |t        j                  t        f      r|j	                  | g i       }|S r   )r@  r   ra   r   r1  r   s     r   _create_exception_typez0InstructionTranslatorBase._create_exception_type'  s6    )++-NO

 ##D"b1C
r   c                    | j                  |      }t        | j                        rVt        |t        j
                        r<|j                  t        u r*t	        j                  t              j                  | g i       }| j                  j                  |       | j                  |      r*t        j                  |j                        } |d|       t!        dt#        t              dg t$        j&                         y )Nraised exception zFailed to raise exceptionz.Attempted to raise a non-Exception type/value.rn  )rw  r,   r}  r@  r   ry   exc_typerr  ra   r  r1  rN  r"  _isinstance_exceptionr   get_dynamo_observed_exceptionr:   r   r   r  )r   r!  observed_exception_types      r   _raise_exception_variablez3InstructionTranslatorBase._raise_exception_variable0  s     ))#.
 %3	 ; ;<-++L9GGbRTUC 	//4 %%c*&)&G&G&U#),=cU*CDD/HH1%001		
r   c                &   |j                   dk(  rt        | j                        s(t        d      }t	        j
                  t        | |g       t        | j                        sJ | j                  d   }| j                  |      sJ |       | j                  |       y |j                   dk(  r!| j                  d   }| j                  |       y | j                         }| j                         }	 | j                  |       | j                  j                         }| j                  |      }|j                  | t        d      |       y # | j                  j                         }| j                  |      }|j                  | t        d      |       w xY w)Nr   zNo active exception to reraiser  r  r   	__cause__)r  r   rN  rb   r   r  r  r{  r~  rE  r  r&  rw  call_setattr)r   r   rJ  r!  from_vtcurr_exccauses          r   rR  z'InstructionTranslatorBase.RAISE_VARARGSO  sW   88q=t(()&'GH,,\4seL t(()))##B'C--c27C72**3/XX]**R.C**3/ hhjG((*CR..s3  ,,BBD33G<%%d,<[,I5Q  ,,BBD33G<%%d,<[,I5Qs   .E A	Fc                    | j                   d   }t        |t              sJ |j                  t        u rt        dddg        y | j                  |       y )Nr  z CLEANUP_THROW with StopIterationr   zRReceived StopIteration when handling generator.throw/close. This is not supported.rn  )rE  r@  ry   rz  rr  r:   RERAISEr   r   toss      r   CLEANUP_THROWz'InstructionTranslatorBase.CLEANUP_THROWk  sN    jjn#0111<<=(:p	 LLr   c                n   t         j                  dk\  ra| j                         }|j                  r"| j                         }| j	                  |       y | j                  |       | j	                  |       y | j                         }| j                         }| j                         }| j	                  |       y )Nr  )rD  r  r  r`  r~  r0  )r   r   r!  r   _exc_tbs         r   r  z!InstructionTranslatorBase.RERAISEy  s     w&((*C{{HHJ..s3 		#..s388:D((*C((*C**3/r   c                L    t        |t        j                  t        t        f      S r   )r@  r   ry   r   r   r   s     r   r{  z/InstructionTranslatorBase._isinstance_exception  s&    ++12
 	
r   c                   g }t         j                  dk\  rt         j                  dk  rdnd}t        | j                        |k\  sJ | j                  |    }| j                  d   }| j	                  |      sJ t        |j                        }t        d       }t         j                  dk\  rt        | j                  d   t              s|j                  | j                  d          nkt        | j                        dk\  sJ | j                  d   }| j                  d	   }| j	                  |      sJ t        |j                        }t        d       }||||gz  }| j                  ||i        y )
Nr  rv           r     ir  )rD  r  r   rE  r{  ra   rz  rb   r@  r{   r   r1  )r   r   r   fn_locr<  r!  typtbs           r   WITH_EXCEPT_STARTz+InstructionTranslatorBase.WITH_EXCEPT_START  s<   &(w&**W4Q!F tzz?f,,,VG$B**R.C--c222!#,,/C!$'B7*!$**R.,?KK

2/tzz?a'''BB**R.C--c222!#,,/C!$'Bc22tR(r   c           	     	    dd fd}t         j                  dk\  r  j                  j                  }|rt	         j
                        |j                  kD  r3 j                          t	         j
                        |j                  kD  r3|j                  r8 j                  t        j                   j                  j                                j                   j                  j                                 j                  |       y  j
                  j!                          t#               t$        u r |        t	         j&                        r< j&                  j                         }|j(                  j*                  dk(  rω j-                  d        j                  j                          t	         j&                        dk(  rX j
                  j!                          t#               t$        u r+t/        dt1              dz   g t2        j4                  	        j&                  j                         }|j(                  j*                  dk(  rω j                  j                         } j                  j7                          t	         j
                        |j8                  kD  r3 j                          t	         j
                        |j8                  kD  r3t;        d
dd d      } j&                  j=                  t?        |d t	         j
                                     t	         j                        dk\  rr j                  d   } j                  t        j@                                 j                  |        j                  t        jB                  |jD                               nl j                  t        j                  d               j                  t        j                  d               j                  t        j                  d               j                  t        j@                                 j                  |        j                  t        jB                  |jD                                j                  |       y  j
                  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.c                 :   j                   j                         } t        j                  | j	                               }t        |      sJ t        dd| j                          d| j                   dg t        j                  t        j                         y )NzObserved exceptionry  r   r  r  )rN  r&  r   r|  python_typer@  r:   python_type_namer   r   r  r  )r  
dynamo_excobserved_exn_gb_explanationraised_exceptionr   s     r   bubble_exception_to_interpreterzTInstructionTranslatorBase.exception_handler.<locals>.bubble_exception_to_interpreter  s    ((>>@H::8;O;O;QRJ.
;;;,+H,E,E,G+H(--XYZ7&11&22 *	r   r  EXCEPT_HANDLERrv  r   z#Observed exception (EXCEPT_HANDLER)z  This graph break is unexpected.rn  g    .Ar  r  r   )#rD  r  rG  r  r   rE  depthr  lastir0  r   rb   r]  rN  r&  r  r   r  r6  r  r   r   r2  r:   r   r   r  r$  r  r+   r   r  r}   ra   rz  )	r   r  r  r  block_stack_entryexception_varexcept_handler_instold_exceptionr  s	   ``      @r   r  z+InstructionTranslatorBase.exception_handler  s   d 	$
	  w& 44BBM
 $**o(;(;;HHJ $**o(;(;; !&&II!2243K3K3R3RS
 		$++AACD 		-( 

  ":!6635&&4##$ %)$4$4$8$8$:!',,337GG IIaL%%))+4++,1 

((*:)>>)(M(+,<(=,G"D-E&E(9(D(D&E /.(,(8(8(<(<(>%' (,,337GG* !% 1 1 G G I!!AAC $**o(9(E(EEHHJ $**o(9(E(EE
 '2#7Gq&Q#  ''#$7s4::O
 t(()Q.$($5$5b$9M IIi779:IIm,IIi778N8NOP IIi88>?IIi88>?IIi88>? 		)3356		-(		)33M4J4JKL 		+, 

  ":!6635&&r   c                   | j                         }t        | j                        dk(  rt        d       }n| j                  d   }| j	                  |       | j	                  |       | j                  j                          y )Nr   r  )r  r   rN  rb   r0  r$  )r   r   r!  prev_excs       r   PUSH_EXC_INFOz'InstructionTranslatorBase.PUSH_EXC_INFO6	  se    ( hhjt  !Q&(8(>H((,H		(		#99;r   c                   t         j                  dk\  rB| j                         }t        | j                        sJ | j                  j                          y t        | j
                        dkD  sJ | j
                  d   j                  j                  dk7  rt        d      | j
                  j                          | j                  d       t        | j                        sJ | j                  j                          y )Nr  r   r  r  zYBug in Dynamo tracing of exception handling.Top of the block stack is not EXCEPT_HANDLER.rv  )
rD  r  r  r   rN  r  r   r   rw  r2  )r   r   r   s      r   
POP_EXCEPTz$InstructionTranslatorBase.POP_EXCEPTS	  s    w&
At(()))!!#t''(1,,,#((//3CC$D    "IIaL t(()))!!#r   c                   t        | j                        dk\  sJ | j                         }t        j                  dk\  r| j                  d   }n| j                  j                         }t        |t        t        t        t        f      s,t        dt        |      d| dg t        j                         t        j                  dk\  r=| j                  |      s,t        dt        |      d	| dg t        j                         t        |t              r|j                  }n|g}|D ]  }t        |t        t        t        f      s,t        d
t        |      d| dg t        j                         | j                  |      r"t!        |j"                  |j$                        r yt        |t&        j                        st!        |j$                  |j$                        s y y)Nr  r  r  z Exception with bad expected typez"`except ...` has unsupported type r?  rn  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   rE  r  rD  r  r@  ra   rw   r   r   r:   r   r   r  r{  r   
issubclassrz  r<  r   )r   expected_exc_typesexc_instanceexpected_typesexpected_types        r   check_if_exc_matchesz.InstructionTranslatorBase.check_if_exc_matchesh	  s   4::!###!XXZw&  ::b>L
  ::>>+L 12	
 :./@AS@TTUV5)445	 w&--l;8-"efresst u9-889	 (-8/55N #N , 	M#65 A."CM?RS T9-889	 )),7J%%  = L)*C*CD  J
 3	6 r   c                h    | j                  t        j                  | j                                      y r   )r0  r   rb   r  r  s     r   CHECK_EXC_MATCHz)InstructionTranslatorBase.CHECK_EXC_MATCH	  s"    		),,T-F-F-HIJr   c                H    | j                         s| j                  |       y y r   )r  r  r  s     r   JUMP_IF_NOT_EXC_MATCHz/InstructionTranslatorBase.JUMP_IF_NOT_EXC_MATCH	  s    ((*IIdO +r   c                    |j                   dk(  r| j                  |       y | j                  t        |j                      | | j	                  d      i              y )Nzexception matchr  )r`  r  r0  r   r2  r  s     r   
COMPARE_OPz$InstructionTranslatorBase.COMPARE_OP	  sD    ;;++  &II)$++6tTYYq\2NOr   c                b    | j                  t        t              | j                         gi        y r   )r1  ra   iterr  r  s     r   GET_ITERz"InstructionTranslatorBase.GET_ITER	  s!    ?40488:,Cr   zjEncountered graph break when attempting to trace CALL_FUNCTION: a call to a regular function, e.g. f(x, y))r0  r  c                    | j                  |j                        }| j                         }| j                  ||i        y r   r2  r`  r  r1  )r   r   r   r<  s       r   CALL_FUNCTIONz'InstructionTranslatorBase.CALL_FUNCTION	  s3    
 yy%XXZ2tR(r   zjEncountered graph break when attempting to trace CALL_FUNCTION_EX: a variadic function call, e.g. f(*args)c           	     v   |j                   dk(  rt        i       }| j                         }n|j                   dk(  st        j                  dk\  r<| j                         }t        |t              rt        i       }| j                         }n9t        dd|j                    d|j                    g t        j                         t        j                  dk\  r"| j                         }t        |t              sJ | j                         }t        j                  d	k\  r5t        j                  dk  r"| j                         }t        |t              sJ t        t              s+|j                  |       rt        |j                  |             }t        t              rt        j                   | t"        |      }t        |t              rt        |t              s7t        d
dt%        |       dt%        |       dg t        j&                         |j)                         }| j+                  ||j,                  |       y )Nr   r   r  z%Variadic function call with bad flagszflags: zHAttempted to call a variadic function (CALL_FUNCTION_EX) with bad flags rn  ru  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`  rg   r  rD  r  r@  r{   r:   r   r  rr   has_force_unpack_var_sequencerw   force_unpack_var_sequencer   ra   call_custom_dictr   r[   r  keys_as_python_constantr1  r   )r   r   
kwargsvarsargsvarsnullr<  s         r   CALL_FUNCTION_EXz*InstructionTranslatorBase.CALL_FUNCTION_EX	  s    ;;!*2.JxxzH[[A!1!1W!<J*l3.r2
xxzH?!$++/fgkgrgrfst5)445	 w&88:DdL111XXZw&3+;+;g+E88:DdL111

 44T:$X%G%G%MNH j";<(99$jQJ ($45Z>

 J%gh&7%8PZH[G\]P5)445  77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                         }| j                  |j                        }| j                         }t        |t              r|j                         sJ |j                         }|d t        |        |t        |       d  }}t        t        ||            }t        |      t        |      k(  sJ | j                  |||       y r   )r  r2  r`  r@  rw   r  r  r   r   zipr1  )r   r   argnamesr   r<  kwargs_listrx  s          r   CALL_FUNCTION_KWz*InstructionTranslatorBase.CALL_FUNCTION_KW
  s     88:yy%XXZ(M2x7R7R7TTT..0 !1CM>2D#h-9I4Jkc(K016{c(m+++2tV,r   c                (   | j                  t        j                  |d             |j                  d   }| j                  d   |   }t
        j                  dk  r| j                  |       y | j                  t        j                  ||             y )Nr  r  r   co_namesr  )	r  r   r  r`  r{  rD  r  rL  LOAD_METHODr   r   r  r`  s       r   LOAD_METHOD_SUPERz+InstructionTranslatorBase.LOAD_METHOD_SUPER(
  st    ;..tA>?kk!n"":.s3g%OOF#[00fEFr   c                    | j                  t        j                  |d             |j                  d   }| j                  d   |   }| j                  |       y )Nr  r  r   r  )r  r   r  r`  r{  rL  r  s       r   LOAD_ATTR_SUPERz)InstructionTranslatorBase.LOAD_ATTR_SUPER1
  sK    ;..tA>?kk!n"":.s3r   c                v   | j                  |j                         | j                         }t        j                  dk\  r#| j                  |       | j                  |       y t        j                  dk\  r#| j                  |       | j                  |       y | j                  |       | j                  d        y )Nru  r  )rL  r`  r  rD  r  r0  r  r   r   objs      r   r  z%InstructionTranslatorBase.LOAD_METHOD7
  s    $hhjw&IIcNNN4 ( NN4 IIcNIIcNIIdOr   c                    | j                  |j                        }| j                         }|J | j                         }| j                  ||i        y r   r  )r   r   r   dummyr<  s        r   CALL_METHODz%InstructionTranslatorBase.CALL_METHODG
  sF    yy%
}}XXZ2tR(r   c                    | j                         }t        t              j                  | |t	        j
                  |      gi       }| j                  |       y r   )r  ra   r  r1  rb   ra  r0  )r   attrr  r  s       r   rL  z$InstructionTranslatorBase._load_attrN
  sJ    hhj )77"))$/0

 			&r   c                    t         j                  dk\  r!|j                  dz  r| j                  |       y | j	                  |j
                         y )Nr  r  )rD  r  r  r  rL  r`  r  s     r   	LOAD_ATTRz#InstructionTranslatorBase.LOAD_ATTRW
  s<    w&xx!|  &$r   zkEncountered graph break when attempting to trace STORE_ATTR: storing an object's attribute, e.g. x.attr = yc                    | j                  d      \  }}t        t              j                  | |t	        j
                  |j                        |gi        y rl  )r2  ra   setattrr1  rb   ra  r`  )r   r   r!  r  s       r   
STORE_ATTRz$InstructionTranslatorBase.STORE_ATTR_
  sH    
 99Q<S .."))$++6<	
r   c                    | j                         }t        t              j                  | |t	        j
                  |j                        gi        y r   )r  ra   delattrr1  rb   ra  r`  r  s      r   DELETE_ATTRz%InstructionTranslatorBase.DELETE_ATTRk
  s>    hhj .."))$++67	
r   c                   g }|dk  sJ |dk(  r|j                  t        d             | j                  dk(  rt        d      nt        d| j                        }|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   rv  r[  r\  r  r  )extendr)   r   r&   r`  )r   r  rW  return_insts       r   codegen_return_with_popsz2InstructionTranslatorBase.codegen_return_with_popss
  s      A~~>LLQ( {{n, ~.#N4;;G 	
 		*,>y,I;W	
 r   c                    |rt        |j                  |j                        D ]  \  \  }}	}
t        t         j
                  |
         }|j                  t                      |j                  |       |j                  g t        d      |j                  |      |j                         |j                  |      t        d              |j                  D ]  \  }}	t        t         j                  |         }|j                  t                      |j                  |       |j                  g t        d      |j                  |      |j                         |j                  |j                   |j"                  |   z         t        d              t%        |      r|j&                  sJ |j&                  }t)        d|j*                         }|rlt-         j.                  |      t1         fd j                  D              }t3        |j4                        t1        fd|D              }t1        fd|D              }|j                  t               |j                  |      |j                         t               g       |D ]`  }|j                  t               |j                  |j                   |j"                  |   z         |j                         gt        d             b |j                  t        d      t        dt7        |      	      gt        d      t9        |j                   d
d             n8t1        |j"                  j;                               }t1        |j4                        }t<        j>                  dk  rt7        |      dk(  sJ d       t7         j
                        t7        |j@                        z
  } jB                  j*                  J tE        jF                   jH                   jJ                   jB                  j*                  |j*                  t1        d  jL                  D              |||t1        d  jL                  D              |t1        |j                        t1        |j                        t1        |j@                        t1        |       jN                         } tQ        jR                   jH                        jU                  dd              }|+tW        jX                  |      tQ        jR                  |      d<   |jZ                  r j\                  j_                  ||       d
}n= j\                  j_                  |ta        jb                  | jd                  |             |} jf                  * jf                  ji                  | jd                  d   |       tj        d   |jl                  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  N   K   | ]  }|v r|j                         vr|  y wr   )rk  )r   r   rm  r   s     r   r   z:InstructionTranslatorBase.create_resume.<locals>.<genexpr>
  s0      !:!4+A+A+C"C !s   "%c              3  ,   K   | ]  }|vs|  y wr   r   r   r   argnames_null_sets     r   r   z:InstructionTranslatorBase.create_resume.<locals>.<genexpr>
  s     S1AR8RQS   	c              3  ,   K   | ]  }|v s|  y wr   r   r  s     r   r   z:InstructionTranslatorBase.create_resume.<locals>.<genexpr>
  s     !TQBS=S!!Tr  r  r  r  NTr  r   z&variables should not be NULL in < 3.12c              3  H   K   | ]  }|j                   j                    y wr   )r  r]  r   r  s     r   r   z:InstructionTranslatorBase.create_resume.<locals>.<genexpr>)  s     <a!((//<s    "c              3  <   K   | ]  }|j                           y wr   )r"  r  s     r   r   z:InstructionTranslatorBase.create_resume.<locals>.<genexpr>-  s     :A!++-:rq  orig_graphmodulec                      y r   r   r   r   r   r   z9InstructionTranslatorBase.create_resume.<locals>.<lambda>9      r   r   resumesr   )7r  stack_ctx_argsstack_ctx_idxes_origr   rc   rE  append_outputr%   r  r  r)   r  r  r&   locals_ctx_argsr?  r  r  r-   r  r.   r]  r   r_  r!  r  locals_null_keysr   r!   keysrD  r  stack_null_idxesrG  rE   lookupr}  r   r  r  r/   get_contextr  weakrefrefrg  r  install_global_unsaferA  FunctionTyper	  rV  add_resume_functionrS   r  )r   r  resume_instmetaresume_codesr  is_leafhandle_inactive_ctxjr   j_origr-  r   resume_nameall_argnamesr  argnames_nullr  	stack_lennew_codeorig_graphmodule_maybepackage_namer  rm  s   `                     @@r   r  z'InstructionTranslatorBase.create_resume
  sl   T "%d&9&94;T;T"U A2DJJv4FG  !12$$R(  $Q,,S1 //1 ,,Q/	
 +>:   // a2D4H4H4NO  !12$$R(  $Q,,S1 //1 ,,T^^d>O>OPT>U-UV	
 +>:( K(%%%%%,,K,{/A/A.B CD %d&7&7EE  !--! L
 !$D$9$9 :SSSH!!T\!TTM "$((-++-"$	   	  &(,,T^^d>O>OPS>T-TU//1 %Q		 &y1&|XG !^
 )tD	 T..3356H!$"7"78Mg%}%*T,TT* 

Oc$*?*?&@@	''..:::#9#@#@KKKK$$++<4+;+;<<:)9)9::$%%&$&&'$''(,---$
("
!9!9$++!F!J!J"
 " "-EL[[&FL$$X./AB
 KK--k8DL KK--""8T^^[I 'L<<#LL,,$..4l 	H,,-2-$$r   c           
        d| _         t        | j                  j                        }g }g }g }| }d}|i|| u r|}	n|j                  }	|	j
                  dk7  r3|j                  |       |j                  |       |j                  |	       |j                  }|dz  }|it        | j                        t        |d   j                        z
  }
|s{|
dk\  sJ |j                  t        |
dz                t        |
dz         D ]  }|j                  t        d              |j                  t        d             |j!                         S |d   | u r)|j                  t        d|
             d}|
|d   _        n|j                  t        |
             t        |
dz
        D ]  }|j                  t        d              |j                  t        dd             ||d      j"                  }||d      xj"                  dz  c_        |d   j%                  t'                      |j                  g t)        d      |j+                  |d         |j-                         t/        ||d	             g }|D ]F  }|j1                  t3               |j+                  |      |j-                         gt        d             H |j1                  t        d      t        dt        |            g       |j                  |       |j                  g t        d      t5        j6                  |      t        d             g }g }t9        |      D ]L  \  }}|j;                  |||   |||      |||| u d	      \  }}|j                  |       |j                  |       N | j=                  |||       |j                  t        d             |j!                         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   r0   r  r  r  r   r   rS  r   rE  r  r  r)   r  r  r&   rT  r  r0  r}   r$   r  r  r!   r  r%   r  deepcopyrU  r  r  )r   r   r  r  txesidxesresume_instsr^  r  r  current_num_stackr   stack_insert_idxfilter_instsr  resume_namesrQ  resume_coder  s                      r   r  z/InstructionTranslatorBase.create_call_resume_atV  s   6 $( t{{**+ 13*,6: ~"$55!!^3F#S!##K0]]F1HC    

Oc%a(99/
 

 
 %)))[):Q)>?@,q01 @  !3I!>?@/?@&&(( 7d? /BSTU 5F%a(2
 [):;<,q01 @  !3I!>?@/!DE8qBLL%eAh/99Q>9GLL*+ 	Q $$U1X. '')	
 %%57GN
	
  	C"$((-++- !^		 	"9-"<SZ@	
 	& 	Q|, Q	
 .0"4 	-IAv'-';';Q)%(3$($K ,,	- 	  |R@
+N;<""$$r   c           	     H   |j                  t        d             t        t        ||             D ]  \  }\  }}|t	        |      dz
  k(  r n|j
                  rL|j                  t               |j                  |      |j                         g       |j                  ||       n"|j                  |j                  |dd             |j                  t        d              |j                  t        d      t        dt	        |       dz
        g       |j                  t        d             | d	   j
                  re|j                  |j                  d	      |j                         g       |j                  |d	   | d	          |j                  g t        d             n;|j                  t        d      g|j                  |d	   d      t        d             |j                  t               t               |j                  d	      |j                         gt        d      |j                  d	      t        d
             |j                  g t        d      t        dd      t        d      t        dd             |j                  t        dd             y)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   Fr   r  r  r  rv  r  r  LIST_EXTENDTN)r  r$   rU  r  r   rg  r%   r  r  make_function_with_closureload_function_namer)   r&   r(   r#   )r  r  r  rQ  r   codes         r   r  z-InstructionTranslatorBase.codegen_call_resume  s   4 	Q((\<)HI 	-OA|dC%))  &(,,Q///1 --dD9  !6!6tUA!FG[^,	-  	"9-"<S5F5JK	
 	Q(''((,++- )),r*:L<LM!!_ &y1**<+;UC "!_( 	  $$R('') Q $$R( #?3	
  	a"<Q7 Q
 #=a8	
 	0=>r   c                   t         j                  dk\  rJ| j                  j                  }|r2| j                  r%|j
                  | j                  d   j
                  uryt        d | j                  D              xr^ | j                   xrO | j                   xr@ | j                   xr1 | j                   xr" | j                  j                  j                  d u S )Nr  r  Fc              3  <   K   | ]  }|j                           y wr   )r  r  s     r   r   zIInstructionTranslatorBase.should_compile_partial_graph.<locals>.<genexpr>|  s     :A:rq  )rD  r  rG  r  r  r  rr  r  r   r  r  r  current_tracerrS  )r   r   s     r   ry  z6InstructionTranslatorBase.should_compile_partial_graphs  s    w&,,::E$$D<L<LR<P<W<W(W:)9)9:: :NN":---: 333: 888	: **11T9	
r   ziEncountered graph break when attempting to trace STORE_SUBSCR: trying to store subscript, e.g. x[key] = yc                Z    | j                  d      \  }}}|j                  | d||gi        y )Nrv  __setitem__r2  call_method)r   r   r!  r  keys        r   r  z&InstructionTranslatorBase.STORE_SUBSCR  s-    
 		!S#mc3Z<r   c                V    | j                  d      \  }}|j                  | d|gi        y )Nr  __delitem__r(  )r   r   r  r*  s       r   r  z'InstructionTranslatorBase.DELETE_SUBSCR  s'    99Q<SmcUB7r   c                n    | j                  |j                        }| j                  t        |             y r   )r2  r`  r0  rw   r   r   r   s      r   BUILD_TUPLEz%InstructionTranslatorBase.BUILD_TUPLE  s%    		$++&		-&'r   c                r    | j                  |j                        }| j                  t        ||              y )N)r   )r2  r`  r0  rv   r.  s      r   BUILD_SLICEz%InstructionTranslatorBase.BUILD_SLICE  s'    		$++&		-$/0r   c                    | j                  |j                        }| j                  t        |t	                            y Nmutation_type)r2  r`  r0  ru   r\   r.  s      r   r  z$InstructionTranslatorBase.BUILD_LIST  s+    		$++&		,u4D4FGHr   c                    t         j                  rt        dddg        | j                  |j                        }t        |t                     }| j                  |       y )Nzmissing BUILD_SET handlerr   z:Missing BUILD_SET bytecode handler (for testing purposes).rn  r4  )r   +inject_BUILD_SET_unimplemented_TESTING_ONLYr:   r2  r`  rh   r\   r0  )r   r   r   new_sets       r   	BUILD_SETz#InstructionTranslatorBase.BUILD_SET  sO    ==3X	 		$++&e3C3EF		'r   c           	     D   | j                  |j                        }g }|D ]#  }	 |j                  |j                  |              % | j                   ||t                            y # t        $ r. t        dt        |      | dg t        j                         Y ~w xY w)Nz-Failed to unpack object for BUILD_LIST_UNPACKzU cannot be unpacked into a list for the BUILD_LIST_UNPACK bytecode (`[*x, *y, ...]`).rn  r4  )r2  r`  r  r  NotImplementedErrorr:   r   r   r  r0  r\   )r   r   r   seqsr   seqs         r   BUILD_LIST_UNPACKz+InstructionTranslatorBase.BUILD_LIST_UNPACK  s    yy% 
	C	S::4@A
	 			#e+;+=>? ' KH#&% (2 !29-889s    A((4BBc                2    | j                  |t               y )Nr  )r>  rw   r  s     r   BUILD_TUPLE_UNPACKz,InstructionTranslatorBase.BUILD_TUPLE_UNPACK  s    t7r   c           	         | j                  |j                  dz        }t        t        |d d d   |dd d               }| j	                  t        |t                            y )Nr  r   r4  )r2  r`  r   r  r0  rg   r\   )r   r   r   ds       r   	BUILD_MAPz#InstructionTranslatorBase.BUILD_MAP  sS    		$++/*U3Q3Zqt!t-.		#A5E5GHIr   c                T   | j                  |j                        }|D cg c]#  }t        t              j	                  | |gi       % }}i }|D ]/  }t        |t              sJ |j                  |j                         1 | j                  t        |t                            y c c}w r3  )r2  r`  ra   r   r1  r@  rg   updater   r0  r\   )r   r   r   rL  r  s        r   BUILD_MAP_UNPACKz*InstructionTranslatorBase.BUILD_MAP_UNPACK  s    		$++&MRS&44TA3CSS!# 	#Aa!2333MM!''"	# 			.0	
 Ts   (B%c           
     d   | j                         }| j                  |j                        }t        |t              sJ |j                         sJ |j                  |       }t        |      t        |      k(  sJ | j                  t        t        t        ||            t                            y r3  )r  r2  r`  r@  rw   r  r  r   r0  rg   r   r  r\   )r   r   r  ru  s       r   BUILD_CONST_KEY_MAPz-InstructionTranslatorBase.BUILD_CONST_KEY_MAP  s    xxz4;;'$...&&(((--d34yCK'''		Sv&'.0	
r   c                
   | j                  d      \  }}|j                  dkD  sJ |j                  J | j                  |j                      j	                         }t        |t              sJ |j                  | d||fi        y )Nr  r   r'  )r2  r`  r  rE  rp  r@  rg   r)  )r   r   r   r   r  s        r   MAP_ADDz!InstructionTranslatorBase.MAP_ADD  sw    yy|1{{Qxx###jj$((#++-#0111maVR8r   c                   | j                         }|j                  dkD  sJ |j                  J | j                  |j                      }t	        |t
              sJ |j                         sJ |j                  | d|gi        y )Nr   r   r  r`  r  rE  r@  rh   
is_mutabler)  r   r   r   r  s       r   SET_ADDz!InstructionTranslatorBase.SET_ADD  su    HHJ{{Qxx###jj$((##{+++~~eaS"-r   c                   | j                         }|j                  dkD  sJ |j                  J | j                  |j                      }t	        |t
              sJ |j                         sJ |j                  | d|gi        y Nr   rE  rL  rN  s       r   
SET_UPDATEz$InstructionTranslatorBase.SET_UPDATE  su    HHJ{{Qxx###jj$((##{+++~~hR0r   c                z   | j                         }|j                  dkD  sJ |j                  J | j                  |j                      j	                         }t        |t              sJ |j                         sJ | j                  j                  j                  |       |j                  j                  |       y r   )r  r`  r  rE  rp  r@  ru   rM  r  r  mutationr   r   rN  s       r   LIST_APPENDz%InstructionTranslatorBase.LIST_APPEND  s    HHJ{{Qxx###jj$((#++-#|,,,~~  ))#.		r   c                b   |j                   }t        j                  dk  r| j                         }| j                         }t        j                  dk\  rBt	        |j
                  d      sJ t        j                  |j
                  j                        }d }d }d }d }t        j                  dk  rV|T|dz  r| j                         }|dz  r| j                         }|dz  r| j                         }|dz  r| j                         }| j                  t        || j                  ||||             y )	Nr  co_qualnamer  ru     r  r  r   )r  rD  r  r  r   r  rb   ra  rW  r0  rl   r	  )	r   r   flagsfn_namer"  defaultsclosurer   
kwdefaultss	            r   MAKE_FUNCTIONz'InstructionTranslatorBase.MAKE_FUNCTION  s
   g%hhjGxxzw& 4::}555&--DJJ4J4JKG
g% 4<"hhjG4<"&((*K4<!%J4<#xxzH		&
	
r   c           	     0   | j                         }|j                         r'|j                  | t        |j                              }nt        |t              rx|j                  j                         r^t        |j                  j                         |j                        }t        |j                        D cg c]  }t        | ||          }}nN|j                  |       r|j                  |       }n+t        dt        |      | dg t         j"                         t%              |j                  k7  r;t        dd|j                   dt%        |       | dg t         j&                         t)        |      D ]  }| j+                  |        y c c}w )	N)r  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`).rn  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`  r@  rz   r  r  as_proxyr   r`   r  r  r:   r   r   r  r   r  r   r0  )r   r   r=  r!  proxyrQ  s         r   r  z)InstructionTranslatorBase.UNPACK_SEQUENCE/  sX   hhj==?))$eDKK6H)IC_-#''2C2C2ECGG,,.9E:?:LMQ=uQx0MCM..t4//5CEC"e $( (5)445 s8t{{"S+DKK=
3s8*M"e $? ?5)445 # 	AIIaL	- Ns   7Fc                n   d|j                   cxk  rdk  sJ  J |j                   dz  }|j                   dz	  }| j                         }|j                  |       rt        |j	                  |             }t        |      ||z   k\  sJ |d | }||t        |      |z
   }|t        |      |z
  d  }t        |      D ]  }	| j                  |	        | j                  t        |             t        |      D ]  }	| j                  |	        y t        dt        |      | dg t        j                         y )Nr   i     rX  z%Failed to unpack object for UNPACK_EXz; cannot be unpacked into a list for the UNPACK_EX bytecode.rn  )r`  r  r  r   r  r   r   r0  rw   r:   r   r   r  )
r   r   prefixsuffixr=  r  vals_prefix	vals_listvals_suffixitems
             r   	UNPACK_EXz#InstructionTranslatorBase.UNPACK_EXO  s2   DKK)6)))))t#!hhj,,T255d;<Dt9///w-KVc$i&&89Is4y6134K -  		$ IImI./ -  		$  ?C"e#^_5)445	r   z-Encountered intentional debugging graph breakc                >    | j                   rt        ddddg       y y )Nz#Forced graph break on leaf functionr   z:Forced graph break for nested graph break testing purposeszGSet torch._dynamo.config.debug_force_graph_break_on_leaf_return = Falsern  )rR  r:   r  s     r   graph_break_on_leaf_functionz6InstructionTranslatorBase.graph_break_on_leaf_functiong  s+     =X]	 r   c                F    |j                   dk(  r| j                  |       y y )NGRAPH_BREAK_IF_LEAF)r`  rl  r  s     r   r  zInstructionTranslatorBase.NOPu  s"    ;;//--d3 0r   c                $    | j                          y r   r  r  s     r   r  z!InstructionTranslatorBase.POP_TOPz      
r   c                    | j                         }| j                         }| j                  |       | j                  |       y r   r  r0  r   r   ar  s       r   ROT_TWOz!InstructionTranslatorBase.ROT_TWO}  s.    HHJHHJ		!		!r   c                    | j                         }| j                         }| j                         }| j                  |       | j                  |       | j                  |       y r   rs  )r   r   ru  r  cs        r   	ROT_THREEz#InstructionTranslatorBase.ROT_THREE  sD    HHJHHJHHJ		!		!		!r   c                   | j                         }| j                         }| j                         }| j                         }| j                  |       | j                  |       | j                  |       | j                  |       y r   rs  )r   r   ru  r  rx  rB  s         r   ROT_FOURz"InstructionTranslatorBase.ROT_FOUR  sZ    HHJHHJHHJHHJ		!		!		!		!r   c                h    | j                         }| j                  |       | j                  |       y r   rs  )r   r   ru  s      r   rK  z!InstructionTranslatorBase.DUP_TOP  s#    HHJ		!		!r   c                    | j                         }| j                         }| j                  |       | j                  |       | j                  |       | j                  |       y r   rs  rt  s       r   DUP_TOP_TWOz%InstructionTranslatorBase.DUP_TOP_TWO  sD    HHJHHJ		!		!		!		!r   c                    |dk(  r!t        t              j                  | |gi       S |dk(  r!t        t              j                  | |gi       S |dk(  r!t        t              j                  | |gi       S |S )Nr   r  rv  )ra   r   r1  r  ascii)r   r  flags      r   _convert_valuez(InstructionTranslatorBase._convert_value  sp    19"3'55dUGRHHQY"4(66teWbIIQY"5)77ugrJJr   c                   | j                         }t        |t              r?ddlm}m}  |j                   |||      |j                        }| j                  |       y | j                  ||dz        }t        j                  d|j                         z   dz         }| j                  t        t        j                        ||gi        y )Nr   )LazySymNodeFormatStringrq   rA  rv  z{:})r  r@  r   torch._dynamo.variables.lazyr  rq   ra  r  r0  r  rb   r  r1  ra   r   format)r   fmt_specrY  r  r  rq   fmt_vars          r   _format_valuez'InstructionTranslatorBase._format_value  s    
e_-
 /'..'x8E IIe##E54<8"))$1L1L1N*NQT*TU?3::6%8H"Mr   c                    |j                   }|J |dz  dk(  r| j                         }nt        j                  d      }| j	                  ||      S )Nr  r   )r  r  rb   ra  r  )r   r   rY  r  s       r   FORMAT_VALUEz&InstructionTranslatorBase.FORMAT_VALUE  sQ       DLT!xxzH'..r2H!!(E22r   c           
     \   g }g }i }|j                   J | j                  |j                         D ]:  }|j                         r#|j                  d       |j                  |       7t	        |t
        j                        r|j                  |j                         |j                  |j                         t        |j                               t        |j                  j                               z  r2t        dd| d| d|j                   dg t        j                         |j!                  |j                         t        dt#        |      d	g t        j                         = | j%                  t
        j                  j'                  d
j)                  |      ||             y )Nz{}zBUILD_STRING key conflictzformat_string_parts: z
, kwargs: z, part.sym_kwargs: z1Failed to build format string due to key conflictrn  zBUILD_STRING type errorzLFormat string part type is not correct - expected constant or format string.r   )r  r2  r  r   r@  r   StringFormatVariableformat_stringr  sym_argsr  r  
sym_kwargsr:   r   r  rE  r   r0  ra  r   )r   r   format_string_partsr   rx  parts         r   BUILD_STRINGz&InstructionTranslatorBase.BUILD_STRING  su   )+&(-/xx###IIdhh' 	D&&(#**40D!D)"@"@A#**4+=+=>DMM*v{{}%DOO,@,@,B(CC! ;"78K7LJW]V^^qrv  sB  sB  rC  !D$W= 1 < <=	 doo.5I n9-8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|      }| j                  |       y )Nr   r   iszis notr  r  )r`  r&   r  )r   r   
new_argvalnew_insts       r   IS_OPzInstructionTranslatorBase.IS_OP  sM    {{a4;;!#333;;!J!J%l:F!r   c           	         |j                   dk(  s|j                   dk(  sJ | j                  d      \  }}|j                   }	 | j                  |j                  | d|gi              |dk(  r| j                  |       y y # t        j
                  t        f$ rf}t        |t              r|j                          | j                  | j                  t        j                  | t              ||gi              Y d }~d }~ww xY w)Nr   r   r  __contains__)r`  r2  r0  r)  r   ObservedTypeErrorr;   r@  r  r  r]   r  rB   	UNARY_NOT)r   r   leftrightr  r  s         r   CONTAINS_OPz%InstructionTranslatorBase.CONTAINS_OP  s    {{a4;;!#333iile[[	IIe''ntfbIJ$ 7NN4   !! 
 	 $,&&(II00#))$0IJ5M 	s   $A> >C=AC88C=c                   | j                         }|j                  dkD  sJ |j                  J | j                  |j                      }t	        |t
              sJ |j                         sJ |j                  | d|gi        y )Nr   r  )r  r`  r  rE  r@  ru   rM  r)  rN  s       r   r  z%InstructionTranslatorBase.LIST_EXTEND  su    HHJ{{Qxx###jj$((##|,,,~~hR0r   c                    | j                  t        t              j                  | | j	                         gi              y r   )r0  ra   r!  r1  r  r  s     r   LIST_TO_TUPLEz'InstructionTranslatorBase.LIST_TO_TUPLE  s*    		/%(66tdhhj\2NOr   c                d   | j                   d   }| j                  |      sJ |j                  t        u r|t	        j
                  t              j                  | t        d      gi       }|j                  | t        d      |       |j                  | t        d      |       || j                   d<   y y )Nr  zgenerator raised StopIterationr)  r  )
rE  r{  rz  rr  r   ra   r  r1  rb   r  )r   r   r!  new_vals       r   STOPITERATION_ERRORz-InstructionTranslatorBase.STOPITERATION_ERROR  s     jjn))#...<<=(//=KK!"BCDG
   '7'FL  '7'DcJ$DJJrN )r   c                $   | j                         }|j                  dkD  sJ |j                  J | j                  |j                      j	                         }t        |t              sJ |j                         sJ |j                  | d|gi        y rQ  )	r  r`  r  rE  rp  r@  rg   rM  r)  rN  s       r   
DICT_MERGEz$InstructionTranslatorBase.DICT_MERGE+  s    HHJ{{Qxx###jj$((#++-#0111~~hR0r   c                $    | j                          y r   rp  r  s     r   	GEN_STARTz#InstructionTranslatorBase.GEN_START6  rq  r   c                    | j                   d   }|j                         r<| j                  t        j                  t        |j                                            y | j                  |j                  | dg i              y )Nr  r  )rE  r  r0  rb   ra  r   r  r)  r  s      r   GET_LENz!InstructionTranslatorBase.GET_LEN9  s\    jjn!!#II&--c#2H2H2J.KLMIIcoodIr2>?r   c                4   | j                   d   }t        |t              sJ t        |j                  t        j
                  j                        r%| j                  t        j                  d             y | j                  t        j                  d             y Nr  TF)
rE  r@  rg   r   collectionsabcMappingr0  rb   ra  r  s      r   MATCH_MAPPINGz'InstructionTranslatorBase.MATCH_MAPPING@  sh    jjn#0111cii!8!89II&--d34II&--e45r   c                v   | j                   d   }|j                         sJ |j                         }t        |t        j
                  j                        r@t        |t        t        t        f      s%| j                  t        j                  d             y | j                  t        j                  d             y r  )rE  r  r  r@  r  r  r   r   bytes	bytearrayr0  rb   ra  )r   r   r  	tos_values       r   MATCH_SEQUENCEz(InstructionTranslatorBase.MATCH_SEQUENCEH  s    jjn%%'''**,	i!9!9::UI.D
 II&--d34II&--e45r   c                x   | j                   d   }t        |t              sJ |j                  |       }| j                   d   t        t              sJ t        fd|D              rq| j                  t        |D cg c]  }j                  | |       c}             t        j                  dk  r%| j                  t        j                  d             y y | j                  t        j                  d              t        j                  dk  r%| j                  t        j                  d             y y c c}w )Nr  r  c              3  &   K   | ]  }|v  
 y wr   r   )r   r   tos1s     r   r   z7InstructionTranslatorBase.MATCH_KEYS.<locals>.<genexpr>Z  s     'QqDy's   r  TF)rE  r@  rw   r  rg   rr  r0  getitem_constrD  r  rb   ra  )r   r   r  r  r   r  s        @r   
MATCH_KEYSz$InstructionTranslatorBase.MATCH_KEYSS  s    jjn#}---&&t,zz"~$ 1222'$''IIm$$OQT%7%7a%@$OPQ')		*11$78 * II&--d34')		*11%89 * %Ps   =D7c                D    | j                  | j                  d             y )Nrw  r0  rR  r  s     r   LOAD_ASSERTION_ERRORz.InstructionTranslatorBase.LOAD_ASSERTION_ERRORc  s    		$//0@ABr   c                D    | j                  | j                  d             y )N__build_class__r  r  s     r   LOAD_BUILD_CLASSz*InstructionTranslatorBase.LOAD_BUILD_CLASSf  s    		$//0ABCr   z`Encountered graph break when attempting to trace BINARY_SUBSCR: a binary subscript, e.g. x[attr]c                p    |j                   dk(  r| j                  |       d| _        y | j                  rJ y )Nr   F)r  append_prefix_instrH  r  s     r   RESUMEz InstructionTranslatorBase.RESUME  s5    88q=##D)&+D#.....r   c                P    |j                   J t        |j                      | |      S r   )r  _binary_op_lookupr  s     r   	BINARY_OPz#InstructionTranslatorBase.BINARY_OP  s(    88'''$TXX.tT::r   c                     y r   r   r  s     r   r  z!InstructionTranslatorBase.PRECALL      r   c                    | j                   d   |j                     }t        |t              sJ |D ]  }t        |t              rJ  | j
                  J t        j                  |      | _        y )N	co_constsr  )r{  r  r@  r!  r   r  rb   ra  )r   r   r  r   s       r   r  z"InstructionTranslatorBase.KW_NAMES  sl    $$[1$((;(E*** 	)DdC(((	)}}$$$(//h?r   c                6    | j                  t                      y r   )r0  r{   r  s     r   r  z#InstructionTranslatorBase.PUSH_NULL  s    		,.!r   c                   |rXt         j                  dk\  sJ | j                         }t        |t              r|j                         sJ |j                         }n$| j                  r| j                  j                  nd}|j                  J | j                  |j                  dz         }t         j                  dk\  r!|d   }t        |d   t              rg n|d   g}n&t        |d   t              r|d   }g }n|d   }|d   g}|rO||dt        |        z   }|t        |       d  }t        t        ||            }t        |      t        |      k(  sJ ||dd  z   }i }	 | j                  |||       d | _        y # d | _        w xY w)Nru  r   r  r   r   )rD  r  r  r@  rw   r  r  r  r  r  r2  r{   r   r   r  r1  )	r   r   call_kwr  contentsr<  r   r  rx  s	            r   _callzInstructionTranslatorBase._call  s~    ##w...xxzHh68;V;V;XXX224H.2mmt}}**Hxx###99TXX\*w&!B#HQK>2Xa[MD(1+|4a[a[ }(1H~66D"CM>#34K#h45Fv;#h-///(12,&DF	! r40 DMDDMs   !E< <	FzTEncountered graph break when attempting to trace CALL: a function call, e.g. f(x, y)c                &    | j                  |       y r   r  r  s     r   r  zInstructionTranslatorBase.CALL  s    
 	

4r   c                r    |j                   J | j                  | j                  |j                              y r   )r  r0  rE  r  s     r   COPYzInstructionTranslatorBase.COPY  s-    xx###		$**dhhY'(r   c                    |j                   J | j                  |j                       | j                  d   c| j                  d<   | j                  |j                    <   y Nr  )r  rE  r  s     r   SWAPzInstructionTranslatorBase.SWAP  sM    xx###04

DHH90EtzzRT~-

2

DHH9-r   c                     y r   r   r  s     r   CACHEzInstructionTranslatorBase.CACHE  r  r   c                &    | j                  |       y r   ra  r  s     r   BEFORE_WITHz%InstructionTranslatorBase.BEFORE_WITH  rd  r   c           	     
   t        |t              r+|j                         s| j                  j	                  |       t
        j                  dk\  r|j                  r>| j                  r/|j                  j                  | j                  d   j                  urd }nE| j                  j                  J | j                  j                  j                  }n|j                  }|rt        | t              st        j                  r;| j                  j	                  t        ||t        | j                         |             n9| j                  j	                  t        ||t        | j                                      |j#                  |       S )Nr  r  )r@  rd   supports_graph_breaksr  r   rD  r  r  r  r  r  r6  r   r  r  r   rE  enter)r   r-  r   r  s       r   	enter_ctxz#InstructionTranslatorBase.enter_ctx  s(    s:;--/0077<w&
 !!$$%%,,D4D4DR4H4O4OO,,::FFF..<<CC[[F$ 56&:T:T  ''#D&#djj/3G   ''fc$**o(VWyyr   c                P    t        dd|  d| j                          dg d       y )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 generalrn  )r:   r  )r-  s    r   unsupported_ctx_graph_breakz5InstructionTranslatorBase.unsupported_ctx_graph_break  s3    1GuM?@T@T@V?WWij		
r   c                .   | j                         }t        |t        t        f      s| j	                  |       t        |t        t        f      sJ | j                  t        ||j                               | j                  | j                  ||             y r   )	r  r@  rc   rd   r  r0  rf   r  r  )r   r   r-  s      r   rb  z.InstructionTranslatorBase.setup_or_before_with-  s    hhj)+IJ
 ,,S1 )+IJ
 	
 
 			*3<=		$..d+,r   c                V    | j                   sJ | j                  j                  |       y r   )rH  rJ  r   r  s     r   r  z,InstructionTranslatorBase.append_prefix_inst<  s%    &&&&  &r   c                .   t         j                  dk\  rq| j                  set        | j                  |j
                     t              sJ | j                  j                  j                         | j                  |j
                  <   y | j                  |       y ro  )rD  r  rH  r@  r?  r`  r{   r  r  track_cell_newr  r  s     r   	MAKE_CELLz#InstructionTranslatorBase.MAKE_CELL@  ss    w&t/F/F d224;;?NNN((779   - ##D)r   c                &    | j                  |       y r   r  r  s     r   COPY_FREE_VARSz(InstructionTranslatorBase.COPY_FREE_VARSK      %r   c                &    | j                  |       y r   r  r  s     r   RETURN_GENERATORz*InstructionTranslatorBase.RETURN_GENERATORN  r  r   c                n    t         j                  dk\  r| j                          y | j                  d       y )Nru  r  )rD  r  r  r2  r  s     r   END_FORz!InstructionTranslatorBase.END_FORU  s$    w&HHJIIaLr   c                    t        | j                  j                  |j                  d       t              r6t        d|j                  d|j                   g t        j                         | j                  |       y )Nz)LOAD_FAST_CHECK on uninitialized variablez/Attempted to load uninitialized local variable rn  )	rX   r?  r  r`  r{   r:   r   r  r  r  s     r   LOAD_FAST_CHECKz)InstructionTranslatorBase.LOAD_FAST_CHECK[  s`    $&&**4;;=|LCMdkk][5)445	 	tr   c                    |j                   | j                  vr| j                  t                      n| j	                  |       t               | j                  |j                   <   y r   )r`  r?  r0  r{   r  r  s     r   LOAD_FAST_AND_CLEARz-InstructionTranslatorBase.LOAD_FAST_AND_CLEARe  sD    ;;d222IIln%NN4 ,8NT[[)r   c                    | j                  t        j                  |d             |j                  J |j                  dz  r| j	                  |       y | j                  |j                         y )Nr  r  r   )r  r   r  r  r  rL  r`  r  s     r   LOAD_SUPER_ATTRz)InstructionTranslatorBase.LOAD_SUPER_ATTRl  sV    ;..tA>?xx###88a<T"OODKK(r   c                   |j                   dk(  r| j                  |       y |j                   dk(  r| j                  |       y |j                   dk(  r8| j                  t	        | j                         j                  |                    y t        dd|j                    d|j                    dg t        j                         y )	Nrv  r     z Missing CALL_INTRINSIC_1 handlerzCALL_INTRINSIC_1 operand: z,No handler implemented for CALL_INTRINSIC_1 z instruction.rn  )
r`  r  UNARY_POSITIVEr0  rw   r  r  r:   r   r  r  s     r   CALL_INTRINSIC_1z*InstructionTranslatorBase.CALL_INTRINSIC_1t  s    ;;!$$T*[[A%[[AIImDHHJ$H$H$NOP:4T[[MBJ4;;-Wde6)556	r   c                f    | j                         }| j                          | j                  |       y r   rs  r  s      r   END_SENDz"InstructionTranslatorBase.END_SEND  s!    hhj
		#r   zpEncountered graph break when attempting to trace CALL_KW: a function call with keyword arguments, e.g. f(x=True)c                *    | j                  |d       y )NT)r  r  r  s     r   CALL_KWz!InstructionTranslatorBase.CALL_KW  s     	

4
&r   c                8    | j                   j                  dv sJ y )N)POP_JUMP_IF_TRUEPOP_JUMP_IF_FALSEr  )r  r   r  s     r   rx  z!InstructionTranslatorBase.TO_BOOL  s'     $$++ 0
 
 	
 
r   c                   |j                   }|J | j                         }t        |t              sJ | j                         }|dz  rDt        j
                  dk\  sJ |j                  | t        j                  d      gi       }||_	        n3|dz  r||_
        n&|dz  r||_	        n|dz  r||_        n|dz  r||_        | j                  |       y )N   r  r   rX  r  r  )r  r  r@  rl   rD  r  r1  rb   ra  r   r\  r]  r[  r0  )r   r   rY  r<  r  s        r   SET_FUNCTION_ATTRIBUTEz0InstructionTranslatorBase.SET_FUNCTION_ATTRIBUTE  s       XXZ"8999xxz4<##w... %%d-=-D-DQ-G,H"MD!BNT\BJT\!BNT\ BMT\BK		"r   c                v    | j                  | j                  | j                         |j                               y r   )r0  r  r  r`  r  s     r   CONVERT_VALUEz'InstructionTranslatorBase.CONVERT_VALUE  s%    		$%%dhhj$++>?r   c                N    | j                  t        j                  d      d       y )Nr   r   )r  rb   ra  r  s     r   FORMAT_SIMPLEz'InstructionTranslatorBase.FORMAT_SIMPLE  s    +2226:r   c                D    | j                  | j                         d       y 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| j	                         }t        |t
        t        f      s| j                  |       t        |t
        t        f      sJ |dk(  r,| j                  t        |             | j                  |       y | j                  t        |d              | j                  |       y | j                  |       | j	                         }| j                  |       | j                  |       y )Nz*expected LOAD_SPECIAL arg to be set to int)r
  r  r
  )r@  r  r   _load_special_namesr  rc   rd   r  r0  re   r  rf   rL  )r   r   r  r-  r  s        r   LOAD_SPECIALz&InstructionTranslatorBase.LOAD_SPECIAL  s   $((C(V*VV(''1,,((*C-/MN 005 -/MN   {"		3C89t$ 		23=>t$ OOD!((*CIIcNNN4 r   c                `    | j                  t        j                  |j                               y r   )r0  rb   ra  r`  r  s     r   LOAD_SMALL_INTz(InstructionTranslatorBase.LOAD_SMALL_INT  s    		"))$++67r   )rw  r;  r!  rr  anyc                    t        |j                  t              sJ d       | j                  | j	                  | j
                  |j                                  y )Nz2expected LOAD_COMMON_CONSTANT arg to be set to int)r@  r  r   r0  rR  _common_constantsr  s     r   LOAD_COMMON_CONSTANTz.InstructionTranslatorBase.LOAD_COMMON_CONSTANT  sH    $((C( 	
@	
( 			$//0F0Ftxx0PQRr   c                P    | j                   j                         dkD  r	d | _        yy)Nr   c                      yNTr   r   r   r   r   z>InstructionTranslatorBase.is_non_empty_graph.<locals>.<lambda>  r  r   TF)r  count_callsr  r   s    r   r  z,InstructionTranslatorBase.is_non_empty_graph  s%    ;;""$q(&2D#r   c           
         |g }dj                  t        j                  | j                         gt	        t        |            z               S )Nr   )r   r~  r  r  r   r   )r   additional_stack_framess     r   format_frame_summaryz.InstructionTranslatorBase.format_frame_summary	  sN     #*&(#ww!!##%&h7N.O)PP
 	
r   c           	         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  s?    %%DKK<KKDKKK8	
 	
r   c                    t        | j                  dd      }t        j                  d      }|j	                  |      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ZZ(?@!''1==r   c                    | j                   j                  |t        j                  |            }t	        t        |      j                  t        j                               |S r   )	r  install_global_by_idr  r  r>   rK   r  r=   WEAKREF_ALIVE)r   rd  r  global_names       r   store_global_weakref_by_idz4InstructionTranslatorBase.store_global_weakref_by_id!  sJ    kk66vw{{5?QR,778R8RS	
 r   c                B    | j                   j                  j                  S r   )r  tracing_context	fake_moder   s    r   r/  z#InstructionTranslatorBase.fake_mode(  s    {{**444r   c              #  b   K   | j                   }|| _         	 d || _         y# || _         w xY ww)zx
        Strict mode is enabled on a per-VariableTracker level depending on the return value of check_fn(node).
        N)rP  )r   check_fnpriors      r   strict_translation_modez1InstructionTranslatorBase.strict_translation_mode,  s3      %% (	*$)D!ED!s   /# /	,/c                   | j                   J | j                   dkD  sJ | j                  j                  | j                  j                  | j
                  | j                   dz
  | j                  | j                   dz
           S r(  )r   r  r   r}  r  r   r_  r   s    r   r  z#InstructionTranslatorBase.speculate:  s|    ''333''!+++##((KK##KK$$q(d66:;	
 	
r   c                     ||dk  r||fS ||fS r   r   )r   r   r   fallback_linenos       r   _make_frame_locz)InstructionTranslatorBase._make_frame_locD  s%     >VaZo..&!!r   c                d   g }t         j                  rs| j                  }|e| j                  |j                  j
                  |j                  |j                  j                        }|j                  |       |j                  }|e|j                          |j                  |       t        |      S r   )r   r  rS  r7  r}  r  r   co_firstlinenor   reverser!  )r   	frame_locframe_loc_chain_list
current_txparent_frame_locs        r   _get_frame_loc_chainz.InstructionTranslatorBase._get_frame_loc_chainK  s     79%%>BkkJ(#'#7#7%%11%%%%44$ 
 %++,<='..
 ( 	$$&##I.)**r   c                  	 |(t         j                  j                  j                         }	 t        j
                  rR| j                  F| j                  | j                  j                  | j                  | j                  j                        }n-| j                  |d   j                  |d   j                  d      }| j                  |      }d}t        j                  r/t!               }dj#                  t%        j&                  |            }nt!               |z   }t)        |      }dj#                  t%        j&                  |            }d|d    d|d    d	| d
	t        j                  r	| d| dz  	n	t+        |      z  	t         j,                  j/                  dd 	fd       t0        j3                  t4        j6                        rgt8        sat;        j<                  |      rLt        j                  r&	dz  		dj#                  | j>                        dz   z  	t0        jA                  	       y t0        jA                  d|d   |d   |       y # t        $ r |d   |d   f}Y w xY w)Nr  r   r  r9  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>  s    3$! r   c                 6      dt        j                          S )Nr   )r~  
format_exc)user_stack_traces   r   r   z;InstructionTranslatorBase.log_graph_break.<locals>.<lambda>  s    "2!32i6J6J6L5M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: %s)!r  _guardsr   extract_stackr   r  rS  r7  r}  r  r   r9  r   
IndexErrorr?  r  r7   r   r~  r  r3   r   _loggingtrace_structuredgraph_break_logisEnabledForr   DEBUGexplainrW   r   rW  r  )
r   r{  r   r  r;  frame_loc_chainstack_above_dynamo_formattedstack_above_dynamouser_stack_formattedrH  s
            @r   rz  z)InstructionTranslatorBase.log_graph_break_  sm    55CCEJ	))dkk.E 00KK++KKKK..	 !00rN++rN))	 33I>')$>>!7!9+-77%%&89,( 01J>J/
;J!wwy'<'<Z'HI*9Q<.)A, H##)( +%% 	 >>/0 1e'( )LL $8 99'' O 	( 	
 ((7/33OD
 ~~ L  !DIId.H.H$ID$PP !!  !! A!!	K  	 ]+-.I	s   BI I,+I,c                   t         |           || _        || _        || _        || _        || _        |	| _        |
| _        d | _	        g | _
        d| _        d | _        t        d      | _        d| _        g | _        g | _        d| _        d | _        d| _        g | _        || _        t/        d      | _        || _        t5        | j2                        | _        || _        || _        || _        || _        || _         || _!        |$tD        jF                  rtI        |||      | _%        nd | _%        i | _&        i | _'        || _(        d| _)        d| _*        d| _+        d | _,        d | _-        d| _.        d | _/        g | _0        || _1        d	d
l2m3}m4}m5}m6} |jn                  ||z  |z  |z  z  r| jq                  ts        d              || _:        d| _;        d gty        |jz                        z  | _>        t~        j                  t        j                        | _C        t        j                  t        j                        | _E        t        j                  |j                  |       y )Nr   r  Tr     )maxlen)r"  r\  r{  Fr   )CO_ASYNC_GENERATORCO_COROUTINECO_GENERATORCO_ITERABLE_COROUTINE)Ir
  r  r  distributed_stater  r?  r@  rA  rB  rC  rE  r   rQ  r&   rG  r  r  r  r   r  rH  rJ  rN  r   rW  r_  r   rW  r  r	  r  r{  r}  r\  r   replay_record_enabledrD   rO  nn_module_stack	num_callsexportr  r   r  rM  rP  rR  rS  rT  rV  resume_executionr[  r\  r]  r^  co_flagsr0  ra   rK  rL  r   r  r  r  rQ  r   rR  r  r  r  r  	lazycacher  )r   r  r_  r  r	  r  r{  r?  r@  rA  rB  r}  rc  rK  r  rN  r_  r\  rV  r[  r\  r]  r^  r  s                          r   r  z"InstructionTranslatorBase.__init__  s[   , 	.!2 . 0-J*%:" -1),.
#$ #5e#< (,%UW,"&(%*"%5" 0</:4;L;L/M 	 *3*4,8&, 6#?#?!2W<"D "&D BD)+ $)!*/'#'  $"	
 	
 ??<'*??BTT
 IIod+,().& FS))** 	
 ++GMM: 	* ))'--8 	( 	F..	:r   r   r  )r   	list[str])r<  r]   r   rD  rx  r>  r   r   )r<  r]   r   zSequence[Any]rx  r   r   r   r   )r   rF  r   r   r   )r   r   r   r   r   r+   r   r   )r   r+   )r  r+   r   r   r   r   )r!  Optional[VariableTracker]r   r   )r  rD  r   r   r   r]   )r  r   r   rD  )r   r+   r   r]   )r  r   r   rJ   )r   r   rV  r   r1  r   r   r   )r`  r   r   r]   )r!  r]   r   r]   )r!  r]   r   r   )r!  r]   r   zTypeIs[ExceptionVals])r  r   r   r   )r  r   r   r   )r   r+   r  r   r   rI  )r  r   r  r+   r  rA   r  list[types.CodeType]r  r0   r	  r   r
  r   r   ztuple[types.CodeType, str]r   r+   r  zlist[StackLocalsMetadata]r   rI  )r  rl  r  rg  r  r0   r   r   )r   r+   r   r  r   r   )r  r]   r  r   r   r]   )r  r]   rY  r   r   r   )F)r   r+   r  r   r   r   )r-  z>Union[ContextWrappingVariable, GenericContextWrappingVariable]r   r+   r   r]   )r-  r]   r   r   )r  zOptional[list[Any]]r   r   )r   ztraceback.FrameSummary)rd  r   r  r   r   r   r   zOptional[FakeTensorMode])r1  z!Callable[[VariableTracker], bool]r   r   )r   r   )r   r   r   rF  r6  r   r   tuple[str, int])r;  ro  r   ztuple[tuple[str, int], ...])r   N)r{  r   r   r   r  zOptional[StackSummary]r   r   )NN)&r  r@   r_  rI  r  r   r	  r   r  r   r{  r   r?  r>  r@  r>  rA  r   rB  r   r}  types.CodeTyperc  r   rK  r   r  r   rN  r  r_  Optional[DistributedState]r\  zOptional[tuple[types.CellType]]rV  rU  r   r   (  r   r   r   r   rY  r|  rd  rh  rk  rn  r1  r{  r  r  r  r  r  rD  r  r  propertyr  r  r  r  r0  r  r  r2  r  r  r  r  r  LOAD_CLOSUREr  r\  r  r:  cached_propertyr  r  r   r   r  r4  r=  rI  EAGER_IMPORT_NAMErM  rR  r  r  JUMP_FORWARDJUMP_ABSOLUTEr  r]  not_r   r^  r  JUMP_IF_FALSE_OR_POPJUMP_IF_TRUE_OR_POPrZ  r]  r_  rc  rf  rh  rj  rm  ru  rw  r~  rR  r  r  r{  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rL  r  r  r  staticmethodr  r  r  r  ry  r  r  r/  r1  r  r9  ru   r>  r@  BUILD_TUPLE_UNPACK_WITH_CALLrC  rF  BUILD_MAP_UNPACK_WITH_CALLrH  rJ  rO  rR  rU  r^  r  rj  rl  r  r  rv  ry  r{  rK  r~  r  r  r  r  r  r  r  r  r  r  DICT_UPDATEr  r  r  r  r  r  r  r=  posr  negUNARY_NEGATIVEr  invertUNARY_INVERTpowBINARY_POWERmulBINARY_MULTIPLYmatmulBINARY_MATRIX_MULTIPLYfloordivBINARY_FLOOR_DIVIDEtruedivBINARY_TRUE_DIVIDErF  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  r  r  r  r  r  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_FALSEr  r  r  r  rb  r  r  r  r  r  r  r  r  r  r  r  rx  r  r  r  r	  LOAD_FAST_BORROWr  POP_ITERr  r  r  r  r  r  r  r  r'  r,  r/  
contextlibcontextmanagerr3  r  r7  r?  rz  r  r  r  s   @r   r   r   `  sX    //00#==.."FF  &&$$""&&K((####33  ..AA//EE%%%%.!F00'

88 $8 +	8
 
8,4!4)64@N4	4
U!
U)6
U@N
U	
Un	WkZ 7"*	+\	 ; ;wr22=h ;!+FV2./ L	*J* J J!E( # #,
2:;| $% 6 6,>4 LM$X]]E:#HNNE:'t<&x~~t<U
U
(UU*
>R80.
)By'v<:$*QfKPD  )	)
  >;	>;@  A
	-
	-G  )%   A
	

 &)	 >A%A% !A% "	A%
 +A% A% A% "A% 
$A%Fg%g% $=g% 
	g%R q?*q?:Cq?IRq?	q? q?f
$  ~=	=8(1I
 @L @ 8 $6 J

 "2
 9.1%
N@0  N		4

N(3
B"!41P%$1 K@6	6: CD hll+Nhll+N'IHOO,LHLL)Lx||,O%hoo6"8#4#45!("2"23X\\*M-(,,'Jx||,O.u x "M X__-MX__-M(--(J&I(,,'JX]]+M.&x'7'78#H$6$67"8#4#45hmm,N /8==)K.h../Nh../N8==)K8==)K(,,'J/ 7"	;@",!\  i	)V M!%+HNNEB ,X^^U C ,X]]E B!-hmmU!C(#K# # 
	#J 
 
 -'	*&&;)$  A
'
'
2@;* !IH
!88S >B	
':	
		

>
 5 5 *9*	* *
""%2"EH"	"+(+	$+. -1	`$` ` +	`
 
`j 48,0)y;y; (y; !	y;
 "y; #y; %y; 4y; 5y; (By;  3y; y; y; y; (y;  %!y;" 6#y;& 1'y;( *)y;* 
+y; y;r   )	metaclassc                       e Zd Zedd       Zej                  dd       Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fdZddZ	ddZ
ddZddZddZdd	Zdd
Z xZS )r6  c                 "    t         j                  S r   )tlsr=  r   r   r   r=  z InstructionTranslator.current_tx>  s    ~~r   c              #     K   t        t        dd       }| t        _        	 d  |t        _        y # |t        _        w xY ww)Nr=  )r  r  r=  )r   r2  s     r   set_current_txz$InstructionTranslator.set_current_txB  s1     \40	#"CNUCNs   A 0 A =A c                    t               t        j                  d|j                   d|d    d|d           t        |   t        ||	| ||||||||
|      ||||||i i d d ||d||||       | j                          t        | j                  j                        5  | j                         5  |
| _        || _        | j                  r| j                  sJ d	       i | _        t        | j!                               }t#        j$                  |      j'                  d
d       }|j)                         D ]h  \  }}||vsd }|r)t+        |j'                  |i       j)                               }t-        j.                  |t1        |d|            }|| j                  |<   j | j                  j2                  }| j5                         D ]x  }||v rL||   }t1        |dd      }t-        j.                  ||      }|j7                         }|j9                  ||       n|j7                         }||_        || j                  |<   z |J t=        | j?                         |      D ]g  \  }}tA        |      }t1        |d      }	 t-        j.                  |jB                  |      }|jK                  |||      }||_        || j                  |<   i tM        |      | _'        tQ               | _)        |r.tF        j,                  jU                  | j                        | _        d d d        d d d        y # tD        $ r tG        jH                         }Y w xY w# 1 sw Y   5xY w# 1 sw Y   y xY w)Nztorchdynamo start tracing r  r  r   r9  )local_scoper+  r}  torch_function_mode_stackr  rV  r   )r  r_  r  r	  r  r\  r{  r?  r@  rA  rB  r}  rc  rK  r  rN  r_  rV  z4Export without one graph - something has gone wrong.dynamismT)is_inputr  )r  is_derefed_cell_contents)r  )+r  r   INFOr  r
  r  r@   _throw_if_in_functorchr   r  r.  r  r  rc  r?  r  rk  r/   r   r  r   	frozensetrq   ra  rM   r  rd  r  r   r  r  rh  rL   cell_contentsr  r   DeletedVariabletrack_cell_existingr   rA  r   rB  realize_all)r   r_  r}  r  r	  r  r\  r  r{  compiler_fnr  rc  export_constraintsframe_stater  rN  r_  rV  rk  r  r   r  local_dynamismvarr  contents_sourcer  cell_varr  cell_sourcer  s                                 r   r  zInstructionTranslator.__init__K  s   ( 	LL((8,}:U9VVWXdeuXvWwx	
 	"$&*C# &!%*."&+%/A 	 !	
F 	##% T[[001 b	43F3F3H b	#,DN DK{{~~ J~ $&D *-d.D.D.F*G#//7;;JMH'~~/ 5e00%)N)28<<b3I3O3O3Q)R-44# %)%3C 25D((.5"  ;;33L #68#$TNE* '2td'O 5H4N4N5L  ,::<H ++HlC+::<H&*#-5$$T*G#6N &&&!$--/7; 6
d-d3"-dT"R?#6#=#=**O$L (;;| '+#-5$$T*6  2L)2D. *=)>D& (1'D'D'P'P((($Ab	 b	 b	\ " ?#,#<#<#>L?]b	 b	 b	 b	sQ   M0BM
>D=M
< L'A:M
M'M	M
M	M

M	MMc                   t         j                  j                  d      }t        j                  | j
                  j                  d| j
                  j                        }t         j                  j                  j                         }t         j                  j                  j                  j                  t         j                  j                  j                  j                  t         j                  j                  j                  j                  f}|\|j                         |v rI||urD|j                         j                  j!                         }d| d| d| d}t#        dd|g 	       y y y y )
Neagerr  zhIf you are reaching here, it means dynamo failed for one of the following reasons:
- Calling torch.func.zP(compiled_fn) function from eager mode is not supported. Ensure that torch.func.zp is also wrapped within a torch.compile function. For more information, see PyTorch issue #128711.
- torch.func.z2(fn) requires the function to be inlined by dynamoz%Unsupported functorch tracing attemptr   rn  )r  _dynamolookup_backendr7  getattr_staticr  r  _C
_functorchpeek_interpreter_stackTransformTypeVmapGradJvpr*  r   lowerr:   )r   r  r  ciforbidden_keysr   rJ  s          r   r  z,InstructionTranslator._throw_if_in_functorch  s0   ,,W5,,KK##]DKK4K4K
 XX  779HH--22HH--22HH--11
 >bffh.8[PU=U668==&&(D((,v .**. 0  !%v%WY  ?	 >V8>r   c                    t        |t              r| j                  |j                     S t        |t              r| j
                  |j                     S t        r   )r@  rM   r  r  rJ   r	  r+  r  )r   r  s     r   get_example_valuez'InstructionTranslator.get_example_value  sE    fk*==!2!233fl+>>&"4"455r   c                    | j                   j                         D ]G  }t        |t              st	        |j                         t        j                  j                        sG y yr   )	r?  ru  r@  r   r  r  r  nnModule)r   r   s     r   $symbolic_locals_contain_module_classz:InstructionTranslator.symbolic_locals_contain_module_class  sR    %%,,. 	A!56:$$&< 		
 r   c                    t        | j                        rU| j                  d   x}rCt        |t              r2t	        |j                  |       t                     | j                  d<   y y y y )Nr  r4  )r   rE  r@  rk   rt   r  r\   )r   r  s     r   "replace_tos_if_return_is_generatorz8InstructionTranslator.replace_tos_if_return_is_generator  s`    

O

2&&3 <=1--d3.0DJJrN > ' r   c                   | j                          | j                  J | j                  J t               j	                  d| j                  | j                  z
         t
        j                  s| j                  j                         dk(  ru| j                  si| j                         sY| j                  sM| j                  sA| j                  s5| j                  s)t        j                   t#        | j$                  d            d | _         t'               t(        j*                  d| j$                  j,                   d|j.                   d       t0        j3                  d       | j                  j5                  | t7        d| j9                         gd	
      |j.                  dk(  rdnd      }t;        |      dk(  sJ |d   j<                  rJ | j                  j?                  | jA                  ||d   jB                               tD        )NrU  r   zno content in function callztorchdynamo done tracing r   r  zreturn triggered compilereturn_valueF)r  r[  r   rs  )#r  r   rQ  rV   rV  r   allow_empty_graphsr  r  rL  r  rc  r  r   r  r   r  r6   r}  r  r   r  r  r   r  r  r  r?   r  r   r  r  r  r  r+  )r   r   r  s      r   _returnzInstructionTranslator._return'  s   //1''333+++''0043C3CC	

 ))'')Q.22==?KKNN--33--)$++7TU  $( LL'(;(;'<Bt{{m1M	
 			,-$(KK$@$@%!3!3!5 6E !KK>9qq %A %
! ,-222,Q/@@@@++))$0I!0L0V0VW	
 r   c                &    | j                  |       y r   r  r  s     r   r[  z"InstructionTranslator.RETURN_VALUES      Tr   c                &    | j                  |       y r   r  r  s     r   r\  z"InstructionTranslator.RETURN_CONSTV  r  r   )r   r6  ri  )$r_  rI  r}  rp  r  r   r	  r   r  r   r\  zOptional[tuple[Any, ...]]r  r   r{  r   r  r   r  r   rc  r   r  r   r  r   r  r   rN  r  r_  rq  rV  rU  r   r   r   )r  rO   r   r   r  rh  )r   r   r   r{  r=  r  r  r  r  r  r  r  r  r  r[  r\  r  r  s   @r   r6  r6  =  s     # #a'a a !	a
 "a #a +a $'a %a a a a  a a (a  %!a" 6#a$ *%a& 
'aF>	*Xr   r6  r  INPLACErv  BINARY_c                  "    e Zd ZU dZded<   ded<   edd       Zedd       Ze	 	 	 	 	 	 	 	 	 	 dd       Z	dd	Z
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd
Zedd       ZddZ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 xZS )r  z Trace and inline a called methodrj  symbolic_resultr   rS  c                J    | j                  ||||      }|j                         S r   )build_inline_tracerinline_call_)r   rS  funcr   rx  tracers         r   r  z)InliningInstructionTranslator.inline_callk  s'    ((tVD""$$r   c                x   | j                         rt        dt        |       dg        t        | t              rt        j                  | j                         dd      rat        j                  | j                         dd       }t        dt        | j                               d| j                          d	| d
dg       t        j                  | d      }|j                  rddlm} t        t        | dd       d      r.| j                  j                   |u rt        j"                  dd      S t        | d      r| j                  j$                  nd}d| dg}d| j'                         vr|d| ddgz  }t        dd| d| j)                          d| j'                          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.rn  _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_applyr<  _originz"allowlist in dynamo known functionr   zAvoid calling the function `r  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   r@  rn   r7  r  get_functionr   check_verboseskippedtorch._dynamo.variables.miscr
  r   r  r<  r  
SkipResultr   get_filenameget_namer   )r  rJ  r  r
  fn_qualnamerr  s         r   check_inlineablez.InliningInstructionTranslator.check_inlineablep  s   ==?6D	k d01g6L6L!77
 ((!!#%?C LD--/05d6G6G6I5J K::=aA ? **4F>>V
 dD19=GGOO'HH #--?  3:$2E$''..2K.{m2>E  1 1 33PQ\P] ^h h 7	  H$[M$--/9J K"//122B6==/S]^i]j k( ( r   c                	    t        |t        t        t        t        f      sJ |j                         d } j                  j                  }t        j                  s"|r |j                  j                  d       x}r|}|ht        |t              rt        dd| dg        t        j                  |      }|j                   du sJ t        j                  s|r||j                  <   	 |j#                   ||      }t5        j6                  |j9                               D ]8  }
t        |
t:              rt        dd| d|
 dg t<        j>                         : j@                  dv rL|rt        |d   tB        jD                        s-t        ddj@                   d| dj@                   dg        d}tF        jH                  jJ                  jL                  jO                  d      r&dtQ        jR                        jQ                          }tT        jV                  dk\  r@ jX                   jZ                  d fd}t\        j_                  dta        |             tb        j_                  d||jd                         |rt        |d   tf              r j                  ji                  |d   jj                        }t        |tF        jl                  jn                        r?tq        jr                  |      tu        jv                  |jx                  jz                        d<   |rt        |d   t|              rr|d   j~                  }t        |tF        jl                  jn                        r?tq        jr                  |      tu        jv                  |jx                  jz                        d<   t        |t              rJ t              r1t         | j                   j                   j                  |      }|S t         | j                   j                   j                  |      }|S # t$        $ r}t'        dj)                  t+        |      d|j-                          d|j/                          d	|j                         j0                   |D 	cg c]  }	|	j3                          nc c}	w c}	|
            d }~ww xY w)NzEAttempted to inline function marked as skipped (SkipFunctionVariable)z+Attempted to inline a SkipFunctionVariable zdAttempted to inline a function that was previously determined to be marked as intentionally skipped.rn  Fz;{reason}.
  func = {func}, args = {args}, kwargs = {kwargs}'z' r   )r   r  r   rx  z:Encountered unconverted argument when attempting to inlinezfunc: z, arg: zWAn argument to an inlined function was not successfully converted to a VariableTracker.)r'  __setattr__r   z2Unsupported __setitem__/__setattr__ inline attemptzcode name: z, args: zAttempted to inline z: where first argument (self) is not a user-defined object.r   bytecoder   r  c                     j                  j                  j                        } t              j	                         }dj
                   d|  d| S )N)r   zTRACE inlined call z from r   )r  	positionsr   rU   r  r  )headerr  r"  cur_instrS  parent_codes     r   get_trace_call_log_strzQInliningInstructionTranslator.build_inline_tracer.<locals>.get_trace_call_log_str  s[    77#--44 8  2+xHOOQ,T\\N&4&QQr   r  zINLINING %s%s, %sr  r   )Er@  rn   rl   rj   rk   r~  r  r.  r   dont_skip_tracingpreviously_inlined_functionsr  rm   r:   r  r  r  	bind_argsr  r1   r  r   r  r  r9  r  rs  rt  ru  r]   r   r  r  r   r   r  rN  	_internal	log_stateis_artifact_enabledrS  BytecoderD  r  rG  r}  trace_call_logr  rY   r  r   r~   r  r  r  r  r  r  r/   r   forward__code__r   r  r,   &InliningGeneratorInstructionTranslatorr@  rA  rB  )rS  r  r   rx  r  tracing_ctxprevious_result
sub_localsr  r  r   re  r  moduler  r"  r  r  s   `              @@@r   r  z1InliningInstructionTranslator.build_inline_tracer  sK    $*.,	
 	
 
  $}}mm33
 ''K"-"J"J"N"Nd#   )>$ 45cI$P~ 3CCDIF>>U***++AG88>	f=J !2!2!45 	Aa1X$TF'!5 y9-889		 <<99ZQ)L)LML%dll^8D6B24<<.@z{	  >>##--AA*M#,,t,00234Fw&11H --KR R   z2H'IJ		%tVV]]C JtAw(89]]00a1C1CDF&%(("6"67
 KK' (()@)@A& JtAw(EF!W]]F&%(("6"67
 KK' (()@)@A& d$8999;''44,,F&  3''44,,F y  
	#NUUq6T]]_-R0A0A0C/DAdmmoFdFdEef7;<#//+<<! V  
	s%   ,P- -	S6AR<R*)R<<Sc                `   | j                   }| j                  }t        j                         }|j                  r| j                  |j                        }	 |5  | j                          d d d        | j                  |_        | j                  j                   rt#        j$                  d       S | j&                  J | j(                  |j(                  u r%|j*                  j-                  | j*                         |xj.                  | j.                  z  c_        t        j                  d|       | j                  j0                  j2                  j5                  |       t6        j8                  st;        | t<              r| j>                  rtA        |      rtt;        | t<              rd| jB                  rXt;        | t<              sJ g }| j&                  jE                         s| j&                  g}t        jF                  tH        | |       y | j&                  S tA        |      rMt;        | t<              sJ | j&                  jE                         sJ tK        | jL                  tO                     S | j&                  S # 1 sw Y   xY w# t        j                  $ r#}d| d| }t        j                  |        d }~wt        j                  $ r.}d| d| }t        j                  |       t        |      |d }~wt        $ r t        j                  d|        w xY w# | j                  |_        w xY w)	Nz!Observed exception DURING INLING z : zSKIPPED INLINING r   zFAILED INLINING %szDONE INLINING %sr  r4  )(rS  r}  r  nullcontextrP  r3  r  r   r   r  r  r  r;   r  r   r  r  rb   ra  r   r	  r@  rE  rL  r.  traced_coder   r   r}  r@  r*  is_generator_from_ctx_managerr,   generator_exhaustedis_constant_noner  rr  rt   generated_itemsr\   )r   rS  r"  
strict_ctxr  rJ  r   s          r   r  z*InliningInstructionTranslator.inline_call_>  s   {{$002
""55f6M6MNJ	D 
 +/*C*CF';;""#**400##///>>V---##**4+@+@A((D,J,JJ(		$d+##//66t<44tCD22 T"t%KL,,!$(NOOO++<<> 001D,,]DtL+++D!!$(NOOO++<<>>>+(("2"4 
 +++u $$ 	5dV3qcBCIIcN}} 	*%dV2aS1CIIcNc") 	II*D1	 +/*C*CF'sH   J I6(J 6J ;J LJ44L
)K33$LL L-c                   |j                         }|d   }	t        |	t              s|	j                  }	|j                  j
                  }
d }|
r*|
j                  j                  |      r|
j                  |   }|'t        |      }t        |       |
r||
j                  |<   t        | -  |j                  i ||	|||||t               D ci c]  }|t        ||       c}||j                  |j                  dz   |j                   |j"                  |j$                  |j&                         || _        || _        |j,                  | _        d | _        |j0                  j3                         | _        |j4                  | _        y c c}w )N__builtins__r   )r  r  r	  r  r?  r@  rA  rB  r_  r{  r}  rc  rK  r  rN  r_  rV  )get_globalsr@  r   __dict__r  r.  previously_cleaned_instructionsr  r    r   r
  r  r*   r  rc  rK  r  rN  r_  rV  funcvarrS  rb  r   ra  r  r  )r   rS  r"  r?  r@  rA  rB  r<  r	  r  r+  r_  r   r  s                r   r  z&InliningInstructionTranslator.__init__  sp    '')	~.
*d+#,,J mm33::>>tD*JJ4P/5L-DP;;DA==!+-*G"7%7DG!!WT1--G==,,q0"22,,$66NN# 	 	
& ))#%55::<)) Hs   Fc                .    | j                   j                  S r   )rS  r/  r   s    r   r/  z'InliningInstructionTranslator.fake_mode  s    {{$$$r   c                \    t        j                  | j                  j                               S r   )r   r  rS  r  r   s    r   r  z)InliningInstructionTranslator.run_ctx_mgr  s     ++DKK,E,E,GHHr   c                    t         j                  rD| j                  j                         sy| j                  j                         syt        |          S yr  )r   r  r<   should_allow_nested_graph_breaksrS  ry  r
  )r   r  s    r   ry  z:InliningInstructionTranslator.should_compile_partial_graph  sC    %%<<@@B;;;;=7799r   c                d    t         j                  rt        |   ||      S t	        dddg        y )NzGraph break in inlined functionr   z2Graph breaks in an inlined call are not supported.rn  )r   r  r
  r  r:   )r   r   r  r  s      r   r  z3InliningInstructionTranslator.create_call_resume_at  s5    
 %%707PQQ5L		
r   c                F    | j                         | _        d | _        t        r   )r  r   r   r+  r  s     r   r[  z*InliningInstructionTranslator.RETURN_VALUE  s    #xxz#' r   c                H    | j                  |      | _        d | _        t        r   )r  r   r   r+  r  s     r   r\  z*InliningInstructionTranslator.RETURN_CONST  s#    #//5#' r   c                2   d| j                   v r| j                   d   j                  d      sv| j                   d   }| j                  |      }d|v r(t        j                  j
                  j                  |   }nt        |      } t        | |      |      }t        ||      }n[| j                  j                  d| j                         }t        |      }| j                   } t        | |      |      }t        ||      }t        |      rt        |      }|||fS )Nr   namedtuple_r"  ___unnamed_scope)r	  rP  r  r  rV  r'  r(  r   r_   rH   r  r)  rJ   rI   rG  rN   )	r   r   r  r  r  fglobals_vtglobal_sourceglobals_nameglobals_sources	            r   get_globals_source_and_valuez:InliningInstructionTranslator.get_globals_source_and_value  s    'z0J0U0U1
 ..4K ..{;M+-MM22LL#  "0!<>/$>~NK&}d;M;;;;"DNNL *,7N!^^N?/$?OK-ndCM^$ ,M:M{M99r   c                   |j                   }|| j                  vr| j                  |      S | j                  j                  | j                  u rt
        |   |       y | j                  |      \  }}}| j                  j                  j                  ||      r6| j                  | j                  j                  j                  ||             y | j                  |   }| j                  t        j                  | ||             y r   )r`  r	  r  r  r+  r
  r  rK  r  has_pending_mutation_of_attrr0  	load_attrr]   r  )r   r   r   r   rG  rH  r  r  s          r   r  z*InliningInstructionTranslator._load_global  s    {{t~~%$$T**;;##t~~5 G &,0,M,Md,S)A{M{{''DD[RVW		$++22<<[$OPt,		///e]KLr   c                l   | j                   j                  | j                  u rt        |   |       y | j                         }t        |t              rt        d|j                  dg        |j                  }| j                  |      \  }}}| j                   j                  j                  |||       y )Nz3Storing Tensor hook handle in globals (inline call)r  rn  )r  r+  r	  r
  r   r  r@  r   r:   r`  rK  r  
store_attr)r   r   r  r   _fglobals_valuerG  r   r  s          r   r   z*InliningInstructionTranslator.STORE_GLOBAL  s    ;;##t~~5 G &HHJE%!89Q KK 8	 ;;D.2.O.OPT.U+O[!KK$$//T5Ir   )
rS  r   r  r   r   r   rx  r   r   r   )r  r   r   ztrace_rules.SkipResult)
rS  r   r  r]   r   rD  rx  r   r   r  rk  )rS  r   r"  rp  r?  r>  r@  r>  rA  r   rB  r   r<  z7BaseUserFunctionVariable | LocalGeneratorObjectVariabler   r   rn  ri  r  rm  rh  )r   r   r   z#tuple[Any, VariableTracker, Source])r   r   r   r   r   r  r  r{  r  r  r  r  rr  r/  r  ry  r  r[  r\  rK  r  r   r  r  s   @r   r  r  d  sR   *..%%% % = =~ KKK $K 	K
 
'K KZB,H9*)9* 9* 4	9*
 59* (B9*  39* I9* 
9*v % %I

 $=
 
	


%:%:	,%:NM"J Jr   r  c                  l     e Zd ZU ded<   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*  rD  r5  c                N    t        |   |i | g | _        d| _        d| _        y r  )r
  r  r5  r3  r2  )r   r   rx  r  s      r   r  z/InliningGeneratorInstructionTranslator.__init__4  s-    $)&)!#( -2*r   c                     yr  r   r   s    r   ry  zCInliningGeneratorInstructionTranslator.should_compile_partial_graph:  s    r   c                d   | j                         }| j                  j                  |       t        | j                        t        kD  rt        j                  dt               | j                  t        j                  d              t        j                  s| j                  r|| _        t        y )NzqToo many yield values in generator. Maybe you are inlining an infinite generator. If not, please report a bug at )r  r5  r   r   rp   r   InfiniteGeneratorErrorPT2_ISSUE_TRACKER_URLr0  rb   ra  r   r}  r2  r   r-  )r   r   tops      r   YIELD_VALUEz2InliningGeneratorInstructionTranslator.YIELD_VALUE>  s    hhj##C(t##$'99,,22G1HJ  			"))$/05511#&D 	 2r   c                    | j                   d   }t        |t              sC| j                          t	        t
              j                  | |gi       }| j                  |       y y r  )rE  r@  rt   r  ra   r  r1  r0  )r   r   r  ress       r   GET_YIELD_FROM_ITERz:InliningGeneratorInstructionTranslator.GET_YIELD_FROM_ITERO  sN    jjn#34HHJ!$'55dSE2FCIIcN 5r   c                0    d| _         t        | 	  |      S r  )r3  r
  r[  r   r   r  s     r   r[  z3InliningGeneratorInstructionTranslator.RETURN_VALUEV      #' w#D))r   c                0    d| _         t        | 	  |      S r  )r3  r
  r\  r^  s     r   r\  z3InliningGeneratorInstructionTranslator.RETURN_CONSTZ  r_  r   c                   t        | j                        dk\  sJ | j                         }| j                  d   }|j                         st	        dddg        	 |j                  |       }t        | j                  t              r| j                  dkD  sJ | xj                  dz  c_        | j                  |       | j                  |       y # t        t        j                  f$ rw}t        |t        j                        rt        j                  |        | j                          | j                  t        j                   |j"                               Y d }~y d }~ww xY w)	Nr  r  Unreachable sub-generator coder   @Should only be encountered while implementing generator support.rn  r   r   )r   rE  r  r4  r:   rq  r@  r   r   r0  rY  rr  r   rs  r  rb   ra  r  r   r   r!  r  exs        r   
YIELD_FROMz1InliningGeneratorInstructionTranslator.YIELD_FROM^  s"   4::!###hhjjjn##% 8^		###D)C 433S9,,q01 $$)$IIcNT"# s<<= 	9"c;;<--d3 HHJII&--bhh788	9s   C E'A-EEc                   t        | j                        dk\  sJ | j                         }| j                  d   }t        |t        t
        f      s>t        |t              rrt        |j                  t        j                  j                        rD|j                         r$	 |j                  |       }| j                  |       y t-        dddg        y t-        dd	t/        |       d
t/        |       dg        y # t        t        j                   f$ rl}t"        j$                  dk  r| j                          | j                  t'        j(                  |j                               | j+                  |       Y d }~y d }~ww xY w)Nr  r  r  rb  r   rc  rn  zSEND with bad typez
TOS type: z(Attempted to SEND with unsupported type r?  )r   rE  r  r@  rs   rk   r   r  r  r  Iteratorr4  rq  r0  rr  r   rs  rD  r  rb   ra  r  r:   r[   rd  s        r   SENDz+InliningGeneratorInstructionTranslator.SEND  s6   4::!###hhjjjnc,.JKLs56399koo&>&>?##%#++D1C IIcN < b	 ,$WS\N3Fws|nTUV	5 &s'D'DE 
$ '''1
II.55bhh?@IIdOO
$s   C; ;F A"E;;F )r   r   rx  r   r   r   r  rh  )r   r   r   r   r  ry  rY  r\  r[  r\  rf  ri  r  r  s   @r   r*  r*  0  s2    **3"**$#L*r   r*  )r   r   r   ztypes.ModuleType)r   r  )r   r   r   zGenerator[None, None, None])r<  zCallable[..., object]r   zCallable[..., Any])rF  r  r   r   )rV  r   r   rg  )r   r   rb  Callable[[object], bool]r0  r   r   r   )rb  rj  r0  r   r   r   )r0  r   r  r   r   zYCallable[[Callable[..., None]], Callable[[InstructionTranslatorBase, Instruction], None]](   r   
__future__r   r  collections.abcr  r  r   rS  r:  r   r7  rs  r  r   r]  r#  rD  	threadingr~  rA  r  r   r   typingr   r   r   r	   r
   r   r   typing_extensionsr   r  torch._loggingtorch._dynamo.excr   r   torch._guardsr   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.   r/   codegenr0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   funcname_cacher<   guardsr=   r>   output_graphr?   r@   rA   	polyfillsrB   replay_recordrC   rD   rd  rE   rF   rG   r  rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   utilsrR   rS   rT   rU   rV   rW   rX   rY   rZ   variables.baser[   r\   r]   variables.builderr^   r_   r`   variables.builtinra   variables.constantrb   variables.ctx_managerrc   rd   re   rf   variables.dictsrg   rh   variables.functionsri   rj   rk   rl   rm   rn   ro   variables.iterrp   variables.lazyrq   variables.listsrr   rs   rt   ru   rv   rw   variables.miscrx   ry   rz   r{   r|   r}   variables.nn_moduler~   r   variables.streamsr   variables.tensorr   r   variables.torch_functionr   r   variables.user_definedr   r   r   r   r   r   r   r   torch._subclasses.fake_tensorr   rV  r   	getLoggerr   r  rN  getArtifactLoggerrP  r'  r  r  r  r  r   r1  r   r   containsr   rx  r   rW  r   cacher   	dataclassr   r   r   r   r   r  r  r  r  r  rw  r   r  r+  r-  r=  rG  rZ  rh  rS  r  r  r  r  r  r   r6  r  _nb_opsr  r  r  r*  )r   r   r   r   s   0000r   <module>r     sj  0 #      
        	 
      " Q Q Q $   O 1 / < /      " '     ) / N N 0 9 
	 	 	 ;
 
 
 G F R R . 0  <   / /   Q 2 G  ==<'g!..228^L11(LI>>33HnM ^^55h@PQ ioo4R4L4R4R4T',0AqAq'''' ^  "("3"34BBX]]+99
 D ! H  D  %&(y  ) )   : @ @ @F 
 
 
 2 2 23 3. 4 4 )!) ) )& $!$ $ $ ! ! !H	~ 		I 	9 A . .&'
#'&' ' 
	'T BG&G.2G=G^PP"PPfJ J4 _ _ _DZ3;'Z3;zfZ5 Zz w 
 FA	 	!#v-F12JWVABZL3I	
IJ$= IJX~-J ~I['^Ls   T7:"T=