
    iw                   X
   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mZmZmZmZmZmZ ddl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l0m1Z1 ddl2m3Z3m4Z4m5Z5mZ6m7Z7m8Z8 ddl9m:Z:m;Z;m<Z<m=Z= ddl>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZL ddlMmMZM ddlNmOZO ddl4mPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZW ddlXmYZY ddlZm[Z[m\Z\ ddl]m^Z^m_Z_ ddl`maZa ddlbmcZcmdZd ddlemfZfmgZgmhZh ddlimjZjmkZkmlZlmmZmmnZnmoZompZpmqZq ddl7mrZrmsZs ddltmuZumvZvmwZwmxZxmyZymzZzm{Z{m|Z|m}Z} ddl~mZmZmZ ddlmZmZmZ ddlmZ dd lmZ dd!lmZmZmZ dd"lmZmZ dd#lmZmZmZmZmZmZmZ dd$lmZ dd%lmZ dd&lmZmZmZmZmZmZ dd'lmZmZmZmZmZmZ dd(lmZ dd)lmZmZmZ dd*lmZmZ dd+lmZmZmZmZmZ erdd,lmZmZ dd-lmZ dd.lmZ  ej                  e      Ze$j                  j                  ed/      Ze$j                  j                  ed0      Ze$j                  j                  ed1      Ze$j                  j                  ed2      Z ej                         Z ej                         D  ci c]  \  } }|  e|      j                   c}} Zd3ed4<    eej                        j                  Z eej                        j                  Zd5 ed6<   d7 ed8<   d9Zee8jL                  eef   Zd:ed;<   e	j                  djd<       Zej                   G d= d>             Zej                   G d? d@             Zej                   G dA dB             Zej                   G dC dD             Z G dE dF      Ze	j                  dkdG       Zej                  	 	 	 	 dldI       Zej                  	 	 	 	 dldJ       Zej                   G dK dL             Z G dM dNe      Z G dO dPe      Z G dQ dRe      ZdmdSZdndTZ	 	 	 	 	 	 	 	 dodUZdVZ	 	 	 dp	 	 	 	 	 	 	 	 	 dqdWZ	 	 	 	 	 	 drdXZ	 	 	 	 dsdYZ G dZ d[e      Zej                   G d\ d]             Z G d^ dHe_      Z G d` dae      Zej                  dbk\  r6ej                  D cg c]  \  }} eedc|v r|ddd nde|ddd         c}}Z G df dge      Z G dh die      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)StackSummary)AnyCallablecastNoReturnOptionalTYPE_CHECKINGUnion)	TypeAliasTypeIs)patch)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_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get_stack_above_dynamoResumePrologueTracingErrorunimplemented_v2Unsupported)get_funcname)GuardBuilderinstall_guard)GraphCompileReasonOutputGraph)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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)supported_comparison_opsSymNodeVariableTensorVariable)SymbolicTorchFunctionStateTorchFunctionModeVariable)RemovableHandleVariableUserDefinedClassVariable!UserDefinedExceptionClassVariable"UserDefinedExceptionObjectVariableUserDefinedObjectVariable)	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>N  s*      
%)QqcAHHJ< 
s   "$)joinr   itemsr   s    r   r   zLocalState.renderM  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   G  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   T  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.specializea  s    !!%%e,r   c                    || j                   v S r   )r   r   s     r   should_specializez TensorifyState.should_specializee  s    1111r   c                8    | j                   j                          y r   )r   r   r   s    r   r   zTensorifyState.cleari  s    !!'')r   c                2    t        | j                        dk(  S r   )r   r   r   s    r   emptyzTensorifyState.emptym  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   [  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  r  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	  w  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                  R    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  rC   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   rC   )r   r   r  r   r   r   )r   r   r   r   r  r  r  r  r   r   r   r  r    s9      	   -5.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
parametersr]   	functoolswraps)fnr.  r,  r-  s     @@r   stack_opr6    sL    !!"%001ER F__RD D Kr   c                    t         j                  dk  ryt        | t        j                        sy| j
                  j                  d      d   t         j                  v S )N   
   F.r   )sysversion_info
isinstancetypes
ModuleTyper   splitstdlib_module_names)mods    r   	is_stdlibrD    sK    
'!c5++,<<c"1%)@)@@@r   c                R   |t         j                  us|ryt        | j                  t              sJ | j                  }| j
                  |   }|j                  dk7  ry|dz  }d}| j
                  |   }|j                  dk(  rrt        |j                  t              sy|j                  }|dz  }| j
                  |   }|j                  dvry|dz  }|j                  dk(  r|dz  }| j
                  |   }|j                  dk7  ry| j                  t        j                  |             y	)
NFLOAD_ASSERTION_ERRORr   zassertion error
LOAD_CONST)CALL_FUNCTIONPRECALLCALLrI  RAISE_VARARGST)operatortruthr>  r   r   instructionsr   argvalr   r)  r^   create)r   truth_fnr)  current_instruction_pointerr   	error_msgs         r   &_detect_and_normalize_assert_statementrT    s.   , 	&4d..444"&":":89D{{,,1$ "I89D{{l"$++s+KK	 	$q(#  !<=;;BB 	$q(#;;)#'1,'  !<={{o%II%%i01r   Fc                r   |(t         j                  j                  j                         }	 |d   j                  |d   j
                  f}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       t"        j%                  t&        j(                        r1t*        s+t-        j.                  |      rt"        j1                         y t"        j1                  d|d   |d   |       y # t        $ r | d   | d   f}Y iw xY w)Nco_filenameco_firstlinenor   zGraph break in user code at r   r   r   z
Graph Break Reason: z
User code traceback:
zQ
========== most recent `torch.compile` tracing attempt started here ==========

a}  
NOTE: the most recent `torch.compile` tracing attempt might not be where you applied `torch.compile`! This is due to how graph breaks are implemented - the optimized code object returned by Dynamo will call another Dynamo-generated resume function and tracing is re-enabled by calling the resume function as a normal Python function, which Dynamo intercepts as a top-level frame.
artifactc                     dddS )Ndynamo_graph_break_reasonstring)r   encodingr   r   r   r   r   z!log_graph_break.<locals>.<lambda>R  s    / 
 r   c                 B     d rt        j                          S d S )Nr   r   )	traceback
format_exc)exc_infouser_stack_traces   r   r   z!log_graph_break.<locals>.<lambda>V  s+    ./rH)2F2F2H1]^ Z\1]^ r   )metadata_fn
payload_fnzmGraph break (user stack suppressed due to duplicate graph break) in user code at %s:%s
Graph Break Reason: %s)torch_guardsr   extract_stackr   r   
IndexErrorr   verboser5   r   r_  format_listr3   r   _loggingtrace_structuredgraph_break_logisEnabledForr   DEBUGexplainrS   r   debug)	code_optionsr   ra  
user_stack	frame_locstack_above_dynamo_formattedstack_above_dynamouser_stack_formattedrb  s	     `     @r   log_graph_breakrx  !  s    ]]11??A

^,,jn.C.CD	 $& ~~35')ww!!"45(
$ ,-
:
+J7
779#8#8#DE
&y|nAil^ D%h '!	!  ~~+, -a#$ %HH	
 	C 455	NN##
 _ $  	$$W]]3+//	:
 		
 	|aLaL		
}  
 ')*
	
s   F F65F6c                |     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           	        t        | j                  t        
d| 	             | j                         sJ | j	                         r=d| j                          }t        j                  |       t        j                  |      | j                  |       t        j                  d       | j                  j                  | t        dt        |       | | j                         g      d      }| j!                          | j#                  | j$                  |d	      }r| j                  |       |j&                  J | j#                  |j&                  |d	      }t(        j*                  d
k\  r%| j                  j-                  t/        d      g       t/        |j0                  |d         }|j3                  |       | j                  j-                  |g|z   |z          y )Nattempted to jump with gb_typecontextexplanationhintsr   BSkipping frame because there is a graph break in a for/while loop
zgeneric_jump triggered compilezgeneric_jump r   r   
stack_popsFr9     TO_BOOLr   )r  )rx  rr  r4   should_compile_partial_graphmaybe_has_backedgeframe_summaryr  infor   	SkipFramer)  rq  r  compile_subgraphr<   rW   popcreate_call_resume_atnext_instructionr  r<  r=  add_output_instructionsr&   r   copy_positions)r   r   value	extra_msgmsgall_stack_locals_metadataif_nextif_jump	jump_inst_explanation_gb_type_hintsr)  s            r   jump_graph_breakz&generic_jump.<locals>.jump_graph_break  s    	- 1%9(		
 00222""$U%%'(*  HHSM--$$		%		23$(KK$@$@%/	{;d>P>P>R=S  %A %
! 	
,,!!#<e
 IIe{{&&&,,KK2E
 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       t        |t              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 t        |t              r| j                         r | ||       y t        |tN              rS| j                  jQ                  |jR                        } |      r%r| jM                  |       | j                  |       y y t        |tT              r(	 |jW                  | d      }	t        |	t^              r|	ja                  | g i       }t        |tb              rWt        |jd                  t
        tf        f      r7 |jd                        rr| jM                  |       | j                  |       y t        |t&              r5|ji                         rir| jM                  |       | j                  |       y t        dd|	 d| dg        y  d      r%r| jM                  |       | j                  |       y y y y t        |t              sW|jk                  |       rF tm        |jo                  |                   r%r| jM                  |       | j                  |       y y t        |t&              r	 t        |j(                  t         j*                        r|ji                  | j                        }
ntq        |j(                  d	k7        }
 |
      r%r| jM                  |       | j                  |       y y t        |tt        jv                        r. d      r%r| jM                  |       | j                  |       y y ddl!m<} |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 Ow xY w# tX        jr                  $ 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).r~  r*  r   z#Assertion failed on symbolic shapesr   )example_value__bool____len__z3Data-dependent branching with non-constant __bool__zmethod: z
, result: z}Attempted to perform data-dependent branching on a user-defined object with a __bool__ method that did not return a constant.Tr   )r  r   )is_constant_sourcerz  r}  r{  )>r  r    rewrite_assert_with_torch_assertrT  is_python_constantr   as_python_constantjumpr  r7   r   FUNDAMENTALr>  r|   r  create_proxyre  _assert_asyncrV   r{   sym_numSymBoolfxexperimentalsymbolic_shapesexpect_truer   
USER_ERRORscalar_tensorr\   rP   noderb   sourcer;   
make_guardr:   SEQUENCE_LENGTHr)  ry   get_submodule
module_keyr   var_getattrr   ObservedAttributeErrorhandle_observed_exceptionrj   r*  r^   r  r   evaluate_exprhas_unpack_var_sequencer   unpack_var_sequencer   	UserErrorr   BackwardHookVariabler  get_real_value)r   r   r  rS  sym_exprresultscalar_to_tensor_proxyscalar_to_tensorrC  xeval_resulter  r  r  r)  rQ  s                r   innerzgeneric_jump.<locals>.inner  sB   !%336tXtL)-I'')002399T?*668$T47$ `")% 1%H.::u
 ;N* %0(((#'' '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
 u~/D4U4U4WT4//0++++E,<,<=C}IIe$		$  89	%%dJ7 !/0r26f&67JLL4+=  - IIe,		$8++- IIe,		$$ U"*1#Zx @%X  D>		%(IIdO " . .. E>2u7T7T8
 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	] -- --d3))$	:A11 11$7Ap == 446+D$2aSRRs[   Z >A\ (\:[\*[:7\9[::\ \]\<5];\<<])r   )
