
    Vh                        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 ddl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 dd
lmZ ddlmZmZmZ ddl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-m.Z.m/Z/m0Z0 ddl1m2Z2 ddl3m4Z4m5Z5m6Z6 ddl7m8Z8 ddl9m:Z:m;Z; ddl<m=Z=m>Z> ddl?m@Z@mAZAmBZB 	 ddlCZD	 ddlFmGZG e	rddlHmIZI  ej                  eK      ZLeMj                  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                  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                  g      ZoeMj                  ej                  g      Zqej                  j                  gZsej                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ejr                  j                  ej                  ej                  ej                  ej                  ej                  ej                   ej                  ej                  ej                  j                  j                  j
                  ej                  ej                  j                  ej                  j                  gesz   Zejr                  j                         rTej                  ejr                  j                  ejr                  j                  ejr                  j                  g       eMj                  es      ZseMj                  e      Zej                  j                  dej                  j                   dej                  j"                  dej$                  j&                  j(                  dej*                  j,                  dej.                  j0                  j2                  dej                  j2                  dej4                  j2                  dej4                  j6                  dej4                  j8                  dej                  j:                  j<                  j>                  diZeMj                  g d      Zej                  jD                  ej                  jF                  ej                  jH                  hZ ejL                  d      d        Z G d de2      Z G d  d!e      Z G d" d#e      Z G d$ d%e      Z G d& d'e      Zy# eE$ r dZDY w xY w# eE$ r dZGY 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)Sequence)TYPE_CHECKING)TracingContext)warning_once)"is_traceable_wrapper_subclass_type   )config	polyfills	variables)	PyCodegen)!can_convert_to_tracable_parameternew_parameter_placeholdertracable_create_parameter) get_registered_device_interfaces)unimplemented)GuardBuilderinstall_guard)CallFunctionNoArgsSourceSyntheticLocalSource)check_unspec_or_constant_argsguard_if_dynhas_torch_functionhashableproductproxy_args_kwargsunwrap_if_wrapper   )VariableTracker)AutocastModeVariableProfilerContextVariableTorchFunctionDisableVariable)ConstDictVariable)DistributedVariableProcessGroupVariable)ListVariableTupleVariable)can_dispatch_torch_functiondispatch_torch_functionTorchFunctionModeStackVariable)_fsdp_param_group)InstructionTranslatorFT)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)	itertoolsr2   torch.overridesr3   setfrom_iterablevaluestorchones	ones_likezeros
zeros_likeemptyfullupdate)r2   get_overridable_functions_funcsmores       M/home/dcms/DCMS/lib/python3.12/site-packages/torch/_dynamo/variables/torch.pyr3   r3      sp    W##$>$@$G$G$IJKE



