
    sg                      ,   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mZm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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) ddl*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1 ddl2m3Z3 ddl4m5Z5m6Z6m7Z7 ddl8m9Z9m:Z: ddl;m<Z<m=Z= ddl>m?Z?m@Z@mAZA 	 d dlBZC	 d dlEmFZF erd dlGmHZH  ej                  eJ      ZKeLj                  e
j                  j                  j                  e
j                  j                  j                  e
j                  j                  j                  e
j                  j                  j                  e
j                  j                  j                  e
j                  j                  e
j                  j                  j                  e
j                  j                  j                  e
j                  j                  j                  e
j                  j                  j                  e
j                  j                  j                  e
j                  j                  j                  e
j                  j                  j                  e
j                  j                  j                  e
j                  j                  j                  e
j                  j                  j                  e
j                  j                  j                  j                  e
j                  j                  j                  j                  g      ZjeLj                  e
j                  j                  j                  e
j                  g      Zoe
j                  e
j                  j                  e
j                  j                  e
j                  j                  e
j                  j                  e
j                  j                  e
jp                  j                  e
j                  e
j                  e
j                  e
j                  e
j                  e
j                  e
j                  e
j                  e
j                  j                   j                  j                  e
j                  e
j                  j                  e
j                  j
                  gZe
jp                  j                         rTej                  e
jp                  j                  e
jp                  j                  e
jp                  j                  g       eLj                  e      Ze
j                  j                  de
j                  j                  de
j                  j                  de
j                  j                   j"                  de
j                  j$                  de
j&                  j(                  j*                  de
j                  j*                  de
j,                  j*                  de
j,                  j.                  de
j                  j0                  j2                  j4                  di
ZeLj                  g d      Z G d de3      Z G d de      Z G d d e      Zy# eD$ r dZCY w xY w# eD$ r dZFY w xY w)!    N)DictListTYPE_CHECKING)TracingContext)warning_once)_StreamBase)"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)SyntheticLocalSource)check_unspec_or_constant_argsguard_if_dynhas_torch_functionhashableproductproxy_args_kwargsunwrap_if_wrapper   )VariableTracker)AutocastModeVariableNullContextVariableTorchFunctionDisableVariable)DistributedVariableProcessGroupVariable)ListVariableTupleVariable)can_dispatch_torch_functiondispatch_torch_functionTorchFunctionModeStackVariable)_fsdp_param_group)InstructionTranslatorFT)addsubmuldivsqrtc                   T     e Zd ZdZed        Zd	 fdZd Zd Zd Z	d
dZ
d Z xZS )BaseTorchVariablezHcommon base for all torch.* functions, classes, modules and other thingsc                 f    t        |j                  t        j                                | ||      S )N)source)r   
make_guardr   FUNCTION_MATCH)clsvaluer4   s      P/var/www/html/venv/lib/python3.12/site-packages/torch/_dynamo/variables/torch.pycreate_with_sourcez$BaseTorchVariable.create_with_source   s(    f''(C(CDE5((    c                 2    t        |   di | || _        y )N )super__init__r8   )selfr8   kwargs	__class__s      r9   r?   zBaseTorchVariable.__init__   s    "6"
r;   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_]+_)	r8   
__module____name__	Exceptionidrer-   extend_outputsetup_globally_cached)r@   codegennameunique_var_names       r9   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r8   r@   s    r9   as_proxyzBaseTorchVariable.as_proxy       zzr;   c                     | j                   S rT   rU   rV   s    r9   as_python_constantz$BaseTorchVariable.as_python_constant   rX   r;   c                 l    t        | j                  |      }t        j                  j	                  |      S rT   )hasattrr8   r   ConstantVariablecreate)r@   txrP   results       r9   call_hasattrzBaseTorchVariable.call_hasattr   s)    T*))0088r;   c                 \    | j                   t        v ryt        | j                   dd       dk(  S )NTrH   math)r8   constant_fold_functionsgetattrrV   s    r9   can_constant_fold_throughz+BaseTorchVariable.can_constant_fold_through   s*    ::00tzz<6&@@r;   )returnNr_   r+   )rI   rH   __qualname____doc__classmethodr:   r?   rR   rW   rZ   ra   rf   __classcell__rB   s   @r9   r2   r2      s7    R) )
9Ar;   r2   c                   P     e Zd ZdZdefdZed        Z	 	 	 	 	 	 	 	 d fdZ xZ	S )TorchCtxManagerClassVariablezLPoints to a context manager class in torch.* that dynamo has implementationsrg   c                 "    d| j                    dS )NzTorchCtxManagerClassVariable()rU   rV   s    r9   __repr__z%TorchCtxManagerClassVariable.__repr__       .tzzl!<<r;   c                 \    t        |       } t        |       xr t        |       xr | t        v S rT   )r   callabler   supported_ctx_manager_classesrU   s    r9   is_matching_clsz,TorchCtxManagerClassVariable.is_matching_cls   s6     "%( UO  ;::	
r;   c           
         ddl 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        j.                  u r3t        |      dk(  r%|	j)                  ||d   j1                         d      S | j                  t        j2                  u rSt        |      dk  rt        |      dk(  sJ t        |      dk(  r|d   j1                         nd}|
j)                  ||      S t5        j6                  | j                        rPt9        | j                  t:              r6ddlm}  ||||j@                  jC                  d| j                  d	i             S | j                  t        jD                  jF                  jH                  t        jJ                  jD                  jH                  t        jL                  jD                  jH                  fv r!tO        j(                  | j                  ||      S | j                  t        jP                  jR                  t        jP                  jT                  t        jV                  jP                  jR                  t        jV                  jP                  jT                  fv r%tY        tZ        d
| j                         t]               S | j                  t        j^                  j`                  u r|s|rJ tc        j(                  |      S | j                  t        jd                  jf                  jh                  u r9t        |      dk(  sJ |j)                  ||D cg c]  }tk        |       c}      S | j                  t        jd                  jl                  jn                  u r!t        |      dk(  sJ |j)                  |      S | j                  t        jV                  jp                  jr                  u r9t        |      dk(  sJ |j)                  ||D cg c]  }tk        |       c}      S | j                  t        jV                  jp                  jt                  u r!t        |      dk(  sJ |j)                  |      S | j                  t        jd                  jl                  jv                  u r!t        |      dk(  sJ |j)                  |      S | j                  t        jd                  jl                  jx                  u r9t        |      dk(  sJ |j)                  ||D cg c]  }tk        |       c}      S | j                  t        jV                  jz                  j|                  u r3t        |      dk(  sJ |j)                  ||d   j1                               S t~        ]| j                  t~        j                  j                  u r7t        |      dk(  sJ |j)                  ||d   |d   j1                               S t        | U  |||      S c c}w c c}w c c}w )Nr   )!DisabledSavedTensorsHooksVariableDualLevelContextManager&FSDPParamGroupUseTrainingStateVariable&GradIncrementNestingCtxManagerVariable)GradInplaceRequiresGradCtxManagerVariableGradModeVariableInferenceModeVariable%JvpIncrementNestingCtxManagerVariableSetFwdGradEnabledContextManagerStreamVariable&VmapIncrementNestingCtxManagerVariabler   FT)initialized)wrap_fx_proxy_clscall_functionr=   z$Profiler function %s will be ignoredr
   )C ry   rz   r{   r|   r}   r~   r   r   r   r   r   r8   torchno_gradlen
isinstancer   	functionsBaseUserFunctionVariabler^   r   enable_gradset_grad_enabledrZ   inference_modeinspectisclass
issubclassr   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_nestingr   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_stater>   )r@   r_   argsrA   ry   rz   r{   r|   r}   r~   r   r   r   r   r   ctxinf_moder   xrB   s                      r9   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

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

 (..tzz4HHZZNN""NN**NN##++NN##33	
 
 DdjjQ&((ZZ588@@@''/66r::ZZ5++00GGGt9>!>9@@*./Qa/  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  w$Rv66O 0 0 0s   -["
'['
2[,
r_   r+   r   zList[VariableTracker]rA   zDict[str, VariableTracker]rg   r   )
rI   rH   ri   rj   strrr   staticmethodrw   r   rl   rm   s   @r9   ro   ro      s\    V=# = 
 
t7#t7 &t7 -	t7
 
t7 t7r;   ro   c                       e Zd ZdZdefdZd Ze ej                  d      d               Z
	 	 	 	 	 	 	 	 ddZddZedd	       Zedd
       Zy)TorchInGraphFunctionVariablez@Points to a torch function/method that should be put in FX graphrg   c                 "    d| j                    dS )NzTorchInGraphFunctionVariable(rq   rU   rV   s    r9   rr   z%TorchInGraphFunctionVariable.__repr__N  rs   r;   c                     | j                   S rT   rU   rV   s    r9   get_functionz)TorchInGraphFunctionVariable.get_functionQ  rX   r;   Nc                    ./012345678 i 66fd} ddl m} ddlm.m/m0m2m3m4m	5 ddl
m1m7m8  | t         	 	 d2.fd       } | t        j                   j"                  j$                        	 	 d21fd       } | t        j&                  j(                  j*                  j,                        d21fd	       } | t.        j0                        d21fd
       } | t        j2                  t        j                   j4                        d2.45fd       } | t        j6                  t        j8                        d2.4fd       } | t        j:                        d2.4fd       } | t<         d24fd       }	 | t        j>                  j@                  jB                  jD                  t        j>                  j@                  jB                  jF                  t        j>                  j@                  jB                  jH                  t        j>                  j@                  jB                  jJ                  t        j>                  j@                  jB                  jL                        d2d       }
 | t        jN                        .0fd       } | t        jP                        	 d3	 d2/fd       } | t        jR                        ./fd       } | t        jT                  jV                        .fd       } | t        j                   jX                  t        j                   jZ                  t        j                   j\                        d2.fd       } | t^        ja                  d tc               D               d22fd       } | t        jd                        d248fd       } | t        jf                  jh                        d2d       } | t        jj                  jl                  jn                        	 	 d2.4fd       } | t        jB                  jp                  jr                        d2d       } | t        j>                  jt                        d2d       } | t        j&                  jv                  jx                  t        j&                  jv                  jx                  jz                        d4d       } | t        j&                  jv                  j|                  t        j&                  jv                  j|                  jz                        d4d       } | t        j~                        d2d       } | t        j                        	 	 d21fd        } | t        j                        	 	 d214fd!       } | t        j                        d2.fd"       } | |      d27fd#       }t        j                         rGdd$lEmF}mG}mH}mI} mJ}! dd%lKmL}"  | ||||!|       	 	 d2.1fd&       }# | |"j                        d27fd'       }$ | t        j                  j                        	 d4dd(	 d2d)       }% | t        j>                  j                  j                        d2d*       }& | t        j                  j                  j                  j                        d2.3fd+       }' | t        jT                  j                  j                        	 	 d2d,       }( | t        j                        d234fd-       }) | t        jT                  j                        	 	 d2d.       }* | t        jT                  j                        	 	 d2.fd/       }+ | t        jT                  j                        	 	 d2.fd0       }, | t        j                        	 	 d2d1       }-6S )5zBuild 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 rT   r=   )handlerfnfnshandlerss     r9   	_registerzOTorchInGraphFunctionVariable._get_handlers.<locals>.register.<locals>._register\  s5     +BX-1r1-#*HRL+ r;   r   )ru   )r   r   r   s   ` r9   registerz<TorchInGraphFunctionVariable._get_handlers.<locals>.register[  s"     CF###r;   r   )
SDPAParamsr   )r]   DeterministicAlgorithmsVariabler~   StreamContextVariableSymNodeVariableTensorVariableUserDefinedObjectVariable)SourcelessBuilderwrap_fx_proxyr   c                 l   |s|rJ | j                   t        j                  j                  t        j                  j
                  j                  t        j                  j                  t        j                  j                  fv r|j                          j                  t        | j                            S rT   )r8   r   _utilsis_compiling_dynamoexternal_utilscompileris_dynamo_compilingmark_inconsistent_side_effectsr^   tracing_state_functionsr@   r_   r   rA   r]   s       r9   handle_tracing_state_functionszRTorchInGraphFunctionVariable._get_handlers.<locals>.handle_tracing_state_functionsr  s     F**zz)),,99++22	  113#**+B4::+NOOr;   c                 `    j                  |t        j                  j                               S rT   )r^   r   	overridesget_default_nowrap_functionsr@   r_   r   rA   r   s       r9   #handle_get_default_nowrap_functionszWTorchInGraphFunctionVariable._get_handlers.<locals>.handle_get_default_nowrap_functions  s*     %++EOO@@B r;   c                 f    |j                  j                  |t        j                        ||      S rT   )inline_user_function_returnr^   r   accumulate_gradr   s       r9   handle_accumulate_grad_zKTorchInGraphFunctionVariable._get_handlers.<locals>.handle_accumulate_grad_  s0    11!((Y-F-FGv r;   c                     t        ||      s1|j                  j                  |t        j                        ||      S y rT   )r   r   r^   r   radiansr   s       r9   handle_radianszBTorchInGraphFunctionVariable._get_handlers.<locals>.handle_radians  s?    0v>55%,,R1B1BCT6  ?r;   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   r8   r   r   is_tensor_liker\   r^   )r@   r_   argr]   r   r   s      r9   handle_is_tensorzDTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_tensor  s_    #~.