r   r   r   r+   r  rY   r  r   r   r   r   r   r   r+   r   r   )r   r  )rQ  r)  r  r  r  r  r  s   `` @@@@r   generic_jumpr  r  s     *H	@ 		&	&;F 	4M'4M4M 4M 	4M
 
4M 4Ml~ ~@ Lr   c                     	 	 	 	 d fd}|S )Nc                f     t        j                         d fd       }	 	 	 	 	 	 	 	 dfd|S )Nc                p   | j                         }|j                  |       r"|j                  J  | ||j                        S 	  | |      S # t        $ r@}| j                  r>|j                          t        dd| j                   ddgt        j                  |       t        |t        j                        r | j                         s t        | j                  dt        |      |j                          | j#                         r>d| j%                          }t&        j)                  |       t        j*                  |      ||j                          |j-                  d	       t/        |j0                  |j                         |_        Y d }~nd }~ww xY w|j3                  | j4                         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.)r  r  r  r  from_excT)ra  r   rs  r  graph_break)	speculater   r   r8   active_generic_context_managersremove_from_statsr7   r   CAUSED_BY_EARLIER_GRAPH_BREAKr>  r   UncapturedHigherOrderOpErrorr  rx  rr  r   
real_stackr  r  r  r  r  add_to_statsr<   r  r   r   )r   r   speculationexcpr  handle_graph_breakinner_fns        r   wrapperz>break_graph_if_unsupported.<locals>.decorator.<locals>.wrapper  s   ..*K!!$'"))555)$k6H6HII)Sd++ 'S77 **,$ R"CDDhDhCi j %Dc.LL "&	 dC$D$DE88:%%!t9#	 **,]--/02  HHSM--,$6&&(!!-0%7$//%R""O'SP 11$2K2KLs   A 
FD6FFc                z   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                        }| j                  j                  | ||z
        }t        |       }g }| j                  D ]  }t        |j                  t              r:|j                  |j!                         j#                  |j$                  |             W|j                  J t        |j                  t&              sJ |j                  j)                  |       |j                  |j!                         j+                  |j$                  |              | j                  j-                  |j/                                ~t         j                  dk\  r|j                  dk(  r| j0                  | j0                  j3                         nd}t5        |      dkD  r<t         j                  dk  sJ | j                  j-                  t7        d	|
      g       |j                  J t9        |j                  d      }	|	d   j;                  |       | j                  j-                  |	       nF|j<                  J t?        j>                  |      }
d |
_         | j                  j-                  |
g       | j                  j-                  |       | jC                  |z
         tE              D ]  }| jG                  tI                       | j                  j-                  | jK                  | jL                  |d             y )Nr9     r9     rJ  rI  r  r   r   r  KW_NAMESrO  FrV  )'r<  r=  r   disstack_effectopmapargopcoder  r  r0   block_stackr>  r  r~   extend_outputr  try_except_torch_function_moderr  r_   reconstruct_typetry_finallyr  get_instructionskw_namesr  r   r&   r#   r  r  copyexn_tab_entryr+  ranger)  rx   r  r  )r   r   r   r  r  cgcleanupbr  
call_insts	inst_copyr   r)  s               r   r  zIbreak_graph_if_unsupported.<locals>.decorator.<locals>.handle_graph_break  s      G+$$w.KK6)  #//IIi($(( $$SYYv%6A B  #//TXXF(,(D(DV|0C )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IId\)*4[ -		/+,-KK//**))+Der   r  )r   r   r   r+   r   r<   r   r   )r3  r4  )r  r  r  r)  s   ` @r   	decoratorz-break_graph_if_unsupported.<locals>.decorator~  sY     
	"/	M 
