
    rh                    B   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- ddl.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5 ddl6m7Z7m8Z8 ddl9m:Z:m;Z;m<Z< ddl=m>Z> ddl?m@Z@mAZA ddlBmCZCmDZD ddlEmFZFmGZGmHZHmIZI 	 ddlJZK	 ddlMmNZN erddlOmPZP  ej                  eR      ZSeTj                  ej                  j                  j                  ej                  j                  j                  ej                  j                  j                  ej                  j                  j                  ej                  j                  j                  ej                  j                  ej                  j                  ej                  j                  j                  ej                  j                  j                  ej                  j                  j                  ej                  j                  j                  ej                  j                  j                  ej                  j                  j                  ej                  j                  j                  ej                  j                  j                  ej                  j                  j                  ej                  j                  j                  ej                  j                  j                  j                  ej                  j                  j                  j                  ej                  j                  j                  ej                  j                  j                  g      ZweTj                  ej                  g      Zyej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  gZe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                  j(                  ej                  j                  ej                  j
                  gez   Zej~                  j                         rSej-                  ej~                  j                  ej~                  j.                  ej~                  j0                  g       eTj                  e      ZeTj                  e      Zej2                  deTe
g e	f   ee   f   fd       ZeTj                  g d      Zej                  j:                  ej                  j<                  ej                  j>                  hZej2                  d        Z G d de8      Z G d  d!e      Z G d" d#e      Z G d$ d%e      Z G d& d'e      Zy# eL$ r dZKY ?w xY w# eL$ r dZNY Fw 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)unimplementedunimplemented_v2)GuardBuilderinstall_guard)CallFunctionNoArgsSourceSyntheticLocalSource)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)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                  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onnxis_in_onnx_export_dynamoexternal_utilsis_compiling_utilscompileris_dynamo_compilingis_exportingnnmodules
activation_is_make_fx_tracing     p/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/torch/_dynamo/variables/torch.pytracing_state_functionsrN      s     					e##T  ..

$$e$$114!!4##T**D##T##77 rL   )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)	itertoolsrU   torch.overridesrV   setfrom_iterablevaluesr5   ones	ones_likezeros
zeros_likeemptyfullupdate)rU   get_overridable_functions_funcsmores       rM   rV   rV      sp    W##$>$@$G$G$IJKE



%D 
LLLrL   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valuerk   s      rM   create_with_sourcez$BaseTorchVariable.create_with_source   s(    f''(C(CDE5((rL   c                 2    t        |   di | || _        y )NrK   )super__init__rp   )selfrp   kwargs	__class__s      rM   rt   zBaseTorchVariable.__init__   s    "6"
rL   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rerP   extend_outputsetup_globally_cached)ru   codegennameunique_var_names       rM   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   ru   s    rM   as_proxyzBaseTorchVariable.as_proxy       zzrL   c                     | j                   S r   r   r   s    rM   as_python_constantz$BaseTorchVariable.as_python_constant   r   rL   c                 l    t        | j                  |      }t        j                  j	                  |      S r   )hasattrrp   r   ConstantVariablecreate)ru   txr   results       rM   call_obj_hasattrz"BaseTorchVariable.call_obj_hasattr   s)    T*))0088rL   c                 \    | j                   t        v ryt        | j                   dd       dk(  S )NTr}   math)rp   constant_fold_functionsgetattrr   s    rM   can_constant_fold_throughz+BaseTorchVariable.can_constant_fold_through  s*    ::00tzz<6&@@rL   r3   N)r   r   r   r2   )r~   r}   __qualname____doc__classmethodrq   rt   r   r   r   r   r   __classcell__rw   s   @rM   rg   rg      s7    R) )
9ArL   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 implementationsr3   c                 "    d| j                    dS )NzTorchCtxManagerClassVariable()r   r   s    rM   __repr__z%TorchCtxManagerClassVariable.__repr__
  s    .tzzl!<<rL   c                 \    t        |       } t        |       xr t        |       xr | t        v S r   )r!   callabler   supported_ctx_manager_classesr   s    rM   is_matching_clsz,TorchCtxManagerClassVariable.is_matching_cls  s6     "%( UO  ;::	
