
    piG                    >   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	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l0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7 ddl8m9Z9m:Z: ddl;m<Z<m=Z=m>Z> ddl?m@Z@ ddlAmBZBmCZC ddlDmEZE ddlFmGZGmHZH ddlImJZJmKZKmLZLmMZM 	 ddlNZO	 ddlQmRZR erddlSmTZT  ej                  eV      ZWeXj                  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                  j                  g      Z{eXj                  ej                  g      Z}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j"                  ej$                  ej                  j&                  j(                  j*                  ej,                  ej                  j.                  ej                  j0                  ej                  j                  ej                  j                  gez   Zej                  j                         rTej5                  ej                  j                  ej                  j6                  ej                  j8                  g       eXj                  e      ZeXj                  e      Zej:                  deXe
g e	f   ee   f   fd       ZeXj                  g d      Zej                  jB                  ej                  jD                  ej                  jF                  hZej:                  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# eP$ r dZOY 3w xY w# eP$ r dZRY :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)AnyCallable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_v2)GuardBuilderinstall_guard)
AttrSourceCallFunctionNoArgsSourceSyntheticLocalSourceTorchSource)check_unspec_or_constant_argsguard_if_dynhas_torch_functionhashableproductproxy_args_kwargsunwrap_if_wrapper   )typestrVariableTracker)AutocastModeVariableProfilerContextVariableTorchFunctionDisableVariable)ConstDictVariable)DistributedVariableProcessGroupVariable)bind_args_cached)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*                  j,                  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nnmodules
activation_is_make_fx_tracing     _/opt/services/ai/voice_agent/venv/lib/python3.12/site-packages/torch/_dynamo/variables/torch.pytracing_state_functionsrR      s     					e##T  ..  77

$$e$$114!!4##T**D##T##77 rP   )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)	itertoolsrY   torch.overridesrZ   setfrom_iterablevaluesr8   ones	ones_likezeros
zeros_likeemptyfullupdate)rY   get_overridable_functions_funcsmores       rQ   rZ   rZ      sp    W##$>$@$G$G$IJKE



%D 
LLLrP   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                 f    t        |j                  t        j                                | ||      S Nsource)r   
make_guardr   FUNCTION_MATCHclsvaluero   s      rQ   create_with_sourcez$BaseTorchVariable.create_with_source   s(    f''(C(CDE5((rP   c                 2    t        |   di | || _        y )NrO   )super__init__rt   )selfrt   kwargs	__class__s      rQ   rx   zBaseTorchVariable.__init__   s    "6"
rP   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_]+_)	rt   
__module____name__	ExceptionidrerT   extend_outputsetup_globally_cached)ry   codegennameunique_var_names       rQ   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rt   ry   s    rQ   as_proxyzBaseTorchVariable.as_proxy      zzrP   c                     | j                   S r   r   r   s    rQ   as_python_constantz$BaseTorchVariable.as_python_constant  r   rP   c                 l    t        | j                  |      }t        j                  j	                  |      S r   )hasattrrt   r   ConstantVariablecreate)ry   txr   results       rQ   call_obj_hasattrz"BaseTorchVariable.call_obj_hasattr
  s)    T*))0088rP   c                 \    | j                   t        v ryt        | j                   dd       dk(  S )NTr   math)rt   constant_fold_functionsgetattrr   s    rQ   can_constant_fold_throughz+BaseTorchVariable.can_constant_fold_through  s*    ::00tzz<6&@@rP   r6   N)r   r   r   r5   )r   r   __qualname____doc__classmethodru   rx   r   r   r   r   r   __classcell__r{   s   @rQ   rk   rk      s7    R) )