#/	MbH	+H	H	 'H	 	H	T r   )r  Callable[..., None]r   8Callable[[InstructionTranslatorBase, Instruction], None]r   )r)  r  s   ` r   break_graph_if_unsupportedr  y  s$    
%	AB r   c                  $     e Zd ZdZd fdZ xZS )BytecodeDistpatchTableMetazTInstalls 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).r~  r7   r   SUPPORTABLE)r   r   s     r   _missingz5BytecodeDistpatchTableMeta.__init__.<locals>._missing  sS    2!(+dV4[\b[ccef;F8 D_ _ '22		r      )r   r   r   r   r   r   )super__init__r  r  r   getattrr3  partialr  getdispatch_table)
r   r   basesdctr  r   opr  i	__class__s
            r   r  z#BytecodeDistpatchTableMeta.__init__  s    uc*
	 "iioo/
 VY%6%6x%HII
 
 >C4[In003I	
 Js   ,B4B)
r   typer   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_exception2  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_stack6  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  r^   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  r^   r)  )r   r  oslow_oslow_update_toggler  s         r   _break_context_reference_cyclez-ExceptionStack._break_context_reference_cycleJ  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  r1  r  s     r   r  z=ExceptionStack._set_context_and_break_context_reference_cyclee  s0     	##CT__)=)AB++C0r   c                6    | j                   j                         S r   )r  r  r   s    r   r  zExceptionStack.popl  s    ""$$r   c                :    | j                   j                  |       y r   )r  r   r  s     r   r   zExceptionStack.appendo  s    s#r   c                ,    t        | j                        S r   )r   r  r   s    r   r  zExceptionStack.__len__r  s    4??##r   c                     | j                   |   S r   )r  )r   r   s     r   __getitem__zExceptionStack.__getitem__u  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__x  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(  r1  r  r  r   r  r7  r9  __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-<   dd.Zdd/Zdd0Zdd1Zdd2Zdd3Z		 	 	 	 	 	 	 	 dd4Z
	 	 	 	 	 	 	 	 dd5Z	 	 	 	 	 	 	 	 dd6Zddd7Zdd8Zdd9Zdd:Zej$                  d;k\  rdd<Zndd=Zedd>       Zdd?Zdd@ZddAZddBZddCZddDZddEZddFZddGZddHZddIZ ddJZ!eZ"ddKZ#ddLZ$ddMZ%e&jN                  ddN       Z(ddOZ)ddPZ*e+ddQ       Z,ddRZ-ddSZ.ddTZ/e/Z0ddUZ1e+ddV       Z2ddWZ3ddXZ4e4Z5e4Z6 e7e8jr                  dY      Z: e7e8jv                  dY      Z< e7e8jr                  dZ      Z= e7e8jv                  dZ      Z>dd[Z?dd\Z@dd]ZAdd^ZBdd_ZCdd`ZDddaZEddbZFddcZGdddZHddeZIddfZJddgZKddhZLddiZMddjZNddkZOddlZPddmZQddnZRddoZSddpZTddqZUddrZV eWdst      ddu       ZX eWdst      ddv       ZY eWdst      ddw       ZZddxZ[ddyZ\ddzZ]dd{Z^dd|Z_dd}Z`dd~ZaddZbddZc	 	 	 	 	 	 	 	 ddZdddZe eWdt      dd       ZfddZgddZhddZiddZjddZkelfddZmddZnenZoddZpddZqeqZrddZsddZtddZuddZvddZwddZxddZyddZz eWdt      dd       Z{ddZ|ddZ}ddZ~ddZddZddZddZddZddZddZddZddZddZddZddZddZddZeZddZddZddZddZddZddZddZ ee8j,                        Z ee8j0                        Z ee8jr                        Z ee8j6                        Z ee8j:                        Z ee8j>                        Z ee8jB                        Z ee8jF                        Z ee8jJ                        Z ee8jN                        Z ee8jN                        Z ee8jT                        Z ee8jX                        Z  eWdst       ee8j\                              Z ee8j`                        Z ee8jd                        Z ee8jh                        Z ee8jl                        Z ee8jp                        Z ee8jt                        Z ee8jx                        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ZdddZ eWdst      dd       ZddZddZe4Ze4Z e7e8jv                  dY      Z e7e8jv                  dY      Z e7e8jr                  dY      Z e7e8jr                  dY      ZddZddZddZddZddZddZddZddZddZddZddZddÄZddĄZ eWdst      ddń       ZddƄZddǄZddȄZddɄZddʄZdd˄Z	 d	 	 	 dd̄Zdd̈́Zdd΄ZddτZeddЄ       Zej                  	 	 	 	 ddф       Zdd҄Z 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fdӄZ xZS )r   r=   r  dict[str, VariableTracker]symbolic_localssymbolic_globalsr}   symbolic_torch_function_statez$Optional[dict[str, VariableTracker]]post_prune_cell_and_freevarslist[VariableTracker]stackOptional[int]r   r+   current_instructionzlist[BlockStackEntry]r  r   r   zOptional[ConstantVariable]r  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]packagec                    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)rI  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)rD  offsetr   rN  r   r   rO  rP  )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rr  r   s    r   cellvarsz"InstructionTranslatorBase.cellvars        //r   c                     | j                   d   S )Nco_freevarsr_  r   s    r   freevarsz"InstructionTranslatorBase.freevars  ra  r   c                ~    t        | d      s&| j                         | j                         z   | _        | j                  S )N_cell_and_freevars)r  r`  rd  rf  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   rg  r@  r   rN  rD  )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>  rY   )r   r  s     r   r   z:InstructionTranslatorBase.call_function.<locals>.<genexpr>  s      
 q/*
   r  r5  z$Attempt to trace forbidden callable )r>  rY   r   r   all	itertoolschainvaluesr  r  r5  callablerM   AssertionErrorr)  r*  )r   r5  r   kwargsr  s        r   r*  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>  re   r*  r   r5  r   rt  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)rO  r   "enable_faithful_generator_behaviorr,   get_coderw  InliningInstructionTranslatorinline_callrv  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   rH  r9   f_coderW  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  rW  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  rW  r  is_trace_source_log_enabledtrace_source_logrq  rU   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                  r6t        j                  d|j                   |j"                  | j                         | j%                  |       	  | j&                  |j(                     | |       | j*                  j,                   S # t.        $ r  t0        j2                  $ r}| j5                  |       Y d}~yd}~wt6        t8        f$ r Y yt:        $ r< | j                  t<        j                  d        t<        j                  dd       Y nw xY w| j                  j?                  | j                         y)	z<Process exactly one instruction, return False we should exitNFr   zTRACE %s %s %sTzempty checkpointzstep triggered compilera  ) rN   r   r   rN  rD  r  rB  r  is_non_empty_graphr  rJ  r   step_graph_breakis_trace_bytecode_log_enabledtrace_bytecode_logrq  r   rO  update_block_stackr  r  r  r  r   r   r   exception_handlerr$  r&  r8   r  r   )r   ipr   r  s       r   stepzInstructionTranslatorBase.step%  s   $=$?!%%:*.*;*;B*?? 4#%6 T--. 

113'')'+~~'7D$''..t4%%d+--$$ $++t{{DJJ 	%	?,D,T48{{....- 	$$ 	""1%|, 	 	?''/		,-II.I>		? 	  ::4;T;TUs%   (6E G);FG)%AG)(G)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   rV  )NOPJUMP_BACKWARDr   )r  r   r  r  r  r   )r   r   r   s      r   r  z,InstructionTranslatorBase.update_block_stackX  s     &&E (()Q.D,<,<R,@,G,GG(8(8(<(C(CC $$((* D H /  ##;S(S t//0A555$$((* )T#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   rN  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 | j                   j	                  | dt        d| j                         g            }| j                  r?t        j                  sJ | j                   j                  | j                  ||d             y t        |       }| j                   j                  |j                  d      |j                         g       |d   j                  j!                         D ]X  \  }}| j                   j                  t#               |j                  |      |j                         |j%                  |      g       Z | j                   j                  t'        d      t)        |      g| j*                         y )NTstep_unsupported)partial_convertr   rV  POP_TOP)r  output_instructionsrJ  rB  r  r<   r  rP  r   nested_graph_breaksr  r  r0   create_load_constcreate_binary_subscrlocals_namesr   r%   create_storer&   r'   rN  )r   continue_instr  r  localidxs         r   r  z*InstructionTranslatorBase.step_graph_break  s   ;;2222''333 ::~ %)KK$@$@ %&84;M;M;O:PQ %A %
!
 ;;----KK//**!#<d 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_mgr  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   r  zData-dependentFT)print_outputinclude_strideinclude_device)%r  r   r  rW  r  push_txr   rN  r  	Exceptionis_tracing_resume_prologuer6   r  r   r   with_traceback__traceback__r   r2   RuntimeErrorr  r  re  r  GraphModule
nn_modulesgraphprint_readablepartial_fx_graphrL  
get_recordexec_recordpop_txr>  r/  r  mark_bytecode_tracing_stop)r   r  readable_graphs      r   runzInstructionTranslatorBase.run  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>  rY   rW   rB  r   r  s     r   r)  zInstructionTranslatorBase.push  s?    {jo> 	
0?	
> 	

#r   c                4    |D ]  }| j                  |        y r   r)  )r   valsr  s      r   	push_manyz#InstructionTranslatorBase.push_many  s     	CIIcN	r   c                6    | j                   j                         S r   )rB  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   r+  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 comprehensionsr~  z*Attempted to read undefined local variablez+Could not find a local variable with name `__stack)rO  rL  f_localsadd_local_varr)  r=  unwrapKeyError
startswithreplacer7   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   )
rO  rg  r=  r  side_effects	load_cellr)  rL  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   )	rO  r  set_name_hintr=  rB   r  r  r   r  )r   r   r   	loaded_vtr  s        r   
STORE_FASTz$InstructionTranslatorBase.STORE_FAST8  sj    {{HHJ	%%.T"55..0C9$$$.1D+ 6r   c                2    | j                   |j                  = y r   )r=  rO  r  s     r   DELETE_FASTz%InstructionTranslatorBase.DELETE_FASTB  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   )rO  rg  r=  r  r  r  
store_cellr>  rs   
local_namer  )r   r   r  r  s       r   STORE_DEREFz%InstructionTranslatorBase.STORE_DEREFE  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  r^   rP  rO  _constants_cache)r   r   r  r  s       r   _load_constz%InstructionTranslatorBase._load_constQ  sv    HH9#**==##A&'7'>'>T[['QD!!!$''*C
r   c                D    | j                  | j                  |             y r   )r)  r  r  s     r   rG  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   )rO  rL  	f_globalsadd_global_var
f_builtinsbuiltinsload_builtinr>  r  r  r)  load_globalrY   buildrF   )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   rY   r  )r   module_namemodule_sourcefglobals_values       r   nn_modules_globals_vtz/InstructionTranslatorBase.nn_modules_globals_vtt  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  r  r  )r  r<  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.r~  )r  rO  rF   r>  objectr  r  track_global_existingr>  r   r7   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_)re  rS  package_importer_package_imported_modulesr  r   add_import_sourcer  import_sourcesglobal_scopeupdate_co_namesrF   )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   rS  levelbitsbases         r   resolve_namez&InstructionTranslatorBase.resolve_name  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)r9  )
stacklevelzYcan't resolve package from __spec__ or __package__, falling back on __name__ and __path__r   __path__r;  r   )r  r
  rP  r  warning
rpartition)r   rS  specs      r   calc_packagez&InstructionTranslatorBase.calc_package  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   )fromlistr  globalszImport failurezmodule_name: z, fromlist: z, level=z"Failure when attempting to import.r~  r   r;  r  zBad import resultz%Import result is not a Python module.)r+  r  rO  r@   LOCAL_MOD_PREFIXr  rF   
__import__r  r7   r   r  r(  r  	partitionr  rL  add_local_modrT   r?  r@  r?   r)  rw   rW   )
r   r   r  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	;   ,+K=XJhW\V]^ D9-889	 s   E> >1F32F3c                H    | j                  |       | j                  |       y r   )DUP_TOP
_load_attrr  s     r   IMPORT_FROMz%InstructionTranslatorBase.IMPORT_FROM  s    Tr   c                :   || j                   vrt        d| d      | j                   |   }t        |      rBt        | j                  j
                        }t        ||      }t        j                  | ||      S t        |      sJ t        j                  |      S )Nzname 'z' is not definedr  )r  r8   rr  rF   r  %name_of_builtins_dict_key_in_fglobalsrE   rY   r  rL   r^   rP  )r   rO  r  builtins_source
var_sources        r   load_builtin_from_argvalz2InstructionTranslatorBase.load_builtin_from_argval  s    (vh.>?@@oof%C=*AAO +?FCJ"((sJ??&s+++#**55r   c                X    | j                  | j                  |j                               y r   )r)  r=  rO  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   rN  r  rR   	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   rB  r  s     r   
SETUP_LOOPz$InstructionTranslatorBase.SETUP_LOOP>  ;    {{&&&dkk3tzz? STr   c           	         |j                   J | j                  j                  t        ||j                   t	        | j
                                     y r   rD  r  s     r   SETUP_EXCEPTz&InstructionTranslatorBase.SETUP_EXCEPTC  rF  r   c                8    | j                   j                          y r   )r  r  r  s     r   	POP_BLOCKz#InstructionTranslatorBase.POP_BLOCKH  s    r   c                &    | j                  |       y r   setup_or_before_withr  s     r   
SETUP_WITHz$InstructionTranslatorBase.SETUP_WITHK      !!$'r   c           	         |j                   J | j                  j                  t        ||j                   t	        | j
                                     y r   rD  r  s     r   SETUP_FINALLYz'InstructionTranslatorBase.SETUP_FINALLYN  s;    {{&&&dkk3tzz? STr   c                &    | j                  d        y r   r  r  s     r   BEGIN_FINALLYz'InstructionTranslatorBase.BEGIN_FINALLYR  s    		$r   c                    | j                  d      \  }}|J | j                  |       | j                  |j                  | t        j                  d       gdz  i              y )Nr  r9  )r+  r)  r*  r^   rP  )r   r   r  r   s       r   WITH_CLEANUP_STARTz,InstructionTranslatorBase.WITH_CLEANUP_STARTU  sX    IIaL	c{{		#		$$$T,<,C,CD,I+JQ+NPRSTr   c                H    | j                  d       | j                  d        y )Nr  )r+  r)  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\  r5| j                  |       | j                  t        j                  d              | j                  |       Y d }~y d }~ww xY wNr  )r  realizenext_variabler)  StopIterationr   ObservedUserStopIterationr>  r  r<  r=  r^   rP  r  )r   r   itr  r  s        r   FOR_ITERz"InstructionTranslatorBase.FOR_ITER_  s    XXZ!	""4(CIIbMIIcNs<<= 	!S::;--d3 7*
 		"		*11$78IIdOO	s   3A C?-BC::C?c                l    t        |t        j                  t        f      r|j	                  | g i       }|S r   )r>  r   r]   r   r*  r  s     r   _create_exception_typez0InstructionTranslatorBase._create_exception_types  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.r~  )ra  r,   r  r>  r   rt   exc_typer\  r]   r  r*  rK  r   _isinstance_exceptionr   get_dynamo_observed_exceptionr7   r   r   r  )r   r  observed_exception_types      r   _raise_exception_variablez3InstructionTranslatorBase._raise_exception_variable|  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 reraiser   rV  r   	__cause__)r  r   rK  r^   r   raise_observed_exceptionr  re  rh  rB  r  r$  ra  call_setattr)r   r   r  r  from_vtcurr_exccauses          r   rK  z'InstructionTranslatorBase.RAISE_VARARGS  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 )NrV  z CLEANUP_THROW with StopIterationr   zRReceived StopIteration when handling generator.throw/close. This is not supported.r~  )rB  r>  rt   rd  r\  r7   RERAISEr   r   toss      r   CLEANUP_THROWz'InstructionTranslatorBase.CLEANUP_THROW  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  )r<  r=  r  rO  rh  r)  )r   r   r  r   _exc_tbs         r   rr  z!InstructionTranslatorBase.RERAISE  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   rt   r   r   r  s     r   re  z/InstructionTranslatorBase._isinstance_exception  s&    ++12
 	
r   c                   t         j                  dk\  rlt        | j                        dk\  sJ | j                  d   }| j                  d   }| j	                  |      sJ t        |j                        }t        d       }nkt        | j                        dk\  sJ | j                  d   }| j                  d   }| j	                  |      sJ t        |j                        }t        d       }| j                  ||||gi        y )Nr     rV     ir  )	r<  r=  r   rB  re  r]   rd  r^   r*  )r   r   r5  r  typtbs         r   WITH_EXCEPT_STARTz+InstructionTranslatorBase.WITH_EXCEPT_START  s    w& tzz?a'''BB**R.C--c222!#,,/C!$'Btzz?a'''BB**R.C--c222!#,,/C!$'B2S"~r2r   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                 8   j                   j                         } t        j                  | j	                               }t        |      sJ t        dd| j                          d| j                   dg t        j                  t        j                         y )NzObserved exceptionrc  r   r  r~  )rK  r$  r   rf  python_typer>  r7   python_type_namer   r   r  r  )ro  
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_HANDLERr9  r   z#Observed exception (EXCEPT_HANDLER)z  This graph break is unexpected.r~  g    .Ar  r  r   )#r<  r=  rD  r  r   rB  depthr  lastir)  r   r^   rY  rK  r$  r  r   r  r/  r  r   r   r+  r7   r   r   
DYNAMO_BUGr"  r  r+   r   r  rx   r]   rd  )	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   rV  )r  r   rK  r^   r)  r"  )r   r   r  prev_excs       r   PUSH_EXC_INFOz'InstructionTranslatorBase.PUSH_EXC_INFOy  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   rV  r  zYBug in Dynamo tracing of exception handling.Top of the block stack is not EXCEPT_HANDLER.r9  )
r<  r=  r  r   rK  r  r   r   rs  r+  )r   r   r   s      r   
POP_EXCEPTz$InstructionTranslatorBase.POP_EXCEPT  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  rV  z Exception with bad expected typez"`except ...` has unsupported type r;  r~  zCaught non-Exception valuezCExcept expects to receive an object of Exception type but received z#Exception with non-type expectationz!`except ...` expects a non-type: TF)r   rB  r  r<  r=  r>  r]   rr   r   r   r7   r   r   r  re  r   
issubclassrd  r5  r   )r   expected_exc_typesexc_instanceexpected_typesexpected_types        r   check_if_exc_matchesz.InstructionTranslatorBase.check_if_exc_matches  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!1!1J /	2 r   c                h    | j                  t        j                  | j                                      y r   )r)  r   r^   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  )rO  r  r)  r   r+  r  s     r   
COMPARE_OPz$InstructionTranslatorBase.COMPARE_OP	  sD    ;;++  &II)$++6tTYYq\2NOr   c                b    | j                  t        t              | j                         gi        y r   )r*  r]   iterr  r  s     r   GET_ITERz"InstructionTranslatorBase.GET_ITER		  s!    ?40488:,Cr   r   r  c                    | j                  |j                        }| j                         }| j                  ||i        y r   r+  rO  r  r*  )r   r   r   r5  s       r   rH  z'InstructionTranslatorBase.CALL_FUNCTION	  s1    yy%XXZ2tR(r   c           	        |j                   dk(  rt        i       }| j                         }ni|j                   dk(  r!| j                         }| 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   z%Variadic function call with bad flagszflags: zHAttempted to call a variadic function (CALL_FUNCTION_EX) with bad flags r~  r  r  z0Variadic function call with bad args/kwargs typezargs type: z, kwargs type: z2Expected args to be a list and kwargs to be a dict)rO  rb   r  r7   r   r  r<  r=  r>  rv   rm   has_force_unpack_var_sequencerr   force_unpack_var_sequencer   r]   call_custom_dictr   rW   r  keys_as_python_constantr*  r   )r   r   
kwargsvarsargsvarsnullr5  s         r   CALL_FUNCTION_EXz*InstructionTranslatorBase.CALL_FUNCTION_EX	  s    ;;!*2.JxxzH[[AJ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   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  r+  rO  r>  rr   r  r  r   r   zipr*  )r   r   argnamesr   r5  kwargs_listrt  s          r   CALL_FUNCTION_KWz*InstructionTranslatorBase.CALL_FUNCTION_KWE	  s    88:yy%XXZ(M2x7R7R7TTT..0 !1CM>2D#h-9I4Jkc(K016{c(m+++2tV,r   c                R   | j                  t        j                  |d             |j                  d   }| j                  d   |   }t
        j                  dk  r'| j                  t        j                  ||             y | j                  t        j                  ||             y )Nr  r  r   co_namesr  )	rH  r   r  rO  rr  r<  r=  r7  LOAD_METHODr   r   r  rO  s       r   LOAD_METHOD_SUPERz+InstructionTranslatorBase.LOAD_METHOD_SUPERQ	  s    ;..tA>?kk!n"":.s3g%OOK//VDE[00fEFr   c                    | j                  t        j                  |d             |j                  d   }| j                  d   |   }| j                  t        j                  ||             y )Nr  r  r   r  )rH  r   r  rO  rr  r7  r  s       r   LOAD_ATTR_SUPERz)InstructionTranslatorBase.LOAD_ATTR_SUPERZ	  sX    ;..tA>?kk!n"":.s3++D@Ar   c                b   | j                  |       | j                         }t        j                  dk\  r#| j	                  |       | j                  |       y t        j                  dk\  r#| j                  |       | j	                  |       y | j	                  |       | j	                  d        y )Nr  r  )r7  r  r<  r=  r)  r  r   r   objs      r   r  z%InstructionTranslatorBase.LOAD_METHOD`	  s{    hhjw&IIcNNN4 ( NN4 IIcNIIcNIIdOr   c                    | j                  |j                        }| j                         }|J | j                         }| j                  ||i        y r   r  )r   r   r   dummyr5  s        r   CALL_METHODz%InstructionTranslatorBase.CALL_METHODp	  sF    yy%
}}XXZ2tR(r   c                    | j                         }t        t              j                  | |t	        j
                  |j                        gi       }| j                  |       y r   )r  r]   r  r*  r^   rP  rO  r)  )r   r   r  r  s       r   r7  z$InstructionTranslatorBase._load_attrw	  sN    hhj )77"))$++67

 			&r   c                    t         j                  dk\  r!|j                  dz  r| j                  |       y | j	                  |       y )Nr  r  )r<  r=  r  r  r7  r  s     r   	LOAD_ATTRz#InstructionTranslatorBase.LOAD_ATTR	  s8    w&xx!|  &r   c                   | j                         }|j                  |       r| j                  |      S | j                  d      \  }}t	        |t
              r1t	        |t              s!| j                  rJ d|j                   d       	 t        t              j                  | |t        j                  |j                        |gi        y # t        $ rS}| j                         s t        j!                  dd       |j#                          |j%                  d       Y d }~nd }~ww xY w|j'                  | j(                         y )Nr  zMutating module attribute z during export.zSTORE_ATTR triggered compileTr  r  )r  r   store_attr_graph_breakr+  r>  ry   r^   exportrO  r]   setattrr*  rP  r8   r  r  rq  r  r  r   r   )r   r   r  r  r  r  s         r   
STORE_ATTRz$InstructionTranslatorBase.STORE_ATTR	  s   nn&d#..t4499Q<Sc+,ZEU5V {{ ,T[[MI?	*G$22&--dkk:C@
  	*446II4tID!NN=))	* 	--d.G.GHs   	A C
 
	D&A	D!!D&c                   t        | j                  d       | j                         st        dddg        | j                  j                  | t        d| j                         g      d	      }| j                  j                  t        j                  |      g       | j                  d       | j                  j                  | j                  | j                  |d
             y )NzSTORE_ATTR-caused graph breakr  z-Should not compile partial graph (STORE_ATTR)r   zDynamo has determined when encountering an unsupported STORE_ATTR instruction (i.e. `obj.attr = val`) that it should not compile the partial graph.r~  
store_attrr  r  F)rx  rr  r  r7   r  r  r<   r  r  r  r+  r  r  )r   r   r  s      r   r  z0InstructionTranslatorBase.store_attr_graph_break	  s    ))2QR002Go %)KK$@$@%lT5G5G5I4JK %A %
!
 	++TYYt_,=>		!++&&%%'@%	
r   c                    | j                         }t        t              j                  | |t	        j
                  |j                        gi        y r   )r  r]   delattrr*  r^   rP  rO  r  s      r   DELETE_ATTRz%InstructionTranslatorBase.DELETE_ATTR	  s>    hhj .."))$++67	
r   c                n   d| _         |j                  dk(  rt        d      gS |j                  dk(  rt        d|j                        gS t	        | j
                  j                        }t        | j                        t        |d   j                        z
  }||d   _
        |j                  t        d|      gt        d      |j                  d      |j                         t        ddd	             g }|  |j!                         j"                   t        |      t        |      k(  sJ t%        |      D ]  \  }}|dk(  r|rt'        |j(                  |j*                        D ]  \  \  }	}
}t-        t.        ||   j                  |         }|j1                  t3                      |j5                  |       |j                  g t7        d      |j                  |      |j                         |j                  |	      t        d
              |j8                  D ]  \  }}
t-        t.        ||   j:                  |         }|j1                  t3                      |j5                  |       |j                  g t7        d      |j                  |      |j                         |j                  |j                  |j<                  |   z         t        d
               g }g }t%        |      D ]=  \  }}||   | u r|}nj>                  }tA        |      r|jB                  sJ |jB                  }tE        d|jF                         }|j!                  |       | u rztI        jJ                  |      tM        fdj:                  jO                         D              }tQ        |jR                        tM        fd|D              }tM        fd|D              }|j                  t3               |j                  |      |j                         t3               g       |D ]`  }|j                  t3               |j                  |j                  |j<                  |   z         |j                         gt7        d             b |j                  t        d      t        dt        |            gt7        d      t        |j                  dd	             n8tM        |j<                  jO                               }tM        |jR                        }tT        jV                  dk  rt        |      dk(  sJ d       t        j                        t        |j                        z
  }tY        jZ                  j\                  j^                  |jF                  tM        d j`                  D              |||tM        d j`                  D              tM        |j(                        tM        |j8                        tM        |j                        tM        |            }|j!                  |        tc        jd                  j\                        jg                  dd              }|+ti        jj                  |      tc        jd                  |      d<   |jl                  rj
                  jo                  ||       d}n=j
                  jo                  |tq        jr                  |jt                  |             |}jv                  jv                  jy                  |jt                  d   |       @ |rddl=m>}  ||d          |d   jl                  r|j                  |d   |d   |d   d	d       n%|j                  |j                  |d   d	d             |j                          |j                          t'        |||      D ]J  \  }}}|jl                  r|j                  |||dd       )|j                  |j                  |dd             L |j                  t        dt        |            gt7        d             |j                  t3               t3               |j                  d      |j                         gt7        d      |j                  d      t        d             |j                  g t        d      t        dd      t7        d      t        dd             |j                  t        dd      t        d      g       |j                         S )a7  
        Codegen resume function(s) and call it.
        Assumes that the unsupported instruction has already been run.

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

        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.
            - disable_current_frame_resume: If True, disable tracing on the current frame's resume function.
                Used for implementing nested step_graph_break.
        NrW  rX  r  r   
BUILD_LIST)r  r  TSTORE_SUBSCR__resume_at_c              3  N   K   | ]  }|v r|j                         vr|  y wr   )rg  )r   r   rZ  ri  s     r   r   zBInstructionTranslatorBase.create_call_resume_at.<locals>.<genexpr>H
  s0      %Ezav/G/G/I&I %s   "%c              3  ,   K   | ]  }|vs|  y wr   r   r   r   argnames_null_sets     r   r   zBInstructionTranslatorBase.create_call_resume_at.<locals>.<genexpr>N
  s      WqAEV<V W   	c              3  ,   K   | ]  }|v s|  y wr   r   r  s     r   r   zBInstructionTranslatorBase.create_call_resume_at.<locals>.<genexpr>O
  s     %XAFWAWa%Xr  r  r  z&variables should not be NULL in < 3.12c              3  H   K   | ]  }|j                   j                    y wr   )r  rY  r   r  s     r   r   zBInstructionTranslatorBase.create_call_resume_at.<locals>.<genexpr>
  s     B!ahhooBs    "c              3  <   K   | ]  }|j                           y wr   )r  r  s     r   r   zBInstructionTranslatorBase.create_call_resume_at.<locals>.<genexpr>
  s     @akkm@rm  orig_graphmodulec                      y r   r   r   r   r   r   zAInstructionTranslatorBase.create_call_resume_at.<locals>.<lambda>
      r   r   r   )	skip_coderV  FDELETE_SUBSCRr9  LIST_EXTENDr  )Dr   r   r&   rO  r0   r  root_txr   rB  stack_null_idxes	num_stackr  r$   r  r  r"   r   rP  	enumerater  stack_ctx_argsstack_ctx_idxes_origr   r_   append_outputr%   r  r)   locals_ctx_argsr=  r  r  r-   r  r.   rY  r   rN  r  keysr   locals_null_keysr<  r=  rA   lookupr  r   r  r/   get_contextr
  weakrefrefrc  install_global_unsafer?  FunctionTyper  rS  add_resume_function
eval_framer  make_function_with_closureload_function_namer  r(   r  ) r   r   r  disable_current_frame_resumer  current_num_stacktxesr  metajr   j_origr+  r   resume_namesresume_codesresume_instresume_nameall_argnamesr  argnames_nullr  	stack_lennew_codeorig_graphmodule_maybepackage_namer  r   coder  rZ  ri  s                                 @@@r   r  z/InstructionTranslatorBase.create_call_resume_at	  s	   4 $( ;;.(&~677[[N*&~dkkJKKt{{**+  

Oc%a(99/
 
 2C!!$.
"<5FG	Q	 $$Q'		
 '')	 %Q40		
* 6: KK]]F   4yC 9:::: !!:; "	GAtAv6"%d&9&94;T;T"U A2DGMM&4IJ  !12$$R(  $Q,,Q/ //1 ,,Q/	
 +>:   // a2DG4K4KD4QR  !12$$R(  $Q,,Q/ //1 ,,T^^d>O>OPT>U-UV	
 +>:)"	J -/ !:; v	GAt!WF~"$55
  ,")))))00#l;3E3E2F$GHK, ~)&*=*={K$ %#3388:%  
 %((=(=$>!  WL WW %%X%X X   &(,,Q///1&(	 $ C$$*,00 $1B1B31G G 335 )^	   *95*<S]K %Q
 -T^^T4H	 !!2!2!7!7!9: %d&;&; <')=)Q.X0XX. FLL)C0E0E,FFI'='D'D""Bv/A/ABB@V-?-?@@d))*d**+d++,l#(H )&\%=%=fmm%L%P%P"L& &" &1IP*J((23EF
 ##33KJ# 33&&x1A1A;O  +~~)22f..z:Liv	p (-l1o& ''))R,r*L,<dA R22<3CT1MN 	!$lC 	HNBd--b$eQG  !6!6tUA!FG		H
 	"<S5FGQ	
& 	  $$R('') Q $$R( #?3	
  	a"<Q7 Q
 #=a8	
 	"#51=">2	
 ""$$r   c                l   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r+ | j                   xr | j                   xr | j                   S )Nr  rV  Fc              3  <   K   | ]  }|j                           y wr   )r  r  s     r   r   zIInstructionTranslatorBase.should_compile_partial_graph.<locals>.<genexpr>
  s     :A:rm  )r<  r=  rD  r  r  r  rn  	one_graphr   r  r  )r   r   s     r   r  z6InstructionTranslatorBase.should_compile_partial_graph
  s    w&,,::E$$D<L<LR<P<W<W(W:)9)9:: 9NN"9---9 3339 888	
r   r   c                Z    | j                  d      \  }}}|j                  | d||gi        y )Nr9  __setitem__r+  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   )r+  rO  r)  rr   r   r   r   s      r   BUILD_TUPLEz%InstructionTranslatorBase.BUILD_TUPLE  %    		$++&		-&'r   c                n    | j                  |j                        }| j                  t        |             y r   )r+  rO  r)  rq   r  s      r   BUILD_SLICEz%InstructionTranslatorBase.BUILD_SLICE  r  r   c                    | j                  |j                        }| j                  t        |t	                            y Nmutation_type)r+  rO  r)  rp   rX   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).r~  r  )r   +inject_BUILD_SET_unimplemented_TESTING_ONLYr7   r+  rO  rc   rX   r)  )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, ...]`).r~  r  )r+  rO  extendr  NotImplementedErrorr7   r   r   r  r)  rX   )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%  rr   r  s     r   BUILD_TUPLE_UNPACKz,InstructionTranslatorBase.BUILD_TUPLE_UNPACK6  s    t7r   c           	         | j                  |j                  dz        }t        t        |d d d   |dd d               }| j	                  t        |t                            y )Nr  r   r  )r+  rO  r   r  r)  rb   rX   )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 r  )r+  rO  r]   r   r*  r>  rb   updater   r)  rX   )r   r   r   r  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 r  )r  r+  rO  r>  rr   r  r  r   r)  rb   r   r  rX   )r   r   r  rq  s       r   BUILD_CONST_KEY_MAPz-InstructionTranslatorBase.BUILD_CONST_KEY_MAPQ  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  )r+  rO  r  rB  rZ  r>  rb   r  )r   r   r   r   r  s        r   MAP_ADDz!InstructionTranslatorBase.MAP_ADDa  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  rO  r  rB  r>  rc   
is_mutabler  r   r   r   r  s       r   SET_ADDz!InstructionTranslatorBase.SET_ADDi  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   r,  r3  r5  s       r   
SET_UPDATEz$InstructionTranslatorBase.SET_UPDATEr  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  rO  r  rB  rZ  r>  rp   r4  r  r  mutationr   r   r5  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  r     r{  r  r   )r  r<  r=  r  r  r  r^   rP  r>  r)  rg   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                         }t        |t              r'|j                  | t	        |j
                              }nt        |t              rxt        |j                  t              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)idxesz+Failed to unpack object for UNPACK_SEQUENCEzV cannot be unpacked into a list for the UNPACK_SEQUENCE bytecode (i.e. `a, b, c = d`).r~  z9Length mismatch when unpacking object for UNPACK_SEQUENCEzexpected length: z
, actual: za unpacked to a list for the UNPACK_SEQUENCE bytecode (i.e. `a, b, c = d`) with unexpected length.)r  r>  r|   r  r  rO  ru   r  r  as_proxyr   r\   r  r  r7   r   r   r  r   r  r   r)  )r   r   r$  r  proxyr  s         r   UNPACK_SEQUENCEz)InstructionTranslatorBase.UNPACK_SEQUENCE  sW   hhjc>*))$eDKK6H)IC_-*SWWn2U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     r?  z%Failed to unpack object for UNPACK_EXz; cannot be unpacked into a list for the UNPACK_EX bytecode.r~  )rO  r  r  r   r  r   r   r)  rr   r7   r   r   r  )
r   r   prefixsuffixr$  r  vals_prefix	vals_listvals_suffixitems
             r   	UNPACK_EXz#InstructionTranslatorBase.UNPACK_EX  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   c                >    | 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 = Falser~  )rO  r7   r  s     r   graph_break_on_leaf_functionz6InstructionTranslatorBase.graph_break_on_leaf_function  s)    =X]	 r   c                F    |j                   dk(  r| j                  |       y y )NGRAPH_BREAK_IF_LEAF)rO  rU  r  s     r   r  zInstructionTranslatorBase.NOP  s"    ;;//--d3 0r   c                $    | j                          y r   r  r  s     r   r  z!InstructionTranslatorBase.POP_TOP      
r   c                    | j                         }| j                         }| j                  |       | j                  |       y r   r  r)  r   r   ar  s       r   ROT_TWOz!InstructionTranslatorBase.ROT_TWO  s.    HHJHHJ		!		!r   c                    | j                         }| j                         }| j                         }| j                  |       | j                  |       | j                  |       y r   r\  )r   r   r^  r  cs        r   	ROT_THREEz#InstructionTranslatorBase.ROT_THREE  sD    HHJHHJHHJ		!		!		!r   c                   | j                         }| j                         }| j                         }| j                         }| j                  |       | j                  |       | j                  |       | j                  |       y r   r\  )r   r   r^  r  ra  r)  s         r   ROT_FOURz"InstructionTranslatorBase.ROT_FOUR  sZ    HHJHHJHHJHHJ		!		!		!		!r   c                h    | j                         }| j                  |       | j                  |       y r   r\  )r   r   r^  s      r   r6  z!InstructionTranslatorBase.DUP_TOP  s#    HHJ		!		!r   c                    | j                         }| j                         }| j                  |       | j                  |       | j                  |       | j                  |       y r   r\  r]  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  r9  )r]   r   r*  repr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   )LazySymNodeFormatStringrl   r,  r9  z{:})r  r>  r{   torch._dynamo.variables.lazyrn  rl   rP  r  r)  rl  r^   r  r*  r]   r   format)r   fmt_specr@  r  rn  rl   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  r^   rP  rt  )r   r   r@  rr  s       r   FORMAT_VALUEz&InstructionTranslatorBase.FORMAT_VALUE7  sQ       DLT!xxzH'..r2H!!(E22r   c           
     \   g }g }i }|j                   J | j                  |j                         D ]:  }t        |t              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 conflictr~  zBUILD_STRING type errorzLFormat string part type is not correct - expected constant or format string.r   )r  r+  r>  r^   r   r   StringFormatVariableformat_stringr!  sym_argsr   r  
sym_kwargsr7   r   r  r,  r   r)  rP  r   )r   r   format_string_partsr   rt  parts         r   BUILD_STRINGz&InstructionTranslatorBase.BUILD_STRINGA  st   )+&(-/xx###IIdhh' 	D$ 01#**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  )rO  r&   r  )r   r   
new_argvalnew_insts       r   IS_OPzInstructionTranslatorBase.IS_OPb  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__)rO  r+  r)  r  r   ObservedTypeErrorr8   r>  r  r}  rY   r  r>   	UNARY_NOT)r   r   leftrightr  r  s         r   CONTAINS_OPz%InstructionTranslatorBase.CONTAINS_OPk  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  rO  r  rB  r>  rp   r4  r  r5  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   )r)  r]   r  r*  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 )NrV  zgenerator raised StopIterationr'  rk  )
rB  re  rd  r\  r   r]   r  r*  r^   rm  )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 r8  )	r  rO  r  rB  rZ  r>  rb   r4  r  r5  s       r   
DICT_MERGEz$InstructionTranslatorBase.DICT_MERGE  s    HHJ{{Qxx###jj$((#++-#0111~~hR0r   c                $    | j                          y r   rY  r  s     r   	GEN_STARTz#InstructionTranslatorBase.GEN_START  rZ  r   c                    | j                   d   }|j                         r<| j                  t        j                  t        |j                                            y | j                  |j                  | dg i              y )NrV  r  )rB  r  r)  r^   rP  r   r  r  rs  s      r   GET_LENz!InstructionTranslatorBase.GET_LEN  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 NrV  TF)
rB  r>  rb   r   collectionsabcMappingr)  r^   rP  rs  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  )rB  r  r  r>  r  r  r   r   bytes	bytearrayr)  r^   rP  )r   r   rt  	tos_values       r   MATCH_SEQUENCEz(InstructionTranslatorBase.MATCH_SEQUENCE  s    jjn%%'''**,	i!9!9::UI.D
 II&--d34II&--e45r   c                2   | j                   d   }| 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 )NrV  r  c              3  &   K   | ]  }|v  
 y wr   r   )r   r   tos1s     r   r   z7InstructionTranslatorBase.MATCH_KEYS.<locals>.<genexpr>  s     &QqDy&s   r  TF)rB  r>  rb   rn  r)  rr   getitem_constr<  r=  r^   rP  )r   r   rt  r   r  s       @r   
MATCH_KEYSz$InstructionTranslatorBase.MATCH_KEYS  s    jjnzz"~$ 1222&#&&IIm#$NQT%7%7a%@$NOP')		*11$78 * II&--d34')		*11%89 * %Os   Dc                D    | j                  | j                  d             y )Nrs  )r)  r=  r  s     r   rF  z.InstructionTranslatorBase.LOAD_ASSERTION_ERROR  s    		$//0@ABr   c                D    t        ddddgt        j                         y )Nz'LOAD_BUILD_CLASS bytecode not supportedr   zPDynamo does not support tracing classes that are defined in the compiled region.z5Move the class definition out of the compiled region.r~  r  r  s     r   LOAD_BUILD_CLASSz*InstructionTranslatorBase.LOAD_BUILD_CLASS  s)    =jG"..		
r   c                p    |j                   dk(  r| j                  |       d| _        y | j                  rJ y )Nr   F)r  append_prefix_instrE  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   rI  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  )rr  r  r>  r  r   r  r^   rP  )r   r   r  r   s       r   r  z"InstructionTranslatorBase.KW_NAMES  sl    $$[1$((;(E*** 	)DdC(((	)}}$$$(//h?r   c                6    | j                  t                      y r   )r)  rv   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)Nr  r   r  r   r   )r<  r=  r  r>  rr   r  r  r  r  r  r+  rv   r   r   r  r*  )	r   r   call_kwr  contentsr5  r   r  rt  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c                &    | j                  |       y r   r  r  s     r   rJ  zInstructionTranslatorBase.CALLQ  s    

4r   c                r    |j                   J | j                  | j                  |j                              y r   )r  r)  rB  r  s     r   COPYzInstructionTranslatorBase.COPYU  s-    xx###		$**dhhY'(r   c                    |j                   J | j                  |j                       | j                  d   c| j                  d<   | j                  |j                    <   y NrV  )r  rB  r  s     r   SWAPzInstructionTranslatorBase.SWAPY  sM    xx###04

DHH90EtzzRT~-

2

DHH9-r   c                     y r   r   r  s     r   CACHEzInstructionTranslatorBase.CACHEe  r  r   c                &    | j                  |       y r   rL  r  s     r   BEFORE_WITHz%InstructionTranslatorBase.BEFORE_WITHh  rO  r   c           	     @   | j                         }t        |t        t        f      s&t	        dd| d|j                          dg d       t        |t              r+|j                         s| j                  j                  |       t        |t        t        f      sJ t        ||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                  }| j!                  |       |rt        | t"              st$        j&                  r;| j                  j                  t)        ||t+        | j,                        |             n9| j                  j                  t)        ||t+        | j,                                     | j!                  |j/                  |              y )	NzUnsupported context managerz$Attempted SETUP_WITH/BEFORE_WITH on z%Dynamo does not know how to enter a `z` context manager.)z,Avoid using the unsupported context manager.aE  If the context manager seems like it should be supported (e.g. torch.set_grad_enabled), then it may be the case that it was created outside the compiled region, which Dynamo does not support. Supported context managers can cross graph break boundaries only if they are local non-closure variables, or are intermediate values.zFile an issue to PyTorch. Simple context managers can potentially be supported, but note that context managers can't be supported in generalr~  r  rV  )r  r>  r_   r`   r7   r  supports_graph_breaksr  r   ra   r  r<  r=  r  r  r  r)  r/  r   r  r  r   rB  enter)r   r   r+  r  r  s        r   rM  z.InstructionTranslatorBase.setup_or_before_withk  s   hhj)+IJ
 5>seDCCDXDXDZC[[mn	  s:;--/0077< )+IJ
 	
 
 (KK

 w&
 !!$$%%,,D4D4DR4H4O4OO,,::FFF..<<CC[[F		$$ 56&:T:T  ''#D&#djj/3G   ''fc$**o(VW		#))D/"r   c                V    | j                   sJ | j                  j                  |       y r   )rE  rG  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 rY  )r<  r=  rE  r>  r=  rO  rv   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_VARS      %r   c                &    | j                  |       y r   r  r  s     r   RETURN_GENERATORz*InstructionTranslatorBase.RETURN_GENERATOR  r  r   c                n    t         j                  dk\  r| j                          y | j                  d       y )Nr  r  )r<  r=  r  r+  r  s     r   END_FORz!InstructionTranslatorBase.END_FOR  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 r~  )	rT   r=  r
  rO  rv   r7   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   )rO  r=  r)  rv   r  r  s     r   LOAD_FAST_AND_CLEARz-InstructionTranslatorBase.LOAD_FAST_AND_CLEAR  sD    ;;d222IIln%NN4 ,8NT[[)r   c                    | j                  t        j                  |d             |j                  J |j                  dz  r| j	                  |       y | j                  |       y )Nr  r  r   )rH  r   r  r  r  r7  r  s     r   LOAD_SUPER_ATTRz)InstructionTranslatorBase.LOAD_SUPER_ATTR  sR    ;..tA>?xx###88a<T"OOD!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 )	Nr9        z Missing CALL_INTRINSIC_1 handlerzCALL_INTRINSIC_1 operand: z,No handler implemented for CALL_INTRINSIC_1 z instruction.r~  )
rO  r  UNARY_POSITIVEr)  rr   r  r  r7   r   r  r  s     r   CALL_INTRINSIC_1z*InstructionTranslatorBase.CALL_INTRINSIC_1  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   r\  rs  s      r   END_SENDz"InstructionTranslatorBase.END_SEND  s!    hhj
		#r   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   r  z!InstructionTranslatorBase.TO_BOOL  s'     $$++ 0
 
 	
 
r   c                   |j                   }|J | j                         }t        |t              sJ | j                         }|dz  r||_        n&|dz  r||_        n|dz  r||_        n|dz  r||_        | j                  |       y )Nr?  r{  r  r   )	r  r  r>  rg   rC  r   rD  rB  r)  )r   r   r@  r5  attrs        r   SET_FUNCTION_ATTRIBUTEz0InstructionTranslatorBase.SET_FUNCTION_ATTRIBUTE  s       XXZ"8999xxz4<BJT\!BNT\ BMT\BK		"r   c                v    | j                  | j                  | j                         |j                               y r   )r)  rl  r  rO  r  s     r   CONVERT_VALUEz'InstructionTranslatorBase.CONVERT_VALUE  s%    		$%%dhhj$++>?r   c                N    | j                  t        j                  d      d       y )Nr   r   )rt  r^   rP  r  s     r   FORMAT_SIMPLEz'InstructionTranslatorBase.FORMAT_SIMPLE   s    +2226:r   c                D    | j                  | j                         d       y r   )rt  r  r  s     r   FORMAT_WITH_SPECz*InstructionTranslatorBase.FORMAT_WITH_SPEC#  s    488:q)r   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_  rj  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 )NrW  	<unknown>r  F)lookup_line)r_  FrameSummaryr  r  r   r   s    r   r  z'InstructionTranslatorBase.frame_summary8  s?    %%DKK<KKDKKK8	
 	
r   c                    t        | j                  dd      }t        j                  d      }|j	                  |      d uS )NrW  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;   rG   r  r:   WEAKREF_ALIVE)r   rM  r  global_names       r   store_global_weakref_by_idz4InstructionTranslatorBase.store_global_weakref_by_idE  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_modeL  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)rM  )r   check_fnpriors      r   strict_translation_modez1InstructionTranslatorBase.strict_translation_modeP  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  rW  r   rN  r   s    r   r  z#InstructionTranslatorBase.speculate^  s|    ''333''!+++##((KK##KK$$q(d66:;	
 	
r   c                   t         |           || _        || _        || _        || _        || _        |	| _        d | _        g | _	        d| _
        d | _        t        d      | _        g | _        g | _        d| _        d | _        d| _        g | _        || _        || _        t-        | j*                        | _        || _        || _        || _        || _        |
| _        |$t:        j<                  rt?        |
||      | _         nd | _         i | _!        i | _"        || _#        d| _$        d| _%        d| _&        d | _'        d | _(        d| _)        d | _*        g | _+        || _,        tZ        j\                  dk\  r>dd	l/m0}m1}m2}m3} |
jh                  ||z  |z  |z  z  r| jk                  tm        d              || _7        d| _8        d gts        |
jt                        z  | _;        tx        j{                  t|        j~                        | _@        t        j{                  t|        j~                        | _B        t        j                  |
j                  |       y )
Nr   r  rV  T)r  rC  rr  Fr8  r   )CO_ASYNC_GENERATORCO_COROUTINECO_GENERATORCO_ITERABLE_COROUTINE)Fr  r  r  distributed_stater  r=  r>  r?  r@  rB  r   rN  r&   rD  r  r  r   r  rE  rG  rK  rN  r   rB  r  r  r  rr  r  r   replay_record_enabledr@   rL  nn_module_stack	num_callsr  r
  r   r  rJ  rM  rO  rP  rQ  rS  r<  r=  resume_executionr  r  r  r  co_flagsr)  r]   rH  rI  r   r  r  r  rn  r   ro  r  r  r  r  	lazycacherW  )r   r  rN  r  r  r  rr  r=  r>  r?  r  r  rH  r  rK  r  rC  rS  r  r  r  r  r  s                         r   r  z"InstructionTranslatorBase.__init__h  sG   * 	.!2 . 0-J* -1),.
#$ #5e#< UW,"&( 0</:4;L;L/M 	 *3*4,8&, 6#?#?!2W<"D "&D BD)+ $)!*/'#'  $"w&  |+.CCFXX 		/$/0().&CG&3L
 C

 ++GMM: 	* ))'--8 	( 	F..	:r   r   r   )r   z	list[str])r5  rY   r   rA  rt  r<  r   r   )r5  rY   r   zSequence[Any]rt  r   r   r   r   )r   rC  r   r   r   )r   r   r   r   r   r+   r   r   )r   r+   )r  r+   r   r   r   r   )r  Optional[VariableTracker]r   r   )r  rA  r   r   r   rY   )r  r   r   rA  )r   r+   r   r^   )r  r   r   rF   )r   r   rS  r   r  r   r   r   )rO  r   r   rY   )r  rY   r   rY   )r  rY   r   r   )r  rY   r   zTypeIs[ExceptionVals])r  r   r   r   r   r+   r  r   r  r   r   rF  )r   r+   r   r  r   r   )r  rY   rk  r   r   rY   )rr  rY   r@  r   r   r   )F)r   r+   r  r   r   r   )r  zOptional[list[Any]]r   r   )r   ztraceback.FrameSummary)rM  r   r  r   r   r   r   zOptional[FakeTensorMode])r  z!Callable[[VariableTracker], bool]r   r   )r   r   )NN)$r  r=   rN  rF  r  r   r  r   r  r   rr  r   r=  r<  r>  r<  r?  r}   r  types.CodeTyper  r   rH  r   r  r   rK  r  r  Optional[DistributedState]rC  zOptional[tuple[types.CellType]]rS  rR  r   r   (  r   r   r   r   rU  r  r`  rd  rg  rj  r*  rw  r}  r  r  r  r  r<  r=  r  propertyr  r  r  r  r)  r  r  r+  r  r  r  r  r  LOAD_CLOSUREr  rG  r  r3  cached_propertyr  r  r  r   r  r  r(  r4  EAGER_IMPORT_NAMEr8  r=  r  r  JUMP_FORWARDJUMP_ABSOLUTEr  rL  not_r  rM  r  JUMP_IF_FALSE_OR_POPJUMP_IF_TRUE_OR_POPrE  rH  rJ  rN  rQ  rS  rU  rW  r_  ra  rh  rK  ru  rr  re  r  r  r  r  r  r  r  r  r  r  rH  r  r  r  r  r  r  r7  r  r  r  r  r  r  r  r  r  r  r  r  rp   r%  r'  BUILD_TUPLE_UNPACK_WITH_CALLr*  r-  BUILD_MAP_UNPACK_WITH_CALLr/  r1  r6  r9  r<  rE  rJ  rS  rU  r  r  r_  rb  rd  r6  rg  rl  rt  rv  r~  r  r  r  r  r  r  DICT_UPDATEr  r  r  r  r  rF  r  r6  posr  negUNARY_NEGATIVEr  invertUNARY_INVERTpowBINARY_POWERmulBINARY_MULTIPLYmatmulBINARY_MATRIX_MULTIPLYfloordivBINARY_FLOOR_DIVIDEtruedivBINARY_TRUE_DIVIDErC  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  rI  r  r  r  rJ  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  rM  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  
