
    3jG                       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  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  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K.J/r/J0r0J1r1J2r2  SSK3J4r4  SSK5J6r6J7r7J8r8  SSK9J:r:  SSK;J<r<J=r=J>r>J?r?J@r@  SSKAJBrBJCrC  SSKDJErEJFrFJGrGJHrHJIrI  SSKJJrJJKrKJLrLJMrMJNrNJOrOJPrPJQrQ  SSKRJSrSJTrT  SSKUJVrVJWrWJXrXJYrY  SSKZJ[r[  SSK\J]r]J^r^  SSK_J`r`Jara  SSKbJcrc  SSKdJereJfrfJgrgJhrh  SS KiJjrj   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                  \GRR                  GRT                  /r\GRX                  \GRZ                  GR\                  \GR                  GR^                  \GR                  GR`                  \GRF                  GRb                  \GR0                  GRd                  \GR0                  GRb                  \R                  GRb                  \GRf                  \GRh                  \GRj                  \GRl                  \GRn                  \GRp                  \GRr                  \GRt                  \GR:                  GRv                  GRx                  GRz                  \GR|                  \GR                  GR~                  \R                  GR                  \GRP                  GRd                  \GRP                  GRb                  /\-   r\R                  GRc                  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   \S-  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                  \S-  4      4S/ jrS*S0S'\\GR2                  GR                     4S1 jr\GR                  S'\\S2\4      4S3 j5       r " S4 S5\T5      r " S6 S7\5      r " S8 S9\GR                  5      r " S: S;\5      r " S< S=\5      r " S> S?\5      rg! \m a    Srl GNf = 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TYPE_CHECKINGTypeVarUnion)TypeIs)DispatchKeySet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raise_type_errorunimplemented	UserErrorUserErrorType)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   )typestrVariableTracker)AutocastModeVariableProfilerContextVariable%ProfilerRecordFunctionContextVariableTorchFunctionDisableVariable)DistributedVariable)bind_args_cachedNestedUserFunctionVariable)ListVariableTupleVariable)TorchScriptObjectVariable)can_dispatch_torch_functiondispatch_torch_functionTensorWithTFOverrideVariableTorchFunctionModeStackVariable)UserDefinedTupleVariable)_fsdp_param_group)InstructionTranslator)
OpaqueBase)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     W/home/wildlama/miniconda3/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      (       ax  [	        U R
                  5      [        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.
r6   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   
isinstancerG   typevaluer   sourcenamer$   r   SUPPORTABLE	enumerateitems_check_for_gradient_edge)rr   rs   rv   source_infory   iitems          rk   r   r      s     (#/00T#))_5T),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                 j   SSK Jn  SSKJn  SSKJn  SSKJn  / n[        X5      (       GaO  U R                  5       R                  R                  R                  S5      n[        U[        R                  5      (       d   e[        U[        R                  R                   R"                  5      (       a  OU" U5      (       af  / n[        R                  R                   R%                  UUS9  ['        S	 U 5       5      (       d$   S
U Vs/ s H  n[)        U5      PM     sn 35       eO[+        S[)        U5       35      eU R,                  (       a  U R,                  R.                  OSn	UR1                  Xi45        U$ [        X5      (       a*  UR3                  [5        U R7                  5       5      5        U$ [        X5      (       a/  U R8                   H  n
UR3                  [5        U
5      5        M     U$ [;        SS[)        U 5      R<                   3S[)        U 5      R<                   S3S/S9  U$ s  snf )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.
r   r   r6   LazyVariableTrackerru   TensorVariableexample_valuer   c              3      #    U  HT  n[        U[        R                  5      (       d  M$  [        U[        R                  R                  R
                  5      v   MV     g 7fN)r   rP   r   _subclassesfake_tensor
FakeTensor).0r   s     rk   	<genexpr>0_collect_tensors_with_sources.<locals>.<genexpr>T  sD      Aa. H
1e//;;FFGGs
   #A5Az2Expected all plain tensors to be FakeTensors, got z%Expected FakeTensor or subclass, got Nz,autograd.grad with unsupported argument typegot z3torch.autograd.grad() received an argument of type @ which is not supported. Expected tensor or sequence of tensors.HEnsure outputs and inputs arguments are tensors or sequences of tensors.rw   )r   r   lazyr   r~   rv   r   r   r   as_proxynodemetagetrP   r   r   r   r   r   allr   AssertionErrorr   r   r   extend_collect_tensors_with_sourcesrealizer   r$   __name__)rr   r   r   rv   r   resultsr   plainr   source_namer   s              rk   r   r   7  s	    K)'&57G#&&lln))..22?C+u||4444k5#4#4#@#@#K#KLL*;77 #%E));; <      
 EW\E]W\RSd1gW\E]D^_  !7[8I7JK  *-cjjoo12& N% 
C	-	-4S[[]CD  N 
C	*	*IIDNN8>?  N 	B49--./Ed3iFXFXEY ZR R [
	
 N5 F^s   H0r8   c                 .   SSK Jn  SSKJn  SSKJn  / n[        X5      (       a=  U R                  5       R                  nUR                  S:X  a  UR                  U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      R"                   3S[!        U 5      R"                   S	3S
/S9  U$ )uP  Recursively collect FX placeholder nodes from a VariableTracker.

The returned placeholder nodes carry grapharg.example (real tensor) and
example_value (FakeTensor) metadata — comparing these reveals lost
autograd linkage (e.g., grad_fn dropped during tracing).
See NOTE [Detecting lost autograd linkage in closure-captured tensors].
r6   r   ru   r   placeholderz-_autograd_grad with unsupported argument typer   z._autograd_grad() received an argument of type r   r   rw   )r   r   r~   rv   r   r   r   r   r   opr   r   _collect_placeholder_nodesr   r   r$   r   r   )rr   r   rv   r   resultr   r   s          rk   r   r   v  s     *'&"$F#&&||~""77m#MM$$ M# 
C	-	-0?@  M 
C	*	*IIDMM4T:;  M 	C49--./@cASAS@T UR R [
	
 Mrj   .c                  N   SSK Jn   SSKJn  SSKJn  [        U R                  U" 5       R                  5       5      5      nUR                  U" 5       5        [        R                  [        R                  [        R                  [        R                  1nUR                  U5        U$ )Nr   )chain)get_overridable_functions)_device_constructors)	itertoolsr   torch.overridesr   torch.utils._devicer   r   from_iterablevaluesupdaterP   	ones_like
zeros_like
empty_like	full_like)r   get_overridable_functions_r   funcsmores        rk   r   r     sw    W8##$>$@$G$G$IJKE	LL%'(	%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\4S jrSSS\S\4S jrS\4S jrSrU =r$ )BaseTorchVariablei  zHcommon base for all torch.* functions, classes, modules and other thingsr   r   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_MATCHr2   r\   compiled_autogradOpFUNCTION_MATCHclsr   r   s      rk   create_with_source$BaseTorchVariable.create_with_source  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                   $ r   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   c                     U R                   $ r   r  r  s    rk   get_real_python_backed_value.BaseTorchVariable.get_real_python_backed_value  r  rj   txrI   r   c                 Z    [        U R                  U5      n[        R                  " X5      $ r   )hasattrr   r8   build)r   r  r   r   s       rk   call_obj_hasattr"BaseTorchVariable.call_obj_hasattr  s%     T*$$R00rj   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  r
  strr   r  boolr  __static_attributes____classcell__r   s   @rk   r   r     s    R)s )F )?R ) )(c S T 
# C c 1)1141	1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   )r5   callabler1   supported_ctx_manager_classesr  s    rk   is_matching_cls,TorchCtxManagerClassVariable.is_matching_cls  s6     "%( UO  ;::	
rj   r  rI   argsr   dict[str, VariableTracker]r8   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 )Nr6   )!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 r2  r3  r4  r5  r6  r7  r8  r9  r:  r;  r<  r   r=  r   rP   no_gradlenr   r   	functionsBaseUserFunctionVariablecreater@  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cpur9   profilerrecord_functionr  r;   profiler   logr:   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   r2  r3  r4  r5  r6  r7  r8  r9  r:  r;  r<  r   r=  ctxinf_moder?  xname_to_arg_maprE  rF  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6S 0 0s   'c
;c
ri   )r   r   r  r  r  r  r(  staticmethodr   r   r-  r   r8   r@  r!  r"  r#  s   @rk   r%  r%    so    V=# = 
s 
t 
 
T7#T7 'T7 -	T7
 
T7 T7rj   r%  c                        \ rS rSrSrSrSrSrg)AllowInGraphKindi  defaultnonstrict_traceleaf_functionri   N)r   r   r  r  DEFAULTNONSTRICT_TRACELEAF_FUNCTIONr!  ri   rj   rk   rz  rz    s    G'O#Mrj   rz  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($ )%TorchInGraphFunctionVariablei  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_rulesr  r  rz  r  r  r~  r  )r   r   r  r   r  r  r   s         rk   r   %TorchInGraphFunctionVariable.__init__  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   r  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                  T*  ^U^V^W^X^Y^Z^[^\^]^^^_^` 0 m^S[         S[        4   S[         [         S[        4   /[         S[        4   4   4U^4S jjn SSKJn  SSKJmUJmVJmWJmXJ	mYJ
mZ  SS	KJm_Jm`  U " [        5       6 S
SS[        S[        S[        4S 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UUUZ4S jj5       n	U " ["        RB                  ["        RD                  5      S
SS[        S[        S-  4S j5       n
U " ["        RF                  5      S
SS[        S[        S-  4S 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TU4UV4S  jj5       nU " ["        Rb                  5       SS
SS![        S"[d        S[        4UU4S# jjj5       nU " ["        Rf                  5      S
SS[        4S$ j5       nU " ["        Rh                  5      S
SS[        4S% j5       nU " ["        Rj                  5      S
SS&[        S'[        S[        4UU4S( jj5       nU " ["        Rl                  5      S
SS'[        S[        4UU4S) jj5       nU " ["        Rn                  Rp                  Rr                  5      S
SS[        4S* j5       nU " ["        Rn                  Rp                  Rt                  5      S
SS[        4S+ j5       nU " ["        Rn                  Rp                  Rv                  5      S
SS,[        S[        4UU4S- jj5       nU " ["        Rx                  5      S
SSTU4S. j5       nU " ["        Rn                  Rz                  5      S
SSTU4S/ j5       nU " ["        Rn                  R|                  5      S
SSTU4S0 j5       nU " ["        Rn                  R~                  5      S
SSTU4S1 j5       nU " ["        R$                  R                  ["        R$                  R                  ["        R$                  R                  5      S
SS[        STU4S2 j5       nU " [        R                  S3 [        5        5       5      6 S
SS4S5STW4UW4S6 jj5       nU " ["        R                  5      S
SS[        STY4UYU`4S7 jj5       nU " ["        R                  R                  5      S
SS8[        S9[        S[        4S: j5       n U " ["        R                  R                  R                  5      S
SS;[        S<[        STU4S= j5       n!U " ["        RT                  R                  R                  5      S
SS[        S[        S[        4S> j5       n"U " ["        RP                  R                  5      S
SS[        S[        S[        4S? j5       n#U " ["        R*                  R                  R                  ["        R*                  R                  R                  R                  5       SS@[        S
SSA[        S-  S[        S-  4SB jj5       n$U " ["        R*                  R                  R                  ["        R*                  R                  R                  R                  5       SS@[        S
SSA[        S-  S[        S-  4SC jj5       n%U " ["        R                  5      S
SS[        S[        S[        S-  4SD j5       n&U " ["        R                  5      S
SSE[        SF[        S[        S[        S-  4
SG j5       n'U " ["        R                  5      SH[        S
SS[        S[        S[        S-  4
SI j5       n(U " ["        R                  5      SH[        S
SS[        S[        S[        S-  4
SJ j5       n)U " ["        Rn                  R                  5      SH[        S
SS[        S[        S[        4
SK j5       n*U " ["        R                  5      S
SSL[        SM[        S[        S-  4UU4SN jj5       n+U " U5      S
SS[        S[        S[        4U_4SO jj5       n,[        R                  " 5       (       a<  SSPK\J]n-J^n.J_n/J`n0Jan1Jbn2Jcn3  U " U-U.U/U1U0U2U35      S
SS[        S[        S[        4SQ j5       n4U " ["        R                  R                  5       SSSR.S
SSS[        S-  S[        ST[        S-  S[        SS4SU jjj5       n5U " ["        RP                  R                  R                  5      S
SS[        S[        SS4SV j5       n6U " ["        R                  R                  R                  R                  5      S
SSW[        S[        S-  4UX4SX jj5       n7U " ["        R                  R                  R                  R                  5       SS
SSW[        SY[        S-  S[        S-  4UX4SZ jjj5       n8U " ["        R                  R                  R                  R                  5      S
SSW[        S[        S-  4UX4S[ jj5       n9U " ["        R                  R                  R                  R                  5      S
SSW[        S[        S-  4UX4S\ jj5       n:U " ["        R                  R                  R                  R                  5      S
SSW[        S[        S-  4UX4S] jj5       n;U " ["        R                  R                  R                  R                  5      S
SSW[        S[        S-  4UX4S^ jj5       n<U " ["        R                  R                  R                  R                  5      S
SS_[        S[        S-  4S` j5       n=U " ["        R                  R                  R                  R                  5      S
SSW[        S[        4UX4Sa jj5       n>U " ["        R                  R                  R                  R                  5      S
SSW[        S[        S-  4UX4Sb jj5       n?U " ["        R                  R                  R                  R                  5      S
SSc[        S[        S-  4UX4Sd jj5       n@U " ["        R                  R                  R                  R                  5      S
SSc[        S[        S-  4UX4Se jj5       nAU " ["        R                  R                  R                  R                  5      S
SSW[        S[        S-  4UX4Sf jj5       nBU " ["        Rn                  R                  R                  5      S
SS[        S[        S[        4Sg j5       nCU " ["        Rn                  Rp                  R                  5      S
SS[        S[        STZ4UZ4Sh jj5       nDU " ["        Rp                  R                  R                  5      S
SS[        S[        S[        4Si j5       nEU " ["        R                  5      S
SS[        S[        S[        S-  4UX4Sj jj5       nFU " ["        Rn                  R                  5      S
SS[        S[        S[        4Sk j5       nGU " ["        Rn                  GR                   5      S
SS[        S[        S[        4UU4Sl jj5       nHU " ["        Rn                  GR                  5      S
SS[        S[        S[        4Sm j5       nIU " ["        Rn                  GR                  5      S
SS[        S[        S[        4Sn j5       nJU " ["        GR                  R(                  5      S
SS[        S[        S[        4So j5       nKU " ["        GR                  GR
                  ["        GR                  GR
                  5      S
SS[        S[        SG[        4Sp j5       nL["        GR                  GR                  Sq["        GR                  GR                  Sr["        GR                  GR                  Ss["        GR                  GR                  St0m[U " ["        GR                  GR                  ["        GR                  GR                  ["        GR                  GR                  ["        GR                  GR                  ["        GR                  GR                  5      S
SS[        S[        S[        4UUU[4Su jj5       nMU " ["        GR                  5      S
SS[        S[        S[        4UU4Sv jj5       nNSSwKJm\  U " T\5      S
SS[        S[        S[        4UYU\4Sx jj5       nOU " ["        GR                  5      S
SS[        S[        S[        4UUU_4Sy jj5       nPS
SSG[         [           S[        G["        [        4   Sz[         [        /[        S-  4   S[        4
S{ jm]U " ["        GR                  GR$                  5      S
SS[        S[        S[        4U]4S| jj5       nQU " ["        GR                  GR&                  5      S
SS[        S[        S[        4U]4S} jj5       nRU " ["        GR(                  GR*                  5      SS~ j5       nSU " ["        Rp                  GR,                  GR.                  5      S
SS[        S[        S[        S-  4S j5       nTT^$ )z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   )
SDPAParamsr6   )r   r8  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         U R                   [        R                  R                  L a/  [        R                  " U[        R                  R                  5      $ [        R                  " U[        5       U R                      5      $ r   )r   rP   r_   r^   r\   r]   r`   ra   rb   rc   rd   mark_inconsistent_side_effectsr8   r  _is_exporting_flagrl   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 zzU^^888&,,R1R1RSS"((-D-Ftzz-RSSrj   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      $ )Nr6   r   r   )r   rP   rT   _dispatch_keysrI  	is_tensorproxyr   r   r   r   r   r   DispatchKeyPythonPythonTLSSnapshotDispatchKeySetVariablerL  )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   )r8   r  rP   	overridesget_default_nowrap_functionsr  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_returnr8   r  r   accumulate_gradr  s       rk   handle_accumulate_grad_KTorchInGraphFunctionVariable._get_handlers.<locals>.handle_accumulate_grad_5  s/     11%%b)*C*CDd rj   Nc                     [        X#5      (       d5  UR                  [        R                  " U[        R
                  5      X#5      $ g r   )r.   r  r8   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  )r   args     rk   r   QTorchInGraphFunctionVariable._get_handlers.<locals>.handle_fma.<locals>.<genexpr>Y  s     7$3}}$   )rI  r   r  rP   addcmulr@  )r   r  r/  r   ru  yz
addcmul_fns           rk   
handle_fma>TorchInGraphFunctionVariable._get_handlers.<locals>.handle_fmaO  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 tracingrG  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_enableda  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  rL  )r   r  r  r   r  s      rk   handle_is_tensorDTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_tensoro  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*  [
        R                  " XR                  R                  5      $ U R                  [        R                  L a*  [
        R                  " XR                  R                  5      $ [        SU R                   35      eg )Nzcalling )	r  dtyper   rP   is_floating_pointr8   r  
is_complexr   )r   r  r  	input_args       rk   handle_is_floating_pointLTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_floating_point|  s     I""$$)D::!8!88*00__5V5VWWZZ5#3#33*00__5O5OPP(8DJJ<)@AArj   c                     UR                  5       (       a?  UR                  5       (       a*  [        R                  " U[	        UR
                  5      5      $ UR                  5       (       a  UR                  US/ 0 5      $ g )Nnumel)r  
valid_sizer8   r  r3   sizecall_methodr   r  r  s      rk   handle_numel@TorchInGraphFunctionVariable._get_handlers.<locals>.handle_numel  sb       U%5%5%7%7&,,R1DEE""((Wb"==rj   c                 r    [        U5      S:X  a  US   $ [        SSU SU 3SS/[        R                  QS9  g )	Nr6   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   rI  r$   r   r   r  s       rk   handle_torch_compileHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_compile  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 )	Nr6   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_triton  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  s      rk   handle_tensor_size_rewritesOTorchInGraphFunctionVariable._get_handlers.<locals>.handle_tensor_size_rewrites  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                    > [        TR                  5        [        R                  " U[        R
                  " 5       5      $ r   )r(   _guards_singletonr8   r  rP   is_grad_enabled)r   r  r8  s     rk   handle_is_grad_enabledJTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_grad_enabled  s0     *<<="((U-B-B-DEErj   mode	warn_onlyc                 r  > 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        TR                  " S 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   rV  create_noderP   rT   _set_deterministic_algorithmsrL  )r   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#**400rj   c                     UR                   R                  S[        R                  S0 5        [        R                  " 5       nUR                   R	                  S 5        [
        R                  " X5      $ )Nr@  ri   c                  ,    [         R                  " 5       $ r   )rP   autocast_decrement_nestingri   rj   rk   <lambda>gTorchInGraphFunctionVariable._get_handlers.<locals>.handle_autocast_increment_nesting.<locals>.<lambda>      u/O/O/Qrj   )rV  r  rP   autocast_increment_nestingadd_cleanup_hookr8   r  r   r  prevs      rk   !handle_autocast_increment_nestingUTorchInGraphFunctionVariable._get_handlers.<locals>.handle_autocast_increment_nesting  Y     II!!!A!A2r 335DII&&'QR"((22rj   c                     UR                   R                  S[        R                  S0 5        [        R                  " 5       nUR                   R	                  S 5        [
        R                  " X5      $ )Nr@  ri   c                  ,    [         R                  " 5       $ r   )rP   r	  ri   rj   rk   r  gTorchInGraphFunctionVariable._get_handlers.<locals>.handle_autocast_decrement_nesting.<locals>.<lambda>	  r  rj   )rV  r  rP   r  r
  r8   r  r  s      rk   !handle_autocast_decrement_nestingUTorchInGraphFunctionVariable._get_handlers.<locals>.handle_autocast_decrement_nesting  r  rj   device_typeenabledc                   >^^ UR                   R                  S[        R                  UR	                  5       UR	                  5       45        UR                  5       m[        R                  " T5      m[        R                  " TUR                  5       5        UR                   R                  UU4S j5        TR                  " S 5      $ )Nr@  c                  2   > [         R                  " T T5      $ r   )rP   set_autocast_enabled)dev_py_constr  s   rk   r  aTorchInGraphFunctionVariable._get_handlers.<locals>.handle_set_autocast_enabled.<locals>.<lambda>  s    22<Frj   )	rV  r  rP   r  r   r  is_autocast_enabledr
  rL  )r   r  r  r  r  r  r   s       @@rk   handle_set_autocast_enabledOTorchInGraphFunctionVariable._get_handlers.<locals>.handle_set_autocast_enabled  s     II!!**%%')9)9);<
 '99;L,,\:D&&|W5O5O5QRII&&F $**400rj   c                 N  >^ UR                   R                  S[        R                  UR	                  5       45        [        R
                  " 5       m[        R                  " UR                  5       5        UR                   R                  U4S j5        TR                  " S 5      $ )Nr@  c                  0   > [         R                  " T 5      $ r   )rP   set_autocast_cache_enabledr  s   rk   r  gTorchInGraphFunctionVariable._get_handlers.<locals>.handle_set_autocast_cache_enabled.<locals>.<lambda>)  s    u/O/OPT/Urj   )	rV  r  rP   r!  r   is_autocast_cache_enabledr  r
  rL  )r   r  r  r  r   s      @rk   !handle_set_autocast_cache_enabledUTorchInGraphFunctionVariable._get_handlers.<locals>.handle_set_autocast_cache_enabled   s{     II!!!A!AGDTDTDVCX 224D,,W-G-G-IJII&&'UV#**400rj   c                 t   UR                   R                  S[        R                  R                  R
                  5        [        R                  R                  R                  5       nUR                   R                  [        R                  R                  R                  5        [        R                  " X5      $ Nr@  )
rV  r  rP   rT   rc  _grad_increment_nestingr
  _grad_decrement_nestingr8   r  r   r  levels      rk   handle_grad_increment_nestingQTorchInGraphFunctionVariable._get_handlers.<locals>.handle_grad_increment_nesting,  x     II!!!4!4!L!L HH''??AEII&&uxx':':'R'RS"((33rj   c                 t   UR                   R                  S[        R                  R                  R
                  5        [        R                  R                  R                  5       nUR                   R                  [        R                  R                  R                  5        [        R                  " X5      $ r(  )
rV  r  rP   rT   rc  r*  r
  r)  r8   r  r+  s      rk   handle_grad_decrement_nestingQTorchInGraphFunctionVariable._get_handlers.<locals>.handle_grad_decrement_nesting7  r/  rj   allowedc                   >^ UR                   R                  S[        R                  R                  R
                  UR                  5       45        [        R                  R                  R                  5       m[        R                  R                  R                  UR                  5       5        UR                   R                  U4S j5        TR                  " S 5      $ )Nr@  c                  V   > [         R                  R                  R                  T 5      $ r   )rP   rT   rc  !set_inplace_requires_grad_allowedr"  s   rk   r  nTorchInGraphFunctionVariable._get_handlers.<locals>.handle_set_inplace_requires_grad_allowed.<locals>.<lambda>P  s    ++MMdSrj   )rV  r  rP   rT   rc  r6  r   !get_inplace_requires_grad_allowedr  r
  rL  )r   r  r3  r  r   s      @rk   (handle_set_inplace_requires_grad_allowed\TorchInGraphFunctionVariable._get_handlers.<locals>.handle_set_inplace_requires_grad_allowedB  s     II!!##EE!!#%
 88&&HHJDHHAA**, II&&S $**400rj   c                     [        [        5       [        R                  5      n[	        U5        [
        R                  " U[        R                  " 5       5      $ r   )	r   r+   r'   DETERMINISTIC_ALGORITHMSr(   r8   r  rP   $are_deterministic_algorithms_enabled)r   r  guards      rk   +handle_are_deterministic_algorithms_enabled_TorchInGraphFunctionVariable._get_handlers.<locals>.handle_are_deterministic_algorithms_enabledT  sH     !#55E % "((E>>@ rj   c                     [        [        R                  5        [        R                  " XR
                  R                  5      $ r   )r(   r<   r  r8   r  symbolic_torch_function_statetorch_function_subclass_enabledr  s     rk    handle_is_torch_function_enabledTTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_torch_function_enableda  s7     6HHI #((44TT rj   c                     [        [        R                  5        [        R                  " XR
                  R                  (       + 5      $ r   )r(   r<   r  r8   r  rB  torch_function_mode_enabledr  s     rk   %handle_is_torch_function_all_disabledYTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_torch_function_all_disabledl  s8     6HHI"((88TTT rj   c                     [        [        R                  5        [        R                  " UUR
                  R                  =(       a    UR
                  R                  5       5      $ r   )r(   r<   r  r8   r  rB  rG  in_torch_function_moder  s     rk   %handle_is_torch_function_mode_enabledYTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_torch_function_mode_enabledu  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[        R
                  " U[        S U 5       5      5      $ )Nr6   r   c              3   8   #    U  H  n[        U5      v   M     g 7fr   )r0   r   ru  s     rk   r   `TorchInGraphFunctionVariable._get_handlers.<locals>.handle_has_torch_function.<locals>.<genexpr>  s     0VPU11CA1F1FPUs   )rI  r   rA   unpack_var_sequencer8   r  any)r   r  r/  elemss       rk   handle_has_torch_functionMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_has_torch_function  s\     t9>ja-&H&H Q++B/ 
 #((S0VPU0V-VWWrj   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   rX  r   c                 &   > TR                  X5      $ r   )rL  )r   r  rX  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 unavailablerG  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_ERRORrV  rW  rP   	as_tensorr4   )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  rf  rg  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[
        R                  " U[        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  rm  r8   r  rE  cudnnis_acceptable)r   r  r   rk  
tensor_inps        rk   handle_cudnn_is_acceptableNTorchInGraphFunctionVariable._get_handlers.<locals>.handle_cudnn_is_acceptable  sz     EEE9##%% D% av||FMMRJ"((ENN((66zB rj   c                 J    [         R                  R                  " U/UQ70 UD6$ r   )r   BackwardHookVariablerL  r  s       rk   handle_backward_hookHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_backward_hook  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_parameter  s     ))">t>v>>rj   self_dimc                 4    Ub  UR                  USU/0 5      $ g r  r  r{  r  r   r|  s       rk   handle_sym_sizeCTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_size  s%    
 ''FSE2>>rj   c                 4    Ub  UR                  USU/0 5      $ g )Nstrider~  r  s       rk   handle_sym_strideETorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_stride  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   r6   r   )rI  r  rP   rp   r@  ro   rm   )r   r  r/  r   r   s        rk   handle_addcdivBTorchInGraphFunctionVariable._get_handlers.<locals>.handle_addcdiv  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  r  rP   emptyr@  r  )r   r  r  r  r   empty_results         rk   handle_full?TorchInGraphFunctionVariable._get_handlers.<locals>.handle_full  sS     ##%%  <EKKHVV  $//Gj\2NNrj   r   c                     [        U5      S:X  aU  [        US   [        5      (       d=  U(       d6  UR                  [        R
                  " U[        R                  5      UU5      $ g )Nr  r   )rI  r   r@   r  r8   r  r   foreach_lerp_inplacer   r  r/  r   s       rk   "handle_inplace_foreach_lerp_scalarVTorchInGraphFunctionVariable._get_handlers.<locals>.handle_inplace_foreach_lerp_scalar$  sV     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   enable_dynamo_decompositionsrI  r  r  r8   r  r   foreach_pow_scalarr  s       rk   handle_foreach_pow_scalarMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_foreach_pow_scalar6  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  r8   r  r   !group_tensors_by_device_and_dtyper  s       rk   (handle_group_tensors_by_device_and_dtype\TorchInGraphFunctionVariable._get_handlers.<locals>.handle_group_tensors_by_device_and_dtypeJ  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  TR                  " S 5      $ g r   )is_python_constantr  r   r   r  evaluate_exprrL  )r   r  r  r  r   s       rk   handle_assertATorchInGraphFunctionVariable._get_handlers.<locals>.handle_assertW  sX     ,,..93O3O3Q3Q9i&?&?@@++--'..t44rj   c           
         > T" UUR                   R                  " S[        R                  R                  /[        X#5      Q76 US9$ )Nr@  )r  
param_vars)rV  rW  rP   rT   _SDPAParamsr4   )r   r  r/  r   r  s       rk   handle_sdpa_paramsFTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sdpa_paramse  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get_rankget_world_sizec                 B   [        U5      S:X  a  [        U5      S:X  a  O[        U5      S:X  a_  [        U5      S:X  aP  US   R                  5       (       d7  [        US   [        5      (       a  US   R                  R
                  S:X  d   eO[        U5      S:X  aB  [        U5      S:X  a3  [        US   [        5      (       a  US   R                  5       (       d   eO=[        U5      S:X  a  [        U5      S:  a  O[        SU SU SU R                   35      eS[        S	[        4S
 jnU Vs/ s H
  oT" U5      PM     nnUR                  5        VVs0 s H  u  pxXt" U5      _M     n	nnU R                  " U0 U	D6n
[        R                  " X5      $ s  snf s  snnf )Nr   r6   z/torch.distributed.distributed_c10d.ProcessGroupr   zInvalid group value (, z) for constant pg function r  rN   c                 x    [        U [        5      (       a  U R                  R                  $ U R	                  5       $ r   )r   rB   r   real_objr  )r  s    rk   get_arg_valueqTorchInGraphFunctionVariable._get_handlers.<locals>.handle_constant_processgroup_functions.<locals>.get_arg_value  s1     "#'@AA"yy1111133rj   )rI  r  r   rB   r   script_class_namer@   r   r8   r   r   r  )r   r  r/  r   r  r  args_as_valuekvkwargs_as_valueinvocation_results              rk   &handle_constant_processgroup_functionsZTorchInGraphFunctionVariable._get_handlers.<locals>.handle_constant_processgroup_functions  s   " t9>c&kQ&6Y!^Fq(875577"47,EFF % 1 1LM  Y!^Fq(8 #47L99 G668898Y!^Fa(/vRx @$$(JJ<1 
4 43 4 @D Dts!3t DCI<<>"R>411mA&6#6>"R$(JJ$Q$Q!
 ',,RCC !E"Rs   1FF)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 )Nr6   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   rb  )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 )
Nr6   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   )rI  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   exprc                    > [        UT5      (       aR  [        R                  " U[        R                  R
                  R                  R                  UR                  5      5      $ UR                  5       (       a  U$ g r   )
r   r8   r  rP   rV   experimentalsymbolic_shapesguarding_hint_or_throwsym_numr  r   r  r  r  s      rk   handle_guarding_hint_or_throwQTorchInGraphFunctionVariable._get_handlers.<locals>.handle_guarding_hint_or_throw  sf     $00&,,HH))99PP  ((**rj   fallbackc                 .  > U(       a  UR                  5       OS n[        UT5      (       aS  [        R                  " U[        R
                  R                  R                  R                  UR                  U5      5      $ UR                  5       (       a  U$ g r   )r  r   r8   r  rP   rV   r  r  optimization_hintr  r  )r   r  r  r  fallback_intr  s        rk   handle_optimization_hintLTorchInGraphFunctionVariable._get_handlers.<locals>.handle_optimization_hint  s{     =E8668$L$00&,,HH))99KKl  ((**rj   c                    > [        UT5      (       aR  [        R                  " U[        R                  R
                  R                  R                  UR                  5      5      $ UR                  5       (       a  U$ g r   )
r   r8   r  rP   rV   r  r  guard_size_obliviousr  r  r  s      rk   handle_guard_size_obliviousOTorchInGraphFunctionVariable._get_handlers.<locals>.handle_guard_size_oblivious  sh     $00 ',,HH))99NN  ((**rj   c                    > [        UT5      (       aR  [        R                  " U[        R                  R
                  R                  R                  UR                  5      5      $ UR                  5       (       a  U$ g r   )
r   r8   r  rP   rV   r  r  guard_or_truer  r  r  s      rk   handle_guard_or_trueHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_guard_or_true+  se     $00 ',,HH))99GGU  ((**rj   c                    > [        UT5      (       aR  [        R                  " U[        R                  R
                  R                  R                  UR                  5      5      $ UR                  5       (       a  U$ g r   )
r   r8   r  rP   rV   r  r  guard_or_falser  r  r  s      rk   handle_guard_or_falseITorchInGraphFunctionVariable._get_handlers.<locals>.handle_guard_or_false;  se     $00 ',,HH))99HHV  ((**rj   c                    > [        UT5      (       aR  [        R                  " U[        R                  R
                  R                  R                  UR                  5      5      $ UR                  5       (       a  U$ g r   )
r   r8   r  rP   rV   r  r  statically_known_falser  r  r  s      rk   handle_statically_known_falseQTorchInGraphFunctionVariable._get_handlers.<locals>.handle_statically_known_falseK  sf     $00&,,HH))99PP  ((**rj   ru  c                 <   SSK Jn  [        X#5      (       a  UR                  5       R                  R
                  R                  S5      nUbP  [        R                  R                  [        R                  R                  R                  R                  U5      5      $ g )Nr6   r   r   )r   r   r   r   r   r   r   r   r   rL  rP   rV   r  r  has_free_unbacked_symbols)r   r  ru  r   r   s        rk    handle_has_free_unbacked_symbolsTTorchInGraphFunctionVariable._get_handlers.<locals>.handle_has_free_unbacked_symbols[  sy     /!,, !

 1 1 6 6 : :? K ,$55<<--==WW) 
 rj   c                 :  > [        UT5      (       a  UR                  nO5UR                  5       (       a  UR                  5       nO[	        SSU 3S/ S9  [
        R                  " U[        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  r  r  r$   r8   r  rP   rV   r  r  guard_scalarr   r  r  valr  s       rk   r  @TorchInGraphFunctionVariable._get_handlers.<locals>.guard_scalark  s     $00ll((**--/e$TFO X	 #((%%55BB3G rj   c                    > [        UT5      (       aR  [        R                  " U[        R                  R
                  R                  R                  UR                  5      5      $ UR                  5       (       a  U$ g r   )
r   r8   r  rP   rV   r  r  statically_known_truer  r  r  s      rk   handle_statically_known_truePTorchInGraphFunctionVariable._get_handlers.<locals>.handle_statically_known_true  sf     $00&,,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   ru  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   rP  s     rk   r   r         61**,,r  r  )r   rL  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  rP  s     rk   r   r    r   r  r  )r   rL  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
                  " U[        R                  R                  R                  R                  U5      5      $ r   )r   r  r  r  r8   r  rP   rV   r  r  has_static_valuer  s       rk   handle_has_static_valueKTorchInGraphFunctionVariable._get_handlers.<locals>.handle_has_static_value  sn     $00ll((**--/"((EHH))99JJ3O rj   c                 J    SSK Jn  [        U5      R                  U/ UQU5      $ )Nr   )_unsafe_set_version_counter)tensor_version_opr  r  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   rc  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   rc  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 )Nru  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>  s     DGq/22Gs   F)r  r   r@   rA   rS  r   )ru  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)r8   r   r  r  rP   _refsr   r@  )r   r  r/  r   data_argr%  r  s        @rk   handle_torch_tensorGTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_tensor  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 stackrG  zTCalled `torch._C._pop_torch_function_stack` when torch function mode stack is empty.z0Do not pop from empty torch function mode stack.rw   )rB  
mode_stackr$   r   rb  rF   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        TR                  " S 5      $ )Nr6   z0push_torch_function takes exactly one argument ( given)r   )rI  r#   rF   r/  rB  push_torch_function_moderL  r   r  r/  r   r   s       rk   handle_push_torch_functionNTorchInGraphFunctionVariable._get_handlers.<locals>.handle_push_torch_function  sg     4yA~ Fs4ykQXY +<<R@,,EEd1gN#**400rj   c                     U(       d  U(       a  [        US5        [        R                  " U[        UR                  R
                  5      5      $ )Nz+len_torch_function_stack takes no arguments)r#   r8   r  rI  rB  r.  r  s       rk   handle_len_torch_functionMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_len_torch_function.  s=     v %RS"((C88CCD 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   $ )Nr6   z2get_function_stack_at takes exactly one argument (r4  r   )rI  r#   r  rB  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)Nr6   rm  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  )rI  r$   r   rb  r  rP   get_device_moduler   r*   r   r(   r   r'   ID_MATCHr)   r,   r   rsplitr8   r  )	r   r  r/  r   rm  moduleer   
new_sources	            rk   handle_get_device_moduleLTorchInGraphFunctionVariable._get_handlers.<locals>.handle_get_device_moduleK  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           	         SSK Jn  [        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      nU R                  [        R                  R                  L aA  [        Xd5      (       d1  U" UR                  UR                  UR                   UR"                  S
9nU$ ! [$         a-  n[        SSU SU 3S/ [        R
                  QUS9   S nAg S nAff = f)Nr6   )CudaStreamVariablerm  z9unsupported arguments to torch.accelerator.current_streamr  r  zGtorch.accelerator.current_stream accepts one optional argument `device`rw   r   r   z7bad device argument to torch.accelerator.current_streamrA  rB  )streamsrN  rI  r$   r   rb  rP   rm  r  symbolic_stream_state
cur_streamr   r[  current_streamr   r  user_object_indexr   r   )r   r  r/  r   rN  rm  
stream_varrI  s           rk   handle_current_streamITorchInGraphFunctionVariable._get_handlers.<locals>.handle_current_streamw  sR    44y3v;&*v(&:PW#D66(; i*55	"\\&*:*M*M*OPF"\\$q'*D*D*FGF!F55@@H
::!:!:::D D "4"(("(("44)00	"J "! U#D66(; c9-889s   C$D7 7
E.#E))E.r[  xpumpsr\  c                   > S nU(       a.  SU;   a(  [         R                  " US   R                  5       5      nO.U(       a'  [         R                  " US   R                  5       5      nUcc  TR                  U R                  5      nUc/  [         R
                  R                  5       nUc   eUR                  n[         R                  " U5      nUR                  S:X  a  TR                  " S 5      $ UR                  R                  S[         R                  R                  R                  UR                  UR                  =(       d    S40 5        TR                  " S 5      $ )Nrm  r   r\  r@  )rP   rm  r  r   r   acceleratorcurrent_acceleratorr   rL  rV  rW  opsrO  synchronize_deviceindex)	r   r  r/  r   rm  r  rZ  r   _synchronize_fn_to_device_types	          rk   handle_synchronizeFTorchInGraphFunctionVariable._get_handlers.<locals>.handle_synchronize  s    F(f,fX&6&I&I&KLd1g&@&@&BC~<@@L&"'"3"3"G"G"IK&222"-"2"2Kk2 {{e#'..t44II""		!!44fll/a0	 $**400rj   c                    > [         R                  " U5        US   R                  5       (       a  [         R                  " U5        O[         R                  " U5        TR
                  " S 5      $ r  )rF   r/  is_constant_noneclear_default_device!register_device_context_insertionrL  r6  s       rk   handle_set_default_deviceMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_set_default_device  sT     +<<R@Aw'')).CCBG.PPQST#**400rj   )elementwise_dtypesc           	        > SSK Jn  US   R                  5       n/ nU H  n[        UT	5      (       a8  UR	                  UR                  5       R                  R                  S   5        ML  UR                  5       (       a!  UR	                  UR                  5       5        M  [        S[        U5      S[        U5      R                   3/ [        R                  QS9  M     T
" USU06nUR                  X5      $ )Nr6   )SourcelessBuildertype_promotion_kindr   z'elementwise_dtypes unsupported arg typez>elementwise_dtypes requires tensor or constant arguments, got rw   )builderrj  r  r   r   r   r   r   r  r$   r  r   r   r   r   rL  )r   r  r/  r   rj  rk  	real_argsr  r   r   rh  s            rk   handle_elementwise_dtypesMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_elementwise_dtypes  s     3"()>"?"R"R"TIc>22$$S\\^%8%8%=%=o%NO++--$$S%;%;%=>! I #C##'9#5#5"68 ? 1 = =>  (0CF %++B77rj   c           	      4  > 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       (       a2  U R                  UR                  5       U5        TR                  " S 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   r6   ri   condr  r@  r  r  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)poprV  rW  r   r   r?   has_closurer$   r  r   r   r  %register_static_attr_and_return_proxyr  r  rL  r   )r   r  r/  r   predicate_vt
message_vt	rest_argsmessage_eagermessage_graph_proxypredicate_proxy
proxy_argsr   r  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'..t44*335O #*-/
-C
 ii,,#JJ	 rj   r  c           	        ^ [        U5      S:w  d  U(       a%  [        U UR                   S[        U5       S35        [        [	        [	        [        S5      S5      S5      5      n[        UR                  [        R                  5      5        US   R                  5       nU" U5      mU R                  R                  SUU40 5        U R                  R                  U4S	 j5        [        R                  " U T5      $ )
Nr6   z takes exactly one argument (r4  rP   r[  current_devicer   r@  c                  B   > [         R                  R                  T 5      $ r   )rP   r[  
set_devicer"  s   rk   r  \TorchInGraphFunctionVariable._get_handlers.<locals>.exchange_device_helper.<locals>.<lambda>w  s    uzz/D/DT/Jrj   )rI  r#   r   r*   r)   r,   r(   r   r'   EQUALS_MATCHr  rV  r  r
  r8   r  )r  r/  r   r  current_device_sourcer  r  s         @rk   exchange_device_helperJTorchInGraphFunctionVariable._get_handlers.<locals>.exchange_device_helper`  s     4yA~ {{m#@T7S %=:l7&;VDFVW%! /::<;T;TUVq',,.Cc7DII!!	 II&&'JK"((T22rj   c                 H   > T" XU[         R                  R                  5      $ r   )rP   r[  _exchange_devicer   r  r/  r   r  s       rk   handle_exchange_deviceJTorchInGraphFunctionVariable._get_handlers.<locals>.handle_exchange_devicez  s     *"FEJJ<W<WXXrj   c                 H   > T" XU[         R                  R                  5      $ r   )rP   r[  _maybe_exchange_devicer  s       rk   handle_maybe_exchange_devicePTorchInGraphFunctionVariable._get_handlers.<locals>.handle_maybe_exchange_device  s#     *&%**"C"C 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                  (       aT  UR                  R                  n	U	(       a  SR                  U	5      OSn
[        SSU
 3SU
 3U	(       a	  SU	S    S3O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       R0                  R2                  R5                  S5      n[-        U[6        R8                  5      (       d   e[;        U5      nUR=                  U5        UR>                  c  M  U H  nUR>                  R@                  UU'   M     M     SSK!J"n  [!        U5      S:  a  [G        US   5      O/ n[!        U5      S:  a  [G        US   5      O/ n[%        5       nU HQ  u  nn[-        U[6        R8                  5      (       d  M'  URH                  c  M6  URK                  URH                  5        MS     U" UUS9nUU-  nU(       aI  U Vs/ s H  nUU;   d  M  UU   PM     nnU(       a  S U 3OS!n[        S"US#S$S%/[L        RN                  QS9  UR5                  S&5      nUR5                  S'5      n[-        UU5      =(       a    URP                  S(L =(       d!    [-        UU5      =(       a    URP                  S(L nU(       d  U Vs1 s H   n[S        U5      RT                  S):w  d  M  UiM"     nnUR&                  RV                  nUU-  n U (       a  [        S*S+[!        U 5       3S,S-/S9  UR&                  RV                  R=                  U5        [6        RX                  RZ                  R]                  5          [6        RX                  RZ                  R_                  5          UR&                  R`                  " S.[6        Rb                  Rd                  /[g        X#5      Q76 n!SSS5        SSS5        U" UW!S/9$ s  snf s  snf ! , (       d  f       N'= f! , (       d  f       N0= f)0a  
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   r6   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   r  unknownz0autograd.grad consumed returned tensor's grad_fnzLeaked output tensors: 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.
  The following returned tensors have consumed grad_fns: z9Detach the problematic tensor(s) before returning: e.g. `r   z
.detach()`z.Call .detach() on the tensor before returning.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.outputsinputsr   N)collect_reachable_grad_fns)stop_atzinputs with external grad_fn: rG  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@  rr  )4rG  r   r   rl  r  constantr   r   r   trace_autograd_opsr$   speculation_loggraph_break_on_autograd_gradautograd_grad_leaked_tensorsjoincompiled_autograd_enabledrI  r   r   rV  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_fnsrV   rP  preserve_node_meta_set_autograd_backwardrW  r  gradr4   )"r   r  r/  r   r   r   r  r   r   leaked
leaked_str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_consumedr  s"                                     rk   handle_autograd_gradHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_autograd_grad  s   p 5.2.,,n1&2K2K1LMT V
 !!>>++HH28TYYv.i
N5j\BT U_S_a " TTZ[\T]S^^hiM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-b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.. *%b)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 ""557""99;		..#NN'' 't4 < 8 !Be44WP%8 <; 87s=   6
R"	R"1R'R'")R=A R,R=,
R:	6R==
Sc                 L   [        U5      S:  a  US   OSn[        U5      S:  a  US   OSnUb  Uc  g[        U5      n[        U5      nU(       a  U(       a  U H  nUR                  R                  S5      n	UR                  R                  S5      n
U
c  M>  [	        U	[
        R                  5      (       d  M_  U	R                  b  Mn  U
R                  n[	        U[
        R                  5      (       d  M  UR                  c  M  [        SSS	S
/[        R                  QS9  M     g)aC  Graph-break when closure-captured tensors lose their grad_fn.

NOTE [Detecting lost autograd linkage in closure-captured tensors]

Functorch transforms (vjp, grad, jacrev) return closures that capture
tensors with grad_fn. When such a closure is compiled separately, those
tensors become graph placeholders whose FakeTensors lose grad_fn,
causing _autograd_grad to silently return zeros.

_collect_placeholder_nodes gathers placeholder nodes from the
outputs/inputs args. For each, we compare grapharg.example (the real
tensor, retains grad_fn) against example_value (FakeTensor, grad_fn
is None). A mismatch means autograd linkage was lost, so we
graph-break.

This is a pre-check only: kwargs (retain_graph, create_graph,
grad_outputs) don't affect linkage detection and are handled by
the default proxy path when this returns None.
r6   r   Nr   r   graphargz(_autograd_grad with lost grad_fn linkagez,outputs lost autograd linkage during tracingzp_autograd_grad() received tensors whose grad_fn was lost during tracing - this silently produces zero gradients.ziCompile the full transform instead of the returned closure: torch.compile(lambda x: torch.func.vjp(f, x))rw   )rI  r   r   r   r   rP   r   r   exampler$   r   r   )r   r  r/  r   outputs_var
inputs_varoutput_placeholder_nodesinput_placeholder_nodesr   faker  reals               rk   handle_functorch_autograd_gradRTorchInGraphFunctionVariable._get_handlers.<locals>.handle_functorch_autograd_grad	  s    4 &)Y!^$q'K$'INaJ"j&8'A+'N$&@&L#',C4D99==9D#yy}}Z8H ,&tU\\:: LL0'//%dELL99dll>V)(R(V%6
%]'" &7%B%B'" 50 rj   )Fr   )r  rI   )r   r   torch.backends.cudar  rG  r   r8  r  r  r   r  rl  r  r?  rl   r8   dispatch_key_set_functionsrP   r  r  rR  r\  inductoraccumulate_grad_r{  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	  r  r  r!  rT   rc  r)  r*  r6  r=  _is_torch_function_enabled_is_torch_function_all_disabled_is_torch_function_mode_enabledr0   has_torch_function_variadichas_torch_function_unarydictfromkeysr!   
from_numpyrQ   rQ  rL   rE  rn  ro  hooksBackwardHook	Parameteratensym_sizeint
sym_strideaddcdivfull_foreach_lerp__foreach_pow"_group_tensors_by_device_and_dtype_assertr=   is_available"torch.distributed.distributed_c10dr  r  r  r  r  r  r  nestednested_tensor
functionalone_hotrV   r  r  r  r  r  r  r  r  r  r  r  r  r
  r  	_autogradr  r  r  r  r  r   _pop_torch_function_stackr   _push_on_torch_function_stack_len_torch_function_stack_get_function_stack_atrE  rZ  rR  r[  r   synchronizerW  rX  r\  set_default_devicetorch._prims_commonrh  _checkr   r  r  r  r  r  rf  _autograd_grad)ar  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r%  r-  r1  r9  r?  rD  rH  rL  rU  r\  rd  ri  rq  ru  ry  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r+  r1  r7  r:  r>  rK  rU  r`  rf  rn  r~  r  r  r  r  r   r8  r  r  r   r  r_  rh  r  r  r  r?  sa                                                                                        @@@@@@@@@@@@rk   _get_handlers*TorchInGraphFunctionVariable._get_handlers  sk    
	38$
	xS)*HS#X,>>?
	 	3	
 	
 	>	*,	-	T'	T #	T &		T
 	T 
.	T2 
-	.	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 
%''	(	F-	F	F 
)	F 
%44	5LQ	1-	158	1EI	1	1 
6	1, 
%22	3	3-	3	3 
4	3 
%22	3	3-	3	3 
4	3 
%,,	-	1'	1 )	1 %		1
 	1 
.	1& 
%22	3		1-		18G		1		1 
4		1 
%((%%==	>	4-	4	4 
?	4 
%((%%==	>	4-	4	4 
?	4 
%((%%GG	H	1-	18G	1	1 
I	1" 
%<<	=
	-
	
	 
>
	 
%((55	6	-		 
7	 
%((::	;	-		 
<	 
%((::	;	-		 
<	 
OO..OO77OO44


	X-	X6E	X	X


	X 
]] +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#	 
!	 
*		'	 #	 &		
 	 
	  ++--   '"'41D+1D '1D *	1D
 !1D1Df 
%,,,,	- 37	
 "&	'	 )4/	 #		
 $J	 &	 	 
.	@ 
%((%%--	.	'	 #	 &		
 	 
/	( 
%((''77NN	O	'	 "	 t#		 
P	" 
%((''77II	J
 04		'	 "	 &,		
 t#	 
K	& 
%((''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QQ	R	-	2A	t#	 
S	 
%((''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	1'	1 #	1 &		1
 	1 
:	1  
%((44	5
	'
	 #
	 &	
	
 
	 
6
	 
%((11	2	D'	D #	D &		D
 '	D 
3	D 
%))55	6)	A')	A #)	A &	)	A
 )	A 
7)	AV 
%##22EJJ4M4M	N+	'+	 #+	 &	+	
 +	 
O+	\ JJ""FII!!5II!!5II!!5	*
& 
))JJ""II!!II!!II!!

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

	1B 
%**	+	1'	1 #	1 &		1
 	1 
,	1* 	;	$	%	8'	8 #	8 &		8
 	8 
&	8< 
%,,	R	'R	 #R	 &	R	
 R	 
 R	h	3'	3?+	3 o-.	3 #d
*+		3
 	34 
%**--	.	Y'	Y #	Y &		Y
 	Y 
/	Y 
%**33	4	'	 #	 &		
 	 
5	 
%..%%	&~	5 
'~	5@ 
%""33BB	C;	';	 #;	 &	;	
 t#;	 
D;	z rj   r  rI   r/  r0  r8   c                   ^ SSK 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         [,        R.                  " UU R1                  5       " U Vs/ s H  ofR1                  5       PM     sn0 UR3                  5        VVs0 s H  u  pxXxR1                  5       _M     snnD65      $ U RC                  5       (       Ga[  U R                  RD                  n
U(       Ga!  US   RG                  5       (       Ga  US   nUR                   b  [I        [J        RL                  RN                  U
5      (       a  [Q        [J        RL                  RN                  U
5      n[I        US5      (       a  [I        XRS                  5       S   5      (       aw  [J        RT                  RV                  [Q        XRS                  5       S   5      RX                  ;   a5  [[        SSS	[]        U R                  5       S
3/ [^        R`                  QSPS9  U Rc                  U[?        U5      U5      $ U Re                  5       Rg                  U R                  5      nU(       a  U" X/UQ70 UD6nU(       a  U$ [i        U4S jU 5       5      n[k        U4S jU 5       5      n[Q        U R                  SS5      S:X  a  U R                  RD                  [l        ;   a  U(       a{  U(       at  S[o        U R                  5       S3n[p        Rs                  U5        [[        SSU R                   SU SU 3S[o        U R                  5       S3/ [^        R`                  QS9  U R                  nU(       aY  SU R                  RD                   3n[Q        U R                  SS 5      S:X  a%  [I        [J        U5      (       a  [Q        [J        U5      nS nS nSU;   a  US   n[u        U[v        [x        45      (       ao  / nUR2                   H]  nURG                  5       (       a2  UR{                  5       R|                  R~                  S   R                  nOS nUR                  U5        M_     URG                  5       (       a1  UR{                  5       R|                  R~                  S   R                  n[J        R                  [J        R                  [J        R                  [J        R                  [J        R                  [J        R                  [J        R                  [J        R                  [J        R                  [J        R                  [J        R                  [J        R                  [J        R                  [J        R                  [J        R                  [J        R                  [J        R                  [J        R                  [J        R                  [J        R                  [J        R                  [J        R                  [J        R                  [J        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RG                  5       (       aO  SU;   aI  US   R1                  5       (       a1  [[        S SU R                   SU SU 3S!S"S#/[^        R`                  QS9  UGbD  [u        U[>        5      (       Ga  [        UR2                  U5       H  u  nnUc  M  URG                  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/ [^        R`                  QS9  [J        R                  R                  U5      (       a  M  [[        S(S)U R                   SU SU 3S!/ [^        R`                  QS9  M     U$ Ub  URG                  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/ [^        R`                  QS9  [J        R                  R                  U5      (       d/  [[        S+S)U R                   SU SU 3S!/ [^        R`                  QS9  U$ s  snf s  snnf ! [4        [6        [8        4 a2  n	[;        [=        U	5      U[?        U	R@                  5      S9   S n	A	GNS n	A	ff = f! , (       d  f       GN= f),Nr6   )r  r  )r/  r   	overloadszInplace op on input tensorrG  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   r  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@  rr  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)frG  r  rl  r  r  rz  r  "_call_nonstrict_traceable_functionr  _call_leaf_functiontorch_function_override_enabledrD   r  r.   r   #constant_fold_functions_need_guardsr   r*   r(   r   r'   r  r8   r  r  r   OverflowError	TypeError
ValueErrorr"   r   listr/  is_tensor_methodr   r  r  rP   r\  r  r  r  Taginplace_viewtagsr$   r7   r   r   call_tensor_methodr  r   rS  r   bin_opsr  r`  warningr   rA   r@   r   r   r   shaper   _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_r  _foreach_pow__foreach_lerpr  _foreach_addcmul_foreach_addcmul__foreach_addcdiv_foreach_addcdiv_r   	fake_mode	shape_envignore_fresh_unbacked_symbolsrV  rW  r4   zipr  _prims_commonis_contiguousis_contiguous_or_false)r   r  r/  r   r  r   ru  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vtr  ops_consuming_unbacked_scalarsrs  tensor_variableout_tensor_vtsaved_out_shapefake_outr  s                                 @rk   r@  *TorchInGraphFunctionVariable.call_function	  s/	    	&*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chh f UsB   %h. <h#h. +h(	h.  -i8#h. .i5'i00i58
jc           
        ^!^"^#^$^%^& SSK Jn  SSKJm#JnJm%Jm&  SSKJn  SSK	J
m"  SSKJn  SSKJn  SS	KJn	Jn
  U R%                  XU5      u  pU" X5      " [&        R(                  " XU45      5      R+                  U5      u  p[-        U[.        5      (       d   eUR0                   HM  nUR3                  5       nU" U5      (       a  M"  UR3                  5       R4                  n[7        S
SU S3SS/S9  MO     UR0                   Vs/ s H  oR9                  5       PM     nn UR;                  5       nU RH                  m$S[&        S[&        S[&        4U"U$4S  jjnU" U5      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[&        R(                  " UT!5      nU	Ri                  U[@        Rj                  5      Rm                  UUU/0 5      n U $ s  snf ! U a  nUR<                  R3                  5       nUR4                  nSS KJs  J n  URC                  U5      (       a/  [7        SSU SU S3SU S3S/[D        RF                  QUS9   S nAGN[7        SSU SU S3SU S3SSU S3/[D        RF                  QUS9   S nAGNS nAff = f! [\        R^                  R`                  Rb                  [\        R^                  R`                  Rd                  4 a5    [7        S%S&T$RN                   3S'/ [D        RF                  QS9  [g        S(5      ef = f))Nr   _make_inlined)
flat_applyis_graphable_typeis_valid_outputto_graphable)_LeafCallable)fake_tensor_tls)tree_flattenr6   )#AsPythonConstantNotImplementedError)rj  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.rB  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resrJ  r  s       rk   
patched_fnSTorchInGraphFunctionVariable._call_nonstrict_traceable_function.<locals>.patched_fn_  sH     &DDG=AO:I$)&)AH>J BI>s   ' 	0	_callable_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_specrE  rG  rH  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)7torch._dynamo.utilsrD  "torch._higher_order_ops.flat_applyrE  rF  rG  rH  ,torch._higher_order_ops.invoke_leaf_functionrI  r   rJ  torch.utils._pytreerK  baserL  rl  rj  r  _extract_nn_module_statesr8   r  rR  r   r@   r   python_typer  r$   r   r  r;  r  _pytreeis_constant_classr   r   r   rV  rv  r   r   r   r   r  objectrW  rP   r\   r1  UnsupportedTorchRuntimeErrorr   rL  tree_unflattenr@  )'r   r  r/  r   rD  rF  rI  rK  rL  rj  r  args_with_stateskwargs_with_statesflat_args_vtsinput_spec_vtflat_arg_vtarg_type	type_nameproxified_flat_args
input_specrI  typpytreerR  
f_callablef_callable_proxyinput_spec_proxyall_argsr[  r  proxy_list_vtout_spec_vtout_vtrZ  rJ  rE  r  rG  rH  s'                                    @@@@@@rk   r  ?TorchInGraphFunctionVariable._call_nonstrict_traceable_function
  s&    	6	
 	
 	OA4== 04/M/Mf0
, (5R'F!!"9K&LM(


b
! 	% -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	"	.=		 	 #:.
99JJ{{m9%z
 99JJKK-'

 &**%5"%)*%5"$&6M9LM *.	c 	d6l 	 	$ 		&&/2
	0)"e4M& (((%++B> #))"g.D.DESS,b
 i
 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  +%	b MM))MM//	
 	0 Q$R[[M2d 7)556 !//!	0s2   J J
 +	M  
L=A.L8.L88L= BOc                 l  ^^ SSK Js  Jn  SSKJn  SSKJn  SSKJn  SSK	J
mJm  S[        S	[        [        S      4UU4S
 jjnU" XR                  5      " [        R                   " XU45      5      R#                  U5      u  p0 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$ [        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_objectrC  )convert_modules_to_statesr6   )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_variable  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   )ra  r  re  #torch._dynamo.graph_bytecode_inputsr  r^  rD  r`  r  	nn_moduler  r  r8   r   r   rK  r  rR  r   r$   r   r   r   r   tuple)r   r  r/  r   ru  r  rD  r  r  flat_args_vartree_spec_varmodule_to_indexr  args_var
kwargs_varmodule_to_index_var
result_varr  r  s                    @@rk   rc  6TorchInGraphFunctionVariable._extract_nn_module_states  s    	-,L5	
 	O	V 	VEMNO	V 	V
 (5R9L9L'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''-33BH"2A!!"Vn57J

 --b11rj   c                 R  ^ SS K Js  Jn  SSKJn  SSKJnJnJn  SSK	J
n	  U R                  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  nnUR+                  T5       Vs/ s H  nUR-                  5       PM     nnUR/                  5       nS	nU(       a  SS
KJn   U" X[3        U5      U5      nS /nU" XU5      u  nnU" U5      nU" U5      nS[:        S[<        S[<        4U4S jjnU" SU5      nU" SU5      nU" SU5      n UUU U/UQ7n!TR>                  RA                  SUU!0 5      n"U	" TU"5      n#US   c   S5       e[&        R(                  " TUS   5      n$U" T[        RB                  5      " U#U$5      $ s  snf ! [         a)  n[5        [6        R8                  [;        U5      5      UeS nAff = f)Nr   rC  )rI  invoke_leaf_functionmake_leaf_function_wrappersr6   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.rG  )_resolve_mutated_flat_indicesr   rY  rN   c                 r   > TR                   R                  X5      n[        U5      UR                  l        U$ r   )rV  rv  r   r   )r   rY  r  r  s      rk   make_callable_proxyMTorchInGraphFunctionVariable._call_leaf_function.<locals>.make_callable_proxyB  s+    IICCDOE"4jEJJOLrj   real_fnfake_fnrs  r@  zjOutput spec was not captured during fake tensor propagation. This should not happen - please report a bug.)"ra  r  re  r^  rD  r`  rI  r  r  rl  r  r   _torchdynamo_leaf_real_fn_torchdynamo_leaf_fake_fn_torchdynamo_leaf_mutates_argsr
  r  rc  rK  r8   r  rR  r   r  r  rI  r%   r&   INVALID_INPUTr  r   rV  rW  rj  )%r   r  r/  r   ru  rD  rI  r  r  r  decorated_fn	real_impl	fake_implmutates_argsrk  rl  r  input_spec_varr  flat_arg_proxiesrs  mutated_flat_indicesr  rI  captured_out_specwrapped_real_implwrapped_fake_implreal_impl_callablefake_impl_callabler  real_impl_proxyfake_impl_proxyrx  invoke_argsresult_proxyflat_output_vtr{  s%    `                                   rk   r  0TorchInGraphFunctionVariable._call_leaf_function  s    	-,5	
 	
 	+zz ::	 ::	#BB!',
L"Q!R SJ J  04/M/M0
, )6b&:M:M(N!!"'7&LM)


b
! 	&~ '4&G&G&K
&KsCLLN&K 	 
 $668
!L'DS1A-BJ($ <@&/J"30
,, ++<=*+<=	c 	 	 	
 .i9KL-i9KL.|ZH  	

 
 yy--1;
 'r<8 #/ 	
<	
/ &++B0A!0DER!7!78UUo
  L ; ;SVD!KLs   G.G3 3
H&=$H!!H&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       (       a\  [        R                  " T[        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   rA   r  rR  r  r8   r  rP   re   rf   r  r  r  r$   )r   countr  s    rk   r  @TorchInGraphFunctionVariable._call_ntuple.<locals>.handle_ntuplem  s    ,,R00 ..22267  ))++&,,HH$$**2259%:R:R:TU 
 n$UG, ?T	rj   )r   rP   re   rf   r  r  r  __closure__cell_contentsr   r  r8   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 exportrG  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_parameterr  r  rm  z8`torch.nn.Parameter` with non-constant Tensor attributeszBEnsure the Tensor argument's shape, dtype, and device are correct.rB  detachr6   r  r@  r   F)'exportr$   r   r   r   r   r8   r  NotImplementedErrorrb  r  r   _nn_param_via_prefix_insertr   graph_break_on_nn_param_ctorrE   r   
class_typer  r   	DIFFICULTr  var_getattrrV  synthetic_graph_inputr   r  r  rl  r  rW  r    r   rP   re   r  has_grad_fnleaf_var_creation_orderr   )r   r  r(  r  r  r  rm  rI  r   r  r   s              rk   rx  .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 k ' 	[,]O< ?>*55		H # 
	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                  " X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[        R                  " XU5      nUR1                  5       n[2        R4                  " 5       R6                  R8                  R;                  U5        U$ )
Nc                  (   > T R                  SS5      $ )Nztorch.nnr  )load_import_from)cgs   rk   r  JTorchInGraphFunctionVariable._nn_param_via_prefix_insert.<locals>.<lambda>  s    !4!4Z!Mrj   r   F)r   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  )rV  new_varroot_txr   add_push_nullr   r8   r  r@  storepregraph_bytecoder   get_instructionsr   r   r   r$   r   r  r-   rP   re   r  example_value_from_input_noder   r   r   guards_contextdynamo_guardsremove_guards_with_source)	r  r(  r  varname	data_noder   r   r   r  s	           @rk   r  8TorchInGraphFunctionVariable._nn_param_via_prefix_insert  s   
 ))##% yy  ,,,ryy(()
MN
4;;
?  34
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   r6   )r  r  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__)
r  r  r   ismethoddescriptorr  r  r  rP   rT   
TensorBase)r   r  s     rk   r  -TorchInGraphFunctionVariable.is_tensor_methodH  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   )r  r   r   rP   r   r   r   rC   r  s       rk   r  <TorchInGraphFunctionVariable.torch_function_override_enabledR  sd     #<#>> !!#&&

(C(CD< *"F;	<rj   c                     grN  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   r8   r  )r   r  s     rk   is_python_equal,TorchInGraphFunctionVariable.is_python_equalc  s/    %11&&(E,D,D,FFFrj   )r  r   rN  ))r   r   r  r  r  r   r   rz  r   r  r(  r  rx  	functoolscacher  r  r   r8   r@  r  r  rc  r  r  r  r   rx  r  r  r  r  r   r  r  r  r  rg  r  r!  r"  r#  s   @rk   r  r    s   J
 )-S! % 	
 
 &O# OhsCx0  __4c 2HS#X4F FG   B8b#b 'b -	b
 
bH	M#M 'M -	M
 
M^?2#?2 '?2 S/)*	?2
 
/	0?2BZV#ZV 'ZV S/)*	ZV
 
ZVx&*#&* '&* S/)*	&*
 
&*P   "	J#J DjJ 	J
 
J JX )#)+.)?C)	) )VW#W ?#W S/)*	W
 
W8$ 8	<)	<19#	<HLSRUX	<		<D    GV G G Grj   r  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  ii  zrepresents torch.DispatchKeySetr   r   rN   c                     [        U 40 UD6$ r   )r  )r   r   s     rk   rL  DispatchKeySetVariable.createl  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_sourcep  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_methodw  s    u}rj   r  rI   r/  r8   c                   > U R                  U5      (       a  [        X45      (       a  [        U R                  U5      n[        R
                  " U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
                  " XR                  R                  5       5      $ [        T	U ])  XX45      $ s  snf s  snnf )NhighestPriorityTypeId)r  r.   r  r   r8   r  r  r   r  r   r  )
r   r  r   r/  r   methodru  r  r  r   s
            rk   r  "DispatchKeySetVariable.call_methodz  s     ''--2O3
 3
 TZZ.F"((6:;d**,d;=C\\^L^TQq..00^L  ,,"((ZZ-M-M-OPPw"2T:: <Ls   C.C3ri   )r   r   r  r  r  rx  r   r   rL  r  r   r   r  r   r  r  r8   r  r  r!  r"  r#  s   @rk   r  r  i  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'   rF  r   s      rk   r   /FuncTorchInterpreterVariable.create_with_source  s(     	f''(=(=>?5((rj   r  rI   r   r/  r   r8   c                   > US:X  a.  [         R                  " XR                  R                  5       5      $ US:X  aI  UR	                  [         R                  " XR                  R
                  R                  5      U /U-   U5      $ US;   a0  [         R                  " U[        U R                  U5      " 5       5      $ US:X  a0  U(       d  U(       a   e[        R                  R                  US 5      $ [        TU ]1  XX45      $ )Nkeyprocess)r,  
batch_size
randomnesslower)r8   r  r   r  r  r  __func__r  r   0TemporarilyPopInterpreterStackCtxManagerVariablerL  r   r  )r   r  r   r/  r   r   s        rk   r  (FuncTorchInterpreterVariable.call_method  s     5="((ZZ^^-=>>Y11%%b***<*<*E*EF 
 ::"((WTZZ-F-HIIW_F**MMTTD  w"2T::rj   ri   )r   r   r  r  r  r  r   r   r   r  r  r8   r  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   typing_extensionsr   torch._CrP   torch._refstorch.fxtorch.nnra  r  re  r    torch._dynamo.variables.constantr   torch._dynamo.variables.streamsr   &torch._dynamo.variables.torch_functionr   torch._guardsr   r   r   torch._loggingr   torch.autograd.graphr   r   r   rG  r   r   r   r   r   r   create_parameter_opr   r   r    rY  r!   r1  r"   r#   r$   r%   r&   guardsr'   r(   r   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   rb  r7   r8   ctx_managerr9   r:   r;   r<   distributedr=   rJ  r>   r?   r~   r@   rA   script_objectrB   torch_functionrC   rD   rE   rF   user_definedrG   numpyra  ModuleNotFoundError#torch.distributed.fsdp._fully_shardrH   torch._dynamo.symbolic_convertrI   torch._opaque_baserJ   rK   rL   rM   	getLoggerr   r`  r  r  r]  r_  r  rh  ri  rj  r^  rT   ra  rb  rc  rd  re  rf  rk  rg  rl  rX  rY  rZ  	grad_moderM  rO  rH  rN  rm  rn  r\  r[  rV   rP  rQ  rR  re   rq  rr  r,  _shape_as_tensorr  rZ  current_device_indexr[  r  is_initializedrW  
__future__)get_overwrite_module_params_on_conversionr  r  r_   _get_device_index_get_cublas_allow_tf32_is_any_autocast_enabledr  get_device_propertiesget_autocast_dtypeget_autocast_gpu_dtypeget_default_dtyper$  is_autocast_cpu_enabledr  r  r  r  
_Reductionget_enumpromote_types_get_privateuse1_backend_name_is_checkpoint_validr  r   r  r  r  r   rl   r  r  _dispatch_tls_local_include_set_dispatch_tls_local_exclude_setr  r  r   r   r   Noder   r  r  r   r   r   r   r%  Enumrz  r  r  r  ri   rj   rk   <module>r>     s  6      	 8 8 " ? ? $     % % # = : L 7 7 ' - K > >  
 @  1 	 	 	 +  - C . 4  3E
 D-, 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&D!E  . --<
= 
HH	HH,,	HH,,  ?/  ?S  ?T  ?F%,, 3u~~7K7K7P7P3Q 6<	<	%cDj(
)*<~"$5 "$uxx}}:M "J 3xS'9#:  $LA LA^j7#4 j7Z$tyy $v*G#4 v*GrU%;. %;P ;#4  ;Ki  	B
  s$   )c .c) c&%c&)c54c5