rL   r   r2   argsrv   dict[str, VariableTracker]r$   c           
      L   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                  u rft!        |      dk(  st!        |      dk(  rd|v sJ t!        |      dk(  r|d   n|d   }d|v 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_functionrK   z$Profiler function %s will be ignored)only_subclassr   backendsset_priority)I r   r   r   r   r   r   r   r   r   r   r   r   rp   r5   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&   r9   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_hooksr1   FSDPParamGroupuse_training_staterG   	attentionsdpa_kernel_sdpa_kernel_variadicrs   )ru   r   r   rv   r   r   r   r   r   r   r   r   r   r   r   r   ctxinf_moder   xr   r   argrw   s                          rM   r   z*TorchCtxManagerClassVariable.call_function  s   	
 	
 	
 	
 ::&4yA~*Q,,EE# '--b%8((T6::'..r599ZZ5,,,4yA~*Q,,EE# '--b$7((T6::#**2t44ZZ5111c$i1n#**DG..0d +   ZZ5///t9>c&kQ&6667:4yA~tAw1134H(//H==__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9t9>c&kQ&6:;OPP"%d)q.tAwfZ6HH5Cv5M6.1SXL%,,H//1<  ZZ588--CCC%,,>#S++->  w$Rv66S 0 00 ?s   (`
3`
(`!
)r~   r}   r   r   strr   staticmethodr   r   r$   r   r   r   s   @rM   r   r     sa    V=# = 
 
G7#G7 'G7 -	G7
 
G7 G7rL   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 graphr3   c                 T    t        |   |fi | ddlm} | ||      }|| _        y )Nr   )is_nonstrict_trace_callable)rs   rt   trace_rulesr   nonstrict_traceable)ru   rp   r   rv   r   rw   s        rM   rt   z%TorchInGraphFunctionVariable.__init__  s1    )&)=&"=e"D#6 rL   c                 <    d| j                    d| j                   dS )NzTorchInGraphFunctionVariable(z, nonstrict_traceable=r   )rp   r   r   s    rM   r   z%TorchInGraphFunctionVariable.__repr__  s%    .tzzl:PQUQiQiPjjkllrL   c                     | j                   S r   r   r   s    rM   get_functionz)TorchInGraphFunctionVariable.get_function  r   rL   c                    >?@ABCDEFG i EEfd} ddl m} ddlm>m?m@mAmBmCm	D ddl
mFmG  | t                	 	 dB>fd       } | t         	 	 dBd       } | t        j                   j"                  j$                        	 	 dBd	       } | t        j&                  j(                  j*                  j,                        dBd
       } | t.        j0                        dBd       } | t        j2                        dBd       } | t        j4                  t        j                   j6                        dB>CDfd       } | t        j8                  t        j:                        dB>Cfd       }	 | t        j<                        dB>Cfd       }
 | t        j>                        dBd       } | t@         dBCf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                        dBd       } | t        jR                        >@fd       } | t        jT                        	 dC	 dB?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                        dB>fd       } | td        jg                  d ti               D               dBAfd       } | t        jj                        dBCGfd       } | t        jl                  jn                        dBd       } | t        jp                  jr                  jt                        	 	 dB>Cfd       } | t        jF                  jv                  jx                        dBd       } | t        jB                  jz                        dBd       } | t        j&                  j|                  j~                  t        j&                  j|                  j~                  j                        dDd!       } | t        j&                  j|                  j                  t        j&                  j|                  j                  j                        dDd"       } | t        j                        dBd#       } | t        j                        Cfd$       } | t        j                        	 	 dBd%       } | t        j                        dBCfd&       } | t        j                        dB>fd'       }  | |      dBFfd(       }!t        j                         rFdd)lImJ}"mK}#mL}$mM}%mN}& dd*lOmP}'  | |"|#|$|&|%      	 	 dB>fd+       }( | |'j                        dBFfd,       }) | t        j                  j                        	 dDd d-	 dBd.       }* | t        jB                  j                  j                        dBd/       }+ | t        j                  j                  j                  j                        dB>Bfd0       }, | t        j                  j                  j                  j                        dB>Bfd1       }- | t        j                  j                  j                  j                        dB>Bfd2       }. | t        j                  j                  j                  j                        dB>Bfd3       }/ | t        j                  j                  j                  j                        dB>Bfd4       }0 | t        j                  j                  j                  j                        dB>Bfd5       }1 | t        j                  j                  j                  j                        dBBfd6       }2 | t        j                  j                  j                  j                        dBBfd7       }3 | t        j                  j                  j                  j                        dB>Bfd8       }4 | t        jX                  j                  j                        	 	 dBd9       }5 | t        jX                  j                  j                        	 	 dBDfd:       }6 | t        j                  j                  j                        	 	 dBd;       }7 | t        j                        dBBCfd<       }8 | t        jX                  j                        	 	 dBd=       }9 | t        jX                  j                        	 	 dB>fd>       }: | t        jX                  j                        	 	 dB>fd?       }; | t        jX                  j                        dBd@       }< | t        j                        	 	 dB>fdA       }=ES )EzBuild 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   rK   )handlerfnfnshandlerss     rM   	_registerzOTorchInGraphFunctionVariable._get_handlers.<locals>.register.<locals>._register  s5     +BX-1r1-#*HRL+ rL   r   )r   )r  r  r  s   ` rM   registerz<TorchInGraphFunctionVariable._get_handlers.<locals>.register  s"     CF###rL   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   )rp   r5   rC   rB   r@   rA   rD   rE   rF   mark_inconsistent_side_effectsr   rN   ru   r   r   rv   r   s       rM   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QrL   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   r5   r9   _dispatch_keysr   r   r   r
  proxynodemeta_subclasses