9ArP   rk   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 implementationsr6   c                 "    d| j                    dS )NzTorchCtxManagerClassVariable()r   r   s    rQ   __repr__z%TorchCtxManagerClassVariable.__repr__  s    .tzzl!<<rP   c                 \    t        |       } t        |       xr t        |       xr | t        v S r   )r#   callabler    supported_ctx_manager_classesr   s    rQ   is_matching_clsz,TorchCtxManagerClassVariable.is_matching_cls  s6     "%( UO  ;::	
rP   r   r5   argsrz   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 s&| j                  t        j`                  jd                  u rA|s|rJ tg        j*                  || j                  t        j`                  jb                  u       S | j                  t        jh                  jj                  jl                  u r"t!        |      dk(  sJ |j+                  ||      S | j                  t        jh                  jn                  jp                  u r!t!        |      dk(  sJ |j+                  |      S | j                  t        jX                  jr                  jt                  u r9t!        |      dk(  sJ |j+                  ||D cg c]  }tw        |       c}      S | j                  t        jX                  jr                  jx                  u r!t!        |      dk(  sJ |j+                  |      S | j                  t        jh                  jn                  jz                  u r!t!        |      dk(  sJ |j+                  |      S | j                  t        jh                  jn                  j|                  u r9t!        |      dk(  sJ |j+                  ||D cg c]  }tw        |       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                  j                  u r\t        | j                  || j                  ||      }|d   j3                         }|d   j3                         }|j+                  |||      S t        | Y  |||      S 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_functionrO   z$Profiler function %s will be ignored)only_subclassr   backendsset_priority)K r   r   r   r   r   r   r   r   r   r   r   r   rt   r8   no_gradlen
isinstancer   	functionsBaseUserFunctionVariabler   r   enable_gradset_grad_enabledr   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(   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_hooksr4   FSDPParamGroupuse_training_staterK   	attentionsdpa_kernel__wrapped__r-   ro   rw   )ry   r   r   rz   r   r   r   r   r   r   r   r   r   r   r   r   ctxinf_moder   xname_to_arg_mapr   r   r{   s                          rQ   r   z*TorchCtxManagerClassVariable.call_function*  s5   	
 	
 	
 	
 ::&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*,,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.

BT6O 'z2EEGH*>:MMOL%,,R<HHw$Rv66K 0 0s   (^9
3^>
)r   r   r   r   strr   staticmethodr   r   r&   r   r   r   s   @rQ   r   r     sa    V=# = 
 
C7#C7 'C7 -	C7
 
C7 C7rP   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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 graphr6   c                 T    t        |   |fi | ddlm} | ||      }|| _        y )Nr   )is_nonstrict_trace_callable)rw   rx   trace_rulesr   nonstrict_traceable)ry   rt   r   rz   r   r{   s        rQ   rx   z%TorchInGraphFunctionVariable.__init__  s1    )&)=&"=e"D#6 rP   c                 <    d| j                    d| j                   dS )NzTorchInGraphFunctionVariable(z, nonstrict_traceable=r   )rt   r   r   s    rQ   r   z%TorchInGraphFunctionVariable.__repr__  s%    .tzzl:PQUQiQiPjjkllrP   c                     | j                   S r   r   r   s    rQ   get_functionz)TorchInGraphFunctionVariable.get_function  r   rP   c                  \  ?@ABCDEFGH i FFfd} ddl m} ddlm?m@mAmBmCmDm	E ddl
mGmH  | t                	 	 dC?fd       } | t         	 	 dCd       } | t        j                   j"                  j$                        	 	 dCd	       } | t        j&                  j(                  j*                  j,                        dCd
       } | t.        j0                        dCd       } | t        j2                        dCd       } | t        j4                  t        j                   j6                        dC?DEfd       } | t        j8                  t        j:                        dC?Dfd       }	 | t        j<                        dC?Dfd       }
 | t        j>                        dCd       } | t@         dCDfd       } | t        jB                  jD                  jF                  jH                  t        jB                  jD                  jF                  jJ                  t        jB                  jD                  jF                  jL                  t        jB                  jD                  jF                  jN                  t        jB                  jD                  jF                  jP                        dCd       } | t        jR                        ?Afd       } | t        jT                        	 dD	 dC@fd       } | t        jV                        ?@fd       } | t        jX                  jZ                        ?fd       } | t        jX                  j\                        ?fd       } | t        j                   j^                  t        j                   j`                  t        j                   jb                        dC?fd       } | td        jg                  d ti               D               dCBfd       } | t        jj                        dCDHfd       } | t        jl                  jn                        dCd       } | t        jp                  jr                  jt                        	 	 dC?Dfd       } | t        jF                  jv                  jx                        dCd       } | t        jB                  jz                        dCd       } | t        j&                  j|                  j~                  t        j&                  j|                  j~                  j                        dEd!       } | t        j&                  j|                  j                  t        j&                  j|                  j                  j                        dEd"       } | t        j                        dCd#       } | t        j                        Dfd$       } | t        j                        	 	 dCd%       } | t        j                        dCDfd&       } | t        j                        dC?fd'       }  | |      dCGfd(       }!t        j                         rFdd)lImJ}"mK}#mL}$mM}%mN}& dd*lOmP}'  | |"|#|$|&|%      	 	 dC?fd+       }( | |'j                        dCGfd,       }) | t        j                  j                        	 dEd d-	 dCd.       }* | t        jB                  j                  j                        dCd/       }+ | t        j                  j                  j                  j                        dC?Cfd0       }, | t        j                  j                  j                  j                        dC?Cfd1       }- | t        j                  j                  j                  j                        dC?Cfd2       }. | t        j                  j                  j                  j                        dC?Cfd3       }/ | t        j                  j                  j                  j                        dC?Cfd4       }0 | t        j                  j                  j                  j                        dC?Cfd5       }1 | t        j                  j                  j                  j                        dCCfd6       }2 | t        j                  j                  j                  j                        dCCfd7       }3 | t        j                  j                  j                  j                        dC?Cfd8       }4 | t        jX                  j                  j                        	 	 dCd9       }5 | t        jX                  j                  j                        	 	 dCEfd:       }6 | t        j                  j                  j                        	 	 dCd;       }7 | t        j                        dCCDfd<       }8 | t        jX                  j                        	 	 dCd=       }9 | t        jX                  j                        	 	 dC?fd>       }: | t        jX                  j                        	 	 dC?fd?       }; | t        jX                  j                        dCd@       }< | t        j                  j$                        dA        }= | t        j                        	 	 dC?fdB       }>FS )FzBuild 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   rO   )handlerfnfnshandlerss     rQ   	_registerzOTorchInGraphFunctionVariable._get_handlers.<locals>.register.<locals>._register  s5     +BX-1r1-#*HRL+ rP   r   )r   )r  r  r  s   ` rQ   registerz<TorchInGraphFunctionVariable._get_handlers.<locals>.register  s"     CF###rP   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                  fv r|j                          j                  t               | j                            S r   )rt   r8   rG   rF   rD   rE   rH   rI   rJ   mark_inconsistent_side_effectsr   rR   ry   r   r   rz   r   s       rQ   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QrP   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)rt   r8   r<   _dispatch_keysr   r   r   r  proxynodemeta_subclasses