contextlibcontextmanagerr
  r  r  r  r  s   @r   r   r   ~  sN	    //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/b 7"%	+R	 ; ;7r22=h ;!+FV2./ L	*J* J J!E( # #,
2:7t $ 6 6>4 LM$X]]E:#HNNE:'t<&x~~t<U
U
(UU(
>R80.
32x't<:$*ObKPD  Q') ()
  Q'0; (0;d  Q'	- (	-GB )I8
.
q%q% $'q% '+	q%
 
q%f	
   Q'= (=8((I
 @L @ 8 $6 J

 "2
 9.1%
N:0  Q'	 (	4

N(3
B"!41P%$1 K@6	6:C	
 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6.A6x@P@P7QRM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"	;@"(!T  Q' ()V M!%+HNNEB ,X^^U C ,X]]E B!-hmmU!C(>#@'	*&&;"$  Q'' ('
$@;* >B	
':	
		

>
 5 5 *9*	* *
8 48,0'u;u; (u; !	u;
 "u; #u; %u; 4u; 5u; (Bu; u; u; u; (u; %u;  6!u;$ 1%u;& *'u;( 
)u; u;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 )r/  c                 "    t         j                  S r   )tls
current_txr   r   r   rr  z InstructionTranslator.current_tx  s    ~~r   c              #     K   t        t        dd       }| t        _        	 d  |t        _        y # |t        _        w xY ww)Nrr  )r  rq  rr  )r   r	  s     r   set_current_txz$InstructionTranslator.set_current_tx  s1     \40	#"CNUCNs   A 0 A =A c                    t               t        j                  d|j                   d|d    d|d           t        |   t        ||	| ||||||||      ||||||i i 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        |      | _'        |r.tF        j,                  jQ                  | 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  rW  r   rX  )local_scoper  r  torch_function_mode_stackrS  r   )r  rN  r  r  r  rC  rr  r=  r>  r?  r  r  rH  r  rK  r  rS  z4Export without one graph - something has gone wrong.dynamismT)is_inputry  )rz  is_derefed_cell_contents)r{  ))r  r   INFOr  r  r  r=   _throw_if_in_functorchr   r  r  rt  r
  r  r=  r   rg  r/   r  r
  r   	frozensetrl   rP  rI   r  r`  r  r  r  r  rd  rH   cell_contents
ValueErrorr   DeletedVariabletrack_cell_existingr}   r?  realize_all)r   rN  r  r  r  r  rC  rx  rr  compiler_fnr
  r  export_constraintsframe_stater  rK  r  rS  rg  ry  r   r  local_dynamismvarr  contents_sourcer  cell_varr  cell_sourcer  s                                 r   r  zInstructionTranslator.__init__  s   ( 	LL((8,}:U9VVWXdeuXvWwx	
 	"$&*C &!%*.+%/= 	 	
B 	##% T[[001 `	43F3F3H `	#,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.  (1'D'D'P'P((($}`	 `	 `	\ " ?#,#<#<#>L?]`	 `	 `	 `	sQ   M.BL9<D=L9: LA+L9ML6	3L95L6	6L99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   r~  )re  _dynamolookup_backendr0  getattr_staticr  r  _C
_functorchpeek_interpreter_stackTransformTypeVmapGradJvpr  r   lowerr7   )r   r  r  ciforbidden_keysr   r  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>  rI   r  r  rF   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=  rq  r>  r   r  r  re  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 )NrV  r  )r   rB  r>  rf   ro   r  rX   )r   rt  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(  ra| j                  sU| j                         sE| j                  s9| j                  s-| j                  s!| j                  st        j                   d      d | _         t#               t$        j&                  d| j(                  j*                   d|j,                   d       t.        j1                  d|j,                         | j                  j3                  | t5        d| j7                         gd	
      |j,                  dk(  rdnd      }t9        |      dk(  sJ |d   j:                  rJ |j,                  dk(  rt=        d      nt=        d|j>                        }| j                  jA                  |g       tB        )Nr@  r   z#because no content in function callztorchdynamo done tracing r   r  z%s triggered compilereturn_valueF)r  rW  r   r  rX  r  )"r  r   rN  rR   rA  r   allow_empty_graphsr  r  rI  r  r  r
  r   r  r   r  r  r   r|  r  r  r   r  rq  r  r<   r  r   r  r&   rO  r  r$  )r   r   r  return_insts       r   _returnzInstructionTranslator._return  s   //1''333+++''0043C3CC	

 ))'')Q.22==?KKNN--33-- EFF#' LL'(;(;'<Bt{{m1M	
 			($++6$(KK$@$@%!3!3!5 6E !KK>9qq %A %
! ,-222,Q/@@@@ {{n, ~.#N4;;G 	 	++[M:r   c                &    | j                  |       y r   r  r  s     r   rW  z"InstructionTranslator.RETURN_VALUE      Tr   c                &    | j                  |       y r   r  r  s     r   rX  z"InstructionTranslator.RETURN_CONST  r  r   )r   r/  r  )$rN  rF  r  r  r  r   r  r   r  r   rC  zOptional[tuple[Any, ...]]rx  r   rr  r   r  r   r
  r   r  r   r  r   r  r   r  r   rK  r  r  r  rS  rR  r   r   r   )r  rK   r   r   r   r  )r   r   r   staticmethodrr  rl  rm  rt  r  r}  r  r  r  r  rW  rX  r  r  s   @r   r/  r/    s     # #]'] ] !	]
 "] #] +] $'] %] ] ] ]  ] ] (]  %!]" 6#]$ *%]& 
']~>	-^r   r/  r  INPLACEr9  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 methodr  symbolic_resultr   rP  c                    t        j                  t        dt        d   i      5  | j                  ||||      }|j	                         cd d d        S # 1 sw Y   y xY w)Nunimplementedr|  )r   r   rO   build_inline_tracerinline_call_)r   rP  funcr   rt  tracers         r   r|  z)InliningInstructionTranslator.inline_call  sQ    ZZ?H]4K"LM 	),,VT4HF&&(	) 	) 	)s   $AAc                z   | 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                   |fv 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.r~  _torchdynamo_disableF_torchdynamo_disable_msgz2Skip inlining `torch.compiler.disable()`d functionzSkip inlining function z= since it was wrapped with `torch.compiler.disable` (reason: r  z(Remove the `torch.compiler.disable` callT)is_inlined_callr   )!produce_trampoline_autograd_applyr5  _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_selfr7   r   r>  ri   r0  r  get_functionr   check_verboseskippedtorch._dynamo.variables.miscr  r  r  r5  r  
SkipResultr   get_filenameget_namer   )r  r  r  r  fn_qualnamer  s         r   check_inlineablez.InliningInstructionTranslator.check_inlineable  s   ==?6D	k d01g6L6L!77
 ((!!#%?C LD--/05d6G6G6I5J K::=aA ? **4F>>V wtT40)<1U B #--?  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                N    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<   t}              r&t         | j                   j                  |      }|S t        |t              rJ t         | 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.r~  Fz;{reason}.
  func = {func}, args = {args}, kwargs = {kwargs}'z' r   )r   r  r   rt  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   rQ   r  r  )headerr  r  cur_instrP  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   )Br>  ri   rg   re   rf   rz  r  r  r   dont_skip_tracingpreviously_inlined_functionsr
  rh   r7   r{  r  r  	bind_args	TypeErrorr1   rq  r   r  r  rX  r  ro  rp  rq  rY   r   r  r  r   r   re  rk  	_internal	log_stateis_artifact_enabledr  Bytecoder<  r=  rD  r  trace_call_logrq  rU   r  r   ry   r  r  r  r  r  r  r/   r  forward__code__r,   &InliningGeneratorInstructionTranslatorr>  r?  )rP  r  r   rt  r  tracing_ctxprevious_result