FakeTensorDispatchKeySetDispatchKeyPythonPythonTLSSnapshotDispatchKeySetVariabler   )ru   r   r   rv   r  dkss         rM   !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BrL   c                 f    t        j                  |t        j                  j	                               S r   )r$   buildr5   	overridesget_default_nowrap_functionsru   r   r   rv   s       rM   #handle_get_default_nowrap_functionszWTorchInGraphFunctionVariable._get_handlers.<locals>.handle_get_default_nowrap_functions  s)     #((EOO@@B rL   c                 l    |j                  t        j                  |t        j                        ||      S r   )inline_user_function_returnr$   r!  r   accumulate_gradr$  s       rM   handle_accumulate_grad_zKTorchInGraphFunctionVariable._get_handlers.<locals>.handle_accumulate_grad_  s/    11%%b)*C*CDdF rL   c                     t        ||      s5|j                  t        j                  |t        j
                        ||      S y r   )r   r'  r$   r!  r   radiansr$  s       rM   handle_radianszBTorchInGraphFunctionVariable._get_handlers.<locals>.handle_radians  s>    0v>55#))"i.?.?@$  ?rL   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)ru   r   s     rM    handle_is_inference_mode_enabledzTTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_inference_mode_enabled  s4    TP&22&55	rL   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   r5   r"  is_tensor_liker   r   )ru   r   r   r   r
  r  s      rM   handle_is_tensorzDTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_tensor(  s_    #~.

eoo<<<s$=>CII';<'..t44'..u55rL   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   r5   is_floating_pointr   
is_complexAssertionError)ru   r   input	input_argr   r
  s       rM   handle_is_floating_pointzLTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_floating_point3  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4rL   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)ru   r   r?  r   r
  s      rM   handle_numelz@TorchInGraphFunctionVariable._get_handlers.<locals>.handle_numelA  sX    %0U5E5E5G'..wuzz/BCCE>2((Wb"== 3rL   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       rM   handle_torch_compilezHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_compileI  s    4yA~AwVWrL   c                 H    t        |      sJ |j                  |dg i       S NrE  )r   rF  )ru   r   r?  r
  s      rM   handle_tensor_size_rewriteszOTorchInGraphFunctionVariable._get_handlers.<locals>.handle_tensor_size_rewritesQ  s*    e^444$$RR88rL   c                 (    | j                  |||      S r   )_call_ntupler$  s       rM   handle_ntuplezATorchInGraphFunctionVariable._get_handlers.<locals>.handle_ntupleV  s     $$Rv66rL   c                 t    t        j                         j                  t        j                               S r   )r   _guards_singletonr   r5   is_grad_enabled)ru   r   r   r   s     rM   handle_is_grad_enabledzJTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_grad_enabled`  s,    *<<=#**5+@+@+BCCrL   c                 ~    |r|j                         rt        d       j                  ||j                               S )Nz2torch.use_deterministic_algorithms(warn_only=True))r   r   r   )ru   r   mode	warn_onlyr  s       rM   #handle_use_deterministic_algorithmszWTorchInGraphFunctionVariable._get_handlers.<locals>.handle_use_deterministic_algorithmse  s8     Y99;RS299"d>U>U>WXXrL   c                 t    t        j                         j                  t        j                               S r   )r   rQ  r   r5   $are_deterministic_algorithms_enabled)ru   r   r   r  s     rM   +handle_are_deterministic_algorithms_enabledz_TorchInGraphFunctionVariable._get_handlers.<locals>.handle_are_deterministic_algorithms_enabledm  s,    9KKL#**5+U+U+WXXrL   c                     t        t        j                         j                  |j                  j
                        S r   )r   r'   rQ  r   symbolic_torch_function_statetorch_function_subclass_enabledru   r   r   s     rM    handle_is_torch_function_enabledzTTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_torch_function_enabledr  s6    6HHI $**00PP rL   c                     t        t        j                         j                  |j                  j
                         S r   )r   r'   rQ  r   r\  torch_function_mode_enabledr^  s     rM   %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 rL   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     rM   	<genexpr>z`TorchInGraphFunctionVariable._get_handlers.<locals>.handle_has_torch_function.<locals>.<genexpr>  s     9a&q)9s   )r   r   r,   unpack_var_sequencer   any)ru   r   r   elemsr   s       rM   handle_has_torch_functionzMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_has_torch_function  s\     t9>ja-&H Q++B/ 
 $**9599 rL   c              3   :   K   | ]  \  }}|j                     y wr   )stream)rf  r|   device_interfaces      rM   rg  z=TorchInGraphFunctionVariable._get_handlers.<locals>.<genexpr>  s#      'A' !''s   c                 (    j                  ||      S r   )r   )ru   r   rm  r  s      rM   handle_device_interface_streamzRTorchInGraphFunctionVariable._get_handlers.<locals>.handle_device_interface_stream  s     )//F;;rL   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_clsr   r  r  )	r   trace_numpyr   npr   r   r5   	as_tensorr    )ru   r   r   r
  r   s      rM   handle_from_numpyzETorchInGraphFunctionVariable._get_handlers.<locals>.handle_from_numpy  sh    %%MNHI$),bii,,#OO 'tR0
 #	 	rL   c                     |S r   rK   )ru   r   the_type	the_values       rM   handle_jit_annotatezGTorchInGraphFunctionVariable._get_handlers.<locals>.handle_jit_annotate  s    rL   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   r5   tensorr;  r|  r   r   cudnnis_acceptable)ru   r   r}  extra