eoo<<<s$=>CII';<'..t44'..u55r;   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   dtyper8   r   is_floating_pointr^   
is_complexAssertionError)r@   r_   input	input_argr]   r   s       r9   handle_is_floating_pointzLTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_floating_point  s    
 I)^49T::!8!88+229??3T3TUUZZ5#3#33+229??3M3MNN(8DJJ<)@AA :U4r;   c                     t        |      r0|j                  $j                  t        |j                              S t        |      r|j	                  |dg i       S y )Nnumel)r   sizer^   r   call_method)r@   r_   r   r]   r   s      r9   handle_numelz@TorchInGraphFunctionVariable._get_handlers.<locals>.handle_numel  sV    %0UZZ5K'..wuzz/BCCE>2((Wb"== 3r;   c                 H    t        |      sJ |j                  |dg i       S Nr   )r   r   )r@   r_   r   r   s      r9   handle_tensor_size_rewriteszOTorchInGraphFunctionVariable._get_handlers.<locals>.handle_tensor_size_rewrites  s*    e^444$$RR88r;   c                 (    | j                  |||      S rT   )_call_ntupler@   r_   r   rA   s       r9   handle_ntuplezATorchInGraphFunctionVariable._get_handlers.<locals>.handle_ntuple  s     $$Rv66r;   c                 t    t        j                         j                  t        j                               S rT   )r   _guards_singletonr^   r   is_grad_enabled)r@   r_   r]   r~   s     r9   handle_is_grad_enabledzJTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_grad_enabled  s,    *<<=#**5+@+@+BCCr;   c                 ~    |r|j                         rt        d       j                  ||j                               S )Nz2torch.use_deterministic_algorithms(warn_only=True))rZ   r   r^   )r@   r_   mode	warn_onlyr   s       r9   #handle_use_deterministic_algorithmszWTorchInGraphFunctionVariable._get_handlers.<locals>.handle_use_deterministic_algorithms  s8     Y99;RS299"d>U>U>WXXr;   c                 t    t        j                         j                  t        j                               S rT   )r   r   r^   r   $are_deterministic_algorithms_enabled)r@   r_   r]   r   s     r9   +handle_are_deterministic_algorithms_enabledz_TorchInGraphFunctionVariable._get_handlers.<locals>.handle_are_deterministic_algorithms_enabled  s,    9KKL#**5+U+U+WXXr;   c                     t        t        j                         j                  |j                  j
                        S rT   )r   r"   r   r^   r   torch_function_enabled)r@   r_   r]   s     r9    handle_is_torch_function_enabledzTTorchInGraphFunctionVariable._get_handlers.<locals>.handle_is_torch_function_enabled  s-    6HHI#**299+K+KLLr;   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rT   )r   .0r   s     r9   	<genexpr>z`TorchInGraphFunctionVariable._get_handlers.<locals>.handle_has_torch_function.<locals>.<genexpr>  s     9a&q)9s   )r   r   r&   unpack_var_sequencer^   any)r@   r_   r   elemsr]   s       r9   handle_has_torch_functionzMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_has_torch_function  s\     t9>ja-&H Q++B/ 
 $**9599 r;   c              3   :   K   | ]  \  }}|j                     y wrT   )stream)r  rG   device_interfaces      r9   r  z=TorchInGraphFunctionVariable._get_handlers.<locals>.<genexpr>  s#      'A' !''s   c                 (    j                  ||      S rT   )r^   )r@   r_   r  r   s      r9   handle_device_interface_streamzRTorchInGraphFunctionVariable._get_handlers.<locals>.handle_device_interface_stream  s     )//F;;r;   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_   proxyexample_value)	r   trace_numpyr   npr   r   r   	as_tensorr   )r@   r_   r   r   r   s      r9   handle_from_numpyzETorchInGraphFunctionVariable._get_handlers.<locals>.handle_from_numpy  sh    %%MNHI$),bii,,#OO 'tR0
 #	 	r;   c                     |S rT   r=   )r@   r_   the_type	the_values       r9   handle_jit_annotatezGTorchInGraphFunctionVariable._get_handlers.<locals>.handle_jit_annotate  s    r;   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   r   tensorr   r'  r^   backendscudnnis_acceptable)r@   r_   r(  extra