FakeTensorDispatchKeySetDispatchKeyPythonPythonTLSSnapshotDispatchKeySetVariabler   )ry   r   r   rz   r  dkss         rQ   !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BrP   c                 f    t        j                  |t        j                  j	                               S r   )r&   buildr8   	overridesget_default_nowrap_functionsry   r   r   rz   s       rQ   #handle_get_default_nowrap_functionszWTorchInGraphFunctionVariable._get_handlers.<locals>.handle_get_default_nowrap_functions  s)     #((EOO@@B rP   c                 l    |j                  t        j                  |t        j                        ||      S r   )inline_user_function_returnr&   r%  r   accumulate_gradr(  s       rQ   handle_accumulate_grad_zKTorchInGraphFunctionVariable._get_handlers.<locals>.handle_accumulate_grad_  s/    11%%b)*C*CDdF rP   c                     t        ||      s5|j                  t        j                  |t        j
                        ||      S y r   )r   r+  r&   r%  r   radiansr(  s       rQ   handle_radianszBTorchInGraphFunctionVariable._get_handlers.<locals>.handle_radians  s>    0v>55#))"i.?.?@$  ?rP   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)ry   r   s     rQ    handle_is_inference_mode_enabledzTTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_inference_mode_enabled%  s4    TP&22&55	rP   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   rt   r8   r&  is_tensor_liker   r   )ry   r   argr   r  r  s      rQ   handle_is_tensorzDTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_tensor1  s_    #~.

eoo<<<s$=>CII';<'..t44'..u55rP   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   dtypert   r8   is_floating_pointr   
is_complexAssertionError)ry   r   input	input_argr   r  s       rQ   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4rP   c                     t        |      r4|j                         r$j                  t        |j                              S t        |      r|j                  |dg i       S y )Nnumel)r   
valid_sizer   r!   sizecall_method)ry   r   rD  r   r  s      rQ   handle_numelz@TorchInGraphFunctionVariable._get_handlers.<locals>.handle_numelJ  sX    %0U5E5E5G'..wuzz/BCCE>2((Wb"== 3rP   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.r2  )r   r   r   SUPPORTABLEr(  s       rQ   handle_torch_compilezHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_compileR  sJ    4yA~Aw:vYvh7lK&22	rP   c                 H    t        |      sJ |j                  |dg i       S NrJ  )r   rK  )ry   r   rD  r  s      rQ   handle_tensor_size_rewriteszOTorchInGraphFunctionVariable._get_handlers.<locals>.handle_tensor_size_rewritesb  s*    e^444$$RR88rP   c                 (    | j                  |||      S r   )_call_ntupler(  s       rQ   handle_ntuplezATorchInGraphFunctionVariable._get_handlers.<locals>.handle_ntupleg  s     $$Rv66rP   c                 t    t        j                         j                  t        j                               S r   )r   _guards_singletonr   r8   is_grad_enabled)ry   r   r   r   s     rQ   handle_is_grad_enabledzJTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_grad_enabledq  s,    *<<=#**5+@+@+BCCrP   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.r2  )r   r   r   rP  r   )ry   r   mode	warn_onlyr  s       rQ   #handle_use_deterministic_algorithmszWTorchInGraphFunctionVariable._get_handlers.<locals>.handle_use_deterministic_algorithmsv  se     Y99; a#D6i[A ?e*66	 399"d>U>U>WXXrP   c                 t    t        j                         j                  t        j                               S r   )r   rY  r   r8   $are_deterministic_algorithms_enabled)ry   r   r   r  s     rQ   +handle_are_deterministic_algorithms_enabledz_TorchInGraphFunctionVariable._get_handlers.<locals>.handle_are_deterministic_algorithms_enabled  s,    9KKL#**5+U+U+WXXrP   c                     t        t        j                         j                  |j                  j
                        S r   )r   r)   rY  r   symbolic_torch_function_statetorch_function_subclass_enabledry   r   r   s     rQ    handle_is_torch_function_enabledzTTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_torch_function_enabled  s6    6HHI $**00PP rP   c                     t        t        j                         j                  |j                  j
                         S r   )r   r)   rY  r   re  torch_function_mode_enabledrg  s     rQ   %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 rP   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   .0r   s     rQ   	<genexpr>z`TorchInGraphFunctionVariable._get_handlers.<locals>.handle_has_torch_function.<locals>.<genexpr>  s     9a&q)9s   )r   r   r/   unpack_var_sequencer   any)ry   r   r   elemsr   s       rQ   handle_has_torch_functionzMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_has_torch_function  s\     t9>ja-&H Q++B/ 
 $**9599 rP   c              3   :   K   | ]  \  }}|j                     y wr   )stream)ro  r   device_interfaces      rQ   rp  z=TorchInGraphFunctionVariable._get_handlers.<locals>.<genexpr>  s#      'A' !''s   c                 (    j                  ||      S r   )r   )ry   r   rv  r  s      rQ   handle_device_interface_streamzRTorchInGraphFunctionVariable._get_handlers.<locals>.handle_device_interface_stream  s     )//F;;rP   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`.r2  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   r8   	as_tensorr"   )ry   r   r   r  r   s      rQ   handle_from_numpyzETorchInGraphFunctionVariable._get_handlers.<locals>.handle_from_numpy  s    %% c*6+=+=*>?M O
  G bS*55	 %),bii,,#OO 'tR0
 #	 	rP   c                     |S r   rO   )ry   r   the_type	the_values       rQ   handle_jit_annotatezGTorchInGraphFunctionVariable._get_handlers.<locals>.handle_jit_annotate  s    rP   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   r8   tensorr@  r  r   r   cudnnis_acceptable)ry   r   r  extra
tensor_inpr   r  s        rQ   handle_cudnn_is_acceptablezNTorchInGraphFunctionVariable._get_handlers.<locals>.handle_cudnn_is_acceptable  st     EEE9fn5 D5 av||FMMRJ#**$$22:> rP   c                 H    t        j                  j                  |g|i |S r   )r   BackwardHookVariabler   r(  s       rQ   handle_backward_hookzHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_backward_hook  s#    1188MdMfMMrP   c                 .     | j                   |g|i |S r   )call_nn_parameterr(  s       rQ   handle_parameterzDTorchInGraphFunctionVariable._get_handlers.<locals>.handle_parameter  s    )4))">t>v>>rP   Nc                 2    ||j                  |d|gi       S y rS  rK  self_r   ry   dims       rQ   handle_sym_sizezCTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_size  s'     ''FSE2>> rP   c                 2    ||j                  |d|gi       S y )Nstrider  r  s       rQ   handle_sym_stridezETorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_stride  s%    ''HseR@@ rP   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   rt   r$   r   )r   r   r8   rV   r   rU   rS   )ry   r   r   rz   r   s        rQ   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~rP   c                     t        |      rkt        t        j                  j                  j
                        j                  ||gi       }t        t        j                        j                  |||g|      S y r   )r   r   r8   opsaten_local_scalar_denser   re   )ry   r   rJ  
