
    qi}n                       d Z ddlZddlZddlZddlZddlZddlmZmZ ddl	m
Z
mZmZ ddlZddlZddlZddlZddlmZ ddlmZ ddlmZ dd	lmZmZmZmZ dd
lmZ ddlm Z m!Z!m"Z" ddl#m$Z$ ddl%m&Z&m'Z' ddl(m)Z)m*Z* ddl+m,Z,m-Z-m.Z.m/Z/ ddl0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8 ddl9m:Z:m;Z;m<Z< ddl=m>Z>m?Z?m@Z@ ddlAmBZB ddlCmDZDmEZE ddlFmGZGmHZH ddlImJZJmKZK ddlLmMZMmNZNmOZOmPZP 	 ddlQZR	 ddlTmUZU erddlVmWZW  ej                  eY      ZZe[j                  ej                  j                  j                  ej                  j                  j                  ej                  j                  j                  ej                  j                  j                  ej                  j                  j                  ej                  j                  ej                  j                  ej                  j                  j                  ej                  j                  j                  ej                  j                  j                  ej                  j                  j                  ej                  j                  j                  ej                  j                  j                  ej                  j                  j                  ej                  j                  j                  ej                  j                  j                  ej                  j                  j                  ej                  j                  j                  j                  ej                  j                  j                  j                  ej                  j                  j                  ej                  j                  j                  j                  ej                  j                  j                   j                  g      Ze[j                  ej                  g      Zej                  j
                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  gZej                  ej                  j                  ej                  j                  ej                  j                  ej                  j                   ej                  j"                  ej                  j                   ej                  j                   ej$                  ej&                  ej(                  ej*                  ej,                  ej.                  ej0                  ej2                  ej                  j4                  j6                  j8                  ej:                  ej                  j<                  ej                  j>                  ej                  j"                  ej                  j                   gez   Zej                  j!                         rTejC                  ej                  j                  ej                  jD                  ej                  jF                  g       e[j                  e      Ze[j                  e      ZejH                  de[eg e
f   ee   f   fd       Ze[j                  g d      Zej                  jP                  ej                  jR                  ej                  jT                  hZejH                  d        Z G d d e<      Z G d! d"e      Z G d# d$e      Z G d% d&e      Z G d' d(e      Zy# eS$ r dZRY w xY w# eS$ r dZUY w xY w))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Sequence)AnyOptionalTYPE_CHECKING)TracingContext)warning_once)"is_traceable_wrapper_subclass_type   )configgraph_break_hints	polyfills	variables)	PyCodegen)!can_convert_to_tracable_parameternew_parameter_placeholdertracable_create_parameter) get_registered_device_interfaces)raise_observed_exceptionunimplemented)GuardBuilderinstall_guard)
AttrSourceCallFunctionNoArgsSourceSyntheticLocalSourceTorchSource)check_unspec_or_constant_argsguard_if_dynhas_torch_functionhashableis_wrapper_or_member_descriptorproductproxy_args_kwargsunwrap_if_wrapper   )raise_type_error_exctypestrVariableTracker)AutocastModeVariableProfilerContextVariableTorchFunctionDisableVariable)ConstDictVariable)DistributedVariableProcessGroupVariable)bind_args_cachedNestedUserFunctionVariable)ListVariableTupleVariable)can_dispatch_torch_functiondispatch_torch_functionTensorWithTFOverrideVariableTorchFunctionModeStackVariable)_fsdp_param_group)InstructionTranslatorreturnc                  "   t         j                  j                  dt         j                  j                  dt         j                  j
                  d t         j                  j                  j                  dt         j                  j                  j                  dt         j                  j                  dt         j                  j                  j                  dt         j                  j                  dt         j                   j                  dt         j                   j"                  dt         j                   j$                  dt         j                  j&                  j(                  dt         j*                  j,                  j.                  j0                  diS )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     c/home/ubuntu/crypto_trading_bot/.venv/lib/python3.12/site-packages/torch/_dynamo/variables/torch.pytracing_state_functionsrW      s     					e##T  ..  77

$$e$$114!!4##T**D##T  88$ 	##77 rU   )addsubmuldivsqrtc                  R   ddl m}  ddlm} t	        | j                   |       j                                     }t        j                  t        j                  t        j                  t        j                  t        j                  t        j                  h}|j                  |       |S )Nr   )chain)get_overridable_functions)	itertoolsr^   torch.overridesr_   setfrom_iterablevaluesr;   ones	ones_likezeros
zeros_likeemptyfullupdate)r^   get_overridable_functions_funcsmores       rV   r_   r_      sp    W##$>$@$G$G$IJKE



%D 
LLLrU   c                   V     e Zd ZdZed        Zd	 fdZd
dZd Zd Z	ddZ
d Z xZS )BaseTorchVariablezHcommon base for all torch.* functions, classes, modules and other thingsc                 J   t        j                  |      r*t        |j                  t        j
                               n[t        j                  |      r*t        |j                  t        j                               nt        j                  |      r)t        |j                  t        j                               nt        j                  |      s>t        |t        j                  j                  t        j                  j                  f      r)t        |j                  t        j                                nbt#        |      s.t        |t        j$                  j&                  j(                        rn(t        |j                  t        j*                                | ||      S Nsource)inspectisclassr   
make_guardr   CLASS_MATCHismoduleMODULE_MATCH
isfunctionCLOSURE_MATCH	isbuiltin
isinstancer;   _ops
OpOverloadOpOverloadPacketBUILTIN_MATCHr!   rG   compiled_autogradOpFUNCTION_MATCHclsvaluert   s      rV   create_with_sourcez$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3z5==22558
 &++L,G,GHI5((rU   c                 2    t        |   di | || _        y )NrT   )super__init__r   )selfr   kwargs	__class__s      rV   r   zBaseTorchVariable.__init__  s    "6"
rU   c                 <   	 | j                   j                   d| j                   j                   }dt        j                  dd|      z   }|j                  |j                  || j                                y # t        $ r dt	        | j                          }Y iw xY w)N.
torch_obj___z[^a-zA-Z0-9_]+_)	r   
__module____name__	ExceptionidrerY   extend_outputsetup_globally_cached)r   codegennameunique_var_names       rV   reconstructzBaseTorchVariable.reconstruct  s    	1jj++,Adjj.A.A-BCD (93!EE))/4::F	
  	14::/0D	1s   /A7 7!BBc                     | j                   S Nr   r   s    rV   as_proxyzBaseTorchVariable.as_proxy      zzrU   c                     | j                   S r   r   r   s    rV   as_python_constantz$BaseTorchVariable.as_python_constant  r   rU   c                 l    t        | j                  |      }t        j                  j	                  |      S r   )hasattrr   r   ConstantVariablecreate)r   txr   results       rV   call_obj_hasattrz"BaseTorchVariable.call_obj_hasattr!  s)    T*))0088rU   c                     | j                   t        v ry| j                   t        j                  j                  u rt
        j                  ryt        | j                   dd       dk(  S )NTr   math)r   constant_fold_functionsr;   autograd_profiler_enabledr   'constant_fold_autograd_profiler_enabledgetattrr   s    rV   can_constant_fold_throughz+BaseTorchVariable.can_constant_fold_through%  sP    ::00 JJ%..:::>> tzz<6&@@rU   r9   N)r   r   r   r8   )r   r   __qualname____doc__classmethodr   r   r   r   r   r   r   __classcell__r   s   @rV   rp   rp      s7    R) )(
9ArU   rp   c                   V     e Zd ZdZdefdZed        Zdddee	   dd	dd
f fdZ
 xZS )TorchCtxManagerClassVariablezLPoints to a context manager class in torch.* that dynamo has implementationsr9   c                 "    d| j                    dS )NzTorchCtxManagerClassVariable()r   r   s    rV   __repr__z%TorchCtxManagerClassVariable.__repr__B  s    .tzzl!<<rU   c                 \    t        |       } t        |       xr t        |       xr | t        v S r   )r$   callabler    supported_ctx_manager_classesr   s    rV   is_matching_clsz,TorchCtxManagerClassVariable.is_matching_clsE  s6     "%( UO  ;::	
rU   r   r8   argsr   dict[str, VariableTracker]r(   c           
         ddl m}m}m}m}m}m}	m}
