o
    Ih                     @   s6  d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z	 ddl
ZddlZddlZddlZddlmZ ddlmZ ddlmZ dd	lmZmZmZ dd
lmZ ddlmZmZmZ ddlm Z  ddl!m"Z" ddl#m$Z$m%Z% ddl&m'Z'm(Z( ddl)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0 ddl1m2Z2 ddl3m4Z4m5Z5m6Z6 ddl7m8Z8 ddl9m:Z:m;Z; ddl<m=Z=m>Z> ddl?m@Z@mAZAmBZB zddlCZDW n eEy   dZDY nw zddlFmGZG W n eEy   dZGY nw e	rddlHmIZI eJeKZLeMNejOjOjPejQjRjSejQjRjTejQjOjPejQjOjUejVjWejXjYjZejXj[j\ejXj[j]ejXj[j^ej_j`jaejQjbjcejQjbjdejQjbjeejQjbjfejQjgjhejij_j`jaejjj_j`jaejkjljmejkjljngZoeMNejpgZqejjjrgZsejtejujvejVjwejVjxejjjyejjjzej9jzej{ej|ej}ej~ejejejejejkjjjejejVjejQjges Zej9z reej9jej9jej9jg eMNesZseMNeZejjdejjdejVjdejjjdejjdejjjdejujdejjdejjdejjdejkjjjdiZeMNg dZejVjejVjejVjhZeddd ZG dd  d e2ZG d!d" d"eZG d#d$ d$eZG d%d& d&eZG d'd( d(eZdS ))ak  
This module implements variable tracking for torch functions and operations during Dynamo tracing.

It provides classes to handle different types of torch operations:

TorchInGraphFunctionVariable: Handles torch.* functions that should be captured in the FX graph.
Provides special handling for constant folding, tensor methods, and torch function overrides.
Manages complex cases like out= variants and parameter construction.

TorchCtxManagerClassVariable: Handles torch context managers like torch.no_grad(), autocast, etc.
Provides implementations for entering/exiting these contexts during tracing.

DispatchKeySetVariable: Represents torch.DispatchKeySet for managing dispatch keys and
device-specific operations during tracing.

The module includes special handling for:
- Constant folding of pure functions
- Tensor method calls
- torch.nn.Parameter construction
- __torch_function__ overrides
- Context manager state tracking
- Device and dtype management

This is a core part of Dynamo's tracing system, translating torch operations into
traceable graph nodes while preserving correct semantics and handling edge cases.
    N)Sequence)TYPE_CHECKING)TracingContext)warning_once)"is_traceable_wrapper_subclass_type   )config	polyfills	variables)	PyCodegen)!can_convert_to_tracable_parameternew_parameter_placeholdertracable_create_parameter) get_registered_device_interfaces)unimplemented)GuardBuilderinstall_guard)CallFunctionNoArgsSourceSyntheticLocalSource)check_unspec_or_constant_argsguard_if_dynhas_torch_functionhashableproductproxy_args_kwargsunwrap_if_wrapper   )VariableTracker)AutocastModeVariableProfilerContextVariableTorchFunctionDisableVariable)ConstDictVariable)DistributedVariableProcessGroupVariable)ListVariableTupleVariable)can_dispatch_torch_functiondispatch_torch_functionTorchFunctionModeStackVariable)_fsdp_param_group)InstructionTranslatorFT)addsubmuldivsqrtc                  C   sV   ddl m}  ddlm} t| |  }tjtj	tj
tjtjtjh}|| |S )Nr   )chain)get_overridable_functions)	itertoolsr0   torch.overridesr1   setfrom_iterablevaluestorchones	ones_likezeros
zeros_likeemptyfullupdate)r0   get_overridable_functions_funcsmore rB   Q/var/www/vscode/kcb/lib/python3.10/site-packages/torch/_dynamo/variables/torch.pyr1      s   
r1   c                       sX   e Zd ZdZedd Zd fddZdd	 Zd
d Zdd Z	dddZ
dd Z  ZS )BaseTorchVariablezHcommon base for all torch.* functions, classes, modules and other thingsc                 C      t |tj | ||dS Nsource)r   
make_guardr   FUNCTION_MATCHclsvaluerH   rB   rB   rC   create_with_source      z$BaseTorchVariable.create_with_sourcereturnNc                    s   t  jdi | || _d S )NrB   )super__init__rM   )selfrM   kwargs	__class__rB   rC   rR      s   
zBaseTorchVariable.__init__c                 C   sh   z| j j d| j j }W n ty   dt| j  }Y nw dtdd| }|||| j  d S )N.
torch_obj___z[^a-zA-Z0-9_]+_)	rM   
__module____name__	Exceptionidrer,   extend_outputsetup_globally_cached)rS   codegennameunique_var_namerB   rB   rC   reconstruct   s   zBaseTorchVariable.reconstructc                 C      | j S NrM   rS   rB   rB   rC   as_proxy      zBaseTorchVariable.as_proxyc                 C   rf   rg   rh   ri   rB   rB   rC   as_python_constant   rk   z$BaseTorchVariable.as_python_constanttxr*   c                 C   s   t | j|}tj|S rg   )hasattrrM   r
   ConstantVariablecreate)rS   rm   rc   resultrB   rB   rC   call_obj_hasattr   s   z"BaseTorchVariable.call_obj_hasattrc                 C   s    | j tv rdS t| j dd dkS )NTr[   math)rM   constant_fold_functionsgetattrri   rB   rB   rC   can_constant_fold_through   s   