fill_valuerz   r   r  s         rQ   handle_fullz?TorchInGraphFunctionVariable._get_handlers.<locals>.handle_full  si    *n55IINN66-ZL"5  4EJJ?MMv 	 6rP   c                     t        |      dk(  rLt        |d   t              s8|s5|j                  t	        j
                  |t        j                        ||      S y y y )Nr  r   )r   r   r.   r+  r&   r%  r   foreach_lerp_inplace)r   r   r   rz   s       rQ   "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~rP   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   r   r   rz   r  s       rQ   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~rP   c                     |j                         r|j                         s*t        |t        j                        r|j                         r d       S y y r   )is_python_constantr   r   r   r  evaluate_expr)ry   r   	conditionmessager   s       rQ   handle_assertzATorchInGraphFunctionVariable._get_handlers.<locals>.handle_assert.  sL    ,,.93O3O3Q9i&?&?@++-'-- . ArP   c           
           | |j                   j                  dt        j                  j                  gt        ||       |      S )Nr   )r  
param_vars)r   r   r8   r<   _SDPAParamsr"   )ry   r   r   rz   r  s       rQ   handle_sdpa_paramszFTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sdpa_params6  sM     ,bii,,#HH(( 'tV4
   rP   )_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.   rC  rt   r   r&   r%  )ry   r   r   r=  args_as_valueinvocation_resultr   s         rQ   &handle_constant_processgroup_functionszZTorchInGraphFunctionVariable._get_handlers.<locals>.handle_constant_processgroup_functionsL  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$   shaper  c                 :     j                   | gi ||dS )Nr  r   )r   r  r  r  kwargs_as_valuery   s      rQ   fn_with_prim_typeszaTorchInGraphFunctionVariable._get_handlers.<locals>.handle_from_local.<locals>.fn_with_prim_types~  s4    %4::)-<DIRX rP   zprim r   r   r   r  )NN)r   itemsrt   r   r   r   r"   )ry   r   r   rz   r   kvkwargs_to_be_proxiedr  r  r  r  s   `        @@rQ   handle_from_localzETorchInGraphFunctionVariable._get_handlers.<locals>.handle_from_localp  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} |rD|j                         t        j                  k(  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.r2  z4Attempted to use `nested_tensor` with non-list inputztensor_list=z'Change `nested_tensor` with list input.)
listsr  r   r8   stridedr   r   rP  r   r~  )ry   r   tensor_listr  r   rz   r  s          rQ   handle_nested_tensorzHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_nested_tensor  s     0&335F C%fX. ?5*66	 k+;< R*;-8 ?A*55	 =rP   c                     t        |      t        |      z   dk(  s7t        |      dk(  rB|d   j                         r.|d   j                         dk(  rt        dd| d| ddg	       y y y y )
Nr$   r   zOAttempted to use `torch.nn.functional.one_hot` with data-dependent output shaperN  rO  r]  zuExplicitly set the `num_classes` param of the function call `torch.nn.functional.one_hot` to something other than -1.r2  )r   r  r   r   r(  s       rQ   handle_one_hotzBTorchInGraphFunctionVariable._get_handlers.<locals>.handle_one_hot  s    4y3v;&!+D	QG..0G..0B6 m#D66(; ?T	 7 1 rP   c                     t        |      rZt        j                  j                  t        j
                  j                  j                  j                  |j                              S t        |      r|S y r   )
r   r   r   r   r8   r>   experimentalsymbolic_shapesguard_size_oblivioussym_numry   r   exprr   r  s      rQ   handle_guard_size_obliviouszOTorchInGraphFunctionVariable._get_handlers.<locals>.handle_guard_size_oblivious  sd    $0 !1188HH))99NN 
 D"23 4rP   c                     t        |      rZt        j                  j                  t        j
                  j                  j                  j                  |j                              S t        |      r|S y r   )
r   r   r   r   r8   r>   r  r  guard_or_truer  r  s      rQ   handle_guard_or_truezHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_guard_or_true  sa    $0 !1188HH))99GGU  D"23 4rP   c                     t        |      rZt        j                  j                  t        j
                  j                  j                  j                  |j                              S t        |      r|S y r   )
r   r   r   r   r8   r>   r  r  guard_or_falser  r  s      rQ   handle_guard_or_falsezITorchInGraphFunctionVariable._get_handlers.<locals>.handle_guard_or_false  sa    $0 !1188HH))99HHV  D"23 4rP   c                     t        |      rZt        j                  j                  t        j
                  j                  j                  j                  |j                              S t        |      r|S y r   )
r   r   r   r   r8   r>   r  r  statically_known_falser  r  s      rQ   handle_statically_known_falsezQTorchInGraphFunctionVariable._get_handlers.<locals>.handle_statically_known_false  sb    $0 1188HH))99PP 
 D"23 4rP   c                 Z   t        |      r|j                  }nBt        |      r|j                  }n)t        j                  j
                  j                  d      t        j                  j                  t        j                  j                  j                  j                  |            S )Nzbranch not supported)r   r  rt   r8   rD   excUnsupportedr   r   r   r>   r  r  guard_scalarry   r   r  valr   r  s       rQ   r  z@TorchInGraphFunctionVariable._get_handlers.<locals>.guard_scalar  s}    $0llD"23jjmm''334JKK--44%%55BB3G rP   c                     t        |      rZt        j                  j                  t        j
                  j                  j                  j                  |j                              S t        |      r|S y r   )