sub_localsr  r  r   rN  r  moduler  r  r  r  s   `              @@@r   r  z1InliningInstructionTranslator.build_inline_tracerP  s    $*.,	
 	
 
  $}}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&
 ;''44F&  "$(DEEE2''44F c  		#NUUq6T]]_-R0A0A0C/DAdmmoFdFdEef7;<#//+<<!	 V  		s%   ,N 	P$AP5PPP$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        |      rt;        | t<              rz| jB                  rnt;        | t<              sJ g }t;        | j&                  t"              r| j&                  jD                  | j&                  g}t        jF                  tH        | |       y | j&                  S tA        |      rMt;        | t<              sJ | j&                  jK                         J tM        | jN                  tQ                     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 %srj  r  ))rP  r  rl  nullcontextrM  r
  r  r   r   r  rq  r  r8   r  r   r  r  r^   rP  r  r  r>  r,  rI  r  traced_coder   r   ry  r>  r  is_generator_from_ctx_managerr,   generator_exhaustedr  rl  r\  r  ro   generated_itemsrX   )r   rP  r  
strict_ctxr  r  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t335EF,,22: 001D,,]DtL+++D!!$(NOOO++>>@HHH+(("2"4 
 +++{ $$ 	5dV3qcBCIIcN}} 	*%dV2aS1CIIcNc") 	II*D1	 +/*C*CF'sH   J J(J JJ L-,K

L- )L		$L--L0 0M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>  r?  rN  rr  r  r  rH  r  rK  r  rS  )get_globalsr>  r   __dict__r  r  previously_cleaned_instructionsr
  r!   r    r  r  r*   r  r  rH  r  rK  r  rS  funcvarrP  r  r  r  r  r
  )r   rP  r  r=  r>  r?  r  r  r  r  rN  r   r  s               r   r  z&InliningInstructionTranslator.__init__  sm    '')	~.
*d+#,,J mm33::>>tD*JJ4P/5L-DP;;DA==!+-*G%7DG!!WT1--G==,,q0"22,,$66NN! 	 	
$ ))#%55::<)) Hs   F c                .    | j                   j                  S r   )rP  r  r   s    r   r  z'InliningInstructionTranslator.fake_modeR  s    {{$$$r   c                \    t        j                  | j                  j                               S r   )r   r  rP  r  r   s    r   r  z)InliningInstructionTranslator.run_ctx_mgrV  s     ++DKK,E,E,GHHr   c                x    t         j                  r)| j                  j                         syt        |          S yr  )r   r  rP  r  r  )r   r  s    r   r  z:InliningInstructionTranslator.should_compile_partial_graphY  s0    %%;;;;=7799r   c                f    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.r~  )r   r  r  r  r7   )r   r   r  r  r  s       r   r  z3InliningInstructionTranslator.create_call_resume_at`  s?     %%70/1M  	5L		
r   c                F    | j                         | _        d | _        t        r   )r  r  r   r$  r  s     r   rW  z*InliningInstructionTranslator.RETURN_VALUEq  s    #xxz#' r   c                H    | j                  |      | _        d | _        t        r   )r  r  r   r$  r  s     r   rX  z*InliningInstructionTranslator.RETURN_CONSTv  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  r  r  re  rS  r  r  r   r[   rD   r  r   rF   rE   rD  rJ   )	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   )rO  r  r  r  r  r  r  r  r  has_pending_mutation_of_attrr)  	load_attrrY   r  )r   r   r   r   r  r  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  r~  )r  r  r  r  r  r  r>  r   r7   rO  r  r  r  )r   r   r  r   _fglobals_valuer  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   )
rP  r   r  r   r   r   rt  r   r   r   )r  r   r   ztrace_rules.SkipResult)
rP  r   r  rY   r   rA  rt  r   r   r{  r  )rP  r   r  r  r=  r<  r>  r<  r?  r}   r  rd   r   r   r  r  r   r  r  )r   r   r   z#tuple[Any, VariableTracker, Source])r   r   r   r   r   r  r|  r  r  r  r  r  r   r  r  r  r  rW  rX  r  r  r  r  r  s   @r   r{  r{    sQ   *..%%) )
 ; ;z  $ 	
 