tensor_inpr   r
  s        rM   handle_cudnn_is_acceptablezNTorchInGraphFunctionVariable._get_handlers.<locals>.handle_cudnn_is_acceptable  st     EEE9fn5 D5 av||FMMRJ#**$$22:> rL   c                 H    t        j                  j                  |g|i |S r   )r   BackwardHookVariabler   r$  s       rM   handle_backward_hookzHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_backward_hook  s#    1188MdMfMMrL   c                 .     | j                   |g|i |S r   )call_nn_parameterr$  s       rM   handle_parameterzDTorchInGraphFunctionVariable._get_handlers.<locals>.handle_parameter  s    )4))">t>v>>rL   Nc                 2    ||j                  |d|gi       S y rK  rF  self_r   ru   dims       rM   handle_sym_sizezCTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_size  s'     ''FSE2>> rL   c                 2    ||j                  |d|gi       S y )Nstrider  r  s       rM   handle_sym_stridezETorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_stride  s%    ''HseR@@ rL   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   r5   rR   r   rQ   rO   )ru   r   r   rv   r   s        rM   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~rL   c                     t        |      rkt        t        j                  j                  j
                        j                  ||gi       }t        t        j                        j                  |||g|      S y r   )r   r   r5   opsaten_local_scalar_denser   ra   )ru   r   rE  
fill_valuerv   r   r
  s         rM   handle_fullz?TorchInGraphFunctionVariable._get_handlers.<locals>.handle_full  si    *n55IINN66-ZL"5  4EJJ?MMv 	 6rL   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   rv   s       rM   "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~rL   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   rv   r
  s       rM   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~rL   c                     |j                         r|j                         s*t        |t        j                        r|j                         r d       S y y r   )is_python_constantr   r   r   r	  evaluate_expr)ru   r   	conditionmessager   s       rM   handle_assertzATorchInGraphFunctionVariable._get_handlers.<locals>.handle_assert  sL    ,,.93O3O3Q9i&?&?@++-'-- . ArL   c           
           | |j                   j                  dt        j                  j                  gt        ||       |      S )Nr   )r  