tensor_inpr]   r   s        r9   handle_cudnn_is_acceptablezNTorchInGraphFunctionVariable._get_handlers.<locals>.handle_cudnn_is_acceptable  s{     EEE9 DCD  av||FMMRJ#**$$22:> r;   c                 H    t        j                  j                  |g|i |S rT   )r   BackwardHookVariabler^   r   s       r9   handle_backward_hookzHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_backward_hook!  s#    1188MdMfMMr;   c                 .     | j                   |g|i |S rT   )call_nn_parameterr   s       r9   handle_parameterzDTorchInGraphFunctionVariable._get_handlers.<locals>.handle_parameter%  s    )4))">t>v>>r;   Nc                 2    ||j                  |d|gi       S y r   r   self_r_   r@   dims       r9   handle_sym_sizezCTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_size)  s'     ''FSE2>> r;   c                 2    ||j                  |d|gi       S y )Nstrider6  r7  s       r9   handle_sym_stridezETorchInGraphFunctionVariable._get_handlers.<locals>.handle_sym_stride/  s%    ''HseR@@ r;   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   r8   r   r   )r   r   r   r/   r   r.   r,   )r@   r_   r   rA   r`   s        r9   handle_addcdivzBTorchInGraphFunctionVariable._get_handlers.<locals>.handle_addcdiv4  s    4yA~'V"3Fq8H 6eii@NN$qr(R 6eii@NN12 4EII>LLa&)2  9I"3~r;   c                     t        |      dk(  rHt        |d   t              s4|s1|j                  j	                  |t
        j                        ||      S y y y )Nr?  r
   )r   r   r%   r   r^   r   foreach_lerp_inplacer   s       r9   "handle_inplace_foreach_lerp_scalarzVTorchInGraphFunctionVariable._get_handlers.<locals>.handle_inplace_foreach_lerp_scalarC  s[     4yA~ja,&GPV55%,,R1O1OP  QW&G~r;   c                     t        |      dk(  rDt        |d         r4|s1|j                  j                  |t        j
                        ||      S y y y )Nr
   r   )r   r   r   r^   r   foreach_pow_scalar)r@   r_   r   rA   r   r   s       r9   handle_foreach_pow_scalarzMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_foreach_pow_scalarN  sZ     4yA~*T!Wn"Ef55%,,R1M1MN  OU"E~r;   c                     |j                         r|j                         s*t        |t        j                        r|j                         r d       S y y rT   )is_python_constantrZ   r   r   r   evaluate_expr)r@   r_   	conditionmessager]   s       r9   handle_assertzATorchInGraphFunctionVariable._get_handlers.<locals>.handle_assert[  sL    ,,.93O3O3Q9i&?&?@++-'-- . Ar;   c           
           | |j                   j                  dt        j                  j                  gt        ||       |      S )Nr   )r  