r   r   r   r   r8   r>   r  r  statically_known_truer  r  s      rQ   handle_statically_known_truezPTorchInGraphFunctionVariable._get_handlers.<locals>.handle_statically_known_true  sb    $0 1188HH))99OO 
 D"23 4rP   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   ro  r   r  s     rQ   rp  zUTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_and.<locals>.<genexpr>
       Aa:a1A   c              3   <   K   | ]  }|j                           y wr   r   rn  s     rQ   rp  zUTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_and.<locals>.<genexpr>       61!**,6   r  )allr   r8   r>   r  r  sym_andry   r   termsr  s      rQ   handle_sym_andzBTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_and  s]    A5AA&--HH))99AA66 ! .   BrP   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     rQ   rp  zTTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_or.<locals>.<genexpr>  r  r  c              3   <   K   | ]  }|j                           y wr   r  rn  s     rQ   rp  zTTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_or.<locals>.<genexpr>  r  r  r  )r  r   r8   r>   r  r  sym_orr  s      rQ   handle_sym_orzATorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_or  s]    A5AA&--HH))99@@66 ! .   BrP   c                 
   t        |      r|j                  }nt        |      r|j                  }ny t        j                  j                  t        j                  j                  j                  j                  |            S r   )r   r  rt   r   r   r   r8   r>   r  r  has_static_valuer  s       rQ   handle_has_static_valuezKTorchInGraphFunctionVariable._get_handlers.<locals>.handle_has_static_value  sd    $0llD"23jj--44%%55FFsK rP   c                 J    ddl m} t        |      j                  |g ||      S )Nr   )_unsafe_set_version_counter)tensor_version_opr  r   r   )ry   r   r   rz   r  s        rQ   !handle_unsafe_set_version_counterzUTorchInGraphFunctionVariable._get_handlers.<locals>.handle_unsafe_set_version_counter+  s*     H/+mB$01rP   c                 `     t         j                  j                  j                               S r   )r8   r<   r   peek_interpreter_stack)ry   r   r   rz   r  s       rQ   'handle_functorch_peek_interpreter_stackz[TorchInGraphFunctionVariable._get_handlers.<locals>.handle_functorch_peek_interpreter_stack5  s(     -##::< rP   c                     |d   j                   }t        t        j                  j                  j                  |            S Nr   )rt   FuncTorchInterpreterVariabler8   r   pyfunctorchcoerce_cinterpreter)ry   r   r   rz   cinterpreters        rQ   0handle_functorch_pyfunctorch_coerce_cinterpreterzdTorchInGraphFunctionVariable._get_handlers.<locals>.handle_functorch_pyfunctorch_coerce_cinterpreter?  s8      7==L/  ,,@@N rP   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r   rO   )ro  ycheck_any_unspecs     rQ   rp  ztTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_tensor.<locals>.check_any_unspec.<locals>.<genexpr>O  s     Dq/2Ds   F)r   r.   r/   rr  r  )r   r  r  r  s    rQ   r  zaTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_tensor.<locals>.check_any_unspecJ  s>    a./!BCL-#@ADAGGDDD !rP   r   data)r   r   r8   _refsr  r   )ry   r   r   rz   data_argr  r  r  s        @rQ   handle_torch_tensorzGTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_tensorH  sx    	! H76!!&> h7<LX<V 4EKK4F4FGUU$  =W7rP   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.r2  )re  
mode_stackr   r   r~  r3   register_mutationpop_torch_function_moder(  s       rQ   handle_pop_torch_functionzMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_pop_torch_functionc  sj     F**33>> S vJ*55	 +<<R@33KKMMrP   c                     t        |      dk(  r|rJ t        j                  |       |j                  j	                  |d          j                  d       S Nr$   r   )r   r3   r   re  push_torch_function_moder   r  s       rQ   handle_push_torch_functionzNTorchInGraphFunctionVariable._get_handlers.<locals>.handle_push_torch_functionu  sO     t9>&00*<<R@,,EEd1gN#**400rP   c                 l    |s|rJ j                  t        |j                  j                              S r   )r   r   re  r  r  s       rQ   handle_len_torch_functionzMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_len_torch_function~  s7     F**#**B44??@ rP   c                     t        |      dk(  r|rJ |d   j                         }|dk\  r"|t        |j                  j                        k  sJ |j                  j                  |   S r$  )r   r   re  r  )ry   r   r   rz   inds        rQ   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CrP   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 argumentsrN  rO  z<torch.get_device_module accepts 1 optional argument `device`r2  r   z.bad device argument to torch.get_device_modulezAExpected valid string/torch.device argument ('cpu', 'cuda', etc.)r3  r4  r5  r6  from_excr}   )maxsplitr  )r   r   r   r~  r   r8   get_device_moduler   r   ro   r   rp   r   ID_MATCHr   r   r   rsplitr&   r%  )	ry   r   r   rz   r  moduleero   
new_sources	            rQ   handle_get_device_modulezLTorchInGraphFunctionVariable._get_handlers.<locals>.handle_get_device_module  sL   4y3v;&*v(&:P H#D66(; ^*55	#H-@@BF!!W779F!F008 ~1$++>f//0E0EFG#v55cA5FrJJ #((VZ@@#   L#D66(; c9-889 s   AD 	E&EEc                     t        j                  |       |d   j                         r)|d   j                         t        j                  |       nt        j
                  |       j                  d       S r  )r3   r   r  r   clear_default_device!register_device_context_insertionr   r  s       rQ   handle_set_default_devicezMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_set_default_device  sc     +<<R@Aw))+Q0J0J0L0T.CCBG.PPQST#**400rP   r   )Fr   )otorch.backends.cudar
  r   r   r  r   r  r  r  r  builderr  r   rR   dispatch_key_set_functionsr8   r&  r'  r   r  inductoraccumulate_grad_defaultr   r/  is_inference_mode_enabled	is_tensorr<  rA  rB  rH  compile!REWRITE_OPS_TO_TENSOR_SIZE_METHODrK   rL   utils_single_pair_triple
