
    N j
                    z   S r SSKrSSKrSSKrSSKrSSKrSSKrSSKJrJ	r	J
r
  SSKJr  SSKJrJrJrJrJrJr  SSKJr  SSKrSSKrSSKrSSKrSSKJs  Jr  SSKJr  SSKJ r J!r!  SS	K"J#r#  SS
K$J%r%  SSK&J'r'J(r(J)r)  SSK*J+r+  SSK,J-r-  SSK.J/r/  SSK0J1r1J2r2J3r3J4r4  SSK5J6r6  SSK7J8r8J9r9J:r:  SSK;J<r<  SSK=J>r>J?r?  SSK@JArAJBrB  SSKCJDrDJErEJFrFJGrGJHrH  SSKJIrIJJrJJKrKJLrLJMrMJNrNJOrOJPrP  SSKQJRrRJSrSJTrT  SSKUJVrVJWrWJXrXJYrY  SSKZJ[r[  SSK\J]r]J^r^  SSK_J`r`Jara  SSKbJcrcJdrdJere  SSKfJgrgJhrhJiriJjrj   SSKkrl SS KnJoro  \(       a  SS!KpJqrq  SS"KrJsrs  SS#KJtrt  \" S$5      ru\" S%5      rv\R                  " \x5      ry\zR                  \R                  R                  R                  \R                  R                  GR                   \R                  R                  GR                  \R                  R                  R                  \R                  R                  GR                  \GR                  GR                  \GR                  GR
                  \GR                  GR                  GR                  \GR                  GR                  GR                  \GR                  GR                  GR                  \GR                  GR                  GR                  \GR                  GR                  GR                  \R                  GR                   GR"                  \R                  GR                   GR$                  \R                  GR                   GR&                  \R                  GR                   GR(                  \R                  GR*                  GR,                  \GR.                  GR                  GR                  GR                  \GR0                  GR                  GR                  GR                  \GR2                  GR4                  GR6                  \GR2                  GR4                  GR6                  GR8                  \GR:                  GR<                  GR>                  GR8                  /5      r\zR                  \GRB                  /5      r\GRF                  GRH                  \GRF                  GRJ                  \GR0                  GRL                  \GR0                  GRN                  \GRP                  GRL                  \GRP                  GRN                  /r\GRT                  \GRV                  GRX                  \GR                  GRZ                  \GR                  GR\                  \GRF                  GR^                  \GR0                  GR`                  \GR0                  GR^                  \R                  GR^                  \GRb                  \GRd                  \GRf                  \GRh                  \GRj                  \GRl                  \GRn                  \GRp                  \GR:                  GRr                  GRt                  GRv                  \GRx                  \GR                  GRz                  \R                  GR|                  \GRP                  GR`                  \GRP                  GR^                  /\-   r\R                  GR_                  5       (       aT  \GR                  \R                  GRN                  \R                  GR                  \R                  GR                  /5        \zR                  \5      r\zR                  \5      r\GR                  S&\z\/ \4   \\   4   4S' j5       r\zR                  / S(Q5      r\GR                  GR                  \GR                  GR                  \GR                  GR                  1rS)\TS*\S&S4S+ jrS,\GR                  S&\\R                  GR*                  GR                     4S- jrS)\TS&\\\GR                  \\   4      4S. jr\GR                  S&\\S/\4      4S0 j5       r " S1 S2\T5      r " S3 S4\5      r " S5 S6\GR                  5      r " S7 S8\5      r " S9 S:\5      r " S; S<\5      rg! \m a    Srl GNf = f! \m a    Sro GNf = f)=ak  
This module implements variable tracking for torch functions and operations during Dynamo tracing.

It provides classes to handle different types of torch operations:

TorchInGraphFunctionVariable: Handles torch.* functions that should be captured in the FX graph.
Provides special handling for constant folding, tensor methods, and torch function overrides.
Manages complex cases like out= variants and parameter construction.

TorchCtxManagerClassVariable: Handles torch context managers like torch.no_grad(), autocast, etc.
Provides implementations for entering/exiting these contexts during tracing.

DispatchKeySetVariable: Represents torch.DispatchKeySet for managing dispatch keys and
device-specific operations during tracing.

The module includes special handling for:
- Constant folding of pure functions
- Tensor method calls
- torch.nn.Parameter construction
- __torch_function__ overrides
- Context manager state tracking
- Device and dtype management

This is a core part of Dynamo's tracing system, translating torch operations into
traceable graph nodes while preserving correct semantics and handling edge cases.
    N)CallableIterableSequence)nullcontext)AnyNoReturnOptionalTYPE_CHECKINGTypeVarUnion)TypeIs)DispatchKeySet)CONSTANT_VARIABLE_NONEConstantVariable)StreamVariable)TorchFunctionModeVariable)GuardSourceTracingContext)warning_once)GradientEdge)"is_traceable_wrapper_subclass_type   )configgraph_break_hints	polyfills	variables)	PyCodegen)!can_convert_to_tracable_parameternew_parameter_placeholdertracable_create_parameter) get_registered_device_interfaces)raise_observed_exceptionunimplemented)GuardBuilderinstall_guard)