param_vars)r   r   r   r   _SDPAParamsr   )r@   r_   r   rA   r   s       r9   handle_sdpa_paramszFTorchInGraphFunctionVariable._get_handlers.<locals>.handle_sdpa_paramsc  sM     ,bii,,#HH(( 'tV4
   r;   )_get_group_size_by_name_get_group_tag_rank_not_in_group$_resolve_group_name_by_ranks_and_tagget_process_group_ranks)DTensorc                 n   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
                  | }j                  ||      S c c}w )Nr   r   r
   zInvalid group value (z) for constant pg function )r   r   r$   r%   r   r8   rZ   r^   )r@   r_   r   r   args_as_valueinvocation_resultr]   r   s         r9   &handle_constant_processgroup_functionszZTorchInGraphFunctionVariable._get_handlers.<locals>.handle_constant_processgroup_functionsy  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$>!
 )//4EFF !Ks   8B2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\  rU   )r   r]  r<  rX  kwargs_as_valuer@   s      r9   fn_with_prim_typeszaTorchInGraphFunctionVariable._get_handlers.<locals>.handle_from_local.<locals>.fn_with_prim_types  s4    %4::)-<DIRX r;   zprim r   r   r_   r  )NN)rZ   itemsr8   rI   r   r   r   )r@   r_   r   rA   r   kvkwargs_to_be_proxiedr`  rX  r_  r   s   `        @@r9   handle_from_localzETorchInGraphFunctionVariable._get_handlers.<locals>.handle_from_local  s     BFab JA!5!5!7 J !'#1 33 q++--# +>(%&fAvayL($ ( /6