z+BaseTorchVariable.can_constant_fold_throughrP   Nrm   r*   )r\   r[   __qualname____doc__classmethodrN   rR   re   rj   rl   rr   rv   __classcell__rB   rB   rU   rC   rD      s    


rD   c                       sP   e Zd ZdZdefddZedd Zddd	ee	 d
dddf fddZ
  ZS )TorchCtxManagerClassVariablezLPoints to a context manager class in torch.* that dynamo has implementationsrP   c                 C   s   d| j  dS )NzTorchCtxManagerClassVariable()rh   ri   rB   rB   rC   __repr__   s   z%TorchCtxManagerClassVariable.__repr__c                 C   s    t | } t| ot| o| tv S rg   )r   callabler   supported_ctx_manager_classesrh   rB   rB   rC   is_matching_cls   s
   z,TorchCtxManagerClassVariable.is_matching_clsrm   r*   argsrT   dict[str, VariableTracker]r   c              	      s  ddl m}m}m}m}m}m}	m}
m}m	}m
}m}m} | jtju rDt|dkr>t|d tjjr>|	|d}||||S |	|dS | jtju rlt|dkrft|d tjjrf|	|d}||||S |	|dS | jtju rt|dkr|	j||d  ddS | jtju rt|dkrt|dksJ t|dkr|d  nd}|
||S t| jrt| jtjrddlm } ||||j!"d| jd	i S | jtj#j$j%tj&j#j%tj'j#j%fv rt(| j||S | jtj)j*tj)j+tj,j)j*tj,j)j+fv rt-t.d
| j t/ S | jtj0j1u r|s|rJ t2|S | jtj3j4j5u r1t|dks+J |||S | jtj3j6j7u rHt|dksCJ ||S | jtj,j8j9u ret|dksZJ ||dd |D S | jtj,j8j:u r|t|dkswJ ||S | jtj3j6j;u rt|dksJ ||S | jtj3j6j<u rt|dksJ ||dd |D S | jtj,j=j>u rt|dksJ |||d  S t?d ur| jt?j@jAu rt|dksJ |||d |d  S | jtjBjCjDu r$t|dkst|dkrd|v sJ t|dkr|d n|d }||| S | jtjBjCjEu r8||dd |D S tF |||S )Nr   )!DisabledSavedTensorsHooksVariableDualLevelContextManager&FSDPParamGroupUseTrainingStateVariable&GradIncrementNestingCtxManagerVariable)GradInplaceRequiresGradCtxManagerVariableGradModeVariableInferenceModeVariable%JvpIncrementNestingCtxManagerVariableSDPAKernelVariableSetFwdGradEnabledContextManagerStreamVariable&VmapIncrementNestingCtxManagerVariabler   FT)initialized)wrap_fx_proxy_clscall_functionrB   z$Profiler function %s will be ignoredr   c                 S      g | ]}t |qS rB   r   .0xrB   rB   rC   
<listcomp>i      z>TorchCtxManagerClassVariable.call_function.<locals>.<listcomp>c                 S   r   rB   r   r   rB   rB   rC   r   w  r   backendsc                 S      g | ]}|  qS rB   rl   r   argrB   rB   rC   r     r   )G r   r   r   r   r   r   r   r   r   r   r   r   rM   r7   no_gradlen
isinstancer
   	functionsBaseUserFunctionVariablerp   r   enable_gradset_grad_enabledrl   inference_modeinspectisclass
issubclassStreamtorch._dynamo.variables.builderr   outputcreate_proxyampautocast_modeautocastcudacpur   profilerprofilerecord_functionautogradr   logr   _CDisableTorchFunctionSubclassr    
_functorchvmapvmap_increment_nestingeager_transformsjvp_increment_nesting
forward_ad_set_fwd_grad_enabled
dual_levelgrad_increment_nestingenable_inplace_requires_gradgraphdisable_saved_tensors_hooksr)   FSDPParamGroupuse_training_statenn	attentionsdpa_kernel_sdpa_kernel_variadicrQ   )rS   rm   r   rT   r   r   r   r   r   r   r   r   r   r   r   r   ctxinf_moder   r   rU   rB   rC   r     s   8





*z*TorchCtxManagerClassVariable.call_function)r\   r[   ry   rz   strr   staticmethodr   r   r   r   r|   rB   rB   rU   rC   r}      s    
r}   c                       s   e Zd ZdZd!d" fddZdefddZdd	 Zee	
dd
d Zdddee ddddfddZd#ddZed$ddZed#ddZdd Zdd Zdd  Z  ZS )%TorchInGraphFunctionVariablez@Points to a torch function/method that should be put in FX graphNrP   c                    s:   t  j|fi | ddlm} |d u r||}|| _d S )Nr   )is_nonstrict_trace_callable)rQ   rR   trace_rulesr   nonstrict_traceable)rS   rM   r   rT   r   rU   rB   rC   rR     s
   
z%TorchInGraphFunctionVariable.__init__c                 C   s   d| j  d| j dS )NzTorchInGraphFunctionVariable(z, nonstrict_traceable=r~   )rM   r   ri   rB   rB   rC   r     s   z%TorchInGraphFunctionVariable.__repr__c                 C   rf   rg   rh   ri   rB   rB   rC   get_function  rk   z)TorchInGraphFunctionVariable.get_functionc            4         sT  i fdd} ddl m} ddlm mmmmmm	 ddl
mm	 | t 			di fd
d}| t 			didd}| tjjj			didd}| tjjjjdidd}| tjdidd}| tjtjjdi fdd}| tjtjdi fdd}| tjdi fdd}	| tjdidd}
| t difdd}| tj j!j"j#tj j!j"j$tj j!j"j%tj j!j"j&tj j!j"j'didd}| tj( fd d!}| tj)	"dj		difd#d$}| tj* fd%d&}| tj+j, fd'd(}| tjj-tjj.tjj/di fd)d*}| t01d+d, t2 D  difd-d.}| tj3di	fd/d0}| tj4j5did1d2}| tj6j7j8			di fd3d4}| tj"j9j:did5d6}| tj j;did7d8}| tjj<j=tjj<j=j>dkd:d;}| tjj<j?tjj<j?j>dkd<d=}| tj@did>d?}| tjAfd@dA}| tjB			didBdC}| tjCdifdDdE}| tjDdi fdFdG}| |difdHdI}tEF rddJlGmH} mI}!mJ}"mK}#mL}$ ddKlMmN}% | | |!|"|$|#			di fdLdM}&| |%jOdifdNdO}'| tjPjQ	9dkd9dP		didQdR}(| tj jRjSdidSdT})| tjTjUjVjWdi fdUdV}*| tj+jXjY			didWdX}+| tj+jZj[			difdYdZ},| tjZj\j]			did[d\}-| tj^difd]d^}.| tj+j_			did_d`}/| tj+j`			di fdadb}0| tj+ja			di fdcdd}1| tj+jbdidedf}2| tjc			di fdgdh}3S )lzBuild 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                     s"    fdd}t  d sJ |S )Nc                    s&    D ]}|vsJ || |< q| S rg   rB   )handlerfn)fnshandlersrB   rC   	_register  s   
zOTorchInGraphFunctionVariable._get_handlers.<locals>.register.<locals>._registerr   )r   )r   r   )r   )r   rC   register  s   z<TorchInGraphFunctionVariable._get_handlers.<locals>.registerr   )
SDPAParamsr   )ro   DeterministicAlgorithmsVariabler   StreamContextVariableSymNodeVariableTensorVariableUserDefinedObjectVariable)wrap_fx_proxyr   rm   r*   c                    sN   |s|rJ | j tjjtjjjtjjtjjtjjfv r|	   
t| j  S rg   )rM   r7   _utilsis_compiling_dynamoexternal_utilscompileris_dynamo_compilingis_exportingmark_inconsistent_side_effectsrp   tracing_state_functionsrS   rm   r   rT   ro   rB   rC   handle_tracing_state_functions  s   zRTorchInGraphFunctionVariable._get_handlers.<locals>.handle_tracing_state_functionsc                 _   s   |rJ | j tjjfv rLt|dksJ t|d tjsJ |d jj	j
d }|  |}t|tjjrG|tjtjjj tjtjjj }t|S |rPJ t|   S )Nr   r   example_value)rM   r7   r   _dispatch_keysr   r   r
   r   proxynodemeta_subclasses
FakeTensorDispatchKeySetDispatchKeyPythonPythonTLSSnapshotDispatchKeySetVariablerp   )rS   rm   r   rT   r   dksrB   rB   rC   !handle_dispatch_key_set_functions  s$   

zUTorchInGraphFunctionVariable._get_handlers.<locals>.handle_dispatch_key_set_functionsc                 _   s   t |tj S rg   )r   buildr7   	overridesget_default_nowrap_functionsr   rB   rB   rC   #handle_get_default_nowrap_functions  s   
zWTorchInGraphFunctionVariable._get_handlers.<locals>.handle_get_default_nowrap_functionsc                 _   s   | t|tj||S rg   )inline_user_function_returnr   r   r	   accumulate_gradr   rB   rB   rC   handle_accumulate_grad_  s   zKTorchInGraphFunctionVariable._get_handlers.<locals>.handle_accumulate_grad_c                 _   s&   t ||s|t|tj||S d S rg   )r   r  r   r   r	   radiansr   rB   rB   rC   handle_radians  s
   
zBTorchInGraphFunctionVariable._get_handlers.<locals>.handle_radiansc                    sB   t |s| jtjju rt |rt|jdr dS  dS )N__torch_function__TF)r   rM   r7   r   is_tensor_likern   rp   )rS   rm   r   )ro   r   r   rB   rC   handle_is_tensor  s   



zDTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_tensorc                    sd   |}t |r.|jd ur0| jtju r |jjS | jtju r& |jjS td| j d S d S )Nzcalling )r   dtyperM   r7   is_floating_pointrp   
is_complexAssertionError)rS   rm   input	input_argro   r   rB   rC   handle_is_floating_point  s   zLTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_floating_pointc                    s@   t |r| r t|jS t |r||dg i S d S )Nnumel)r   
valid_sizerp   r   sizecall_methodrS   rm   r  r  rB   rC   handle_numel   s
   
z@TorchInGraphFunctionVariable._get_handlers.<locals>.handle_numelc                 _   s    t |dkr
|d S td d S )Nr   r   z:torch.compile is used as a decorator in the compiled frame)r   r   r   rB   rB   rC   handle_torch_compile(  s   zHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_compilec                    s   t | sJ ||dg i S Nr  )r   r  r  r   rB   rC   handle_tensor_size_rewrites0  s   zOTorchInGraphFunctionVariable._get_handlers.<locals>.handle_tensor_size_rewritesc                 _   s   |  |||S rg   )_call_ntupler   rB   rB   rC   handle_ntuple5  s   zATorchInGraphFunctionVariable._get_handlers.<locals>.handle_ntuplec                       t j  t S rg   )r   _guards_singletonrp   r7   is_grad_enabledrS   rm   )ro   r   rB   rC   handle_is_grad_enabled?     
zJTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_grad_enabledFc                    s$   |r
|  r
td  ||  S )Nz2torch.use_deterministic_algorithms(warn_only=True))rl   r   rp   )rS   rm   mode	warn_only)r   rB   rC   #handle_use_deterministic_algorithmsD  s   zWTorchInGraphFunctionVariable._get_handlers.<locals>.handle_use_deterministic_algorithmsc                    r  rg   )r   r  rp   r7   $are_deterministic_algorithms_enabledr!  )ro   r   rB   rC   +handle_are_deterministic_algorithms_enabledL  r#  z_TorchInGraphFunctionVariable._get_handlers.<locals>.handle_are_deterministic_algorithms_enabledc                    s   t tj  |jjS rg   )r   r    r  rp   r   torch_function_enabledr!  r   rB   rC    handle_is_torch_function_enabledQ  r#  zTTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_torch_function_enabledc                    sD   t |dkrt|d tr|d |n|} tdd |D S )Nr   r   c                 s   s    | ]}t |V  qd S rg   )r   r   rB   rB   rC   	<genexpr>b      z`TorchInGraphFunctionVariable._get_handlers.<locals>.handle_has_torch_function.<locals>.<genexpr>)r   r   r%   unpack_var_sequencerp   any)rS   rm   r   elemsr   rB   rC   handle_has_torch_functionV  s   zMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_has_torch_functionc                 s   s    | ]\}}|j V  qd S rg   )stream)r   rZ   device_interfacerB   rB   rC   r+  f  s
    
z=TorchInGraphFunctionVariable._get_handlers.<locals>.<genexpr>c                    s     ||S rg   )rp   )rS   rm   r1  )r   rB   rC   handle_device_interface_streame  s   zRTorchInGraphFunctionVariable._get_handlers.<locals>.handle_device_interface_streamc                    sD   t jstd tstd  ||jjdtjgt|i R  d dS )Nz-torch.from_numpy. config.trace_numpy is Falsez(torch.from_numpy. NumPy is not availabler   )
target_clsrm   r   r   )	r   trace_numpyr   npr   r   r7   	as_tensorr   )rS   rm   r   )r   r   rB   rC   handle_from_numpyn  s   zETorchInGraphFunctionVariable._get_handlers.<locals>.handle_from_numpyc                 S   s   |S rg   rB   )rS   rm   the_type	the_valuerB   rB   rC   handle_jit_annotate  s   zGTorchInGraphFunctionVariable._get_handlers.<locals>.handle_jit_annotatec                    sF   |rJ dt |sJ dtjd|j|jd} tjj|S )Nz%Expect 1 input to cudnn.is_acceptablez2Expect input to cudnn.is_acceptable to be a tensorr   )r
  device)	r   r7   tensorr
  r<  rp   r   cudnnis_acceptable)rS   rm   r=  extra
tensor_inpr  rB   rC   handle_cudnn_is_acceptable  s   	zNTorchInGraphFunctionVariable._get_handlers.<locals>.handle_cudnn_is_acceptablec                 _   s   t jj|g|R i |S rg   )r
   BackwardHookVariablerp   r   rB   rB   rC   handle_backward_hook  s   zHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_backward_hookc                 _   s   | j |g|R i |S rg   )call_nn_parameterr   rB   rB   rC   handle_parameter  s   zDTorchInGraphFunctionVariable._get_handlers.<locals>.handle_parameterNc                 S      |d ur| |d|gi S d S r  r  self_rm   rS   dimrB   rB   rC   handle_sym_size  s   zCTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_sizec                 S   rG  )NstriderH  rI  rB   rB   rC   handle_sym_stride  s   zETorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_stridec                 _   s   t |dkr<d|v r>t |dkr@ttj|g |dd  i }ttj|||d gi }ttj||d |gi S d S d S d S )N   rM   r   r   )r   r   r7   r.   r   r-   r+   )rS   rm   r   rT   rq   rB   rB   rC   handle_addcdiv  s    


zBTorchInGraphFunctionVariable._get_handlers.<locals>.handle_addcdivc                    s@   t | rttjjj||gi }ttj|||g|S d S rg   )r   r   r7   opsaten_local_scalar_denser   r=   )rS   rm   r  
fill_valuerT   rq   r  rB   rC   handle_full  s   


z?TorchInGraphFunctionVariable._get_handlers.<locals>.handle_fullc                 _   sB   t |dkrt|d ts|s|t|tj||S d S d S d S )NrO  r   )r   r   r$   r  r   r   r	   foreach_lerp_inplacerZ   rm   r   rT   rB   rB   rC   "handle_inplace_foreach_lerp_scalar     zVTorchInGraphFunctionVariable._get_handlers.<locals>.handle_inplace_foreach_lerp_scalarc                    sB   t |dkrt|d  r|s|t|tj||S d S d S d S )Nr   r   )r   r   r  r   r   r	   foreach_pow_scalarrW  r  rB   rC   handle_foreach_pow_scalar  rY  zMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_foreach_pow_scalarc                    s4   |  r| st|tjr| r d S d S d S rg   )is_python_constantrl   r   r
   r   evaluate_expr)rS   rm   	conditionmessager   rB   rC   handle_assert  s   
zATorchInGraphFunctionVariable._get_handlers.<locals>.handle_assertc                    s*    ||j jdtjjgt||R  |dS )Nr   )r   
param_vars)r   r   r7   r   _SDPAParamsr   r   r   rB   rC   handle_sdpa_params  s   zFTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sdpa_params)_get_group_size_by_name_get_group_tag_rank_not_in_group$_resolve_group_name_by_ranks_and_tagget_process_group_ranks)DTensorc                    s   t |dkrt|d t fsJ n"t |dkr)t|d tr&t|d  s(J ntd| d| j dd |D }| j| }t||S )Nr   r   r   zInvalid group value (z) for constant pg function c                 S   r   rB   r   r   rB   rB   rC   r     r   znTorchInGraphFunctionVariable._get_handlers.<locals>.handle_constant_processgroup_functions.<locals>.<listcomp>)r   r   r#   r$   r  rM   r   r   )rS   rm   r   args_as_valueinvocation_resultr   rB   rC   &handle_constant_processgroup_functions  s   

zZTorchInGraphFunctionVariable._get_handlers.<locals>.handle_constant_processgroup_functionsc                    s   dd |dd  D  dd   D fdddD }d fdd		}d
jj |_||jjd|gt|d g|R  dS )Nc                 S   r   rB   r   r   rB   rB   rC   r     r   zYTorchInGraphFunctionVariable._get_handlers.<locals>.handle_from_local.<locals>.<listcomp>r   c                 S   s"   i | ]\}}|d vr||  qS )shaperM  r   r   kvrB   rB   rC   
<dictcomp>  s
    zYTorchInGraphFunctionVariable._get_handlers.<locals>.handle_from_local.<locals>.<dictcomp>c                    s   i | ]}| v r| | qS rB   rB   )r   rq  )rT   rB   rC   rs  #  s    rn  c                    s"   j | g R i ||dS )Nrn  rh   )r   ro  rM  )rk  kwargs_as_valuerS   rB   rC   fn_with_prim_types'  s   
zaTorchInGraphFunctionVariable._get_handlers.<locals>.handle_from_local.<locals>.fn_with_prim_typeszprim r   r   rm   r   )NN)itemsrM   r\   r   r   r   )rS   rm   r   rT   kwargs_to_be_proxiedru  rc  )rk  rT   rt  rS   rC   handle_from_local  s*   
zETorchInGraphFunctionVariable._get_handlers.<locals>.handle_from_local)layoutc                _   s@   ddl m} |r| tjkrtd t||std d S d S )Nr   )BaseListVariablez3torch.compile does not support strided NestedTensorz!nested_tensor with non-list input)listsr{  rl   r7   stridedr   r   )rS   rm   tensor_listrz  r   rT   r{  rB   rB   rC   handle_nested_tensor;  s   	
zHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_nested_tensorc                 _   sT   t |t | dkst |dkr$|d  r&|d  dkr(td d S d S d S d S )Nr   r   z<torch.nn.functional.one_hot with data-dependent output shape)r   r\  rl   r   r   rB   rB   rC   handle_one_hotK  s   
zBTorchInGraphFunctionVariable._get_handlers.<locals>.handle_one_hotc                    s6   t |rtjtjjj|j	S t | r|S d S rg   )
r   r
   ro   rp   r7   fxexperimentalsymbolic_shapesguard_size_oblivioussym_num)rS   rm   expr)ro   r   rB   rC   handle_guard_size_obliviousV  s   


zOTorchInGraphFunctionVariable._get_handlers.<locals>.handle_guard_size_obliviousc                 _   s"   ddl m} t||g ||S )Nr   )_unsafe_set_version_counter)tensor_version_opr  r   r   )rS   rm   r   rT   r  rB   rB   rC   !handle_unsafe_set_version_counterc  s   zUTorchInGraphFunctionVariable._get_handlers.<locals>.handle_unsafe_set_version_counterc                    s    t jj S rg   )r7   r   r   peek_interpreter_stackr   )r   rB   rC   'handle_functorch_peek_interpreter_stackm  s   
z[TorchInGraphFunctionVariable._get_handlers.<locals>.handle_functorch_peek_interpreter_stackc                 _   s   |d j }ttjj|S Nr   )rM   FuncTorchInterpreterVariabler7   r   pyfunctorchcoerce_cinterpreter)rS   rm   r   rT   cinterpreterrB   rB   rC   0handle_functorch_pyfunctorch_coerce_cinterpreterw  s   
zdTorchInGraphFunctionVariable._get_handlers.<locals>.handle_functorch_pyfunctorch_coerce_cinterpreterc                    sf    fdd d }|r|d }nd|v r|d }t |s/ |r1ttjj|g ||S d S d S )Nc                    s<   t |  fr	dS t | ttfrtfdd| jD S dS )NTc                 3   s    | ]} |V  qd S rg   rB   )r   ycheck_any_unspecrB   rC   r+    r,  ztTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_tensor.<locals>.check_any_unspec.<locals>.<genexpr>F)r   r$   r%   r.  rw  )r   )r   r   r  rB   rC   r    s
   zaTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_tensor.<locals>.check_any_unspecr   data)r   r   r7   _refsr=  r   )rS   rm   r   rT   data_arg)r   r   r  rC   handle_torch_tensor  s   

zGTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_tensorc                 _   s0   |s|rJ |j jstdt| |j  S )Nz/Popping from an empty torch function mode stack)symbolic_torch_function_state
mode_stackr   r(   register_mutationpop_torch_function_moder   rB   rB   rC   handle_pop_torch_function  s
   

zMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_pop_torch_functionc                    s8   t |dkr|r
J t| |j|d   d S Nr   r   )r   r(   r  r  push_torch_function_moderp   r   r   rB   rC   handle_push_torch_function  s   

zNTorchInGraphFunctionVariable._get_handlers.<locals>.handle_push_torch_functionc                    s   |s|rJ   t|jjS rg   )rp   r   r  r  r   r   rB   rC   handle_len_torch_function  s   
zMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_len_torch_functionc                 _   sH   t |dkr|r
J |d  }|dkr|t |jjk sJ |jj| S r  )r   rl   r  r  )rS   rm   r   rT   indrB   rB   rC   handle_get_stack_at  s   zGTorchInGraphFunctionVariable._get_handlers.<locals>.handle_get_stack_atc                    sF   t | |d  r|d  d u rt | nt |  d S r  )r(   r  r\  rl   clear_default_device!register_device_context_insertionrp   r   r   rB   rC   handle_set_default_device  s
   


zMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_set_default_devicerx   )Frg   )dtorch.backends.cudar   r   ro   r   r   r   r   r   r   builderr   r   r   dispatch_key_set_functionsr7   r   r   __wrapped__rQ  inductoraccumulate_grad_defaultrs   r  	is_tensorr  r  r  r  compile!REWRITE_OPS_TO_TENSOR_SIZE_METHODr   modulesutils_single_pair_triple
_quadruple_ntupler   use_deterministic_algorithmsr'  r   _is_torch_function_enabledr   has_torch_function_variadichas_torch_function_unarydictfromkeysr   
from_numpyjitannotater   r>  r?  hooksBackwardHook	ParameterrR  sym_sizeint
sym_strideaddcdivr=   _foreach_lerp__foreach_pow_assertr"   is_available"torch.distributed.distributed_c10dre  rf  rg  rh  ri  torch.distributed.tensorrj  
from_localnestednested_tensor
functionalone_hotr  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)4r   r   r   r   r  r  r  r	  r  r  r  r  r  r"  r&  r(  r*  r0  r3  r8  r;  rB  rD  rF  rL  rN  rP  rU  rX  r[  r`  rd  re  rf  rg  rh  ri  rj  rm  ry  r  r  r  r  r  r  r  r  r  r  r  r  rB   )
ro   r   r   r   r   r   r   r   r   r   rC   _get_handlers  s4  
$	











	



!
		

	

z*TorchInGraphFunctionVariable._get_handlersrm   r*   r   rT   r   r   c           /   
      sh  ddl m}m m} ddlm} | jrdd lm  m	} ddlm
}m}	 ddlm ddlm}
 ddlm} t|t||t||f}t|
||gi }t|tr^t|jd	ks`J |j\}}t|tslJ |jD ]}| }|	|s| j}td
| d qodd |jD }z| }W n< |y } z0|j  }|j}dd lm!  m"} |#|rtd| d ntd| d| d W Y d }~nd }~ww | j$fdd}||\}}|j%&j' d|}|j%&j'd |}t(||j)_(t(||j)_(||g|R }|j%*d||i }|||}|S | +|||r)t,|| ||S | - r_t.||r_| j$t/v rGt0| j1}t2|3t4j5 |6|  dd |D i dd | D S | 7 rk| 8|||S | 9 :| j$} | r| | |g|R i |}!|!r|!S t; fdd|D }"t<dd |D }#t=| j$ddd kr| j$j't>v r|"r|#rd!t?| j$ d"}$t@A|$ t|$ | j$}%|"rd#| j$j' }&t=| j$dd d$krtBtC|&rt=tC|&}%d }'d%|v r t|d% tjr |d% jDj)jEd& jF}'|||j%j*d|%gtG||R  d'}(t|(|r(d(|v r(|d(  r(td) d%|v r2t|d% tjr?|d%  d u s2t|(tr{t|d% ttfsQJ tH|d% j|(jD ]\})}*t|)tjrwt|*tjrw|)jI|*jIkrwtd* qZ|(S t|(|rt|d% |sJ d&|d% jDj)jEv sJ |(jDj)jEd& }+|d% jDj)jEd& },|'|+jFkrtd* tCjJK|,std+ |(S t|(|r'|(j$d u r't|d% |rd&|d% jDj)jEv sJ |d% jDj)jEd& },tCjJK|,std+ |(S t|d% tr%tL|d% jD ]!\}-}.d&|.jDj)jEv sJ |.jDj)jEd& },tCjJK|,s#td, q|(S td-t(|d%   |(S ).Nr   )ro   r   r   rc  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`
c                 S   r   rB   )rj   )r   flat_arg_vtrB   rB   rC   r     s    z>TorchInGraphFunctionVariable.call_function.<locals>.<listcomp>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                     s0    j }d _ z| i |}W | _ |S | _ w NT)allow_non_fake_inputs_override)r   rT   old_valres)r  r   rB   rC   
patched_fn.  s   z>TorchInGraphFunctionVariable.call_function.<locals>.patched_fn_spec_input_specr   c                 S   r   rB   r   r   rB   rB   rC   r   b  r   c                 S      i | ]	\}}||  qS rB   r   rp  rB   rB   rC   rs  c      z>TorchInGraphFunctionVariable.call_function.<locals>.<dictcomp>c                 3   s    | ]}t | V  qd S rg   )r   r   )r   rB   rC   r+  p  s    z=TorchInGraphFunctionVariable.call_function.<locals>.<genexpr>c                 s   s"    | ]}t |tjtjfV  qd S rg   )r   r
   ro   r   r   rB   rB   rC   r+  r  s
    
r[   r   r7   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_rs   outr   rv  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 )Mr   ro   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   rw  r$   python_typery   r   rl   vtr  _pytreeis_constant_classrM   r   %register_static_attr_and_return_proxyr\   typer   r   torch_function_override_enabledr'   rv   r   #constant_fold_functions_need_guardsr   rH   r   rI   r   EQUALS_MATCHrp   is_tensor_methodcall_tensor_methodr  getr.  allru   bin_opsr   r   warningrn   r7   r   r   ro  r   zip_size_prims_commonis_contiguous	enumerate)/rS   rm   r   rT   ro   r   r   r  r  r  r  r  packed_input_vtout_vtflat_args_vtsinput_spec_vtr  arg_type	type_nameproxified_flat_args
input_specetyppytreer  rZ   f_specf_spec_proxyinput_spec_proxyall_argsr   rH   special_handlerrq   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   rB   )r   r  r   rC   r     sp  
















+z*TorchInGraphFunctionVariable.call_functionc                    sz   | j tjjjju r|d   n| j jd j t	 t
sJ |r"J  fdd}| j tjjjju r7t|S ||d S )z1inline behavior of torch.nn.modules.utils._ntupler   c                    sZ   |  rtt| S |  r#tjtj	j
j |  S td|  d d S )Nztorch.nn.modules.utils._ntuple(r~   )has_unpack_var_sequencer
   r%   listr-  r\  ro   rp   r7   r   r  r  r  rl   r   rh   countrm   rB   rC   r    s   
z@TorchInGraphFunctionVariable._call_ntuple.<locals>.handle_ntuple)rM   r7   r   r  r  r  rl   __closure__cell_contentsr   r  r
   LambdaVariable)rS   rm   r   rT   r  rB   r'  rC   r    s   
z)TorchInGraphFunctionVariable._call_ntupleTc              
   C   s  |j rtd t|tjr!z| }W n ty    td Y nw t|tjs/td| d |jr9| 	|||S t
|jrBtd t sItd zt||d }||d }||d	 }W n ty~ } ztd
|  W Y d}~nd}~ww |jt||||g}|jr||dg i }ddlm}	 |	||jdt| | fi |jd}
t|
tjsJ tjj|
_d|
_|
S )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 constructionro  r
  r<  zParameter not python_constant: Ndetachr   rc  r   rG   F)exportr   r   r
   r   rl   NotImplementedErrorr   rH   _nn_param_via_prefix_insertr   
class_typer   tuplevar_getattrr   synthetic_graph_inputr   r  r  r  r   r   r   rj   r7   r   r  has_grad_fn)rL   rm   r  r  ro  r
  r<  r  placeholderr   rq   rB   rB   rC   rE    sX   

z.TorchInGraphFunctionVariable.call_nn_parameterc                    s   | j  }t|    fdd  |j  t|  dd  | | j j	
   | j}|jdvr@td t|}tj| j | j}t| ||}t jj| |S )Nc                      s     ddS )Nztorch.nnr  )load_import_fromrB   cgrB   rC   <lambda>^  s    zJTorchInGraphFunctionVariable._nn_param_via_prefix_insert.<locals>.<lambda>r   F)r5  get_attrzAUnexpected type of data placeholder op for parameter construction)r   new_varr   add_push_nullrH   r
   ro   r   storepregraph_bytecodeextendget_instructionsrj   r   opr   r   r7   r   r  example_value_from_input_noder   r   r   r   guards_contextdynamo_guardsremove_guards_with_source)rm   r  r  varname	data_noderH   r   rq   rB   r7  rC   r/  W  s,   




z8TorchInGraphFunctionVariable._nn_param_via_prefix_insertc                 C   s"   |d  ||  j|dd  |S )Nr   r   )r  r   r\   r   rB   rB   rC   r  x  s   "z/TorchInGraphFunctionVariable.call_tensor_methodc                 C   sH   ddl m} t|  ot|  do|  jtjj	kp#|  | v S )Nr   )get_tensor_method__objclass__)
r   rH  r   ismethoddescriptorr   rn   rI  r7   r   
TensorBase)rS   rH  rB   rB   rC   r  {  s   z-TorchInGraphFunctionVariable.is_tensor_methodc                 C   s4   |   t v pt|   tjjtjjfot|||S rg   )r   r1   r   r7   _ops
OpOverloadOpOverloadPacketr&   r   rB   rB   rC   r    s   
z<TorchInGraphFunctionVariable.torch_function_override_enabledrg   rw   rx   r  )r\   r[   ry   rz   rR   r   r   r   r   	functools	lru_cacher  r   r   r   r  r{   rE  r/  r  r  r  r|   rB   rB   rU   rC   r     s>        2
  
*@ 	r   c                       sZ   e Zd ZdZedd Zedd Zdd Zde	e
 d	eee
f d
df fddZ  ZS )r   zrepresents torch.DispatchKeySetc                 K   s   t | fi |S rg   )r   )rM   rT   rB   rB   rC   rp     s   zDispatchKeySetVariable.createc                 C   rE   rF   )r   rI   r   DISPATCH_KEY_SET_MATCHrK   rB   rB   rC   rN     rO   z)DispatchKeySetVariable.create_with_sourcec                 C   s   |dv S )N)hasrB   )rS   rc   rB   rB   rC   is_constant_fold_method  s   z.DispatchKeySetVariable.is_constant_fold_methodr   rT   rP   r   c                    sx   |  |r't||r't| j|}tj|dd |D i dd | D S |dkr3t| j	 S t
 ||||S )Nc                 S   r   rB   r   r   rB   rB   rC   r     r   z6DispatchKeySetVariable.call_method.<locals>.<listcomp>c                 S   r  rB   r   rp  rB   rB   rC   rs    r  z6DispatchKeySetVariable.call_method.<locals>.<dictcomp>highestPriorityTypeId)rS  r   ru   rM   r
   ro   rp   rw  EnumVariablerT  rQ   r  )rS   rm   rc   r   rT   methodrU   rB   rC   r    s   z"DispatchKeySetVariable.call_method)r\   r[   ry   rz   r   rp   r{   rN   rS  r&  r   r  r   r  r|   rB   rB   rU   rC   r     s    


r   c                       sF   e Zd ZdZedd Zdee dee	ef ddf fdd	Z
  ZS )
r  z<represents torch._functorch.pyfunctorch.FuncTorchInterpreterc                 C   rE   rF   )r   rI   r   ID_MATCHrK   rB   rB   rC   rN     rO   z/FuncTorchInterpreterVariable.create_with_sourcer   rT   rP   r   c                    s   |dkrt | j S |dkr |t | jjj| g| |S |dv r/t j	t
| j| S |dkr@|s7|r9J t j	|d S t ||||S )Nkeyprocess)level
batch_size
randomnesslower)r
   rU  rM   rX  r  r  rY  __func__ro   rp   ru   0TemporarilyPopInterpreterStackCtxManagerVariablerQ   r  )rS   rm   rc   r   rT   rU   rB   rC   r    s    z(FuncTorchInterpreterVariable.call_method)r\   r[   ry   rz   r{   rN   r&  r   r  r   r  r|   rB   rB   rU   rC   r    s    

r  )rz   rO  r   loggingrs   r_   collections.abcr   typingr   torch._Cr7   torch._refstorch.fxtorch.nntorch._guardsr   torch._loggingr   torch.utils._python_dispatchr   r   r   r	   r
   rb   r   create_parameter_opr   r   r   r2  r   excr   guardsr   r   rH   r   r   r  r   r   r   r   r   r   r   r  r   ctx_managerr   r   r    dictsr!   distributedr"   r#   r|  r$   r%   torch_functionr&   r'   r(   numpyr6  ModuleNotFoundError#torch.distributed.fsdp._fully_shardr)   torch._dynamo.symbolic_convertr*   	getLoggerr\   r   r  r  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   	grad_moder   r   r   r   r   r   r   r   r   r   r   r   r   _shape_as_tensorr  current_devicer  r  r   _get_device_index_get_cublas_allow_tf32_is_any_autocast_enabledget_device_propertiesr  get_autocast_dtypeget_autocast_gpu_dtypeget_default_dtypeis_autocast_cache_enabledis_autocast_cpu_enabledis_autocast_enabledr  r  r  
_Reductionget_enumpromote_types_get_privateuse1_backend_name_is_checkpoint_validrt   r?  is_initializedget_rankget_world_sizer  is_scripting
is_tracing_get_tracing_stater  _symbolic_traceis_fx_tracingonnxis_in_onnx_exportr   r   r   r   r   r   r  
activation_is_make_fx_tracingr   r  r   _dispatch_tls_local_include_set_dispatch_tls_local_exclude_setr  rP  r1   rD   r}   r   r   r  rB   rB   rB   rC   <module>   s  $	








&         %