D 
LLL    c                   T     e Zd ZdZed        Zd	 fdZ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                 f    t        |j                  t        j                                | ||      S Nsource)r   
make_guardr   FUNCTION_MATCHclsvaluerK   s      rD   create_with_sourcez$BaseTorchVariable.create_with_source   s(    f''(C(CDE5((rE   c                 2    t        |   di | || _        y )N )super__init__rP   )selfrP   kwargs	__class__s      rD   rU   zBaseTorchVariable.__init__   s    "6"
rE   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_]+_)	rP   
__module____name__	Exceptionidrer-   extend_outputsetup_globally_cached)rV   codegennameunique_var_names       rD   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rP   rV   s    rD   as_proxyzBaseTorchVariable.as_proxy       zzrE   c                     | j                   S rj   rk   rl   s    rD   as_python_constantz$BaseTorchVariable.as_python_constant   rn   rE   c                 l    t        | j                  |      }t        j                  j	                  |      S rj   )hasattrrP   r   ConstantVariablecreate)rV   txrf   results       rD   call_obj_hasattrz"BaseTorchVariable.call_obj_hasattr   s)    T*))0088rE   c                 \    | j                   t        v ryt        | j                   dd       dk(  S )NTr^   math)rP   constant_fold_functionsgetattrrl   s    rD   can_constant_fold_throughz+BaseTorchVariable.can_constant_fold_through   s*    ::00tzz<6&@@rE   returnNru   r+   )r_   r^   __qualname____doc__classmethodrQ   rU   rh   rm   rp   rw   r|   __classcell__rX   s   @rD   rG   rG      s7    R) )
9ArE   rG   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 implementationsr~   c                 "    d| j                    dS )NzTorchCtxManagerClassVariable()rk   rl   s    rD   __repr__z%TorchCtxManagerClassVariable.__repr__   s    .tzzl!<<rE   c                 \    t        |       } t        |       xr t        |       xr | t        v S rj   )r   callabler   supported_ctx_manager_classesrk   s    rD   is_matching_clsz,TorchCtxManagerClassVariable.is_matching_cls   s6     "%( UO  ;::	
rE   ru   r+   argsrW   dict[str, VariableTracker]r   c           
         ddl 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        j.                  u rlt!        |      dk(  rLt#        |d   t$        j&                  j(                        r%|	j+                  |d      }|j-                  |||      S |	j+                  |d      S | j                  t        j0                  u r3t!        |      dk(  r%|	j+                  ||d   j3                         d      S | j                  t        j4                  u rSt!        |      dk  rt!        |      dk(  sJ t!        |      dk(  r|d   j3                         nd}|
j+                  ||      S t7        j8                  | j                        rZt;        | j                  t        j<                        r6ddlm }  ||||jB                  jE                  d| j                  d	i             S | j                  t        jF                  jH                  jJ                  t        jL                  jF                  jJ                  t        jN                  jF                  jJ                  fv r!tQ        j*                  | j                  ||      S | j                  t        jR                  jT                  t        jR                  jV                  t        jX                  jR                  jT                  t        jX                  jR                  jV                  fv r%t[        t\        d
| j                         t_               S | j                  t        j`                  jb                  u r|s|rJ te        j*                  |      S | j                  t        jf                  jh                  jj                  u r"t!        |      dk(  sJ |j+                  ||      S | j                  t        jf                  jl                  jn                  u r!t!        |      dk(  sJ |j+                  |      S | j                  t        jX                  jp                  jr                  u r9t!        |      dk(  sJ |j+                  ||D cg c]  }tu        |       c}      S | j                  t        jX                  jp                  jv                  u r!t!        |      dk(  sJ |j+                  |      S | j                  t        jf                  jl                  jx                  u r!t!        |      dk(  sJ |j+                  |      S | j                  t        jf                  jl                  jz                  u r9t!        |      dk(  sJ |j+                  ||D cg c]  }tu        |       c}      S | j                  t        jX                  j|                  j~                  u r3t!        |      dk(  sJ |j+                  ||d   j3                               S t        ]| j                  t        j                  j                  u r7t!        |      dk(  sJ |j+                  ||d   |d   j3                               S | j                  t        j                  j                  j                  u rZt!        |      dk(  st!        |      dk(  rd|v sJ t!        |      dk(  r|d   n|d   }|j+                  ||j3                               S | j                  t        j                  j                  j                  u r.|j+                  ||D cg c]  }|j3                          c}      S t        | Y  |||      S c c}w c c}w c c}w )Nr   )!DisabledSavedTensorsHooksVariableDualLevelContextManager&FSDPParamGroupUseTrainingStateVariable&GradIncrementNestingCtxManagerVariable)GradInplaceRequiresGradCtxManagerVariableGradModeVariableInferenceModeVariable%JvpIncrementNestingCtxManagerVariableSDPAKernelVariableSetFwdGradEnabledContextManagerStreamVariable&VmapIncrementNestingCtxManagerVariabler   FT)initialized)wrap_fx_proxy_clscall_functionrS   z$Profiler function %s will be ignoredr   backends)H r   r   r   r   r   r   r   r   r   r   r   r   rP   r9   no_gradlen
isinstancer   	functionsBaseUserFunctionVariablert   r   enable_gradset_grad_enabledrp   inference_modeinspectisclass
issubclassStreamtorch._dynamo.variables.builderr   outputcreate_proxyampautocast_modeautocastcudacpur   profilerprofilerecord_functionautogradr   logr    _CDisableTorchFunctionSubclassr!   
_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_hooksr*   FSDPParamGroupuse_training_statenn	attentionsdpa_kernel_sdpa_kernel_variadicrT   )rV   ru   r   rW   r   r   r   r   r   r   r   r   r   r   r   r   ctxinf_moder   xr   argrX   s                         rD   r   z*TorchCtxManagerClassVariable.call_function  sK   	
 	
 	
 	
 ::&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==__TZZ(Z

ELL-QI$		&&#JJ		 	 ZZII##,,JJNN##IIMM""
 

 (..tzz4HHZZ NN""NN**NN##++NN##33
 
 DdjjQ*,,ZZ588@@@''/66r::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9t9>c&kQ&6:;OPP"%d)q.tAwfZ6HH%,,R1L1L1NOOZZ588--CCC%,,>#S++->  w$Rv66M 0 0* ?s   ^?
'_
_	
)r_   r^   r   r   strr   staticmethodr   r   r   r   r   r   s   @rD   r   r      sa    V=# = 
 
7#7 '7 -	7
 
7 7rE   r   c                        e Zd ZdZdd fdZdefdZd Ze e	j                  d      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 xZS )TorchInGraphFunctionVariablez@Points to a torch function/method that should be put in FX graphNr~   c                 T    t        |   |fi | ddlm} | ||      }|| _        y )Nr   )is_nonstrict_trace_callable)rT   rU   trace_rulesr   nonstrict_traceable)rV   rP   r   rW   r   rX   s        rD   rU   z%TorchInGraphFunctionVariable.__init__  s1    )&)=&"=e"D#6 rE   c                 <    d| j                    d| j                   dS )NzTorchInGraphFunctionVariable(z, nonstrict_traceable=r   )rP   r   rl   s    rD   r   z%TorchInGraphFunctionVariable.__repr__  s%    .tzzl:PQUQiQiPjjkllrE   c                     | j                   S rj   rk   rl   s    rD   get_functionz)TorchInGraphFunctionVariable.get_function  rn   rE   c                    456789:;<= i ;;fd} ddl m} ddlm4m5m6m7m8m9m	: ddl
m<m=  | t         	 	 d84fd       } | t         	 	 d8d       } | t        j                   j"                  j$                        	 	 d8d	       } | t        j&                  j(                  j*                  j,                        d8d
       } | t.        j0                        d8d       } | t        j2                  t        j                   j4                        d849:fd       } | t        j6                  t        j8                        d849fd       } | t        j:                        d849fd       }	 | t        j<                        d8d       }
 | t>         d89fd       } | t        j@                  jB                  jD                  jF                  t        j@                  jB                  jD                  jH                  t        j@                  jB                  jD                  jJ                  t        j@                  jB                  jD                  jL                  t        j@                  jB                  jD                  jN                        d8d       } | t        jP                        46fd       } | t        jR                        	 d9	 d85fd       } | t        jT                        45fd       } | t        jV                  jX                        4fd       } | t        j                   jZ                  t        j                   j\                  t        j                   j^                        d84fd       } | t`        jc                  d te               D               d87fd       } | t        jf                        d89=fd       } | t        jh                  jj                        d8d       } | t        jl                  jn                  jp                        	 	 d849fd       } | t        jD                  jr                  jt                        d8d       } | t        j@                  jv                        d8d       } | t        j&                  jx                  jz                  t        j&                  jx                  jz                  j|                        d:d       } | t        j&                  jx                  j~                  t        j&                  jx                  j~                  j|                        d:d        } | t        j                        d8d!       } | t        j                        9fd"       } | t        j                        	 	 d8d#       } | t        j                        d89fd$       } | t        j                        d84fd%       } | |      d8<fd&       }t        j                         rFdd'lGmH} mI}!mJ}"mK}#mL}$ dd(lMmN}%  | | |!|"|$|#      	 	 d84fd)       }& | |%j                        d8<fd*       }' | t        j                  j                        	 d:dd+	 d8d,       }( | t        j@                  j                  j                        d8d-       }) | t        j                  j                  j                  j                        d848fd.       }* | t        jV                  j                  j                        	 	 d8d/       }+ | t        jV                  j                  j                        	 	 d8:fd0       }, | t        j                  j                  j                        	 	 d8d1       }- | t        j                        d889fd2       }. | t        jV                  j                        	 	 d8d3       }/ | t        jV                  j                        	 	 d84fd4       }0 | t        jV                  j                        	 	 d84fd5       }1 | t        jV                  j                        d8d6       }2 | t        j                        	 	 d84fd7       }3;S );zBuild 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 rj   rS   )handlerfnfnshandlerss     rD   	_registerzOTorchInGraphFunctionVariable._get_handlers.<locals>.register.<locals>._register  s5     +BX-1r1-#*HRL+ rE   r   )r   )r   r   r   s   ` rD   registerz<TorchInGraphFunctionVariable._get_handlers.<locals>.register  s"     CF###rE   r   )