8K8K.K"+$0"))00'* +!!WI0
 
% !K#
(s   CC 	C*C)layoutc                    ddl m} |r,|j                         t        j                  k(  rt        d       t        ||      st        d       y y )Nr   )BaseListVariablez3torch.compile does not support strided NestedTensorz!nested_tensor with non-list input)listsri  rZ   r   stridedr   r   )r@   r_   tensor_listrg  r   rA   ri  s          r9   handle_nested_tensorzHTorchInGraphFunctionVariable._get_handlers.<locals>.handle_nested_tensor  s@     0&335FSTk+;<AB =r;   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   rH  rZ   r   r   s       r9   handle_one_hotzBTorchInGraphFunctionVariable._get_handlers.<locals>.handle_one_hot  sa    4y3v;&!+D	QG..0G..0B6R 7 1 r;   c                     t        |      rZt        j                  j                  t        j
                  j                  j                  j                  |j                              S t        |      r|S y rT   )
r   r   r]   r^   r   fxexperimentalsymbolic_shapesguard_size_oblivioussym_num)r@   r_   exprr]   r   s      r9   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 4r;   c                 J    ddl m} t        |      j                  |g ||      S )Nr
   )_unsafe_set_version_counter)tensor_version_oprz  r   r   )r@   r_   r   rA   rz  s        r9   !handle_unsafe_set_version_counterzUTorchInGraphFunctionVariable._get_handlers.<locals>.handle_unsafe_set_version_counter  s*     H/+mB$01r;   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rT   r=   )r  ycheck_any_unspecs     r9   r  ztTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_tensor.<locals>.check_any_unspec.<locals>.<genexpr>  s     Dq/2Ds   F)r   r%   r&   r  rb  )r   r   r   r  s    r9   r  zaTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_tensor.<locals>.check_any_unspec  s>    a./!BCL-#@ADAGGDDD !r;   r   data)r   r   r   _refsr(  r   )r@   r_   r   rA   data_argr  r   r   s        @r9   handle_torch_tensorzGTorchInGraphFunctionVariable._get_handlers.<locals>.handle_torch_tensor  sx    	! H76!!&> h7<LX<V 4EKK4F4FGUU$  =W7r;   c                     |s|rJ |j                   st        d      t        j                  |       |j                   j	                         S )Nz/Popping from an empty torch function mode stack)"symbolic_torch_function_mode_stackr   r)   register_mutationpopr   s       r9   handle_pop_torch_functionzMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_pop_torch_function  sG     F**88#$UVV*<<R@88<<>>r;   c                     t        |      dk(  r|rJ t        j                  |       |j                  j	                  |d          j                  d       S )Nr   r   )r   r)   r  r  appendr^   r   s       r9   handle_push_torch_functionzNTorchInGraphFunctionVariable._get_handlers.<locals>.handle_push_torch_function  sO     t9>&00*<<R@1188aA#**400r;   c                 X    |s|rJ j                  t        |j                              S rT   )r^   r   r  r   s       r9   handle_len_torch_functionzMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_len_torch_function  s,     F**#**3r/T/T+UVVr;   c                     t        j                  |       |d   j                         r)|d   j                         t        j                  |       y t        j
                  |       y )Nr   )r)   r  rH  rZ   clear_default_device!register_device_context_insertionr   s       r9   handle_set_default_devicezMTorchInGraphFunctionVariable._get_handlers.<locals>.handle_set_default_device&  s[     +<<R@Aw))+Q0J0J0L0T.CCBG  /PPQSTr;   rh   )FrT   )]torch.backends.cudar   r   r]   r   r~   r   r   r   r   builderr   r   r   r   r   r   r   __wrapped__opsinductoraccumulate_grad_defaultrc   r   	is_tensorr   r   r   r   !REWRITE_OPS_TO_TENSOR_SIZE_METHODnn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	Parameteratensym_sizeint
