
    ȅi                        % S r SSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSK	r	SSK
Jr  SSKJrJr  SSK	JrJr  SSKJrJrJrJrJrJr  SSKJr  SSKJr  SSKrSS	KJr  SS
KJ r   SSK!J"r"  SSK#J$r$J%r%J&r&J'r'  SSK(J)r)J*r*J+r+  SSK,J-r-J.r.J/r/J0r0J1r1J2r2J3r3J4r4J5r5J6r6J7r7  SSK8J9r9J:r:  SSK;J<r<J=r=J>r>J?r?J@r@JArAJBrBJCrCJDrD  SSKEJFrFJGrGJHrHJIrIJJrJJKrKJLrLJMrM  SSKNJOrOJPrPJQrQJRrRJSrS  SSKTJUrU   SSKVJWrW  \(       a.  SSKYJZrZ  SSK[J\r\J]r]J^r^J_r_  SSK`Jara  SSKbJcrcJdrd  SSKeJfrfJgrg  SSKhJiri  \" S\S\4   S9rjS rkS!rl\m" 1 S"k5      rn\m" S#15      roS$rp\" 5       rq\\\4   \rS%'    " S& S'\s5      rt\R                  S(\<4S) j5       rv " S* S+5      rwS,\S(\w4S- jrxS,\S.S/S0\ S-  S1\\   S2\y\z\4   S(\y\z\S4   4S3 jr{ SS.S/S4\S5\ S-  S(\S4S6 jjr|S.S/S7\y\z\4   S(S4S8 jr}S9S/S7\y\z\S4   S:\	R                  S(S4S; jrS:\	R                  S<\y\z\4   S=\zS>\\S4   S-  S?\\   S-  S@\y\z\4   S-  SA\y\z\4   S-  S(\	R                   4SB jr1 SCkrS.S/SD\S5\ S-  S=\zS(\S4
SE jr " SF SG\S5      r " SH SI\5      r " SJ SK\5      r " SL SM\5      r " SN SO\S5      r " SP SQ\5      r " SR SS\5      r " ST SU\5      r " SV SW\5      r " SX SY\5      r " SZ S[\5      rS.S/SD\S\4   S=\zS1\\S   S2\y\z\S4   S(\S4S\ jr " S] S^\5      r " S_ S`\5      r " Sa Sb\S5      r " Sc Sd\5      r " Se Sf\S5      r " Sg Sh\5      rS(\y\\4   4Si jrS.S/SD\S\4   S(\<4Sj jr " Sk Sl\5      r " Sm Sn\5      r " So Sp\5      r " Sq Sr\S5      r " Ss St\S5      r " Su Sv\S5      rSSwKbJrJrJrJr   " Sx Sy\5      r\" 5       r " Sz S{\S5      r " S| S}\S5      r " S~ S\S5      r " S S\S5      r " S S\S5      r " S S\5      r " S S\5      rg! \X a    SrW GNf = f)a  
Function-related variable tracking classes for Dynamo's symbolic execution.

This module contains classes that track different types of functions during graph
compilation, including:
- User-defined functions and methods
- Built-in functions and methods
- Wrapped functions (e.g. from decorators)
- Special function types (e.g. functools.partial)
- Triton kernels and related function types

These classes are responsible for:
- Tracking function calls and their arguments
- Managing function closures and cell variables
- Handling function attributes and special methods
- Maintaining guards for function identity and closure contents
- Supporting function inlining and specialization
- Enabling proper symbolic execution of different function types

The variable trackers here work together with the rest of Dynamo to enable
accurate graph capture while handling Python's various function-related behaviors.
    N)