' BE,N7*)7* 7* 4	7*
 57* (B7* *7* 
7*r % %I

 $'
 '+	

 

"

%:%:	,%:NM"J Jr   r{  c                  d     e Zd ZU ded<   d
 f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  rA  r  c                N    t        |   |i | g | _        d| _        d| _        y r  )r  r  r  r  r  )r   r   rt  r  s      r   r  z/InliningGeneratorInstructionTranslator.__init__  s-    $)&)!#( -2*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  r  r   r   rk   r   InfiniteGeneratorErrorPT2_ISSUE_TRACKER_URLr)  r^   rP  r   ry  r  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  )rB  r>  ro   r  r]   r  r*  r)  )r   r   rt  ress       r   GET_YIELD_FROM_ITERz:InliningGeneratorInstructionTranslator.GET_YIELD_FROM_ITER  sN    jjn#34HHJ!$'55dSE2FCIIcN 5r   c                0    d| _         t        | 	  |      S r  )r  r  rW  r   r   r  s     r   rW  z3InliningGeneratorInstructionTranslator.RETURN_VALUE      #' w#D))r   c                0    d| _         t        | 	  |      S r  )r  r  rX  r  s     r   rX  z3InliningGeneratorInstructionTranslator.RETURN_CONST  r  r   c                   t        | j                        dk\  sJ | j                         }| j                  d   }t        |t              r|j
                  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  rV  Unreachable sub-generator coder   @Should only be encountered while implementing generator support.r~  r   r   )r   rB  r  r>  r^   r  r7   r[  r   r   r)  r  r\  r   r]  r  rP  r   r   r  rt  exs        r   