param_vars)r   r   r5   r9   _SDPAParamsr    )ru   r   r   rv   r  s       rM   handle_sdpa_paramszFTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sdpa_params  sM     ,bii,,#HH(( 'tV4
   rL   )_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   r   r$   r!  )ru   r   r   r   args_as_valueinvocation_resultr   s         rM   &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"   shaper  c                 :     j                   | gi ||dS )Nr  r   )r   r  r  r  kwargs_as_valueru   s      rM   fn_with_prim_typeszaTorchInGraphFunctionVariable._get_handlers.<locals>.handle_from_local.<locals>.fn_with_prim_typesS  s4    %4::)-<DIRX rL   zprim r   r   r   r  )NN)r   itemsrp   r~   r   r   r    )ru   r   r   rv   r   kvkwargs_to_be_proxiedr  r  r  r  s   `        @@rM   handle_from_localzETorchInGraphFunctionVariable._get_handlers.<locals>.handle_from_localE  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  r   r5   stridedr   r   )ru   r   tensor_listr  r   rv   r  s          rM   handle_nested_tensorzHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_nested_tensorg  s@     0&335FSTk+;<AB =rL   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  r   r   r$  s       rM   handle_one_hotzBTorchInGraphFunctionVariable._get_handlers.<locals>.handle_one_hotw  sa    4y3v;&!+D	QG..0G..0B6R 7 1 rL   c                     t        |      rZt        j                  j                  t        j
                  j                  j                  j                  |j                              S t        |      r|S y r   )
r   r   r   r   r5   r;   experimentalsymbolic_shapesguard_size_oblivioussym_numru   r   exprr   r	  s      rM   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 4rL   c                     t        |      rZt        j                  j                  t        j
                  j                  j                  j                  |j                              S t        |      r|S y r   )
r   r   r   r   r5   r;   r  r  guard_or_truer  r  s      rM   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 4rL   c                     t        |      rZt        j                  j                  t        j
                  j                  j                  j                  |j                              S t        |      r|S y r   )
r   r   r   r   r5   r;   r  r  guard_or_falser  r  s      rM   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 4rL   c                     t        |      rZt        j                  j                  t        j
                  j                  j                  j                  |j                              S t        |      r|S y r   )
r   r   r   r   r5   r;   r  r  statically_known_falser  r  s      rM   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 4rL   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  rp   r5   r@   excUnsupportedr   r   r   r;   r  r  guard_scalarru   r   r  valr   r	  s       rM   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 rL   c                     t        |      rZt        j                  j                  t        j
                  j                  j                  j                  |j                              S t        |      r|S y r   )
r   r   r   r   r5   r;   r  r  statically_known_truer  r  s      rM   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 4rL   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   rf  r   r	  s     rM   rg  zUTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_and.<locals>.<genexpr>       Aa:a1A   c              3   <   K   | ]  }|j                           y wr   r   re  s     rM   rg  zUTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_and.<locals>.<genexpr>       61!**,6   r  )allr   r5   r;   r  r  sym_andru   r   termsr	  s      rM   handle_sym_andzBTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_and  s]    A5AA&--HH))99AA66 ! .   BrL   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     rM   rg  zTTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_or.<locals>.<genexpr>  r  r  c              3   <   K   | ]  }|j                           y wr   r  re  s     rM   rg  zTTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_or.<locals>.<genexpr>  r  r  r  )r  r   r5   r;   r  r  sym_orr  s      rM   handle_sym_orzATorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_or  s]    A5AA&--HH))99@@66 ! .   BrL   c                 
   t        |      r|j                  }nt        |      r|j                  }ny t        j                  j                  t        j                  j                  j                  j                  |            S r   )r   r  rp   r   r   r   r5   r;   r  r  has_static_valuer  s       rM   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 rL   c                 J    ddl m} t        |      j                  |g ||      S )Nr   )_unsafe_set_version_counter)tensor_version_opr  r   r   )ru   r   r   rv   r  s        rM   !handle_unsafe_set_version_counterzUTorchInGraphFunctionVariable._get_handlers.<locals>.handle_unsafe_set_version_counter  s*     H/+mB$01rL   c                 `     t         j                  j                  j                               S r   )r5   r9   r   peek_interpreter_stack)ru   r   r   rv   r  s       rM   'handle_functorch_peek_interpreter_stackz[TorchInGraphFunctionVariable._get_handlers.<locals>.handle_functorch_peek_interpreter_stack  s(     -##::< rL   c                     |d   j                   }t        t        j                  j                  j                  |            S Nr   )rp   FuncTorchInterpreterVariabler5   r   pyfunctorchcoerce_cinterpreter)ru   r   r   rv   cinterpreters        rM   0handle_functorch_pyfunctorch_coerce_cinterpreterzdTorchInGraphFunctionVariable._get_handlers.<locals>.handle_functorch_pyfunctorch_coerce_cinterpreter  s8      7==L/  ,,@@N rL   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   rK   )rf  ycheck_any_unspecs     rM   rg  ztTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_tensor.<locals>.check_any_unspec.<locals>.<genexpr>  s     Dq/2Ds   F)r   r+   r,   ri  r  )r   r	  r
  r  s    rM   r  zaTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_tensor.<locals>.check_any_unspec	  s>    a./!BCL-#@ADAGGDDD !rL   r   data)r   r   r5   _refsr}  r   )ru   r   r   rv   data_argr  r	  r
  s        @rM   handle_torch_tensorzGTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_tensor  sx    	! H76!!&> h7<LX<V 4EKK4F4FGUU$  =W7rL   c                     |s|rJ |j                   j                  st        d      t        j                  |       |j                   j                         S )Nz/Popping from an empty torch function mode stack)r\  
mode_stackr   r0   register_mutationpop_torch_function_moder$  s       rM   handle_pop_torch_functionzMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_pop_torch_function"  sM     F**33>>#$UVV*<<R@33KKMMrL   c                     t        |      dk(  r|rJ t        j                  |       |j                  j	                  |d          j                  d       S Nr"   r   )r   r0   r  r\  push_torch_function_moder   r  s       rM   handle_push_torch_functionzNTorchInGraphFunctionVariable._get_handlers.<locals>.handle_push_torch_function,  sO     t9>&00*<<R@,,EEd1gN#**400rL   c                 l    |s|rJ j                  t        |j                  j                              S r   )r   r   r\  r  r  s       rM   handle_len_torch_functionzMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_len_torch_function5  s7     F**#**B44??@ rL   c                     t        |      dk(  r|rJ |d   j                         }|dk\  r"|t        |j                  j                        k  sJ |j                  j                  |   S r  )r   r   r\  r  )ru   r   r   rv   inds        rM   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CrL   c                     t        j                  |       |d   j                         r)|d   j                         t        j                  |       nt        j
                  |       j                  d       S r  )r0   r  r  r   clear_default_device!register_device_context_insertionr   r  s       rM   handle_set_default_devicezMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_set_default_deviceE  sc     +<<R@Aw))+Q0J0J0L0T.CCBG.PPQST#**400rL   r   )Fr   )ntorch.backends.cudar  r   r   r  r   r  r	  r
  r  builderr  r   rN   dispatch_key_set_functionsr5   r"  r#  __wrapped__r  inductoraccumulate_grad_defaultr   r+  is_inference_mode_enabled	is_tensorr8  r<  r=  rC  compile!REWRITE_OPS_TO_TENSOR_SIZE_METHODrG   rH   utils_single_pair_triple
_quadruple_ntuplerR  use_deterministic_algorithmsrY  r9   _is_torch_function_enabled_is_torch_function_all_disabledr   has_torch_function_variadichas_torch_function_unarydictfromkeysr   
from_numpyr6   annotater   r~  r  hooksBackwardHook	Parameterr  sym_sizeint
sym_strideaddcdivra   _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_atset_default_device)Hr  r  r  r  r%  r)  r,  r5  r9  rA  rG  rI  rL  rO  rS  rW  rZ  r_  rb  rk  rp  rv  rz  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r
  r  r  r  r  r!  r%  r   r  r   r  r	  r
  r  r  r  r   sH                                                                 @@@@@@@@@@rM   _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 
%++		> 
	> 
%--	 	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	 
7	 
%((::	;	 
<	 
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
	 
%((''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 
%**	+	1-	1 
,	1$ rL   r   r2   r   rv   r   r$   c                 f  345 ddl m}m3m} ddlm} | j                  r3dd lmc m	} ddlm
}m}	 ddlm4 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                  545fd} ||      \  }}|jL                  jO                  5jP                   d|      }|jL                  jO                  5jP                  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                         rD| jJ                  jP                  }#|rt1        |d   t*        j                        r|d   }$|$jd                  ts        tt        jv                  jx                  |#      rt{        tt        jv                  jx                  |#      5ts        5d      rts        55j}                         d         rytt        j~                  j                  t{        55j}                         d         j                  v r8t        dddt        | jJ                         dg t        j                  d       | j                  |||      S | j                         j                  | jJ                        }%|%r |%| |g|i |}&|&r|&S t        3f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 ts        tt        |+      rt{        tt        |+      }*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%   }0|d$   j                  jT                  j                  d%   }1|,|0j                  k7  rt=        d)       tt        j                  j                  |1      st=        d*       |-S t1        |-|      r<|-jJ                  /t1        |d$   |      ryd%|d$   j                  jT                  j                  v sJ |d$   j                  jT                  j                  d%   }1tt        j                  j                  |1      st=        d*       |-S t1        |d$   t6              rt        |d$   j4                        D ]w  \  }2} d%| j                  jT                  j                  v sJ | j                  jT                  j                  d%   }1tt        j                  j                  |1      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 }~d }~ww xY wc c} w c c}"}!w )-Nr"   )r   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   rv   old_valresr]  r  s       rM   
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   	overloadszInplace op on input tensorr   z6Attempted to trace an inplace view op on input tensor ry   z/Ensure you do not modify input tensor in place.r.  c              3   6   K   | ]  }t        |        y wr   r  r  s     rM   rg  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	  re  s     rM   rg  z=TorchInGraphFunctionVariable.call_function.<locals>.<genexpr>  s0      !
 q955y7P7PQR!
s   46r}   r5   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_r   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 )Xr   r   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   r   r   vtr1  _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_MATCHr   is_tensor_methodr   r5   r  r  r   rh  Taginplace_viewtagsr   r#   r   SUPPORTABLEcall_tensor_methodrX  getri  r  bin_opsr   r   warningr  r  r  r    zip_size_prims_commonis_contiguous	enumerate)6ru   r   r   rv   r   r
  r  rp  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pytreere  r|   f_specf_spec_proxyinput_spec_proxyall_argsr  rk   r   r  r  r   
tensor_varspecial_handlerr   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  s6                                                      @@@rM   r   z*TorchInGraphFunctionVariable.call_functionZ  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    "::&&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#
 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Q# 7 dd&&(,,	44++C0!``i_j k "V W`  Va a~ H  ~I I	b <Ls+   f.1f3 ?h(,h-3h%8A"h  h%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,   listrh  r  r   r   r5   rG   rH   r1  r6  r   r   )rp   countr   s    rM   rO  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IrL   )rp   r5   rG   rH   r1  r6  r   __closure__cell_contentsr   rD  r   LambdaVariable)ru   r   r   rv   rO  r  s    `   @rM   rN  z)TorchInGraphFunctionVariable._call_ntuple  s    ::))//777G..0EJJ**1-;;E%%%%z	J ::))//777++M:: a))rL   c           
         |j                   rt        d       t        |t        j                        r	 |j                         }t        |t        j                        st        d| d       |j                  r| j                  |||      S t        |t              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t0        |j3                         |j3                         fi       |j                        }
t        |
t        j                        sJ t4        j6                  j8                  |
_        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;  r|  zParameter not python_constant: Ndetachr"   rZ  r   rj   F)exportr   r   r   r$   r   NotImplementedErrorr
  rk   _nn_param_via_prefix_insertr/   r
   
class_typer   tuplevar_getattrr   synthetic_graph_inputr   rm  rF  r'  r  r   r   r   r5   rG   rB  has_grad_fn)ro   r   r  rm  r  r;  r|  r  placeholderr  r   s              rM   r  z.TorchInGraphFunctionVariable.call_nn_parameter  s    99OPmY%>%>?K - @ @ B $	 8 89OD61BCD ;;222t]KK.
/@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.. # q ' KIJK, # 	A;A3?@@	As*   H A)H6 H32H36	I?IIc                 X   | 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,                  | ||      }|j/                         }t1        j2                         j4                  j6                  j9                  |       |S )Nc                  (     j                  dd      S )Nztorch.nnrB  )load_import_from)cgs   rM   <lambda>zJTorchInGraphFunctionVariable._nn_param_via_prefix_insert.<locals>.<lambda>  s    !4!4Z!M rL   r   F)r  get_attrzAUnexpected type of data placeholder op for parameter construction)r   new_varr   add_push_nullrk   r   r   r   storepregraph_bytecodeextendget_instructionsr   r  opr   r   r5   rG   rB  example_value_from_input_noder$   r!  realizer   r  guards_contextdynamo_guardsremove_guards_with_source)	r   r  rm  varname	data_noderk   r  r   r  s	           @rM   r  z8TorchInGraphFunctionVariable._nn_param_via_prefix_insert  sD    ))##% r]
MN
4;;
9%%m45
E"

		##**2+>+>+@AMMO((	<<::S
 &g.**II33DMMO4H4HI
 !&&r=&A! 	++99SS	
 rL   c                 f    |d   j                  || j                         j                  |dd  |      S )Nr   r"   )rF  r   r~   r$  s       rM   r  z/TorchInGraphFunctionVariable.call_tensor_method  s2    Aw""2t'8'8':'C'CT!"XvVVrL   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  r5   r9   
TensorBase)ru   r  s     rM   r~  z-TorchInGraphFunctionVariable.is_tensor_method  s    3 &&t'8'8':; H))+^<H!!#00EHH4G4GG8  $5$77		8rL   c                     | j                         t               v xsL t        | j                         t        j                  j
                  t        j                  j                  f      xr t        |||      S r   )r   rV   r   r5   _ops
OpOverloadOpOverloadPacketr-   r$  s       rM   r{  z<TorchInGraphFunctionVariable.torch_function_override_enabled%  sd    #<#>> !!#&&

(C(CD< *"dF;	<rL   r   r   r   ra  )r~   r}   r   r   rt   r   r   r   r   	functoolscacherX  r   r$   r   rN  r   r  r  r  r~  r{  r   r   s   @rM   r   r     s    J7m# m __^
  ^
@|#| '| -	|
 
||	*6 @ @D    DW8<rL   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  )rp   rv   s     rM   r   zDispatchKeySetVariable.create2  s    %e6v66rL   c                 f    t        |j                  t        j                                | ||      S ri   )r   rl   r   DISPATCH_KEY_SET_MATCHrn   s      rM   rq   z)DispatchKeySetVariable.create_with_source6  s(    f''(K(KLM5((rL   c                 
    |dv S )N)hasrK   )ru   r   s     rM   is_constant_fold_methodz.DispatchKeySetVariable.is_constant_fold_method;  s    wrL   r   rv   r3   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   r   r   r   r  EnumVariabler  rs   rF  )
ru   r   r   r   rv   methodr   r  r  rw   s
            rM   rF  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   r   r   rq   r  r  r$   r<  r   rF  r   r   s   @rM   r  r  /  sh    )7 7 ) ); ?#	;
 S/)*; 
; ;rL   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      rM   rq   z/FuncTorchInterpreterVariable.create_with_sourceW  s(    f''(=(=>?5((rL   r   rv   r3   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'  rt  r  __func__r   r   r   0TemporarilyPopInterpreterStackCtxManagerVariablers   rF  )ru   r   r   r   rv   rw   s        rM   rF  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::rL   )r~   r}   r   r   r   rq   r  r$   r<  r   rF  r   r   s   @rM   r  r  T  sO    F) ); ?#	;
 S/)*; 
; ;rL   r  )r   r  r   loggingr   r   collections.abcr   typingr   r   r   r   torch._Cr5   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   rn  r   r  r   r   guardsr   r   rk   r   r   r1  r   r   r   r   r   r    r!   rs  r#   r$   ctx_managerr%   r&   r'   dictsr(   distributedr)   r*   r  r+   r,   torch_functionr-   r.   r/   r0   numpyrt  ModuleNotFoundError#torch.distributed.fsdp._fully_shardr1   torch._dynamo.symbolic_convertr2   	getLoggerr~   r   r<  r=  r   r   r   r   r   r   r   r9   r   r   r   r   r   r   r   r   r   r   r   r   	grad_moder   r   r   r   r   r   r   r   rG   r   r   r   r   _shape_as_tensorr0  acceleratorcurrent_device_indexcurrent_deviceis_initializedxpur|  rI  rC   _get_device_index_get_cublas_allow_tf32_is_any_autocast_enabledrJ  get_device_propertiesget_autocast_dtypeget_autocast_gpu_dtypeget_default_dtypeis_autocast_cache_enabledis_autocast_cpu_enabledis_autocast_enabledr=  r<  rP  
_Reductionget_enumpromote_types_get_privateuse1_backend_name_is_checkpoint_validr   r  get_rankget_world_sizer  boolrN   r  r  _dispatch_tls_local_include_set_dispatch_tls_local_exclude_setr(  rV   rg   r   r   r  r  rK   rL   rM   <module>r$     sb  6     	 $ 9 9     ( ' K > >  
 @ 1 0 C   + 
 % B . E
 D g! $''!!77!!,,''//--%%44))@@))??))FF		((  ,,  //  ((  1188		##,,

$$--&&00+! 6 %)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]7#4 ]7@E<#4 E<P$";. ";J;#4 ;}/  	B
  s$   \ \ \\\\