m}m	}m
}m}m}m} | j                  t        j                   u rlt#        |      dk(  rLt%        |d   t&        j(                  j*                        r%|
j-                  |d      }|j/                  |||      S |
j-                  |d      S | j                  t        j0                  u rlt#        |      dk(  rLt%        |d   t&        j(                  j*                        r%|
j-                  |d      }|j/                  |||      S |
j-                  |d      S | j                  t        j2                  u r3t#        |      dk(  r%|
j-                  ||d   j5                         d      S | j                  t        j6                  u rSt#        |      dk  rt#        |      dk(  sJ t#        |      dk(  r|d   j5                         nd}|j-                  ||      S | j                  t        j8                  j:                  j<                  t        j8                  j:                  j<                  j>                  fv rCt#        |      dk  rt#        |      dk(  sJ  ||d   j5                         | j@                        S tC        jD                  | j                        rZtG        | j                  t        jH                        r6ddl%m&}  ||||jN                  jQ                  d	| j                  d
i             S | j                  t        jR                  jT                  jV                  t        jX                  jR                  jV                  t        jZ                  jR                  jV                  fv r!t]        j,                  | j                  ||      S | j                  t        j^                  j`                  t        j^                  jb                  t        jd                  j^                  j`                  t        jd                  j^                  jb                  fv r%tg        th        d| j                         tk               S | j                  t        jl                  jn                  u s&| j                  t        jl                  jp                  u rA|s|rJ ts        j,                  || j                  t        jl                  jn                  u       S | j                  t        jt                  jv                  jx                  u r"t#        |      dk(  sJ |j-                  ||      S | j                  t        jt                  jz                  j|                  u r!t#        |      dk(  sJ |j-                  |      S | j                  t        jd                  j~                  j                  u r9t#        |      dk(  sJ |j-                  ||D cg c]  }t        |       c}      S | j                  t        jd                  j~                  j                  u r!t#        |      dk(  sJ |j-                  |      S | j                  t        jt                  jz                  j                  u r!t#        |      dk(  sJ |j-                  |      S | j                  t        jt                  jz                  j                  u r9t#        |      dk(  sJ |	j-                  ||D cg c]  }t        |       c}      S | j                  t        jd                  j                  j                  u r3t#        |      dk(  sJ |j-                  ||d   j5                               S t        ]| j                  t        j                  j                  u r7t#        |      dk(  sJ |j-                  ||d   |d   j5                               S | j                  t        j                  j                  j                  j>                  u r\t        | j                  || j@                  ||      }|d   j5                         }|d   j5                         }|j-                  |||      S t        | ]  |||      S c c}w c c}w )Nr%   )!DisabledSavedTensorsHooksVariableDualLevelContextManager&FSDPParamGroupUseTrainingStateVariableFxTracebackAnnotateVariable&GradIncrementNestingCtxManagerVariable)GradInplaceRequiresGradCtxManagerVariableGradModeVariableInferenceModeVariable%JvpIncrementNestingCtxManagerVariableSDPAKernelVariableSetFwdGradEnabledContextManagerStreamVariable&VmapIncrementNestingCtxManagerVariabler   FT)initializedrs   )wrap_fx_proxy_clscall_functionrT   z$Profiler function %s will be ignored)only_subclassr   backendsset_priority)O r   r   r   r   r   r   r   r   r   r   r   r   r   r   r;   no_gradlenr~   r   	functionsBaseUserFunctionVariabler   r   enable_gradset_grad_enabledr   inference_moderA   	tracebackannotate__wrapped__rt   ru   rv   
issubclassStreamtorch._dynamo.variables.builderr   outputcreate_proxyampautocast_modeautocastcudacpur)   profilerprofilerecord_functionr   r	   logr*   r?   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_hooksr7   FSDPParamGroupuse_training_staterP   	attentionsdpa_kernelr/   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ctxinf_moder   xname_to_arg_mapr   r   r   s                           rV   r   z*TorchCtxManagerClassVariable.call_functionU  s   	
 	
 	
 	
  ::&4yA~*Q,,EE# '--b%8((T6::'..r599ZZ5,,,4yA~*Q,,EE# '--b$7((T6::#**2t44ZZ5111c$i1n#**D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I$		&&#JJ		 	 ZZII##,,JJNN##IIMM""
 
 (..tzz4HHZZ NN""NN**NN##++NN##33
 
 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;;DG..0  )

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

O 'z2EEGH*>:MMOL%,,R<HHw$Rv66U 0 0s   a
a!
)r   r   r   r   strr   staticmethodr   r   r(   r   r   r   s   @rV   r   r   ?  sa    V=# = 
 
R7#R7 'R7 -	R7
 
R7 R7rU   r   c                        e Zd ZdZdd fdZdefdZd Zee	j                  d               Zddd	ee   d
dddfdZddd	ee   d
dddfdZddZedd       Zedd       Zd Zd Zd Zd Zd Zd Z xZS )TorchInGraphFunctionVariablez@Points to a torch function/method that should be put in FX graphr9   c                 T    t        |   |fi | ddlm} | ||      }|| _        y )Nr   )is_nonstrict_trace_callable)r   r   trace_rulesr  nonstrict_traceable)r   r   r  r   r  r   s        rV   r   z%TorchInGraphFunctionVariable.__init__  s1    )&)=&"=e"D#6 rU   c                 <    d| j                    d| j                   dS )NzTorchInGraphFunctionVariable(z, nonstrict_traceable=r   )r   r  r   s    rV   r   z%TorchInGraphFunctionVariable.__repr__  s%    .tzzl:PQUQiQiPjjkllrU   c                     | j                   S r   r   r   s    rV   get_functionz)TorchInGraphFunctionVariable.get_function  r   rU   c                  P  BCDEFGHIJK i IIfd} ddl m} ddlmBmCmDmEmFmGm	H ddl
mJmK  | t                	 	 dGBfd       } | t         	 	 dGd       } | t        j                   j"                  j$                        	 	 dGd	       } | t        j&                  j(                  j*                  j,                        dGd
       } | t.        j0                        dGd       }t3        t.        d      r | t.        j4                        dGd       } | t        j6                        dGd       } | t        j8                  t        j                   j:                        dGBHfd       }	 | t        j<                  t        j>                        dGBfd       }
 | t        j@                        dGBfd       } | t        jB                        dGd       } | tD         dGd       } | t        jF                  jH                  jJ                  jL                  t        jF                  jH                  jJ                  jN                  t        jF                  jH                  jJ                  jP                  t        jF                  jH                  jJ                  jR                  t        jF                  jH                  jJ                  jT                        dGd       } | t        jV                        BDfd       } | t        jX                        	 dH	 dGCfd       } | t        jZ                        BCfd       } | t        j\                  j^                        Bfd       } | t        j\                  j`                        Bfd       } | t        j                   jb                  t        j                   jd                  t        j                   jf                        dGBfd       } | th        jk                  d tm               D               dGEfd       } | t        jn                        dGGKfd       } | t        jp                  jr                        dGd       } | t        jt                  jv                  jx                        	 	 dGBfd       } | t        jJ                  jz                  j|                        dGd        } | t        jF                  j~                        dGd!       } | t        j&                  j                  j                  t        j&                  j                  j                  j                        dId#       } | t        j&                  j                  j                  t        j&                  j                  j                  j                        dId$       } | t        j                        dGd%       } | t        j                        d&        } | t        j                        	 	 dGd'       } | t        j                        dGd(       }  | t        j                        dGBfd)       }! | |      dGJfd*       }"t        j                         rEdd+lKmL}#mM}$mN}%mO}&mP}' dd,lQmR}(  | |#|$|%|'|&      	 	 dGd-       }) | |(j                        dGBJfd.       }* | t        j                  j                        	 dId"d/	 dGd0       }+ | t        jF                  j                  j                        dGd1       }, | t        j                  j                  j                  j                        dGFfd2       }- | t        j                  j                  j                  j                        dGFfd3       }. | t        j                  j                  j                  j                        dGFfd4       }/ | t        j                  j                  j                  j                        dGFfd5       }0 | t        j                  j                  j                  j                        dGFfd6       }1 | t        j                  j                  j                  j                        dGFfd7       }2 | t        j                  j                  j                  j                        dGFfd8       }3 | t        j                  j                  j                  j                        dGFfd9       }4 | t        j                  j                  j                  j                        dGFfd:       }5 | t        j\                  j                  j                        	 	 dGd;       }6 | t        j\                  j                  j                        	 	 dGHfd<       }7 | t        j                  j                  j                        	 	 dGd=       }8 | t        j                        dGFfd>       }9 | t        j\                  j                        	 	 dGd?       }: | t        j\                  j                        	 	 dGBfd@       }; | t        j\                  j                        	 	 dGBfdA       }< | t        j\                  j                        dGdB       }= | t        j                  j$                        dC        }> | t        j                  j                  t        j                  j                        dGdD       }? | t        j                        	 	 dGBfdE       }@ | t        j                        dGBJfdF       }AIS )JzBuild a dict from function -> method to handle it so that we are O(1)
        in terms of the number of function with special handling.c                  6      fd}t         d         sJ |S )Nc                 6    D ]  }|vsJ |       | |<    | S r   rT   )handlerfnfnshandlerss     rV   	_registerzOTorchInGraphFunctionVariable._get_handlers.<locals>.register.<locals>._register  s5     +BX-1r1-#*HRL+ rU   r   )r   )r  r  r  s   ` rV   registerz<TorchInGraphFunctionVariable._get_handlers.<locals>.register  s"     CF###rU   r   )