_quadruple_ntuplerZ  use_deterministic_algorithmsrb  r<   _is_torch_function_enabled_is_torch_function_all_disabledr   has_torch_function_variadichas_torch_function_unarydictfromkeysr   
from_numpyr9   annotater   r  r  hooksBackwardHook	Parameterr  sym_sizeint
sym_strideaddcdivre   _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  r  r  r  r  r  r  r  r  	_autogradr  r   r  r  r  r  _pop_torch_function_stack_push_on_torch_function_stack_len_torch_function_stack_get_function_stack_atr0  set_default_device)Ir	  r
  r  r#  r)  r-  r0  r9  r>  rF  rL  rQ  rT  rW  r[  r`  rc  rh  rk  rt  ry  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r
  r  r  r  r"  r&  r(  r+  r6  r:  r   r  r   r  r  r  r  r  r  r   sI                                                                  @@@@@@@@@@rQ   _get_handlersz*TorchInGraphFunctionVariable._get_handlers  s	   
 	 	3	
 	
 	
 	>	*,	-	R-	R 
.	R 
-	.	C-	C 
/	C6 
%//>>JJ	K		-		 
L		 
%))$$55==	>	 
?	
 
$,,		 
 	 
%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 
%--	 	 
!	 
%**		 
	 
%&&	'	*	 
(	 
%$$	%	 
&	 
%--	 	. 
!	. 
*			 
		 ++-  9'"'4D1DD: g(() *B 
%,,,,	- 	
 	'	 
.	> 
%((%%--	.	 
/	  
%((''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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J 
%**	+	1-	1 
,	1$ rP   r   r5   r   rz   r   r&   c                 ~  567 ddl m}m5m} ddlm} | j                  r9dd lmc m	} ddlm
}m}	 ddlm6 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 ]I  }|j9                         } |	|      r|j9                         j:                  }t=        d
d| dddg       K |j4                  D cg c]  }|j?                          }}	 |jA                         }| jN                  767fd} ||      \  }}|jP                  jS                  7jT                   d|      }|jP                  jS                  7jT                  dz         }tW        |      |jX                  _+        tW        |      |jX                  _+        ||g|}|jP                  j[                  d ||i       }	  |||      }|S | jg                  |||      rti        || ||      S | jk                         rtm        ||      r| jN                  tn        v r=tq        | jr                        }tu        |jw                  tx        jz                               	 |j}                   | jA                         |D  cg c]  } | jA                          c} i |j5                         D !"ci c]  \  }!}"|!|"jA                          c}"}!      S | j                         rD| jN                  jT                  }$|rt1        |d   t*        j                        r|d   }%|%jr                  t        t\        j                  j                  |$      rt        t\        j                  j                  |$      7t        7d%      rt        77j                         d         ryt\        j                  j                  t        77j                         d         j                  v r8t=        d&d'd(t        | jN                         d)g tJ        jL                  d*       | j                  |||      S | j                         j                  | jN                        }&|&r |&| |g|i |}'|'r|'S t        5fd+|D              }(t        d, |D              })t        | jN                  d-d'      d.k(  r| jN                  jT                  t        v r{|(ry|)rwd/t        | jN                         d0}*t        j                  |*       t=        d1d2| jN                   d3| d4| d5t        | jN                         d6g tJ        jL                         | jN                  }+|(rSd7| jN                  jT                   },t        | jN                  d-d       d8k(  r t        t\        |,      rt        t\        |,      }+d }-d }.d9|v r|d9   }.t1        |.t$        t6        f      rng }-|.j4                  D ]]  }/t1        |/t*        j                        r.|/j                  jX                  j                  d:   j                  }0nd }0|-j                  |0       _ t1        |.t*        j                        r-|.j                  jX                  j                  d:   j                  }- || |jP                  jZ                  d |+gt        ||       ;      }1t1        |1|      rKd<|v rG|d<   jA                         r4t=        d=d2| jN                   d3| d4| d>d?d@gtJ        jL                         |-t1        |-t              rt        |.j4                  |-      D ]  \  }2}3|3	t1        |2|      sJ |2j                  jX                  j                  d:   }4|3|4j                  k7  rPt=        dAd2| jN                   d3| d4| dB| jN                   dC|3 dD|4j                   d)g tJ        jL                         t\        j                  j                  |4      rt=        dEdF| jN                   d3| d4| d>g tJ        jL                          |1S t1        |.|      sJ d:|.j                  jX                  j                  v sJ |.j                  jX                  j                  d:   }4|-|4j                  k7  rPt=        dGd2| jN                   d3| d4| dB| jN                   dC|- dD|4j                   d)g tJ        jL                         t\        j                  j                  |4      s2t=        dHdF| jN                   d3| d4| d>g tJ        jL                         |1S c c}w # |$ r}|jB                  j9                         }|j:                  }dd lm"c m#} |jI                  |      r-t=        dd| d| dd| ddgtJ        jL                  |       n1t=        dd| d| dd| ddd| dgtJ        jL                  |       Y d }~d }~ww xY w# t\        j^                  j`                  jb                  t\        j^                  j`                  jd                  f$ r0 t=        d!d"7jT                   d#g tJ        jL                         Y |S w xY wc c} w c c}"}!w # t~        t        t        f$ rI}#t        tW        |#      |t        t        |j|                  |#j                              $       Y d }#~#d }#~#ww xY w)INr$   )r   r  r  r  r   )func_to_graphableis_graphable_type)fake_tensor_tls)tree_flatten)#AsPythonConstantNotImplementedErrorr   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`r2  zVInput marked with `pytree.register_constant` constructed in the `torch.compile` regionzInput=z, offending type <zWCalling a `nonstrict_trace`-ed function with an input that contains an object of type <z>, which was marked with `pytree.register_constant`. However, the object was constructed _inside_ the `torch.compile` region. This is not supported.zXConstruct the object _outside_ the `torch.compile` region, or submit an issue to GitHub.r-  z>Invalid use of pytree_flatten with nonstrict_trace-ed functionzCalling a `nonstrict_trace`-ed function where one of the inputs has been registered with a `pytree_flatten` that places an object of type <z> into the context.zLModifying the `pytree_flatten` to avoid placing the object into the context.zApply one of the following to <z>:
* `torch.utils._pytree.register_constant`
* `torch.utils._pytree.register_dataclass`
* `torch.utils._pytree.register_pytree_node`c                  f    j                   }d_         	  | i |}|_         |S # |_         w xY wNT)allow_non_fake_inputs_override)r   rz   old_valresrq  r  s       rQ   
patched_fnz>TorchInGraphFunctionVariable.call_function.<locals>.patched_fn7  sH     *HHAE>Md-f-CELOB
 FMOBs   ' 	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.)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.c              3   6   K   | ]  }t        |        y wr   r  r  s     rQ   rp  z=TorchInGraphFunctionVariable.call_function.<locals>.<genexpr>  s     &T!z!_'E&Tr  c              3   p   K   | ].  }t        |t        j                  t        j                  f       0 y wr   )r   r   r   r  rn  s     rQ   rp  z=TorchInGraphFunctionVariable.call_function.<locals>.<genexpr>  s0      !
 q955y7P7PQR!