SDPAParamsr   )rs   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                  fv r|j                          j                  t        | j                            S rj   )rP   r9   _utilsis_compiling_dynamoexternal_utilscompileris_dynamo_compilingis_exportingmark_inconsistent_side_effectsrt   tracing_state_functionsrV   ru   r   rW   rs   s       rD   handle_tracing_state_functionszRTorchInGraphFunctionVariable._get_handlers.<locals>.handle_tracing_state_functions  s     F**zz)),,99++22++  113#**+B4::+NOOrE   c                    |rJ | j                   t        j                  j                  fv r%t	        |      dk(  sJ t        |d   t        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)rP   r9   r   _dispatch_keysr   r   r   r   proxynodemeta_subclasses
FakeTensorDispatchKeySetDispatchKeyPythonPythonTLSSnapshotDispatchKeySetVariablert   )rV   ru   r   rW   r   dkss         rD   !handle_dispatch_key_set_functionszUTorchInGraphFunctionVariable._get_handlers.<locals>.handle_dispatch_key_set_functions  s    :zzehh55774yA~%~!$q'9+C+CDDD $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BrE   c                 f    t        j                  |t        j                  j	                               S rj   )r   buildr9   	overridesget_default_nowrap_functionsrV   ru   r   rW   s       rD   #handle_get_default_nowrap_functionszWTorchInGraphFunctionVariable._get_handlers.<locals>.handle_get_default_nowrap_functions  s)     #((EOO@@B rE   c                 l    |j                  t        j                  |t        j                        ||      S rj   )inline_user_function_returnr   r
  r
   accumulate_gradr  s       rD   handle_accumulate_grad_zKTorchInGraphFunctionVariable._get_handlers.<locals>.handle_accumulate_grad_  s/    11%%b)*C*CDdF rE   c                     t        ||      s5|j                  t        j                  |t        j
                        ||      S y rj   )r   r  r   r
  r
   radiansr  s       rD   handle_radianszBTorchInGraphFunctionVariable._get_handlers.<locals>.handle_radians  s>    0v>55#))"i.?.?@$  ?rE   c                     t        |      sH| j                  t        j                  j                  u r3t        |      r't        |j                  d      rj                  d      S j                  d      S )N__torch_function__TF)r   rP   r9   r  is_tensor_likerr   rt   )rV   ru   r   rs   r   r   s      rD   handle_is_tensorzDTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_tensor  s_    #~.

eoo<<<s$=>CII';<'..t44'..u55rE   c                 p   |}t        |      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   dtyperP   r9   is_floating_pointrt   
is_complexAssertionError)rV   ru   input	input_argrs   r   s       rD   handle_is_floating_pointzLTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_floating_point  s    
 I)^49T::!8!88+229??3T3TUUZZ5#3#33+229??3M3MNN(8DJJ<)@AA :U4rE   c                     t        |      r4|j                         r$j                  t        |j                              S t        |      r|j                  |dg i       S y )Nnumel)r   
valid_sizert   r   sizecall_method)rV   ru   r  rs   r   s      rD   handle_numelz@TorchInGraphFunctionVariable._get_handlers.<locals>.handle_numel   sX    %0U5E5E5G'..wuzz/BCCE>2((Wb"== 3rE   c                 @    t        |      dk(  r|d   S t        d       y )Nr   r   z:torch.compile is used as a decorator in the compiled frame)r   r   r  s       rD   handle_torch_compilezHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_compile(  s    4yA~AwVWrE   c                 H    t        |      sJ |j                  |dg i       S Nr%  )r   r&  )rV   ru   r  r   s      rD   handle_tensor_size_rewriteszOTorchInGraphFunctionVariable._get_handlers.<locals>.handle_tensor_size_rewrites0  s*    e^444$$RR88rE   c                 (    | j                  |||      S rj   )_call_ntupler  s       rD   handle_ntuplezATorchInGraphFunctionVariable._get_handlers.<locals>.handle_ntuple5  s     $$Rv66rE   c                 t    t        j                         j                  t        j                               S rj   )r   _guards_singletonrt   r9   is_grad_enabled)rV   ru   rs   r   s     rD   handle_is_grad_enabledzJTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_grad_enabled?  s,    *<<=#**5+@+@+BCCrE   c                 ~    |r|j                         rt        d       j                  ||j                               S )Nz2torch.use_deterministic_algorithms(warn_only=True))rp   r   rt   )rV   ru   mode	warn_onlyr   s       rD   #handle_use_deterministic_algorithmszWTorchInGraphFunctionVariable._get_handlers.<locals>.handle_use_deterministic_algorithmsD  s8     Y99;RS299"d>U>U>WXXrE   c                 t    t        j                         j                  t        j                               S rj   )r   r1  rt   r9   $are_deterministic_algorithms_enabled)rV   ru   rs   r   s     rD   +handle_are_deterministic_algorithms_enabledz_TorchInGraphFunctionVariable._get_handlers.<locals>.handle_are_deterministic_algorithms_enabledL  s,    9KKL#**5+U+U+WXXrE   c                     t        t        j                         j                  |j                  j
                        S rj   )r   r!   r1  rt   r   torch_function_enabled)rV   ru   rs   s     rD    handle_is_torch_function_enabledzTTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_torch_function_enabledQ  s-    6HHI#**299+K+KLLrE   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rj   )r   .0r   s     rD   	<genexpr>z`TorchInGraphFunctionVariable._get_handlers.<locals>.handle_has_torch_function.<locals>.<genexpr>b  s     9a&q)9s   )r   r   r&   unpack_var_sequencert   any)rV   ru   r   elemsrs   s       rD   handle_has_torch_functionzMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_has_torch_functionV  s\     t9>ja-&H Q++B/ 
 $**9599 rE   c              3   :   K   | ]  \  }}|j                     y wrj   )stream)rA  r]   device_interfaces      rD   rB  z=TorchInGraphFunctionVariable._get_handlers.<locals>.<genexpr>f  s#      'A' !''s   c                 (    j                  ||      S rj   )rt   )rV   ru   rH  r   s      rD   handle_device_interface_streamzRTorchInGraphFunctionVariable._get_handlers.<locals>.handle_device_interface_streame  s     )//F;;rE   c                     t         j                  st        d       t        st        d        | |j                  j
                  dt        j                  gt        |i        d       S )Nz-torch.from_numpy. config.trace_numpy is Falsez(torch.from_numpy. NumPy is not availabler   )