SDPAParamsr%   )r   DeterministicAlgorithmsVariabler   StreamContextVariableSymNodeVariableTensorVariableUserDefinedObjectVariable)wrap_fx_proxyr   c                    |s|rJ | j                   t        j                  j                  t        j                  j
                  j                  t        j                  j                  t        j                  j                  t        j                  j                  t        j                  j                  j                  fv r|j                          j                  t               | j                            S r   )r   r;   rJ   rI   rG   rH   rK   rL   rM   rN   rO   mark_inconsistent_side_effectsr   rW   r   r   r   r   r   s       rV   handle_tracing_state_functionszRTorchInGraphFunctionVariable._get_handlers.<locals>.handle_tracing_state_functions  s     F**zz)),,99++22++((@@  113#**+B+DTZZ+PQQrU   c                    |rJ | j                   t        j                  j                  u rt	        |      dk(  sJ |d   j                         sJ |d   j                  j                  j                  d   }| j                  |      }t        |t        j                  j                        r|t        j                  j                  t        j                  j                  j                        z
  t        j                  j                  t        j                  j                  j                        z
  }t         j#                  |      S |rJ t         j#                  | j                               S )Nr%   r   example_value)r   r;   r?   _dispatch_keysr   	is_tensorproxynodemetar~   _subclasses
FakeTensorDispatchKeySetDispatchKeyPythonPythonTLSSnapshotDispatchKeySetVariabler   )r   r   r   r   r,  dkss         rV   !handle_dispatch_key_set_functionszUTorchInGraphFunctionVariable._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((11%((2F2F2M2MNO((11!HH00BB  .44S99x-44TZZ\BBrU   c                 f    t        j                  |t        j                  j	                               S r   )r(   buildr;   	overridesget_default_nowrap_functionsr   r   r   r   s       rV   #handle_get_default_nowrap_functionszWTorchInGraphFunctionVariable._get_handlers.<locals>.handle_get_default_nowrap_functionsF  s)     #((EOO@@B rU   c                 l    |j                  t        j                  |t        j                        ||      S r   )inline_user_function_returnr(   r<  r   accumulate_gradr?  s       rV   handle_accumulate_grad_zKTorchInGraphFunctionVariable._get_handlers.<locals>.handle_accumulate_grad_R  s/    11%%b)*C*CDdF rU   c                     t        ||      s5|j                  t        j                  |t        j
                        ||      S y r   )r   rB  r(   r<  r   radiansr?  s       rV   handle_radianszBTorchInGraphFunctionVariable._get_handlers.<locals>.handle_radiansX  s>    0v>55#))"i.?.?@$  ?rU   fmac                     t        |      dk7  s|ry t        d |D              r5|\  }}}t        t        j                        }|j                  ||||gi       S y )N   c              3   <   K   | ]  }|j                           y wr   )r.  ).0args     rV   	<genexpr>zQTorchInGraphFunctionVariable._get_handlers.<locals>.handle_fma.<locals>.<genexpr>g  s     73s}}7   )r   allr  r;   addcmulr   )r   r   r   r   r
  yz
addcmul_fns           rV   
handle_fmaz>TorchInGraphFunctionVariable._get_handlers.<locals>.handle_fmab  sY    t9>V7$77"GAq!!=emm!LJ%33BAq	2FF rU   c                 b    t        dddg t        j                  t        j                         y )Nz:Encountered torch.is_inference_mode_enabled during tracingr   z2torch.is_inference_mode_enabled() is not supportedgb_typecontextexplanationhints)r   r   FUNDAMENTALINFERENCE_MODE)r   r   s     rV    handle_is_inference_mode_enabledzTTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_inference_mode_enabledo  s4    TP&22&55	rU   c                     |j                         sH| j                  t        j                  j                  u r3t        |      r't        |j                  d      rj                  d      S j                  d      S )N__torch_function__TF)r.  r   r;   r=  is_tensor_liker~   r   r   )r   r   rM  r   r%  s      rV   handle_is_tensorzDTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_tensor{  s^    }}

eoo<<<s$=>CII';<'..t44'..u55rU   c                 x   |}|j                         r|j                  | j                  t        j                  u r%j                  |j                  j                        S | j                  t        j                  u r%j                  |j                  j                        S t        d| j                         y y )Nzcalling )r.  dtyper   r;   is_floating_pointr   
is_complexAssertionError)r   r   input	input_argr   s       rV   handle_is_floating_pointzLTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_floating_point  s    
 I""$)D::!8!88+229??3T3TUUZZ5#3#33+229??3M3MNN(8DJJ<)@AA *E$rU   c                     |j                         r4|j                         r$j                  t        |j                              S |j                         r|j                  |dg i       S y )Nnumel)r.  
valid_sizer   r"   sizecall_method)r   r   rh  r   s      rV   handle_numelz@TorchInGraphFunctionVariable._get_handlers.<locals>.handle_numel  sX     U%5%5%7'..wuzz/BCC"((Wb"== #rU   c                 v    t        |      dk(  r|d   S t        dd| d| ddgt        j                         y )	Nr%   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  )r   r   r   SUPPORTABLEr?  s       rV   handle_torch_compilezHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_compile  sJ    4yA~Aw:vYvh7lK&22	rU   c                 N    |j                         sJ |j                  |dg i       S Nrn  )r.  ro  )r   r   rh  s      rV   handle_tensor_size_rewriteszOTorchInGraphFunctionVariable._get_handlers.<locals>.handle_tensor_size_rewrites  s)    ??$$$$$RR88rU   c                 (    | j                  |||      S r   )_call_ntupler?  s       rV   handle_ntuplezATorchInGraphFunctionVariable._get_handlers.<locals>.handle_ntuple  s     $$Rv66rU   c                 t    t        j                         j                  t        j                               S r   )r   _guards_singletonr   r;   is_grad_enabled)r   r   r   r   s     rV   handle_is_grad_enabledzJTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_grad_enabled  s,    *<<=#**5+@+@+BCCrU   c                     |r6|j                         r&t        dd| d| ddgt        j                         j	                  ||j                               S )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   rt  r   )r   r   mode	warn_onlyr!  s       rV   #handle_use_deterministic_algorithmszWTorchInGraphFunctionVariable._get_handlers.<locals>.handle_use_deterministic_algorithms  se    
 Y99;a#D6i[A ?e*66	 399"d>U>U>WXXrU   c                 t    t        j                         j                  t        j                               S r   )r   r}  r   r;   $are_deterministic_algorithms_enabled)r   r   r   r!  s     rV   +handle_are_deterministic_algorithms_enabledz_TorchInGraphFunctionVariable._get_handlers.<locals>.handle_are_deterministic_algorithms_enabled  s,    9KKL#**5+U+U+WXXrU   c                     t        t        j                         j                  |j                  j
                        S r   )r   r+   r}  r   symbolic_torch_function_statetorch_function_subclass_enabledr   r   r   s     rV    handle_is_torch_function_enabledzTTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_torch_function_enabled  s6    6HHI $**00PP rU   c                     t        t        j                         j                  |j                  j
                         S r   )r   r+   r}  r   r  torch_function_mode_enabledr  s     rV   %handle_is_torch_function_all_disabledzYTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_torch_function_all_disabled  s7    6HHI#**44PPP rU   c                     t        |      dk(  r't        |d   t              r|d   j                  |      n|}j	                  t        d |D                    S )Nr%   r   c              3   2   K   | ]  }t        |        y wr   )r   rL  r
  s     rV   rN  z`TorchInGraphFunctionVariable._get_handlers.<locals>.handle_has_torch_function.<locals>.<genexpr>  s     9a&q)9s   )r   r~   r2   unpack_var_sequencer   any)r   r   r   elemsr   s       rV   handle_has_torch_functionzMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_has_torch_function  s\     t9>ja-&H Q++B/ 
 $**9599 rU   c              3   :   K   | ]  \  }}|j                     y wr   )stream)rL  r   device_interfaces      rV   rN  z=TorchInGraphFunctionVariable._get_handlers.<locals>.<genexpr>  s#      'A' !''s   c                 (    j                  ||      S r   )r   )r   r   r  r"  s      rV   handle_device_interface_streamzRTorchInGraphFunctionVariable._get_handlers.<locals>.handle_device_interface_stream  s     )//F;;rU   c                 2   t         j                  s!t        ddt         j                   ddg       t        s t        dddd	gt        j
                          | |j                  j                  d
t        j                  gt        |i        d       S )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 unavailabler   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_ERRORr   r   r;   	as_tensorr#   )r   r   r   r$  r   s      rV   handle_from_numpyzETorchInGraphFunctionVariable._get_handlers.<locals>.handle_from_numpy  s    %%c*6+=+=*>?M O
 G bS*55	 %),bii,,#OO 'tR0
 #	 	rU   c                     |S r   rT   )r   r   the_type	the_values       rV   handle_jit_annotatezGTorchInGraphFunctionVariable._get_handlers.<locals>.handle_jit_annotate!  s    rU   c                    |rJ d       |j                         sJ d       t        j                  d|j                  |j                        }j                  t        j                  j                  j                  |            S )Nz%Expect 1 input to cudnn.is_acceptablez2Expect input to cudnn.is_acceptable to be a tensorr   )rd  device)	r.  r;   tensorrd  r  r   r   cudnnis_acceptable)r   r   r  extra
tensor_inpr   s        rV   handle_cudnn_is_acceptablezNTorchInGraphFunctionVariable._get_handlers.<locals>.handle_cudnn_is_acceptable%  sv     EEE9##% D% av||FMMRJ#**$$22:> rU   c                 H    t        j                  j                  |g|i |S r   )r   BackwardHookVariabler   r?  s       rV   handle_backward_hookzHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_backward_hook7  s#    1188MdMfMMrU   c                 .     | j                   |g|i |S r   )call_nn_parameterr?  s       rV   handle_parameterzDTorchInGraphFunctionVariable._get_handlers.<locals>.handle_parameter;  s    )4))">t>v>>rU   Nc                 2    ||j                  |d|gi       S y rw  ro  self_r   r   dims       rV   handle_sym_sizezCTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_size?  s'     ''FSE2>> rU   c                 2    ||j                  |d|gi       S y )Nstrider  r  s       rV   handle_sym_stridezETorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_strideE  s%    ''HseR@@ rU   c                 b   t        |      dk(  rd|v rt        |      dk(  rt        t        j                        j	                  |g |dd  i       }t        t        j
                        j	                  |||d   gi       }t        t        j                        j	                  ||d   |gi       S y y y )NrJ  r   r%   r   )r   r  r;   r[   r   rZ   rX   )r   r   r   r   r   s        rV   handle_addcdivzBTorchInGraphFunctionVariable._get_handlers.<locals>.handle_addcdivJ  s    4yA~'V"3Fq8H 6eii@NN$qr(R 6eii@NN12 4EII>LLa&)2  9I"3~rU   c                     |j                         r@t        t        j                        j	                  ||g|      }|j                  |d|gi       S y )Nfill_)r.  r  r;   ri   r   ro  )r   r   rn  
fill_valuer   empty_results         rV   handle_fullz?TorchInGraphFunctionVariable._get_handlers.<locals>.handle_fullY  sR    ##%  <EKKHVV  $//Gj\2NN &rU   c                     t        |      dk(  rLt        |d   t              s8|s5|j                  t	        j
                  |t        j                        ||      S y y y )NrJ  r   )r   r~   r1   rB  r(   r<  r   foreach_lerp_inplacer   r   r   r   s       rV   "handle_inplace_foreach_lerp_scalarzVTorchInGraphFunctionVariable._get_handlers.<locals>.handle_inplace_foreach_lerp_scalard  sZ     4yA~ja,&GPV55#))"i.L.LM  QW&G~rU   c                     t        |      dk(  rL|d   j                         r8|s5|j                  t        j                  |t
        j                        ||      S y y y )Nr   r   )r   r.  rB  r(   r<  r   foreach_pow_scalarr  s       rV   handle_foreach_pow_scalarzMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_foreach_pow_scalaro  sZ     4yA~$q'"3"3"5f55#))"i.J.JK  ?E"5~rU   c                     |j                         r|j                         s*t        |t        j                        r|j                         r d       S y y r   )is_python_constantr   r~   r   r#  evaluate_expr)r   r   	conditionmessager   s       rV   handle_assertzATorchInGraphFunctionVariable._get_handlers.<locals>.handle_assertz  sL    ,,.93O3O3Q9i&?&?@++-'-- . ArU   c           
           | |j                   j                  dt        j                  j                  gt        ||       |      S )Nr   )r/  
param_vars)r   r   r;   r?   _SDPAParamsr#   )r   r   r   r   r&  s       rV   handle_sdpa_paramszFTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sdpa_params  sM     ,bii,,#HH(( 'tV4
   rU   )_get_group_size_by_name_get_group_tag_rank_not_in_group$_resolve_group_name_by_ranks_and_tagget_process_group_ranks)DTensorc                    t        |      dk(  r(t        |d   t              sf|d   j                         sSJ t        |      dk(  r(t        |d   t              r|d   j                         sJ t        d| d| j                         |D cg c]  }|j                          }} | j                  | }t        j                  ||      S c c}w )Nr%   r   r   zInvalid group value (z) for constant pg function )
r   r~   r.   r  r1   rg  r   r   r(   r<  )r   r   r   rM  args_as_valueinvocation_results         rV   &handle_constant_processgroup_functionszZTorchInGraphFunctionVariable._get_handlers.<locals>.handle_constant_processgroup_functions  s     t9> #47,@A75578 Y!^ #47L9 G6689 )/v 6$$(JJ<1  FJ Jc!7!7!9 J J$.DJJ$>!
 ',,R1BCC !Ks   C
c           
      $    |j                  d      }|t        |      dk\  r|d   }|j                  d       }nEt        |t        j
                        r+t	        j                  t              j                  ||gi       }t        |      }t        |      dk\  r||d<   n|j                  d      ||d<   |dd  D cg c]  }|j                          c}|j                         D ci c]  \  }}|dvr||j                          c}}dD ci c]  }||v s|||    }	}d fd	}
d j                  j                  z   |
_         | |j                  j                  d|
gt!        |d	   g|	       
      S c c}w c c}}w c c}w )N
placementsrJ  r   r%   shaper  c                 :     j                   | gi ||dS )Nr  r   )r
  r  r  r  kwargs_as_valuer   s      rV   fn_with_prim_typeszaTorchInGraphFunctionVariable._get_handlers.<locals>.handle_from_local.<locals>.fn_with_prim_types  s4    %4::)-<DIRX rU   zprim r   r   r   r/  )NN)getr   r   r~   r   r%  BuiltinVariabletupler   listr   itemsr   r   r   r   r#   )r   r   r   r   placements_vtnew_argsr
  kvkwargs_to_be_proxiedr  r  r  r   r&  s   `          @@rV   handle_from_localzETorchInGraphFunctionVariable._get_handlers.<locals>.handle_from_local  s    !'

< 8 (SY!^$(GM ($4$;$;D$AMy/R/RS$-$=$=e$D$R$R]OR%M  :x=A%"/HQKZZ-9+8F<(AI!" NA!5!5!7 N !'#1 33 q++--# +>(%&fAvayL($ ( /6

8K8K.K"+$0"))00'* +!!WI0
 
' !O#(s    F-F	FF)layoutc                    ddl m} |rB|j                  t        j                        r#t        dd| ddgt        j                         t        ||      s$t        dd	| dd
gt        j                         y y )Nr%   )BaseListVariablez%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.)
listsr  is_constant_matchr;   stridedr   r   rt  r~   r  )r   r   tensor_listr  r   r   r  s          rV   handle_nested_tensorzHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_nested_tensor  s     0&225==AC%fX. ?5*66	 k+;<R*;-8 ?A*55	 =rU   c                     t        |      t        |      z   dk(  s"t        |      dk(  r,|d   j                  d      rt        dd| d| ddg	       y y y )
Nr%   r   zOAttempted to use `torch.nn.functional.one_hot` with data-dependent output shaperr  rs  r  zuExplicitly set the `num_classes` param of the function call `torch.nn.functional.one_hot` to something other than -1.rW  )r   r  r   r?  s       rV   handle_one_hotzBTorchInGraphFunctionVariable._get_handlers.<locals>.handle_one_hot  sd    4y3v;&!+D	Q47#<#<R#@m#D66(; ?T	 $ArU   c                     t        |      rZt        j                  j                  t        j
                  j                  j                  j                  |j                              S |j                         r|S y r   )r~   r   r   r   r;   rA   experimentalsymbolic_shapesguard_size_oblivioussym_numr  r   r   exprr#  s      rV   handle_guard_size_obliviouszOTorchInGraphFunctionVariable._get_handlers.<locals>.handle_guard_size_oblivious$  se    $0 !1188HH))99NN 
 ((* +rU   c                     t        |      rZt        j                  j                  t        j
                  j                  j                  j                  |j                              S |j                         r|S y r   )r~   r   r   r   r;   rA   r  r   guard_or_truer  r  r  s      rV   handle_guard_or_truezHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_guard_or_true1  sb    $0 !1188HH))99GGU  ((* +rU   c                     t        |      rZt        j                  j                  t        j
                  j                  j                  j                  |j                              S |j                         r|S y r   )r~   r   r   r   r;   rA   r  r   guard_or_falser  r  r  s      rV   handle_guard_or_falsezITorchInGraphFunctionVariable._get_handlers.<locals>.handle_guard_or_false<  sb    $0 !1188HH))99HHV  ((* +rU   c                     t        |      rZt        j                  j                  t        j
                  j                  j                  j                  |j                              S |j                         r|S y r   )r~   r   r   r   r;   rA   r  r   statically_known_falser  r  r  s      rV   handle_statically_known_falsezQTorchInGraphFunctionVariable._get_handlers.<locals>.handle_statically_known_falseG  sc    $0 1188HH))99PP 
 ((* +rU   c                 <   t        |      r|j                  }n3|j                         r|j                         }nt	        dd| dg        t
        j                  j                  t        j                  j                  j                  j                              S )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   r   r   r   r;   rA   r  r   guard_scalarr   r   r  valr#  s       rV   r  z@TorchInGraphFunctionVariable._get_handlers.<locals>.guard_scalarR  s    $0ll((*--/e$TFO X	 --44%%55BB3G rU   c                     t        |      rZt        j                  j                  t        j
                  j                  j                  j                  |j                              S |j                         r|S y r   )r~   r   r   r   r;   rA   r  r   statically_known_truer  r  r  s      rV   handle_statically_known_truezPTorchInGraphFunctionVariable._get_handlers.<locals>.handle_statically_known_trued  sc    $0 1188HH))99OO 
 ((* +rU   c                     t        fd|D              rIj                  |t        j                  j                  j
                  j                  d |D         d       S y )Nc              3   6   K   | ]  }t        |        y wr   r~   rL  r
  r#  s     rV   rN  zUTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_and.<locals>.<genexpr>q       Aa:a1A   c              3   <   K   | ]  }|j                           y wr   r   r  s     rV   rN  zUTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_and.<locals>.<genexpr>u       61!**,6rO  r  )rP  r   r;   rA   r  r   sym_andr   r   termsr#  s      rV   handle_sym_andzBTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_ando  s]    A5AA&--HH))99AA66 ! .   BrU   c                     t        fd|D              rIj                  |t        j                  j                  j
                  j                  d |D         d       S y )Nc              3   6   K   | ]  }t        |        y wr   r  r  s     rV   rN  zTTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_or.<locals>.<genexpr>|  r  r  c              3   <   K   | ]  }|j                           y wr   r  r  s     rV   rN  zTTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_or.<locals>.<genexpr>  r  rO  r  )rP  r   r;   rA   r  r   sym_orr!  s      rV   handle_sym_orzATorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_orz  s]    A5AA&--HH))99@@66 ! .   BrU   c                    t        |      r|j                  }n"|j                         r|j                         }ny t        j
                  j                  t        j                  j                  j                  j                  |            S r   )r~   r  r  r   r   r   r   r;   rA   r  r   has_static_valuer  s       rV   handle_has_static_valuezKTorchInGraphFunctionVariable._get_handlers.<locals>.handle_has_static_value  sj    $0ll((*--/--44%%55FFsK rU   c                 J    ddl m} t        |      j                  |g ||      S )Nr   )_unsafe_set_version_counter)tensor_version_opr-  r  r   )r   r   r   r   r-  s        rV   !handle_unsafe_set_version_counterzUTorchInGraphFunctionVariable._get_handlers.<locals>.handle_unsafe_set_version_counter  s*     H/+mB$01rU   c                 `     t         j                  j                  j                               S r   )r;   r?   r   peek_interpreter_stack)r   r   r   r   r%  s       rV   'handle_functorch_peek_interpreter_stackz[TorchInGraphFunctionVariable._get_handlers.<locals>.handle_functorch_peek_interpreter_stack  s(     -##::< rU   c                     |d   j                   }t        t        j                  j                  j                  |            S Nr   )r   FuncTorchInterpreterVariabler;   r   pyfunctorchcoerce_cinterpreter)r   r   r   r   cinterpreters        rV   0handle_functorch_pyfunctorch_coerce_cinterpreterzdTorchInGraphFunctionVariable._get_handlers.<locals>.handle_functorch_pyfunctorch_coerce_cinterpreter  s8      7==L/  ,,@@N rU   c                     fdd }|r|d   }n	d|v r|d   }|P|j                         s? |      r6t        t        j                  j                        j                  |g ||      S y y y )Nc                     | j                         st        |       ryt        | t        t        f      rt	        fd| j
                  D              S y)NTc              3   .   K   | ]  } |        y wr   rT   )rL  rR  check_any_unspecs     rV   rN  ztTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_tensor.<locals>.check_any_unspec.<locals>.<genexpr>  s     Dq/2Ds   F)r.  r~   r1   r2   r  r  )r
  r#  r=  s    rV   r=  zaTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_tensor.<locals>.check_any_unspec  sA    ;;=Jq/$BL-#@ADAGGDDD !rU   r   data)r.  r  r;   _refsr  r   )r   r   r   r   data_argr=  r#  s        @rV   handle_torch_tensorzGTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_tensor  s    	! H76!!&> $ **,$X. 4EKK4F4FGUU$ 	 / - %rU   c                     |s|rJ |j                   j                  s t        ddddgt        j                         t        j                  |       |j                   j                         S )Nz5Attempted to pop from empty torch function mode stackr   zTCalled `torch._C._pop_torch_function_stack` when torch function mode stack is empty.z0Do not pop from empty torch function mode stack.rW  )r  
mode_stackr   r   r  r6   register_mutationpop_torch_function_moder?  s       rV   handle_pop_torch_functionzMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_pop_torch_function  sj     F**33>>S vJ*55	 +<<R@33KKMMrU   c                     t        |      dk7  s|rt        |dt        |       d       t        j                  |       |j                  j                  |d          j                  d       S )Nr%   z0push_torch_function takes exactly one argument ( given)r   )r   r&   r6   rD  r  push_torch_function_moder   r)  s       rV   handle_push_torch_functionzNTorchInGraphFunctionVariable._get_handlers.<locals>.handle_push_torch_function  sg     4yA~$Fs4ykQXY +<<R@,,EEd1gN#**400rU   c                     |s|rt        |d       j                  t        |j                  j                              S )Nz+len_torch_function_stack takes no arguments)r&   r   r   r  rC  r)  s       rV   handle_len_torch_functionzMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_len_torch_function  s<     v$R)VW#**B44??@ rU   c                     t        |      dk7  s|rt        |dt        |       d       |d   j                         }|dk\  r"|t        |j                  j                        k  sJ |j                  j                  |   S )Nr%   z2get_function_stack_at takes exactly one argument (rH  r   )r   r&   r   r  rC  )r   r   r   r   inds        rV   handle_get_stack_atzGTorchInGraphFunctionVariable._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CrU   c                    t        |      t        |      z   dkD  s|r)d|vr%t        dd| d| dg t        j                         	 |r|d   j	                         }n|r|d   j	                         }nd }t        j                  |      }=t        | j                        }t        |j                  t        j                               t        t               j                   j#                  dd      d         }t%        j&                  |||      S # t        $ r0}t        d	d| d| d
g t        j                  |       Y d }~d }~ww xY w)Nr%   r  z*improper torch.get_device_module argumentsrr  rs  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_excr   )maxsplitr  )r   r   r   r  r   r;   get_device_moduler   r   rt   r   rw   r   ID_MATCHr   r   r   rsplitr(   r<  )	r   r   r   r   r  moduleert   
new_sources	            rV   handle_get_device_modulezLTorchInGraphFunctionVariable._get_handlers.<locals>.handle_get_device_module  sM   4y3v;&*v(&:PH#D66(; ^*55	#H-@@BF!!W779F!F008 ~1$++>f//0E0EFG#&&sQ&7;J #((VZ@@+  L#D66(; c9-889 s   AD 	E&EEc                    t        |      t        |      z   dkD  s|r)d|vr%t        dd| d| dg t        j                         	 |r't	        j
                  |d   j                               }n+|r't	        j
                  |d   j                               }nd }|j                  j                  |      S # t        $ r0}t        d	d| d| d
g t        j                  |       Y d }~y d }~ww xY w)Nr%   r  z9unsupported arguments to torch.accelerator.current_streamrr  rs  zGtorch.accelerator.current_stream accepts one optional argument `device`rW  r   z7bad device argument to torch.accelerator.current_streamrQ  rR  )
r   r   r   r  r;   r  r   symbolic_stream_state
cur_streamr   )r   r   r   r   r  rY  s         rV   handle_current_streamzITorchInGraphFunctionVariable._get_handlers.<locals>.handle_current_stream-  s    4y3v;&*v(&:PW#D66(; i*55	"\\&*:*M*M*OPF"\\$q'*D*D*FGF!F//::6BB U#D66(; c9-889 s   A.B6 6	C/?&C**C/c                     t        j                  |       |d   j                         rt        j                  |       nt        j                  |       j                  d       S r4  )r6   rD  is_constant_noneclear_default_device!register_device_context_insertionr   r)  s       rV   handle_set_default_devicezMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_set_default_deviceJ  sQ     +<<R@Aw'').CCBG.PPQST#**400rU   c           	         d }d }|r|d   }|dd  }nd}|d|v r|j                  d      }|r|d   }nd|v r|j                  d      }|0 ||j                  j                  d| j                  di             S d }d }|vt	        |t
              r|j                         r*t        dt        |      d	d
dgt        j                         |j                         }|j                  j                  d|      }|j                         r1| j                  |j                         |       j                  d       S |j!                         }	||	f}
n|	|f}
 ||j                  j                  d| j                  |
i             S )Nr   r%   rT   condr  r   r  z)Can't extract message from torch._check()zThe second argument of torch._check() must be a functiondefined within the torch.compile regionthat 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)popr   r   r   r~   r0   has_closurer   r  r   rt  r  %register_static_attr_and_return_proxyr  r   r   r   )r   r   r   r   predicate_vt
message_vt	rest_argsmessage_eagermessage_graph_proxypredicate_proxy
proxy_argsr   r&  s              rV   handle_checkz@TorchInGraphFunctionVariable._get_handlers.<locals>.handle_check]  s   LJ#Aw H		#&(8%zz&1&q\
f$#ZZ	2
#$))00'

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

<::<mL'..t44*335O #*-/
-/BC
 ii,,#JJ	 rU   r   )Fr   )utorch.backends.cudar   r   r   r!  r   r"  r#  r$  r%  builderr&  r   rW   dispatch_key_set_functionsr;   r=  r>  r   opsinductoraccumulate_grad_defaultr   rF  r   rH  is_inference_mode_enabledr.  ra  re  rf  rl  compile!REWRITE_OPS_TO_TENSOR_SIZE_METHODrP   rQ   utils_single_pair_triple
_quadruple_ntupler~  use_deterministic_algorithmsr  r?   _is_torch_function_enabled_is_torch_function_all_disabledr   has_torch_function_variadichas_torch_function_unarydictfromkeysr   
from_numpyr<   r   r   r  r  hooksBackwardHook	Parameteratensym_sizeint
sym_strideaddcdivrj   _foreach_lerp__foreach_pow_assertr-   is_available"torch.distributed.distributed_c10dr  r  r  r  r  torch.distributed.tensorr  
from_localnestednested_tensor
functionalone_hotrA   r  r   r  r  r
  r  r  r  r   r'  r*  	_autogradr-  r   r1  r6  r7  r  _pop_torch_function_stack_push_on_torch_function_stack_len_torch_function_stack_get_function_stack_atrU  acceleratorcurrent_streamr   set_default_device_check)Lr  r   r*  r:  r@  rD  rG  rU  r^  rb  rj  rp  ru  rx  r{  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r#  r(  r+  r/  r2  r9  rA  rF  rJ  rL  rO  r[  r_  rd  rr  r   r!  r   r"  r#  r$  r%  r  r&  r   sL                                                                     @@@@@@@@@@rV   _get_handlersz*TorchInGraphFunctionVariable._get_handlers  s"
   
 	 	3	
 	
 	
 	>	*,	-	R-	R 
.	R  
-	.	C-	C 
/	C6 
%//>>JJ	K		-		 
L		 
%))$$55==	>	 
?	
 
$,,		 
 	 4dhh
  
 
%11	2		 
3		 
%//5??#A#A	B	6 
C	6 
##

	B	

	B 
%++		> 
	> 
%--	 	 
!	 
4	5	9 
6	9 
HH""**HH""((HH""**HH""--HH""**

	7

	7 
%''	(	D 
)	D 
%44	5?D	Y-	Y 
6	Y  
%<<	=	Y 
>	Y 
%((55	6	 
7	 
%((::	;	 
<	 
OO..OO77OO44


	


	 
]] +K+M 

	<

	< 
%""	# 	 
$ 	D 
%))$$	%	 
&	 
%..&&44	5	-	 
6	" 
%++##00	1	N 
2	N 
%(($$	%	? 
&	? 
%))..))599>>+B+B+F+F	G	? 
H	?
 
%))..++UYY^^-F-F-J-J	K	A 
L	A 
%--	 	 
!	 
%**		O 
	O 
%&&	'	*	 
(	 
%$$	%	 
&	 
%--	 	. 
!	. 
*			 
		 ++-  9'"'4D1DDB g(()2 *2h 
%,,,,	- 	
 	'	 
.	> 
%((%%--	.	 
/	 
%((''77LL	M
	 
N
	 
%((''77EE	F	 
G	 
%((''77FF	G	 
H	 
%((''77NN	O	 
P	 
%((''77DD	E	 
F	" 
%((''77MM	N	 
O	 
%((''77??	@	 
A	 
%((''77>>	?	 
@	 
%((''77HH	I	 
J	 
%(($$@@	A	1-	1 
B	1 
%((%%<<	=	-	 
>	 
%""..BB	C	-	 
D	 
%,,		 
 	< 
%((44	5	N-	N 
6	N" 
%((88	9
	1-
	1 
:
	1 
%((44	5	-	 
6	 
%((11	2	D 
3	D 
%))55	6'	A 
7'	AR 
%##22EJJ4M4M	N	 
O	8 
%**	+	1-	1 
,	1$ 
%,,	M	 
 M	^ rU   r   r8   r   r   r   r(   c                    ddl m}m ddlm} | j
                  r| j                  |||      S | j                  |||      rt        || ||      S | j                         rt        ||      r| j                  t        v rK| j                  J t        | j                        }t        |j!                  t"        j$                               	 |j'                   | j)                         |D cg c]  }|j)                          c}i |j+                         D 	ci c]  \  }}	||	j)                          c}	}      S | j=                         r:| j                  j>                  }|r|d   jA                         r|d   }|j                  tC        tD        jF                  jH                  |      rtK        tD        jF                  jH                  |      }tC        |d      rtC        ||jM                         d         rytD        jN                  jP                  tK        ||jM                         d         jR                  v r8tU        ddd	tW        | j                         d
g tX        jZ                  d       | j]                  |||      S | j_                         ja                  | j                        }|r || |g|i |}|r|S tc        fd|D              }te        fd|D              }tK        | j                  dd      dk(  r| j                  j>                  tf        v r{|ry|rwdti        | j                         d}tj        jm                  |       tU        dd| j                   d| d| dti        | j                         dg tX        jZ                         | j                  }|rSd| j                  j>                   }tK        | j                  dd       dk(  r tC        tD        |      rtK        tD        |      }d }d }d|v r|d   }to        |tp        tr        f      rhg }|j*                  D ]W  }|jA                         r2|ju                         jv                  jx                  d   jz                  }nd }|j}                  |       Y |jA                         r1|ju                         jv                  jx                  d   jz                  } || |j~                  j                  d|gt        ||             }|jA                         rKd|v rG|d   j)                         r4tU        d d| j                   d| d| d!d"d#gtX        jZ                         |-to        |t6              r	t        |j*                  |      D ]  \  }}|	|jA                         sJ |j                  jv                  jx                  d   }||jz                  k7  rPtU        d$d| j                   d| d| d%| j                   d&| d'|jz                   d
g tX        jZ                         tD        j                  j                  |      rtU        d(d)| j                   d| d| d!g tX        jZ                          |S ||jA                         sJ d|ju                         jv                  jx                  v sJ |ju                         jv                  jx                  d   }||jz                  k7  rPtU        d*d| j                   d| d| d%| j                   d&| d'|jz                   d
g tX        jZ                         tD        j                  j                  |      s2tU        d+d)| j                   d| d| d!g tX        jZ                         |S c c}w c c}	}w # t,        t.        t0        f$ rI}
t3        t5        |
      |t7        t9        |j&                  |
j:                                     Y d }
~
d }
~
ww xY w),Nr%   )r   r#  r&  )r   r   	overloadszInplace op on input tensorr   z6Attempted to trace an inplace view op on input tensor r   z/Ensure you do not modify input tensor in place.rW  c              3   6   K   | ]  }t        |        y wr   r  r  s     rV   rN  z=TorchInGraphFunctionVariable.call_function.<locals>.<genexpr>  s     &T!z!_'E&Tr  c              3   Z   K   | ]"  }t        |      xs |j                          $ y wr   )r~   r  r  s     rV   rN  z=TorchInGraphFunctionVariable.call_function.<locals>.<genexpr>  s.      !
IJJq/*Da.B.B.DD!
s   (+r   r;   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=rs  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   outr,  r   r  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)Gr   r   r#  rt  r&  r  "_call_nonstrict_traceable_functiontorch_function_override_enabledr4   r   r   r   #constant_fold_functions_need_guardsrt   r   r   rw   r   EQUALS_MATCHr   r   r  OverflowError	TypeError
ValueErrorr   typer  mapr   is_tensor_methodr   r.  r   r;   rv  r  r   r  Taginplace_viewtagsr   r'   r   rt  call_tensor_methodr  r  r  rP  bin_opsr  r   warningr~   r2   r1   r   r0  r1  r  appendr   r   r#   zipr/  _prims_commonis_contiguousis_contiguous_or_false)r   r   r   r   r   r&  rt   r
  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  tensor_variableout_tensor_vtsaved_out_shapefake_outr#  s                                @rV   r   z*TorchInGraphFunctionVariable.call_function  s    	8*##::2tVLL//D&A*2tT6BB))+0M&1
 zz@@{{...1$++>f//0I0IJK'..-D++-:>?Q!..0?AGPA1a2244P    "::&&DQ))+!!W
$$0WUYY^^T5R 6BK0#Bq(9:!II22"2r||~a'89>>? &$@$&*`ahimisisat`uuv(w#!2!>!># Q#	 **2tV<<,,.224::>$T2???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 e\2  F?!%=L ,(EF#% &,, 3B||~ " 2 2 7 7 H N N $$++E23 %%' ))+0055oFLL ! (("))(( #40
 %%'6)'::<[djj\ixH;D6 '22		 ' *D169 &&$7 $2M? '. !(22444,2277<<_MH&(..8 &$V&)$**WTF)F8$T">tzzl K::I9JJZ[c[i[iZjjk!m#!2!>!>#
 !..<<XF &$e&1$**WTF)TZS[$\(G#!2!>!>#	;$F ; $/L4J4J4L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 " X"-djj\iPVx X$C.::	 { @P "9j9 (Ic"2"9"9388DE s6   
^ )^ ^ ^
1	^ ^ _-$>_((_-c           
         dd l mc m} ddl m}m} ddlm ddlm} ddl	m
} ddlm}	 t        j                  |t        j                  ||      t        j                  ||      f      }
t!        j"                  |      j%                  ||
gi       }t'        |t              rt)        |j*                        dk(  sJ |j*                  \  }}t'        |t,              sJ |j*                  D ]I  }|j/                         } ||      r|j/                         j0                  }t3        d	d
| dddg       K |j*                  D cg c]  }|j5                          }}	 |j7                         }| jD                  fd} ||      \  }}|jF                  jI                  jJ                   d|      }|jF                  jI                  jJ                  dz         }tM        |      |jN                  _&        tM        |      |jN                  _&        ||g|}|jF                  jQ                  d||i       }	  |	||      }|S c c}w # |$ r}|j8                  j/                         }|j0                  }dd lmc m} |j?                  |      r-t3        dd| d| dd| ddgt@        jB                  |       n1t3        dd| d| dd| ddd| dgt@        jB                  |       Y d }~d }~ww xY w# tR        jT                  jV                  jX                  tR        jT                  jV                  jZ                  f$ r0 t3        d d!jJ                   d"g t@        jB                         Y |S w xY w)#Nr   )func_to_graphableis_graphable_type)fake_tensor_tls)tree_flattenr%   )#AsPythonConstantNotImplementedErrorr  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.rR  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`c                  f    j                   }d_         	  | i |}|_         |S # |_         w xY wNT)allow_non_fake_inputs_override)r   r   old_valresr  r  s       rV   
patched_fnzSTorchInGraphFunctionVariable._call_nonstrict_traceable_function.<locals>.patched_fn  sH     &DDG=AO:I$)&)AH>J BI>s   ' 	0_spec_input_specr   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.)."torch._higher_order_ops.flat_apply_higher_order_ops
flat_applyr  r  torch._subclasses.fake_tensorr  torch.utils._pytreer  baser  rt  r&  r2   r<  r,   r   UserFunctionVariabler   r~   r   r  r1   python_typer   r   r   r   r  r}  _pytreeis_constant_classr   rt  r   r   rj  r   r  r0  r   r;   rG   r  UnsupportedTorchRuntimeError)r   r   r   r   r  r  r  r  r  r&  packed_input_vtout_vtflat_args_vtsinput_spec_vtflat_arg_vtarg_type	type_nameproxified_flat_args
input_specrY  typpytreer  r   f_specf_spec_proxyinput_spec_proxyall_argsr/  r  r  s                                @@rV   r  z?TorchInGraphFunctionVariable._call_nonstrict_traceable_function  s    	@?	
 	B4=* (--$$R.0A0G0GF0ST
 //=KK!2
 &-0S5F!5KKK'-||$}-666 ).. 	K"..0H$X.'335BB	P9)BG/