s   46r   r8   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=rO  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  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)ar   r   r  r  r<  r  r   "torch._higher_order_ops.flat_apply_higher_order_ops
flat_applyro  rp  torch._subclasses.fake_tensorrq  torch.utils._pytreerr  basers  r/   r%  r*   r   UserFunctionVariabler   r   r   r  r.   python_typer   r   r   r   vtrE  _pytreeis_constant_classr   rP  rt   r   %register_static_attr_and_return_proxyr   typer  r   r8   rD   r  r  TorchRuntimeErrortorch_function_override_enabledr1   r   r   #constant_fold_functions_need_guardsr   ro   r   rp   r   EQUALS_MATCHr   OverflowError	TypeError
ValueErrorr   listmapr   is_tensor_methodr   r  r  r   r|  Taginplace_viewtagsr%   call_tensor_methodrl  getrr  r  bin_opsr   r   warningr  r  r  appendr"   zip_prims_commonis_contiguous)8ry   r   r   rz   r   r  r  r  ro  rp  rr  rs  packed_input_vtout_vtflat_args_vtsinput_spec_vtflat_arg_vtarg_type	type_nameproxified_flat_args
input_specr4  typpytreery  r   f_specf_spec_proxyinput_spec_proxyall_argsr  ro   r   r  r  r  r   
tensor_varspecial_handlerr   any_symints_or_symfloatsall_ints_or_floatsmsgfn_torch_sym_opsaved_out_shapesout_kwarg_vtr  r  tensor_variableout_tensor_vtsaved_out_shapefake_outr  rq  r  s8                                                        @@@rQ   r   z*TorchInGraphFunctionVariable.call_function  sA    	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$ T"=i[ K3
K	. ;H:M:M#+6$$&# #'*==?
P B
 **5IAv 99JJ;;-u%vL  "yyNNm+Z  &*&\L")-j)9!!&$&6M9LMH II**?JRTUE
 'r51" M//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
47I,D,DE!!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   !
!
 

 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!"i&>&>? " 2 2? C I I $$++E23 ,	(@(@A#/#5#5#:#:#?#?#P#V#V '("))(( #40
 76)'::<[djj\ixH;D6 '22		 ' *D169 &&$7 $2M? '. !%m^DDD,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 ; ",???&,*<*<*A*A*F*FFFF'--2277H#x~~5 % I"%djj\ix P:4::, G66F5GGWX`XfXfWgghj.::
 **88B % X"-djj\iPVx X$C.::	 g
# 7 %dd&&(,,	44++C0$ x"(7I)TV W((1{ 3jj
 w.:: "# % `"(7I)TV WVV_U``su k=i[ IK K /:: "#+%^ !!--!!33	  !U(6h ;-99: M!@ @P "9j9 (Ic"2"9"9388DE sh   f 6f% 	i 'k kk 3k	k %i*BiiA7kkk l<3>l77l<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.r2  )has_unpack_var_sequencer   r/   r  rq  r  r   r   r8   rK   rL   rE  rJ  r   r   )rt   countr   s    rQ   rW  z@TorchInGraphFunctionVariable._call_ntuple.<locals>.handle_ntupleb  s    ,,R0 ..22267  ))+ 1188HH$$**2259%:R:R:TU  !n$UG, ?T	rP   )rt   r8   rK   rL   rE  rJ  r   __closure__cell_contentsr   rX  r   LambdaVariable)ry   r   r   rz   rW  r  s    `   @rQ   rV  z)TorchInGraphFunctionVariable._call_ntupleY  s    ::))//777G..0EJJ**1-;;E%%%%z	( ::))//777++M:: a))rP   c           
      *   |j                   r t        ddddgt        j                         t	        |t
        j                        r	 |j                         }t	        |t
        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                         }
t	        |
t
        j                        sJ tB        jD                  jF                  |
_        d!|
_$        |
S # t        $ r' t        dd| ddgt        j                         Y ~w xY w# t        $ r/}t        dd
| ddgt        j                  |       Y d}~Gd}~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.r2  z=non-constant `requires_grad` argument to `torch.nn.Parameter`zrequires_grad=z$Change `requires_grad` to be a bool.z1`torch.nn.Parameter()` with unsupported data typezdata=z7Called `torch.nn.Parameter()` with non-Tensor argument.zBEnsure the argument to `torch.nn.Parameter()` is a `torch.Tensor`.z?Attempted to use `torch.nn.Parameter()` constructor with DynamozDynamo does not support thiszDTry to construct `torch.nn.Parameter()` outside the compiled region.z@If this is not possible, turn `graph_break_on_nn_param_ctor` offzDAttempted to use torch.nn.Parameter constructor with tensor subclassz:`torch.nn.Parameter`: cannot convert to traceable tracablez+convert_tracable_parameter is set to False.zDCheck usage of context manager: do_not_convert_to_tracable_parameterr  r@  r  z8`torch.nn.Parameter` with non-constant Tensor attributeszBEnsure the Tensor argument's shape, dtype, and device are correct.r-  Ndetachr$   rn  r   rn   F)%exportr   r   rP  r   r   r&   r   NotImplementedErrorr~  r  ro   _nn_param_via_prefix_insertr   graph_break_on_nn_param_ctorr2   r
   