target_clsru   r   r   )	r	   trace_numpyr   npr   r   r9   	as_tensorr   )rV   ru   r   r   r   s      rD   handle_from_numpyzETorchInGraphFunctionVariable._get_handlers.<locals>.handle_from_numpyn  sh    %%MNHI$),bii,,#OO 'tR0
 #	 	rE   c                     |S rj   rS   )rV   ru   the_type	the_values       rD   handle_jit_annotatezGTorchInGraphFunctionVariable._get_handlers.<locals>.handle_jit_annotate  s    rE   c                    |rJ d       t        |      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   )r  device)	r   r9   tensorr  rW  rt   r   cudnnis_acceptable)rV   ru   rX  extra
tensor_inprs   r   s        rD   handle_cudnn_is_acceptablezNTorchInGraphFunctionVariable._get_handlers.<locals>.handle_cudnn_is_acceptable  st     EEE9fn5 D5 av||FMMRJ#**$$22:> rE   c                 H    t        j                  j                  |g|i |S rj   )r   BackwardHookVariablert   r  s       rD   handle_backward_hookzHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_backward_hook  s#    1188MdMfMMrE   c                 .     | j                   |g|i |S rj   )call_nn_parameterr  s       rD   handle_parameterzDTorchInGraphFunctionVariable._get_handlers.<locals>.handle_parameter  s    )4))">t>v>>rE   Nc                 2    ||j                  |d|gi       S y r+  r&  self_ru   rV   dims       rD   handle_sym_sizezCTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_size  s'     ''FSE2>> rE   c                 2    ||j                  |d|gi       S y )Nstridere  rf  s       rD   handle_sym_stridezETorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_stride  s%    ''HseR@@ rE   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 )N   rP   r   r   )r   r   r9   r/   r   r.   r,   )rV   ru   r   rW   rv   s        rD   handle_addcdivzBTorchInGraphFunctionVariable._get_handlers.<locals>.handle_addcdiv  s    4yA~'V"3Fq8H 6eii@NN$qr(R 6eii@NN12 4EII>LLa&)2  9I"3~rE   c                     t        |      rkt        t        j                  j                  j
                        j                  ||gi       }t        t        j                        j                  |||g|      S y rj   )r   r   r9   opsaten_local_scalar_denser   r?   )rV   ru   r%  
fill_valuerW   rv   r   s         rD   handle_fullz?TorchInGraphFunctionVariable._get_handlers.<locals>.handle_full  si    *n55IINN66-ZL"5  4EJJ?MMv 	 6rE   c                     t        |      dk(  rLt        |d   t              s8|s5|j                  t	        j
                  |t        j                        ||      S y y y )Nrn  r   )r   r   r%   r  r   r
  r
   foreach_lerp_inplace)r]   ru   r   rW   s       rD   "handle_inplace_foreach_lerp_scalarzVTorchInGraphFunctionVariable._get_handlers.<locals>.handle_inplace_foreach_lerp_scalar  sZ     4yA~ja,&GPV55#))"i.L.LM  QW&G~rE   c                     t        |      dk(  rHt        |d         r8|s5|j                  t        j                  |t
        j                        ||      S y y y )Nr   r   )r   r   r  r   r
  r
   foreach_pow_scalar)r]   ru   r   rW   r   s       rD   handle_foreach_pow_scalarzMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_foreach_pow_scalar  sZ     4yA~*T!Wn"Ef55#))"i.J.JK  OU"E~rE   c                     |j                         r|j                         s*t        |t        j                        r|j                         r d       S y y rj   )is_python_constantrp   r   r   r   evaluate_expr)rV   ru   	conditionmessagers   s       rD   handle_assertzATorchInGraphFunctionVariable._get_handlers.<locals>.handle_assert  sL    ,,.93O3O3Q9i&?&?@++-'-- . ArE   c           
           | |j                   j                  dt        j                  j                  gt        ||       |      S )Nr   )r   
param_vars)r   r   r9   r   _SDPAParamsr   )rV   ru   r   rW   r   s       rD   handle_sdpa_paramszFTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sdpa_params  sM     ,bii,,#HH(( 'tV4
   rE   )_get_group_size_by_name_get_group_tag_rank_not_in_group$_resolve_group_name_by_ranks_and_tagget_process_group_ranks)DTensorc                 v   t        |      dk(  rt        |d   t        f      sOJ t        |      dk(  r$t        |d   t              rt        |d         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%   r  rP   rp   r   r
  )rV   ru   r   r   args_as_valueinvocation_resultrs   s         rD   &handle_constant_processgroup_functionszZTorchInGraphFunctionVariable._get_handlers.<locals>.handle_constant_processgroup_functions  s     t9>%d1g0DFV/WXXXY!^%d1g|<Q!1B   )/v 6$$(JJ<1  FJ Jc!7!7!9 J J$.DJJ$>!
 ',,R1BCC !Ks   8B6c           
         	
 |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 )	Nr   shaperk  c                 :     j                   | gi ||dS )Nr  rk   )r   r  rk  r  kwargs_as_valuerV   s      rD   fn_with_prim_typeszaTorchInGraphFunctionVariable._get_handlers.<locals>.handle_from_local.<locals>.fn_with_prim_types'  s4    %4::)-<DIRX rE   zprim r   r   ru   r   )NN)rp   itemsrP   r_   r   r   r   )rV   ru   r   rW   r   kvkwargs_to_be_proxiedr  r  r  r   s   `        @@rD   handle_from_localzETorchInGraphFunctionVariable._get_handlers.<locals>.handle_from_local  s     BFab JA!5!5!7 J !'#1 33 q++--# +>(%&fAvayL($ ( /6

8K8K.K"+$0"))00'* +!!WI0
 
% !K#
(s   CC 	C*C)layoutc                    ddl m} |r,|j                         t        j                  k(  rt        d       t        ||      st        d       y y )Nr   )BaseListVariablez3torch.compile does not support strided NestedTensorz!nested_tensor with non-list input)listsr  rp   r9   stridedr   r   )rV   ru   tensor_listr  r   rW   r  s          rD   handle_nested_tensorzHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_nested_tensor;  s@     0&335FSTk+;<AB =rE   c                     t        |      t        |      z   dk(  s7t        |      dk(  r7|d   j                         r#|d   j                         dk(  rt        d       y y y y )Nr   r   z<torch.nn.functional.one_hot with data-dependent output shape)r   r}  rp   r   r  s       rD   handle_one_hotzBTorchInGraphFunctionVariable._get_handlers.<locals>.handle_one_hotK  sa    4y3v;&!+D	QG..0G..0B6R 7 1 rE   c                     t        |      rZt        j                  j                  t        j
                  j                  j                  j                  |j                              S t        |      r|S y rj   )
r   r   rs   rt   r9   fxexperimentalsymbolic_shapesguard_size_oblivioussym_num)rV   ru   exprrs   r   s      rD   handle_guard_size_obliviouszOTorchInGraphFunctionVariable._get_handlers.<locals>.handle_guard_size_obliviousV  sd    $0 !1188HH))99NN 
 D"23 4rE   c                 J    ddl m} t        |      j                  |g ||      S )Nr   )_unsafe_set_version_counter)tensor_version_opr  r   r   )rV   ru   r   rW   r  s        rD   !handle_unsafe_set_version_counterzUTorchInGraphFunctionVariable._get_handlers.<locals>.handle_unsafe_set_version_counterc  s*     H/+mB$01rE   c                 `     t         j                  j                  j                               S rj   )r9   r   r   peek_interpreter_stack)rV   ru   r   rW   r   s       rD   'handle_functorch_peek_interpreter_stackz[TorchInGraphFunctionVariable._get_handlers.<locals>.handle_functorch_peek_interpreter_stackm  s(     -##::< rE   c                     |d   j                   }t        t        j                  j                  j                  |            S Nr   )rP   FuncTorchInterpreterVariabler9   r   pyfunctorchcoerce_cinterpreter)rV   ru   r   rW   cinterpreters        rD   0handle_functorch_pyfunctorch_coerce_cinterpreterzdTorchInGraphFunctionVariable._get_handlers.<locals>.handle_functorch_pyfunctorch_coerce_cinterpreterw  s8      7==L/  ,,@@N rE   c                     fdd }|r|d   }n	d|v r|d   }t        |      s? |      r6t        t        j                  j                        j                  |g ||      S y y )Nc                     t        | f      ryt        | t        t        f      rt        fd| j                  D              S y)NTc              3   .   K   | ]  } |        y wrj   rS   )rA  ycheck_any_unspecs     rD   rB  ztTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_tensor.<locals>.check_any_unspec.<locals>.<genexpr>  s     Dq/2Ds   F)r   r%   r&   rD  r  )r   r   r   r  s    rD   r  zaTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_tensor.<locals>.check_any_unspec  s>    a./!BCL-#@ADAGGDDD !rE   r   data)r   r   r9   _refsrX  r   )rV   ru   r   rW   data_argr  r   r   s        @rD   handle_torch_tensorzGTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_tensor  sx    	! H76!!&> h7<LX<V 4EKK4F4FGUU$  =W7rE   c                     |s|rJ |j                   j                  st        d      t        j                  |       |j                   j                         S )Nz/Popping from an empty torch function mode stack)symbolic_torch_function_state
mode_stackr   r)   register_mutationpop_torch_function_moder  s       rD   handle_pop_torch_functionzMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_pop_torch_function  sM     F**33>>#$UVV*<<R@33KKMMrE   c                     t        |      dk(  r|rJ t        j                  |       |j                  j	                  |d          j                  d       S Nr   r   )r   r)   r  r  push_torch_function_modert   r   s       rD   handle_push_torch_functionzNTorchInGraphFunctionVariable._get_handlers.<locals>.handle_push_torch_function  sO     t9>&00*<<R@,,EEd1gN#**400rE   c                 l    |s|rJ j                  t        |j                  j                              S rj   )rt   r   r  r  r   s       rD   handle_len_torch_functionzMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_len_torch_function  s7     F**#**B44??@ rE   c                     t        |      dk(  r|rJ |d   j                         }|dk\  r"|t        |j                  j                        k  sJ |j                  j                  |   S r  )r   rp   r  r  )rV   ru   r   rW   inds        rD   handle_get_stack_atzGTorchInGraphFunctionVariable._get_handlers.<locals>.handle_get_stack_at  sc    t9>&00q',,.C!8c"*J*J*U*U&V VVV33>>sCCrE   c                     t        j                  |       |d   j                         r)|d   j                         t        j                  |       nt        j
                  |       j                  d       S r  )r)   r  r}  rp   clear_default_device!register_device_context_insertionrt   r   s       rD   handle_set_default_devicezMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_set_default_device  sc     +<<R@Aw))+Q0J0J0L0T.CCBG.PPQST#**400rE   r   )Frj   )dtorch.backends.cudar   r   rs   r   r   r   r   r   r   builderr   r   r   dispatch_key_set_functionsr9   r  r  __wrapped__rq  inductoraccumulate_grad_defaultry   r  	is_tensorr  r  r  r#  compile!REWRITE_OPS_TO_TENSOR_SIZE_METHODr   modulesutils_single_pair_triple
_quadruple_ntupler2  use_deterministic_algorithmsr9  r   _is_torch_function_enabledr   has_torch_function_variadichas_torch_function_unarydictfromkeysr   
from_numpyjitannotater   rY  rZ  hooksBackwardHook	Parameterrr  sym_sizeint
sym_strideaddcdivr?   _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_hotr  r  r  r  	_autogradr  r   r  r  r  rX  _pop_torch_function_stack_push_on_torch_function_stack_len_torch_function_stack_get_function_stack_atset_default_device)>r   r   r   r  r  r  r  r  r!  r'  r)  r,  r/  r3  r7  r:  r=  rF  rK  rQ  rU  r]  r`  rc  ri  rl  ro  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  rs   r   r   r   r   r   r   r   r   r   s>                                                       @@@@@@@@@@rD   _get_handlersz*TorchInGraphFunctionVariable._get_handlers  s   
 	 	3	
 	
 	
 	>	*	+	P-	P 
,	P 
-	.	C-	C 
/	C6 
%//>>JJ	K		-		 
L		 
%))$$55==	>	 
?	
 
$,,		 
 	 
%//5??#A#A	B	6 
C	6 
##

	B	

	B 
%++		> 
	> 
%--	 	X 
!	X 
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	M 
7	M 
OO..OO77OO44


	


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

	<

	< 
%""	#	 
$	  
%))$$	%	 
&	 
%..&&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 
%--	 	 
!	 
%**		 
	 
%&&	'	*	 
(	 
%$$	%	 
&	 
%--	 	. 
!	. 
*			 
		 ++-  9'"'4D1DD: g(() *B 
%,,,,	- 	C
 	C'	C 
.	C 
%((%%--	.	 
/	 
%((''77LL	M
	 
N
	 
%(($$@@	A	1-	1 
B	1 
%((%%<<	=	-	 
>	 
%""..BB	C	-	 
D	 
%,,		 
 	4 
%((44	5	N-	N 
6	N 
%((88	9	1-	1 
:	1 
%((44	5	-	 
6	 
%((11	2	D 
3	D 
%**	+	1-	1 
,	1$ rE   ru   r+   r   rW   r   r   c                   123 ddl m}m1m} ddlm} | j                  r3dd lmc m	} ddlm
}m}	 ddlm2 ddlm}
 ddlm} t%        j&                  |t%        j&                  ||      t)        j&                  ||      f      }t+        j,                  |
      j/                  ||gi       }t1        |t$              rt3        |j4                        d	k(  sJ |j4                  \  }}t1        |t6              sJ |j4                  D ]D  }|j9                         } |	|      r|j9                         j:                  }t=        d
| d       F |j4                  D cg c]  }|j?                          }}	 |jA                         }| jJ                  323fd} ||      \  }}|jL                  jO                  3jP                   d|      }|jL                  jO                  3jP                  dz         }tS        |      |jT                  _)        tS        |      |jT                  _)        ||g|}|jL                  jW                  d||i       } |||      }|S | jY                  |||      rt[        || ||      S | j]                         rt_        ||      r| jJ                  t`        v r=tc        | jd                        }tg        |ji                  tj        jl                               |jo                   | jA                         |D  cg c]  } | jA                          c} i |j5                         D !"ci c]  \  }!}"|!|"jA                          c}"}!      S | jq                         r| js                  |||      S | ju                         jw                  | jJ                        }#|#r |#| |g|i |}$|$r|$S ty        1fd|D              }%t{        d |D              }&t}        | jJ                  dd      dk(  rY| jJ                  jP                  t~        v r=|%r;|&r9dt        | jJ                         d}'t        j                  |'       t=        |'       | jJ                  }(|%rSd| jJ                  jP                   })t}        | jJ                  dd       dk(  r t        t        |)      rt}        t        |)      }(d }*d|v rMt1        |d   t*        j                        r0|d   j                  jT                  j                  d   j                  }* || |jL                  jV                  d|(gt        ||              }+t1        |+|      r"d!|v r|d!   jA                         rt=        d"       d|v rt1        |d   t*        j                        r|d   jA                         t1        |+t$              rt1        |d   t$        t6        f      sJ t        |d   j4                  |+j4                        D ]`  \  },}-t1        |,t*        j                        s!t1        |-t*        j                        s<|,j                  |-j                  k7  sVt=        d#       b |+S t1        |+|      rt1        |d   |      sJ d|d   j                  jT                  j                  v sJ |+j                  jT                  j                  d   }.|d   j                  jT                  j                  d   }/|*|.j                  k7  rt=        d#       t        j                  j                  |/      st=        d$       |+S t1        |+|      r<|+jJ                  /t1        |d   |      ryd|d   j                  jT                  j                  v sJ |d   j                  jT                  j                  d   }/t        j                  j                  |/      st=        d$       |+S t1        |d   t6              rt        |d   j4                        D ]w  \  }0} d| j                  jT                  j                  v sJ | j                  jT                  j                  d   }/t        j                  j                  |/      rmt=        d%       y |+S t=        d&tS        |d                 |+S c c}w # |$ rm}|jB                  j9                         }|j:                  }dd lm"c m#} |jI                  |      rt=        d| d       nt=        d| d| d       Y d }~md }~ww xY wc c} w c c}"}!w )'Nr   )rs   r   r   r   r   )func_to_graphableis_graphable_type)fake_tensor_tls)tree_flatten)#AsPythonConstantNotImplementedErrorr   z
For `nonstrict_trace`-ed function, the only allowed input types are basic types (e.g., torch.Tensor, int, float) or pytree containers of those. Here you are calling the function with arguments that contain a value of type <z>, please use 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`
z`
You are calling 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.

Please construct the object _outside_ the `torch.compile` region, or submit an issue to GitHub.
    z
You are calling a `nonstrict_trace`-ed function where one one of the inputs has been registered with a `pytree_flatten` that puts an object of type <z> into the context.

Please consider modifying that `pytree_flatten` to avoid putting the object into context, and apply one of the following to <z>
  * `torch.utils._pytree.register_constant`
  * `torch.utils._pytree.register_dataclass`
  * `torch.utils._pytree.register_pytree_node`

If the above doesn't work, please subtmit an issue to GitHub.
c                  f    j                   }d_         	  | i |}|_         |S # |_         w xY wNT)allow_non_fake_inputs_override)r   rW   old_valresr  r   s       rD   
patched_fnz>TorchInGraphFunctionVariable.call_function.<locals>.patched_fn.  sH     *HHAE>Md-f-CELOB
 FMOBs   ' 	0_spec_input_specr   c              3   6   K   | ]  }t        |        y wrj   )r   )rA  r   r   s     rD   rB  z=TorchInGraphFunctionVariable.call_function.<locals>.<genexpr>p  s     &T!z!_'E&Ts   c              3   p   K   | ].  }t        |t        j                  t        j                  f       0 y wrj   )r   r   rs   r   r@  s     rD   rB  z=TorchInGraphFunctionVariable.call_function.<locals>.<genexpr>r  s0      !
 q955y7P7PQR!
s   46r^   r   r9   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.
_sym_ry   outr   r  requires_gradzfactory functions that return tensors that require grad are not supported.
Either create the tensor outside the compiled region, or do not set the tensor to require_gradz*out variants with resizing on graph inputsz9out= op was called where output tensor was non-contiguouszGout= op was called where some of the output tensors were non-contiguouszout variant of )Nr   rs   r   r   r  r   r   "torch._higher_order_ops.flat_apply_higher_order_ops
flat_applyr  r  torch._subclasses.fake_tensorr  torch.utils._pytreer  baser  r&   r
  r"   r   UserFunctionVariabler   r   r   r  r%   python_typer   r   rm   rp   vtr  _pytreeis_constant_classrP   r   %register_static_attr_and_return_proxyr_   typer   r   torch_function_override_enabledr(   r|   r   #constant_fold_functions_need_guardsr   rK   r   rL   r   EQUALS_MATCHrt   is_tensor_methodcall_tensor_methodr
  getrD  allr{   bin_opsr   r   warningrr   r9   r   r   r  r   zip_size_prims_commonis_contiguous	enumerate)4rV   ru   r   rW   rs   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_specetyppytreer  r]   f_specf_spec_proxyinput_spec_proxyall_argsr   rK   r   r  r  special_handlerrv   any_symints_or_symfloatsall_ints_or_floatsmsgfn_torch_sym_opfake_out_shapetensor_variable
out_tensorresult_tensorfake_tensorfake_outidxr   r  r   s4                                                    @@@rD   r   z*TorchInGraphFunctionVariable.call_function  s    	HG*##CC F8A ,11]((T24E4K4KBPV4WXO 33LAOO_%rF fm4V\\9Ja9OOO+1<<(M=m\:::  -22 &224(2 + 7 7 9 F FI!` aj  `k k	  ;H:M:M#+6$$&# #*==?
8 B
 **5IAv 99JJ;;-u%vL  "yyNNm+Z  &*&\L")-j)9!!&$&6M9LMH II**?JRTUE"2u-F M//D&A*2tT6BB))+0M&1
 zz@@1$++>f//0I0IJK#**)'')6:;a**,;=C\\^LTQq!..00L    "**2tV<<,,.224::>$T2???F#&&Tt&T#T   !
!
 

 DJJb1W<

##w.("		TZZ C
 KK#
 jj#"4::#6#6"78Ltzz<6&@W|F e\2 F?z&-9Q9QR $E]0055::?KQQN'("))(( #40
 76)'::<b F?ve}i&@&@Au002: /=9!&--1NOOO145M'')>)>2 T-J #:y/G/GH&}i6N6NO&,,(../
 &&RSTl U O^<!&-@@@&&-*=*=*B*B*G*GGGG-3388==oN!%=..3388I![%6%66 ""NO**88B "S< 5 ?,<=#))1 fUm^<*fUm.A.A.F.F.K.KKKK%e}2277<<_MH ..<<XF &W    u|<"+F5M,?,?"@ Q.!'',,2C2CCCC#$77<<#4#4_#E$22@@J * i  VE]0C/DEFe# 7 dd&&(,,	44++C0!``i_j k "V W`  Va a~ H  ~I I	b <Ls+   a<1b ?c6,c;c3A"c..c3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       y )Nztorch.nn.modules.utils._ntuple(r   )has_unpack_var_sequencer   r&   listrC  r}  rs   rt   r9   r   r  r  r  rp   r   )rP   countru   s    rD   r/  z@TorchInGraphFunctionVariable._call_ntuple.<locals>.handle_ntuple  s    ,,R0 ..22267  ))+ 1188HH$$**2259%:R:R:TU   ?waHIrE   )rP   r9   r   r  r  r  rp   __closure__cell_contentsr   r  r   LambdaVariable)rV   ru   r   rW   r/  r[  s    `   @rD   r.  z)TorchInGraphFunctionVariable._call_ntuple  s    ::))//777G..0EJJ**1-;;E%%%%z	J ::))//777++M:: a))rE   c           
         |j                   rt        d       t        |t        j                        r	 |j                         }t        |t        j                        st        d| d       |j                  r| j                  |||      S t        |j                        rt        d       t               st        d       	 t        |j                  |d      j                               }|j                  |d      j                         }|j                  |d	      j                         }|j                  j!                  t"        |g      }|j$                  r|j'                  |dg i       }ddlm}	  |	||j                  j-                  dt.        |j1                         |j1                         fi       |j                        }
t        |
t        j                        sJ t2        j4                  j6                  |
_        d|
_        |
S # t        $ r t        d       Y w xY w# t        $ r}t        d
|        Y d}~d}~ww xY w)z>A call to torch.nn.Parameter() gets lifted to before the graphz3nn parameter construction not supported with exportz)Parameter(requires_grad=...) not constantzParameter(data=z) not implementedz.Parameter constructor with tensor subclass NYIz4Workaround for issues with nn_parameter constructionr  r  rW  zParameter not python_constant: Ndetachr   r  r   rJ   F)exportr   r   r   r   rp   NotImplementedErrorr   rK   _nn_param_via_prefix_insertr   
class_typer   tuplevar_getattrr   synthetic_graph_inputr   r  r&  r  r   r   r   rm   r9   r   r  has_grad_fn)rO   ru   r  r  r  r  rW  rC  placeholderr   rv   s              rD   rb  z.TorchInGraphFunctionVariable.call_nn_parameter  s
    99OPmY%>%>?K - @ @ B $	 8 89OD61BCD ;;222t]KK-doo>JK02PQ	A$**2w7JJLME$$R1DDFE%%b(3FFHF ii55%ufm'L
 ##B"b9D*II"")+"6"6"89	 %%
 &)":":;;;!HH.. # m ' KIJK( # 	A;A3?@@	As*   H A)H& H#"H#&	I/IIc                 8   | j                   j                         }t        |       j                  fd        |j                          t        j                  |             j                  dd       j                  |       | j                   j                  j                  j                                |j                         j                  }|j                  dvrt        d       t!        |      }t"        j$                  j'                  | j                   j)                  |j                         j                              }t+        j,                  | ||      }t/        j0                         j2                  j4                  j7                  |       |S )Nc                  (     j                  dd      S )Nztorch.nnr  )load_import_from)cgs   rD   <lambda>zJTorchInGraphFunctionVariable._nn_param_via_prefix_insert.<locals>.<lambda>^  s    !4!4Z!M rE   r   F)ri  get_attrzAUnexpected type of data placeholder op for parameter construction)r   new_varr   add_push_nullrK   r   rs   r   storepregraph_bytecodeextendget_instructionsrm   r   opr   r   r9   r   r  example_value_from_input_noder   r
  r   r1  guards_contextdynamo_guardsremove_guards_with_source)	ru   r  r  varname	data_noderK   r   rv   rm  s	           @rD   rc  z8TorchInGraphFunctionVariable._nn_param_via_prefix_insertW  s8    ))##% r]
MN
4;;
9%%m45
E"

		##**2+>+>+@AMMO((	<<::S
 &g.**II33DMMO4H4HI
 !&&r=&A 	++99SS	
 rE   c                 f    |d   j                  || j                         j                  |dd  |      S )Nr   r   )r&  r   r_   r  s       rD   r0  z/TorchInGraphFunctionVariable.call_tensor_methodx  s2    Aw""2t'8'8':'C'CT!"XvVVrE   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  r   ismethoddescriptorr   rr   r  r9   r   
TensorBase)rV   r  s     rD   r/  z-TorchInGraphFunctionVariable.is_tensor_method{  s    3 &&t'8'8':; H))+^<H!!#00EHH4G4GG8  $5$77		8rE   c                     | j                         t               v xsL t        | j                         t        j                  j
                  t        j                  j                  f      xr t        |||      S rj   )r   r3   r   r9   _ops
OpOverloadOpOverloadPacketr'   r  s       rD   r,  z<TorchInGraphFunctionVariable.torch_function_override_enabled  sd    #<#>> !!#&&

(C(CD< *"dF;	<rE   rj   r}   r   r  )r_   r^   r   r   rU   r   r   r   r   	functools	lru_cacher
  r   r   r   r.  r   rb  rc  r0  r/  r,  r   r   s   @rD   r   r     s    J7m# m Yl  l\f#f 'f -	f
 
fP	*6 > >@  @W8<rE   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 )
r  zrepresents torch.DispatchKeySetc                     t        | fi |S rj   )r  )rP   rW   s     rD   rt   zDispatchKeySetVariable.create  s    %e6v66rE   c                 f    t        |j                  t        j                                | ||      S rI   )r   rL   r   DISPATCH_KEY_SET_MATCHrN   s      rD   rQ   z)DispatchKeySetVariable.create_with_source  s(    f''(K(KLM5((rE   c                 
    |dv S )N)hasrS   )rV   rf   s     rD   is_constant_fold_methodz.DispatchKeySetVariable.is_constant_fold_method  s    wrE   r   rW   r~   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{   rP   r   rs   rt   rp   r  EnumVariabler  rT   r&  )
rV   ru   rf   r   rW   methodr   r  r  rX   s
            rD   r&  z"DispatchKeySetVariable.call_method  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   rt   r   rQ   r  rZ  r   r  r   r&  r   r   s   @rD   r  r    sh    )7 7 ) ); ?#	;
 S/)*; 
; ;rE   r  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 )r  z<represents torch._functorch.pyfunctorch.FuncTorchInterpreterc                 f    t        |j                  t        j                                | ||      S rI   )r   rL   r   ID_MATCHrN   s      rD   rQ   z/FuncTorchInterpreterVariable.create_with_source  s(    f''(=(=>?5((rE   r   rW   r~   r   c                    |dk(  r-t        j                  | j                  j                               S |dk(  rH|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        | 5  ||||      S )Nkeyprocess)level
batch_size
randomnesslower)r   r  rP   r  r  r%  r  __func__rs   rt   r{   0TemporarilyPopInterpreterStackCtxManagerVariablerT   r&  )rV   ru   rf   r   rW   rX   s        rD   r&  z(FuncTorchInterpreterVariable.call_method  s     5=))$**..*:;;Y11..tzz/A/A/J/JK 
 ::--445NWTZZ5N5PQQW_F**MMTTD  w"2tT6::rE   )r_   r^   r   r   r   rQ   rZ  r   r  r   r&  r   r   s   @rD   r  r    sO    F) ); ?#	;
 S/)*; 
; ;rE   r  )r   r  r   loggingry   rb   collections.abcr   typingr   torch._Cr9   torch._refstorch.fxtorch.nntorch._guardsr   torch._loggingr   torch.utils._python_dispatchr   r   r	   r
   r   re   r   create_parameter_opr   r   r   rI  r   excr   guardsr   r   rK   r   r   r  r   r   r   r   r   r   r   r$  r   ctx_managerr   r    r!   dictsr"   distributedr#   r$   r  r%   r&   torch_functionr'   r(   r)   numpyrO  ModuleNotFoundError#torch.distributed.fsdp._fully_shardr*   torch._dynamo.symbolic_convertr+   	getLoggerr_   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   r   r   r   r   r   _shape_as_tensorr  current_devicer-  r  r   _get_device_index_get_cublas_allow_tf32_is_any_autocast_enabledget_device_propertiesr  get_autocast_dtypeget_autocast_gpu_dtypeget_default_dtypeis_autocast_cache_enabledis_autocast_cpu_enabledis_autocast_enabledr  r  r  
_Reductionget_enumpromote_types_get_privateuse1_backend_name_is_checkpoint_validrz   rt  is_initializedget_rankget_world_sizer  is_scripting
is_tracing_get_tracing_stater  _symbolic_traceis_fx_tracingonnxis_in_onnx_exportr   r   r   r   r   r   r  
activation_is_make_fx_tracingr   r3  r   _dispatch_tls_local_include_set_dispatch_tls_local_exclude_setr  r  r3   rG   r   r   r  r  rS   rE   rD   <module>r     s  6     	 $       ( ' K + +  
 @  0 C   " 
 % B . E
 D g! $''!!77!!,,''//--44))@@))??))FF		((  ,,  //  ((  1188		##,,

$$--&&00)! 4 %)MM% ! 
JJ' #
 
MM	LL""	HH##	HH%%	JJ$$	JJ	""		  		##	!!				HH""++		HH**	NN'''( ()( * 	!!#"",,&&,,	
 '+mm4W&X #--(?@  
IIE	II%	HH	HH**E	JJ  %	MM  --t	LLt	NN	NN&&	NN	HH33U  --<
= 
HH	HH,,	HH,,  T $#A #ALU7#4 U7py<#4 y<x";. ";J;#4 ;}*  	B
  s$   >] ]# ] ] #].-].