namedtuple)CallableSequence)CellTypeFunctionType)AnycastLiteralOptionalTYPE_CHECKINGTypeVar)Never)WeakKeyDictionary)get_stack_above_dynamo)Source)is_namedtuple_class   )configgraph_break_hints	polyfills	variables)create_call_functioncreate_rot_nis_generator)format_frame_infoget_dynamo_observed_exceptionhandle_observed_exceptionInfiniteGeneratorErrorObservedExceptionObservedGeneratorExitObservedUserStopIterationraise_observed_exceptionStepUnsupportedunimplementedUnsupported)GuardBuilderinstall_guard)	
AttrSourceClosureSourceCollectionsSourceConstantSourceDefaultsSourceGetItemSourceSkipGuardSourceTorchSource
TypeSource)check_constant_argscheck_unspec_or_constant_argscmp_name_to_op_mappingidentityis_functionis_wrapper_or_member_descriptoristype	make_cell   )#AsPythonConstantNotImplementedErrorAttributeMutationNewraise_type_error_excValueMutationNewVariableTrackerConstantVariable)_fsdp_param_group)	PyCodegen)&InliningGeneratorInstructionTranslatorInliningInstructionTranslatorInstructionTranslatorInstructionTranslatorBase)ContextWrappingVariable)TritonGridTypeTritonKernelType)BaseListVariableListVariable)TensorVariable_F.)bound      >   is_leaf	namespacenone_is_leafrQ   zihttps://github.com/pytorch/pytorch/issues/new?&labels=oncall%3A+pt2&projects=&template=pt2-bug-report.yml_spec_cachec                       \ rS rSrSrg)ClosureConversionError    N)__name__
__module____qualname____firstlineno____static_attributes__rX       [/home/james-whalen/.local/lib/python3.13/site-packages/torch/_dynamo/variables/functions.pyrV   rV      s    r^   rV   returnc                  R    [        [        [        [        5       S5      S5      S5      $ )Nutils_pytreeSUPPORTED_NODES)r(   r/   rX   r^   r_   !get_pytree_SUPPORTED_NODES_sourcere      s%    :kmW5yACT r^   c                   8    \ rS rSrS\SS4S jrS\SS4S jrSrg)FunctionSpec   funcr`   Nc                    UR                   nUR                  nUR                  U l        UR                  U l        UR                  U l        US U R                   U l        X0R                  U R
                   U l	        U R                  U R                  -   U l
        X0R
                  U R
                  U R                  -    U l        U R
                  U R                  -   nUR                  [        -  (       a  X4   OS U l        X@R                  (       a  SOS-  nUR                  [        -  (       a
  X4   U l        g S U l        g )Nr9   r   )__code__co_varnamesco_posonlyargcountposonly_countco_argcount	arg_countco_kwonlyargcountkwonly_countposonly_namespos_or_kw_namesall_pos_nameskwonly_namesco_flags
CO_VARARGSvarargs_nameCO_VARKEYWORDS
varkw_name)selfri   codevnoffs        r_   __init__FunctionSpec.__init__   s    }}!44)) 22 4$"4"45!"4"4t~~F!//$2F2FF~~ARAR0RSnnt000'+}}z'ABGt%%q1,%)]]^%C"'r^   c           
         UR                   =(       d    SU l        UR                  =(       d    0 U l        [	        [        U R                  [        U R                  5      * S  [        [        U R                  5      5      5      5      U l	        g NrX   )
__defaults__defaults__kwdefaults__
kwdefaultsdictzipru   lenrangepos_default_map)r|   ri   s     r_   update_defaultsFunctionSpec.update_defaults   si     ))/R--3  $""C$6#6#895T]]AS;TU 
r^   )ru   rp   r   r   rr   rv   r   rt   rn   rs   ry   r{   )rY   rZ   r[   r\   r   r   r   r]   rX   r^   r_   rg   rg      s+    N\ Nd N$	
L 	
T 	
r^   rg   ri   c                 ^    [         R                  U 5      nUc  [        U 5      nU[         U '   U$ N)rT   getrg   )ri   specs     r_   	_get_specr      s.    ??4 D|D! DKr^   txrE   	fn_sourceargskwargsc                    [        U 5      nUR                  U 5        0 n[        U5      nUR                  R	                  5        Hq  u  pS n
U(       aH  [
        R                  " UR                  U	   5      (       a  [        R                  (       d  [        X)5      n
[        XR                  U	   U
5      Xh'   Ms     [        UR                  5       H  u  pU[        U5      :  a  [        XU   5      Xh'   M&  X;   a-  XR                  ;  a  [        XR!                  U5      5      Xh'   MX  X;  d  M_  [#        [$        U[
        R&                  " SU 35      /S9  M     U[        UR                  5      S  nUR(                  (       a"  [        U[+        U5      5      XeR(                  '   OMU(       aF  [#        [$        U[
        R&                  " S[        U5       S[        UR                  5       35      /S9  UR,                   H  nX;   a  [        XR!                  U5      5      Xh'   M&  XR.                  ;   a0  S nU(       a
  [        X(SS9n[        XR.                  U   U5      Xh'   Me  [#        [$        U[
        R&                  " SU 35      /S9  M     UR0                  (       a  [        X5      XeR0                  '   U$ U(       a0  [#        [$        U[
        R&                  " S[3        U5       35      /S9  U$ )	Nz&Missing required positional argument: r   z#Too many positional arguments: got z, expected T)is_kwz(Missing required keyword-only argument: zUnexpected keyword arguments: )r   r   r   r   itemsr@   
is_literalr   r   %skip_guards_on_constant_func_defaultsr,   wrap_bound_arg	enumerateru   r   rs   popr"   	TypeErrorcreatery   tuplerv   r   r{   list)ri   r   r   r   r   r   barem_kwnameidxdefault_sourceiextrakwdefault_sources                 r_   bind_args_cachedr      s    T?D	B&\F ))//1	''c(:;;<<+I;N!"mmC&8.I 2 T//0s4y=%bq'2BH^ *** &b**T*:;BH^$$++@G! 16 T''(*+E .r5< @	  ''9#d)KPSTXTfTfPgOhi	
 !!>%b**T*:;BH__$##1)#N %b//$*?AQRBH$$++B4&I "( ,R8?? I 
  ''*Hf(WX	
 Ir^   valsourcec                     [        U[        5      (       a  U$ U(       d  [        R                  " X5      $ [        R                  R                  X5      $ r   )
isinstancer>   buildr   LazyVariableTrackerr   )r   r   r   s      r_   r   r     sC     #''
$$R-- ,,33C@@r^   resultc                     [        UR                  5       5       H/  u  p#[        U[        [        45      (       d  M"  [        X5      X'   M1     g r   )r   r   r   r   r   r   )r   r   kvs       r_   wrap_args_kwargsr   $  s6    V\\^$a%''&r-FI %r^   parentr}   c                     U R                   R                  nUR                   H<  nUR                  5       nXA;   a   UR	                  XQR                  U5      5        XQU'   M>     g)z
Update `result` to add mapping from local name to new cells created
directly by `code`, or update SideEffects in `parent` if the a local cell is
already in `result` (cell argument).
N)outputside_effectsco_cellvarstrack_cell_new
store_cellr   )r   r   r}   r   r   new_cells         r_   init_cellvarsr   +  sV     ==--L  ..0> ##Hjj.>?t !r^   	f_globalsr   r   closurer   annotationsc                     SSK Jn  U" XX#U5      nXXl        [        U[        5      (       a  SSKJn	  [        U	" U5      5      nUb  [        U[        5      (       d   eXhl        U$ )Nr   r   pairwise)	typesr   r   r   r   	itertoolsr   r   __annotations__)
r}   r   r   r   r   r   r   r   ri   r   s
             r_   _create_nested_fnr   @  s_     #AD$+u%%&8K01 *[$"?"???&Kr^   >   __doc__rk   __closure____globals__r   r   r   fnc                 n   U=(       a    [        X#5      nU(       a  US:X  a  [        U5      nS n [        R                  " X5      nU[        ;   a  [        X5      nU(       a  [        R                  R                  XB5      $ [        R                  " X5      $ ! [         a    [        [        U 5         Nmf = f)Nr   )r(   r.   inspectgetattr_staticAttributeErrorr"   fn_known_dunder_attrsgetattrr   r   r   r>   r   )r   r   r   r   subobjs        r_   fn_var_getattrr   e  s     0
60F$++ !(F5''1 $$",,33FCC  ,,  5 	!45s   B B43B4c                       \ rS rSrS\4S jrS\4S jrS\\\4   4S jr	SSS\
\   S	\\\4   S\4S
 jrSSS\S\4S jrSSS\\\4   4S jrS\4S jrSrg)BaseUserFunctionVariablei  r`   c                 6    U R                  5       R                  $ r   get_codeco_filenamer|   s    r_   get_filename%BaseUserFunctionVariable.get_filename      }}***r^   c                 6    U R                  5       R                  $ r   r   co_namer   s    r_   get_name!BaseUserFunctionVariable.get_name      }}&&&r^   c                     [         er   NotImplementedErrorr   s    r_   get_globals$BaseUserFunctionVariable.get_globals  s    !!r^   r   rE   r   r   c                     U R                  5       S:X  a:  U R                  5       R                  S5      (       a  [        R                  " S 5      $ UR                  U / U R                  5       QUQU5      $ )Npatch_track_step_calledztorch/optim/lr_scheduler.py)r   r   endswithr@   r   inline_user_function_return	self_argsr|   r   r   r   s       r_   call_function&BaseUserFunctionVariable.call_function  sg     MMO88!!#,,-JKK#**400--d4Ndnn6F4N4NPVWWr^   r   c                     Sn [        U R                  5       U5      n[
        R                  R                  U5      $ ! [         a     US:X  a  [        U [        5      (       a  Sn NHf = f)NFrY   T)hasattrget_functionr   r   NestedUserFunctionVariabler   r@   r   r|   r   r   r   s       r_   call_obj_hasattr)BaseUserFunctionVariable.call_obj_hasattr  sh     	T..0$7F ))0088 # 	z!j7Q&R&R	s   = 'A'&A'c                     0 $ r   rX   r|   r   s     r_   closure_vars%BaseUserFunctionVariable.closure_vars      	r^   c                     gNTrX   r   s    r_    should_allow_nested_graph_breaks9BaseUserFunctionVariable.should_allow_nested_graph_breaks      r^   rX   N)rY   rZ   r[   r\   strr   r   r   r   r   r   r>   r   r@   r   r   boolr  r]   rX   r^   r_   r   r     s    +c +'# '"T#s(^ "X#X 'X S/)*	X
 
X
9)
914
9	
96 4_@T;U $ r^   r   c                   N  ^  \ rS rSrSrSS1\R                  kr\" 1 Sk5      r\	S\
S\
SS 4S	 j5       r S-S\R                  \R                  R                   -  S\S
\
SS4U 4S jjjrS\
4U 4S jjrS\\   4S jrS\R                  4S jrS\R0                  4S jrS\4S jrS\4S jrS\\\
4   4S jrS\ 4S jr!SSS\"\   S
\\\4   S\\\4   4S jr#SSS\S\4S jr$SSS\S\%4S jr&SSS\"\   S
\\\4   S\4U 4S jjr'SSS\"\   S
\\\4   S\S-  4S jr(S\4S jr)S\4S  jr*SSS\"\   S
\\\4   S\+S \"\   \\\4   4   S-  4S! jr,S\R                  S-  4S" jr-S#\S\+\S$4   S-  4S% jr.S\
S\\   S-  4S& jr/S\0S'   4S( jr1S\24S) jr3S*\4S\4S+ jr5S,r6U =r7$ ).UserFunctionVariablei  z-Some unsupported user-defined global functionr   is_constant>   optree
optree.opstorch.utils._pytreetorch.utils._cxx_pytreevaluer   r`   c                 ^    [        UR                  [        R                  5      5        U " XS9$ Nr   r'   
make_guardr&   CLOSURE_MATCHclsr  r   s      r_   create_with_source'UserFunctionVariable.create_with_source  s&    f''(B(BCD5((r^   r   Nc                 R  > [         TU ]  " S	0 UD6  [        USS5      (       a  SU l        OSU l        [	        U[
        R                  [        R                  R                  45      (       d  [        SU SS/[        R                  QS9  [        R                  " USU5      nXl        g )
N_dynamo_marked_constantFTz1can't handle functions not implemented in python z2Dynamo can only handle functions defined in pythonz9Move usage of this function out of `torch.compile` regiongb_typecontextexplanationhints_torchdynamo_inlinerX   )superr   r   r  r   r   r   torchjitScriptFunctionr$   r   INFERENCE_MODEr   r   r   r|   r   r  r   	__class__s       r_   r   UserFunctionVariable.__init__  s     	"6"20%88#D$D "u115993K3KLMMK$PO&55	 ##B(=rBr^   c                 b   > [        U [        5      (       a  U R                  $ [        TU ]  5       $ r   )r7   r
  r   r#  as_python_constant)r|   r)  s    r_   r,  'UserFunctionVariable.as_python_constant  s(    $,--77Nw)++r^   c                     / $ r   rX   r   s    r_   r   UserFunctionVariable.self_args  r  r^   c                     U R                   $ r   r   r   s    r_   r   !UserFunctionVariable.get_function      wwr^   c                 .    U R                   R                  $ r   )r   rk   r   s    r_   r   UserFunctionVariable.get_code  s    wwr^   c                 "    [         R                  $ r   r   r   r   s    r_   python_type UserFunctionVariable.python_type      !!!r^   c                 4    [        U R                  SS 5      S L$ N__self__)r   r   r   s    r_   has_selfUserFunctionVariable.has_self  s    tww
D1==r^   c                 .    U R                   R                  $ r   )r   r   r   s    r_   r    UserFunctionVariable.get_globals  s    ww"""r^   c                     U R                   nU(       a+  [        U [        R                  5      (       a  U R                  nU$ r   )r   r   r   UserMethodVariable	source_fn)r|   r   s     r_   
get_sourceUserFunctionVariable.get_source  s.    jy'C'CDD^^Fr^   r   rE   r   c                 R   U R                   (       a   eU R                  n[        U[        5      (       d  [	        S5      eUR
                  R                  nU R                  5       n[        XEXbU5      n[        XUR                  5        U R                  R                  =(       d    Sn[        U5      [        U R                  R                  R                  5      :X  d   e[        [        R                   " 5       U R                  R                  R                  U5       H  u  pnUR
                  R"                  nX;   a  X   nOU(       aV  [%        ['        U5      U	5      n[)        US5      n [*        R,                  " XR.                  U5      nUR7                  XU5      nO4 [*        R,                  " XR.                  5      nUR7                  SUU5      nXU
'   M     U$ ! [0         a    [2        R4                  " 5       n Nrf = f! [0         a    [2        R4                  " 5       n Ncf = f)z
Assume `args` and `kwargs` are VariableTracker arguments for a call to
this function, create new bindings for initial locals.
z'Only supports regular Python functions.rX   cell_contentsN)r  r   r   r   r   r   root_txrE  r   r   rk   r   r   co_freevarsr   r   countr   r-   r)   r(   r>   r   rH  
ValueErrorr   DeletedVariabletrack_cell_existing)r|   r   r   r   r   rI  r   r   r   r   r   cellr   cell_varclosure_cellclosure_cell_contentscontents_vars                    r_   	bind_argsUserFunctionVariable.bind_args  s    ####!%"l++EFF--''"!"vVDfbkk2''%%+7|s477#3#3#?#?@@@@"OOtww//;;W 
OCt "==55L#'-,]6-BCH(2<(Q%?#2#8#8 2 24I$L (;; ?#2#8#8ASAS#TL (;;D$U#4LA 
D ' " ?#,#<#<#>L? " ?#,#<#<#>L?s$   -!G" HH HH&%H&r   r   c                     U[         ;   a  [        R                  " X5      $ U R                  5       n[	        XR
                  X25      $ r   )r3   r   GetAttrVariablerE  r   r   )r|   r   r   r   s       r_   var_getattr UserFunctionVariable.var_getattr@  s9    )),,T88"b''688r^   c                 l    [        U R                  U5      n[        R                  R	                  U5      $ r   )r   r   r   r@   r   r   s       r_   r   %UserFunctionVariable.call_obj_hasattrF  s+     $'))0088r^   c           
      	  > U R                   [        R                  R                  L a   U Vs/ s H  oDR	                  5       PM     nnUR                  5        VVs0 s H  u  pgXgR	                  5       _M     nnn[        R                  R                  " U0 UD6R                  n	[        R                  " U	5      $ U R                   [        R                  R                  L a{   [        R                  " U R                   5      R                  " U0 UD6nUR                  S   R	                  5       n[!        U["        5      (       d   e[        R$                  " U5      $ U R                   [        R                  R(                  L Ga  [        R                  " U R                   5      R                  " U0 UD6nUR*                  S   n[!        U[,        5      (       dP  UR/                  5       nSUR0                   S3n[3        SS	U R4                   S
U S
U S3U/ [6        R8                  QS9  [!        U[:        5      (       d)  UR=                  5       nSU S3n[3        SU  USU S3S/S9  UR                   n[        R>                  " USS9$ U R@                  (       a%  [C        XR                   U R=                  5       X#5      $ URD                  RF                  RH                  (       dt  U R                   [        R                  RJ                  RL                  L aC  [        R                  RN                  RQ                  U5         [R        TU ]  XU5      sS S S 5        $ [W        URD                  RF                  SS 5      S:X  a  URD                  RF                  RX                  (       ds   SSK-J.n  Ubi  U R                   UR^                  UR`                  4;   aC  [        R                  RN                  RY                  U5         [R        TU ]  XU5      sS S S 5        $ U Rc                  XU5      nUb  U$ [R        TU ]  XU5      $ s  snf s  snnf ! [         a  n
[        SU SU 35      U
eS n
A
ff = f! [&         a  n
[        SU SU 35      U
eS n
A
ff = f! , (       d  f       GNF= f! [&         a    S n GNf = f! , (       d  f       N= f)NzCannot convert patch_dynamo_config args/kwargs to constants. Please fix your call to patch_dynamo_config by using simpler inputs. args: z
, kwargs: error_on_graph_breakz\Improper error_on_graph_break() call. Please fix your call to error_on_graph_break(). args: r   z=`nonstrict_trace` expects a callable, but got value of type <>zTypeError from user codezcall_function(, )r  z(Applying `nonstrict_trace` to function <zk>; however, `nonstrict_trace` currently requires the function to be defined outside `torch.compile` region.zLimitation of `nonstrict_tracezmake sure definition of z is outside z`torch.compile` regionTnonstrict_traceabledescriptionz!torch.utils.checkpoint.checkpoint)	FSDPState)2r   r$  _dynamopatch_dynamo_configr,  r   changesr   DynamoConfigPatchVariabler:   RuntimeErrorr]  r   	signaturebind	argumentsr   r  ErrorOnGraphBreakVariable	Exceptionnonstrict_tracer   r   r8  rY   r$   r  r   
USER_ERRORr
  r   TorchInGraphFunctionVariabler  invoke_and_store_as_constantr   current_tracer,unsafe_allow_externally_visible_side_effectsrb   8_disable_side_effect_safety_checks_for_current_subtracerr   2allow_externally_visible_side_effects_in_subtracerr#  r   r   allow_side_effects_in_hop/torch.distributed.fsdp._fully_shard._fsdp_staterd  _pre_forward_post_forward_maybe_call_tree_map_fastpath)r|   r   r   r   arg
args_constkeyr   kwargs_constrg  erN   r]  fn_vartypmsgfn_namer   rd  tree_map_resultr)  s                       r_   r   "UserFunctionVariable.call_functionL  sc    77emm777BFG$3446$
GBH,,. BPhcC//11.     --;;#/'  !::7CC WW:::))$''277HH',*($$& % ""6==== ::;OPP WW555%%dgg.33TDVDEZZ]Ff&>??((*UVYVbVbUccde6,TZZL4&6(!L #*55	 f&:;; //+@	  Ju  v<#f #27)<H0	 B99"RVWW/GGT]]_d 
 		((UU}}""[[\ ++^^ w,Rv>  BII,,mTB23II,,FF!U $&&''5 * ]]//II"M 706B NM <<RvN&""w$Rv66} H  7 "!F*VH6 	  "!F*VH6 ^   ! 	! NMsx   Q P;Q Q 8AQ #A9Q* =R"R  :R3;Q 
Q'Q""Q'*
R4RR
R R0/R03
Sc                     U R                  XU5      nUb  Uu  pVnOU nUnUn[        U[        5      (       a4  UR                  5       (       a  1 Uk[        -
  (       d  [        U5      S:  d  g US   nUS   n	USS  n
U	R                  UUUU
U5      $ )Nr   r   r9   )_rewrite_tree_map_only_callr   r
  _is_tree_map_function_SUPPORTED_TREE_MAP_KWARGSr   call_tree_map)r|   r   r   r   rewritetree_map_fntree_map_argstree_map_kwargsmap_fn
first_treerests              r_   r{  2UserFunctionVariable._maybe_call_tree_map_fastpath  s     222VD:A7KK M$O {$8991133''*DDM"a'q!"1%
QR ''
 	
r^   c                     [        U R                  SS 5      S:H  =(       a$    [        U R                  SS 5      U R                  ;   $ )NrY   tree_maprZ   r   r   _TREE_MAP_MODULESr   s    r_   r  *UserFunctionVariable._is_tree_map_function  s=    DGGZ.*< Ot48N8NN	
r^   c                     [        U R                  SS 5      S:H  =(       a$    [        U R                  SS 5      U R                  ;   $ )NrY   tree_map_onlyrZ   r  r   s    r_   _is_tree_map_only_function/UserFunctionVariable._is_tree_map_only_function  s=    DGGZ./A Ot48N8NN	
r^   c           	      B   U R                  5       (       d  g [        U5      S:w  a  g 1 Uk[        -
  (       a  g Uu  pEnU R                  U5      nUc  g U R	                  5       nUc  g [        UU[        USS 5      S9n	[        R                  " U5      n
XU/[        U5      4$ )N   r   r  )
r  r   _TREE_MAP_ONLY_SUPPORTED_KWARGS_extract_tree_map_only_types_lookup_tree_map_functionTreeMapOnlyFunctionVariabler   r   r
  r   )r|   r   r   r   type_selectorr  tree_argallowed_typestree_map_callablewrapped_map_fntree_map_variables              r_   r  0UserFunctionVariable._rewrite_tree_map_only_call  s     ..00t9>V966*.'x99-H  ::<$468T2

 &::;LM 8"<d6lJJr^   c                     [        U R                  SS 5      nU(       d  g [        R                  R	                  U5      nUc  g [        USS 5      n[        U[        R                  5      (       a  U$ g )NrZ   r  )r   r   sysmodulesr   r   r   r   )r|   module_namemoduler  s       r_   r  .UserFunctionVariable._lookup_tree_map_function  s]    dgg|T:->6:t4h 2 233Or^   selector.c                    UR                  5       (       d  g  UR                  5       nU R                  U5      nU(       d  g [	        S U 5       5      (       d  g [        [        R                  U5      5      $ ! [         a     g f = f)Nc              3   B   #    U  H  n[        U[        5      v   M     g 7fr   )r   type).0r  s     r_   	<genexpr>DUserFunctionVariable._extract_tree_map_only_types.<locals>.<genexpr>!  s     >IS:c4((Is   )is_python_constantr,  r   _flatten_type_specallr   r   fromkeys)r|   r  	raw_value	flatteneds       r_   r  1UserFunctionVariable._extract_tree_map_only_types  s}     **,,	 335I ++I6	>I>>>T]]9-.. # 		s   A7 7
BBc                    [        U[        5      (       a  U/$ [        U[        5      (       a4  / nU H*  nU R                  U5      nUc    g UR	                  U5        M,     U$ [        [        SS 5      nUbN  [        X5      (       a>  / nUR                   H*  nU R                  U5      nUc    g UR	                  U5        M,     U$ g )N	UnionType)r   r  r   r  extendr   r   __args__)r|   r  	collectedentryflat
union_types         r_   r  'UserFunctionVariable._flatten_type_spec%  s    eT""7NeU##$&I..u5<  &	 
 UK6
!j&C&CI..u5<  &	 (
 r^   Tc                     gr  rX   r   s    r_   is_python_hashable'UserFunctionVariable.is_python_hashable;  r  r^   c                 ,    [        U R                  5      $ r   )hashr   r   s    r_   get_python_hash$UserFunctionVariable.get_python_hash>  s    DGG}r^   otherc                 t    [        U[        R                  5      =(       a    U R                  UR                  L $ r   )r   r   r
  r   r|   r  s     r_   is_python_equal$UserFunctionVariable.is_python_equalA  s*    %!?!?@XTWWPUPXPXEXXr^   )r   r  F)8rY   rZ   r[   r\   r   r   _nonvar_fields	frozensetr  classmethodr   r  r   r   r$  r%  r&  r  r   r,  r   r>   r   r   CodeTyper   r  r8  r>  r   r  r   r   rE  r   rT  rX  r@   r   r   r{  r  r  r   r  r  r  r  r
   r  intr  objectr  r]   __classcell__r)  s   @r_   r
  r
    s1   7 	 
"	0	0N "	
 )s )C )<R ) ) "!9!99  	
 
 >,C ,40 e00  %..  "T ">$ >#T#s(^ #F :': ': S/)*	:
 
c?"	#:x95 9S 9_ 99)9149	9g7#g7 'g7 S/)*	g7
 
g7R
#
 '
 S/)*	

 
4	
B
t 

D 
$K#$K '$K S/)*	$K 	"_%o%&(	

 	$KL
5+=+=+D 
/'/	tSy	D	 /" T
T0A ,GDM  YV Y Y Yr^   r
  c                      ^  \ rS rSrS1\R
                  krS\\S4   S\S\	SS4U 4S jjr
S\4S	 jrS
\S\4S jrSSS\\   S\\\4   S\4S jrSrU =r$ )r  iE  r  .r  r   r`   Nc                 >   > [         TU ]  " S0 UD6  Xl        X l        g r   )r#  r   r  r  )r|   r  r  r   r)  s       r_   r   $TreeMapOnlyFunctionVariable.__init__K  s      	"6"*r^   c                     [         $ r   r   r   s    r_   r8  'TreeMapOnlyFunctionVariable.python_typeU  s    r^   nodec                    ^  UR                  5       m[        U4S jU R                   5       5      $ ! [         a     gf = f)NFc              3   <   >#    U  H  n[        TU5      v   M     g 7fr   )
issubclass)r  allowed	node_types     r_   r  DTreeMapOnlyFunctionVariable._matches_allowed_type.<locals>.<genexpr>]  s     TASg:i11ASs   )r8  r   anyr  )r|   r  r  s     @r_   _matches_allowed_type1TreeMapOnlyFunctionVariable._matches_allowed_typeX  sC    	((*I TASASTTT # 		s   2 
??r   rE   r   c                    U(       d  U R                   R                  XU5      $ US   nU R                  U5      (       a  U R                   R                  XU5      $ [        U5      S:w  d  U(       a  U R                   R                  XU5      $ U$ )Nr   r9   )r  r   r  r   )r|   r   r   r   leafs        r_   r   )TreeMapOnlyFunctionVariable.call_function_  sz     ;;,,Rv>>Aw%%d++;;,,Rv>>t9>V ;;,,Rv>>r^   )r  r  )rY   rZ   r[   r\   r   r  r   r  r>   r   r   r8  r  r  r   r   r  r   r]   r  r  s   @r_   r  r  E  s    	!	0	0N
T3Y'   	
 
T U/ Ud U# ' S/)*	
 
 r^   r  c            	          ^  \ rS rSr SS\R
                  S\S\SS4U 4S jjjr\	S\S\4S	 j5       r
S
SS\\   S\\\4   S\4S jrSrU =r$ )BuiltinMethodVariableiq  r   r  r   r`   Nc                 t   > [         TU ]  " S0 UD6  [        U[        R                  5      (       d   eXl        g r   )r#  r   r   r   BuiltinMethodTyper   r(  s       r_   r   BuiltinMethodVariable.__init__r  s3     	"6""e556666r^   objc                     U R                   nU R                  nU[        L =(       a    US:H  =(       d    [        U5      [        L =(       a    US:H  $ )N__new____contains__)r=  rY   r   r  r  )r  method_selfmethod_names      r_   is_supported_builtin_method1BuiltinMethodVariable.is_supported_builtin_methody  sI    llll u$A	)A 
*L{n/L	
r^   r   rE   r   c                     U R                   R                  nU R                   R                  nU R                  =(       a    [	        U R                  S5      n[
        R                  " XU5      nUR                  XX#5      $ r<  )r   r=  rY   r   r(   r>   r   call_method)r|   r   r   r   r  r   
obj_sourceobj_vts           r_   r   #BuiltinMethodVariable.call_function  s`     gg&&ww[[HZZ%H
 &&r
C!!"D99r^   r1  r  )rY   rZ   r[   r\   r   r  r  r   r   staticmethodr  r   r>   r   r  r   r]   r  r  s   @r_   r  r  q  s    ?D))8<PS	  
 
 
 

:#
: '
: S/)*	
:
 

: 
:r^   r  c            
       <  ^  \ rS rSrS\R
                  S\\\4   SSS\SS4
U 4S	 jjr	S\R
                  4S
 jr
S\4S jrS\4S jrS\4S jrS\4S jrS\4S jr S\4S jr\rS)S jrSSS\\   S\\\4   S\\\4   4S jrS\\\4   4S jrS\4S jrSSS\4S jrSSS\S\4S jrSSS\4S jrSSS\4S jrSSS\\   4S jr SSS\!\/\4   SS4S  jr"S\#S!   4S" jr$SSS#\SS4S$ jr%S\4S% jr&S\4S& jr'SSS\S\\   S\\\4   S\4
U 4S' jjr(S(r)U =r*$ )*LocalGeneratorObjectVariablei  r}   r   inline_tracerrC   r   r`   Nc                 J   > [         TU ]  " S0 UD6  Xl        X l        X0l        g r   )r#  r   r}   r   r  )r|   r}   r   r  r   r)  s        r_   r   %LocalGeneratorObjectVariable.__init__  s%     	"6"	"*r^   c                     U R                   $ r   )r}   r   s    r_   r   %LocalGeneratorObjectVariable.get_code  s    yyr^   c                 6    U R                  5       R                  $ r   r   r   s    r_   r   )LocalGeneratorObjectVariable.get_filename  r   r^   c                 6    U R                  5       R                  $ r   r   r   s    r_   r   %LocalGeneratorObjectVariable.get_name  r   r^   c                     [        S5      e)Nr   r   r   s    r_   r   )LocalGeneratorObjectVariable.get_function  s    !.11r^   c                     gNFrX   r   s    r_   r>  %LocalGeneratorObjectVariable.has_self      r^   c                 "    U R                  5       $ r   )r   r   s    r_   rY   %LocalGeneratorObjectVariable.__name__  s    }}r^   c                 V    U R                   R                   SU R                  5        S3$ )N(r`  )r)  rY   r   r   s    r_   __str__$LocalGeneratorObjectVariable.__str__  s'    ..))*!DMMO+<A>>r^   c                    SSK Jn  SSKJnJn  UR
                  nU" U5      nU" U5      nU" U5      nU   U   U   U R                  n	U	R                  (       d  U R                  U5      U l	        [        R                  " U R                  5      R                  U5        S S S 5        S S S 5        S S S 5        g ! , (       d  f       N= f! , (       d  f       N(= f! , (       d  f       g = f)Nr   )"disallow_side_effects_in_generator) save_and_restart_speculation_log(temporarely_allow_writes_to_output_graph)torch._dynamo.side_effectsr  torch._dynamo.symbolic_convertr  r  r   r  generator_exhaustedforce_unpack_var_sequenceremaining_itemsr   ListIteratorVariablereconstruct)
r|   codegenr  r  r  r   savedisallowtemptracers
             r_   r"  (LocalGeneratorObjectVariable.reconstruct  s    Q	

 ZZ/35b97;8T''F--'+'E'Eb'I$**4+?+?@LLWU	 "8TTTT88TTs:   CCA#B7C&C7
CC
C	C
C'r   rE   r   c                 :    U R                   R                  XU5      $ r   )vtrT  r   s       r_   rT  &LocalGeneratorObjectVariable.bind_args  s     ww  622r^   c                     U R                   $ r   r   r   s    r_   r   (LocalGeneratorObjectVariable.get_globals      ~~r^   c                 "    [         R                  $ r   )r   GeneratorTyper   s    r_   r8  (LocalGeneratorObjectVariable.python_type  s    """r^   rF   c           	      "   U R                   nU R                  5       (       a  [        [        U5         UR	                  5       $ ! [
         a  nSUl        UeS nAf[         a(    [        SSSS[         S3/[        R                  QS9   g [         a{  n[        R                  R                  R!                  U R#                  5       5        SUl        UR&                  (       d&  UR(                  (       d  U=R*                  S-  sl        e S nAff = f)	NTzinfinite generator detected z}Dynamo traced the YIELD_VALUE bytecode too many times. This could mean that we have attempted to trace an infinite generator.zLIf you are sure that your generator is not infinite, please report a bug at .r  zA

Skipping frame due to graph break in a generator's next() call.)r  _is_generator_exhaustedr"   StopIterationinline_call_r   r  r   r$   PT2_ISSUE_TRACKER_URLr   rp  r%   r$  re  
eval_frame	skip_coder   
skip_frame	one_graphr]  r  )r|   r   r'  r  s       r_   next_variable*LocalGeneratorObjectVariable.next_variable  s    ##''))$]B7	 &&((  	)-F&G% 	5I ccxbyyz{&11	  	MM$$..t}}?AL<<(?(?^^	s*   A 
D	A2D
	DA6D		Dr   c                     X R                  5       R                  ;   a  [        R                  " S5      $ [        R                  " S5      $ )NTF)r8  __dict__r@   r   r|   r   r   s      r_   r   -LocalGeneratorObjectVariable.call_obj_hasattr  s;     ##%...#**400&&u--r^   c                     gr  rX   r   s     r_   has_unpack_var_sequence4LocalGeneratorObjectVariable.has_unpack_var_sequence  r  r^   c                     gr  rX   r   s     r_   has_force_unpack_var_sequence:LocalGeneratorObjectVariable.has_force_unpack_var_sequence   r  r^   c                 @    / nU R                  XR                  5        U$ r   )force_apply_to_var_sequenceappend)r|   r   r   s      r_   r  6LocalGeneratorObjectVariable.force_unpack_var_sequence  s      )+((]];r^   r   c                 n      U" U R                  U5      5        M  ! [         a    [        U5         g f = fr   )r>  r!   r   )r|   r   r   s      r_   rK  8LocalGeneratorObjectVariable.force_apply_to_var_sequence
  s@     4%%b)*  - )"-s    44Fc                     gr  rX   r   s    r_   r  =LocalGeneratorObjectVariable.should_allow_nested_graph_breaks  r  r^   excc                     U R                   n UR                  U5        g ! [         a  nUR                  U5         S nAg S nAff = fr   )r  _raise_exception_variabler   exception_handler)r|   r   rR  r'  r  s        r_   _setup_exception-LocalGeneratorObjectVariable._setup_exception  sE     ##	(,,S1  	( $$Q''	(s     
AA  Ac                 ^    U R                   S L =(       d    U R                   R                  S:H  $ )Nr   )r  instruction_pointerr   s    r_   _is_generator_just_started7LocalGeneratorObjectVariable._is_generator_just_started#  s*    !!T)XT-?-?-S-SWX-XXr^   c                 0    [        U R                  SS5      $ )Nr  F)r   r  r   s    r_   r6  4LocalGeneratorObjectVariable._is_generator_exhausted&  s    t))+@%HHr^   c                   > US:X  a  U R                  U5      $ US:X  a  U $ US:X  az  U R                  5       (       a7  [        U5      (       a'  [        S U 5       5      (       d  [	        [
        U5        U R                  nUR                  U5        U R                  U5      $ US:X  a  U R                  nU R                  5       (       d  U R                  5       (       a  SUl	        [        R                  " S 5      $  U R                  U[        R                  " [        S5      5        [        R                   S:  a7  UR"                  R$                  S	:X  a  SUl	        [        R                  " S 5      $  U R                  U5      (       a  [	        [(        U5        OUS
:X  a  U R                  n U R                  U[        U5      S:X  a  US   OUS   5        U R                  U5      n[1        S[2        40 5      n U R                  U[        R                  " US5      5        U R                  U5        [	        [(        U5        U$ [6        TU ]q  XX45      $ ! [&         a     SUl	        [        R                  " S 5      s $ f = f! [&         a     SUl	        [        R                  " S 5      s $ [*         a    UR,                  c   eUR,                  s $ f = f! [.         a    e f = f! [5        U5       a     U$ f = f)N__next____iter__sendc              3   @   #    U  H  oR                  5       v   M     g 7fr   )is_constant_noner  r|  s     r_   r  ;LocalGeneratorObjectVariable.call_method.<locals>.<genexpr><  s     BTc//11T   closeTrX   )r     CALL_INTRINSIC_1throwr  r9   r   __InternalThrowException)r>  rZ  r   r  r"   r   r  	push_manyr6  r  r   r@   rV  ExceptionVariableGeneratorExitr  version_infonext_instructionopnamer    ri  r!   symbolic_resultr   r  rn  r   r#  r  )	r|   r   r   r   r   r'  retvalexc_typer)  s	           r_   r  (LocalGeneratorObjectVariable.call_method)  s    :%%b))ZKV^ ..00SYY BTBBB,Y;''FT"%%b))W_ ''F..00D4P4P4R4R-1* 11$778%%	33M2F( $$///66:LL15F.$55d;;.%%b)),\2> W_ ''F
 %%bSY!^$q'aQ
 ''+F| 6bIH?%%b)*E*EhPR*ST""2&
 )v>Mw"2T::a ) 8-1* 11$778 ) 8-1* 11$77, .
 --999---., % N 1:  MsI   ;A5I 2&J  ,)K 98K" 'I=<I= 'K)$KKK"K54K5)r}   r   r  r   r#  rB   r`   N)+rY   rZ   r[   r\   r   r  r   r  r   r   r   r   r   r   r   r  r>  r  __repr__r"  r   r>   rT  r   r  r8  r>  r@   r   rE  rH  r   r  r   rK  r
   r  rV  rZ  r6  r  r]   r  r  s   @r_   r  r    sE   
+nn
+ S>
+ @	
+
 
+ 

+%.. +c +'# '2e 2$ # ? ? HV$3#3 '3 S/)*	3
 
c?"	#3T#s(^ #T # ;  B.).14.	.*A d 0G D -	o	-3;_<Ms<R3S	'%. 	()	(0?	(		(YD YI Iw;#w; w; ?#	w;
 S/)*w; 
w; w;r^   r  c                       \ rS rSrSrSrg)4ContextlibContextManagerLocalGeneratorObjectVariablei  z
.. note::

    This is only used when the function is annotated with @contextlib.contextmanager

    It is a special case of a generator function as we do not allow return a context manager
    from a torch.compile function.
rX   N)rY   rZ   r[   r\   r   r]   rX   r^   r_   ry  ry    s    r^   ry  c            	          ^  \ rS rSrSr\S.S\S\S\SS4U 4S	 jjjr	S
\
S\4S jrS\\
\4   4S jrSSS\\   S\\
\4   SS4S jrSSS\\   S\\
\4   S\4S jrSrU =r$ )LocalGeneratorFunctionVariablei  zlfunctions that behaves like iterators

.. note::

    This is a wrapper around (Nested)UserFunctionVariable
)generator_clsr*  r|  r   r`   Nc                >   > [         TU ]  " S0 UD6  Xl        X l        g r   )r#  r   r*  r|  )r|   r*  r|  r   r)  s       r_   r   'LocalGeneratorFunctionVariable.__init__  s      	"6"*r^   r   c                 v    XR                   R                  ;   a  [        X5      $ [        U R                  U5      $ r   )r)  rA  r   r*  )r|   r   s     r_   __getattr__*LocalGeneratorFunctionVariable.__getattr__  s.    >>***4&&tww%%r^   c                 6    U R                   R                  5       $ r   )r*  r   r   s    r_   r   *LocalGeneratorFunctionVariable.get_globals  s    ww""$$r^   r   rF   r   rD   c                 8    SSK Jn  UR                  " UU UU5      $ )Nr   )rD   )r  rD   build_inline_tracer)r|   r   r   r   rD   s        r_   _build_inline_tracer3LocalGeneratorFunctionVariable._build_inline_tracer  s'     	Q,@@	
 	
r^   rE   c                    [        U R                  R                  5       5      (       d/  [        S[	        U R                  R                  5       5      SSS/S9  U R                  U[        U5      U5      nU R                  R                  5       nU R                  R                  5       nU R                  UUUU R                  S9$ )Nz'non-generator contextlib.contextmanagerzvCannot compile function decorated with `@contextlib.contextmanager` that is not a generator, i.e. does not use `yield`z5Use `yield` in the function body instead of `return`.z2Remove the `@contextlib.contextmanager` decorator.r  r  )
r   r*  r   r$   r  r  r   r   r|  r   )r|   r   r   r   r  r}   r   s          r_   r   ,LocalGeneratorFunctionVariable.call_function  s     DGG,,.//ADGG,,./. LH	 11"d4j&Iww!GG'')	 !!;;	 " 
 	
r^   )r|  r*  )rY   rZ   r[   r\   r   r  r>   r  r   r   r  r  r   r   r   r  r   r   r]   r  r  s   @r_   r{  r{    s     ;		+	+ 		+
 	+ 
	+ 	+& & &
%T#s(^ %
'
 ?#
 S/)*	

 
)

#
 '
 S/)*	

 

 
r^   r{  c                   l   ^  \ rS rSrSrS\S\SS4U 4S jjrSS	S
\\   S\	\
\4   SS4U 4S jjrSrU =r$ )3FunctionDecoratedByContextlibContextManagerVariablei9  za
.. note::

    This is only used when the function is annotated with @contextlib.contextmanager
r*  r   r`   Nc                 4   > [         TU ]  " U4S[        0UD6  g )Nr|  )r#  r   ry  )r|   r*  r   r)  s      r_   r   <FunctionDecoratedByContextlibContextManagerVariable.__init__B  s%    	
N	
 	
r^   r   rF   r   rC   c                    > [         TU ]  XU5      n[        U[        R                  R
                  R                  5      (       d   eSUl        U$ r  )r#  r  r   r$  re  symbolic_convertrC   is_generator_from_ctx_manager)r|   r   r   r   r'  r)  s        r_   r  HFunctionDecoratedByContextlibContextManagerVariable._build_inline_tracerI  sR     -b?MM**QQ
 
 	
 
 04,r^   rX   )rY   rZ   r[   r\   r   r>   r   r   r   r   r  r  r]   r  r  s   @r_   r  r  9  sa    
? 
c 
d 
' ?# S/)*	
 
2 r^   r  c                      ^  \ rS rSrSr SS\S\4   S\S\S-  S\S	S4
U 4S
 jjjr	S	\
4S jrS	\\   4S jrS	\\R                      4S jrSSS\\   S\\
\4   S	\4U 4S jjrSSS\
S	\4U 4S jjrSrU =r$ )rC  i\  z$Some unsupported user-defined methodNr   .r  rD  r   r`   c                    > [         TU ]  " SSU0UD6  X l        X0l        Uc4  UR	                  S5      b!  [        UR	                  S5      S5      U l        g g g )Nr   r   __func__rX   )r#  r   r  rD  r   r(   )r|   r   r  rD  r   r)  s        r_   r   UserMethodVariable.__init___  sZ     	)B)&)" H!5!A'

8(<jIDN "Br^   c                 h    U R                   R                   SU R                   SU R                   S3$ )Nr  r_  r`  )r)  rY   r   r  r   s    r_   rw  UserMethodVariable.__repr__x  s.    ..))*!DGG9BtxxjBBr^   c                     U R                   /$ r   r  r   s    r_   r   UserMethodVariable.self_args{      zr^   c                 "    [         R                  $ r   )r   
MethodTyper   s    r_   r8  UserMethodVariable.python_type~  s    r^   r   rE   r   c                 $  > SSK Jn  U" U R                  5      (       aE  / U R                  5       QUQn[        R
                  " U R                  SS9nUR                  XU5      $ UR                  R                  5       (       a  [        U R                  [        R                  5      (       a  [        U R                  SS5      nUb  UR                  S5      (       a  US:w  d  U R                  (       aB  U R                  R                  XR                  R                   [#        U5      X0R                  S	9$ Ol[$        be  U R                  [$        R&                  R(                  L a>  [        R*                  " U R                  5      R                  XR                  /UQ7U5      $ U R                  (       aO  [        U R                  R,                  U R                  R                   5      n[/        XU R1                  5       X#5      $ [2        T	U ]  XU5      $ )
Nr   )is_nonstrict_trace_callableTra  rZ   r4  z	torch.nn.ztorch.nn.utils.parametrize)constant)trace_rulesr  r   r   r   rq  r   r   is_root_tracerr   r  NNModuleVariabler   
startswithr  r  rY   r   rA   FSDPParamGroupuse_training_stateTorchCtxManagerClassVariabler  rr  r   r#  )
r|   r   r   r   r  	call_argsvarmodule_attrr   r)  s
            r_   r    UserMethodVariable.call_function  s    	>&tww//2$..*2T2I88TC $$RF;; 99##%%*HHi00+
 +
 "$''<<K '**;77#??##xx++(($t*fGWGW ,   $ ),;;NNN99$''BPPXX%%v  )9)9:B/VVw$Rv66r^   r   c                    > US:X  a  U R                   $ US:X  a+  [        R                  " XR                  U R                  5      $ [
        TU ]  X5      $ )Nr=  r  )r  r>   r   r   rD  r#  rX  r|   r   r   r)  s      r_   rX  UserMethodVariable.var_getattr  sI    :88O: #((WWdnnEEw"2,,r^   )r  rD  r   )rY   rZ   r[   r\   r   r   r   r>   r   r   r  rw  r   r   r  r   r  r8  r   r   r   rX  r]   r  r  s   @r_   rC  rC  \  s    . $(	JS#XJ J D=	J
 J 
J J2C# C40  T%"2"23  ;7#;7 ';7 S/)*	;7
 
;7z-5 -S -_ - -r^   rC  c                   v   ^  \ rS rSrS\SSS\SS4U 4S jjrS	S
S\\   S\	\
\4   S\4U 4S jjrSS jrSrU =r$ )WrappedUserMethodVariablei  wrappedr  rG   r   r`   Nc                    > UR                  SS 5        UR                  SS 5        [        TU ]  " UR                  UR                  40 UD6  Xl        X l        g )Nr   r  )r   r#  r   r   r  r  r  r|   r  r  r   r)  s       r_   r   "WrappedUserMethodVariable.__init__  sG     	

4

5$W[[;F;r^   r   rE   r   c                    > U R                   R                  U5        [        TU ]  XU5      nU R                   R	                  U5        U$ r   r  enterr#  r   exitr|   r   r   r   r   r)  s        r_   r   'WrappedUserMethodVariable.call_function  ?     	2&r8"r^   c                    ^ ^ TR                  UU 4S j5        T" T R                  5        TR                  [        SS5      5        g )Nc                  (   > T " TR                   5      $ r   r  r#  r|   s   r_   <lambda>7WrappedUserMethodVariable.reconstruct.<locals>.<lambda>      gdll&;r^   r9   Fadd_push_nullr  extend_outputr   r|   r#  s   ``r_   r"  %WrappedUserMethodVariable.reconstruct  4    ;<21e<=r^   r  r  rv  )rY   rZ   r[   r\   rC  r   r   r   r>   r   r  r   r"  r]   r  r  s   @r_   r  r    sr    
#
 +
 	

 

	#	 '	 S/)*		
 
	> >r^   r  c                   v   ^  \ rS rSrS\SSS\SS4U 4S jjrS	S
S\\   S\	\
\4   S\4U 4S jjrSS jrSrU =r$ )WrappedUserFunctionVariablei  r  r  rG   r   r`   Nc                 x   > UR                  SS 5        [        TU ]  " UR                  40 UD6  Xl        X l        g )Nr   )r   r#  r   r   r  r  r  s       r_   r   $WrappedUserFunctionVariable.__init__  s3     	

4.v.r^   r   rE   r   c                    > U R                   R                  U5        [        TU ]  XU5      nU R                   R	                  U5        U$ r   r  r  s        r_   r   )WrappedUserFunctionVariable.call_function  r  r^   c                    ^ ^ TR                  UU 4S j5        T" T R                  5        TR                  [        SS5      5        g )Nc                  (   > T " TR                   5      $ r   r  r  s   r_   r  9WrappedUserFunctionVariable.reconstruct.<locals>.<lambda>  r  r^   r9   Fr  r  s   ``r_   r"  'WrappedUserFunctionVariable.reconstruct  r  r^   r  rv  )rY   rZ   r[   r\   r
  r   r   r   r>   r   r  r   r"  r]   r  r  s   @r_   r  r    sr    	%	 +	 		
 
		#	 '	 S/)*		
 
	> >r^   r  c                    S[         S[        4S jnU Vs/ s H
  oe" U5      PM     nnUR                  5        VVs0 s H  u  pxXu" U5      _M     nnnU" U0 UD6n	U R                  R	                  U	U[        U5      S9$ s  snf s  snnf )Nxr`   c                     U R                  5       (       a  [        SU 5      R                  5       $ U R                  5       $ )NrL   )	is_tensorr	   get_real_valuer,  )r  s    r_   convert-invoke_and_store_as_constant.<locals>.convert  s3    ;;==(!,;;==##%%r^   r  )r>   r   r   r   register_attr_or_moduler+   )
r   r   r   r   r   r  r  r   r   ress
             r_   rr  rr    s    &? &s &
 !%%1GAJD%(.7amF7
d
f
C99,,d# -   &7s
   A?Bc                   z  ^  \ rS rSrS1\R
                  kr S&S\S\S\\\	4   S\S-  S\S-  S\S-  S	\S-  S
\S-  S\	SS4U 4S jjjr
S\\   4S jrS\R                  4S jrS\R                   4S jrS\\R                     4S jrS&S\\   S-  S\R                  4S jjrS\\   S\R                  4S jrSSS\S\S\4S jrSSS\S\\   S\\\4   S\4
U 4S jjrS\4S jrSSS\S\	4U 4S jjrSSS\S\4U 4S jjrS\4S  jr S\\\	4   4S! jr!S"SS\\   S\\\4   S\\\4   4S# jr"S'S$ jr#S%r$U =r%$ )(r   i  r   Nr  r}   r   r   r   r   
wrapped_fnr   r`   c	                   > U	R                  S5      c  U	R                  [        5       S9  [        T
U ]  " S0 U	D6  [        UR                  5       [        5      (       d   e[        UR                  5       [        R                  5      (       d   e[        U[        5      (       d   eXl        X l        X0l        X@l        XPl        X`l        Xpl        Xl        g )Nmutation_typer  rX   )r   updater;   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)  s             r_   r   #NestedUserFunctionVariable.__init__   s     ::o&.MM(<(>M?"6"'446<<<<$113U^^DDDD)T****	" $&2<r^   c                     / $ r   rX   r   s    r_   r   $NestedUserFunctionVariable.self_args=  r  r^   c                 "    U R                  5       $ r   )r   r   s    r_   r,  -NestedUserFunctionVariable.as_python_constant@  s      ""r^   c                 6    U R                   R                  5       $ r   )r}   r,  r   s    r_   r   #NestedUserFunctionVariable.get_codeC  s    yy++--r^   c                 "    [         R                  $ r   r7  r   s    r_   r8  &NestedUserFunctionVariable.python_typeF  r:  r^   _convertingc                 &   SSK Jn  [        U 5      nUc
  [        5       nX1;   a  [	        S5      eUR                  U5         U R                  U5      UR                  U5        $ ! U a  n[	        S5      UeS nAff = f! UR                  U5        f = f)Nr9   )r:   z-cycle detected in mutually recursive closuresz1failed to convert closure cell to Python constant)baser:   idsetrV   add_get_function_impldiscard)r|   r  r:   self_idr  s        r_   r   'NestedUserFunctionVariable.get_functionI  s     	>T(%K!(?  	 	)**;7 ( 3 	(C	
 (s$   A# #A:)A55A::A= =Bc                    S nU R                   (       a  SSKJn  UR                  " 5       n/ nU R                   R                   H  nUR
                  R                  R                  U5      nXpL a  [        S5      e[        U[        5      (       a  UR                  U5      nOUR                  5       nUR                  [        U5      5        M     [        U5      n[         R"                  " U R$                  R                  5       U R&                  U R(                  R                  5       S US9n	U R*                  (       a  U R*                  R                  5       U	l        U R.                  (       a  U R.                  R                  5       U	l        U R2                  (       ac  U R2                  R                  5       n
[        U
[        5      (       a  SSKJn  [9        U" U
5      5      n
[        U
[8        5      (       d   eXl        U	$ )Nr   )rE   z self-referential nested function)argdefsr   r   )r   r  rE   
current_txr   r   r   	load_cellrV   r   r   r   r,  rL  r8   r   r   r   r}   r   r  r   r   r   r   r   r   r   r   r   )r|   r  closure_cellsrE   r   cellsrP  rH  r  ri   r   r   s               r_   r  -NestedUserFunctionVariable._get_function_impl_  s   <<L&113BE LL.. !#		 6 6 @ @ J !(01STT m-GHH)66{CE)<<>EYu-.- /. "%LM!!II((*NNLL++-!
 == $ @ @ BD??"&//"D"D"FD**==?K+u--."8K#89 k40000#. r^   r   rE   name_varr   c                 x    UR                   R                  R                  XR                  U5        [	        S 5      $ r   )r   r   
store_attrr  r@   )r|   r   r  r   s       r_   call_setattr'NestedUserFunctionVariable.call_setattr  s-     			))$D%%r^   r   r   c                 j   > US:X  a  U R                   " U/UQ76 $ [        TU ]	  X[        U5      U5      $ )N__setattr__)r  r#  r  r   r|   r   r   r   r   r)  s        r_   r  &NestedUserFunctionVariable.call_method  s;     = $$R/$//w"2T$Z@@r^   c                     U R                   S L$ r   )r   r   s    r_   has_closure&NestedUserFunctionVariable.has_closure  s    ||4''r^   c                    > US:X  a  U R                  5       $ US:X  a  U R                  5       $ US:X  a&  [        U SS 5      nU(       a  UR                  5       $ S $ [        TU ]  X5      $ )NrY   rk   r   r   )r   r   r   r,  r#  const_getattr)r|   r   r   dr)  s       r_   r  (NestedUserFunctionVariable.const_getattr  sg    :==?":==?">!j$/A-.1'')8D8w$R..r^   c                    > US:X  a)  [         R                  R                  [        U S5      5      $ US:X  a)  [         R                  R                  [        U S5      5      $ [        TU ]  X5      $ )Nrk   r}   r   r   )r   r@   r   r   r#  r   r  s      r_   r   +NestedUserFunctionVariable.call_obj_hasattr  sa     :--44WT65JKK>!--44WT:5NOOw'11r^   c                     gr  rX   r   s    r_   r>  #NestedUserFunctionVariable.has_self  r  r^   c                     U R                   $ r   r-  r   s    r_   r   &NestedUserFunctionVariable.get_globals  r/  r^   r   c                 `   U R                  5       n[        R                  " UU R                  U R                  R                  5       U R                  (       a  [        U R                  R                  5      OS [        S [        [        U R                  5       R                  5      5       5       5      5      nU R                  (       a  U R                  R                  5       Ul        [        R                   " U5      R"                  " U0 UD6nUR%                  5         ['        UR(                  R                  5       5      n[+        UR,                  R.                  U5        [1        XU5        [3        UR                  5       H)  u  pX;  d   eU R4                  R                  U   n
XU	'   M+     U$ )Nc              3   8   #    U  H  n[        S 5      v   M     g 7fr   )r8   )r  _s     r_   r  7NestedUserFunctionVariable.bind_args.<locals>.<genexpr>  s     S+Ra)D//+Rs   )r   r   r   r   r  r,  r   r   r   r   r   rJ  r   keys_as_python_constantr   r   rj  rk  apply_defaultsr   rl  r   r   rI  r   r   r   )r|   r   r   r   r}   ri   rN   r   r   r   rO  s              r_   rT  $NestedUserFunctionVariable.bind_args  s;    }}!!NNLL++-*.--E$--%%&TS5T]]_5P5P1Q+RSS
 ??"&//"I"I"KD!!$',,d=f=eoo++-...7fd+"4#3#34IC%%%<<%%c*D4L 5
 r^   c                   ^ TR                  U4S j5        T" U R                  5        TR                  TR                  U R                  5      /5        T" [
        R                  " U R                  R                  R                  5      5        U R                  (       a  T" U R                  5        O!TR                  TR                  S 5      /5        U R                  (       a  T" U R                  5        O!TR                  TR                  S 5      /5        U R                  (       a  T" U R                  5        O!TR                  TR                  S 5      /5        U R                  (       a=   U R                  R                  5       nTR                  TR                  U5      /5        O!TR                  TR                  S 5      /5        TR                  [!        SS5      5        U R"                  (       aw  TR                  U4S j5        T" U R"                  5        TR                  [!        SS5      5        TR                  [%        S5      5        TR                  [!        SS5      5        TR&                  nUR(                  R*                  R-                  U 5      (       a~  UR(                  R*                  R.                  U    R1                  5        HH  u  pETR3                  5         T" U5        TR                  [%        S5      5        TR5                  U5        MJ     g g ! [         a    T" U R                  5         GNwf = f)Nc                  0   > T R                  [        S5      $ )Nr   )load_import_fromrY   r#  s   r_   r  8NestedUserFunctionVariable.reconstruct.<locals>.<lambda>  s    G,,X7JKr^      Fc                  (   > T R                  SS5      $ )N	functoolswrapsr  r  s   r_   r  r  
  s    00gFr^   r9   r   T)r  r}   r  create_load_const_uncheckedr   r@   r   r  r   r   create_load_constr   r   r   r,  r   r   r  r   r   r   r   has_pending_mutationstore_attr_mutationsr   dup_topr   )r|   r#  r   r   r   r  s    `    r_   r"  &NestedUserFunctionVariable.reconstruct  sw   K	
 			wBB4>>RST ''		(?(?@A==DMM"!!7#<#<T#B"CD<<DLL!!!7#<#<T#B"CD??DOO$!!7#<#<T#B"CD*"..AAC%%88EF !!7#<#<T#B"CD21e<=??!!F DOO$!!"6q%"@A!!,q/2!!"6q$"?@ ZZ99!!66t<<!yy55JJ eg  !%%l1o6""4(  =% ' *(()*s   /;L$ $MM)r   r   r}   r   r   r  r   r  r   rv  )&rY   rZ   r[   r\   r   r  r>   r   r  r   r   r   r   r   r   r,  r  r   r  r8  r  r  r   r  r  r   r  r  r  r  r@   r   r>  r   rT  r"  r]   r  r  s   @r_   r   r     s]   	!	0	0N  .2= = = S>	=
 "D(= $d*= %t+= !4'= $d*= = 
= =:40 #E$6$6 #.%.. ."T%"4"45 ")C4 )5CUCU ),6c#h 65;M;M 6p&#& "& 	&
 
&	A#	A 	A '		A
 S/)*	A 
	A(T (/ 7 /s /s /2)2142	2$ T#s(^ ' ' S/)*	
 
c?"	#:6) 6)r^   r   c                   v   ^  \ rS rSrS\SSS\SS4U 4S jjrS	S
S\\   S\\	\4   S\4U 4S jjr
SS jrSrU =r$ )!WrappedNestedUserFunctionVariablei  r  r  rG   r   r`   Nc           
      
  > UR                  SS 5        UR                  SS 5        UR                  SS 5        UR                  SS 5        UR                  SS 5        UR                  SS 5        UR                  SS 5        UR                  SS 5        [        TU ]	  UR                  UR                  UR
                  UR                  UR                  UR                  UR                  UR                  5        Xl        X l        g )	Nr  r}   r   r   r   r   r   r  )r   r#  r   r  r}   r   r   r   r   r   r  r  r  r  s       r_   r   *WrappedNestedUserFunctionVariable.__init__  s     	

9d#

64 

;%

:t$

<&

=$'

9d#

<&OOLLOO		
 r^   r   rE   r   c                    > U R                   R                  U5        [        TU ]  XU5      nU R                   R	                  U5        U$ r   r  r  s        r_   r   /WrappedNestedUserFunctionVariable.call_function9  r  r^   c                    ^ ^ TR                  UU 4S j5        T" T R                  5        TR                  [        SS5      5        g )Nc                  (   > T " TR                   5      $ r   r  r  s   r_   r  ?WrappedNestedUserFunctionVariable.reconstruct.<locals>.<lambda>E  r  r^   r9   Fr  r  s   ``r_   r"  -WrappedNestedUserFunctionVariable.reconstructD  r  r^   r  rv  )rY   rZ   r[   r\   r   r   r   r>   r   r  r   r"  r]   r  r  s   @r_   r,  r,    sr     + 	
 
6	#	 '	 S/)*		
 
	> >r^   r,  c            	         ^  \ rS rSrSS1\R
                  krSS\S\S-  S\SS4U 4S jjjrS\4S jr	\
S\S	\SS 4S
 j5       rSSS\\   S\\\4   S\4S jrSSS\S\4S jrSSS\S\4S jrS\4S jrS\4S jrS\S\4S jrSrU =r$ )SkipFunctionVariableiJ  r  reasonNr   r`   c                 >   > [         TU ]  " S0 UD6  Xl        X l        g r   )r#  r   r  r7  )r|   r  r7  r   r)  s       r_   r   SkipFunctionVariable.__init__Q  s    "6"
r^   c                     U R                   $ r   r  r   s    r_   r,  'SkipFunctionVariable.as_python_constantV  s    zzr^   r   c                    [         R                  " USS5      (       ab  UnUn[        USS5      (       a,  UR                  n[	        US5      n[        USS5      (       a  M,  UR                  [        R                  5        O|[         R                  " U5      (       a)  [        UR                  [        R                  5      5        O8[        U5      (       d(  [        UR                  [        R                  5      5        U " XS9$ )N_torchdynamo_disableF_torchdynamo_orig_callabler  )r   r   r   r?  r(   r  r&   r  	isbuiltinr'   BUILTIN_MATCHr6   )r  r  r   guard_on_sourceguard_on_values        r_   r  'SkipFunctionVariable.create_with_sourceY  s     !!%)?GG %O"N.*FNN!/!J!J",#%A# .*FNN &&|'A'ABu%%&++L,F,FGH077 &++L,F,FGH5((r^   r   rE   r   c                 p   [         R                  " U R                  SS5      (       aT  [         R                  " U R                  SS 5      n[        S[	        U R                  5      SU R                   SU S3S/S	9  g U R                  [
        R                  R                  L a@  UR                  S
5      nU(       a  UR                  5       n[        SSU SU S3SU 3S/S	9  g U R                  [
        R                  R                  L aZ  UR                  S
5      nU(       a  UR                  5       nOSn[        SSU SU S[        UR                  5       S3SU 3S/SS9  g U R                  [
        R                  R                  L a   [        SSSS/S	9  g ["        R$                  (       aH  SSKJn  UR+                  XR                  5      n	[-        U	[.        5      (       d  U	R1                  XU5      $ [3        U R                  SS 5      n
[3        U R                  S!S 5      nUc  S"O
[	        U5      n [         R4                  " U R                  5      nS#U
 S$U S%3nS&U
 S'3/nS(U;  a  US)U
 S*3S+/-  nU
S9:X  a  S:n/ nU R@                  (       a  U R@                  OS;n[        S<S=U S>U
 S?U 3UUS	9  g ! [         a  n[        UR                   5      S eS nAff = f! [6         a    S,S-1nUU;   a  S.U SU
 S'3nS/S0/n NUbb  UR9                  S15      (       aL  S2U SU
 S3nS3/n[
        R                  R:                  R=                  US4-   S4R?                  U5      -   5         NS5U SU
 S63nS7S8/n[
        R                  R:                  R=                  US4-   S4R?                  U5      -   5         GN=f = f)@Nr>  F_torchdynamo_disable_msgz1Skip calling `torch.compiler.disable()`d functionzSkip calling function `z>` since it was wrapped with `torch.compiler.disable` (reason: r`  z(Remove the `torch.compiler.disable` callr  r  z%Call to `torch._dynamo.graph_break()`z0Called `torch._dynamo.graph_break()` with args `z`, kwargs ``z$User-inserted graph break. Message: z.Remove the `torch._dynamo.graph_break()` call.r4  z$Call to `torch._dynamo.skip_frame()`z/Called `torch._dynamo.skip_frame()` with args `z`. Skipping frame r5  z#User-inserted skip frame. Message: z-Remove the `torch._dynamo.skip_frame()` call.T)r  r  r   r!  r<  z*Call to `torch._dynamo.step_unsupported()`zUser-inserted step_unsupported.z3Remove the `torch._dynamo.step_unsupported()` call.r9   SourcelessBuilderr[   z<unknown qualname>rZ   z<unknown module>z?Dynamo developers have intentionally marked that the function `z` in file `z` should not be traced.zAvoid calling the function `z`.re  z:Apply `@torch._dynamo.dont_skip_tracing` to the function `zw` to force tracing into the function. More graph breaks may occur as a result of attempting to trace into the function.z Please file an issue to PyTorch._abc	_warningsz6Dynamo does not know how to trace the Python builtin `zIf you are attempting to call a logging function (e.g. `_warnings.warn`), you can try adding it to `torch._dynamo.config.reorderable_logging_functions`.zKPlease file an issue on GitHub so the PyTorch team can add support for it. r  z*Dynamo cannot trace optree C/C++ function zi Consider using torch.utils._pytree - https://github.com/pytorch/pytorch/blob/main/torch/utils/_pytree.py
z/Dynamo does not know how to trace the builtin `z.` This function is either a Python builtin (e.g. _warnings.warn) or a third-party C/C++ Python extension (perhaps created with pybind).zIf it is a Python builtin, please file an issue on GitHub so the PyTorch team can add support for it and see the next case for a workaround.a  If it is a third-party C/C++ Python extension, please either wrap it into a PyTorch-understood custom operator (see https://pytorch.org/tutorials/advanced/custom_ops_landing_page.html for more details) or, if it is traceable, use `torch.compiler.allow_in_graph`.allow_in_graphzFound an allow_in_graph decorator to a function which is created inside the parent function that is getting compiled. This is not supported for now.z<missing reason>z,Attempted to call function marked as skippedzmodule: z, qualname: z, skip reason: )!r   r   r  r$   r  r$  re  graph_breakr   r,  r<  r   f_codestep_unsupportedr%   r#   r  r   dont_skip_tracingbuilderrI  r   r   r6  r   r   getfiler   r  rb   	warn_oncejoinr7  )r|   r   r   r   r  graph_break_msgskip_frame_msgr  rI  
rebuilt_fnqualname	module_orr  pathr   r!  known_python_builtin_modulesr7  s                     r_   r   "SkipFunctionVariable.call_functiont  sB    !!$**.DeLL((5OQUVCKDJJ5djj\ B::=aA ? ZZ5==444$jj/O"1"D"D"F?J4&P[\b[ccdeB?BSTD	 ZZ5==333#ZZ.N!/!B!B!D!#>I${[aZb c""3BII">!?qBA.AQRC  	 ZZ5==999
7H AM	 ''6 /55b**E
!*.BCC%33BfEEtzz>;OPH

L$?I090A,s9~K8Ytzz2UV^U_ `  $v%<> 
 38*B? D(TU]T^ _l l ;	 EX ++? 
 $(KKT[[5GFF";-|H:_U[T\]'	_  7%aee,$67B  %Y06/D, <<'=(27  
iGE *y/C/CH/M/M$N{m[\]e\ffg"hK^E
 MM''11+2DtyyQVGW2WX J+VWX`Wa ba b  m;E MM''11+2DtyyQVGW2WXK%Ys9   J6 6AK 6
K KK!N5A#N5&AN54N5r   c                 h    [         R                  R                  [        U R                  U5      5      $ r   )r   r@   r   r   r  rB  s      r_   r   %SkipFunctionVariable.call_obj_hasattr  s&     ))00T1JKKr^   c                     U[         ;   a  [        R                  " X5      $ [        XR                  U R
                  U5      $ r   )r3   r   rW  r   r  r   rB  s      r_   rX   SkipFunctionVariable.var_getattr  s3    )),,T88b**dkk4@@r^   c                     gr  rX   r   s    r_   r  'SkipFunctionVariable.is_python_hashable  r  r^   c                 ,    [        U R                  5      $ r   )r  r  r   s    r_   r  $SkipFunctionVariable.get_python_hash  s    DJJr^   r  c                 r    [        U[        5      =(       a!    U R                  5       UR                  5       :H  $ r   )r   r>   r,  r  s     r_   r  $SkipFunctionVariable.is_python_equal  s1    uo. H'')U-E-E-GG	
r^   )r7  r  r   )rY   rZ   r[   r\   r>   r  r   r  r   r,  r  r   r  r   r   r   r@   r   rX  r  r  r  r  r  r  r]   r  r  s   @r_   r6  r6  J  s1    
	'	'Nc 3:  PT  
C  )s )F )?U ) )4K#K 'K S/)*	K
 
KZL)L14L	L
A5 AS A_ AD    
V 
 
 
r^   r6  c                   v   ^  \ rS rSrS\SSS\SS4U 4S jjrS	S
S\\   S\\	\4   S\4U 4S jjr
SS jrSrU =r$ )WrappedSkipFunctionVariablei  r  r  rG   r   r`   Nc                    > UR                  SS 5        UR                  SS 5        [        TU ]  " UR                  4SUR                  0UD6  Xl        X l        g )Nr  r7  )r   r#  r   r  r7  r  r  r  s       r_   r   $WrappedSkipFunctionVariable.__init__  sJ     	

7D!

8T"Hw~~HHr^   r   rE   r   c                    > U R                   R                  U5        [        TU ]  XU5      nU R                   R	                  U5        U$ r   r  r  s        r_   r   )WrappedSkipFunctionVariable.call_function&  r  r^   c                    ^ ^ TR                  UU 4S j5        T" T R                  5        TR                  [        SS5      5        g )Nc                  (   > T " TR                   5      $ r   r  r  s   r_   r  9WrappedSkipFunctionVariable.reconstruct.<locals>.<lambda>2  r  r^   r9   Fr  r  s   ``r_   r"  'WrappedSkipFunctionVariable.reconstruct1  r  r^   r  rv  )rY   rZ   r[   r\   r>   r   r   r   r   r  r   r"  r]   r  r  s   @r_   ri  ri    sr    
 
 +
 	

 

	#	 '	 S/)*		
 
	> >r^   ri  c                      ^  \ rS rSrSrS\S\S\SS4U 4S jjrS	S
S\S\4U 4S jjr	S\
\   4S jrS	S
S\\   S\\\4   S\4S jrSrU =r$ )WrapperUserFunctionVariablei7  a  
Used to represent a wrapper object that contains the actual callable as an
attribute. For example, torch.jit.script/trace have the original function at
their _torchdynamo_inline attribute. Similarly, functions with
__script_if_tracing_wrapper have the original attr at "__original_fn".
wrapper_objattr_to_tracer   r`   Nc                 >   > [         TU ]  " S0 UD6  Xl        X l        g r   )r#  r   rt  ru  )r|   rt  ru  r   r)  s       r_   r   $WrapperUserFunctionVariable.__init__?  s    "6"&*r^   r   rE   r   c                    > X R                   :X  a`  [        U R                  U R                   5      nU R                  =(       a    [	        U R                  U5      n[
        R                  " XU5      $ [        TU ]!  X5      $ r   )	ru  r   rt  r   r(   r>   r   r#  rX  )r|   r   r   r   r   r)  s        r_   rX  'WrapperUserFunctionVariable.var_getattrD  sc    %%%$**D,>,>?C[[BZT%BF"((&99w"2,,r^   c                     / $ r   rX   r   s    r_   r   %WrapperUserFunctionVariable.self_argsL  r  r^   r   c           
         [        U R                  S5      (       Ga  [        U R                  U R                  S 5      n[        USS5      =(       d    SnUR	                  SSS9S   S:w  Ga  UR                  5       n[        R                  R                  UR                  5      nUR                  nS	U S
U SSR                  [        R                  " U/5      5       3n	[        R                  R                   R#                  U	5        [        R                  R                   R$                  R'                  S5      n
U
R)                  [$        R*                  5      (       a  [        R,                  R.                  R1                  5       n[3        5       U-   nUS   R                  US   R                  4nSR                  [        R                  " U5      5      nSUS    S
US    S3nU[5        U5      -  nU
R7                  U5        U R9                  5       [;        U5      -   n[<        R>                  " [@        RB                  5      RE                  UU [<        RF                  " U R                  5      /UQU5      $ )N
cache_inforZ   r4  r5  r9   )maxsplitr   r$  zGDynamo detected a call to a `functools.lru_cache`-wrapped function at ':z'. Dynamo ignores the cache wrapper and directly traces the wrapped function. Silent incorrectness is only a *potential* risk, not something we have observed. Enable TORCH_LOGS=+dynamo for a DEBUG stack trace.

This call originates from:
ztorch._dynamoz)call to a lru_cache wrapped function at: rL  )$r   rt  r   ru  splitframe_summaryosr[  basenamefilenamelinenorU  	tracebackformat_listr$  re  rb   rT  logging	getLoggerisEnabledForDEBUG_guardsTracingContextextract_stackr   r  debugr   r   r   r
  r   getattr_and_tracer   r@   )r|   r   r   r   	target_fnr  r  r  r  r  dynamo_logger
user_stack	frame_locuser_stack_formatteduser_stack_traceall_argss                   r_   r   )WrapperUserFunctionVariable.call_functionO  s$    4##\22 0 0$2D2DdKI!)\2>D"K  q 1!4? " 0 0 277++M,B,BC&--$$,:Qvh 73 wwy44m_EFGI  ##--c2 % 3 3 ; ; E Eo V --gmm<<!&!=!=!K!K!MJ!7!9J!FJ!+B!8!8*R.:O:O PI+-7793H3H3T+U()RS\]^S_R``abklmbnaooq'r$$,@(AA$!''(89>>#d4j0--''

-9--d.@.@AMHM
	
r^   )ru  rt  )rY   rZ   r[   r\   r   r   r  r   r>   rX  r   r   r   r   r   r]   r  r  s   @r_   rs  rs  7  s    +C + +s +t +
-5 -S -_ -40 ,
#,
 ',
 S/)*	,

 
,
 ,
r^   rs  c            
       X   ^  \ rS rSrSrS\S\S\S\SS4
U 4S	 jjrS\	\   4S
 jr
SrU =r$ )WrapperUserMethodVariablei~  z
Similar to WrapperUserFunctionVariable, but for methods. The only delta is
saving the vt for `self` object of the method which is then used by
WrapperUserFunctionVariable in `call_function` method.
rt  ru  self_objr   r`   Nc                 4   > [         TU ]  " X40 UD6  X0l        g r   )r#  r   r  )r|   rt  ru  r  r   r)  s        r_   r   "WrapperUserMethodVariable.__init__  s     	>v>r^   c                     U R                   /$ r   r  r   s    r_   r   #WrapperUserMethodVariable.self_args  r  r^   r  )rY   rZ   r[   r\   r   r   r  r>   r   r   r   r]   r  r  s   @r_   r  r  ~  sS      "	
  
40  r^   r  c                  \    [         R                  R                  5       (       a  SSKJn   U $ 0 $ )Nr   traceable_collective_remaps)r$  distributedis_available)torch.distributed._functional_collectivesr  r  s    r_   _traceable_collective_remapsr    s)    %%''	
 +*Ir^   c                     [         R                  R                  5       (       d   S5       eU[        5       R	                  5       ;   d   eUR
                  nU R                  S5      n[        X25      $ )NzIllegal invocation.z)torch.distributed._functional_collectives)r$  r  r  r  valuesrY   import_sourcer(   )r   r   
inner_namepath_sources       r_   _traceable_collectives_sourcer    se     ))++B-BB+-/668888J""#NOKk..r^   c                      ^  \ rS rSrSrS\S\4   S\S\SS4U 4S	 jjr\	S
SS\S\4   S\
S\SS 4
S j5       r\	S\S\4S j5       r\	S
SS\S\4   S\\\4   4S j5       rS
SS\\   S\\\4   S\4S jrSrU =r$ )!CollectiveFunctionRewriteVariablei  a  
Some of the torch.distributed.* collective APIs are possible to rewrite to 'traceable' collectives.

This class provides both a way to check if a function is remappable, and perform the remapping.

In the case that a function is 'remappable' but only for some combinations of call-time arguments,
we check the args at `call_function` time and fall back to graph-breaking if needed.  This is no worse
than status-quo as we currently graph-break on all distributed.* collectives.
r   .replacement_varr   r`   Nc                b   > [         TU ]  " U40 UD6  [        U[        5      (       d   eX l        g r   )r#  r   r   r
  r  )r|   r   r  r   r)  s       r_   r   *CollectiveFunctionRewriteVariable.__init__  s1     	&v&/+?@@@@.r^   r   rE   old_fnr   optionsc                 f    [         R                  X5      u  pE[        U4[        U4SU0UD6US.UD6$ )Nr   )r  r   )r  r  r
  )r   r  r   r  new_fn
new_sources         r_   r   (CollectiveFunctionRewriteVariable.create  sK     ?FFrR0
0V
VgV
 	
 	
r^   variablec                 V    [         R                  " U 5      =(       a    U [        5       ;   $ r   )r   
isfunctionr  )r  s    r_   can_rewrite-CollectiveFunctionRewriteVariable.can_rewrite  s%     x(WX9U9W-W	
r^   c                 6    [        5       U   nU[        X5      4$ r   )r  r  )r   r   r  s      r_   r  )CollectiveFunctionRewriteVariable.rewrite  s"     ./34R@@@r^   r   c                    SS K Jn  SSKJn  [        R
                  " U R                  5      n[        UR                  " U0 UD6R                  5      nSnSU;   aU  US   R                  5       (       a=  [        SU R                   SU< SU< 3SU R                   3/ [        R                  QS	9  U R                  UR                  UR                  UR                   4;   ap  UR#                  S
5      nUb  UR$                  OUR&                  S
   R(                  nX;  a  [+        SU 35      e[,        R.                  R1                  XX   5      US
'   U R2                  R5                  XU5      $ )Nr   )REDUCE_OP_TO_STRrX   async_opz)async_op=True for distributed collectivesz, args=	, kwargs=z3`torch.compile` doesn't support `async_op=True for r  opzUnsupported all_reduce op: )torch.distributedr  r  r  r   rj  r   r   rk  rl  r,  r$   r   SUPPORTABLE
all_reducereduce_scatter_tensor_reduce_scatter_baser   r  
parametersdefaultrL  r   r@   r   r  r   )	r|   r   r   r   distr  rj  reduce_op_var	reduce_ops	            r_   r   /CollectiveFunctionRewriteVariable.call_function  sd    	)N %%dgg.	innd5f5??@F:$6$I$I$K$KC77)8dWJvi8QRVRYRYQZ[&22	 77OO&&%%	
 
 #JJt,M !, ##))$/77 
 0 #>yk!JKK$55<< +F4L ##11"FCCr^   )r  )rY   rZ   r[   r\   r   r   r   r
  r   r   r   r   r  r  r   r(   r  r   r>   r   r  r   r]   r  r  s   @r_   r  r    s1   	/S#X	/ .		/
 	/ 
	/ 
#
c"
 
 	

 
-
 
 
c 
d 
 

 A#A)1#s();A	sJ	A A-D#-D '-D S/)*	-D
 
-D -Dr^   r  c                   L   ^  \ rS rSrSSS\\   S\\\4   S\4U 4S jjrSr	U =r
$ )	FunctoolsWrapsVariablei	  r   rE   r   r   r`   c                    >^ U(       d9  [        T5      S:X  a*  S[        S[        4U4S jjn[        R                  " U5      $ [
        TU ]  UTU5      $ )Nr9   r   r`   c                    > [        U [        R                  5      (       a  U R                  TS   S9$ [	        SU  S/ [
        R                  QS9  g )Nr   )r  zfunctools.wrapsz]`torch.compile` can't trace `functools.wraps` on functions defined outside the compile regionr  )r   r   r   cloner$   r   r  )r   r   s    r_   r#  3FunctoolsWrapsVariable.call_function.<locals>.wraps	  sQ    b)"F"FGG88tAw877-!d *66	r^   )r   r   r>   r   LambdaVariabler#  r   )r|   r   r   r   r#  r)  s     `  r_   r   $FunctoolsWrapsVariable.call_function	  sL     #d)q.
# 
/ 
 ++E22w$Rv66r^   rX   )rY   rZ   r[   r\   r   r>   r   r  r   r]   r  r  s   @r_   r  r  	  sB    7#7 '7 S/)*	7
 
7 7r^   r  c                   L    \ rS rSrS\4S jrSSS\\   S\\	\4   S\4S jr
S	rg
)CollectionsNamedTupleFunctioni%	  r`   c                     U R                   $ r   r1  r   s    r_   r,  0CollectionsNamedTupleFunction.as_python_constant&	  r3  r^   r   rE   r   r   c                 0   [        X#5      nU(       a   U R                  " U Vs/ s H  oUR                  5       PM     sn0 UR                  5        VVs0 s H  u  pgXgR                  5       _M     snnD6n[        R                  " W[        5       S9$ [        SSU< SU< 3S/ [         R"                  QS9  g s  snf s  snnf ! [         aI  n	[        [        U	5      U[        [        [        R                  U	R                  5      5      S9   S n	A	NS n	A	ff = f)Nr   r  znamedtuple constructionzargs=r  z?`torch.compile` only support certain input types for namedtupler  )r1   r   r,  r   r   r"   r  r   mapr@   r   r   r   UserDefinedClassVariabler=   r$   r   r  )
r|   r   r   r   constant_argsr  r   r   r  rR  s
             r_   r   +CollectionsNamedTupleFunction.call_function)	  s     ,D9
6:;d**,d;=C\\^L^TQq..00^L 55.0 
 	-tgZy)Y"..		
 <L (Ic"2"9"9388DEs3   C B7C B</C 7C 
D?DDrX   N)rY   rZ   r[   r\   r   r,  r   r>   r   r  r   r]   rX   r^   r_   r  r  %	  sH    C 
#
 '
 S/)*	

 

r^   r  c                   >  ^  \ rS rSrS1\R
                  kr SS\S\\   S\\\4   S\	S\	SS4U 4S	 jjjr
S\4S
 jrSS jrS\	4S jrSSS\\   S\\\4   S\4S jrSSS\S\4S jrSSS\S\4S jrS\	4S jrS\	4S jrS\4S jrS\4S jrS\S\4S jrSrU =r$ )FunctoolsPartialVariableiK	  original_cache_hashNri   r   keywordsr   r`   c                    > [         TU ]  " S0 UD6  Xl        [        U[        5      (       d   eX l        [        U[        5      (       d   eX0l        [        R                  " [        5      U l        X@l        g r   )r#  r   ri   r   r   r   r   r  r"  partialr4   
fake_valuer  )r|   ri   r   r  r  r   r)  s         r_   r   !FunctoolsPartialVariable.__init__Q	  sd     	"6"	$%%%%	(D))))  $++H5#6 r^   c                 "    [         R                  $ r   )r"  r  r   s    r_   r8  $FunctoolsPartialVariable.python_typee	  s       r^   c                 R  ^ TR                  U4S j5        T" U R                  5        U R                  (       a  TR                  U R                  5        U R                  (       d2  TR                  [        [        U R                  5      S-   S5      5        g TR                  U R                  R                  5       5        [        U R                  R                  5       5      nTR                  TR                  [        U5      [        U R                  5      -   S-   US5      5        g )Nc                  (   > T R                  SS5      $ )Nr"  r  r$  r  s   r_   r  6FunctoolsPartialVariable.reconstruct.<locals>.<lambda>i	  s    g&>&>{I&Vr^   r9   F)r  ri   r   foreachr  r  r   r   r  r   keyscreate_call_function_kw)r|   r#  r  s    ` r_   r"  $FunctoolsPartialVariable.reconstructh	  s    VW		99OODII&}}!!"6s499~7I5"QR,,./T]]'')*++CIDII,F,JDRWX	
r^   c                 "    U R                  5       $ r   r,  r   s    r_   r   %FunctoolsPartialVariable.get_functionw	      &&((r^   r   rE   c                     U R                   [        U5      -   n0 U R                  EUEnU R                  R	                  XU5      $ r   )r   r   r  ri   r   )r|   r   r   r   merged_argsmerged_kwargss         r_   r   &FunctoolsPartialVariable.call_functionz	  s@     ii$t*,34==3F3yy&&rFFr^   r   c                     [         R                  R                  [        [        R
                  " [        5      U5      5      $ r   )r   r@   r   r   r"  r  r4   rB  s      r_   r   )FunctoolsPartialVariable.call_obj_hasattr	  s2     ))00I%%h/6
 	
r^   c                    U R                   =(       a    [        U R                   U5      nUS:X  a  U R                  $ US:X  a  [        R                  " U R
                  US9$ US:X  aW  U R                  R                  5        VVs0 s H  u  pE[        R                  " U5      U_M     nnn[        R                  " XcS9$ U[        ;   a  [        R                  " X5      $ [        [        U5        g s  snnf )Nri   r   r  r  )r   r(   ri   r   rK   r   r  r   r@   r   ConstDictVariabler3   rW  r"   r   )r|   r   r   r   r   r   r   s          r_   rX  $FunctoolsPartialVariable.var_getattr	  s    >DKK!>6>996>))$))FCC:?C}}?R?R?TU?Ttq%,,Q/2?TEU..uDD)),,T88 4	 Vs   #C/c                 B   [         R                  " U R                  R                  5       /U R                   Vs/ s H  oR                  5       PM     snQ70 U R
                  R                  5        VVs0 s H  u  p#X#R                  5       _M     snnD6$ s  snf s  snnf r   )r"  r  ri   r,  r   r  r   )r|   r|  r   r   s       r_   r,  +FunctoolsPartialVariable.as_python_constant	  s      II((*
26))<)3$$&)<
 6:]]5H5H5JK5JTQq&&((5JK
 	
<Ks   B
4Bc                    [         R                  " U R                  R                  5       /U R                   Vs/ s H  oR                  5       PM     snQ70 U R
                  R                  5        VVs0 s H  u  p!X!R                  5       _M     snnD6nU R                  b  U R                  Ul        U$ s  snf s  snnf )zcSimilar to as_python_constant(), but add ID_MATCH guards to try to force things to become constants)	r"  r  ri   guard_as_python_constantr   r  r   r  
cache_hash)r|   r   r   r   s       r_   r  1FunctoolsPartialVariable.guard_as_python_constant	  s    ""II..0
48II>Iq((*I>
 <@==;N;N;PQ;P41q,,..;PQ
 ##/ $ 8 8F ?Qs   B6
4B;c                     U R                   R                  5       =(       aM    [        S U R                   5       5      =(       a*    [        S U R                  R                  5        5       5      $ )Nc              3   @   #    U  H  oR                  5       v   M     g 7fr   r  rd  s     r_   r  >FunctoolsPartialVariable.is_python_hashable.<locals>.<genexpr>	  s     B	**,,	rf  c              3   @   #    U  H  oR                  5       v   M     g 7fr   r  r  r  s     r_   r  r  	  s     S<R5,,..<Rrf  )ri   r  r  r   r  r  r   s    r_   r  +FunctoolsPartialVariable.is_python_hashable	  sP    II((* TB		BBTSDMM<P<P<RSS	
r^   c                     U R                   R                  5       nS U R                   5       nS U R                  R	                  5        5       n[        U/UQUQ75      $ )Nc              3   @   #    U  H  oR                  5       v   M     g 7fr   r  rd  s     r_   r  ;FunctoolsPartialVariable.get_python_hash.<locals>.<genexpr>	  s     @is((**irf  c              3   @   #    U  H  oR                  5       v   M     g 7fr   r  r
  s     r_   r  r  	  s     S<R5,,..<Rrf  )ri   r  r   r  r  r  )r|   	func_hash	args_hashvalues_hashs       r_   r  (FunctoolsPartialVariable.get_python_hash	  sQ    II--/	@dii@	SDMM<P<P<RSY99[9::r^   r  c                    [        U[        5      =(       a    U R                  R                  UR                  5      =(       a    [	        S [        U R                  UR                  5       5       5      =(       aL    [	        S [        U R                  R                  5       UR                  R                  5       5       5       5      $ )Nc              3   H   #    U  H  u  pUR                  U5      v   M     g 7fr   r  )r  arg_aarg_bs      r_   r  ;FunctoolsPartialVariable.is_python_equal.<locals>.<genexpr>	  s'      &@NU %%e,,&@    "c              3   H   #    U  H  u  pUR                  U5      v   M     g 7fr   r  )r  value_avalue_bs      r_   r  r  	  s,      +&W ''00+r  )	r   r  ri   r  r  r   r   r  r  r  s     r_   r  (FunctoolsPartialVariable.is_python_equal	  s    u67 		))%**5 &)$))UZZ&@   *-MM((*ENN,A,A,C+ 	
r^   )r   r  ri   r  r  r   rv  )rY   rZ   r[   r\   r>   r  r   r   r  r   r   r  r8  r"  r   r   r@   r   rX  r,  r  r  r  r  r  r  r  r]   r  r  s   @r_   r  r  K	  sO   		'	'N $(77 '7 sO+,	7
 !7 7 
7 7(!T !
)c )G#G 'G S/)*	G
 
G
)
14
	
55 5S 5_ 5
C 

# 

D 
; ;
V 
 
 
r^   r  c            
       |  ^  \ rS rSrSSS1\R
                  kr\\R                  S\	\
S\4   \R                  4   4S j5       5       r\S\S	\SS 4S
 j5       rS\S\SS4U 4S jjr\S\
S\4   4S j5       rS\4S jrS\4S jrSSS\\   S\	\\4   S\4S jrSSS\S\\   S\	\\4   S\4
S jrS\4S jrSrU =r$ )PolyfilledFunctionVariablei	  r   r  traceable_fnr`   .c                     0 $ r   rX   )r  s    r_   _get_polyfill_handlers1PolyfilledFunctionVariable._get_polyfill_handlers	  s	     	r^   r  r   c                 ^    [        UR                  [        R                  5      5        U " XS9$ r  r  r  s      r_   r  -PolyfilledFunctionVariable.create_with_source	  s(     	f''(B(BCD5((r^   r   Nc                 H  > [         TU ]  " S0 UD6  Xl        U R                  5       R	                  X5      nS n[        U5      (       d   SU SU 35       eS H,  n[        X5S 5      nU(       d  M  [        U5      (       d   eUn  O   [        SU S35      eX0l        X@l	        g )NzPolyfill handler z is not callable for )__torch_dynamo_polyfill____python_implementation__z# does not have a traceable functionrX   )
r#  r   r   r$  r   callabler   ri  r  r"  )r|   r   r   handlerr"  candidate_attr	candidater)  s          r_   r   #PolyfilledFunctionVariable.__init__	  s    "6"--/33B;  X$5gY>STVSW"XX 
N  >Iy	****(
 #G9,OP  " ,r^   c                     U R                   $ r   )r"  r   s    r_   polyfill_fn&PolyfilledFunctionVariable.polyfill_fn	  s       r^   c                 0    [        U R                  SS5      $ )N*__torch_dynamo_can_constant_fold_through__F)r   r  r   s    r_   can_constant_fold_through4PolyfilledFunctionVariable.can_constant_fold_through	  s    OOI5
 	
r^   c                 "    U R                  5       $ r   r  r   s    r_   r   'PolyfilledFunctionVariable.get_function
  r  r^   r   rE   r   c                 f   U R                  5       (       a  [        X#5      (       aw  U R                  " U Vs/ s H  oDR                  5       PM     sn0 UR	                  5        VVs0 s H  u  pVXVR                  5       _M     snnD6n[
        R                  " X5      $ U R                  [        R                  L Ga6  [        U5      S:X  Ga&  U(       Gd  [        US   [        R                  [        R                  45      (       a  [        S US   R                   5       5      (       a  [        R                  R!                  UUR"                  R%                  S[&        R(                  [+        S US   R                   5       5      40 5      [&        R(                  " US   R                   Vs/ s H4  nUR-                  5       (       a  UR                  5       OUR.                  PM6     sn5      S9$ [
        R                  " XR0                  5      nUR3                  XU5      $ s  snf s  snnf s  snf )Nr9   r   c              3     #    U  Hx  nUR                  5       =(       a    [        UR                  5       [        5      =(       d7    [        U[        R
                  5      =(       a    UR                  5       [        L v   Mz     g 7fr   )r  r   r,  r  r   SymNodeVariabler8  )r  r  s     r_   r  ;PolyfilledFunctionVariable.call_function.<locals>.<genexpr>
  sd       'A %%'SJq7K7K7Ms,S Yq)";";<WTWAWY&s   B Br   c              3   @   #    U  H  oR                  5       v   M     g 7fr   )as_proxy)r  as     r_   r  r<  (
  s     ?A::<<rf  )sym_num)r5  r2   r   r,  r   r>   r   builtinssumr   r   r   rK   TupleVariabler  r;  r   r   create_proxyr$  sym_sumr   r  r@  r"  r   )	r|   r   r   r   r  r   r   r   traceable_function_variables	            r_   r   (PolyfilledFunctionVariable.call_function
  s    ))++0M1
 1
 6:;d**,d;=C\\^L^TQq..00^L  #((44 GGx||#D	Q47Y%;%;Y=T=T$UVV  a   ,,33		&&#MM?a??A	  "&a "/A  !3355 002!"+ "/	 4  ( '6&;&;B@Q@Q&R#*886JJO <L4s   H#%H(-;H.r   c                 Z   US:X  a  U R                  XU5      $ [        U R                  US 5      nU(       d,  [        U5      (       d  [	        USU SU R                   35        0 nU R
                  (       a  [        U R
                  U5      US'   [        U40 UD6nUR                  XU5      $ )N__call__zCannot find callable z in r   )r   r   r   r5   r<   r   r(   r!  )r|   r   r   r   r   methodr  polyfilled_method_variables           r_   r  &PolyfilledFunctionVariable.call_method:
  s     :%%b77$-+f-- '<TF$twwi%PQ;; *4;; =GH%?%R'%R")77&IIr^   c                     U R                   $ r   r1  r   s    r_   r,  -PolyfilledFunctionVariable.as_python_constantN
  r3  r^   )r   r"  r  ) rY   rZ   r[   r\   r>   r  r  r"  cacher   r   r   r   r   r$  r   r  rM   r   propertyr1  r  r5  r   r   r  r   r   r  r,  r]   r  r  s   @r_   r!  r!  	  s    
	'	'	N __tHS#X,>@R@R,R'S    ))!')	%) )-2 - - -4 !Xc3h/ ! !
4 

)c )2K#2K '2K S/)*	2K
 
2KhJ#J J ?#	J
 S/)*J 
J(C  r^   r!  c                   ~   ^  \ rS rSrS\S\SS4U 4S jjrSS	 jrSSS\4S
 jrSSS\	\   S\
\\4   S\4S jrSrU =r$ )SysFunctionVariableiR
  r  r   r`   Nc                 2   > [         TU ]  " S0 UD6  Xl        g r   )r#  r   r  )r|   r  r   r)  s      r_   r   SysFunctionVariable.__init__S
  s    "6"
r^   r   rE   c                 p   [        UR                  5      (       aF  UR                  S   nUR                  nUR                  US5      n[        R
                  " X5      X$/nOA[        R                  " S 5      [        R                  " S 5      [        R                  " S 5      /n[        R                  " U5      $ )Nr  __traceback__)	r   exn_vt_stackrt  rX  r>   r   r   r@   rC  )r|   r   exnr  tbr   s         r_   exc_infoSysFunctionVariable.exc_infoW
  s    r//"%C,,C_5B$**23S=E **40**40**40E
 &&u--r^   c                 >    U R                  U5      R                  S   $ )Nr9   )rZ  r   r   s     r_   	exceptionSysFunctionVariable.exceptione
  s    }}R &&q))r^   r   c                     U R                   [        R                  L a  U R                  U5      $ U R                   [        R                  L d   eU R                  U5      $ r   )r  r  rZ  r]  r   s       r_   r   !SysFunctionVariable.call_functionh
  sH     ::%==$$zzS]]***~~b!!r^   r;  )r   rE   r`   zvariables.TupleVariable)rY   rZ   r[   r\   r   r   rZ  r>   r]  r   r   r  r   r]   r  r  s   @r_   rR  rR  R
  su    c S T .*3 * *
"#
" '
" S/)*	
"
 

" 
"r^   rR  ) create_tma_experimental_metadatacreate_tma_stable_metadataTMADescriptorMetadataTritonHOPifierc                      \ rS rSrS\S\4S jrS\S\4S jr	S\S\
4S jrS	S
S\\R                  R                  R                   S4   4S jrS	\
S\\\
4   SSS\
4S jrS\S\
4   S\\   S\\\4   S\S   S\
S\4S jrS\
S\S   S\
S\S\4
S jrS\
S\S   S\\
   4S jrS\S\
4S jrSSS\\
   SS4S jrSSS\
S \\\
4   SSSS!4
S" jrS#rg$)%DynamoTritonHOPifieri}
  r  r`   c                 "    [        SSSU 3/ S9  g )Nz!triton kernel unsupported featurer4  z/Encountered triton kernel unsupported feature: r  )r$   )r|   r  s     r_   raise_unsupported&DynamoTritonHOPifier.raise_unsupported~
  s    7I#O		
r^   maybe_callablec                 .    [        U[        [        45      $ r   )r   r   r
  )r|   rj  s     r_   is_callable DynamoTritonHOPifier.is_callable
  s    79MN
 	
r^   r   c                     UR                   $ r   r;  )r|   r   s     r_   	get_valueDynamoTritonHOPifier.get_value
  s    yyr^   gridrJ   .c                     SSK Jn  [        X5      (       a  UR                  5       $ [	        SS[        U5       3S/ [        R                  QS9  g )Nr9   )rJ   z/unsupported grid type for triton hop check_gridzgrid type = z;`torch.compile` only supports list-like grid for check_gridr  )listsrJ   r   r>  r$   r  r   r  )r|   rq  rJ   s      r_   
check_gridDynamoTritonHOPifier.check_grid
  sK    +d--==?"I&tDzl3Y&22	r^   metar   rE   c                     UR                  5        VVs0 s H%  u  pE[        R                  R                  U5      U_M'     nnnUR	                  X6/0 5      nU$ s  snnf r   )r   r   r@   r   r   )r|   rq  rv  r   r   r   meta_vars          r_   	call_gridDynamoTritonHOPifier.call_grid
  sW     IM

UI..55a8!;U!!"j"5 Vs   ,Auser_fnr   r   r  c                 X    SSK Jn  UR                  XA5      nUR                  XBU5      nU$ )Nr9   rH  )rR  rI  r   r   )	r|   r{  r   r   r   r  rI  wrapped_user_functionr   s	            r_   call_user_defined_fn)DynamoTritonHOPifier.call_user_defined_fn
  s/     	/ 1 8 8 E&44RvFr^   user_objr   c                 v    SSK Jn  Uc   eU" U[        UR                  U 5      5      R	                  U5      nU$ )Nr9   )VariableBuilder)rR  r  r(   kernel_source_wrap)r|   r  r   r  r   r  wrapped_user_objs          r_   wrap_user_defined_obj*DynamoTritonHOPifier.wrap_user_defined_obj
  sC     	-~~*
811dV=

%/ 	  r^   configsc                 r    UR                  U5      nU Vs/ s H  o3R                  " 5       PM     nnU$ s  snf r   )unpack_var_sequencer  )r|   r  r   r   s       r_   maybe_unpack_configs)DynamoTritonHOPifier.maybe_unpack_configs
  s>     --b1 DKK72247K Ls   4r   c                 n    UR                  5       (       d  U R                  S5        UR                  5       $ )Nz`@triton.heuristics must return constant values because configs can only contain constant values.)r  rh  r  )r|   r   s     r_   maybe_unpack_heuristic_result2DynamoTritonHOPifier.maybe_unpack_heuristic_result
  s3    ((**""r ..00r^   TritonKernelVariablec                     UR                   c  [        U5      S:w  a  U R                  S5        [        U5      " UR                  UR
                  US   UR                  S9$ )Nr9   z7Triton kernels should be called with only a single gridr   )kernel
kernel_idxrq  r  )rq  r   rh  r  r  r  r   )r|   r  r   s      r_   call_getitem!DynamoTritonHOPifier.call_getitem
  sZ     ==$D	Q""I H~??**a"//	
 	
r^   gridscombined_argszvariables.ConstantVariablec                    SSK Jn  0 n[        UR                  5       5       HH  nX7   n[	        U[
        [        45      (       d  M$  UR                  5       Xg'   UR                  5       X7'   MJ     UR                  5        VVs0 s H%  u  px[        R                  R                  U5      U_M'     n	nnSSKJn
Jn  UR                  5        VVs0 s HC  u  px[	        U[         5      (       d  M  UR#                  5       (       d  M3  XxR%                  5       _ME     nnnU	R                  5        VVs0 s H3  u  px[	        U[         5      (       a  UR#                  5       (       a  M1  Xx_M5     nnnUR'                  5        H_  nUR)                  5       nUR+                  5       (       a  M*  UR-                  5       (       a  MA  U R/                  S[1        U5       S35        Ma     U
R3                  U5      nU" U[4        5      nUR6                  R9                  SUSUR:                  UUUUR=                  5       S	.5        [        R                  " S 5      $ s  snnf s  snnf s  snnf )
Nr9   )r  r   )kernel_side_tabletriton_kernel_wrapper_mutationz.Unexpected argument type for a Triton kernel: r5  r   rX   )r  constant_args_idxrq  tma_descriptor_metadatar   )dictsr  r   r  r   !TMADescriptorExperimentalVariableTMADescriptorStableVariableto_metadata
get_tensorr   r   r@   r   *torch._higher_order_ops.triton_kernel_wrapr  r  r>   r  r,  r  realizer  is_symnode_likerh  repradd_constant_argsr   r   rD  r  r>  )r|   r  r  r  r   r  r  r   r   combined_args_vtr  r  r  non_constant_argsr  rv  s                   r_   call_HOPDynamoTritonHOPifier.call_HOP
  s'    	- :<m((*+A A57RS  ./]]_'*#$<<>  , AN@S@S@U
@UI&&--a0!3@U 	 
	
 &++-
-!_- &232F2F2H &A##%%- 	 
 )..0
0q/22q7K7K7M AD0 	 
 #))+A		AKKMMQ%6%6%8%8&&DT!WIQO , .??N !2D9
		*&11%6+B--/		
 ))
 	
[



s$   ,II*II+0IIrX   N)rY   rZ   r[   r\   r  r   rh  r>   r  rl  r   ro  r   r$  fxproxyProxyrt  r   ry  r   r   r   r~  r  r   r  r  r  r  r]   rX   r^   r_   rf  rf  }
  s   
S 
U 

/ 
d 

_  1 eEHHNN<P<PRU<U6V #CH~3J	#s(# ' S/)*	
 ,-  
   ,-  	 
   
 		 ()@ A		c	1O 1 1
(
 sm
 
 	
$G
(G
 G
 CH~	G

 $G
 
&G
r^   rf  c            
          ^  \ rS rSr% S\S'   S\S'   \S-  \S'   S\S	'   S\S\S-  S\S
\SS4
U 4S jjrSSS\\	   S
\
\\	4   S\	4S jrSSS\S\\	   S
\
\\	4   S\	4
U 4S jjrS\S\4S jrSrU =r$ )r  i6  rH   rq  rI   r  Nr  r(   r  r   r`   c                    > UR                  SS 5      U l        [        TU ]  " S0 UD6  [        R                  XX#5        g )Nr  rX   )r   r  r#  r    dynamo_triton_hopifier_singletoninit_variable)r|   r  r  rq  r   r)  s        r_   r   TritonKernelVariable.__init__<  s8     $ZZ>"6"(66tZVr^   r   rE   r   c                 .    [         R                  XX15      $ r   )r  call_triton_kernelr   s       r_   r   "TritonKernelVariable.call_functionC  s     0BB
 	
r^   r   c                    > US:X  a  [         R                  X5      $ US:X  a  [         R                  XXA5      $ [        TU ]  XX45      $ )N__getitem__run)r  r  call_runr#  r  r  s        r_   r   TritonKernelVariable.call_methodM  sL     = 3@@LLU]3<<TTT w"2T::r^   r|  c                 ~    SSK Jn  SSKJn  [	        X5      (       a   UR
                  " UR                  5       5      $ U$ )Nr9   r?   )r;  )r  r@   tensorr;  r   r   evaluate_expr)r|   r|  r@   r;  s       r_   specialize_symbolic(TritonKernelVariable.specialize_symbolic\  s4    .+ c++#**3+<+<+>??
r^   )r  )rY   rZ   r[   r\   r   r  r   r   r   r>   r   r  r   r   r  r  r]   r  r  s   @r_   r  r  6  s    
d
WW'*TzW9<WHKW	W
#
 '
 S/)*	

 

;#; ; ?#	;
 S/)*; 
;s s  r^   r  c                   v   ^  \ rS rSrSSS\\   S\\   S\S\SS	4U 4S
 jjrS\4S jrSS jr	S\4S jr
SrU =r$ )r  if  data_ptrzvariables.DataPtrVariabledims
block_dimselement_sizer   r`   Nc                    > [        U[        R                  5      (       d   e[        TU ]  " S0 UD6  Xl        X l        X0l        X@l        g r   )	r   r   DataPtrVariabler#  r   r  r  r  r  )r|   r  r  r  r  r   r)  s         r_   r   *TMADescriptorExperimentalVariable.__init__g  sC     (I$=$=>>>>"6" 	$(r^   c                     [        U R                   Vs/ s H  oR                  5       PM     snU R                   Vs/ s H  oR                  5       PM     snU R                  R                  5       5      $ s  snf s  snf r   )ra  r  r>  r  r  )r|   dims     r_   r  -TMADescriptorExperimentalVariable.to_metadatav  s[    /'+yy1y\\^y1'+7\\^7&&(
 	
17s
   A6A;
c                   ^ ^ TR                  UU 4S j5        T R                  R                  T5        / T R                  QT R                  QT R
                  PnTR                  U5        TR                  [        U5      S-   S5        g )Nc                  V   > T R                  SS[        TR                  5       S35      $ )Nz$triton.tools.experimental_descriptorcreate_d_tma_descriptor)r  r   r  r  s   r_   r  ?TMADescriptorExperimentalVariable.reconstruct.<locals>.<lambda>  s)    G,,6#dii.))9:r^   r9   F)	r  r  r"  r  r  r  r  r   r   )r|   r#  r   s   `` r_   r"  -TMADescriptorExperimentalVariable.reconstruct}  sp    	
 	!!'*@@T__@d.?.?@c$i!mU3r^   c                 .    U R                   R                  $ r   )r  from_tensorr   s    r_   r  ,TMADescriptorExperimentalVariable.get_tensor  s    }}(((r^   )r  r  r  r  rv  )rY   rZ   r[   r\   r   r>   r   r   r  r"  r  r]   r  r  s   @r_   r  r  f  sn    )-) ?#) )	)
 &) ) 
)
S 

4)O ) )r^   r  c                   b   ^  \ rS rSrSSSSS\SS4U 4S	 jjrS\4S
 jrSS jrS\4S jrSr	U =r
$ )r  i  r  rL   block_shaperK   r   r`   Nc                 l   > UR                  5       (       d   e[        TU ]  " S0 UD6  Xl        X l        g r   )r  r#  r   r  r  )r|   r  r  r   r)  s       r_   r   $TMADescriptorStableVariable.__init__  s5     !!!!"6"&r^   c                 H    [        U R                  R                  5       5      $ r   )rb  r  r>  r   s    r_   r  'TMADescriptorStableVariable.to_metadata  s!    )%%'
 	
r^   c                    ^ TR                  U4S j5        TR                  S5        U R                  R                  T5        T" U R                  5        TR                  S5        g )Nc                  (   > T R                  SS5      $ )Nztriton.tools.tensor_descriptorTensorDescriptorr$  r  s   r_   r  9TMADescriptorStableVariable.reconstruct.<locals>.<lambda>  s    G,,0"r^   r  r   )r  load_methodr  r"  r  r  r  s    `r_   r"  'TMADescriptorStableVariable.reconstruct  sT    	
 	M*(  !Ar^   c                     U R                   $ r   )r  r   s    r_   r  &TMADescriptorStableVariable.get_tensor  s    {{r^   )r  r  rv  )rY   rZ   r[   r\   r   r   r  r"  r  r]   r  r  s   @r_   r  r    sP    	' 	' $	' 		'
 
	'
S 


C  r^   r  c                   b   ^  \ rS rSrS\S\SS4U 4S jjrSSS	\\   S\	\
\4   S\4S
 jrSrU =r$ )'CreateTMADescriptorExperimentalVariablei  rankr   r`   Nc                 B   > US;   d   e[         TU ]  " S0 UD6  Xl        g )N)r9   r   rX   )r#  r   r  )r|   r  r   r)  s      r_   r   0CreateTMADescriptorExperimentalVariable.__init__  s&    
 v~~"6"	r^   r   rE   r   c                    SU;   a  US   OUS   n[        U[        R                  5      (       d/  [        SSU SU 3SU R                   S3SU R                   S	3/S
9  U R                  S:X  ab  [        U5      [        U5      -   S:w  a$  [        US[        U5      [        U5      -    35        SU;   a  US   OUS   /nSU;   a  US   OUS   /nO[        U5      [        U5      -   S:w  a$  [        US[        U5      [        U5      -    35        SU;   a  US   OUS   SU;   a  US   OUS   /nSU;   a  US   OUS   SU;   a  US   OUS   /nSU;   a  US   OUS   n[        U[        R                  5      (       d   e[        UUUUS9$ )Nptrr   z.invalid ptr argument for create_tma_descriptorzargs = z, kwargs = z#Expected `ptr` argument of `create_z]d_tma_descriptor`to be from a `.data_ptr()` call, represented internally by `DataPtrVariable`z`torch.compile` may fail to internally represent result of `.data_ptr()` with `DataPtrVariable` due to a graph break between the `.data_ptr()` call and `create_zTd_tma_descriptor`. Please ensure there were no graph breaks between these two calls.r  r9   rO   z6TMA metadata rank=1 requires exactly 4 arguments, got r  	block_dimr      z6TMA metadata rank=2 requires exactly 6 arguments, got dim1dim0
block_dim1r  
block_dim0r  r  )r  r  r  r  )r   r   r  r$   r  r   r<   r  )r|   r   r   r   r  r  r  r  s           r_   r   5CreateTMADescriptorExperimentalVariable.call_function  s     %fUmDG#y8899H!${6(;A$)) M_ _#yyk *// 99>4y3v;&!+$LSQUYY\]cYdMdLef
 "'&ud1gD (3f'<{#$q'J 4y3v;&!+$LSQUYY\]cYdMdLef
 #)F"2vQ"(F"2vQD
 )5(>|$DG(4(>|$DGJ 2@61Ivn-tTVx #y8899990!%	
 	
r^   )r  )rY   rZ   r[   r\   r  r   r   r   r>   r   r  r   r]   r  r  s   @r_   r  r    sc      
	:
#:
 ':
 S/)*	:

 
:
 :
r^   r  c                   >    \ rS rSrSSS\\   S\\\4   S\4S jrSr	g	)
!CreateTMADescriptorStableVariablei  r   rE   r   r   r`   c                 V    SU;   a  US   OUS   nSU;   a  US   OUS   n[        UUS9$ )Nr  r   r  r9   )r  r  )r  )r|   r   r   r   r  r  s         r_   r   /CreateTMADescriptorStableVariable.call_function  sF     &.%7!T!W/</Ff]+DQRG*#
 	
r^   rX   N)
rY   rZ   r[   r\   r   r>   r   r  r   r]   rX   r^   r_   r  r    s<    
#
 '
 S/)*	

 

r^   r  c                   B    \ rS rSrSrSSS\\   S\\\4   S\4S jr	S	r
g
)!PyTreeGetNodeTypeFunctionVariablei  a  
`torch.utils._pytree._get_node_type` function is very hot function. We want to special case it to reduce Dynamo tracing time.

def _get_node_type(tree: Any) -> Any:
    node_type = type(tree)
    # All namedtuple types are implicitly registered as pytree nodes.
    # XXX: Other parts of the codebase expect namedtuple types always return
    #      `namedtuple` instead of the actual namedtuple type. Even if the type
    #      is explicitly registered.
    if is_namedtuple_class(node_type):
        return namedtuple
    return node_type
r   rE   r   r   r`   c                    [        U5      S:w  a  [        US[        U5       35        S nUS   R                  (       aM  [        US   R                  R	                  [
        R                  5      5        [        US   R                  5      nUS   R                  5       n[        U5      (       a0  [        [        5       S5      n[        R                  " U[        U5      $ [        R                  " XUS9$ )Nr9   z6pytree_get_node_type requires exactly 1 argument, got r   r   r  )r   r<   r   r'   r  r&   
TYPE_MATCHr0   r8  r   r(   r*   r>   r   r   )r|   r   r   r   type_sourcer8  s         r_   r   /PyTreeGetNodeTypeFunctionVariable.call_function  s     t9> HTT 7>>$q'..33L4K4KLM$T!W^^4K1g))+{++$%6%8,GK"((ZEE$$R[IIr^   rX   N)rY   rZ   r[   r\   r   r   r>   r   r  r   r]   rX   r^   r_   r  r    sF    J#J 'J S/)*	J
 
Jr^   r  c                   P   ^  \ rS rSrSrSSS\\   S\\\4   S\4U 4S jjr	S	r
U =r$ )
 PyTreeTreeIsLeafFunctionVariablei*  a  
`torch.utils._pytree.tree_is_leaf` function is a hot function. We want to special case it to reduce Dynamo tracing time.

def tree_is_leaf(
    tree: PyTree,
    is_leaf: Callable[[PyTree], bool] | None = None,
) -> bool:
    if is_leaf is not None and is_leaf(tree):
        return True
    return _get_node_type(tree) not in SUPPORTED_NODES

When is_leaf is None (the common case), we can optimize by not tracing into the function.
When is_leaf is not None, we fall back to regular tracing since it requires executing user code.
r   rE   r   r   r`   c                   > [        U5      S:  d  [        U5      S:  a  [        US[        U5       35        UR                  S[        R                  " S 5      5      n[        U5      S:X  a  US   nUR                  5       (       d  [        T	U ]  XU5      $ US   n[        [        R                  R                  R                  5      R                  X/0 5      n[        R                  " U[        R                  R                  R                  [!        5       S9nUR#                  USU/0 5      n[        R                  " UR$                  (       + 5      $ )Nr9   r   z,tree_is_leaf requires 1 or 2 arguments, got rQ   r   r  r  )r   r<   r   r@   r   rc  r#  r   r  r$  rb   rc   _get_node_typer>   r   rd   re   r  r  )
r|   r   r   r   rQ   treenode_type_varsupported_nodes_varoutr)  s
            r_   r   .PyTreeTreeIsLeafFunctionVariable.call_function:  s     t9q=CIM >s4ykJ **Y(8(?(?(EFt9>1gG''))7(6:: Aw9KK..

-FB
' 	 .33KK//46

 "--b.=/SUV&&399}55r^   rX   )rY   rZ   r[   r\   r   r   r>   r   r  r   r]   r  r  s   @r_   r  r  *  sG    $6#$6 '$6 S/)*	$6
 
$6 $6r^   r  r   )r   rA  r"  r   r   r  r  r  r  r   collectionsr   collections.abcr   r   r   r   typingr   r	   r
   r   r   r   typing_extensionsr   weakrefr   r$  torch._dynamo.excr   torch._guardsr   torch.utils._pytreer   r4  r   r   r   r   bytecode_transformationr   r   r   rR  r   r   r   r   r   r    r!   r"   r#   r$   r%   guardsr&   r'   r   r(   r)   r*   r+   r,   r-   r.   r/   r0   rb   r1   r2   r3   r4   r5   r6   r7   r8   r  r:   r;   r<   r=   r>   r  r@   #torch.distributed.fsdp._fully_shardrA   ModuleNotFoundErrortorch._dynamo.codegenrB   r  rC   rD   rE   rF   #torch._dynamo.variables.ctx_managerrG   r  rH   rI   rs  rJ   rK   r  rL   rM   rx   rz   r  r  r  r9  rT   r   r   rV   	lru_cachere   rg   r   r   r  r   r   r   r  r   r   r  r   r   r   r   r
  r  r  r  ry  r{  r  rC  r  r  rr  r   r,  r6  ri  rs  r  r  r  r  r  r  r  r!  rR  ra  rb  rc  rd  rf  r  r  r  r  r  r  r  r  rX   r^   r_   <module>r     s  .      	 
   " . ( G G # %  4   3 > > V V    1
 
 
	 	 	  'E
 /  L
 6& T#s(+,
&'OP "+YK"8  D  ,=+>sCx( >	0 	 :  
 
>L \ `
`` }` 3-	`
 cN` 
#
`H DHAA&)A39D=AA.0 .$sCx. .T . # o%&  ..  
	 *
..CH~  FCK 4'	
 8_t# S#X% c3h$& 4 --%+-5;d]-JM--6, ,^SY3 SYl)": )X:4 :@O;? O;d

 
F
%= F
R " Fj-- j-Z> 2 ><>"6 >:c  ?
#	
 o%& ,@)!9 @)F*>(B *>ZL
? L
^>"6 ><D
/ D
N ; *d38n //%-c3h%7//_D(< _DD71 74#
$8 #
L}
 }
@D DN "/  "F s
> s
l $8#9  -? -`$) $)N/ BD
o D
N
 
 "J(< "JJ46'; 46Y^  s   'O1 1O=<O=