AttrSourceCallFunctionNoArgsSourceGlobalStateSourceImportSourceSyntheticLocalSource)check_unspec_or_constant_argsguard_if_dynhas_torch_functionhashableis_wrapper_or_member_descriptorproductproxy_args_kwargsunwrap_if_wrapper   )raise_type_error_exctypestrVariableTracker)AutocastModeVariableProfilerContextVariable%ProfilerRecordFunctionContextVariableTorchFunctionDisableVariable)ConstDictVariable)DistributedVariableProcessGroupVariable)bind_args_cachedNestedUserFunctionVariable)ListVariableNamedTupleVariableTupleVariable)can_dispatch_torch_functiondispatch_torch_functionTensorWithTFOverrideVariableTorchFunctionModeStackVariable)_fsdp_param_group)InstructionTranslator)
OpaqueType)TreeSpecVTreturnc                  "   [         R                  R                  S[         R                  R                  S[         R                  R
                  S [         R                  R                  R                  S[         R                  R                  R                  S[         R                  R                  S[         R                  R                  R                  S[         R                  R                  S[         R                   R                  S[         R                   R"                  S[         R                   R$                  S[         R                  R&                  R(                  S[         R*                  R,                  R.                  R0                  S0$ )NFT)torchjitis_scripting
is_tracing_C_get_tracing_statefx_symbolic_traceis_fx_tracingis_fx_symbolic_tracingonnxis_in_onnx_export_dynamoexternal_utilsis_compiling_utilscompileris_dynamo_compilingis_exporting
eval_frame_is_in_optimized_modulennmodules
activation_is_make_fx_tracing     n/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/torch/_dynamo/variables/torch.pytracing_state_functionsrl      s     					e##T  ..  77

$$e$$114!!4##T**D##T  88$ 	##77# rj   )addsubmuldivsqrtvararg_namec                    SSK Jn  [        U [        5      (       ao  U R                  [
        L a\  U R                  (       a  U R                  R                  OSnSU 3nU(       a  USU 3-  n[        SUSSS	/[        R                  QS
9  g[        X5      (       a0  [        U R                  5       H  u  pV[        Xa SU S35        M     gg)zCheck if var contains a GradientEdge from outside the compiled region.

Used by handle_autograd_grad to reject external GradientEdge objects that
cannot be traced through.
r4   BaseListVariableNzGradientEdge in z: z(autograd.grad with external GradientEdgea  torch.autograd.grad() cannot be used with GradientEdge inputs passed from outside the compiled region. The GradientEdge contains a reference to an autograd node that was created before torch.compile started tracing, so Dynamo cannot trace through its computation.z?Move the autograd.grad() call outside the torch.compile region.z>Or use tensor inputs directly instead of GradientEdge objects.gb_typecontextexplanationhints[])listsrv   
isinstancerB   	tuple_clsr   sourcenamer$   r   SUPPORTABLE	enumerateitems_check_for_gradient_edge)rr   rs   rv   source_infory   iitems          rk   r   r      s     (#)**s}}/L),cjjoo$XJ/K=))G>S RP #..	
 
C	*	* +GA$TZq1+=> , 
+rj   tensorc                    SSK Jn  SSKJn  [	        5       n/ nU" U 5      (       a  U" XS9  OUR                  U 5        U H  n[        U[        R                  5      (       d  M$  UR                  b  UR                  UR                  5        UR                  c  M[  UR                  R                  c  Mt  UR                  UR                  R                  5        M     U$ )Nr   )get_plain_tensors)is_traceable_wrapper_subclass)out)torch._subclasses.fake_tensorr   torch.utils._python_dispatchr   setappendr   rP   Tensorgrad_fnrm   _base)r   r   r   grad_fnsplain_tensorsts         rk   _collect_all_grad_fnsr     s    ?J/2uHJLM$V,,&4V$!U\\**99 LL# 77177??#>LL)  Orj   c                    SSK Jn  SSKJn  SSKJn  / n[        X5      (       a  U R                  5       R                  R                  R                  S5      n[        U[        R                  R                  R                  5      (       d   eU R                  (       a  U R                  R                   OSnUR#                  XV45        U$ [        X5      (       a*  UR%                  ['        U R)                  5       5      5        U$ [        X5      (       a/  U R*                   H  nUR%                  ['        U5      5        M     U$ [-        SS[/        U 5      R0                   3S	[/        U 5      R0                   S
3S/S9  U$ )zExtract (fake_tensor, source_name) pairs from a VariableTracker.

Used by handle_autograd_grad to collect tensors from the outputs and inputs
arguments for grad_fn reachability analysis.
r4   )LazyVariableTrackerru   TensorVariableexample_valueNz,autograd.grad with unsupported argument typezgot z3torch.autograd.grad() received an argument of type z@ which is not supported. Expected tensor or sequence of tensors.zHEnsure outputs and inputs arguments are tensors or sequences of tensors.rw   )lazyr   r~   rv   r   r   r   as_proxynodemetagetrP   _subclassesfake_tensor
FakeTensorr   r   r   extend_collect_tensors_with_sourcesrealizer   r$   type__name__)rr   r   rv   r   resultsr   source_namer   s           rk   r   r   /  sB    *'&8:G#&&lln))..22?C+u'8'8'D'D'O'OPPPP),cjjoo12& N% 
C	-	-4S[[]CD  N 
C	*	*IIDNN8>?  N 	B49--./Ed3iFXFXEY ZR R [
	
 Nrj   .c                  R   SSK Jn   SSKJn  [	        U R                  U" 5       R                  5       5      5      n[        R                  [        R                  [        R                  [        R                  [        R                  [        R                  1nUR                  U5        U$ )Nr   )chain)get_overridable_functions)	itertoolsr   torch.overridesr   r   from_iterablevaluesrP   ones	ones_likezeros
zeros_likeemptyfullupdate)r   get_overridable_functions_funcsmores       rk   r   r   V  sp    W##$>$@$G$G$IJKE



%D 
LLLrj   c                      ^  \ rS rSrSr\S\S\SS 4S j5       rS\S\SS4U 4S	 jjr	SS
 jr
S\4S jrS\4S jrSSS\S\4S jrS\4S jrSrU =r$ )BaseTorchVariableii  zHcommon base for all torch.* functions, classes, modules and other thingsvaluer   rN   c                    [         R                  " U5      (       a*  [        UR                  [        R
                  5      5        GO|[         R                  " U5      (       a*  [        UR                  [        R                  5      5        GO7[         R                  " U5      (       a)  [        UR                  [        R                  5      5        O[         R                  " U5      (       dC  [        U[        R                  R                  [        R                  R                  45      (       a)  [        UR                  [        R                   5      5        Ol[#        U5      (       d3  [        U[        R$                  R&                  R(                  5      (       a  O([        UR                  [        R*                  5      5        U " XS9$ Nr   )inspectisclassr&   
make_guardr%   CLASS_MATCHismoduleMODULE_MATCH
isfunctionCLOSURE_MATCH	isbuiltinr   rP   _ops
OpOverloadOpOverloadPacketBUILTIN_MATCHr0   r\   compiled_autogradOpFUNCTION_MATCHclsr   r   s      rk   create_with_source$BaseTorchVariable.create_with_sourcel  s)   ??5!!&++L,D,DEFe$$&++L,E,EFG&&&++L,F,FGHu%%EJJ))5::+F+FG*
 *
 &++L,F,FGH,U33z5==22558
 8
 &++L,G,GHI5((rj   kwargsNc                 2   > [         TU ]  " S0 UD6  Xl        g )Nri   )super__init__r   )selfr   r   	__class__s      rk   r   BaseTorchVariable.__init__  s    "6"
rj   c                 >    U R                   R                   SU R                   R                   3nS[
        R                  " SSU5      -   nUR                  UR                  X0R                   5      5        g ! [         a    S[	        U R                   5       3n Njf = f)N.
torch_obj___z[^a-zA-Z0-9_]+_)	r   
__module__r   	Exceptionidrern   extend_outputsetup_globally_cached)r   codegenr   unique_var_names       rk   reconstructBaseTorchVariable.reconstruct  s    	1jj++,Adjj.A.A-BCD (93!EE))/::F	
  	14::/0D	1s   /A7 7"BBc                     U R                   $ Nr   r   s    rk   r   BaseTorchVariable.as_proxy      zzrj   c                     U R                   $ r   r   r   s    rk   as_python_constant$BaseTorchVariable.as_python_constant  r   rj   txrI   r   c                 l    [        U R                  U5      n[        R                  R	                  U5      $ r   )hasattrr   r   r   create)r   r   r   results       rk   call_obj_hasattr"BaseTorchVariable.call_obj_hasattr  s+     T*))0088rj   c                     U R                   [        ;   a  gU R                   [        R                  R                  L a  [
        R                  (       a  g[        U R                   SS 5      S:H  $ )NTr   math)r   constant_fold_functionsrP   autograd_profiler_enabledr   'constant_fold_autograd_profiler_enabledgetattrr   s    rk   can_constant_fold_through+BaseTorchVariable.can_constant_fold_through  sP    ::00 JJ%..:::>> tzz<6&@@rj   r   )r   r   rN   N)r   r   __qualname____firstlineno____doc__classmethodr   r   r   r   r   r   r   strr   r   boolr  __static_attributes____classcell__r   s   @rk   r   r   i  s    R)s )F )?R ) )(c S T 
# C 9)9149	9A4 A Arj   r   c                   p   ^  \ rS rSrSrS\4S jr\S\S\	4S j5       r
SSS	\\   S
SSS4U 4S jjrSrU =r$ )TorchCtxManagerClassVariablei  zLPoints to a context manager class in torch.* that dynamo has implementationsrN   c                 "    SU R                    S3$ )NzTorchCtxManagerClassVariable()r   r   s    rk   __repr__%TorchCtxManagerClassVariable.__repr__  s    .tzzl!<<rj   r   c                 r    [        U 5      n [        U 5      =(       a    [        U 5      =(       a	    U [        ;   $ r   )r3   callabler/   supported_ctx_manager_classesr   s    rk   is_matching_cls,TorchCtxManagerClassVariable.is_matching_cls  s6     "%( UO  ;::	
rj   r   rI   argsr   dict[str, VariableTracker]r7   c           
        > SSK JnJnJnJnJnJn	Jn
JnJ	nJ
nJnJnJn  U R                  [        R                   L aq  [#        U5      S:X  aP  [%        US   [&        R(                  R*                  5      (       a$  U
R-                  US5      nUR/                  XU5      $ U
R-                  US5      $ U R                  [        R0                  L aq  [#        U5      S:X  aP  [%        US   [&        R(                  R*                  5      (       a$  U
R-                  US5      nUR/                  XU5      $ U
R-                  US5      $ U R                  [        R2                  L a0  [#        U5      S:X  a!  U
R-                  XS   R5                  5       SS9$ U R                  [        R6                  L aV  [#        U5      S::  a  [#        U5      S:X  d   e[#        U5      S:X  a  US   R5                  5       OSnUR-                  UU5      $ U R                  [        R8                  R:                  R<                  [        R8                  R:                  R<                  R>                  4;   aB  [#        U5      S::  a  [#        U5      S:X  d   eU" US   R5                  5       U R@                  S9$ [B        RD                  " U R                  5      (       a_  [G        U R                  [        RH                  5      (       a6  SSK%J&n  U" UUURN                  RQ                  S	U R                  S
0 5      5      $ U R                  [        RR                  RT                  RV                  [        RX                  RR                  RV                  [        RZ                  RR                  RV                  4;   a!  [\        R,                  " U R                  X#5      $ U R                  [        R^                  R`                  [        Rb                  R^                  R`                  4;   a  [d        R,                  " U R                  X#S9$ U R                  [        R^                  Rf                  [        Rb                  R^                  Rf                  4;   a%  [i        [j        SU R                  5        [m        5       $ U R                  [        Rn                  Rp                  L d'  U R                  [        Rn                  Rr                  L aH  U(       d  U(       a   e[t        R,                  " XR                  [        Rn                  Rp                  L S9$ U R                  [        Rv                  Rx                  Rz                  L a#  [#        U5      S:X  d   eUR-                  UU5      $ U R                  [        Rv                  R|                  R~                  L a"  [#        U5      S:X  d   eUR-                  U5      $ U R                  [        Rb                  R                  R                  L a=  [#        U5      S:X  d   eUR-                  UU Vs/ s H  n[        U5      PM     sn5      $ U R                  [        Rb                  R                  R                  L a"  [#        U5      S:X  d   eUR-                  U5      $ U R                  [        Rv                  R|                  R                  L a"  [#        U5      S:X  d   eUR-                  U5      $ U R                  [        Rv                  R|                  R                  L a=  [#        U5      S:X  d   eU	R-                  UU Vs/ s H  n[        U5      PM     sn5      $ U R                  [        Rb                  R                  R                  L a3  [#        U5      S:X  d   eUR-                  XS   R5                  5       5      $ [        b^  U R                  [        R                  R                  L a7  [#        U5      S:X  d   eUR-                  XS   US   R5                  5       5      $ U R                  [        R                  R                  R                  R>                  L a\  [        U R                  UU R@                  UU5      nUS   R5                  5       nUS   R5                  5       nUR-                  UUU5      $ [        TU ]]  XU5      $ s  snf s  snf )Nr4   )!DisabledSavedTensorsHooksVariableDualLevelContextManager&FSDPParamGroupUseTrainingStateVariableFxTracebackAnnotateVariable&GradIncrementNestingCtxManagerVariable)GradInplaceRequiresGradCtxManagerVariableGradModeVariableInferenceModeVariable%JvpIncrementNestingCtxManagerVariableSDPAKernelVariableSetFwdGradEnabledContextManagerr   &VmapIncrementNestingCtxManagerVariabler   FT)initializedr   )wrap_fx_proxy_clscall_functionri   )funcrecord_argsrecord_kwargsz$Profiler function %s will be ignored)only_subclassr   backendsset_priority)P r  r   r!  r"  r#  r$  r%  r&  r'  r(  r)  r   r*  r   rP   no_gradlenr   r   	functionsBaseUserFunctionVariabler   r-  enable_gradset_grad_enabledr   inference_moderV   	tracebackannotate__wrapped__r   r   r   
issubclassStreamtorch._dynamo.variables.builderr,  outputcreate_proxyampautocast_modeautocastcudacpur8   profilerrecord_functionr  r:   profiler   logr9   rT   DisableTorchFunctionSubclassDisableTorchFunctionr;   
_functorchvmapvmap_increment_nestingeager_transformsjvp_increment_nesting
forward_ad_set_fwd_grad_enabledr-   
dual_levelgrad_increment_nestingenable_inplace_requires_gradgraphdisable_saved_tensors_hooksrH   FSDPParamGroupuse_training_statere   	attentionsdpa_kernelr?   r   )r   r   r  r   r  r   r!  r"  r#  r$  r%  r&  r'  r(  r)  r   r*  ctxinf_moder,  xname_to_arg_mapr2  r3  r   s                           rk   r-  *TorchCtxManagerClassVariable.call_function  s   	
 	
 	
 	
  ::&4yA~*Q,,EE# # '--b%8((6::'..r599ZZ5,,,4yA~*Q,,EE# # '--b$7((6::#**2t44ZZ5111c$i1n#**G..0d +   ZZ5///t9>c&kQ&6667:4yA~tAw1134H(//H==ZZHH''HH''33
 
 t9>c&kQ&666.Q**,T[[  __TZZ((Z

ELL-Q-QI$		&&#JJ		 	 ZZII##,,JJNN##IIMM""
 
 (..tzz4HHZZNN**NN##33
 
 9??ZZT  ZZNN""NN##++
 
 DdjjQ*,,JJ%((???zzUXX:::''/66**0U0U"U  ZZ5++00GGGt9>!>9@@  ZZ5++<<RRRt9>!>8??CCZZ5>>44JJJt9>!>299*./$Qa$/  ZZ5>>44???t9>!>*11"55ZZ5++<<SSSt9>!>9@@DDJJ%**;;XXXt9>!><CC*./$Qa$/  ZZ5>>//KKKt9>!>4;;G..0  )

/>>QQQt9>!>9@@GT!W779  ZZ588--99EEE.

O 'z2EEGH*>:MMOL%,,R<HHw$Rv66U 0 0s   'c
;c
ri   )r   r   r  r	  r
  r  r  staticmethodr   r  r  r   r7   r-  r  r  r  s   @rk   r  r    so    V=# = 
s 
t 
 
U7#U7 'U7 -	U7
 
U7 U7rj   r  c                        \ rS rSrSrSrSrSrg)AllowInGraphKindic  defaultnonstrict_traceleaf_functionri   N)r   r   r  r	  DEFAULTNONSTRICT_TRACELEAF_FUNCTIONr  ri   rj   rk   rf  rf  c  s    G'O#Mrj   rf  c            
       ~  ^  \ rS rSrSr S#S\S\4   S\S-  S\SS4U 4S	 jjjrS\	4S
 jr
S\S\4   4S jr\\R                  S\\S\4   \S\4   4   4S j5       5       rSSS\\   SSSS4S jrSSS\\   SSS\4S jrSSS\\   S\\	\4   S\\\4   4S jrSSS\\   S\\	\4   S\4S jrSSS\\   S\\	\4   S\4S jr\  S$SSS\S-  S\S\4S jj5       r\SSS\S\S\4S j5       rSSS\\   S\\	\4   S\4S jrS\4S jrSSS\ \   S\\	\4   S\4S jr!S\4S jr"S\#4S jr$S \%S\4S! jr&S"r'U =r($ )%TorchInGraphFunctionVariableii  z@Points to a torch function/method that should be put in FX graphNr   .kindr   rN   c                    > [         TU ]  " U40 UD6  SSKJnJn  UcL  U" U5      (       a  [
        R                  nO.U" U5      (       a  [
        R                  nO[
        R                  nX l	        g )Nr   )is_leaf_functionis_nonstrict_trace_callable)
r   r   trace_rulesrq  rr  rf  rl  rk  rj  ro  )r   r   ro  r   rq  rr  r   s         rk   r   %TorchInGraphFunctionVariable.__init__l  s[     	)&)O<&&'55,U33'77'//	rj   c                 <    SU R                    SU R                   S3$ )NzTorchInGraphFunctionVariable(z, kind=r  )r   ro  r   s    rk   r  %TorchInGraphFunctionVariable.__repr__  s    .tzzl'$))ANNrj   c                     U R                   $ r   r   r   s    rk   get_function)TorchInGraphFunctionVariable.get_function  r   rj   c                  !  ^G^H^I^J^K^L^M^N^O 0 mMS[         S[        4   S[         [         S[        4   /[         S[        4   4   4UM4S jjn SSKJn  SSKJmGJmHJmIJmJJ	mKJ
mL  SS	KJmNJmO  U " [        5       6 S
SS[        S[        S[        4UG4S jj5       nU " [         6 S
SS[        S[        S[        4S j5       nU " ["        R$                  R&                  R(                  5      S
SS[        S[        S[        4S j5       nU " ["        R*                  R,                  R.                  R0                  5      S
SS[        S[        S[        4S j5       nU " [2        R4                  5      S
SS[        S[        S[        S-  4S j5       n[7        [2        S5      (       a5  U " [2        R8                  5      S
SS[        S[        S[        S-  4S j5       nU " ["        R:                  5      S
SS[<        4S j5       nU " ["        R>                  ["        R$                  R@                  5      S
SS[        S[        4UGUL4S jj5       n	U " ["        RB                  ["        RD                  5      S
SS[        S[        S-  4UG4S jj5       n
U " ["        RF                  5      S
SS[        S[        S-  4UG4S jj5       nU " ["        RH                  5      S
SS[        S[        S[        4S j5       nU " ["        RJ                  RL                  5      S
SS[        S[        S[        4S j5       nU " [N        6 S
SS[        S[        4S j5       nU " ["        RP                  RR                  RT                  RV                  ["        RP                  RR                  RT                  RX                  ["        RP                  RR                  RT                  RZ                  ["        RP                  RR                  RT                  R\                  ["        RP                  RR                  RT                  R^                  5      S
SS[        S[        S[        4S j5       nU " ["        R`                  5      S
SSTG4UGUH4S  jj5       nU " ["        Rb                  5       SiS
SS![        S"[d        S[        4S# jj5       nU " ["        Rf                  5      S
SSTG4UG4S$ jj5       nU " ["        Rh                  Rj                  5      S
SSTG4UG4S% jj5       nU " ["        Rh                  Rl                  5      S
SSTG4UG4S& jj5       nU " ["        Rh                  Rn                  5      S
SSTG4S' j5       nU " ["        R$                  Rp                  ["        R$                  Rr                  ["        R$                  Rt                  5      S
SS[        STG4UG4S( jj5       nU " [v        Ry                  S) [{        5        5       5      6 S
SS*S+STI4UI4S, jj5       nU " ["        R|                  5      S
SS[        STK4UKUO4S- jj5       nU " ["        R~                  R                  5      S
SS.[        S/[        S[        4S0 j5       nU " ["        R                  R                  R                  5      S
SS1[        S2[        STG4UG4S3 jj5       nU " ["        RT                  R                  R                  5      S
SS[        S[        S[        4S4 j5       nU " ["        RP                  R                  5      S
SS[        S[        S[        4S5 j5       nU " ["        R*                  R                  R                  ["        R*                  R                  R                  R                  5       SjS6[        S
SS7[        S-  S[        S-  4S8 jj5       nU " ["        R*                  R                  R                  ["        R*                  R                  R                  R                  5       SjS6[        S
SS7[        S-  S[        S-  4S9 jj5       nU " ["        R                  5      S
SS[        S[        S[        S-  4S: j5       nU " ["        R                  5      S
SS;[        S<[        S[        S[        S-  4
S= j5       n U " ["        R                  5      S>[        S
SS[        S[        S[        S-  4
S? j5       n!U " ["        R                  5      S>[        S
SS[        S[        S[        S-  4
S@ j5       n"U " ["        Rh                  R                  5      S>[        S
SS[        S[        S[        4
SA j5       n#U " ["        R                  5      S
SSB[        SC[        S[        S-  4SD j5       n$U " U5      S
SS[        S[        S[        4UN4SE jj5       n%[        R                  " 5       (       ag  SSFKTJUn&JVn'JWn(JXn)JYn*  SSGKZJ[n+  U " U&U'U(U*U)5      S
SS[        S[        4SH j5       n,U " U+R                  5      S
SS[        S[        S[        4UN4SI jj5       n-U " ["        R                  R                  5       SjSSJ.S
SSK[        S-  S[        SL[        S-  S[        SS4SM jjj5       n.U " ["        RP                  R                  R                  5      S
SS[        S[        SS4SN j5       n/U " ["        R                  R                  R                  R                  5       SjS
SSO[        SP[        [           S[        S-  4UJ4SQ jjj5       n0U " ["        R                  R                  R                  R                  5      S
SSO[        S[        S-  4UJ4SR jj5       n1U " ["        R                  R                  R                  R                  5      S
SSO[        S[        S-  4UJ4SS jj5       n2U " ["        R                  R                  R                  R                  5      S
SSO[        S[        S-  4UJ4ST jj5       n3U " ["        R                  R                  R                  R                  5      S
SSO[        S[        S-  4UJ4SU jj5       n4U " ["        R                  R                  R                  R                  5      S
SSO[        S[        4UJ4SV jj5       n5U " ["        R                  R                  R                  R                  5      S
SSO[        S[        S-  4UJ4SW jj5       n6U " ["        R                  R                  R                  R                  5      S
SSX[        S[        S-  4UJ4SY jj5       n7U " ["        R                  R                  R                  R                  5      S
SSX[        S[        S-  4UJ4SZ jj5       n8U " ["        R                  R                  R                  R                  5      S
SSO[        S[        S-  4UJ4S[ jj5       n9U " ["        Rh                  R                  R                  5      S
SS[        S[        S[        4S\ j5       n:U " ["        Rh                  R                  R                  5      S
SS[        S[        STL4UL4S] jj5       n;U " ["        R                  R                  R                  5      S
SS[        S[        S[        4S^ j5       n<U " ["        R                  5      S
SS[        S[        S[        S-  4UJ4S_ jj5       n=U " ["        Rh                  R                  5      S
SS[        S[        S[        4S` j5       n>U " ["        Rh                  R                  5      S
SS[        S[        S[        4Sa j5       n?U " ["        Rh                  R                  5      S
SS[        S[        S[        4UG4Sb jj5       n@U " ["        Rh                  R                  5      S
SS[        S[        S[        4Sc j5       nAU " ["        R                  R(                  5      S
SS[        S[        S[        4Sd j5       nBU " ["        R                  R                  ["        R                  R                  5      S
SS[        S[        SG[         4Se j5       nCU " ["        GR                  5      S
SS[        S[        S[        4Sf j5       nDU " ["        GR                  5      S
SS[        S[        S[        4UN4Sg jj5       nEU " ["        GR                  GR                  5      SkSh j5       nFTM$ )lzBuild a dict from function -> method to handle it so that we are O(1)
in terms of the number of function with special handling.fns.rN   c                     >^  S[         S[        4   S[         S[        4   4U U4S jjn[        T S   5      (       d   eU$ )Nhandler.rN   c                 >   > T H  nUT;  d   U5       eU TU'   M     U $ r   ri   )r}  fnr{  handlerss     rk   	_registerOTorchInGraphFunctionVariable._get_handlers.<locals>.register.<locals>._register  s0    BX-1r1-#*HRL  rj   r   )r   r   r  )r{  r  r  s   ` rk   register<TorchInGraphFunctionVariable._get_handlers.<locals>.register  sJ    8CH#5 (38:L   CF####rj   r   )
SDPAParamsr4   )r   r%  StreamContextVariableSymNodeVariabler   UserDefinedObjectVariable)wrap_fx_proxyr,  r   rI   r  r   c                   > U(       d  U(       a   eU R                   [        R                  R                  [        R                  R
                  R                  [        R                  R                  [        R                  R                  [        R                  R                  [        R                  R                  R                  4;   a  UR                  5         TR                  " [        5       U R                      5      $ r   )r   rP   r_   r^   r\   r]   r`   ra   rb   rc   rd   mark_inconsistent_side_effectsr   rl   r   r   r  r   r   s       rk   handle_tracing_state_functionsRTorchInGraphFunctionVariable._get_handlers.<locals>.handle_tracing_state_functions  s     F**zz)),,99++22++((@@	 	 113#**+B+DTZZ+PQQrj   c                    U(       a   eU R                   [        R                  R                  L Ga&  [	        U5      S:X  d   eUS   R                  5       (       d   eUS   R                  R                  R                  S   nU R                  U5      n[        U[        R                  R                  5      (       a  U[        R                  R                  [        R                  R                  R                  5      -
  [        R                  R                  [        R                  R                  R                  5      -
  n[         R#                  U5      $ U(       a   e[         R#                  U R                  5       5      $ )Nr4   r   r   )r   rP   rT   _dispatch_keysr6  	is_tensorproxyr   r   r   r   r   r   DispatchKeyPythonPythonTLSSnapshotDispatchKeySetVariabler   )r   r   r  r   r   dkss         rk   !handle_dispatch_key_set_functionsUTorchInGraphFunctionVariable._get_handlers.<locals>.handle_dispatch_key_set_functions  s    :zzUXX4444yA~%~Aw((**** $Q 2 2 7 7 Hjj/ mU->->-I-IJJ((11%((2F2F2M2MNO((11!HH00BB  .44S99x-44TZZ\BBrj   c                 h    [         R                  " U[        R                  R	                  5       5      $ r   )r7   buildrP   	overridesget_default_nowrap_functionsr   r   r  r   s       rk   #handle_get_default_nowrap_functionsWTorchInGraphFunctionVariable._get_handlers.<locals>.handle_get_default_nowrap_functions  s)     #((EOO@@B rj   c                 l    UR                  [        R                  " U[        R                  5      X#5      $ r   )inline_user_function_returnr7   r  r   accumulate_gradr  s       rk   handle_accumulate_grad_KTorchInGraphFunctionVariable._get_handlers.<locals>.handle_accumulate_grad_  s/     11%%b)*C*CDd rj   Nc                     [        X#5      (       d5  UR                  [        R                  " U[        R
                  5      X#5      $ g r   )r,   r  r7   r  r   radiansr  s       rk   handle_radiansBTorchInGraphFunctionVariable._get_handlers.<locals>.handle_radians  s?     1>>55#))"i.?.?@$  ?rj   fmac                     [        U5      S:w  d  U(       a  g [        S U 5       5      (       a2  Uu  pEn[        [        R                  5      nUR                  XXE/0 5      $ g )N   c              3   @   #    U  H  oR                  5       v   M     g 7fr   )r  ).0args     rk   	<genexpr>QTorchInGraphFunctionVariable._get_handlers.<locals>.handle_fma.<locals>.<genexpr>  s     7$3}}$   )r6  allrn  rP   addcmulr-  )r   r   r  r   ra  yz
addcmul_fns           rk   
handle_fma>TorchInGraphFunctionVariable._get_handlers.<locals>.handle_fma  sX     t9>V7$777"GA!!=emm!LJ%33BA	2FF rj   c                 \    [        SSS/ [        R                  Q[        R                  QS9  g )Nz:Encountered torch.is_inference_mode_enabled during tracingr4  z2torch.is_inference_mode_enabled() is not supportedrw   )r$   r   FUNDAMENTALINFERENCE_MODEr   r   s     rk    handle_is_inference_mode_enabledTTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_inference_mode_enabled  s6     TP&22&55	rj   r  c                   > UR                  5       (       dS  U R                  [        R                  R                  L a>  [        UT5      (       a-  [        UR                  S5      (       a  TR                  " S5      $ TR                  " S5      $ )N__torch_function__TF)r  r   rP   r  is_tensor_liker   r   r   )r   r   r  r   r  s      rk   handle_is_tensorDTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_tensor#  sh     }}

eoo<<<s$=>>CII';<<'..t44'..u55rj   inputc                   > UnUR                  5       (       a  UR                  b  U R                  [        R                  L a&  TR
                  " UR                  R                  5      $ U R                  [        R                  L a&  TR
                  " UR                  R                  5      $ [        SU R                   35      eg )Nzcalling )r  dtyper   rP   is_floating_pointr   
is_complexAssertionError)r   r   r  	input_argr   s       rk   handle_is_floating_pointLTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_floating_point0  s     I""$$)D::!8!88+229??3T3TUUZZ5#3#33+229??3M3MNN(8DJJ<)@AArj   c                    > UR                  5       (       a:  UR                  5       (       a%  TR                  " [        UR                  5      5      $ UR                  5       (       a  UR                  US/ 0 5      $ g )Nnumel)r  
valid_sizer   r1   sizecall_method)r   r   r  r   s      rk   handle_numel@TorchInGraphFunctionVariable._get_handlers.<locals>.handle_numelA  sa       U%5%5%7%7'..wuzz/BCC""((Wb"==rj   c                 r    [        U5      S:X  a  US   $ [        SSU SU 3SS/[        R                  QS9  g )	Nr4   r   z torch.compile call with > 1 argsargs=	, kwargs=zNAttempted to call `torch.compile` with > 1 args. Dynamo does not support this.z5Remove the torch.compile call or its additional args.rw   r6  r$   r   r   r  s       rk   handle_torch_compileHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_compileL  sL     4yA~Aw:vYvh7lK&22	rj   c                 r    [        U5      S:X  a  US   $ [        SSU SU 3SS/[        R                  QS9  g )	Nr4   r   z,torch.library.wrap_triton call with > 1 argsr  r  zZAttempted to call `torch.library.wrap_triton` with > 1 args. Dynamo does not support this.zARemove the torch.library.wrap_triton call or its additional args.rw   r  r  s       rk   handle_wrap_tritonFTorchInGraphFunctionVariable._get_handlers.<locals>.handle_wrap_tritona  sL     4yA~AwFvYvh7xW&22	rj   c                 X    UR                  5       (       d   eUR                  US/ 0 5      $ Nr  )r  r  )r   r   r  s      rk   handle_tensor_size_rewritesOTorchInGraphFunctionVariable._get_handlers.<locals>.handle_tensor_size_rewritesv  s.     ??$$$$$$RR88rj   c                 &    U R                  XU5      $ r   )_call_ntupler  s       rk   handle_ntupleATorchInGraphFunctionVariable._get_handlers.<locals>.handle_ntuple}  s     $$Rv66rj   c                 x   > [        TR                  5        TR                  " [        R                  " 5       5      $ r   )r&   _guards_singletonr   rP   is_grad_enabled)r   r   r   r%  s     rk   handle_is_grad_enabledJTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_grad_enabled  s.     *<<=#**5+@+@+BCCrj   mode	warn_onlyc                 X   U(       a8  UR                  5       (       a#  [        SSU SU 3SS/[        R                  QS9  UR                  5       nUR                  R                  S[        R                  R                  U40 5        [        R                  R                  U5        [        $ )NzCAttempted to use torch.use_deterministic_algorithms(warn_only=True)zmode=z, warn_only=Dynamo does not support this.zKRemove param warn_only in function call torch.use_deterministic_algorithms.rw   r-  )
r   r$   r   r   rB  create_noderP   rT   _set_deterministic_algorithmsr   )r   r   r  r  r   s        rk   #handle_use_deterministic_algorithmsWTorchInGraphFunctionVariable._get_handlers.<locals>.handle_use_deterministic_algorithms  s    
 Y99;;a#D6i[A ?e*66	 ++-EII!!!G!G%SU HH2259))rj   c                    > [        [        5       [        R                  5      n[	        U5        TR
                  " [        R                  " 5       5      $ r   )r   r)   r%   DETERMINISTIC_ALGORITHMSr&   r   rP   $are_deterministic_algorithms_enabled)r   r   guardr   s      rk   +handle_are_deterministic_algorithms_enabled_TorchInGraphFunctionVariable._get_handlers.<locals>.handle_are_deterministic_algorithms_enabled  sB     !#55E % #**5+U+U+WXXrj   c                    > [        [        R                  5        TR                  " UR                  R
                  5      $ r   )r&   r;   r  r   symbolic_torch_function_statetorch_function_subclass_enabledr   r   r   s     rk    handle_is_torch_function_enabledTTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_torch_function_enabled  s8     6HHI $**00PP rj   c                    > [        [        R                  5        TR                  " UR                  R
                  (       + 5      $ r   )r&   r;   r  r   r  torch_function_mode_enabledr  s     rk   %handle_is_torch_function_all_disabledYTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_torch_function_all_disabled  s9     6HHI#**44PPP rj   c                     [        [        R                  5        [        R                  " UUR
                  R                  =(       a    UR
                  R                  5       5      $ r   )r&   r;   r  r7   r  r  r   in_torch_function_moder  s     rk   %handle_is_torch_function_mode_enabledYTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_torch_function_mode_enabled  sR     6HHI #((00LL N44KKM rj   c                    > [        U5      S:X  a,  [        US   [        5      (       a  US   R                  U5      OUnTR                  " [        S U 5       5      5      $ )Nr4   r   c              3   8   #    U  H  n[        U5      v   M     g 7fr   )r.   r  ra  s     rk   r  `TorchInGraphFunctionVariable._get_handlers.<locals>.handle_has_torch_function.<locals>.<genexpr>  s     95a&q))5s   )r6  r   rC   unpack_var_sequencer   any)r   r   r  elemsr   s       rk   handle_has_torch_functionMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_has_torch_function  s_     t9>ja-&H&H Q++B/ 
 $**9599 rj   c              3   >   #    U  H  u  pUR                   v   M     g 7fr   )stream)r  r   device_interfaces      rk   r  =TorchInGraphFunctionVariable._get_handlers.<locals>.<genexpr>  s       +M'A !''+Ms   r  r   c                 &   > TR                  X5      $ r   )r   )r   r   r  r  s      rk   handle_device_interface_streamRTorchInGraphFunctionVariable._get_handlers.<locals>.handle_device_interface_stream  s     )//;;rj   c                 4  > [         R                  (       d  [        SS[         R                   3SS/S9  [        (       d  [        SSSS	/[        R
                  QS9  T" TUUR                  R                  " S
[        R                  /[        U0 5      Q76 S S9$ )NzEcall `torch.from_numpy` with `torch._dynamo.config.trace_numpy=False`ztrace_numpy=zcAttempted to call `torch.from_numpy` with config `torch._dynamo.config.trace_numpy` set to `False`.z4Change `torch._dynamo.config.trace_numpy` to `True`.rw   z)`torch.from_numpy` with NumPy unavailabler4  z@Attempted to call `torch.numpy` but NumPy could not be imported.z9Check NumPy version and installation in your environment.r-  )
target_clsr   r  r   )r   trace_numpyr$   npr   
USER_ERRORrB  rC  rP   	as_tensorr2   )r   r   r  r   r,  s      rk   handle_from_numpyETorchInGraphFunctionVariable._get_handlers.<locals>.handle_from_numpy  s     %%c*6+=+=*>?M O
 2G bS*55	 %)ii,,#OO 'tR0
 #	 	rj   the_type	the_valuec                     U$ r   ri   )r   r   r  r   s       rk   handle_jit_annotateGTorchInGraphFunctionVariable._get_handlers.<locals>.handle_jit_annotate  s
     rj   r   extrac                   > U(       a   S5       eUR                  5       (       d   S5       e[        R                  " SUR                  UR                  S9nTR
                  " [        R                  R                  R                  U5      5      $ )Nz%Expect 1 input to cudnn.is_acceptablez2Expect input to cudnn.is_acceptable to be a tensorr   )r  device)	r  rP   r   r  r&  r   r2  cudnnis_acceptable)r   r   r   r$  
tensor_inpr   s        rk   handle_cudnn_is_acceptableNTorchInGraphFunctionVariable._get_handlers.<locals>.handle_cudnn_is_acceptable  sy     EEE9##%% D% av||FMMRJ#**$$22:> rj   c                 J    [         R                  R                  " U/UQ70 UD6$ r   )r   BackwardHookVariabler   r  s       rk   handle_backward_hookHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_backward_hook0  s%     1188MdMfMMrj   c                 .    U R                   " U/UQ70 UD6$ r   )call_nn_parameterr  s       rk   handle_parameterDTorchInGraphFunctionVariable._get_handlers.<locals>.handle_parameter9  s     ))">t>v>>rj   self_dimc                 4    Ub  UR                  USU/0 5      $ g r  r  r4  r   r   r5  s       rk   handle_sym_sizeCTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_sizeB  s%    
 ''FSE2>>rj   c                 4    Ub  UR                  USU/0 5      $ g )Nstrider7  r8  s       rk   handle_sym_strideETorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_strideK  s%     ''HseR@@rj   c                 b   [        U5      S:X  a  SU;   a  [        U5      S:X  a  [        [        R                  5      R	                  U/ USS  Q0 5      n[        [        R
                  5      R	                  XUS   /0 5      n[        [        R                  5      R	                  XS   U/0 5      $ g )Nr  r   r4   r   )r6  rn  rP   rp   r-  ro   rm   )r   r   r  r   r   s        rk   handle_addcdivBTorchInGraphFunctionVariable._get_handlers.<locals>.handle_addcdivS  s     4yA~'V"3Fq8H 6eii@NN$qr(R 6eii@NN12 4EII>LLa&)2  rj   r  
fill_valuec                     UR                  5       (       a?  [        [        R                  5      R	                  X/U5      nUR                  USU/0 5      $ g )Nfill_)r  rn  rP   r   r-  r  )r   r   r  rB  r   empty_results         rk   handle_full?TorchInGraphFunctionVariable._get_handlers.<locals>.handle_fullh  sS     ##%%  <EKKHVV  $//Gj\2NNrj   r   c                     [         R                  (       d  g [        U5      S:X  aU  [        US   [        5      (       d=  U(       d6  UR                  [        R                  " U[        R                  5      UU5      $ g )Nr  r   )
r   enable_dynamo_decompositionsr6  r   rA   r  r7   r  r   foreach_lerp_inplacer   r   r  r   s       rk   "handle_inplace_foreach_lerp_scalarVTorchInGraphFunctionVariable._get_handlers.<locals>.handle_inplace_foreach_lerp_scalarz  sb     664yA~ja,&G&GPV55#))"i.L.LM 
 rj   c                     [         R                  (       d  g [        U5      S:X  aU  US   R                  5       (       a=  U(       d6  UR	                  [
        R                  " U[        R                  5      UU5      $ g )Nr   r   )	r   rI  r6  r  r  r7   r  r   foreach_pow_scalarrK  s       rk   handle_foreach_pow_scalarMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_foreach_pow_scalar  se     66 4yA~$q'"3"3"5"5f55#))"i.J.JK 
 rj   c                 n    UR                  [        R                  " U[        R                  5      UU5      $ r   )r  r7   r  r   !group_tensors_by_device_and_dtyperK  s       rk   (handle_group_tensors_by_device_and_dtype\TorchInGraphFunctionVariable._get_handlers.<locals>.handle_group_tensors_by_device_and_dtype  s3     11%%b)*U*UV rj   	conditionmessagec                     UR                  5       (       a  UR                  5       (       d4  [        U[        R                  5      (       a  UR                  5       (       a  [        $ g r   )is_python_constantr   r   r   r  evaluate_exprr   )r   r   rV  rW  s       rk   handle_assertATorchInGraphFunctionVariable._get_handlers.<locals>.handle_assert  sL     ,,..93O3O3Q3Q9i&?&?@@++----rj   c           
         > T" UUR                   R                  " S[        R                  R                  /[        X#5      Q76 US9$ )Nr-  )r  
param_vars)rB  rC  rP   rT   _SDPAParamsr2   )r   r   r  r   r  s       rk   handle_sdpa_paramsFTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sdpa_params  sK     !ii,,#HH(( 't4
   rj   )_get_group_size_by_name_get_group_tag_rank_not_in_group$_resolve_group_name_by_ranks_and_tagget_process_group_ranks)DTensorc                    [        U5      S:X  a3  [        US   [        5      (       d  US   R                  5       (       d   eO][        U5      S:X  a3  [        US   [        5      (       a  US   R                  5       (       d   eO[        SU SU R                   35      eU Vs/ s H  o3R                  5       PM     nnU R                  " U6 n[        R                  " X5      $ s  snf )Nr4   r   r   zInvalid group value (z) for constant pg function )
r6  r   r>   rY  rA   r  r   r   r7   r  )r   r   r  r  args_as_valueinvocation_results         rk   &handle_constant_processgroup_functionsZTorchInGraphFunctionVariable._get_handlers.<locals>.handle_constant_processgroup_functions  s     t9> #47,@AA755778 Y!^ #47L99 G668898 )/v 6$$(JJ<1  FJ JTc!7!7!9T J$(JJ$>!
 ',,RCC !Ks   $C$c           
      x  >^ ^^ SSK Jn  UR                  S5      nUc  [        U5      S:  a  US   nUc  [        nOF[        U[        R                  5      (       a'  UR                  U[        5      R                  X/0 5      n[        U5      n[        U5      S:  a  XVS'   OUR                  S5      b  XSS'   USS   Vs/ s H  owR                  5       PM     snmUR                  5        VV	s0 s H  u  pUS;  d  M  XR                  5       _M     sn	nmS Vs0 s H  oU;   d  M
  XU   _M     n
n SS[        S[        S -  S	[        S -  S
[        4UUU 4S jjjnST R                  R                   -   Ul        T" UUR"                  R$                  " SU/['        US   /U
5      Q76 S9$ s  snf s  sn	nf s  snf )Nr4   )SourcelessBuilder
placementsr  r   shaper<  ra  rq  r<  rN   c                 8   > TR                   " U /TQ70 TDXS.D6$ )Nrp  r   )ra  rq  r<  ri  kwargs_as_valuer   s      rk   fn_with_prim_typesaTorchInGraphFunctionVariable._get_handlers.<locals>.handle_from_local.<locals>.fn_with_prim_types&  s1      ::)-<DI rj   zprim r-  r   r   r  )NN)builderrn  r   r6  r   r   r   r  r   tupler-  listr   r   r   r   r   rB  rC  r2   )r   r   r  r   rn  placements_vtnew_argsra  kvkwargs_to_be_proxiedrt  ri  rs  r  s   `           @@rk   handle_from_localETorchInGraphFunctionVariable._get_handlers.<locals>.handle_from_local  s    7 !'

< 8 (SY!^$(GM ($:My/R/RSS$5$<$<R$G$U$UOR%M  :x=A%"/QKZZ-9+8<(AI!" NA!5!5!7 N !'# . 33 .A++-- .# +>(*=QfLAayL*= % (
 LP#&:>ADj  /6

8K8K.K"+$))00'* +!!WI0
 
+ !O#(s   ?F,.F1>F1	F7'	F7)layouttensor_listr  c                   SSK Jn  U(       aD  UR                  [        R                  5      (       a   [        SSU 3SS/[        R                  QS9  [        X&5      (       d   [        SS	U 3SS
/[        R                  QS9  g )Nr4   ru   z%Attempted to use strided NestedTensorzlayout=r  zChange layout=torch.jagged.rw   z4Attempted to use `nested_tensor` with non-list inputztensor_list=z'Change `nested_tensor` with list input.)
r~   rv   is_constant_matchrP   stridedr$   r   r   r   r  )r   r   r  r  r  r   rv   s          rk   handle_nested_tensorHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_nested_tensor<  s     0&225==AAC%fX. ?5*66	 k<<R*;-8 ?A*55	 rj   c                     [        U5      [        U5      -   S:X  d(  [        U5      S:X  a,  US   R                  S5      (       a  [        SSU SU 3SS/S	9  g )
Nr4   r   zOAttempted to use `torch.nn.functional.one_hot` with data-dependent output shaper  r  r  zuExplicitly set the `num_classes` param of the function call `torch.nn.functional.one_hot` to something other than -1.rw   )r6  r  r$   r  s       rk   handle_one_hotBTorchInGraphFunctionVariable._get_handlers.<locals>.handle_one_hot]  sf     4y3v;&!+D	Q47#<#<R#@#@m#D66(; ?T	 rj   exprfallbackc                 >  > U(       a  UR                  5       OS n[        UT5      (       a[  [        R                  R	                  [
        R                  R                  R                  R                  UR                  U5      5      $ UR                  5       (       a  U$ g r   )r   r   r   r   r   rP   rV   experimentalsymbolic_shapes	size_hintsym_numrY  )r   r   r  r  fallback_intr  s        rk   handle_size_hintDTorchInGraphFunctionVariable._get_handlers.<locals>.handle_size_hintr  s~     =E8668$L$00 1188HH))99CCl 
 ((**rj   c                 
  > [        UT5      (       aZ  [        R                  R                  [        R
                  R                  R                  R                  UR                  5      5      $ UR                  5       (       a  U$ g r   )r   r   r   r   rP   rV   r  r  guard_size_obliviousr  rY  r   r   r  r  s      rk   handle_guard_size_obliviousOTorchInGraphFunctionVariable._get_handlers.<locals>.handle_guard_size_oblivious  sk     $00 !1188HH))99NN 
 ((**rj   c                 
  > [        UT5      (       aZ  [        R                  R                  [        R
                  R                  R                  R                  UR                  5      5      $ UR                  5       (       a  U$ g r   )r   r   r   r   rP   rV   r  r  guard_or_truer  rY  r  s      rk   handle_guard_or_trueHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_guard_or_true  sh     $00 !1188HH))99GGU  ((**rj   c                 
  > [        UT5      (       aZ  [        R                  R                  [        R
                  R                  R                  R                  UR                  5      5      $ UR                  5       (       a  U$ g r   )r   r   r   r   rP   rV   r  r  guard_or_falser  rY  r  s      rk   handle_guard_or_falseITorchInGraphFunctionVariable._get_handlers.<locals>.handle_guard_or_false  sh     $00 !1188HH))99HHV  ((**rj   c                 
  > [        UT5      (       aZ  [        R                  R                  [        R
                  R                  R                  R                  UR                  5      5      $ UR                  5       (       a  U$ g r   )r   r   r   r   rP   rV   r  r  statically_known_falser  rY  r  s      rk   handle_statically_known_falseQTorchInGraphFunctionVariable._get_handlers.<locals>.handle_statically_known_false  si     $00 1188HH))99PP 
 ((**rj   c                 J  > [        UT5      (       a  UR                  nO5UR                  5       (       a  UR                  5       nO[	        SSU 3S/ S9  [
        R                  R                  [        R                  R                  R                  R                  W5      5      $ )NzGtorch.fx.experimental.symbolic_shapes.guard_scalar branch not supportedzexpr: z6Expected `expr` to be a symbolic variable or constant.rw   )r   r  rY  r   r$   r   r   r   rP   rV   r  r  guard_scalarr   r   r  valr  s       rk   r  @TorchInGraphFunctionVariable._get_handlers.<locals>.guard_scalar  s     $00ll((**--/e$TFO X	 --44%%55BB3G rj   c                 
  > [        UT5      (       aZ  [        R                  R                  [        R
                  R                  R                  R                  UR                  5      5      $ UR                  5       (       a  U$ g r   )r   r   r   r   rP   rV   r  r  statically_known_truer  rY  r  s      rk   handle_statically_known_truePTorchInGraphFunctionVariable._get_handlers.<locals>.handle_statically_known_true  si     $00 1188HH))99OO 
 ((**rj   termsc                    > [        U4S jU 5       5      (       aG  TR                  U[        R                  R                  R
                  R                  " S U 5       6 S S9$ g )Nc              3   <   >#    U  H  n[        UT5      v   M     g 7fr   r   r  ra  r  s     rk   r  UTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_and.<locals>.<genexpr>       A5a:a115   c              3   @   #    U  H  oR                  5       v   M     g 7fr   r   r	  s     rk   r  r         61**,,r  r  )r  r   rP   rV   r  r  sym_andr   r   r  r  s      rk   handle_sym_andBTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_and  sa     A5AAA&--HH))99AA66 ! .   rj   c                    > [        U4S jU 5       5      (       aG  TR                  U[        R                  R                  R
                  R                  " S U 5       6 S S9$ g )Nc              3   <   >#    U  H  n[        UT5      v   M     g 7fr   r  r  s     rk   r  TTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_or.<locals>.<genexpr>  r  r  c              3   @   #    U  H  oR                  5       v   M     g 7fr   r  r	  s     rk   r  r    r  r  r  )r  r   rP   rV   r  r  sym_orr  s      rk   handle_sym_orATorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_or  sa     A5AAA&--HH))99@@66 ! .   rj   c                 .  > [        UT5      (       a  UR                  nO'UR                  5       (       a  UR                  5       nOg [        R
                  R                  [        R                  R                  R                  R                  U5      5      $ r   )r   r  rY  r   r   r   r   rP   rV   r  r  has_static_valuer  s       rk   handle_has_static_valueKTorchInGraphFunctionVariable._get_handlers.<locals>.handle_has_static_value  sr     $00ll((**--/--44%%55FFsK rj   c                 J    SSK Jn  [        U5      R                  U/ UQU5      $ )Nr   )_unsafe_set_version_counter)tensor_version_opr  rn  r-  )r   r   r  r   r  s        rk   !handle_unsafe_set_version_counterUTorchInGraphFunctionVariable._get_handlers.<locals>.handle_unsafe_set_version_counter  s*     H/+mB$01rj   c                 `   > T" [         R                  R                  R                  5       5      $ r   )rP   rT   rO  peek_interpreter_stack)r   r   r  r   r  s       rk   'handle_functorch_peek_interpreter_stack[TorchInGraphFunctionVariable._get_handlers.<locals>.handle_functorch_peek_interpreter_stack  s(     -##::< rj   c                     US   R                   n[        [        R                  R                  R                  U5      5      $ Nr   )r   FuncTorchInterpreterVariablerP   rO  pyfunctorchcoerce_cinterpreter)r   r   r  r   cinterpreters        rk   0handle_functorch_pyfunctorch_coerce_cinterpreterdTorchInGraphFunctionVariable._get_handlers.<locals>.handle_functorch_pyfunctorch_coerce_cinterpreter+  s8      7==L/  ,,@@N rj   c                   >^ S[         S[        4UU4S jjmS nU(       a  US   nOSU;   a  US   nUbX  UR                  5       (       dC  T" U5      (       a6  [        [        R
                  R                  5      R                  U/ UQU5      $ g )Nra  rN   c                    > U R                  5       (       d  [        U T5      (       a  g[        U [        [        45      (       a  [	        U4S jU R
                   5       5      $ g)NTc              3   4   >#    U  H  nT" U5      v   M     g 7fr   ri   )r  r  check_any_unspecs     rk   r  tTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_tensor.<locals>.check_any_unspec.<locals>.<genexpr>D  s     DGq/22Gs   F)r  r   rA   rC   r  r   )ra  r  r  s    rk   r  aTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_tensor.<locals>.check_any_unspec?  sI    ;;==Jq/$B$BL-#@AADAGGDDD !rj   r   data)r7   r  r  rn  rP   _refsr   r-  )r   r   r  r   data_argr  r  s        @rk   handle_torch_tensorGTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_tensor8  s    	!O 	! 	! 	! H76!!&> $ **,,$X.. 4EKK4F4FGUU$  rj   c                     U(       d  U(       a   eUR                   R                  (       d  [        SSSS/[        R                  QS9  [
        R                  " U5        UR                   R                  5       $ )Nz5Attempted to pop from empty torch function mode stackr4  zTCalled `torch._C._pop_torch_function_stack` when torch function mode stack is empty.z0Do not pop from empty torch function mode stack.rw   )r  
mode_stackr$   r   r  rG   register_mutationpop_torch_function_moder  s       rk   handle_pop_torch_functionMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_pop_torch_function^  sj     F**33>>S vJ*55	 +<<R@33KKMMrj   c                     [        U5      S:w  d  U(       a  [        US[        U5       S35        [        R                  " U5        UR                  R                  US   5        [        $ )Nr4   z0push_torch_function takes exactly one argument ( given)r   )r6  r5   rG   r  r  push_torch_function_moder   r  s       rk   handle_push_torch_functionNTorchInGraphFunctionVariable._get_handlers.<locals>.handle_push_torch_functions  s[     4yA~$Fs4ykQXY +<<R@,,EEd1gN))rj   c                    > U(       d  U(       a  [        US5        TR                  " [        UR                  R                  5      5      $ )Nz+len_torch_function_stack takes no arguments)r5   r   r6  r  r  r  s       rk   handle_len_torch_functionMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_len_torch_function  s<     v$R)VW#**B44??@ rj   c                    [        U5      S:w  d  U(       a  [        US[        U5       S35        US   R                  5       nUS:  a#  U[        UR                  R                  5      :  d   eUR                  R                  U   $ )Nr4   z2get_function_stack_at takes exactly one argument (r  r   )r6  r5   r   r  r  )r   r   r  r   inds        rk   handle_get_stack_atGTorchInGraphFunctionVariable._get_handlers.<locals>.handle_get_stack_at  s}     4yA~$HTSZ[ q',,.C!8c"*J*J*U*U&V VVV33>>sCCrj   c           	         [        U5      [        U5      -   S:  d  U(       a(  SU;  a"  [        SSU SU 3S/ [        R                  QS9   U(       a  US   R	                  5       nOU(       a  US   R	                  5       nOS n[
        R                  " U5      nWc=  [        U R                  5      n[        UR                  [        R                  5      5        [        [        S5      WR                   R#                  SSS9S   5      n[$        R&                  " XU5      $ ! [         a-  n[        S	SU SU 3S
/ [        R                  QUS9   S nANS nAff = f)Nr4   r&  z*improper torch.get_device_module argumentsr  r  z<torch.get_device_module accepts 1 optional argument `device`rw   r   z.bad device argument to torch.get_device_moduleAExpected valid string/torch.device argument ('cpu', 'cuda', etc.)rx   ry   rz   r{   from_excrP   r   )maxsplitr  )r6  r$   r   r  r   rP   get_device_moduler   r(   r   r&   r   r%   ID_MATCHr'   r*   r   rsplitr7   r  )	r   r   r  r   r&  moduleer   
new_sources	            rk   handle_get_device_moduleLTorchInGraphFunctionVariable._get_handlers.<locals>.handle_get_device_module  sK    4y3v;&*v(&:PH#D66(; ^*55	#H-@@BF!!W779F!F008 ~1$++>f//0E0EFG#W%&&sQ&7;J #((Z@@+  L#D66(; c9-889s   AD! !
E+#EEc           	         [        U5      [        U5      -   S:  d  U(       a(  SU;  a"  [        SSU SU 3S/ [        R                  QS9   U(       a(  [        R
                  " US   R                  5       5      nO1U(       a(  [        R
                  " US   R                  5       5      nOS nUR                  R                  U5      $ ! [         a-  n[        S	SU SU 3S
/ [        R                  QUS9   S nAg S nAff = f)Nr4   r&  z9unsupported arguments to torch.accelerator.current_streamr  r  zGtorch.accelerator.current_stream accepts one optional argument `device`rw   r   z7bad device argument to torch.accelerator.current_streamr  r  )
r6  r$   r   r  rP   r&  r   symbolic_stream_state
cur_streamr   )r   r   r  r   r&  r  s         rk   handle_current_streamITorchInGraphFunctionVariable._get_handlers.<locals>.handle_current_stream  s     4y3v;&*v(&:PW#D66(; i*55	"\\&*:*M*M*OPF"\\$q'*D*D*FGF!F//::6BB U#D66(; c9-889s   A:C 
C>#C99C>c                     [         R                  " U5        US   R                  5       (       a  [         R                  " U5        [
        $ [         R                  " U5        [
        $ r  )rG   r  is_constant_noneclear_default_device!register_device_context_insertionr   r  s       rk   handle_set_default_deviceMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_set_default_device  sR     +<<R@Aw'')).CCBG *) /PPQST))rj   c           	        > S nS nU(       a  US   nUSS  nOSnUc  SU;   a  UR                  S5      nU(       a  US   nOSU;   a  UR                  S5      nUc-  T" UUR                  R                  SU R                  S0 5      S9$ S nS nUb}  [	        U[
        5      (       a  UR                  5       (       a'  [        S[        U5      S	S
S/[        R                  QS9  UR                  5       nUR                  R                  SU5      nUR                  5       (       a&  U R                  UR                  5       U5        [        $ UR!                  5       n	Uc  U	4n
OX4n
T" UUR                  R                  SU R                  U
0 5      S9$ )Nr   r4   ri   condrW  r-  rv  z)Can't extract message from torch._check()zThe second argument of torch._check() must be a function defined within the torch.compile region that does not reference a non-local variable.zFMake sure the message function is defined in the torch.compile region.zmRemove any closure variables, e.g. remove references to closure variable `x` in `lambda: f'{x} failed check'`rw   _check_message)poprB  rC  r   r   r@   has_closurer$   r  r   r   rx  %register_static_attr_and_return_proxyrY  r   r   r   )r   r   r  r   predicate_vt
message_vt	rest_argsmessage_eagermessage_graph_proxypredicate_proxy
proxy_argsr  s              rk   handle_check@TorchInGraphFunctionVariable._get_handlers.<locals>.handle_check  s     LJ#Aw H		#&(8%zz&1&q\
f$#ZZ	2
#$))00'

	  !M"&%":/IJJ!--//! K #JL
 ei /::	  !+ 7 7 9&(ii&U&U$m'# ..00

<::<mL--*335O #*-/
-C
 ii,,#JJ	 rj   c           
         SSK JnJn  SSKJn  SSKJn  SSKJn  UR                  (       d  [        SSUR                   3S	S
/S9  UR                  R                  (       a  [        SSSSS/S9  UR                  (       a  [        SSS/ SQS9  [        U5      S:  a  [        US   S5        [        U5      S:  a  [        US   S5        [!        5       n	0 n
UR"                  R$                  R'                  5        H  n[)        X5      (       d  M  UR+                  5       R,                  R.                  R1                  S5      n[)        U[2        R4                  5      (       d   e[7        U5      nU	R9                  U5        UR:                  c  M  U H  nUR:                  R<                  X'   M     M     SSKJ n  [        U5      S:  a  [C        US   5      O/ n[        U5      S:  a  [C        US   5      O/ n[!        5       nU HQ  u  nn[)        U[2        R4                  5      (       d  M'  URD                  c  M6  URG                  URD                  5        MS     U" UUS9nUU	-  nU(       aG  U Vs/ s H  nX;   d  M
  X   PM     nnU(       a  SU 3OSn[        SUSSS/[H        RJ                  QS9  UR1                  S 5      nUR1                  S!5      n[)        UU5      =(       a    URL                  S"L =(       d!    [)        UU5      =(       a    URL                  S"L nU(       d  U Vs1 s H   n[O        U5      RP                  S#:w  d  M  UiM"     nnUR"                  RR                  nUU-  nU(       a  [        S$S%[        U5       3S&S'/S9  UR"                  RR                  R9                  U5        U" UUR"                  RT                  " S([2        RV                  RX                  /[[        X#5      Q76 S)9$ s  snf s  snf )*a  
Handle torch.autograd.grad() calls within compiled regions.

NOTE [Tracing autograd.grad in dynamo]

We validate two things:

1. External grad_fns cannot be consumed: The grad_fn on external inputs
   could change at runtime, so we would need to guard on it if we wanted
   to trace through it. For now, we reject this case.
   We compute "consumed" grad_fns (reachable from outputs, excluding
   autograd.grad inputs parameter) and verify no graph input's grad_fn is in this set.

2. Returned tensors cannot have consumed grad_fns: If autograd.grad
   consumes a grad_fn and we return a tensor connected to it, the user
   would get "backward through graph a second time" error. We track
   consumed grad_fns and check at output time. If violated, we retry
   with a graph break at autograd.grad.

Safe vs Unsafe Cases:

Case 1 - Safe (external tensor is autograd.grad input):
    x = torch.randn(4, requires_grad=True)
    external = x * 2  # has external grad_fn

    @torch.compile
    def fn(external_input):
        loss = external_input.sum()
        return torch.autograd.grad(loss, external_input)

    Safe because autograd.grad stops at external_input, never consuming
    its external grad_fn.

Case 2 - Unsafe (external grad_fn in path):
    @torch.compile
    def fn(external_input):
        loss = mod(external_input).sum()
        return torch.autograd.grad(loss, mod.weight)

    Unsafe because autograd.grad must traverse through external_input's
    grad_fn to reach mod.weight. The external grad_fn could change at
    runtime, so we would need to guard on it (like AOTAutograd does).
    For now, we reject this case.

Case 3 - Unsafe (returning tensor with consumed grad_fn):
    @torch.compile
    def fn(x):
        y = x * 2
        grad = torch.autograd.grad(y.sum(), x)
        return y, grad  # y's grad_fn was consumed!

    Unsafe because y's grad_fn was consumed by autograd.grad. Trying to
    backward through y later would error.
r   )r   r   r4   r  )r   r   zPusing `torch.autograd.grad` with `torch._dynamo.config.trace_autograd_ops=False`ztrace_autograd_ops=zmAttempted to call `torch.autograd.grad` with config `torch._dynamo.config.trace_autograd_ops` set to `False`.z;Change `torch._dynamo.config.trace_autograd_ops` to `True`.rw   z0autograd.grad consumed returned tensor's grad_fnr4  ztorch.autograd.grad() consumes grad_fns that are needed by tensors returned from this compiled function. This would cause 'backward through graph a second time' errors.zrIf you don't need to backward through the returned tensor, call .detach() before returning: `return loss.detach()`z^If you need to backward through the returned tensor, use retain_graph=True in autograd.grad().z$autograd.grad with compiled autogradz&compiled_autograd is currently enabledztorch.autograd.grad() inside torch.compile is not supported when compiled autograd is enabled. These two features have conflicting requirements for how the autograd graph is traced.)zLDisable compiled autograd by removing the compiled_autograd context manager.zBOr move the autograd.grad() call outside the torch.compile region.zPOr restructure your code so autograd.grad() and compiled_autograd don't overlap.r   outputsinputsr   )collect_reachable_grad_fns)stop_atzinputs with external grad_fn: z#autograd.grad with external grad_fna	  torch.autograd.grad() cannot trace through the autograd graph because it's output depends on a tensor that was created outside the compiled region and has a grad_fn attached. The autograd graph extends beyond the compiled region boundary, which Dynamo cannot trace.zIf you don't need gradients to flow back to the original tensor outside the compiled region, detach the input: `tensor.detach().requires_grad_(True)`.zEOtherwise, move the autograd.grad() call outside the compiled region.retain_graphcreate_graphTAccumulateGradz+autograd.grad with already consumed grad_fnzdouble consumed grad_fns: ztorch.autograd.grad() is trying to consume grad_fns that were already consumed by a previous autograd.grad() call. This would cause 'backward through graph a second time' errors at runtime.zUse retain_graph=True in the first autograd.grad() call if you need to compute gradients through the same graph multiple times.r-  rv  ).r4  r   r   rw  r  constantr   r   r   trace_autograd_opsr$   speculation_loggraph_break_on_autograd_gradcompiled_autograd_enabledr6  r   r   rB  input_source_to_varr   r   r   r   r   r   rP   r   r   r   r   r   output_graphr$  r   r   rm   r   r   r   r   r   autograd_grad_consumed_grad_fnsrC  r  gradr2   )r   r   r  r   r   r   r  r   r   external_grad_fnsgrad_fn_to_sourcerr   r   tensor_grad_fnsgfr$  outputs_with_sourcesinputs_with_sourcesinputs_grad_fnsr   r   consumed_grad_fnsexternal_in_consumedsourcesry   r&  r'  graph_preservednon_leaf_consumedalready_consumeddouble_consumeds                                  rk   handle_autograd_gradHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_autograd_grad^  s   r 5.2.,,n1&2K2K1LMT V
 !!>>N?
Rx$ !::BDM" 4yA~(a)<4yA~(a(; ADFHyy44;;=c22"%,,."5"5":":">">"OK%k5<<@@@@&;K&HO%,,_=zz-"1B47JJOO-1 #2 > B ;>d)q.-d1g6b ! ;>d)q.-d1g6b   ?BeO0	fell338R#''7 1 !;$o! $57H#H # 32. *%)2  
 IP:7)DUWA#bi_ +66	* "::n5L!::n5L<)9: / &&$. <)9: / &&$.  # 0%/Bx((,<< / " % $&99#L#L "36F"F"! M"<S=Q<R S^
_ 		99@@ARS ii,,#NN'' 't4 EP%s   /	O5<O5(O:	O:)Fr   )r   rI   )r   r   torch.backends.cudar  r4  r   r%  r  r  r   r  rw  r  r,  rl   r7   dispatch_key_set_functionsrP   r  r  r>  opsinductoraccumulate_grad_rg  r   r  r   r  is_inference_mode_enabledr   r  r  r  r  r  compilelibrarywrap_triton!REWRITE_OPS_TO_TENSOR_SIZE_METHODre   rf   utils_single_pair_triple
_quadruple_ntupler  use_deterministic_algorithmsr  r  rT   _is_torch_function_enabled_is_torch_function_all_disabled_is_torch_function_mode_enabledr.   has_torch_function_variadichas_torch_function_unarydictfromkeysr"   
from_numpyrQ   r=  rL   r2  r'  r(  hooksBackwardHook	Parameteratensym_sizeint
sym_strideaddcdivr   _foreach_lerp__foreach_pow"_group_tensors_by_device_and_dtype_assertr=   is_available"torch.distributed.distributed_c10drb  rc  rd  re  rf  torch.distributed.tensorrg  
from_localnestednested_tensor
functionalone_hotrV   r  r  r  r	   r  r  r  r  r  r  r  r  r  	_autogradr  rO  r  r  r  r  r   _pop_torch_function_stackr   _push_on_torch_function_stack_len_torch_function_stack_get_function_stack_atr  acceleratorcurrent_streamrG  r   set_default_device_checkr  r1  )Pr  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.  r2  r9  r=  r@  rF  rL  rP  rT  r[  r`  rb  rc  rd  re  rf  rg  rk  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,  sP                                                                          @@@@@@@@@rk   _get_handlers*TorchInGraphFunctionVariable._get_handlers  s   
 
	38$
	xS)*HS#X,>>?
	 	3	
 	
 	>	*,	-	R'	R #	R &		R
 	R 
.	R* 
-	.	C'	C #	C &		C
 	C 
/	C> 
%//>>JJ	K	'	 #	 &		
 	 
L	 
%))$$55==	>	'	 #	 &		
 	 
?	 
$,,	
	'
	 #
	 &	
	
 t#
	 
 
	 4dhh+ ' *	
 !4'  " 
%11	2	-		 
3	 
%//5??#A#A	B
	6-
	64C
	6
	6 
C
	6 
##

	-	69	t#		

	 
%++		-	69	t#	 
	 
%--	 	'	 #	 &		
 	 
!	( 
%--++	,	'	 #	 &		
 	 
-	( 
4	5	9-	96E	9	9 
6	9 
HH""**HH""((HH""**HH""--HH""**

	7'	7 #	7 &		7
 	7

	7 
%''	(	D-	D	D 
)	D 
%44	5LQ	*-	*58	*EI	*	* 
6	*, 
%<<	=	Y-	Y	Y 
>	Y 
%((55	6	-		 
7	 
%((::	;	-		 
<	 
%((::	;	-		 
<	 
OO..OO77OO44



	-
	6E
	
	



	 
]] +K+M 

	<-	<7G	<"	<

	<
 
%""	#"	-"	6E"	"	 
$"	H 
%))$$	%	-	9<	IJ		 
&	
 
%..&&44	5	-	7:	DG		 
6	" 
%++##00	1	N'	N #	N &		N
 	N 
2	N 
%(($$	%	?'	? #	? &		?
 	? 
&	? 
%))..))599>>+B+B+F+F	GMQ		3	@Cd
	t#	 
H	 
%))..++UYY^^-F-F-J-J	KMQ		3	@Cd
	t#	 
L	 
%--	 	'	 #	 &		
 t#	 
!	( 
%**		'	 "	 (		
 &	 t#	 
	" 
%&&	'		'	 #	 &		
 t#	 
(	" 
%$$	%		'	 #	 &		
 t#	 
&	& 
%((==	>
	
	'
	 #
	 &	
	
 
	 
?
	 
%--	 	'	 '	 %		
 t#	 
!	 
*		'	 #	 &		
 	 
	  ++--  9'"'4D1D:ID DDB g(();+; '; *	;
 !; *;z 
%,,,,	- 37	
 "&	'	 )4/	 #		
 $J	 &	 	 
.	@ 
%((%%--	.	'	 #	 &		
 	 
/	( 
%((''77AA	B
 37		'	 "	 /		
 t#	 
C	$ 
%((''77LL	M	-	5D	t#	 
N	  
%((''77EE	F	-	5D	t#	 
G	 
%((''77FF	G	-	5D	t#	 
H	 
%((''77NN	O	-	5D	t#	 
P	 
%((''77DD	E	-	5D		 
F	& 
%((''77MM	N	-	5D	t#	 
O	 
%((''77??	@	-	7F	t#	 
A	 
%((''77>>	?	-	7F	t#	 
@	 
%((''77HH	I	-	5D	t#	 
J	 
%(($$@@	A
	1'
	1 #
	1 &	
	1
 
	1 
B
	1 
%((%%<<	=
	'
	 #
	 &	
	
 '
	 
>
	 
%""..BB	C
	'
	 #
	 &	
	
 *
	 
D
	 
%,,	#	'#	 ##	 &	#	
 t##	 
 #	J 
%((44	5	N'	N #	N &		N
 '	N 
6	N( 
%((88	9	*'	* #	* &		*
 	* 
:	*  
%((44	5
	'
	 #
	 &	
	
 
	 
6
	 
%((11	2	D'	D #	D &		D
 '	D 
3	D 
%))55	6,	A',	A #,	A &	,	A
 ,	A 
7,	A\ 
%##22EJJ4M4M	N	'	 #	 &		
 	 
O	B 
%**	+	*'	* #	* &		*
 	* 
,	** 
%,,	S	'S	 #S	 &	S	
 S	 
 S	j 
%..%%	&x	 
'x	t rj   r   rI   r  r  r7   c                 6  ^ SSK JnJm  SSKJn  U R
                  [        R                  :X  a  U R                  XU5      $ U R
                  [        R                  :X  a  U R                  XU5      $ U R                  XU5      (       a  [        XX#5      $ U R                  5       (       a  [        X#5      (       a  U R                  [         ;   aL  U R"                  c   e[%        U R"                  5      n['        UR)                  [*        R,                  5      5         UR.                  " U R1                  5       " U Vs/ s H  owR1                  5       PM     sn0 UR3                  5        VV	s0 s H  u  pXR1                  5       _M     sn	nD65      $ U RE                  5       (       Ga[  U R                  RF                  nU(       Ga!  US   RI                  5       (       Ga  US   nUR"                  b  [K        [L        RN                  RP                  U5      (       a  [S        [L        RN                  RP                  U5      n[K        US5      (       a  [K        XRU                  5       S   5      (       aw  [L        RV                  RX                  [S        XRU                  5       S   5      RZ                  ;   a5  []        SSS	[_        U R                  5       S
3/ [`        Rb                  QSPS9  U Re                  U[?        U5      U5      $ U Rg                  5       Ri                  U R                  5      nU(       a  U" X/UQ70 UD6nU(       a  U$ [k        U4S jU 5       5      n[m        U4S jU 5       5      n[S        U R                  SS5      S:X  a  U R                  RF                  [n        ;   a  U(       a{  U(       at  S[q        U R                  5       S3n[r        Ru                  U5        []        SSU R                   SU SU 3S[q        U R                  5       S3/ [`        Rb                  QS9  U R                  nU(       aY  SU R                  RF                   3n[S        U R                  SS 5      S:X  a%  [K        [L        U5      (       a  [S        [L        U5      nS nS nSU;   a  US   n[w        U[x        [z        45      (       ao  / nUR2                   H]  nURI                  5       (       a2  UR}                  5       R~                  R                  S   R                  nOS nUR                  U5        M_     URI                  5       (       a1  UR}                  5       R~                  R                  S   R                  n[L        R                  [L        R                  [L        R                  [L        R                  [L        R                  [L        R                  [L        R                  [L        R                  [L        R                  [L        R                  [L        R                  [L        R                  [L        R                  [L        R                  [L        R                  [L        R                  [L        R                  [L        R                  [L        R                  [L        R                  [L        R                  [L        R                  [L        R                  [L        R                  1n[        nUU;   aL  UR                  (       a;  UR                  R                  (       a   UR                  R                  R                  nU" 5          U" UUR                  R                  " SU/[        X#5      Q76 S9nS S S 5        WRI                  5       (       aO  SU;   aI  US   R1                  5       (       a1  []        S SU R                   SU SU 3S!S"S#/[`        Rb                  QS9  UGbD  [w        U[>        5      (       Ga  [        UR2                  U5       H  u  nnUc  M  URI                  5       (       d   eUR                  R~                  R                  S   nUUR                  :w  aM  []        S$SU R                   SU SU 3S%U R                   S&U S'UR                   S
3/ [`        Rb                  QS9  [L        R                  R                  U5      (       a  M  []        S(S)U R                   SU SU 3S!/ [`        Rb                  QS9  M     U$ Ub  URI                  5       (       d   eSUR}                  5       R~                  R                  ;   d   eUR}                  5       R~                  R                  S   nUUR                  :w  aM  []        S*SU R                   SU SU 3S%U R                   S&U S'UR                   S
3/ [`        Rb                  QS9  [L        R                  R                  U5      (       d/  []        S+S)U R                   SU SU 3S!/ [`        Rb                  QS9  U$ s  snf s  sn	nf ! [4        [6        [8        4 aF  n
[;        [=        U
5      U[?        [A        UR.                  U
RB                  5      5      S9   S n
A
GNS n
A
ff = f! , (       d  f       GN&= f),Nr4   )r   r  r!  )r  r   	overloadszInplace op on input tensorr4  z6Attempted to trace an inplace view op on input tensor r   z/Ensure you do not modify input tensor in place.rw   c              3   <   >#    U  H  n[        UT5      v   M     g 7fr   r  r  s     rk   r  =TorchInGraphFunctionVariable.call_function.<locals>.<genexpr>  s     &Tt!z!_'E'Etr  c              3   j   >#    U  H(  n[        UT5      =(       d    UR                  5       v   M*     g 7fr   )r   rY  r  s     rk   r  r}    s,      !
NRJq/*Da.B.B.DDds   03r   rP   zCalling z on only torch.SymInt arguments is not yet supported.
To support this behavior, we need to allow const-propping tensors that store symint data.
For now, dynamo will explicitly graph break when it encounters user code with this behavior.
zHAttempted to call torch in-graph function on only torch.SymInt argumentszfn=z, args=r  zAttempted to call zc (that should be put in the FX graph) on only torch.SymInt arguments. Dynamo does not support this._sym_r   r   r   r-  rv  requires_gradzAAttempted to use tensor creation function with requires_grad=Truer  z.Create the tensor outside the compiled region.z Do not set `requires_grad=True`.z0Shape mismatch with out= list of tensor variantszShape mismatch when calling z% with `out=`. Provided `out=` shape: z. Actual shape: z?Attempted to call op with non-contiguous `out=` list of tensorszself.value=z'Shape mismatch with out= tensor variantz6Attempted to call op with non-contiguous `out=` tensor)gr4  r   r  rw  r  ro  rf  rk  "_call_nonstrict_traceable_functionrl  _call_leaf_functiontorch_function_override_enabledrE   r  r,   r   #constant_fold_functions_need_guardsr   r(   r&   r   r%   EQUALS_MATCHr   r   r   OverflowError	TypeError
ValueErrorr#   r   ry  mapr  is_tensor_methodr   r  r   rP   rD  r^  r  r{  Taginplace_viewtagsr$   r6   r   r   call_tensor_methodrx  r   r  r  bin_opsr  rL  warningr   rC   rA   r   r   r   rq  r   _foreach_add_foreach_add__foreach_sub_foreach_sub__foreach_mul_foreach_mul__foreach_div_foreach_div__foreach_clamp_max_foreach_clamp_max__foreach_clamp_min_foreach_clamp_min__foreach_maximum_foreach_maximum__foreach_minimum_foreach_minimum_rd  _foreach_pow__foreach_lerprc  _foreach_addcmul_foreach_addcmul__foreach_addcdiv_foreach_addcdiv_r   	fake_mode	shape_envignore_fresh_unbacked_symbolsrB  rC  r2   zipr  _prims_commonis_contiguousis_contiguous_or_false) r   r   r  r   r   r  r   ra  r|  r}  excr   
tensor_varr  special_handlerr   any_symints_or_symfloatsall_ints_or_floatsmsgfn_torch_sym_opsaved_out_shapesout_kwarg_vtvtrq  ops_consuming_unbacked_scalarsr_  tensor_variableout_tensor_vtsaved_out_shapefake_outr  s                                   @rk   r-  *TorchInGraphFunctionVariable.call_function\  s;	    	8*99(888::2VLL99(666++Bf==//&AA*2TBB))++0M1
 1
 zz@@{{...1$++>f//0I0IJK'..++-:>?$Q..0$?AGP12244P    ""::&&DQ))++!!W
$$0WUYY^^T5R5R 6BK00#Bq(9::!II22"2||~a'89>>? &$@$&*`ahimisisat`uuv(w#!2!>!># Q#	 **2tDz6BB,,.224::>$T???F#&&Tt&T#T   !
NR!
 
 DJJb1W<

##w.("		TZZ C
 KKbdjj\ixH(TZZ(9 :4 4&22
 jj#"4::#6#6"78Ltzz<6&@W|F F e\2  F?!%=L ,(EFF#% &,,B||~~ " 2 2 7 7 H N N $$++E2 - %%'' ))+0055oFLL ! $$%%$$%%""##""##  ""##""##3*
&6 00|| 6 6ll,,JJU+ii,,# 't4O  %%''6)'::<<[djj\ixH;D6 '22		 ' *D1169 &&$72M? '. !(224444,2277<<_MH&(..8 &$V&)$**WTF)F8$T">tzzl K::I9JJZ[c[i[iZjjk!m#!2!>!>#
 !..<<XFF &$e&1$**WTF)TZS[$\(G#!2!>!>#	;7F ; $/L4J4J4L4LLL&,*?*?*A*F*F*K*KKKK'00277<<_M#x~~5 " I"%djj\ix P:4::, G66F5GGWX`XfXfWgghj.::
 **AA(KK " X"-djj\iPVx X$C.::	 G @P "9j9 (Ic"2"9"9388DE f UsB    h+ 9h h+ (h%	h+ =-j	 h+ +j ;jj	
jc           
      *  ^ ^!^"^#^$^% SSK Jm"JnJnJm$Jm%  SSKJm!  SSKJ	n  SSK
Jn  SSKJnJn	  [        R                   " U[        R                   " X5      ["        R                   " X5      45      n
UR%                  X5      R'                  X/0 5      n[)        U[        5      (       a  [+        UR,                  5      S:X  d   eUR,                  u  p[)        U[.        5      (       d   eUR,                   HM  nUR1                  5       nU" U5      (       a  M"  UR1                  5       R2                  n[5        S	S
U S3SS/S9  MO     UR,                   Vs/ s H  oR7                  5       PM     nn UR9                  5       nU RF                  m#S[H        S[H        S[H        4U!U#4S jjnU" U5      u  nnURJ                  RM                  T#RN                   S 3U5      nURJ                  RM                  T#RN                  S!-   W5      n[Q        U5      URR                  l(        [Q        U5      URR                  l(        UU/UQ7nS m S[T        S[V        [X           4U U"U$U%4S" jjnURJ                  R[                  S#UU0 5      n U	" UU5      nT c   e[H        R                   " UT 5      nUR%                  U[>        Rh                  5      R'                  UUU/0 5      nU$ s  snf ! U a  nUR:                  R1                  5       nUR2                  nSS KJs  Jn  URA                  U5      (       a/  [5        SSU SU S3SU S3S/[B        RD                  QUS9   S nAGN[5        SSU SU S3SU S3SSU S3/[B        RD                  QUS9   S nAGNS nAff = f! [\        R^                  R`                  Rb                  [\        R^                  R`                  Rd                  4 a5    [5        S$S%T#RN                   3S&/ [B        RD                  QS9  [g        S'5      ef = f)(Nr   )
flat_applyfunc_to_graphableis_graphable_typeis_valid_outputto_graphable)fake_tensor_tls)tree_flattenr4   )#AsPythonConstantNotImplementedError)rn  r  r   z2Invalid input type for nonstrict_trace-ed functionzEncountered input of type <z>.zFor `nonstrict_trace`-ed functions, only basic types (e.g., torch.Tensor, int, float) or pytree containers of those are allowed as inputs. The provided argument contains an unsupported type.zUse one of the following to register the type with pytree:
* `torch.utils._pytree.register_constant`
* `torch.utils._pytree.register_dataclass`
* `torch.utils._pytree.register_pytree_node`rw   zVInput marked with `pytree.register_constant` constructed in the `torch.compile` regionzInput=z, offending type <zWCalling a `nonstrict_trace`-ed function with an input that contains an object of type <z>, which was marked with `pytree.register_constant`. However, the object was constructed _inside_ the `torch.compile` region. This is not supported.zXConstruct the object _outside_ the `torch.compile` region, or submit an issue to GitHub.r  z>Invalid use of pytree_flatten with nonstrict_trace-ed functionzCalling a `nonstrict_trace`-ed function where one of the inputs has been registered with a `pytree_flatten` that places an object of type <z> into the context.zLModifying the `pytree_flatten` to avoid placing the object into the context.zApply one of the following to <z>:
* `torch.utils._pytree.register_constant`
* `torch.utils._pytree.register_dataclass`
* `torch.utils._pytree.register_pytree_node`r  r   rN   c                  f   > TR                   nSTl          T" U 0 UD6nUTl         U$ ! UTl         f = fNT)allow_non_fake_inputs_override)r  r   old_valresr  r  s       rk   
patched_fnSTorchInGraphFunctionVariable._call_nonstrict_traceable_function.<locals>.patched_fn	  sH     &DDG=AO:I$)&)AH>J BI>s   ' 	0_spec_input_specc                     > T" U SS06nT" U5      u  p#Tc  UmOTU:X  d   SU< ST< 35       eT" U5      (       d   eU$ )Nchecked_outputFzTError: nonstrict-traced functions must return the same output shape every time. got z vs but expected ri   )r  r   flat_outspeccaptured_specr  r  r  s       rk   flat_apply_capture[TorchInGraphFunctionVariable._call_nonstrict_traceable_function.<locals>.flat_apply_capture
  sp    d959C
 *#.NH$ $$, 4488;L]L]_, #8,,,,Orj   r-  z7Unsupported output type for nonstrict_trace-ed functionz
Function: zFor `nonstrict_trace`-ed functions, only basic types (e.g., torch.Tensor, int, list) are allowed as output. The result of this call contains an unsupported type.unreachable)5"torch._higher_order_ops.flat_applyr  r  r  r  r  r   r  torch.utils._pytreer  baser  rw  rn  r  rC   r  r<   r   r-  r   r6  r   rA   python_typer  r$   r   r   r  rL  _pytreeis_constant_classr   r   r   r7   rB  r  r   r   r   r   ry  objectrC  rP   r\   r  UnsupportedTorchRuntimeErrorr  tree_unflatten)&r   r   r  r   r  r  r  r  rn  r  packed_input_vtout_vtflat_args_vtsinput_spec_vtflat_arg_vtarg_type	type_nameproxified_flat_args
input_specr  typpytreer  r   f_specf_spec_proxyinput_spec_proxyall_argsr  r  proxy_list_vtout_spec_vtr  r  r  r  r  r  s&                                   @@@@@@rk   r  ?TorchInGraphFunctionVariable._call_nonstrict_traceable_function	  sX   	
 	
 	B4== (--$$R.0A0G0G0ST
 #))";II!2
 &-00S5F!5KKK'-||$-6666 )..K"..0H$X..'335BB	P9)BG/
G	 /. 7D6I6I
6I{  "6I 	 
'	&99;JP ZZ	"	.=		 	 &j1	6 yyFF{{m5!6
 99JJKK-'

 "&f%)*%5" "2I5HI *.	c 	d6l 	 	$ 		&&/2
	0)"e4M& (((%++B> #))"g.D.DESS,b
 o
 3 %	$$""$C((I00'',,t$]O3Ei[PRS$$-; /ff
 s*66   \$]O3Ei[PRSRR[Q\\oq g9) EG G +66  +%	h MM))MM//	
 	0 Q$R[[M2d 7)556 !//!	0s2   K.K <	N N!A.N	.N		NBPc                   ^^^^ SSK Js  Jm  SSKJn  SSKJm  SSKJn  SSK	J
mJm  S[        S	[        [        S      4UU4S
 jjnU" UTR                  5      " [        R                   " XU45      5      R#                  U5      u  px0 n	UR#                  U5       H  n
U" U
5      (       d  M  U
R$                  c.  ['        SS[)        U
R*                  5      R,                   3SSS/S9  U
R$                  c   eU" U
R*                  U
R$                  5      U	[/        U
R*                  5      '   M     U	(       d9  [        R                   " U[1        U5      5      n[        R                   " X5      nX4$ S[2        S[4        [6        [6        4   S	[2        4UU4S jjn[        R                   " X5      nU" X5      " [        R                   " XU45      U5      nUR#                  U5      $ )z
Extract nn.Module states from arguments for leaf function invocation.

Replaces nn.Module arguments with LeafModuleState objects containing
the module's index (for later retrieval), parameters, and buffers.
r   N)register_user_object)LeafModuleStater4   _make_inlined)NNModuleVariableUnspecializedNNModuleVariablerr   rN   c                     > [        U TT45      $ r   r  )rr   r  r  s    rk   is_module_variableRTorchInGraphFunctionVariable._extract_nn_module_states.<locals>.is_module_variablee
  s     c$46S#TUUrj   z0leaf_function: nn.Module argument without sourcezmodule type: zleaf_function received an nn.Module argument that cannot be traced back to its origin. This typically happens when the module is created dynamically inside the compiled region.zXEnsure the nn.Module is created outside the compiled function and passed as an argument.zCIf the module is a class attribute, access it via self.module_name.rw   r   module_to_indexc                 R   >^ S[         S[         4UU4S jjnTR                  X 5      $ )Nr  rN   c                    > [        U [        R                  R                  5      (       aB  T" T[	        U 5         [        U R                  5       5      [        U R                  5       5      S9$ U $ )N)nn_module_indexnamed_parametersnamed_buffers)r   rP   re   Moduler   rX  r  r  )r  r  r  s    rk   module_to_staterTorchInGraphFunctionVariable._extract_nn_module_states.<locals>.convert_modules_to_states.<locals>.module_to_state
  sZ    c588??33*(73(@)-c.B.B.D)E&*3+<+<+>&? 
 
rj   )r   tree_map)r   r  r  r  r  s    ` rk   convert_modules_to_statesYTorchInGraphFunctionVariable._extract_nn_module_states.<locals>.convert_modules_to_states
  s,    S S   ???;;rj   )r  rL  r  #torch._dynamo.graph_bytecode_inputsr  ,torch._higher_order_ops.invoke_leaf_functionr  higher_order_opsr  	nn_moduler  r  r7   r   r   r  r  r  r   r$   r   r   r   r   rx  r   rX  r`  )r   r   r  r   r  r  r  flat_args_vartree_spec_varr  r  args_var
kwargs_varr	  module_to_index_var
result_varr  r  r  r  s                   @@@@rk   _extract_nn_module_states6TorchInGraphFunctionVariable._extract_nn_module_statesR
  s    	-,LP3N	V 	VEMNO	V 	V
 (5R9L9L'M!!"Vn5(


b
! 	% +- 44R8C!#&&::%! R"/SYY0H0H/I JX
9a zz---1EIIszz2399.% 9, &,,Rt=H(..r:J''	<	<*.sCx.	<	< 	< .33BH"2A!!"Vn57J

 --b11rj   c                 N  ^^^ SS K Js  Jn  SSKJn  SSKJnJm  SSKJ	n  SSK
Jn  U R                  n	U	R                  n
U	R                  nUc  [        S[!        U	SU	5       S	35      eU R#                  TX#5      u  pU" TUR$                  5      " [&        R(                  " TX45      5      R+                  T5      u  pUR+                  T5       Vs/ s H  nUR-                  5       PM     nnUR/                  5       mS
[0        S[2        4   S[0        S[2        4   4UU4S jjnU" U
5      nU" U5      nU" U5      u  nnU" U5      u  nnS[4        S[2        S[2        4U4S jjnU" SU5      nU" SU5      nUU/UQ7nTR6                  R9                  SUU0 5      nU" TU5      $ s  snf )Nr   )r  )invoke_leaf_functionreconstruct_original_argsr4   r!  r  zleaf_function 'r   z' requires a fake implementation. Please provide one using the @<func>.register_fake decorator. See the leaf_function docstring for details.impl.rN   c                 6   >^  S[         S[         4U UU4S jjnU$ )N	flat_argsrN   c                  b   > T" TU 5       u  nnT" U0 UD6sS S S 5        $ ! , (       d  f       g = fr   ri   )r  args_with_moduleskwargs_with_modulesr  r  r  s      rk   wrapped_imploTorchInGraphFunctionVariable._call_leaf_function.<locals>.wrap_impl_for_leaf_module_state.<locals>.wrapped_impl
  s7     /z9E J%'!2J6IJ	 FEEs    
.)r   )r  r   r  r  s   ` rk   wrap_impl_for_leaf_module_stateYTorchInGraphFunctionVariable._call_leaf_function.<locals>.wrap_impl_for_leaf_module_state
  s'    
K 
K 
K 
K  rj   r   r  c                 r   > TR                   R                  X5      n[        U5      UR                  l        U$ r   )rB  r  r   r   )r   r  r  r   s      rk   make_spec_proxyITorchInGraphFunctionVariable._call_leaf_function.<locals>.make_spec_proxy
  s+    IICCDOE"4jEJJOLrj   real_fnfake_fnr-  )r  rL  r  r  r  r  r  r  rw  r  r  r  r   _torchdynamo_leaf_real_fn_torchdynamo_leaf_fake_fnr  r  r  r  r7   r  r  r   r   r   r   r  rB  rC  )r   r   r  r   r  r  r  r  r  decorated_fn	real_impl	fake_implargs_with_stateskwargs_with_statesr  input_spec_varr  flat_arg_proxiesr"  wrapped_real_implwrapped_fake_implr   real_impl_specfake_impl_specr%  real_impl_proxyfake_impl_proxyinvoke_argsresult_proxyr  r  s    `                           @@rk   r  0TorchInGraphFunctionVariable._call_leaf_function
  s    	-,H	

 	+3zz ::	 ::	!',
L"Q!R SJ J  04/M/M0
, )6b&:M:M(N!!"'7&LM)


b
! 	& '4&G&G&K
&KsCLLN&K 	 
 --/ 		 38$	 c3h	  	 " <IF;IF-.?@>-.?@>	# 	S 	S 	
 *)^D))^D 
 

 yy--1;
 R..]
s   F"c                   ^^ U R                   [        R                  R                  R                  R
                  L a  US   R                  5       mO#U R                   R                  S   R                  m[        T[        5      (       d   eU(       a   eS[        S[        4UU4S jjnU R                   [        R                  R                  R                  R
                  L a  [        R                  " U5      $ U" US   5      $ )z1inline behavior of torch.nn.modules.utils._ntupler   r   rN   c                   > U R                  T5      (       a.  [        R                  " [        U R	                  T5      5      5      $ U R                  5       (       ad  [        R                  R                  [        R                  R                  R                  R                  T5      " U R                  5       5      5      $ [        SSU  3SS/S9  g )NzPAttempted to use `torch.nn.modules.utils._ntuple` with unsupported argument typezvalue=r  z:Change use of _ntuple with argument as constant or tensor.rw   )has_unpack_var_sequencer   rC   ry  r  rY  r   r   rP   re   rf   rL  rQ  r   r$   )r   countr   s    rk   r  @TorchInGraphFunctionVariable._call_ntuple.<locals>.handle_ntuple
  s    ,,R00 ..22267  ))++ 1188HH$$**2259%:R:R:TU  n$UG, ?T	rj   )r   rP   re   rf   rL  rQ  r   __closure__cell_contentsr   r`  r7   r   LambdaVariable)r   r   r  r   r  r>  s    `   @rk   r  )TorchInGraphFunctionVariable._call_ntuple
  s     ::))//777G..0EJJ**1-;;E%%%%%z	 	_ 	 	( ::))//777++M:: a))rj   r  r  c           
         UR                   (       a  [        SSSS/[        R                  QS9  [	        U[
        R                  5      (       a   UR                  5       nUb  UR                  5       (       d   [        S
SU 3SS/[        R                  QS9  UR                  (       a  U R                  XU5      $ [        R                  (       a  [        SSSSS/[        R                  QS9  [	        U[        5      (       d  [!        UR"                  5      (       a%  [        S[%        U5      S/ [        R                  QS9  ['        5       (       d  [        SSSS/[        R(                  QS9   [+        UR-                  US5      R                  5       5      nUR-                  US5      R                  5       nUR-                  US5      R                  5       nUR.                  R1                  [2        WWWU45      nUR4                  (       a  UR7                  US/ 0 5      nSSKJn	  U	" UUR.                  R=                  S[>        URA                  5       URA                  5       40 5      UR                  S 9n
U
R                  5       (       d   e[B        RD                  RF                  U
l        S!U
l$        UR.                  RJ                  RM                  U
5        U
$ ! [         a$    [        SSU 3SS/[        R                  QS9   GNf = f! [         a,  n[        SSU 3SS/[        R                  QUS9   S	nAGNeS	nAff = f)"z>A call to torch.nn.Parameter() gets lifted to before the graphz3Attempted to use `torch.nn.Parameter()` with exportr4  r  z.Do not use `torch.nn.Parameter()` with export.rw   z=non-constant `requires_grad` argument to `torch.nn.Parameter`zrequires_grad=z$Change `requires_grad` to be a bool.Nz1`torch.nn.Parameter()` with unsupported data typezdata=z7Called `torch.nn.Parameter()` with non-Tensor argument.zBEnsure the argument to `torch.nn.Parameter()` is a `torch.Tensor`.z?Attempted to use `torch.nn.Parameter()` constructor with DynamozDynamo does not support thiszDTry to construct `torch.nn.Parameter()` outside the compiled region.z@If this is not possible, turn `graph_break_on_nn_param_ctor` offzDAttempted to use torch.nn.Parameter constructor with tensor subclassz:`torch.nn.Parameter`: cannot convert to traceable tracablez+convert_tracable_parameter is set to False.zDCheck usage of context manager: do_not_convert_to_tracable_parameterrq  r  r&  z8`torch.nn.Parameter` with non-constant Tensor attributeszBEnsure the Tensor argument's shape, dtype, and device are correct.r  detachr4   r!  r-  r   F)'exportr$   r   r   r   r   r7   r   NotImplementedErrorr  r  r   _nn_param_via_prefix_insertr   graph_break_on_nn_param_ctorrF   r   
class_typer  r   	DIFFICULTrx  var_getattrrB  synthetic_graph_inputr    r  r  rw  r  rC  r!   r   rP   re   r]  has_grad_fnleaf_var_creation_orderr   )r   r   r  r  rq  r  r&  r  placeholderr  r   s              rk   r1  .TorchInGraphFunctionVariable.call_nn_parameter  s6    99M;D&22	 mY%>%>?? - @ @ B <t~~//KvUX&11	 ;;222]KK..Y:ZV '22		 (
 
 0@@^D	;&22	 122TIZ&00		$**2w7JJLME$$R1DDFE%%b(3FFHF ii55%E6=1
 ##B"b9D*II"")+"6"6"89 %%
 !!!!!HH.. #
 			))008 } ' 	[,]O< ?>*55		R # 
	Rv;X&11 	 	
	s+   K .A)L *LL
M!L<<Mc                   ^ U R                   R                  5       nU R                   R                  c   e[        U R                   R                  5      mTR	                  U4S j5        T" UR
                  5        T" [        R                  " U5      5        TR                  SS5        TR                  U5        U R                   R                  R                  TR                  5       5        UR                  5       R                  nUR                  S;  a)  [!        SSUR                   3S/ ["        R$                  QS9  ['        U5      n[(        R*                  R-                  U R                   R/                  UR                  5       R                  5      US	9n[0        R2                  " XU5      nUR5                  5       n[6        R8                  " 5       R:                  R<                  R?                  U5        U$ )
Nc                  (   > T R                  SS5      $ )Nztorch.nnr]  )load_import_from)cgs   rk   <lambda>JTorchInGraphFunctionVariable._nn_param_via_prefix_insert.<locals>.<lambda>  s    !4!4Z!Mrj   r   F)rP  get_attrzAUnexpected type of data placeholder op for parameter constructionzdata_node.op=z/Data node op should be placeholder or get_attr.rw   )r  ) rB  new_varroot_txr   add_push_nullr   r   r   r-  storepregraph_bytecoder   get_instructionsr   r   opr$   r   rK  r+   rP   re   r]  example_value_from_input_noder7   r  r   r   r   guards_contextdynamo_guardsremove_guards_with_source)	r   r  r  varname	data_noder   r   r   rU  s	           @rk   rH  8TorchInGraphFunctionVariable._nn_param_via_prefix_insert  s   
 ))##% yy  ,,,ryy(()
MN
4;;
9%%m45
E"

		##**2+>+>+@AMMO((	<<::['	~6M&00	 &g.**II33DMMO4H4HI' + 
 !&&r&A! 	++99SS	
 rj   c                 d    US   R                  XR                  5       R                  USS  U5      $ )Nr   r4   )r  rx  r   r  s       rk   r  /TorchInGraphFunctionVariable.call_tensor_method  s2     Aw""2'8'8':'C'CT!"XvVVrj   c                 N   SSK Jn  [        R                  " U R	                  5       5      =(       aV    [        U R	                  5       S5      =(       a5    U R	                  5       R                  [        R                  R                  :H  =(       d    U R	                  5       U" 5       ;   $ )Nr   )get_tensor_method__objclass__)
rs  rj  r   ismethoddescriptorrx  r   rk  rP   rT   
TensorBase)r   rj  s     rk   r  -TorchInGraphFunctionVariable.is_tensor_method  s    3 &&t'8'8':; H))+^<H !!#00EHH4G4GG	8
  $5$77	8rj   c                    U R                  5       [        5       ;   =(       dL    [        U R                  5       [        R                  R
                  [        R                  R                  45      =(       a    [        XU5      $ r   )rx  r   r   rP   r   r   r   rD   r  s       rk   r  <TorchInGraphFunctionVariable.torch_function_override_enabled  sd     #<#>> !!#&&

(C(CD< *"F;	<rj   c                     gr  ri   r   s    rk   is_python_hashable/TorchInGraphFunctionVariable.is_python_hashable  s    rj   c                 ,    [        U R                  5      $ r   )hashr   r   s    rk   get_python_hash,TorchInGraphFunctionVariable.get_python_hash  s    DJJrj   otherc                 p    [        U[        5      (       d  gU R                  5       UR                  5       :H  $ )NF)r   r7   r   )r   rx  s     rk   is_python_equal,TorchInGraphFunctionVariable.is_python_equal  s/    %11&&(E,D,D,FFFrj   )ro  r   r  ))r   r   r  r	  r
  r   r   rf  r   r  r  rx  rd  	functoolscacherX  rx  r   r7   r-  r  rx  r  r  r  r  r  r1  rH  ry  r  r  r   r  rr  r`  rv  r  rz  r  r  r  s   @rk   rn  rn  i  s   J
 )-S! % 	
 
 &O# OhsCx0  __S4c 2HS#X4F FG S  Sj.a#a 'a -	a
 
aF	Q#Q 'Q -	Q
 
QfK2#K2 'K2 S/)*	K2
 
/	0K2ZO/#O/ 'O/ S/)*	O/
 
O/b%*#%* '%* S/)*	%*
 
%*N   "	S#S DjS 	S
 
S Sj )#)+.)?C)	) )VW#W ?#W S/)*	W
 
W8$ 8	<)	<19#	<HLSRUX	<		<D    GV G G Grj   rn  c            
          ^  \ rS rSrSr\S\S\SS 4S j5       r\	S\S\
SS 4S j5       rS	\S\4S
 jrSSS	\S\\   S\\\4   SS4
U 4S jjrSrU =r$ )r  i  zrepresents torch.DispatchKeySetr   r   rN   c                     [        U 40 UD6$ r   )r  )r   r   s     rk   r   DispatchKeySetVariable.create  s    %e6v66rj   r   c                 ^    [        UR                  [        R                  5      5        U " XS9$ r   )r&   r   r%   DISPATCH_KEY_SET_MATCHr   s      rk   r   )DispatchKeySetVariable.create_with_source	  s(     	f''(K(KLM5((rj   r   c                     US:H  $ )Nhasri   )r   r   s     rk   is_constant_fold_method.DispatchKeySetVariable.is_constant_fold_method  s    u}rj   r   rI   r  r7   c                   > U R                  U5      (       a  [        X45      (       a  [        U R                  U5      n[        R
                  R                  U" U Vs/ s H  ofR                  5       PM     sn0 UR                  5        VVs0 s H  u  pxXxR                  5       _M     snnD65      $ US:X  a.  [        R                  " U R                  R                  5       5      $ [        T	U ]1  XX45      $ s  snf s  snnf )NhighestPriorityTypeId)r  r,   r  r   r   r   r   r   r   EnumVariabler  r   r  )
r   r   r   r  r   methodra  r|  r}  r   s
            rk   r  "DispatchKeySetVariable.call_method  s     ''--2O3
 3
 TZZ.F--446:;d**,d;=C\\^L^TQq..00^L  ,,))$***J*J*LMMw"2T:: <Ls   C6C;ri   )r   r   r  r	  r
  rd  r   r   r   r  r   r   r  r  r  ry  r7   rX  r  r  r  r  s   @rk   r  r    s    )7n 7 78P 7 7 )"),2)	!) )C D ;#; ; ?#	;
 S/)*; 
; ;rj   r  c            
       t   ^  \ rS rSrSr\S\S\SS 4S j5       rSSS	\	S
\
\   S\\	\4   SS4
U 4S jjrSrU =r$ )r  i)  z<represents torch._functorch.pyfunctorch.FuncTorchInterpreterr   r   rN   c                 ^    [        UR                  [        R                  5      5        U " XS9$ r   )r&   r   r%   r  r   s      rk   r   /FuncTorchInterpreterVariable.create_with_source,  s(     	f''(=(=>?5((rj   r   rI   r   r  r   r7   c                   > US:X  a.  [         R                  " U R                  R                  5       5      $ US:X  aI  UR	                  [
        R                  " XR                  R                  R                  5      U /U-   U5      $ US;   a8  [         R                  R                  [        U R                  U5      " 5       5      $ US:X  a0  U(       d  U(       a   e[         R                  R                  US 5      $ [        TU ]9  XX45      $ )Nkeyprocess)level
batch_size
randomnesslower)r   r  r   r  r  r7   r  r  __func__r   r   r  0TemporarilyPopInterpreterStackCtxManagerVariabler   r  )r   r   r   r  r   r   s        rk   r  (FuncTorchInterpreterVariable.call_method3  s     5=))$**..*:;;Y11%%b***<*<*E*EF 
 ::--44WTZZ5N5PQQW_F**MMTTD  w"2T::rj   ri   )r   r   r  r	  r
  r  r   r   r   r  ry  r7   rX  r  r  r  r  s   @rk   r  r  )  sy    F))!')	') );#; ; ?#	;
 S/)*; 
; ;rj   r  )r
  enumr|  r   loggingr   r   collections.abcr   r   r   
contextlibr   typingr   r   r	   r
   r   r   typing_extensionsr   torch._CrP   torch._refstorch.fxtorch.nnr  rL  r  r    torch._dynamo.variables.constantr   r   torch._dynamo.variables.streamsr   &torch._dynamo.variables.torch_functionr   torch._guardsr   r   r   torch._loggingr   torch.autograd.graphr   r   r   r4  r   r   r   r   r   r   create_parameter_opr   r    r!   r  r"   r  r#   r$   guardsr%   r&   r   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r  r5   r6   r7   ctx_managerr8   r9   r:   r;   dictsr<   distributedr=   r>   r7  r?   r@   r~   rA   rB   rC   torch_functionrD   rE   rF   rG   numpyr  ModuleNotFoundError#torch.distributed.fsdp._fully_shardrH   torch._dynamo.symbolic_convertrI   torch._library.opaque_objectrJ   rK   rL   rM   	getLoggerr   rL  rX  rY  rI  rK  r  rT  rU  rV  rJ  rT   rM  rN  rO  rP  rQ  rR  rW  rS  rX  rD  rE  rF  	grad_moder9  r;  r5  r:  rY  rZ  rH  rG  rV   r<  r=  r>  re   r]  r^  r  _shape_as_tensorrK  rt  current_device_indexcurrent_acceleratorcurrent_deviceis_initializedxpur  rf  r_   _get_device_index_get_cublas_allow_tf32_is_any_autocast_enabledrg  get_device_propertiesget_autocast_dtypeget_autocast_gpu_dtypeget_default_dtypeis_autocast_cache_enabledis_autocast_cpu_enabledis_autocast_enabledr  r  rm  
_Reductionget_enumpromote_types_get_privateuse1_backend_name_is_checkpoint_validr  r   get_rankget_world_sizer}  r  rl   r  r  _dispatch_tls_local_include_set_dispatch_tls_local_exclude_setrC  r  r   r   r   Noder   ry  rx  r   r   r   r  Enumrf  rn  r  r  ri   rj   rk   <module>r     s  6      	 8 8 " I I $     % % # U : L 7 7 ' - K > >  
 @ 9 0 	 	 	 A @  % B C B B E
 D7, CLCL! $''!!77!!,,''//--%%44))@@))??))FF		((  ,,  //  ((  1188		##,,

$$--####// 	&&22; ! F %)MM% ! 
**	))	JJ	JJ	II	II' # 
MM	LL""	HH##	HH%%	""	JJ$$	JJ	""		  		##	!!				HH""++		HH**	NN''	II##	II-. (/( 0 	!!##"",,&&,,	
 '+mm4W&X #--(?@  hr3w&7$&G!H  . --<
= 
HH	HH,,	HH,,  ?/  ?S  ?T  ?F%,, 3u~~7K7K7P7P3Q 6$	$	%hsm+
,-$N 3xS'9#:  $IA IAXk7#4 k7\$tyy $V&G#4 V&GrL$;. $;N ;#4  ;I^  	B
  s$   'b ,b. b+*b+.b:9b: