o
    Zh                     @   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   Ztorch.overridesr1   setfrom_iterablevaluestorchZonesZ	ones_likeZzerosZ
zeros_likeemptyfullupdate)r0   Zget_overridable_functions_funcsmore r<   L/var/www/auris/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   ZFUNCTION_MATCHclsvaluerB   r<   r<   r=   create_with_source      z$BaseTorchVariable.create_with_sourcereturnNc                    s   t  jdi | || _d S )Nr<   )super__init__rF   )selfrF   kwargs	__class__r<   r=   rK      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.Z
torch_obj___z[^a-zA-Z0-9_]+_)	rF   
__module____name__	Exceptionidrer,   Zextend_outputZsetup_globally_cached)rL   codegennameZunique_var_namer<   r<   r=   reconstruct   s   zBaseTorchVariable.reconstructc                 C      | j S NrF   rL   r<   r<   r=   as_proxy      zBaseTorchVariable.as_proxyc                 C   r[   r\   r]   r^   r<   r<   r=   as_python_constant   r`   z$BaseTorchVariable.as_python_constanttxr*   c                 C   s   t | j|}tj|S r\   )hasattrrF   r
   ConstantVariablecreate)rL   rb   rY   resultr<   r<   r=   call_obj_hasattr   s   z"BaseTorchVariable.call_obj_hasattrc                 C   s    | j tv rdS t| j dd dkS )NTrS   math)rF   constant_fold_functionsgetattrr^   r<   r<   r=   can_constant_fold_through   s   
z+BaseTorchVariable.can_constant_fold_throughrI   Nrb   r*   )rT   rS   __qualname____doc__classmethodrG   rK   rZ   r_   ra   rg   rk   __classcell__r<   r<   rN   r=   r>      s    


r>   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 implementationsrI   c                 C   s   d| j  dS )NzTorchCtxManagerClassVariable()r]   r^   r<   r<   r=   __repr__   s   z%TorchCtxManagerClassVariable.__repr__c                 C   s    t | } t| ot| o| tv S r\   )r   callabler   supported_ctx_manager_classesr]   r<   r<   r=   is_matching_cls   s
   z,TorchCtxManagerClassVariable.is_matching_clsrb   r*   argsrM   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)Zinitialized)wrap_fx_proxy_clscall_functionr<   z$Profiler function %s will be ignoredr   c                 S      g | ]}t |qS r<   r   .0xr<   r<   r=   
<listcomp>i      z>TorchCtxManagerClassVariable.call_function.<locals>.<listcomp>c                 S   r   r<   r   r   r<   r<   r=   r   w  r   backendsc                 S      g | ]}|  qS r<   ra   r   argr<   r<   r=   r     r   )G rz   r{   r|   r}   r~   r   r   r   r   r   r   r   rF   r6   no_gradlen
isinstancer
   Z	functionsZBaseUserFunctionVariablere   r   enable_gradset_grad_enabledra   inference_modeinspectisclass
issubclassZStreamZ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)   ZFSDPParamGroupZuse_training_statenn	attentionsdpa_kernel_sdpa_kernel_variadicrJ   )rL   rb   rx   rM   rz   r{   r|   r}   r~   r   r   r   r   r   r   r   ctxZinf_moder   r   rN   r<   r=   r     s   8





*z*TorchCtxManagerClassVariable.call_function)rT   rS   rn   ro   strrt   staticmethodrw   r   r   r   rq   r<   r<   rN   r=   rr      s    
rr   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 graphNrI   c                    s:   t  j|fi | ddlm} |d u r||}|| _d S )Nr   )is_nonstrict_trace_callable)rJ   rK   trace_rulesr   nonstrict_traceable)rL   rF   r   rM   r   rN   r<   r=   rK     s
   
z%TorchInGraphFunctionVariable.__init__c                 C   s   d| j  d| j dS )NzTorchInGraphFunctionVariable(z, nonstrict_traceable=rs   )rF   r   r^   r<   r<   r=   rt     s   z%TorchInGraphFunctionVariable.__repr__c                 C   r[   r\   r]   r^   r<   r<   r=   get_function  r`   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 r\   r<   )handlerfn)fnshandlersr<   r=   	_register  s   
zOTorchInGraphFunctionVariable._get_handlers.<locals>.register.<locals>._registerr   )ru   )r   r   )r   )r   r=   register  s   z<TorchInGraphFunctionVariable._get_handlers.<locals>.registerr   )
SDPAParamsr   )rd   DeterministicAlgorithmsVariabler   StreamContextVariableSymNodeVariableTensorVariableUserDefinedObjectVariable)wrap_fx_proxyr   rb   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 r\   )rF   r6   _utilsis_compiling_dynamoexternal_utilscompileris_dynamo_compilingis_exportingZmark_inconsistent_side_effectsre   tracing_state_functionsrL   rb   rx   rM   rd   r<   r=   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)rF   r6   r   _dispatch_keysr   r   r
   r   proxynodemetaZ_subclassesZ
FakeTensorZDispatchKeySetZDispatchKeyPythonZPythonTLSSnapshotDispatchKeySetVariablere   )rL   rb   rx   rM   r   Zdksr<   r<   r=   !handle_dispatch_key_set_functions  s$   

zUTorchInGraphFunctionVariable._get_handlers.<locals>.handle_dispatch_key_set_functionsc                 _   s   t |tj S r\   )r   buildr6   	overridesget_default_nowrap_functionsr   r<   r<   r=   #handle_get_default_nowrap_functions  s   
zWTorchInGraphFunctionVariable._get_handlers.<locals>.handle_get_default_nowrap_functionsc                 _   s   | t|tj||S r\   )inline_user_function_returnr   r   r	   Zaccumulate_gradr   r<   r<   r=   handle_accumulate_grad_  s   zKTorchInGraphFunctionVariable._get_handlers.<locals>.handle_accumulate_grad_c                 _   s&   t ||s|t|tj||S d S r\   )r   r   r   r   r	   radiansr   r<   r<   r=   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 )NZ__torch_function__TF)r   rF   r6   r   is_tensor_likerc   re   )rL   rb   r   )rd   r   r   r<   r=   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   dtyperF   r6   is_floating_pointre   
is_complexAssertionError)rL   rb   inputZ	input_argrd   r   r<   r=   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   Z
valid_sizere   r   sizecall_methodrL   rb   r   r   r<   r=   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   r<   r<   r=   handle_torch_compile(  s   zHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_compilec                    s   t | sJ ||dg i S Nr   )r   r   r   r   r<   r=   handle_tensor_size_rewrites0  s   zOTorchInGraphFunctionVariable._get_handlers.<locals>.handle_tensor_size_rewritesc                 _   s   |  |||S r\   )_call_ntupler   r<   r<   r=   handle_ntuple5  s   zATorchInGraphFunctionVariable._get_handlers.<locals>.handle_ntuplec                       t j  t S r\   )r   _guards_singletonre   r6   is_grad_enabledrL   rb   )rd   r   r<   r=   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))ra   r   re   )rL   rb   modeZ	warn_only)r   r<   r=   #handle_use_deterministic_algorithmsD  s   zWTorchInGraphFunctionVariable._get_handlers.<locals>.handle_use_deterministic_algorithmsc                    r   r\   )r   r  re   r6   $are_deterministic_algorithms_enabledr  )rd   r   r<   r=   +handle_are_deterministic_algorithms_enabledL  r  z_TorchInGraphFunctionVariable._get_handlers.<locals>.handle_are_deterministic_algorithms_enabledc                    s   t tj  |jjS r\   )r   r    r  re   r   Ztorch_function_enabledr  r   r<   r=    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 r\   )r   r   r<   r<   r=   	<genexpr>b      z`TorchInGraphFunctionVariable._get_handlers.<locals>.handle_has_torch_function.<locals>.<genexpr>)r   r   r%   unpack_var_sequencere   any)rL   rb   rx   Zelemsr   r<   r=   handle_has_torch_functionV  s   zMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_has_torch_functionc                 s   s    | ]\}}|j V  qd S r\   )stream)r   rR   device_interfacer<   r<   r=   r  f  s
    
z=TorchInGraphFunctionVariable._get_handlers.<locals>.<genexpr>c                    s     ||S r\   )re   )rL   rb   r  )r   r<   r=   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   )Z
target_clsrb   r   r   )	r   Ztrace_numpyr   npr   r   r6   Z	as_tensorr   )rL   rb   rx   )r   r   r<   r=   handle_from_numpyn  s   zETorchInGraphFunctionVariable._get_handlers.<locals>.handle_from_numpyc                 S   s   |S r\   r<   )rL   rb   Zthe_typeZ	the_valuer<   r<   r=   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   r6   tensorr   r  re   r   cudnnis_acceptable)rL   rb   r  extraZ
tensor_inpr   r<   r=   handle_cudnn_is_acceptable  s   	zNTorchInGraphFunctionVariable._get_handlers.<locals>.handle_cudnn_is_acceptablec                 _   s   t jj|g|R i |S r\   )r
   ZBackwardHookVariablere   r   r<   r<   r=   handle_backward_hook  s   zHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_backward_hookc                 _   s   | j |g|R i |S r\   )call_nn_parameterr   r<   r<   r=   handle_parameter  s   zDTorchInGraphFunctionVariable._get_handlers.<locals>.handle_parameterNc                 S      |d ur| |d|gi S d S r   r   Zself_rb   rL   dimr<   r<   r=   handle_sym_size  s   zCTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_sizec                 S   r  )Nstrider   r!  r<   r<   r=   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   rF   r   r   )r   r   r6   r.   r   r-   r+   )rL   rb   rx   rM   rf   r<   r<   r=   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 r\   )r   r   r6   opsatenZ_local_scalar_denser   r8   )rL   rb   r   Z
fill_valuerM   rf   r   r<   r=   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 )Nr&  r   )r   r   r$   r   r   r   r	   Zforeach_lerp_inplacerR   rb   rx   rM   r<   r<   r=   "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	   Zforeach_pow_scalarr+  r   r<   r=   handle_foreach_pow_scalar  r-  zMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_foreach_pow_scalarc                    s4   |  r| st|tjr| r d S d S d S r\   )is_python_constantra   r   r
   r   Zevaluate_expr)rL   rb   	conditionmessager   r<   r=   handle_assert  s   
zATorchInGraphFunctionVariable._get_handlers.<locals>.handle_assertc                    s*    ||j jdtjjgt||R  |dS )Nr   )r   Z
param_vars)r   r   r6   r   Z_SDPAParamsr   r   r   r<   r=   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   r<   r   r   r<   r<   r=   r     r   znTorchInGraphFunctionVariable._get_handlers.<locals>.handle_constant_processgroup_functions.<locals>.<listcomp>)r   r   r#   r$   r   rF   r   r   )rL   rb   rx   args_as_valueZinvocation_resultr   r<   r=   &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   r<   r   r   r<   r<   r=   r     r   zYTorchInGraphFunctionVariable._get_handlers.<locals>.handle_from_local.<locals>.<listcomp>r   c                 S   s"   i | ]\}}|d vr||  qS )shaper$  r   r   kvr<   r<   r=   
<dictcomp>  s
    zYTorchInGraphFunctionVariable._get_handlers.<locals>.handle_from_local.<locals>.<dictcomp>c                    s   i | ]}| v r| | qS r<   r<   )r   r@  )rM   r<   r=   rB  #  s    r=  c                    s"   j | g R i ||dS )Nr=  r]   )r   r>  r$  )r;  kwargs_as_valuerL   r<   r=   fn_with_prim_types'  s   
zaTorchInGraphFunctionVariable._get_handlers.<locals>.handle_from_local.<locals>.fn_with_prim_typeszprim r   r   rb   r   )NN)itemsrF   rT   r   r   r   )rL   rb   rx   rM   Zkwargs_to_be_proxiedrD  r3  )r;  rM   rC  rL   r=   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)listsrI  ra   r6   Zstridedr   r   )rL   rb   Ztensor_listrH  rx   rM   rI  r<   r<   r=   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/  ra   r   r   r<   r<   r=   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 r\   )
r   r
   rd   re   r6   fxexperimentalsymbolic_shapesguard_size_obliviousZsym_num)rL   rb   expr)rd   r   r<   r=   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)Ztensor_version_oprT  r   r   )rL   rb   rx   rM   rT  r<   r<   r=   !handle_unsafe_set_version_counterc  s   zUTorchInGraphFunctionVariable._get_handlers.<locals>.handle_unsafe_set_version_counterc                    s    t jj S r\   )r6   r   r   peek_interpreter_stackr   )r   r<   r=   '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   )rF   FuncTorchInterpreterVariabler6   r   pyfunctorchcoerce_cinterpreter)rL   rb   rx   rM   Zcinterpreterr<   r<   r=   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 r\   r<   )r   ycheck_any_unspecr<   r=   r    r  ztTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_tensor.<locals>.check_any_unspec.<locals>.<genexpr>F)r   r$   r%   r  rF  )r   )r   r   r_  r<   r=   r_    s
   zaTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_tensor.<locals>.check_any_unspecr   data)r   r   r6   Z_refsr  r   )rL   rb   rx   rM   Zdata_arg)r   r   r^  r=   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_mutationZpop_torch_function_moder   r<   r<   r=   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(   rd  rb  Zpush_torch_function_modere   r   r   r<   r=   handle_push_torch_function  s   

zNTorchInGraphFunctionVariable._get_handlers.<locals>.handle_push_torch_functionc                    s   |s|rJ   t|jjS r\   )re   r   rb  rc  r   r   r<   r=   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 rf  )r   ra   rb  rc  )rL   rb   rx   rM   indr<   r<   r=   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 rX  )r(   rd  r/  ra   Zclear_default_deviceZ!register_device_context_insertionre   r   r   r<   r=   handle_set_default_device  s
   


zMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_set_default_devicerm   )Fr\   )dZtorch.backends.cudar   r   rd   r   r   r   r   r   r   builderr   r   r   dispatch_key_set_functionsr6   r   r   __wrapped__r(  ZinductorZaccumulate_grad_defaultrh   r   Z	is_tensorr   r   r   r   compile!REWRITE_OPS_TO_TENSOR_SIZE_METHODr   modulesutilsZ_singleZ_pairZ_tripleZ
_quadruple_ntupler  Zuse_deterministic_algorithmsr  r   Z_is_torch_function_enabledr   Zhas_torch_function_variadicZhas_torch_function_unarydictfromkeysr   Z
from_numpyjitZannotater   r  r  hooksZBackwardHook	Parameterr)  Zsym_sizeintZ
sym_strideZaddcdivr8   Z_foreach_lerp_Z_foreach_pow_assertr"   is_availableZ"torch.distributed.distributed_c10dr5  r6  r7  r8  r9  Ztorch.distributed.tensorr:  Z
from_localnestedZnested_tensor
functionalZone_hotrN  rO  rP  rQ  Z	_autogradrT  r   rV  rZ  r[  r  Z_pop_torch_function_stackZ_push_on_torch_function_stackZ_len_torch_function_stackZ_get_function_stack_atZset_default_device)4r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r	  r
  r  r  r  r  r  r  r  r#  r%  r'  r*  r,  r.  r2  r4  r5  r6  r7  r8  r9  r:  r<  rG  rK  rM  rS  rU  rW  r\  ra  re  rg  rh  rj  rk  r<   )
rd   r   r   r   r   r   r   r   r   r   r=   _get_handlers  s4  
$	











	



!
		

	

z*TorchInGraphFunctionVariable._get_handlersrb   r*   rx   rM   ry   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   )rd   r   r   r3  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   r<   )r_   )r   flat_arg_vtr<   r<   r=   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)Zallow_non_fake_inputs_override)rx   rM   Zold_valres)r  r   r<   r=   
patched_fn.  s   z>TorchInGraphFunctionVariable.call_function.<locals>.patched_fn_specZ_input_specr   c                 S   r   r<   r   r   r<   r<   r=   r   b  r   c                 S      i | ]	\}}||  qS r<   r   r?  r<   r<   r=   rB  c      z>TorchInGraphFunctionVariable.call_function.<locals>.<dictcomp>c                 3   s    | ]}t | V  qd S r\   )r   r   )r   r<   r=   r  p  s    z=TorchInGraphFunctionVariable.call_function.<locals>.<genexpr>c                 s   s"    | ]}t |tjtjfV  qd S r\   )r   r
   rd   r   r   r<   r<   r=   r  r  s
    
rS   r   r6   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.
Z_sym_rh   outr   rE  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   rd   r   r   rl  r   r   Z"torch._higher_order_ops.flat_applyZ_higher_order_ops
flat_applyr  r  Ztorch._subclasses.fake_tensorr  Ztorch.utils._pytreer  baser  r%   r   r!   r
   UserFunctionVariabler   r   r   rF  r$   Zpython_typern   r   ra   vtrs  Z_pytreeZis_constant_classrF   r   Z%register_static_attr_and_return_proxyrT   typer   r   torch_function_override_enabledr'   rk   r   #constant_fold_functions_need_guardsr   rB   r   rC   r   ZEQUALS_MATCHre   is_tensor_methodcall_tensor_methodr  getr  allrj   bin_opsr   r   warningrc   r6   r   r   r>  r   zip_sizeZ_prims_commonZis_contiguous	enumerate)/rL   rb   rx   rM   rd   r   r   r  r  r  r  r  Zpacked_input_vtZout_vtZflat_args_vtsZinput_spec_vtr  Zarg_type	type_nameZproxified_flat_argsZ
input_specetypZpytreer  rR   Zf_specZf_spec_proxyZinput_spec_proxyall_argsr   rB   Zspecial_handlerrf   Zany_symints_or_symfloatsZall_ints_or_floatsmsgZfn_Ztorch_sym_opZfake_out_shapeZtensor_variableZ
out_tensorZresult_tensorZfake_tensorZfake_outidxr   r<   )r   r  r   r=   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(rs   )Zhas_unpack_var_sequencer
   r%   listr  r/  rd   re   r6   r   rr  rs  rt  ra   r   r]   countrb   r<   r=   r     s   
z@TorchInGraphFunctionVariable._call_ntuple.<locals>.handle_ntuple)rF   r6   r   rr  rs  rt  ra   __closure__cell_contentsr   rz  r
   ZLambdaVariable)rL   rb   rx   rM   r   r<   r  r=   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 constructionr>  r   r  zParameter not python_constant: Ndetachr   r3  r   rA   F)Zexportr   r   r
   r   ra   NotImplementedErrorr   rB   _nn_param_via_prefix_insertr   Z
class_typer   tupleZvar_getattrr   Zsynthetic_graph_inputr   r  r   rl  r   r   r   r_   r6   r   ry  Zhas_grad_fn)rE   rb   r`  r  r>  r   r  r  placeholderr   rf   r<   r<   r=   r    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.nnry  )Zload_import_fromr<   Zcgr<   r=   <lambda>^  s    zJTorchInGraphFunctionVariable._nn_param_via_prefix_insert.<locals>.<lambda>r   F)r  Zget_attrzAUnexpected type of data placeholder op for parameter construction)r   Znew_varr   Zadd_push_nullrB   r
   rd   r   storeZpregraph_bytecodeextendget_instructionsr_   r   opr   r   r6   r   ry  Zexample_value_from_input_noder   r   r   r  Zguards_contextZdynamo_guardsZremove_guards_with_source)rb   r`  r  varnameZ	data_noderB   r   rf   r<   r  r=   r  W  s,   




z8TorchInGraphFunctionVariable._nn_param_via_prefix_insertc                 C   s"   |d  ||  j|dd  |S )Nr   r   )r   r   rT   r   r<   r<   r=   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   r  r   ismethoddescriptorr   rc   r  r6   r   Z
TensorBase)rL   r  r<   r<   r=   r  {  s   z-TorchInGraphFunctionVariable.is_tensor_methodc                 C   s4   |   t v pt|   tjjtjjfot|||S r\   )r   r1   r   r6   Z_opsZ
OpOverloadZOpOverloadPacketr&   r   r<   r<   r=   r    s   
z<TorchInGraphFunctionVariable.torch_function_override_enabledr\   rl   rm   r  )rT   rS   rn   ro   rK   r   rt   r   r   	functools	lru_cacher  r   r   r   r   rp   r  r  r  r  r  rq   r<   r<   rN   r=   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 r\   )r   )rF   rM   r<   r<   r=   re     s   zDispatchKeySetVariable.createc                 C   r?   r@   )r   rC   r   ZDISPATCH_KEY_SET_MATCHrD   r<   r<   r=   rG     rH   z)DispatchKeySetVariable.create_with_sourcec                 C   s   |dv S )N)hasr<   )rL   rY   r<   r<   r=   is_constant_fold_method  s   z.DispatchKeySetVariable.is_constant_fold_methodrx   rM   rI   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   r<   r   r   r<   r<   r=   r     r   z6DispatchKeySetVariable.call_method.<locals>.<listcomp>c                 S   r  r<   r   r?  r<   r<   r=   rB    r  z6DispatchKeySetVariable.call_method.<locals>.<dictcomp>highestPriorityTypeId)r  r   rj   rF   r
   rd   re   rF  EnumVariabler  rJ   r   )rL   rb   rY   rx   rM   methodrN   r<   r=   r     s   z"DispatchKeySetVariable.call_method)rT   rS   rn   ro   r   re   rp   rG   r  r  r   ru  r   r   rq   r<   r<   rN   r=   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 )
rY  z<represents torch._functorch.pyfunctorch.FuncTorchInterpreterc                 C   r?   r@   )r   rC   r   ZID_MATCHrD   r<   r<   r=   rG     rH   z/FuncTorchInterpreterVariable.create_with_sourcerx   rM   rI   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)levelZ
batch_sizeZ
randomnesslower)r
   r  rF   r  r   r  r  __func__rd   re   rj   Z0TemporarilyPopInterpreterStackCtxManagerVariablerJ   r   )rL   rb   rY   rx   rM   rN   r<   r=   r     s    z(FuncTorchInterpreterVariable.call_method)rT   rS   rn   ro   rp   rG   r  r   ru  r   r   rq   r<   r<   rN   r=   rY    s    

rY  )ro   r  r   loggingrh   rW   collections.abcr   typingr   Ztorch._Cr6   Ztorch._refsZtorch.fxZtorch.nnZtorch._guardsr   Ztorch._loggingr   Ztorch.utils._python_dispatchr   r   r   r	   r
   rX   r   Zcreate_parameter_opr   r   r   r  r   excr   Zguardsr   r   rB   r   r   rs  r   r   r   r   r   r   r   r  r   Zctx_managerr   r   r    Zdictsr!   distributedr"   r#   rJ  r$   r%   Ztorch_functionr&   r'   r(   numpyr  ModuleNotFoundErrorZ#torch.distributed.fsdp._fully_shardr)   Ztorch._dynamo.symbolic_convertr*   	getLoggerrT   r   ru  rv  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Z	grad_moder   r   r   r   r   r   r   r   r   r   r   r   rv   Z_shape_as_tensorrq  Zcurrent_devicer  r{  r   Z_get_device_indexZ_get_cublas_allow_tf32Z_is_any_autocast_enabledZget_device_propertiesr|  Zget_autocast_dtypeZget_autocast_gpu_dtypeZget_default_dtypeZis_autocast_cache_enabledZis_autocast_cpu_enabledZis_autocast_enabledr   r   r~  Z
_ReductionZget_enumZpromote_typesZ_get_privateuse1_backend_nameZ_is_checkpoint_validri   r  Zis_initializedZget_rankZget_world_sizerw  Zis_scripting
is_tracingZ_get_tracing_staterN  Z_symbolic_traceZis_fx_tracingZonnxZis_in_onnx_exportr   r   r   r   r   r   rr  Z
activationZ_is_make_fx_tracingr   r  r   Z_dispatch_tls_local_include_setZ_dispatch_tls_local_exclude_setrm  r  r1   r>   rr   r   r   rY  r<   r<   r<   r=   <module>   s  $	








&         %