sym_strideaddcdiv_foreach_lerp__foreach_pow_assertr#   is_available"torch.distributed.distributed_c10drQ  rR  rS  rT  rU  torch.distributed.tensorrV  
from_localnestednested_tensor
functionalone_hotrr  rs  rt  ru  	_autogradrz  r(  _pop_torch_function_stack_push_on_torch_function_stack_len_torch_function_stackset_default_device)9r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r!  r%  r.  r1  r4  r:  r=  r@  rC  rF  rL  rP  rQ  rR  rS  rT  rU  rV  rZ  rf  rm  rp  rx  r|  r  r  r  r  r  r]   r   r~   r   r   r   r   r   r   r   r   s9                                                 @@@@@@@@@@@r9   _get_handlersz*TorchInGraphFunctionVariable._get_handlersT  s   
 	 	3	
 	
 	
 	QP	*	+	P-	P 
,	P 
%//>>JJ	K		-		 
L		 
%))$$55==	>	 
?	
 
$,,		 
 	 
%//5??#A#A	B	6 
C	6 
##

	B	

	B 
%++		> 
	> 
4	5	9 
6	9 
HH""**HH""((HH""**HH""--HH""**

	7

	7 
%''	(	D 
)	D 
%44	5?D	Y-	Y 
6	Y 
%<<	=	Y 
>	Y 
%((55	6	M 
7	M 
OO..OO77OO44


	


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

	<

	< 
%""	#	 
$	  
%))$$	%	 
&	 
%..&&44	5	-	 
6	" 
%++##00	1	N 
2	N 
%(($$	%	? 
&	? 
%))..))599>>+B+B+F+F	G	? 
H	?
 