YIELD_FROMz1InliningGeneratorInstructionTranslator.YIELD_FROM  s*   4::!###hhjjjn3 01cii6G 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*3A-E%%E*c                   t        | j                        dk\  sJ | j                         }| j                  d   }t        |t        t
        f      s>t        |t              r~t        |j                  t        j                  j                        rPt        |t              r0|j                  $	 |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  rV  r  r  r   r  r~  zSEND with bad typez
TOS type: z(Attempted to SEND with unsupported type r;  )r   rB  r  r>  rn   rf   r   r  r  r  Iteratorr^   r[  r)  r\  r   r]  r<  r=  rP  r  r7   rW   r  s        r   SENDz+InliningGeneratorInstructionTranslator.SEND  s>   4::!###hhjjjnc,.JKLs56399koo&>&>?#/0SYY5F#++D1C IIcN !< b	 ,$WS\N3Fws|nTUV	5 &s'D'DE 
$ '''1
II.55bhh?@IIdOO
$s   +D F A"FF)r   r   rt  r   r   r   r  )r   r   r   r   r  r  r
  rW  rX  r  r  r  r  s   @r   r  r    s-    **3"**$#L*r   r  )r   r   r   ztypes.ModuleType)r   r  )r   r   r   zGenerator[None, None, None])r5  zCallable[..., object]r   zCallable[..., Any])rC  r  r   r   )r   r   rQ  Callable[[object], bool]r)  r   r   r   )r   FN)
rr  r   r   r   ra  r   rs  zOptional[StackSummary]r   r   )rQ  r  r)  r   r   r  )r)  r   r   zYCallable[[Callable[..., None]], Callable[[InstructionTranslatorBase, Instruction], None]])r   
__future__r   r  collections.abcrl  r  r   r  r3  r   r0  ro  r  r   rL  r  r<  	threadingr_  r?  r  r   typingr   r   r   r   r	   r
   r   typing_extensionsr   r   unittest.mockr   re  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/   codegenr0   r1   r2   r3   r4   r5   r6   r7   r8   funcname_cacher9   guardsr:   r;   output_graphr<   r=   	polyfillsr>   replay_recordr?   r@   r  rA   rB   rC   r  rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   utilsrN   rO   rP   rQ   rR   rS   rT   rU   rV   variables.baserW   rX   rY   variables.builderrZ   r[   r\   variables.builtinr]   variables.constantr^   variables.ctx_managerr_   r`   ra   variables.dictsrb   rc   variables.functionsrd   re   rf   rg   rh   ri   rj   variables.iterrk   variables.lazyrl   variables.listsrm   rn   ro   rp   rq   rr   variables.miscrs   rt   ru   rv   rw   rx   variables.nn_modulery   variables.tensorrz   r{   r|   variables.torch_functionr}   r~   variables.user_definedr   r   r   r   r   r   r   torch._subclasses.fake_tensorr   rS  r   	getLoggerr   r  rk  getArtifactLoggerrm  r  r  r  r  rq  r   r*  r   r   containsr   r&  r   r  r   cacher   	dataclassr   r   r   r   r   r  rm  r	  r  r  rs  r   r  r$  r&  r6  rD  rT  rp  rx  r  r  r  r  r  r   r/  r=  _nb_opsr  r  r{  r  )r   r   r   r   s   0000r   <module>rD     s|  0 #      
        	 
     " P P P /    O 1 / < /       ' 	 	 	 ) / 9 0 9 
	 	 	 ;
 
 
 G F R R . 0 
 <   / /   2 W W  3<'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 )!) ) )& $!$ $ $ . . .B	~ 		I 	9 A?
#?&? ? 
	?D 
 )-	N
 N
N
 N
 '	N

 
N
bD&D.2D=DNFFFRJ J2 _ _ _D_);(_);DSY5 Yx w 
 FA	 	!#v-F12JWVABZL3I	
}J$= }J@z-J zAH'nys    T #"T&