G		. 7D6I6I
'2K  "
 
'	&99;JP ZZ
	 &j1	6 yyFF{{m5!6
 99JJKK-'

 "&f%)*%5" "2I5HI 		&&
HbQ	
 #2u-F" U
 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 +%	d MM))MM//	
 	 Q$R[[M2d 7)556 !	s2   II 7	K8 K5BK00K58A7M32M3c                    | j                   t        j                  j                  j                  j
                  u r|d   j                         n#| j                   j                  d   j                  t        t              sJ |rJ fd}| j                   t        j                  j                  j                  j
                  u rt        j                  |      S  ||d         S )z1inline behavior of torch.nn.modules.utils._ntupler   c                    | j                        r-t        j                  t        | j	                                    S | j                         rct        j                  j                  t        j                  j                  j                  j                        | j                                     S t        dd|  ddg       y )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   r2   r  r  r  r   r   r;   rP   rQ   r}  r  r   r   )r   countr   s    rV   r{  z@TorchInGraphFunctionVariable._call_ntuple.<locals>.handle_ntupleV  s    ,,R0 ..22267  ))+ 1188HH$$**2259%:R:R:TU  n$UG, ?T	rU   )r   r;   rP   rQ   r}  r  r   __closure__cell_contentsr~   r  r   LambdaVariable)r   r   r   r   r{  r   s    `   @rV   rz  z)TorchInGraphFunctionVariable._call_ntupleM  s    ::))//777G..0EJJ**1-;;E%%%%z	( ::))//777++M:: a))rU   c           
         |j                   r t        ddddgt        j                         t	        |t
        j                        r	 |j                         }||j                         s#t        d
d| ddgt        j                         |j                  r| j                  |||      S t        j                  r!t        dddddgt        j                         t	        |t              st!        |j"                        r(t        dt%        |      dg t        j                         t'               s t        ddddgt        j(                         	 t+        |j-                  |d      j                               }|j-                  |d      j                         }|j-                  |d      j                         }|j.                  j1                  t2        |g      }|j4                  r|j7                  |dg i       }ddlm}	  |	||j.                  j=                  dt>        |jA                         |jA                         fi       |j                         }
|
j                         sJ tB        jD                  jF                  |
_        d!|
_$        |
S # t        $ r' t        dd| ddgt        j                         Y lw xY w# t        $ r/}t        dd| ddgt        j                  |       Y d	}~=d	}~ww xY w)"z>A call to torch.nn.Parameter() gets lifted to before the graphz3Attempted to use `torch.nn.Parameter()` with exportr   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  rd  r  z8`torch.nn.Parameter` with non-constant Tensor attributeszBEnsure the Tensor argument's shape, dtype, and device are correct.rR  detachr%   r  r   rs   F)%exportr   r   rt  r~   r   r(   r   NotImplementedErrorr  r.  rt   _nn_param_via_prefix_insertr   graph_break_on_nn_param_ctorr5   r
   
class_typer  r   	DIFFICULTr  var_getattrr   synthetic_graph_inputr   r  ro  rt  r&  r   r   r   r;   rP   r  has_grad_fn)r   r   r>  r  r  rd  r  rY  placeholderr&  r   s              rV   r  z.TorchInGraphFunctionVariable.call_nn_parametero  s    99M;D&22	 mY%>%>? - @ @ B <t~~/KvUX&11	 ;;222t]KK..Y:ZV '22		 (
 0@^D	;&22	 1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.. # s ' 	[,]O< ?>*55	 	R # 
	Rv;X&11 	 	
	s+   J A)K ,KK	L $K;;L c                    | j                   j                         }| j                   j                  J t        | j                   j                        j	                  fd        |j
                          t        j                  |             j                  dd       j                  |       | j                   j                  j                  j                                |j                         j                  }|j                  dvr,t!        dd|j                   dg t"        j$                         t'        |      }t(        j*                  j-                  | j                   j/                  |j                         j                        |	      }t1        j2                  | ||      }|j5                         }t7        j8                         j:                  j<                  j?                  |       |S )
Nc                  (     j                  dd      S )Nztorch.nnr  )load_import_from)cgs   rV   <lambda>zJTorchInGraphFunctionVariable._nn_param_via_prefix_insert.<locals>.<lambda>  s    !4!4Z!M rU   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  ) r   new_varroot_txr   add_push_nullrt   r   r   r   storepregraph_bytecodeextendget_instructionsr   r0  opr   r   r  r   r;   rP   r  example_value_from_input_noder(   r<  realizer   r  guards_contextdynamo_guardsremove_guards_with_source)	r   r>  r  varname	data_nodert   r,  r   r  s	           @rV   r  z8TorchInGraphFunctionVariable._nn_param_via_prefix_insert  s    ))##% yy  ,,,ryy(()
MN
4;;
9%%m45
E"

		##**2+>+>+@AMMO((	<<::['	~6M&00	 &g.**II33DMMO4H4HI' + 
 !&&r=&A! 	++99SS	
 rU   c                 f    |d   j                  || j                         j                  |dd  |      S )Nr   r%   )ro  r  r   r?  s       rV   r  z/TorchInGraphFunctionVariable.call_tensor_method%  s2    Aw""2t'8'8':'C'CT!"XvVVrU   c                 ,   ddl m} t        j                  | j	                               xrQ t        | j	                         d      xr5 | j	                         j                  t        j                  j                  k(  xs | j	                          |       v S )Nr   )get_tensor_method__objclass__)
r  r'  ru   ismethoddescriptorr  r   r(  r;   r?   
TensorBase)r   r'  s     rV   r  z-TorchInGraphFunctionVariable.is_tensor_method(  s    3 &&t'8'8':; H))+^<H!!#00EHH4G4GG8  $5$77		8rU   c                     | j                         t               v xsL t        | j                         t        j                  j
                  t        j                  j                  f      xr t        |||      S r   )r  r_   r~   r;   r   r   r   r3   r?  s       rV   r  z<TorchInGraphFunctionVariable.torch_function_override_enabled1  sd    #<#>> !!#&&

(C(CD< *"dF;	<rU   c                      yr  rT   r   s    rV   is_python_hashablez/TorchInGraphFunctionVariable.is_python_hashable:  s    rU   c                 ,    t        | j                        S r   )hashr   r   s    rV   get_python_hashz,TorchInGraphFunctionVariable.get_python_hash=  s    DJJrU   c                 D    | j                         |j                         k(  S r   )r   )r   others     rV   is_python_equalz,TorchInGraphFunctionVariable.is_python_equal@  s    &&(E,D,D,FFFrU   r   r   r   r  )r   r   r   r   r   r  r   r  r  	functoolscacher  r   r(   r   r  rz  r   r  r  r  r  r  r-  r0  r3  r   r   s   @rV   r  r    s    J7m# m __p  pdx#x 'x -	x
 
xtb#b 'b -	b
 
bH *D I IV ' 'RW8< GrU   r  c                   f     e Zd ZdZed        Zed        Zd Zde	e
   deee
f   ddf fd	Z xZS )
r8  zrepresents torch.DispatchKeySetc                     t        | fi |S r   )r8  )r   r   s     rV   r   zDispatchKeySetVariable.createG  s    %e6v66rU   c                 f    t        |j                  t        j                                | ||      S rr   )r   rw   r   DISPATCH_KEY_SET_MATCHr   s      rV   r   z)DispatchKeySetVariable.create_with_sourceK  s(    f''(K(KLM5((rU   c                     |dk(  S )NhasrT   )r   r   s     rV   is_constant_fold_methodz.DispatchKeySetVariable.is_constant_fold_methodP  s    u}rU   r   r   r9   r(   c                    | j                  |      rt        ||      rt        | j                  |      }t        j
                  j                   ||D cg c]  }|j                          c}i |j                         D ci c]  \  }}||j                          c}}      S |dk(  r-t	        j                  | j                  j                               S t        	| 1  ||||      S c c}w c c}}w )NhighestPriorityTypeId)r<  r   r   r   r   r   r   r   r  EnumVariabler>  r   ro  )
r   r   r   r   r   methodr
  r  r  r   s
            rV   ro  z"DispatchKeySetVariable.call_methodS  s     ''-2O&3
 TZZ.F--446:;a**,;=C\\^LTQq!..00L  ,,))$***J*J*LMMw"2tT6:: <Ls   C*C/)r   r   r   r   r  r   r   r   r<  r  r(   r  r  ro  r   r   s   @rV   r8  r8  D  sh    )7 7 ) ); ?#	;
 S/)*; 
; ;rU   r8  c                   P     e Zd ZdZed        Zdee   dee	ef   ddf fdZ
 xZS )r5  z<represents torch._functorch.pyfunctorch.FuncTorchInterpreterc                 f    t        |j                  t        j                                | ||      S rr   )r   rw   r   rV  r   s      rV   r   z/FuncTorchInterpreterVariable.create_with_sourcel  s(    f''(=(=>?5((rU   r   r   r9   r(   c                    |dk(  r-t        j                  | j                  j                               S |dk(  rI|j	                  t        j                  || j                  j                  j                        | g|z   |      S |dv r8t         j                  j                   t        | j                  |                   S |dk(  r&|s|rJ t         j                  j                  |d       S t        | 9  ||||      S )Nkeyprocess)level
batch_size
randomnesslower)r   r?  r   rD  rB  r(   r<  rE  __func__r   r   r   0TemporarilyPopInterpreterStackCtxManagerVariabler   ro  )r   r   r   r   r   r   s        rV   ro  z(FuncTorchInterpreterVariable.call_methodq  s     5=))$**..*:;;Y11%%b$***<*<*E*EF 
 ::--445NWTZZ5N5PQQW_F**MMTTD  w"2tT6::rU   )r   r   r   r   r   r   r  r(   r  r  ro  r   r   s   @rV   r5  r5  i  sO    F) ); ?#	;
 S/)*; 
; ;rU   r5  )r   r4  ru   loggingr   r   collections.abcr   r   typingr   r   r   torch._Cr;   torch._refstorch.fxtorch.nntorch._guardsr   torch._loggingr	   torch.utils._python_dispatchr
   r   r   r   r   r   r   r   create_parameter_opr   r   r   r  r   r  r   r   guardsr   r   rt   r   r   r   r   r}  r   r   r   r    r!   r"   r#   r$   r  r&   r'   r(   ctx_managerr)   r*   r+   dictsr,   distributedr-   r.   r   r/   r0   r  r1   r2   torch_functionr3   r4   r5   r6   numpyr  ModuleNotFoundError#torch.distributed.fsdp._fully_shardr7   torch._dynamo.symbolic_convertr8   	getLoggerr   r   r  r  r   r   r   r   r   r   r   r?   r   r   r   r   r   r   r   r   r  r   r   r   	grad_moder   r   r   r   r  r  r   r   rA   r   r   r   rP   r  r  r   _shape_as_tensorr|  r  current_device_indexcurrent_acceleratorcurrent_deviceis_initializedxpur  r  rJ   _get_device_index_get_cublas_allow_tf32_is_any_autocast_enabledr  get_device_propertiesget_autocast_dtypeget_autocast_gpu_dtypeget_default_dtypeis_autocast_cache_enabledis_autocast_cpu_enabledis_autocast_enabledrf  re  r  
_Reductionget_enumpromote_types_get_privateuse1_backend_name_is_checkpoint_validr   r  get_rankget_world_sizer5  boolrW   r  r-  _dispatch_tls_local_include_set_dispatch_tls_local_exclude_setru  r_   rp   r   r  r8  r5  rT   rU   rV   <module>r|     s  6     	 . / /     ( ' K > >  
 @ 9 0 	 	 	 A @ 
 % B C . E
 D g! $''!!77!!,,''//--%%44))@@))??))FF		((  ,,  //  ((  1188		##,,

$$--####// 	&&22; ! F %)MM% ! 
**	))	JJ	JJ	II	II' # 
MM	LL""	HH##	HH%%	""	JJ$$	JJ	""		  		##	!!				HH""++		HH**	NN''	II##	II-. (/( 0 	!!#"",,&&,,	
 '+mm4W&X #--(?@  hr3w&7$&G!H  * --<
= 
HH	HH,,	HH,,   $GA GATh7#4 h7VWG#4 WGt2";. ";J;#4 ;Y@  	B
  s$   ]- !]; -]87]8;^^