%))..++UYY^^-F-F-J-J	K	A 
L	A 
%--	 	 
!	 
%&&	'	-	 
(	 
%$$	%
	-
	 
&
	 
%--	 	. 
!	. 
*			 
		 ++-  9'"'4G1GG: g(() *B 
%,,,,	- 	C
 	C'	C 
.	C 
%((%%--	.	 
/	 
%((''77LL	M
	 
N
	 
%(($$@@	A	1-	1 
B	1 
%,,		 
 	4 
%((44	5	?-	? 
6	? 
%((88	9	1-	1 
:	1 
%((44	5	W-	W 
6	W 
%**	+	-	 
,	$ r;   c                 |   ddl m}mm} ddlm} | j                         r}t        ||      rq|j                   | j                         |D cg c]  }|j                          c}i |j                         D 	ci c]  \  }}	||	j                          c}	}      S | j                         j                  | j                        }
|
r |
| |g|i |}|r|S t        |||      rt        || ||      S t!        fd|D              }t#        d |D              }t%        | j                  dd      dk(  rY| j                  j&                  t(        v r=|r;|r9d	t+        | j                         d
}t,        j/                  |       t1        |       | j                  }|rSd| j                  j&                   }t%        | j                  dd       dk(  r t3        t4        |      rt%        t4        |      }d }d|v rMt7        |d   t8        j                        r0|d   j:                  j<                  j>                  d   j@                  } || |jB                  jD                  d|gtG        ||             }t7        ||      r"d|v r|d   j                         rt1        d       d|v rt7        |d   t8        j                        r|d   j                         t7        |tH              r2t7        |d   tH        tJ        f      sJ |d   j                  D cg c]  }|jM                  |       }}tO        |      D ]0  \  }}||jP                  v s|j                  |   |jP                  |<   2 tS        |d   j                  |j                        D ]  \  }}|jT                  s||jB                  jV                  v s,t7        |t8        j                        sGt7        |t8        j                        sb|jX                  |jX                  k7  s|t1        d        |S t7        ||      r"t7        |d   |      sJ d|d   j:                  j<                  j>                  v sJ |j:                  j<                  j>                  d   }|d   j:                  j<                  j>                  d   }|d   jT                  r5|d   |jB                  jV                  v r||j@                  k7  rt1        d       t4        jZ                  j]                  |      st1        d       |jM                  |d         }||jP                  v r||jP                  |<   |S t7        ||      r<|j                  /t7        |d   |      ryd|d   j:                  j<                  j>                  v sJ |d   j:                  j<                  j>                  d   }t4        jZ                  j]                  |      st1        d       |S t7        |d   tJ              rtO        |d   j                        D ]w  \  }}d|j:                  j<                  j>                  v sJ |j:                  j<                  j>                  d   }t4        jZ                  j]                  |      rmt1        d       y |S t1        dt_        |d                 |S c c}w c c}	}w c c}w )Nr   )r]   r   r   r   c              3   6   K   | ]  }t        |        y wrT   )r   )r  r   r   s     r9   r  z=TorchInGraphFunctionVariable.call_function.<locals>.<genexpr>X  s     *Xa:a+I*Xs   c              3   p   K   | ].  }t        |t        j                  t        j                  f       0 y wrT   )r   r   r]   r   r  s     r9   r  z=TorchInGraphFunctionVariable.call_function.<locals>.<genexpr>Z  s0      % 1y999;T;TUV%s   46rH   r   r   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_rc   outr  r   ra  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 )0r   r]   r   r   r  r   rf   r   r^   rZ   rb  r  getr8   r'   r(   r  allre   rI   bin_opsr   r   warningr   r\   r   r   r   r  nodemetar]  r   r   r   r&   r%   find_symbolic_locals_name	enumeratesymbolic_localszipr4   	graphargsr   _prims_commonis_contiguoustype)r@   r_   r   rA   r]   r   r   r   rc  rd  special_handlerr`   any_symints_or_symfloatsall_ints_or_floatsmsgfn_torch_sym_opfake_out_shapetensor_variableoutput_tensor_namesidxrP   
out_tensorresult_tensorfake_tensorfake_outr   s                             @r9   r   z*TorchInGraphFunctionVariable.call_function;  sS    	HG*))+0M&1
 $**)'')6:;a**,;=C\\^LTQq!..00L  ,,.224::>$T2???F&r48*2tT6BB'**XSW*X'X$!$ %% "
 

L"5@JJ''72,&		TZZ 
 C c"
 **C'!&tzz':':&;<4::|T:fD<J "%6C!N:fUmY=U=U#V "(!4!4!9!9!>!>!O!U!U+,bii,,# 'tV4O ?N;#v-?+>>@b
 6%=)*D*DE5M446>
 o}=%fUmm\5RSSSAGATAT+<=44Q7+' + &//B%C R	T2#5#557F7L7LS7QB..t4R 69u++_-B-B6 X1