class_typer   r   	DIFFICULTtuplevar_getattrr   synthetic_graph_inputr   r  rK  r<  r  r   r   r   r8   rK   rV  has_grad_fn)rs   r   r  r  r  r@  r  r4  placeholderr  r   s              rQ   r  z.TorchInGraphFunctionVariable.call_nn_parameter{  s    99M;D&22	 mY%>%>? - @ @ B $	 8 89KvUX&11	 ;;222t]KK..Y:ZV '22		 .
/@^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%ufm'L
 ##B"b9D*II"")+"6"6"89	 %%
 &)":":;;;!HH.. # ] ' 	 [,]O< ?>*55	 	F # 
	Rv;X&11 	 	
	s+   J' A)K ',KK	L#$L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.nnrV  )load_import_from)cgs   rQ   <lambda>zJTorchInGraphFunctionVariable._nn_param_via_prefix_insert.<locals>.<lambda>  s    !4!4Z!M rP   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.r2  )r  ) r   new_varroot_txr   add_push_nullro   r   r   r   storepregraph_bytecodeextendget_instructionsr   r  opr   r   r  r   r8   rK   rV  example_value_from_input_noder&   r%  realizer   r  guards_contextdynamo_guardsremove_guards_with_source)	r   r  r  varname	data_nodero   r  r   r  s	           @rQ   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	
 rP   c                 f    |d   j                  || j                         j                  |dd  |      S )Nr   r$   )rK  r   r   r(  s       rQ   r  z/TorchInGraphFunctionVariable.call_tensor_method&  s2    Aw""2t'8'8':'C'CT!"XvVVrP   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   r   r  r8   r<   
TensorBase)ry   r  s     rQ   r  z-TorchInGraphFunctionVariable.is_tensor_method)  s    3 &&t'8'8':; H))+^<H!!#00EHH4G4GG8  $5$77		8rP   c                     | j                         t               v xsL t        | j                         t        j                  j
                  t        j                  j                  f      xr t        |||      S r   )r   rZ   r   r8   _ops
OpOverloadOpOverloadPacketr0   r(  s       rQ   r  z<TorchInGraphFunctionVariable.torch_function_override_enabled2  sd    #<#>> !!#&&

(C(CD< *"dF;	<rP   r   r   r   ru  )r   r   r   r   rx   r   r   r   r   	functoolscacherl  r   r&   r   rV  r   r  r  r  r  r  r   r   s   @rQ   r   r     s    J7m# m __D  DLN#N 'N -	N
 
N` *D ~ ~@ ' 'RW8<rP   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 r   )r!  )rt   rz   s     rQ   r   zDispatchKeySetVariable.create?  s    %e6v66rP   c                 f    t        |j                  t        j                                | ||      S rm   )r   rp   r   DISPATCH_KEY_SET_MATCHrr   s      rQ   ru   z)DispatchKeySetVariable.create_with_sourceC  s(    f''(K(KLM5((rP   c                 
    |dv S )N)hasrO   )ry   r   s     rQ   is_constant_fold_methodz.DispatchKeySetVariable.is_constant_fold_methodH  s    wrP   r   rz   r6   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   rt   r   r   r   r   r  EnumVariabler  rw   rK  )
ry   r   r   r   rz   methodr   r  r  r{   s
            rQ   rK  z"DispatchKeySetVariable.call_methodK  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   ru   r  r  r&   rP  r   rK  r   r   s   @rQ   r!  r!  <  sh    )7 7 ) ); ?#	;
 S/)*; 
; ;rP   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 rm   )r   rp   r   r1  rr   s      rQ   ru   z/FuncTorchInterpreterVariable.create_with_sourced  s(    f''(=(=>?5((rP   r   rz   r6   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  rt   r  r+  r  r  __func__r   r   r   0TemporarilyPopInterpreterStackCtxManagerVariablerw   rK  )ry   r   r   r   rz   r{   s        rQ   rK  z(FuncTorchInterpreterVariable.call_methodi  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::rP   )r   r   r   r   r   ru   r  r&   rP  r   rK  r   r   s   @rQ   r  r  a  sO    F) ); ?#	;
 S/)*; 
; ;rP   r  )r   r  r   loggingr   r   collections.abcr   typingr   r   r   r   torch._Cr8   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   rw  r   r  r   r   guardsr   r   ro   r   r   r   r   rE  r   r   r   r    r!   r"   r#   r  r%   r&   ctx_managerr'   r(   r)   dictsr*   distributedr+   r,   r   r-   r  r.   r/   torch_functionr0   r1   r2   r3   numpyr}  ModuleNotFoundError#torch.distributed.fsdp._fully_shardr4   torch._dynamo.symbolic_convertr5   	getLoggerr   r   rP  rQ  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   rK   r   r   r   r   _shape_as_tensorrD  acceleratorcurrent_device_indexcurrent_deviceis_initializedxpur  r]  rG   _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_enabledrB  rA  rd  
_Reductionget_enumpromote_types_get_privateuse1_backend_name_is_checkpoint_validr   r  get_rankget_world_sizer  boolrR   r  r  _dispatch_tls_local_include_set_dispatch_tls_local_exclude_setr=  rZ   rk   r   r   r!  r  rO   rP   rQ   <module>r;     sb  6     	 $ 9 9     ( ' K > >  
 @ < 0    + 
 % B ' . E
 D g! $''!!77!!,,''//--%%44))@@))??))FF		((  ,,  //  ((  1188		##,,

$$-- 	&&227! B %)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,,   $#A #ALY7#4 Y7xI<#4 I<X,";. ";J;#4 ;K8  	B
  s$   \ \ \\\\