M '-- *bii.A.A A *:y7O7O P *=):R:R S *=3E3E E **VWX| #"c  @%fUm^DDD*fUm.A.A.F.F.K.KKKK"1"7"7"<"<"A"A/"RK%e}2277<<_MHu,,"5MRYY-@-@@*k.?.?? &&RS ..<<XF &W 77uFDr1113B**408 #"5 0@A'--5 "&-@.&-2E2E2J2J2O2OOOO#)%=#6#6#;#;#@#@#Q$22@@J * [  #" $F5M<@&/u0C0C&D "FC#2aggll6G6G#GG#G'(ww||'8'8'IH#(#6#6#D#DX#N !.$m!"" #" "OD4G3H"IJ""o <Ld+s   Z.:Z3Z9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(rq   )has_unpack_var_sequencer   r&   listr  rH  r]   r^   r   r  r  r  r  rZ   r   )r8   countr_   s    r9   r   z@TorchInGraphFunctionVariable._call_ntuple.<locals>.handle_ntuple  s    ,,R0 ..22267  ))+ 1188HH$$**2259%:R:R:TU   ?waHIr;   )r8   r   r  r  r  r  rZ   __closure__cell_contentsr   r  r   LambdaVariable)r@   r_   r   rA   r   r  s    `   @r9   r   z)TorchInGraphFunctionVariable._call_ntuple  s    ::))//777G..0EJJ**1-;;E%%%%z	J ::))//777++M:: a))r;   c           
         |j                   rt        d       t        |t        j                        r	 |j                         }t        |t        j                        st        d| d       |j                  r| j                  |||      S t        |j                        rt        d       t               st        d       	 t        |j                  |d      j                               }|j                  |d      j                         }|j                  |d	      j                         }|j                  j!                  t"        |g      }|j$                  r|j'                  |dg i       }ddlm}	  |	||j                  j-                  dt.        |j1                         |j1                         fi             }
t        |
t        j                        sJ t2        j4                  j6                  |
_        d|
_        |j                  |
_        |
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   r  r   F)exportr   r   r   r   rZ   NotImplementedErrorr   r4   _nn_param_via_prefix_insertr	   
class_typer   tuplevar_getattrr   synthetic_graph_inputr   r  r   r  r   r   r   rW   r   r  r  has_grad_fn)r7   r_   r  r  r]  r   r'  eplaceholderr   r`   s              r9   r3  z.TorchInGraphFunctionVariable.call_nn_parameter  s    99OPmY%>%>?K - @ @ B $	 8 89OD61BCD ;;222t]KK-doo>JK02PQ	A$**2w7JJLME$$R1DDFE%%b(3FFHF ii55%ufm'L
 ##B"b9D*II"")+"6"6"89	
 &)":":;;;!HH.. # $** m ' KIJK( # 	A;A3?@@	As*   H A)H+ H('H(+	I4IIc                 4  	 ddl m} | 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/        j0                         j2                  j4                  j7                  |       |S )Nr   )VariableBuilderc                  (     j                  dd      S )Nztorch.nnr  )load_import_from)cgs   r9   <lambda>zJTorchInGraphFunctionVariable._nn_param_via_prefix_insert.<locals>.<lambda>H  s    !4!4Z!M r;   r
   F)r  get_attrzAUnexpected type of data placeholder op for parameter construction)r  r  r   new_varr   add_push_nullr4   r   r]   r   storepregraph_bytecodeextendget_instructionsrW   r  opr   r   r   r  r  example_value_from_input_noder   r  guards_contextdynamo_guardsremove_guards_with_source)
r_   r  r  r  varname	data_noder4   r  r`   r  s
            @r9   r  z8TorchInGraphFunctionVariable._nn_param_via_prefix_insert?  s:    	-))##% r]
MN
4;;
9%%m45
E"

		##**2+>+>+@AMMO((	<<::S
 &g.**II33DMMO4H4HI
 -V,]; 	++99SS	
 r;   r   rh   )NT)rI   rH   ri   rj   r   rr   r   r   	functools	lru_cacher  r   r   rk   r3  r  r=   r;   r9   r   r   K  s    J=# = Yc  cJf##f# &f# -	f#
 
f#P*6 > >@    r;   r   )r  r   loggingrc   rL   typingr   r   r   torch._Cr   torch._refstorch.fxtorch.nntorch.onnx.operatorstorch._guardsr   torch._loggingr   torch._streambaser   torch.utils._python_dispatchr	   r   r   r   r   rO   r   create_parameter_opr   r   r   r  r   excr   guardsr   r   r4   r   r  r   r   r   r   r   r   r   baser   ctx_managerr    r!   r"   distributedr#   r$   rj  r%   r&   torch_functionr'   r(   r)   numpyr  ModuleNotFoundError"torch.distributed._composable.fsdpr*   torch._dynamo.symbolic_convertr+   	getLoggerrI   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   rv   onnx	operatorsshape_as_tensor_shape_as_tensorr  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  r  
_Reductionget_enumpromote_types_get_privateuse1_backend_name_is_checkpoint_validrd   r	  is_initializedget_rankget_world_sizer  is_scripting
is_tracing_get_tracing_staterr  _symbolic_traceis_fx_tracingis_in_onnx_exportr   r   r   r   r   r  
activation_is_make_fx_tracingr   r  r2   ro   r   r=   r;   r9   <module>rJ     s       	 , ,      ( ' ) K + +  
 @  0 )   " 
 C . D
 D g! $''!!77!!,,''//--44))@@))??))FF		((  ,,  //  ((  1188		##,,

$$--%! 0 %)MM

,,% ! 
MM	LL""	HH##	HH%%	JJ$$	JJ	""		  		##	!!				HH""++		HH**	NN''' * 	!!#"",,&&,,	
 --(?@  
IIE	II%	HH	HH**E	JJ  %	MM  --t	LLt	NN	NN&&	HH33U  --<
=#A #ALJ7#4 J7ZU#4 Ue  	B
  s$   <Y: Z :ZZZZ