
    3j"                    l   % 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/J0r0J1r1J2r2J3r3J4r4J5r5J6r6J7r7J8r8  SSK9J:r:J;r;  SSK<J=r=J>r>J?r?J@r@JArAJBrBJCrCJDrDJErE  SSKFJGrGJHrHJIrIJJrJJKrKJLrLJMrMJNrN  SSKOJPrPJQrQJRrRJSrS  SSKTJUrU  SSKVJWrW   SSKXJYrY  \(       a4  SSK[J\r\  SSK]J^r^J_r_J`r`Jara  SSKbJcrc  SSKdJereJfrf  SSKgJhrh  SSKiJjrjJkrk  SSKlJmrm  \" S\S \4   S!9rnS"roS#rp\q" 1 S$k5      rr\q" S%15      rsS&rt\" 5       ru\\\4   \vS''    " S( S)\w5      rx\R                  S*\=4S+ j5       rz " S, S-5      r{S.\S*\{4S/ jr|S.\S0S1S2\ S-  S3\\   S4\}\~\4   S*\}\~\S4   4S5 jr SS0S1S6\S7\ S-  S*\S4S8 jjrS0S1S9\}\~\4   S*S4S: jrS;S1S9\}\~\S4   S<\	GR                  S*S4S= jrS<\	GR                  S>\}\~\4   S?\~S@\\S 4   S-  SA\\   S-  SB\}\~\4   S-  SC\}\~\4   S-  S*\	R                   4SD jr1 SEkrS0S1SF\S7\ S-  S?\~S*\S4
SG jr " SH SI\S5      r " SJ SK\5      r " SL SM\5      r " SN SO\5      r " SP SQ\5      r " SR SS\S5      r " ST SU\5      r " SV SW\5      r " SX SY\5      r " SZ S[\5      r " S\ S]\5      r " S^ S_\5      rS0S1SF\S \4   S?\~S3\\S   S4\}\~\S4   S*\S4S` jr " Sa Sb\5      r " Sc Sd\5      r " Se Sf\S5      r " Sg Sh\5      r " Si Sj\5      r " Sk Sl\5      rS*\}\\4   4Sm jrS0S1SF\S \4   S*\=4Sn jr " So Sp\5      r " Sq Sr\5      r " Ss St\S5      r " Su Sv\S5      r " Sw Sx\S5      rSSyKdJrJrJrJr   " Sz S{\5      r\" 5       r " S| 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      r " S S\5      rS0S1S\/ S4   S?\~S*S4S jr " S S\S5      rg! \Z a    SrY 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raise_type_errorStepUnsupportedunimplementedUnsupported)GuardBuilderinstall_guard)	
AttrSourceCellContentsSourceClosureSourceConstantSourceDefaultsSourceGetItemSourceImportSourceSkipGuardSource
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ValueMutationNewVariableTrackerConstantVariable)UserDefinedObjectVariable)_fsdp_param_group)	PyCodegen)&InliningGeneratorInstructionTranslatorInliningInstructionTranslatorInstructionTranslatorInstructionTranslatorBase)ContextWrappingVariable)TritonGridTypeTritonKernelType)DunderDictVariable)BaseListVariableListVariable)TensorVariable_F.)bound      >   is_leaf	namespacenone_is_leafrS   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__rZ       [/home/wildlama/miniconda3/lib/python3.13/site-packages/torch/_dynamo/variables/functions.pyrX   rX      s    r`   rX   returnc            	      T    [        [        [        [        S5      S5      S5      S5      $ )Ntorchutils_pytreeSUPPORTED_NODES)r)   r/   rZ   r`   ra   !get_pytree_SUPPORTED_NODES_sourcerh      s(    :l73W=yI 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   funcrb   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 Nr:   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)selfrl   codevnoffs        ra   __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 NrZ   )
__defaults__defaults__kwdefaults__
kwdefaultsdictzipry   lenrangepos_default_map)r   rl   s     ra   update_defaultsFunctionSpec.update_defaults   si     ))/R--3  $""C$6#6#895T]]AS;TU 
r`   )ry   rt   r   r   rv   rz   r   rx   rr   rw   r}   r   )r[   r\   r]   r^   r   r   r   r_   rZ   r`   ra   rj   rj      s+    N\ Nd N$	
L 	
T 	
r`   rj   rl   c                 ^    [         R                  U 5      nUc  [        U 5      nU[         U '   U$ N)rV   getrj   )rl   specs     ra   	_get_specr      s.    ??4 D|D! DKr`   txrF   	fn_sourceargskwargsc           
      P   [        U 5      n[        U5      UR                  :X  a  U R                  (       dq  U(       dj  UR                  (       dY  UR
                  (       dH  UR                  (       d7  [        UR                  5       VVs0 s H  u  pgU[        XU   5      _M     snn$ UR                  U 5        0 n[        U5      n	UR                  R                  5        Hq  u  pzS nU(       aH  [        R                  " UR                   U
   5      (       a  ["        R$                  (       d  ['        X*5      n[        XR                   U
   U5      X'   Ms     [        UR                  5       Hk  u  pgU[        U5      :  a  [        XU   5      X'   M&  Xy;   a-  XuR(                  ;  a  [        XR+                  U5      5      X'   MX  Xx;  d  M_  [-        SU 35      e   U[        UR                  5      S  nUR                  (       a"  [        U[/        U5      5      XR                  '   O4U(       a-  [-        S[        U5       S[        UR                  5       35      eUR                   Hq  nXy;   a  [        XR+                  U5      5      X'   M&  XuR0                  ;   a0  S nU(       a
  ['        X'SS9n[        XR0                  U   U5      X'   Me  [-        SU 35      e   UR
                  (       a  [        X5      XR
                  '   U$ U	(       a  [-        S[3        U	5       35      eU$ s  snnf )Nz&missing required positional argument: z#Too many positional arguments: got z, expected T)is_kwz(Missing required keyword-only argument: zUnexpected keyword arguments: )r   r   rt   r   r}   r   rz   	enumeratery   wrap_bound_argr   r   r   itemsr@   
is_literalr   r   %skip_guards_on_constant_func_defaultsr-   rw   pop	TypeErrortupler   list)rl   r   r   r   r   r   inamebarem_kwidxdefault_sourceextrakwdefault_sources                 ra   bind_args_cachedr      s    T?D
 	D	T^^#!!!!!! %T%7%78
8 .!W--8
 	
 		B&\F ))//1	''c(:;;<<+I;N!"mmC&8.I 2 T//0s4y=%bq'2BH^ *** &b**T*:;BH^DTFKLL! 1& T''(*+E .r5< @	1#d)KDL^L^H_G`a
 	

 !!>%b**T*:;BH__$##1)#N %b//$*?AQRBHFtfMNN " ,R8?? I 
8fGHHIE
s   L"valsourcec                     [        U[        5      (       a  U$ U(       d  [        R                  " X5      $ [        R                  R                  X5      $ r   )
isinstancer>   buildr   LazyVariableTrackercreate)r   r   r   s      ra   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       ra   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         ra   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   rl   r   s
             ra   _create_nested_fnr   8  s_     #AD$+u%%&8K01 *[$"?"???&Kr`   >	   __doc__ro   r[   r\   __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)   r0   inspectgetattr_staticAttributeErrorr"   fn_known_dunder_attrsgetattrr   r   r   r>   r   )r   r   r   r   subobjs        ra   fn_var_getattrr   _  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SSS\SS4U 4S jjjrS\S-  4S jrSS jrS	S
S\	S\
\   S\\	\4   S\4
U 4S jjrS\	4S jrS\	4S jrS\	4S jrS\	S-  4S jrS\\	\4   4S jrS\R(                  4S jrS\4S jrS\R0                  4S jrS\	4S jrS	S
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\\	\4   4S jrS\4S jr Sr!U =r"$ ) BaseUserFunctionVariablei|  Ndict_vtzDunderDictVariable | Noner   rb   c                 2   > [         TU ]  " S0 UD6  Xl        g r   )superr   r   )r   r   r   	__class__s      ra   r   !BaseUserFunctionVariable.__init__}  s     	"6"29r`   c                     U R                   $ r   r   r   s    ra   
get_source#BaseUserFunctionVariable.get_source      {{r`   r   rF   c                 |    U R                   c$  [        R                  R                  X5      U l         U R                   $ r   )r   r   rK   r   r   r   s     ra   get_dict_vt$BaseUserFunctionVariable.get_dict_vt  s.    <<$77>>rHDL||r`   r   r   c                    > US:X  a,  U R                  U5      R                  US[        U5      U5      $ US:X  a,  U R                  U5      R                  US[        U5      0 5      $ [        TU ]  X[        U5      U5      $ )N__setattr____setitem____delattr____delitem__)r   call_methodr   r   r   r   r   r   r   r   s        ra   r   $BaseUserFunctionVariable.call_method  s     = ##B'33M4:v  ]"##B'33BtDzSUVVw"2T$Z@@r`   c                 6    U R                  5       R                  $ r   get_codeco_filenamer   s    ra   get_filename%BaseUserFunctionVariable.get_filename      }}***r`   c                 6    U R                  5       R                  $ r   r   co_namer   s    ra   get_name!BaseUserFunctionVariable.get_name      }}&&&r`   c                 ~    [         R                  S:  a  U R                  5       R                  $ U R	                  5       $ )N)      )sysversion_infor   co_qualnamer   r   s    ra   get_qualname%BaseUserFunctionVariable.get_qualname  s.    w&==?...==?"r`   c                 <    U R                  5       R                  S   $ Nr   )r   	co_constsr   s    ra   get_doc BaseUserFunctionVariable.get_doc  s    }}((++r`   c                     [         er   NotImplementedErrorr   s    ra   get_globals$BaseUserFunctionVariable.get_globals      !!r`   c                     [         er   r  r   s    ra   r   !BaseUserFunctionVariable.get_code  r  r`   c                     [         er   r  r   s    ra   has_self!BaseUserFunctionVariable.has_self  r  r`   c                     [         er   r  r   s    ra   get_function%BaseUserFunctionVariable.get_function  r  r`   c                 (    U R                  5       S   $ )Nr[   )r  r   s    ra   
get_module#BaseUserFunctionVariable.get_module  s    !*--r`   c                    U R                  U5      nUS;   ay  [        U SUSS  35      " 5       nUR                  U5      (       a  UR                  U5      $ [        R
                  " X@R                  =(       a    [        U R                  U5      S9$ US:X  a  U$ US:X  a  UR                  US 5      $ US	:X  a  UR                  US
 5      $ UR                  U5      (       a  UR                  U5      $ [        [        U5        g )N)r[   r]   r   r\   ro   get_r   r   __dict__r   c                  <    [         R                  " 0 [        5       S9$ Nmutation_type)r   ConstDictVariabler=   rZ   r`   ra   <lambda>6BaseUserFunctionVariable.var_getattr.<locals>.<lambda>  s    	33"2"4r`   __type_params__c                  <    [         R                  " / [        5       S9$ r%  )r   TupleVariabler=   rZ   r`   ra   r)  r*    s    	//"2"4r`   )r   r   containsgetitemr@   r   r   r)   getitem_or_defaultr"   r   )r   r   r   fn_dictr   s        ra   var_getattr$BaseUserFunctionVariable.var_getattr  s   ""2& TT$$tAbzl 346C%%t,,#**KKIJt{{D,I  ZN&&--  &&--  %%t,,(<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       ra   call_function&BaseUserFunctionVariable.call_function  sg     MMO88!!#,,-JKK#**400--d4Ndnn6F4N4NPVWWr`   c                     SnU[         ;   d  US:X  a  SnO [        U R                  5       U5      n[        R
                  " X5      $ ! [         a    Sn N$f = f)NFr#  T)r   hasattrr  r  r>   r   r   r   r   r   s       ra   call_obj_hasattr)BaseUserFunctionVariable.call_obj_hasattr  sa     ((DJ,>F !2!2!4d; $$R00 ' s   A AAc                     0 $ r   rZ   r   s     ra   closure_vars%BaseUserFunctionVariable.closure_vars      	r`   c                     gNTrZ   r   s    ra    should_allow_nested_graph_breaks9BaseUserFunctionVariable.should_allow_nested_graph_breaks      r`   )r   r   )r   rF   rb   rK   )#r[   r\   r]   r^   r   r   r   r   r   strr   r>   r   r   r   r   r  r  r  r   CodeTyper   boolr  r   r  r  r2  r:  r@   r?  rB  rG  r_   __classcell__r   s   @ra   r   r   |  s   59:2:EH:	: :FTM 
A#A A '	A
 S/)*A 
A+c +'# '#c #,t ,"T#s(^ ""%.. ""$ ""e00 ".C .!=5 !=S !=FX#X 'X S/)*	X
 
X1)1141	16 4_@T;U $  r`   r   c                     ^  \ rS rSrSr\R                  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U 4S jjrS\\   4S jrS\R                  4S jrS\R4                  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U 4S j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\4S" jr-SSS\$\   S
\\ \4   S\.S \$\   \\ \4   4   S-  4S# jr/S\R                  S-  4S$ jr0S%\S\.\S&4   S-  4S' jr1S\S\\   S-  4S( jr2S\3S)   4S* jr4S\54S+ jr6S,\7S\4S- jr8S.r9U =r:$ )0UserFunctionVariablei  z-Some unsupported user-defined global functionr   is_constant>   optree
optree.opstorch.utils._pytreetorch.utils._cxx_pytreevaluer   rb   c                 ^    [        UR                  [        R                  5      5        U " XS9$ Nr   r(   
make_guardr'   CLOSURE_MATCHclsrV  r   s      ra   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_inlinerZ   )r   r   r   rQ  r   r   r   rd   jitScriptFunctionr%   r   INFERENCE_MODEr   r   r   r   r   rQ  r   r   s       ra   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   )r8   rP  r   r   as_python_constantr   r   s    ra   rn  'UserFunctionVariable.as_python_constant<  s(    $,--77Nw)++r`   c                 b   > [        U [        5      (       a  U R                  $ [        TU ]  5       $ r   )r8   rP  r   r   get_real_python_backed_valuero  s    ra   rr  1UserFunctionVariable.get_real_python_backed_valueB  s(    $,--77Nw355r`   c                     / $ r   rZ   r   s    ra   r8  UserFunctionVariable.self_argsG  rD  r`   c                     U R                   $ r   r   r   s    ra   r  !UserFunctionVariable.get_functionJ      wwr`   c                 .    U R                   R                  $ r   )r   ro   r   s    ra   r   UserFunctionVariable.get_codeM  s    wwr`   c                 "    [         R                  $ r   r   r   r   s    ra   python_type UserFunctionVariable.python_typeP      !!!r`   c                 4    [        U R                  SS 5      S L$ )N__self__)r   r   r   s    ra   r  UserFunctionVariable.has_selfS  s    tww
D1==r`   c                 .    U R                   R                  $ r   )r   r   r   s    ra   r   UserFunctionVariable.get_globalsV  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     ra   r   UserFunctionVariable.get_sourceY  s.    jy'C'CDD^^Fr`   r   rF   r   c                 P   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(       aU  [%        ['        U5      U	5      n[)        USU
S9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.rZ   cell_contents)freevar_nameN)rQ  r   r   r   r   r   root_txr   r   r   ro   r   r   co_freevarsr   r   countr   r.   r+   r*   r>   r   r  
ValueErrorr   DeletedVariabletrack_cell_existing)r   r   r   r   r   r  r   r   r   r   r   cellr   cell_varclosure_cellclosure_cell_contentscontents_vars                    ra   	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#8#8 2 24I$L (;; ?#2#8#8ASAS#TL (;;D$U#4LE 
H ' " ?#,#<#<#>L? " ?#,#<#<#>L?s$   ,!G! HH ?H H%$H%r   r   c           
         > US:X  a  [         TU ]  X5      $ U[        ;   a2  [        R                  " X[        [        U R                  U5      5      S9$ U R                  5       n[        XR                  X25      $ )Nr#  py_type)
r   r2  r4   r   GetAttrVariabletyper   r   r   r   )r   r   r   r   r   s       ra   r2   UserFunctionVariable.var_getattr  sk    :7&r00++,,D$)?$@  "b''688r`   c                 Z    [        U R                  U5      n[        R                  " X5      $ r   )r=  r   r>   r   r>  s       ra   r?  %UserFunctionVariable.call_obj_hasattr  s%     $'$$R00r`   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 a   [        R                  " U R                   5      R                  " U0 UD6nUR+                  5         UR                  S   nUR                  S   n[!        U[,        5      (       a  UR	                  5       n[!        U[,        5      (       a  UR	                  5       n[        R.                  " X5      $ U R                   [        R                  R0                  R2                  L a0  SUR4                  l        [        R8                  R;                  S 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RC                  5       nS
URD                   S3n[G        SSU RH                   SU SU S3U/ [J        RL                  QS9  [!        U[N        5      (       d)  URQ                  5       nSU S3n[G        SU  USU S3S/S9  UR                   n[        RR                  " U[        R                  RT                  RV                  S9$ U RX                  (       a%  [[        XR                   U RQ                  5       X#5      $ UR4                  R\                  R^                  (       dt  U R                   [        R                  R`                  Rb                  L aC  [        R                  Rd                  Rg                  U5         [h        TU ]  XU5      sS S S 5        $ [m        UR4                  R\                  SS 5      S:X  a  UR4                  R\                  Rn                  (       ds   S	SK8J9n  Ubi  U R                   URt                  URv                  4;   aC  [        R                  Rd                  Ro                  U5         [h        TU ]  XU5      sS S S 5        $ U Ry                  XU5      nUb  U$ [h        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! [&         a  n
[        SU SU 35      U
eS n
A
ff = f! , (       d  f       GNj= 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: 
, kwargs: error_on_graph_breakz\Improper error_on_graph_break() call. Please fix your call to error_on_graph_break(). args: fwdbwdzZImproper override_cudagraphs() call. Please fix your call to override_cudagraphs(). args: Tr   z=`nonstrict_trace` expects a callable, but got value of type <>zTypeError from user codezcall_function(, )rb  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` regionkinddescriptionz!torch.utils.checkpoint.checkpoint)	FSDPState)=r   rd   _dynamopatch_dynamo_configrn  r   changesr   DynamoConfigPatchVariabler;   RuntimeErrorr  r   	signaturebind	argumentsr   rL  ErrorOnGraphBreakVariable	Exceptionoverride_cudagraphsapply_defaultsr>   CudagraphOverrideVariablebytecode_debugger
breakpointr   _emit_debugger_breakpointr@   r   nonstrict_tracer   r   r~  r[   r%   rV  r   
USER_ERRORrP  r   TorchInGraphFunctionVariableAllowInGraphKindNONSTRICT_TRACErQ  invoke_and_store_as_constantcurrent_tracer,unsafe_allow_externally_visible_side_effectsre   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_stater  _pre_forward_post_forward_maybe_call_tree_map_fastpath)r   r   r   r   arg
args_constkeyr   kwargs_constr  erP   r  r  r  fn_vartypmsgfn_namer   r  tree_map_resultr   s                         ra   r:  "UserFunctionVariable.call_function  s    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999))$''277HH$$&ooe,ooe,c?33002Cc?33002C ::3DD WW77BBB26BII/--44T::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99II  /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6g H  7 "!F*VH6 	  "!F*VH6    "!F*VH6 h   ! 	! NMs   V/ V$V/ V)8AV/ #A9W B=W7 &XX- #Y $V/ /
W9WW
W4W//W47
XXX
X*-X=<X= 
Yc           	         U R                  XU5      nUb  Uu  pVnOU nUnUn[        U[        5      =(       a    UR                  5       n[        U[        5      =(       a    UR	                  5       n	U(       d  U	(       d  g 1 Uk[
        -
  (       a  g [        U5      S:  a  g US   n
US   nUSS  nU	(       a  UR                  UUU
UUSS9$ UR                  UUU
UU5      $ )Nr   r   r:   rZ   )keypath)	_rewrite_tree_map_only_callr   rP  _is_tree_map_function_is_tree_map_with_path_function_SUPPORTED_TREE_MAP_KWARGSr   call_tree_map_with_pathcall_tree_map)r   r   r   r   rewritetree_map_fntree_map_argstree_map_kwargsis_tree_mapis_tree_map_with_pathmap_fn
first_treerests                ra   r  2UserFunctionVariable._maybe_call_tree_map_fastpath,  s    222VD:A7KK M$O {$89 4113 	
 {$89 >;;= 	
 4_ ::}!q!"1%
QR  55 6   ++ r`   c                     [        U R                  SS 5      S:H  =(       a$    [        U R                  SS 5      U R                  ;   $ )Nr[   tree_mapr\   r   r   _TREE_MAP_MODULESr   s    ra   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                  ;   $ )Nr[   tree_map_with_pathr\   r  r   s    ra   r  4UserFunctionVariable._is_tree_map_with_path_functionf  s>    DGGZ.2FF Ot48N8NN	
r`   c                     [        U R                  SS 5      S:H  =(       a$    [        U R                  SS 5      U R                  ;   $ )Nr[   tree_map_onlyr\   r  r   s    ra   _is_tree_map_only_function/UserFunctionVariable._is_tree_map_only_functionl  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                  " X5      n
XU/[        U5      4$ )Nr  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              ra   r  0UserFunctionVariable._rewrite_tree_map_only_callr  s     ..00t9>V966*.'x99-H  ::<$468T2

 ,11"H 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 )Nr\   r  )r   r   r  modulesr   r   r   r   )r   module_namemoduler  s       ra   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   r  ).0r  s     ra   	<genexpr>DUserFunctionVariable._extract_tree_map_only_types.<locals>.<genexpr>  s     >IS:c4((Is   )is_python_constantrn  r  _flatten_type_specallr   r   fromkeys)r   r  	raw_value	flatteneds       ra   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   rV  	collectedentryflat
union_types         ra   r  'UserFunctionVariable._flatten_type_spec  s    eT""7NeU##$&I..u5<  &	 
 UK6
!j&C&CI..u5<  &	 (
 r`   Tc                     grF  rZ   r   s    ra   is_python_hashable'UserFunctionVariable.is_python_hashable  rI  r`   c                 ,    [        U R                  5      $ r   )hashr   r   s    ra   get_python_hash$UserFunctionVariable.get_python_hash  s    DGG}r`   otherc                 t    [        U[        R                  5      =(       a    U R                  UR                  L $ r   )r   r   rP  r   r   r$  s     ra   is_python_equal$UserFunctionVariable.is_python_equal  s*    %!?!?@XTWWPUPXPXEXXr`   )r   rQ  F);r[   r\   r]   r^   r   r   r   _cpython_typer   _nonvar_fields	frozensetr  classmethodr   r^  rd   rh  ri  rL  r   rn  rr  r   r>   r8  r  rK  r   r  r~  r  r   rJ  r  r   r   r   r  r2  r@   r?  r:  r  r  r  r  r   r  r  r  r  r
   r  intr"  objectr'  r_   rM  rN  s   @ra   rP  rP    sV   7 &&M 	 
"	0	0N "	
 )s )C )<R ) ) "!9!99  	
 
 >,C ,6c 6
40 e00  %..  "T ">$ >#T#s(^ #F <'< '< S/)*	<
 
c?"	#<|95 9S 9_ 91)1141	1|7#|7 '|7 S/)*	|7
 
|7|2#2 '2 S/)*	2
 
4	2h
t 

 

D 
$K#$K '$K S/)*	$K 	"_%o%&(	

 	$KL
5+=+=+D 
/'/	tSy	D	 /" T
T0A ,GDM  YV Y Y Yr`   rP  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$ )
InspectSignatureVariablei  a  
Variable tracker for inspect.signature with caching support.

inspect.Signature is expensive to trace. When inspect.signature is called
repeatedly on the same function during tracing, we cache the result to avoid
retracing the signature construction each time. Although this is different
from CPython behavior, it is safe to do so because inspect.signature does
not change across different calls to the same function.
r   rF   r   r   rb   c                 r  > [        U5      S:X  a  U(       d  US   nS n[        U[        [        45      (       a  UR	                  5       nUb\  XQR
                  R                  ;   a  UR
                  R                  U   $ [        TU ]!  XU5      nXaR
                  R                  U'   U$ [        TU ]!  XU5      $ rn   )	r   r   rP  r  r  r   signature_cacher   r:  )r   r   r   r   
target_arg	cache_keyr   r   s          ra   r:  &InspectSignatureVariable.call_function  s     t9>&aJI*';=O&PQQ&335	$		 9 999944Y??.r@7=		)))4w$Rv66r`   rZ   r[   r\   r]   r^   r   r   r>   r   rJ  r:  r_   rM  rN  s   @ra   r1  r1    sG    7#7 '7 S/)*	7
 
7 7r`   r1  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  i  r  .r  r   rb   Nc                 >   > [         TU ]  " S0 UD6  Xl        X l        g r   )r   r   r  r  )r   r  r  r   r   s       ra   r   $TreeMapOnlyFunctionVariable.__init__  s      	"6"*r`   c                     [         $ r   r   r   s    ra   r~  'TreeMapOnlyFunctionVariable.python_type	  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     ra   r  DTreeMapOnlyFunctionVariable._matches_allowed_type.<locals>.<genexpr>  s     TASg:i11ASs   )r~  r  anyr  )r   r=  rB  s     @ra   _matches_allowed_type1TreeMapOnlyFunctionVariable._matches_allowed_type  sC    	((*I TASASTTT # 		s   2 
??r   rF   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   r:   )r  r:  rE  r   )r   r   r   r   leafs        ra   r:  )TreeMapOnlyFunctionVariable.call_function  sz     ;;,,Rv>>Aw%%d++;;,,Rv>>t9>V ;;,,Rv>>r`   )r  r  )r[   r\   r]   r^   r   r+  r   r  r>   r   r   r~  rL  rE  r   r   rJ  r:  r_   rM  rN  s   @ra   r  r    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\	4S jr
\S	\S\4S
 j5       rSSS\\   S\\\4   S\4S jrSrU =r$ )BuiltinMethodVariablei%  r   rQ  r   rb   Nc                 t   > [         TU ]  " S0 UD6  [        U[        R                  5      (       d   eXl        g r   )r   r   r   r   BuiltinMethodTyper   rk  s       ra   r   BuiltinMethodVariable.__init__&  s3     	"6""e556666r`   c                 "    [         R                  $ r   )r   rM  r   s    ra   r~  !BuiltinMethodVariable.python_type-  s    &&&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  r[   r   r  r,  )rQ  method_selfmethod_names      ra   is_supported_builtin_method1BuiltinMethodVariable.is_supported_builtin_method0  sI    llll u$A	)A 
*L{n/L	
r`   r   rF   r   c                     U R                   R                  nU R                   R                  nU R                  =(       a    [	        U R                  S5      n[
        R                  " XUSS9nUR                  XX#5      $ )Nr  T)realize)r   r  r[   r   r)   r>   r   r   )r   r   r   r   rU  r   
obj_sourceobj_vts           ra   r:  #BuiltinMethodVariable.call_function;  sb     gg&&ww[[HZZ%H
 &&r
DQ!!"D99r`   rw  r)  )r[   r\   r]   r^   r   rM  rL  r   r   r  r~  staticmethodrW  r   r>   r   rJ  r:  r_   rM  rN  s   @ra   rK  rK  %  s    ?D))8<PS	 'T ' 
 
 
 

:#
: '
: S/)*	
:
 

: 
:r`   rK  c            
          ^  \ rS rSr\R
                  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\\	\
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*$ ))LocalGeneratorObjectVariableiH  r   r   inline_tracerrD   r   rb   Nc                 J   > [         TU ]  " S0 UD6  Xl        X l        X0l        g r   )r   r   r   r   ra  )r   r   r   ra  r   r   s        ra   r   %LocalGeneratorObjectVariable.__init__L  s%     	"6"	"*r`   c                     U R                   $ r   )r   r   s    ra   r   %LocalGeneratorObjectVariable.get_codeX  s    yyr`   c                 6    U R                  5       R                  $ r   r   r   s    ra   r   )LocalGeneratorObjectVariable.get_filename[  r   r`   c                 6    U R                  5       R                  $ r   r   r   s    ra   r   %LocalGeneratorObjectVariable.get_name^  r  r`   c                     [        S5      e)Nr  r  r   s    ra   r  )LocalGeneratorObjectVariable.get_functiona  s    !.11r`   c                     gNFrZ   r   s    ra   r  %LocalGeneratorObjectVariable.has_selfd      r`   c                 "    U R                  5       $ r   )r   r   s    ra   r[   %LocalGeneratorObjectVariable.__name__g  s    }}r`   c                 V    U R                   R                   SU R                  5        S3$ )N(r  )r   r[   r   r   s    ra   __str__$LocalGeneratorObjectVariable.__str__j  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_effectsrw  torch._dynamo.symbolic_convertrx  ry  r   ra  generator_exhaustedforce_unpack_var_sequenceremaining_itemsr   ListIteratorVariablereconstruct)
r   codegenrw  rx  ry  r   savedisallowtemptracers
             ra   r  (LocalGeneratorObjectVariable.reconstructo  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'c                     U R                   $ r   r   r   s    ra   r  (LocalGeneratorObjectVariable.get_globals      ~~r`   c                 "    [         R                  $ r   )r   GeneratorTyper   s    ra   r~  (LocalGeneratorObjectVariable.python_type  s    """r`   r   rG   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 .rb  zA

Skipping frame due to graph break in a generator's next() call.)ra  _is_generator_exhaustedr"   StopIterationinline_call_r   r|  r   r%   PT2_ISSUE_TRACKER_URLr   r  r&   rd   r  
eval_frame	skip_coder   
skip_frame	one_graphr  r  )r   r   r  r  s       ra   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rF   r   c                     X R                  5       R                  ;   a  [        R                  " S5      $ [        R                  " S5      $ NTF)r~  r#  r@   r   r   r   r   s      ra   r?  -LocalGeneratorObjectVariable.call_obj_hasattr  s;     ##%...#**400&&u--r`   c                     grm  rZ   r   s     ra   has_unpack_var_sequence4LocalGeneratorObjectVariable.has_unpack_var_sequence  ro  r`   c                     grF  rZ   r   s     ra   has_force_unpack_var_sequence:LocalGeneratorObjectVariable.has_force_unpack_var_sequence  rI  r`   c                 @    / nU R                  XR                  5        U$ r   )force_apply_to_var_sequenceappend)r   r   r   s      ra   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      ra   r  8LocalGeneratorObjectVariable.force_apply_to_var_sequence  s@     4%%b)*  - )"-s    44Fc                     grm  rZ   r   s    ra   rG  =LocalGeneratorObjectVariable.should_allow_nested_graph_breaks  ro  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   )ra  _raise_exception_variabler   exception_handler)r   r   r  r  r  s        ra   _setup_exception-LocalGeneratorObjectVariable._setup_exception  sE     ##	(,,S1  	( $$Q''	(s     
AA  Ac                 ^    U R                   S L =(       d    U R                   R                  S:H  $ r  )ra  instruction_pointerr   s    ra   _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   ra  r   s    ra   r  4LocalGeneratorObjectVariable._is_generator_exhausted  s    t))+@%HHr`   r   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  Ga  U R                  nU R                  5       (       d  U R                  5       (       a&  SUl	        [        R                  R                  S 5      $  U R                  U[        R                  " [        / 5      5        [         R"                  S:  a@  UR$                  R&                  S:X  a&  SUl	        [        R                  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[3        S[4        40 5      n U R                  U[        R                  " U/ 5      5        U R                  U5        [	        [*        U5        U$ [8        TU ]u  XX45      $ ! [(         a)    SUl	        [        R                  R                  S 5      s $ f = f! [(         a)    SUl	        [        R                  R                  S 5      s $ [,         a    UR.                  c   eUR.                  s $ f = f! [0         a    e f = f! [7        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     ra   r  ;LocalGeneratorObjectVariable.call_method.<locals>.<genexpr>  s     BTc//11T   closeT)r     CALL_INTRINSIC_1throwr  r:   r   __InternalThrowException)r  r  r   r  r"   r   ra  	push_manyr  r|  r   r@   r   r  ExceptionVariableGeneratorExitr  r  next_instructionopnamer    r  r!   symbolic_resultr   r  r  r   r   r   )	r   r   r   r   r   r  retvalexc_typer   s	           ra   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88>>?%%	33M2F( $$///66:LL15F.$55<<TBB.%%b)),\2> W_ ''F
 %%bSY!^$q'aQ
 ''+F| 6bIH?%%b)*E*EhPR*ST""2&
 )v>Mw"2T::a ) ?-1* 1188>>? ) ?-1* 1188>>, .
 --999---., % N 1:  MsI   A>I& &J ?)K8 8L &0JJ0K5$K54K58LLL)r   r   ra  r~  r  rC   rb   N)+r[   r\   r]   r^   r   r  r*  rK  r   rJ  r   r   r   r   r   r   r  rL  r  rt  __repr__r  r  r  r~  r>   r  r@   r?  r  r  r   r}  r   r  r
   rG  r  r  r  r   r_   rM  rN  s   @ra   r`  r`  H  s   ''M
+nn
+ S>
+ @	
+
 
+ 

+%.. +c +'# '2e 2$ # ? ? HV$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.
rZ   N)r[   r\   r]   r^   r   r_   rZ   r`   ra   r  r    s    r`   r  c            	         ^  \ rS rSrSrS\4S jr\S.S\S\S\	SS	4U 4S
 jjjr
S\S\	4S jrS\R                  4S jrS\\\	4   4S jrS\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\4S jrSrU =r$ )LocalGeneratorFunctionVariablei  zlfunctions that behaves like iterators

.. note::

    This is a wrapper around (Nested)UserFunctionVariable
rb   c                 "    [         R                  $ r   r}  r   s    ra   r~  *LocalGeneratorFunctionVariable.python_type  r  r`   )generator_clsvtr  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       ra   r   'LocalGeneratorFunctionVariable.__init__  s      	"6"*r`   r   c                 v    XR                   R                  ;   a  [        X5      $ [        U R                  U5      $ r   )r   r#  r   r  )r   r   s     ra   __getattr__*LocalGeneratorFunctionVariable.__getattr__  s.    >>***4&&tww%%r`   c                 6    U R                   R                  5       $ r   )r  r   r   s    ra   r   'LocalGeneratorFunctionVariable.get_code      ww!!r`   c                 6    U R                   R                  5       $ r   )r  r  r   s    ra   r  *LocalGeneratorFunctionVariable.get_globals  s    ww""$$r`   c                 6    U R                   R                  5       $ r   )r  r  r   s    ra   r  'LocalGeneratorFunctionVariable.has_self  r  r`   r   rG   r   rE   c                 8    SSK Jn  UR                  " UU UU5      $ )Nr   )rE   )r{  rE   build_inline_tracer)r   r   r   r   rE   s        ra   _build_inline_tracer3LocalGeneratorFunctionVariable._build_inline_tracer  s'     	Q,@@	
 	
r`   rF   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.rb  r   )
r   r  r   r%   rJ  r  r   r  r  r   )r   r   r   r   ra  r   r   s          ra   r:  ,LocalGeneratorFunctionVariable.call_function  s     DGG,,.//ADGG,,./. LH	 11"d4j&Iww!GG'')	 !!;;	 " 
 	
r`   c                 6    U R                   R                  5       $ r   )r  rr  r   s    ra   rr  ;LocalGeneratorFunctionVariable.get_real_python_backed_value  s    ww3355r`   )r  r  )r[   r\   r]   r^   r   r  r~  r`  r   r   r   rJ  r  r   rK  r   r   r  rL  r  r   r>   r  r   r:  r/  rr  r_   rM  rN  s   @ra   r  r    s   "T " ;		+$	+ 		+
 	+ 
	+ 	+& & &"%.. "%T#s(^ %"$ "
'
 ?#
 S/)*	

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

 

<6f 6 6r`   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$ )3FunctionDecoratedByContextlibContextManagerVariablei  za
.. note::

    This is only used when the function is annotated with @contextlib.contextmanager
r  r   rb   Nc                 4   > [         TU ]  " U4S[        0UD6  g )Nr  )r   r   r  )r   r  r   r   s      ra   r   <FunctionDecoratedByContextlibContextManagerVariable.__init__  s%    	
N	
 	
r`   r   rG   r   rD   c                    > [         TU ]  XU5      n[        U[        R                  R
                  R                  5      (       d   eSUl        U$ rF  )r   r  r   rd   r  symbolic_convertrD   is_generator_from_ctx_manager)r   r   r   r   r  r   s        ra   r  HFunctionDecoratedByContextlibContextManagerVariable._build_inline_tracer  sR     -b?MM**QQ
 
 	
 
 04,r`   rZ   )r[   r\   r]   r^   r   r   r   r   r   r>   r   rJ  r  r_   rM  rN  s   @ra   r  r    sb    
3 
s 
t 
' ?# S/)*	
 
2 r`   r  c                     ^  \ rS rSrSr\R                  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	\	4S jrSrU =r$ )r  i  z$Some unsupported user-defined methodNr   .rQ  r  r   rb   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__rZ   )r   r   rQ  r  r   r)   )r   r   rQ  r  r   r   s        ra   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$ )Nrs  r  r  )r   r[   r   rQ  r   s    ra   r  UserMethodVariable.__repr__:  s.    ..))*!DGG9BtxxjBBr`   c                     U R                   /$ r   rQ  r   s    ra   r8  UserMethodVariable.self_args=      zr`   c                 "    [         R                  $ r   r   
MethodTyper   s    ra   r~  UserMethodVariable.python_type@      r`   r   rF   r   c                 h  > SSK JnJn  U" U R                  5      (       ag  / U R	                  5       QUQn[
        R                  " U R                  [
        R                  R                  R                  S9nUR                  XU5      $ U" U R                  5      (       ag  / U R	                  5       QUQn[
        R                  " U R                  [
        R                  R                  R                  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                  [.        R0                  R2                  L a>  [
        R4                  " U R                  5      R                  XR                  /UQ7U5      $ U R&                  (       aO  [#        U R                  R6                  U R                  R*                  5      n	[9        XU R;                  5       X#5      $ [<        T
U ])  XU5      $ )	Nr   )is_leaf_functionis_nonstrict_trace_callabler  r\   r  z	torch.nn.ztorch.nn.utils.parametrize)constant)trace_rulesr  r  r   r8  r   r  rd   r  r  r:  LEAF_FUNCTIONr   is_root_tracerr   rQ  NNModuleVariabler   
startswithrQ  r   r[   r   rB   FSDPParamGroupuse_training_stateTorchCtxManagerClassVariablerV  r  r   r   )r   r   r   r   r  r  	call_argsvarmodule_attrr   r   s             ra   r:   UserMethodVariable.call_functionC  s    	P&tww//2$..*2T2I88ioo>>NNC $$RF;;DGG$$2$..*2T2I88ioo>>LL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  )rQ  r>   r   r   r  r   r2  )r   r   r   r   s      ra   r2  UserMethodVariable.var_getattr  sI    :88O: #((WWdnnEEw"2,,r`   c                     U R                   $ r   rw  r   s    ra   rr  /UserMethodVariable.get_real_python_backed_value  ry  r`   )rQ  r  r   )r[   r\   r]   r^   r   r   r  r*  r   r   r>   r   r   rJ  r  r   r8  r  r~  r   r   r:  r2  rr  r_   rM  rN  s   @ra   r  r    s   . $$M $(	JS#XJ J D=	J
 J 
J J2C# C40  T%"2"23  B7#B7 'B7 S/)*	B7
 
B7H-5 -S -_ -c  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$ )WrappedUserMethodVariablei  wrappedrd  rH   r   rb   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   rQ  )r   r   r   r   rQ  r  rd  r   r  rd  r   r   s       ra   r   "WrappedUserMethodVariable.__init__  sG     	

4

5$W[[;F;r`   r   rF   r   c                 X  > [         R                  (       aM  [        [        R                  5      nUR                  XR                  U R                  /[        U5      -   U5      $ U R                  R                  U5        [        TU ]  XU5      nU R                  R                  U5        U$ r   r   nested_graph_breaksrP  r   _fn_with_ctxr:  rd  r  r   enterr   exitr   r   r   r   
wrapper_fnr   r   s         ra   r:  'WrappedUserMethodVariable.call_function       %%-i.D.DEJ++\\4<<04:=v  	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   rd  r  r   s   ra   r)  7WrappedUserMethodVariable.reconstruct.<locals>.<lambda>      gdll&;r`   r:   Fadd_push_nullr  extend_outputr   r   r  s   ``ra   r  %WrappedUserMethodVariable.reconstruct  4    ;<21e<=r`   rd  r  r  )r[   r\   r]   r^   r  r   r   r   r>   r   rJ  r:  r  r_   rM  rN  s   @ra   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  rd  rH   r   rb   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  rd  r  s       ra   r   $WrappedUserFunctionVariable.__init__  s3     	

4.v.r`   r   rF   r   c                 X  > [         R                  (       aM  [        [        R                  5      nUR                  XR                  U R                  /[        U5      -   U5      $ U R                  R                  U5        [        TU ]  XU5      nU R                  R                  U5        U$ r   r   r%  s         ra   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   ra   r)  9WrappedUserFunctionVariable.reconstruct.<locals>.<lambda>  r.  r`   r:   Fr/  r2  s   ``ra   r  'WrappedUserFunctionVariable.reconstruct  r4  r`   r5  r  )r[   r\   r]   r^   rP  r   r   r   r>   r   rJ  r:  r  r_   rM  rN  s   @ra   r7  r7    sr    	%	 +	 		
 
	# ' S/)*	
 
 > >r`   r7  c                   ^ S[         S[        4U4S j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	T[        T5      S9$ s  snf s  snnf )Nxrb   c           
        > U R                  5       (       a  [        SU 5      R                  5       $ [        U [        5      (       aA  U R
                  b(  [        U R                  [        R                  5      5        U R                  $  U R                  5       $ ! [         aD    [        SST S[        U 5      R                   3S[        U 5      R                   ST S3SS	/S
9   g f = f)NrN   z1assume_constant_result argument conversion failedz	function z, variable type z Cannot convert argument of type z# to a Python constant for function zm marked with torch._dynamo.assume_constant_result. The variable tracker does not support constant conversion.z>Remove torch._dynamo.assume_constant_result from this functionzIEnsure all arguments passed to the function can be converted to constantsrb  )	is_tensorr	   get_real_valuer   rA   r   r(   rZ  r'   ID_MATCHrV  rn  r;   r%   r  r[   )rA  r   s    ra   convert-invoke_and_store_as_constant.<locals>.convert  s    ;;==(!,;;==a233xx#all<+@+@AB77N	''))2 	K#D6)9$q':J:J9KL>tAw?O?O>P Q  $v &MN U_
	s   B AC&%C&r   )r>   r   r   r   register_attr_or_moduler,   )
r   r   r   r   r   rF  rA  r   r   ress
     `       ra   r  r    s    ? s , !%%1GAJD%(.7amF7
d
f
C99,,d# -   &7s   B Bc                   L  ^  \ 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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\4S jrS\\   S\R                  4S jrSSS\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%$ )%NestedUserFunctionVariablei  r   Nr  r   r   r   r   
wrapped_fnr   rb   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        g )Nr'  r&  rZ   )r   updater<   r   r   r   rn  rJ  r   rK  r   r  r   r   r   r   r   rL  )
r   r  r   r   r   r   r   rL  r   r   s
            ra   r   #NestedUserFunctionVariable.__init__  s     ::o&.MM(<(>M?"6"'446<<<<$113U^^DDDD)T****	" $2<r`   c                     / $ r   rZ   r   s    ra   r8  $NestedUserFunctionVariable.self_args"  rD  r`   c                 "    U R                  5       $ r   )r  r   s    ra   rn  -NestedUserFunctionVariable.as_python_constant%  s      ""r`   c                 6    U R                   R                  5       $ r   )r   rn  r   s    ra   r   #NestedUserFunctionVariable.get_code(  s    yy++--r`   c                 "    [         R                  $ r   r}  r   s    ra   r~  &NestedUserFunctionVariable.python_type+  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)Nr:   )r;   z-cycle detected in mutually recursive closuresz1failed to convert closure cell to Python constant)baser;   idsetrX   add_get_function_impldiscard)r   rX  r;   self_idr  s        ra   r  'NestedUserFunctionVariable.get_function.  s     	>T(%K!(?  	 	)**;7 ( 3 	(C	
 (s$   A# #A:)A55A::A= =Bc                 R     U R                  5         g! [        [        4 a     gf = fr  )rn  r  r&   r   s    ra   r  -NestedUserFunctionVariable.is_python_constantD  s-    	##%#[1 		s    &&c                 b   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                  (       a  U R2                  R5                  S5      (       ar  U R2                  R7                  S5      R                  5       n
[        U
[        5      (       a  SSKJn  [=        U" U
5      5      n
[        U
[<        5      (       d   eXl        U	$ )Nr   )rF   z self-referential nested function)argdefsr   r   r   ) r   r{  rF   
current_txr   r   r   	load_cellrX   r   rK  r  rn  r  r9   r   r   r   r   r   r  r   r   r   r   r   r.  r/  r   r   r   r   )r   rX  closure_cellsrF   r   cellsr  r  rV  rl   r   r   s               ra   r^  -NestedUserFunctionVariable._get_function_implK  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<<DLL112CDD,,../@ATTVK+u--."8K#89 k40000#. r`   r   rF   r   c           
      L  > US;   a  [         TU ]  X5      $ US:X  a:  [        U SS 5      nU(       a  UR                  5       $ [        R
                  " S 5      $ U[        ;   a6  [        R                  " X[        [        [        R                  U5      5      S9$ [         TU ]  X5      $ )N)r   r#  r   ro   r\   r[   r]   r+  r   r   r  )r   r2  r   rn  r@   r   r4   r   r  r  r   r   r   r   r   dr   s       ra   r2  &NestedUserFunctionVariable.var_getattr  s     	
 	
 7&r00>!j$/A-.1'')Q4D4K4KD4QQ++,,D1C1CT)J$K  7&r00r`   c                     U R                   S L$ r   )r   r   s    ra   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      $ )Nr[   ro   r   r   )r   r   r   rn  r   const_getattrrl  s       ra   rs  (NestedUserFunctionVariable.const_getattr  sg    :==?":==?">!j$/A-.1'')8D8w$R..r`   c                 >  > US:X  a!  [         R                  " U[        U S5      5      $ US:X  a!  [         R                  " U[        U S5      5      $ [        R                  " U5      nX0R                  U5      ;   a  [        R                  " S5      $ [        TU ]  X5      $ )Nro   r   r   r   T)r>   r   r=  r@   r   r   r   r?  )r   r   r   r  r   s       ra   r?  +NestedUserFunctionVariable.call_obj_hasattr  s     :"((WT6-BCC>!"((WT:-FGG$$T*!!"%%#**400w'11r`   c                     grm  rZ   r   s    ra   r  #NestedUserFunctionVariable.has_self  ro  r`   c                     U R                   $ r   r  r   s    ra   r  &NestedUserFunctionVariable.get_globals  r  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   )r9   )r
  _s     ra   r  7NestedUserFunctionVariable.bind_args.<locals>.<genexpr>  s     S+Ra)D//+Rs   )r   r   r   r   r  rn  r   r   r   r   r   r  r   keys_as_python_constantr   r   r  r  r  r   r  r   r   r  r   r   r   )r   r   r   r   r   rl   rP   r   r   r   r  s              ra   r  $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                  (       an  U R                  R                  S5      (       aN  U R                  R                  S5      n U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.                  R1                  U 5      (       a~  UR,                  R.                  R2                  U    R5                  5        HH  u  pETR7                  5         T" U5        TR                  [)        S5      5        TR9                  U5        MJ     g g ! ["         a    T" U5         GNmf = f)	Nc                  0   > T R                  [        S5      $ )Nr   )load_import_fromr[   r  s   ra   r)  8NestedUserFunctionVariable.reconstruct.<locals>.<lambda>  s    G,,X7JKr`   r      Fc                  (   > T R                  SS5      $ )N	functoolswrapsr  r  s   ra   r)  r    s    00gFr`   r:   r   T)r0  r   r1  create_load_const_uncheckedr   r@   r   rV  r   r   create_load_constr   r   r   r.  r/  rn  r  r   rL  r   r   r   r   has_pending_mutationstore_attr_mutationsr   dup_top
store_attr)r   r  r   r   r   rV  s    `    ra   r  &NestedUserFunctionVariable.reconstruct  s   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<<DLL112CDD,,../@AK%)<<>%%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   *1M M+*M+)r   r   r   r   r  r   rL  r   r  )&r[   r\   r]   r^   r   r+  r>   r   rJ  r   r   r   r8  r   r   rn  rK  r   r  r~  r\  r.  r  rL  r  r^  r2  rp  rs  r@   r?  r  r  r   r  r  r_   rM  rN  s   @ra   rK  rK    s    	!	0	0N .2= = = S>	=
 "D(= $d*= !4'= $d*= = 
= =640 #E$6$6 #.%.. ."T%"4"45 ")C4 )5CUCU ),D 6c#h 65;M;M 6p15 1S 1_ 1,(T (/ 7 /s /s /
2)
214
2	
2$ T#s(^ ' ' S/)*	
 
c?"	#:7) 7)r`   rK  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  rd  rH   r   rb   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                  5        Xl
        X l        g )	Nr  r   r   r   r   r   r   rL  )r   r   r   r  r   r   r   r   r   rL  r  rd  r  s       ra   r   *WrappedNestedUserFunctionVariable.__init__  s     	

9d#

64 

;%

:t$

<&

=$'

9d#

<&OOLLOO	
 r`   r   rF   r   c                 X  > [         R                  (       aM  [        [        R                  5      nUR                  XR                  U R                  /[        U5      -   U5      $ U R                  R                  U5        [        TU ]  XU5      nU R                  R                  U5        U$ r   r   r%  s         ra   r:  /WrappedNestedUserFunctionVariable.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   ra   r)  ?WrappedNestedUserFunctionVariable.reconstruct.<locals>.<lambda>;  r.  r`   r:   Fr/  r2  s   ``ra   r  -WrappedNestedUserFunctionVariable.reconstruct:  r4  r`   r5  r  )r[   r\   r]   r^   rK  r   r   r   r>   r   rJ  r:  r  r_   rM  rN  s   @ra   r  r    sr    + + 	
 
4# ' 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\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$ )SkipFunctionVariablei@  rV  reasonNr   rb   c                 >   > [         TU ]  " S0 UD6  Xl        X l        g r   )r   r   rV  r  )r   rV  r  r   r   s       ra   r   SkipFunctionVariable.__init__G  s    "6"
r`   c                     U R                   $ r   rV  r   s    ra   rn  'SkipFunctionVariable.as_python_constantL      zzr`   c                     U R                   $ r   r  r   s    ra   rr  1SkipFunctionVariable.get_real_python_backed_valueO  r  r`   r   c                 x   [         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      (       ac  [        US5      (       a)  [        UR                  [        R                  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)   rZ  r'   r[  	isbuiltinr=  r   r  r   
ModuleTyper(   BUILTIN_MATCHr7   )r]  rV  r   guard_on_sourceguard_on_values        ra   r^  'SkipFunctionVariable.create_with_sourceR  s    !!%)?GG %O"N.*FNN!/!J!J",#%A# .*FNN &&|'A'ABu%%
 5*-- 0 02 2 f//0J0JKL077 &++L,F,FGH5((r`   r   rF   r   c                    [         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 U R                  ["        R$                  R&                  L ay  [)        U5      S;   aD  U(       d=  US   nUS   n	[+        U[,        5      (       a  [/        UR0                  XR2                  S 9$ [        S!S"U  S#U S$U 3S%/ [4        R6                  QS	9  g [8        R:                  (       aH  SS&KJn
  U
RA                  XR                  5      n[+        U[B        5      (       d  URE                  XU5      $ [G        U R                  S'S(5      n[G        U R                  S)S 5      nUc  S*O
[	        U5      n [         RH                  " U R                  5      nS+U S,U S-3nS.U S/3/nS0U;  a  US1U S23S3/-  nUSA:X  a  SBnSC/nU RT                  (       a  U RT                  nOSDSEK+J,n  U" U R                  5      n[        SFSGU SHU SIU 3UUS	9  g ! [         a  n[        UR                   5      S eS nAff = f! [J         a    S4S51nUU;   a  S6U SU S/3nS7S8/n NUbc  URM                  S95      (       aM  S:U SU S3nS;/n[
        R                  RN                  RQ                  US<-   S<RS                  U5      -   5         GNS=U SU S>3nS?S@/n[
        R                  RN                  RQ                  US<-   S<RS                  U5      -   5         GNVf = f)JNr  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` callrb  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.r  z$Call to `torch._dynamo.skip_frame()`z/Called `torch._dynamo.skip_frame()` with args `z`. Skipping frame r  z#User-inserted skip frame. Message: z-Remove the `torch._dynamo.skip_frame()` call.T)rc  rd  re  rf  r  z*Call to `torch._dynamo.step_unsupported()`zUser-inserted step_unsupported.z3Remove the `torch._dynamo.step_unsupported()` call.)r   r  r   r:   )r  z!unsupported function.__get__ callzcall_function z, args: r  z^Dynamo only supports function.__get__(func, obj[, cls]) where func is a user-defined function.SourcelessBuilderr]   z<unknown qualname>r\   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`.r  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. rR  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_graphztorch.compiler.allow_in_graph (or torch._dynamo.allow_in_graph) was called inside a compiled region. Dynamically annotating functions inside a compiled region is not supported.ziApply @torch.compiler.allow_in_graph as a decorator before compilation, not inside the compiled function.r   )get_skip_reasonz,Attempted to call function marked as skippedzmodule: z, qualname: z, skip reason: )-r   r   rV  r%   rJ  rd   r  graph_breakr   rn  r  r   f_codestep_unsupportedr&   r$   r  r   r   __get__r   r   rP  r  r   r   r   SUPPORTABLEr   dont_skip_tracingbuilderr  r   r  r:  r   getfiler   r  re   	warn_oncejoinr  r	  r  )r   r   r   r   r  graph_break_msgskip_frame_msgr  func_varobj_varr  
rebuilt_fnqualname	module_orr  pathre  rf  known_python_builtin_modulesr  r  s                        ra   r:  "SkipFunctionVariable.call_functiont  s    !!$**.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	 ZZ5--555 4yF"67q'h(<==- W  ;(htfJvhO96)556 ''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 ++A 8 {{9(4F";-|H:_U[T\]'	[  7%aee,$67n  %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   M. AN .
N8NN!Q.9A#Q.AQ.-Q.r   c                 X    [         R                  " U[        U R                  U5      5      $ r   )r>   r   r=  rV  r  s      ra   r?  %SkipFunctionVariable.call_obj_hasattr	  s"     $$RT)BCCr`   c           
          U[         ;   a2  [        R                  " X[        [	        U R
                  U5      5      S9$ [        XR
                  U R                  U5      $ )Nr  )r4   r   r  r  r   rV  r   r   r  s      ra   r2   SkipFunctionVariable.var_getattr$	  sL    )),,DT)B$C  b**dkk4@@r`   c                     grF  rZ   r   s    ra   r  'SkipFunctionVariable.is_python_hashable,	  rI  r`   c                 ,    [        U R                  5      $ r   )r!  rV  r   s    ra   r"  $SkipFunctionVariable.get_python_hash/	      DJJr`   r$  c                 r    [        U[        5      =(       a!    U R                  5       UR                  5       :H  $ r   )r   r>   rn  r&  s     ra   r'  $SkipFunctionVariable.is_python_equal2	  s1    uo. H'')U-E-E-GG	
r`   )r  rV  r   )r[   r\   r]   r^   r>   r+  r   rJ  r   rn  rr  r-  r   r^  r   r   r:  r@   r?  r2  rL  r  r.  r"  r/  r'  r_   rM  rN  s   @ra   r  r  @  s>    
	'	'Nc 3:  PT  
C c  )s )F )?U ) )Bi#i 'i S/)*	i
 
iVD)D14D	D
A5 AS A_ AD    
V 
 
 
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$ )WrappedSkipFunctionVariablei9	  r  rd  rH   r   rb   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 )NrV  r  )r   r   r   rV  r  r  rd  r  s       ra   r   $WrappedSkipFunctionVariable.__init__:	  sJ     	

7D!

8T"Hw~~HHr`   r   rF   r   c                 X  > [         R                  (       aM  [        [        R                  5      nUR                  XR                  U R                  /[        U5      -   U5      $ U R                  R                  U5        [        TU ]  XU5      nU R                  R                  U5        U$ r   r   r%  s         ra   r:  )WrappedSkipFunctionVariable.call_functionF	  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   ra   r)  9WrappedSkipFunctionVariable.reconstruct.<locals>.<lambda>W	  r.  r`   r:   Fr/  r2  s   ``ra   r  'WrappedSkipFunctionVariable.reconstructV	  r4  r`   r5  r  )r[   r\   r]   r^   r  r   r   r   r>   r   rJ  r:  r  r_   rM  rN  s   @ra   r  r  9	  sr    
%
 +
 	

 

# ' S/)*	
 
 > >r`   r  c                      ^  \ rS rSrSrS\4S jrS\S\S\SS4U 4S	 jjr	S\4S
 jr
S\4S jrS\R                  4S jrSSS\S\4U 4S jjrS rS\\   4S jrSSS\\   S\\\4   S\4S jrS\4S jrSrU =r$ )WrapperUserFunctionVariablei\	  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".
rb   c                 "    [         R                  $ r   r}  r   s    ra   r~  'WrapperUserFunctionVariable.python_typed	  r  r`   wrapper_objattr_to_tracer   Nc                 >   > [         TU ]  " S0 UD6  Xl        X l        g r   )r   r   r  r  )r   r  r  r   r   s       ra   r   $WrapperUserFunctionVariable.__init__g	  s    "6"&*r`   c                 .    U R                   R                  $ r   )r  r\   r   s    ra   r  &WrapperUserFunctionVariable.get_modulel	  s    ***r`   c                 .    U R                   R                  $ r   )r  r[   r   s    ra   r   $WrapperUserFunctionVariable.get_nameo	  s    (((r`   c                 6    U R                  5       R                  $ r   )r  ro   r   s    ra   r   $WrapperUserFunctionVariable.get_coder	  s      "+++r`   r   rF   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   )	r  r   r  r   r)   r>   r   r   r2  )r   r   r   r   r   r   s        ra   r2  'WrapperUserFunctionVariable.var_getattru	  sc    %%%$**D,>,>?C[[BZT%BF"((&99w"2,,r`   c                 B    [        U R                  U R                  5      $ r   r   r  r  r   s    ra   r  (WrapperUserFunctionVariable.get_function|	      t''););<<r`   c                     / $ r   rZ   r   s    ra   r8  %WrapperUserFunctionVariable.self_args	  rD  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>                  " U[@        RB                  5      RE                  UU [<        R>                  " XR                  5      /UQU5      $ )N
cache_infor\   r  r  r:   )maxsplitr   rd   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: r  )#r=  r  r   r  splitframe_summaryosr  basenamefilenamelinenor  	tracebackformat_listrd   r  re   r  logging	getLoggerisEnabledForDEBUG_guardsTracingContextextract_stackr   rJ  debugr8  r   r>   r   r   getattr_and_tracer:  )r   r   r   r   	target_fnr  r  r  r  r  dynamo_logger
user_stack	frame_locuser_stack_formatteduser_stack_traceall_argss                   ra   r:  )WrapperUserFunctionVariable.call_function	  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$$''
 -?((-?-?@L8L
	
r`   c                 B    [        U R                  U R                  5      $ r   r  r   s    ra   rr  8WrapperUserFunctionVariable.get_real_python_backed_value	  r  r`   )r  r  )r[   r\   r]   r^   r   r  r~  r   rJ  r   r  r   r   rK  r   r>   r2  r  r   r8  r   r   r:  r/  rr  r_   rM  rN  s   @ra   r  r  \	  s    "T "+C + +s +t +
+C +)# ),%.. ,-5 -S -_ -=40 -
#-
 '-
 S/)*	-

 
-
^=f = =r`   r  c            
       f   ^  \ rS rSrSrS\4S j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.
rb   c                 "    [         R                  $ r   r  r   s    ra   r~  %WrapperUserMethodVariable.python_type	  r  r`   r  r  self_objr   Nc                 4   > [         TU ]  " X40 UD6  X0l        g r   )r   r   rQ  )r   r  r  r  r   r   s        ra   r   "WrapperUserMethodVariable.__init__	  s     	>v>r`   c                     U R                   /$ r   r  r   s    ra   r8  #WrapperUserMethodVariable.self_args	  r  r`   r  )r[   r\   r]   r^   r   r  r~  r   rJ  r>   r   r   r8  r_   rM  rN  s   @ra   r  r  	  s_     T    "	
  
40  r`   r  c                  \    [         R                  R                  5       (       a  SSKJn   U $ 0 $ )Nr   traceable_collective_remaps)rd   distributedis_available)torch.distributed._functional_collectivesr$  r#  s    ra   _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)rd   r%  r&  r(  valuesr[   import_sourcer)   )r   r   
inner_namepath_sources       ra   _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   rb   Nc                b   > [         TU ]  " U40 UD6  [        U[        5      (       d   eX l        g r   )r   r   r   rP  r1  )r   r   r1  r   r   s       ra   r   *CollectiveFunctionRewriteVariable.__init__	  s1     	&v&/+?@@@@.r`   r   rF   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   )r1  r   )r0  r  rP  )r   r4  r   r5  new_fn
new_sources         ra   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    ra   can_rewrite-CollectiveFunctionRewriteVariable.can_rewrite
  s%     x(WX9U9W-W	
r`   c                 6    [        5       U   nU[        X5      4$ r   )r(  r.  )r   r   r7  s      ra   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                  :X  Ga  [        R                   (       d  [        S
U R                   SS/S	9  US   n[#        U[$        R&                  5      (       d)  [(        R*                  R,                  R/                  S5      e[1        5       n[1        5       n	[1        5       n
[1        5       nS nUR2                   GH  nUR5                  5       UR6                  La)  [(        R*                  R,                  R/                  S5      eUR9                  US5      nUR;                  5       (       a  UR                  5       nXR<                  UR>                  4;  a,  [(        R*                  R,                  R/                  SU 35      e[$        R@                  RC                  URD                  5      nOk[G        US5      (       a.  [$        R@                  RC                  URI                  5       5      nO,[(        R*                  R,                  R/                  SU 35      eURK                  U5        URK                  UR9                  US5      5        U	RK                  UR9                  US5      5        U
RK                  UR9                  US5      5        Ub  GM  UR9                  US5      nGM     Uc   eSn[$        R&                  " U5      [$        R&                  " U	5      [$        R&                  " U
5      [$        R&                  " U5      US.nU RL                  RO                  UUU5      $ U R                  UR<                  UR>                  4;   aI  [        R                   (       d  [        SU R                   SS/S	9  U RL                  RO                  XU5      $ U R                  URP                  URR                  URT                  4;   ai  URW                  S5      nUb  URX                  OURZ                  S   R\                  nUU;  a  [_        SU 35      e[`        Rb                  " XU   5      US'   U RL                  RO                  XU5      $ )Nr   )REDUCE_OP_TO_STRrZ   async_opz)async_op=True for distributed collectivesz, args=	, kwargs=z3`torch.compile` doesn't support `async_op=True for rb  z.P2P compilation disabled for batch_isend_irecvzP2P compilation is disabled.z3Set TORCHDYNAMO_ENABLE_P2P_COMPILATION=1 to enable.p2p_op_listz`P2POp` used incorrectlyopzunexpected P2POp op r   zunexpected P2POp op variable tensorpeertaggroup)op_list	peer_listtag_listtensors
group_namez(P2P compilation disabled for isend/irecvzUnsupported all_reduce op: )2torch.distributedr%  r'  rB  r   r  r   r   r  r  rn  r%   r   r  batch_isend_irecvr   enable_p2p_compilationr   r   rM   rd   r  r  InternalTorchDynamoErrorr   r   r~  P2POpr2  r  isendirecvr@   r   r[   r=  r   r  r1  r:  
all_reducereduce_scatter_tensor_reduce_scatter_baser   rV  
parametersdefaultr  r>   r   )r   r   r   r   distrB  r  p2p_opsopspeerstagsrN  	group_varitemop_varrF  new_args
new_kwargsreduce_op_var	reduce_ops                       ra   r:  /CollectiveFunctionRewriteVariable.call_function
  sT    	)N %%dgg.	innd5f5??@F:$6$I$I$K$KC77)8dWJvi8QRVRYRYQZ[&22	 77d,,,00L#wwi >M	 ]+Ggy'='=>>mm''@@.  *.CFE6DfG04I##%TZZ7--++DD2  ))"d3,,..224B**djj!99#mm//HH22$7  '77>>r{{KFVZ00&77>>v?PQF--++DD7x@  

6"t//H=>T--b&9:D,,R78$ $ 0 0W =I5 &8 (((46H$11#6&33E:%2248$11':'6J ''55b(JOO77tzz4::..00F#wwi >M	 ''55bGG77OO&&%%	
 
 #JJt,M !, ##))$/77 
  00 #>yk!JKK*00i5PQF4L##11"FCCr`   )r1  )r[   r\   r]   r^   r   r   r   rP  r   r^  r   r   rL  r=  r   r)   r  r   r>   r   rJ  r:  r_   rM  rN  s   @ra   r0  r0  	  s7   	/S#X	/ .		/
 	/ 
	/ 
#
c"
 
 	

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

 A#A)1#s();A	sJ	A AvD#vD 'vD S/)*	vD
 
vD vDr`   r0  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
  rb   c                     U R                   $ r   rw  r   s    ra   rn  0CollectionsNamedTupleFunction.as_python_constant
  ry  r`   r   rF   r   r   c                     [        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 ! [         a1  n	[        [        U	5      U[        U	R                  5      S9   S n	A	NS n	A	ff = f)N)r   r&  znamedtuple constructionzargs=rD  z?`torch.compile` only support certain input types for namedtuplerb  )r2   r   rn  r   r   r"   r  r   r   r   UserDefinedClassVariabler=   r%   r   r  )
r   r   r   r   constant_argsrA  r   r   rV  r  s
             ra   r:  +CollectionsNamedTupleFunction.call_function
  s     ,D9
6:;d**,d;=C\\^L^TQq..00^L 55.0  	-tgZy)Y"..		
 <L (Ichhs3   C B7C B</C 7C 
C='C88C=rZ   N)r[   r\   r]   r^   r   rn  r   r>   r   rJ  r:  r_   rZ   r`   ra   rj  rj  
  sH    C 
#
 '
 S/)*	

 

r`   rj  c                   V  ^  \ rS rSr\R
                  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$ )FunctoolsPartialVariablei
  original_cache_hashNrl   r   keywordsr   rb   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   rl   r   r   r   r   rt  r  partialr5   
fake_valuers  )r   rl   r   rt  rs  r   r   s         ra   r   !FunctoolsPartialVariable.__init__
  sd     	"6"	$%%%%	(D))))  $++H5#6 r`   c                 "    [         R                  $ r   )r  rv  r   s    ra   r~  $FunctoolsPartialVariable.python_type
  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  rv  r  r  s   ra   r)  6FunctoolsPartialVariable.reconstruct.<locals>.<lambda>
  s    g&>&>{I&Vr`   r:   F)r0  rl   r   foreachrt  r1  r   r   r*  r   keyscreate_call_function_kw)r   r  r  s    ` ra   r  $FunctoolsPartialVariable.reconstruct
  s    VW		99OODII&}}!!"6s499~7I5"QR,,./T]]'')*++CIDII,F,JDRWX	
r`   c                 "    U R                  5       $ r   rn  r   s    ra   r  %FunctoolsPartialVariable.get_function
      &&((r`   r   rF   c                     U R                   [        U5      -   n0 U R                  EUEnU R                  R	                  XU5      $ r   )r   r   rt  rl   r:  )r   r   r   r   merged_argsmerged_kwargss         ra   r:  &FunctoolsPartialVariable.call_function
  s@     ii$t*,34==3F3yy&&rFFr`   r   c                 t    [         R                  " U[        [        R                  " [
        5      U5      5      $ r   )r>   r   r=  r  rv  r5   r  s      ra   r?  )FunctoolsPartialVariable.call_obj_hasattr
  s)     $$R1B1B81Ld)STTr`   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                  " X5      U_M     nnn[        R                  " XcS9$ U[        ;   a6  [        R                  " X[        [        [        R                   U5      5      S9$ [#        [$        U5        g s  snnf )Nrl   r   r   rt  r  )r   r)   rl   r   rM   r   rt  r   r>   r   r(  r4   r  r  r   r  rv  r"   r   )r   r   r   r   r   r   r   s          ra   r2  $FunctoolsPartialVariable.var_getattr
  s    >DKK!>6>996>))$))FCC:AEATATAVWAV_**2114AVEW..uDD)),,D1B1BD)I$J  	!4 Xs   #D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  rv  rl   rn  r   rt  r   )r   r  r   r   s       ra   rn  +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  rv  rl   guard_as_python_constantr   rt  r   rs  
cache_hash)r   r   r   r   s       ra   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  r  s     ra   r  >FunctoolsPartialVariable.is_python_hashable.<locals>.<genexpr>  s     B	**,,	r  c              3   @   #    U  H  oR                  5       v   M     g 7fr   r  r
  rV  s     ra   r  r    s     S<R5,,..<Rr  )rl   r  r  r   rt  r*  r   s    ra   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"  r  s     ra   r  ;FunctoolsPartialVariable.get_python_hash.<locals>.<genexpr>!  s     @is((**ir  c              3   @   #    U  H  oR                  5       v   M     g 7fr   r  r  s     ra   r  r  "  s     S<R5,,..<Rr  )rl   r"  r   rt  r*  r!  )r   	func_hash	args_hashvalues_hashs       ra   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      ra   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      ra   r  r  -  s,      +&W ''00+r  )	r   rr  rl   r'  r  r   r   rt  r*  r&  s     ra   r'  (FunctoolsPartialVariable.is_python_equal%  s    u67 		))%**5 &)$))UZZ&@   *-MM((*ENN,A,A,C+ 	
r`   )r   rw  rl   rt  rs  r   r  ) r[   r\   r]   r^   r  rv  r*  r>   r+  r   r   rJ  r   r   r  r~  r  r  r:  r@   r?  r2  rn  r  rL  r  r.  r"  r/  r'  r_   rM  rN  s   @ra   rr  rr  
  s`   %%M 			'	'N $(77 '7 sO+,	7
 !7 7 
7 7(!T !
)c )G#G 'G S/)*	G
 
GU)U14U	U55 5S 5_ 5 
C 

# 

D 
; ;
V 
 
 
r`   rr  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$ )PolyfilledFunctionVariablei6  r   rL  traceable_fnrb   .c                     0 $ r   rZ   )r]  s    ra   _get_polyfill_handlers1PolyfilledFunctionVariable._get_polyfill_handlers>  s	     	r`   rV  r   c                 ^    [        UR                  [        R                  5      5        U " XS9$ rX  rY  r\  s      ra   r^  -PolyfilledFunctionVariable.create_with_sourceC  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 functionrZ   )
r   r   r   r  r   callabler   r  rL  r  )r   r   r   handlerr  candidate_attr	candidater   s          ra   r   #PolyfilledFunctionVariable.__init__K  s    "6"--/33B;  X$5gY>STVSW"XX 
N  >Iy	****(
 #G9,OP  " ,r`   c                     U R                   $ r   )r  r   s    ra   polyfill_fn&PolyfilledFunctionVariable.polyfill_fne  s       r`   c                 0    [        U R                  SS5      $ )N*__torch_dynamo_can_constant_fold_through__F)r   rL  r   s    ra   can_constant_fold_through4PolyfilledFunctionVariable.can_constant_fold_throughi  s    OOI5
 	
r`   c                 "    U R                  5       $ r   r  r   s    ra   r  'PolyfilledFunctionVariable.get_functionn  r  r`   r   rF   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 )Nr:   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   rn  r.  r   SymNodeVariabler~  )r
  rA  s     ra   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     ra   r  r    s     ?A::<<r  )sym_num)r  r3   r   rn  r   r>   r   builtinssumr   r   r   rM   r-  r  r  r   r   create_proxyrd   sym_sumr   r  r  r  r:  )	r   r   r   r   rA  r   r   r   traceable_function_variables	            ra   r:  (PolyfilledFunctionVariable.call_functionq  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   r6   r#   r   r)   r  )r   r   r   r   r   methodr5  polyfilled_method_variables           ra   r   &PolyfilledFunctionVariable.call_method  s     :%%b77$-+f--R#8d477)!LM;; *4;; =GH%?%R'%R")77&IIr`   c                     U R                   $ r   rw  r   s    ra   rn  -PolyfilledFunctionVariable.as_python_constant  ry  r`   )r   r  rL  ) r[   r\   r]   r^   r>   r+  r-  r  cacher   r   r   r   r   r  r   r^  rO   r   propertyr  rL  r  r  r   rJ  r:  r   r   rn  r_   rM  rN  s   @ra   r  r  6  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\4S 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$ )SysFunctionVariablei  rV  r   rb   Nc                 2   > [         TU ]  " S0 UD6  Xl        g r   r   r   rV  r   rV  r   r   s      ra   r   SysFunctionVariable.__init__      "6"
r`   c                 "    [         R                  $ r   )r   BuiltinFunctionTyper   s    ra   r~  SysFunctionVariable.python_type  s    (((r`   r   rF   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_stackr  r2  r>   r   r@   r   r   r-  )r   r   exnr  tbr   s         ra   exc_infoSysFunctionVariable.exc_info  s    r//"%C,,C_5B$**23S=E !''- ''- ''-E
 &&u--r`   c                 >    U R                  U5      R                  S   $ )Nr:   )r  r   r   s     ra   	exceptionSysFunctionVariable.exception  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   )rV  r  r  r  r9  s       ra   r:  !SysFunctionVariable.call_function  sH     ::%==$$zzS]]***~~b!!r`   r  )r   rF   rb   zvariables.TupleVariable)r[   r\   r]   r^   r   r   r  r~  r  r>   r  r   r   rJ  r:  r_   rM  rN  s   @ra   r  r    s    c S T )T ).*3 * *
"#
" '
" S/)*	
"
 

" 
"r`   r  ) 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  rb   c                 "    [        SSSU 3/ S9  g )Nz!triton kernel unsupported featurer  z/Encountered triton kernel unsupported feature: rb  )r%   )r   r  s     ra   raise_unsupported&DynamoTritonHOPifier.raise_unsupported  s    7I#O		
r`   maybe_callablec                 .    [        U[        [        45      $ r   )r   rK  rP  )r   r  s     ra   is_callable DynamoTritonHOPifier.is_callable  s    79MN
 	
r`   r   c                     UR                   $ r   r  )r   r   s     ra   	get_valueDynamoTritonHOPifier.get_value  s    yyr`   gridrL   .c                     SSK Jn  [        X5      (       a  UR                  5       $ [	        SS[        U5       3S/ [        R                  QS9  g )Nr:   )rL   z/unsupported grid type for triton hop check_gridzgrid type = z;`torch.compile` only supports list-like grid for check_gridrb  )listsrL   r   r  r%   r  r   r  )r   r  rL   s      ra   
check_gridDynamoTritonHOPifier.check_grid  sK    +d--==?"I&tDzl3Y&22	r`   metar   rF   c                     UR                  5        VVs0 s H  u  pE[        R                  " X45      U_M     nnnUR                  X6/0 5      nU$ s  snnf r   )r   r>   r   r:  )r   r  r
  r   r   r   meta_vars          ra   	call_gridDynamoTritonHOPifier.call_grid
  sQ     AE

MO))"0!3M!!"j"5 Ns   #Auser_fnr   r   r:  c                 X    SSK Jn  UR                  XA5      nUR                  XBU5      nU$ )Nr:   r  )r  r  r   r:  )	r   r  r   r   r   r:  r  wrapped_user_functionr   s	            ra   call_user_defined_fn)DynamoTritonHOPifier.call_user_defined_fn  s/     	/ 1 8 8 E&44RvFr`   user_objr   c                 d    SSK Jn  Uc   eU" U[        UR                  U 5      5      " U5      nU$ )Nr:   )VariableBuilder)r  r  r)   kernel_source)r   r  r   r:  r   r  wrapped_user_objs          ra   wrap_user_defined_obj*DynamoTritonHOPifier.wrap_user_defined_obj   sB     	-~~ +
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       ra   maybe_unpack_configs)DynamoTritonHOPifier.maybe_unpack_configs2  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  r  r  )r   r   s     ra   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$ )Nr:   z7Triton kernels should be called with only a single gridr   )kernel
kernel_idxr  r  )r  r   r  r  r%  r&  r  )r   r:  r   s      ra   call_getitem!DynamoTritonHOPifier.call_getitemG  s\     ==$D	Q""I H~??**a"00	
 	
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                  " XG5      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[-        U5       S35        Ma     U
R/                  U5      nU" U[0        5      nUR2                  R5                  SUSUR6                  UUUUR9                  5       S	.5        [        R                  " US 5      $ s  snnf s  snnf s  snnf )
Nr:   )r(  r   )kernel_side_tabletriton_kernel_wrapper_mutationz.Unexpected argument type for a Triton kernel: r  r:  rZ   )r&  constant_args_idxr  tma_descriptor_metadatar   )dictsr(  r   r  r   !TMADescriptorExperimentalVariableTMADescriptorStableVariableto_metadata
get_tensorr   r>   r   *torch._higher_order_ops.triton_kernel_wrapr,  r-  r  rn  r*  rZ  rC  is_symnode_liker  repradd_constant_argsr   r   r  r&  r  )r   r:  r)  r*  r   r(  r/  r   r   combined_args_vtr,  r-  ro  non_constant_argsr.  r
  s                   ra   call_HOPDynamoTritonHOPifier.call_HOPY  s#    	- :<m((*+A A57RS  ./]]_'*#$<<>  , 9F8K8K8M
8MO!!"(!+8M 	 
	
 &++-
-!_- &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	8I	"0IIrZ   N)r[   r\   r]   r^   rJ  r   r  r>   rL  r   r   r  r   rd   fxproxyProxyr  r   r  r   r   r   r  r  r   r  r!  r'  r;  r_   rZ   r`   ra   r  r    s   
S 
U 

/ 
d 

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

 $H
 
&H
r`   r  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
\
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#  i  rI   r  rJ   r%  Nr&  r  r   rb   c                    > UR                  SUR                  S5      5      U l        [        TU ]  " S0 UD6  [
        R                  XX#5        g )Nr  r   rZ   )r   r   r  r   r    dynamo_triton_hopifier_singletoninit_variable)r   r%  r&  r  r   r   s        ra   r   TritonKernelVariable.__init__  sA     $ZZH9MN"6"(66tZVr`   r   rF   r   c                 .    [         R                  XX15      $ r   )rB  call_triton_kernelr9  s       ra   r:  "TritonKernelVariable.call_function  s     0BB
 	
r`   r  c                 .    [         R                  X/5      $ r   )rB  r'  )r   r   r  s      ra   mp_subscript_impl&TritonKernelVariable.mp_subscript_impl  s     0<<T5IIr`   r   c                 \   > US:X  a  [         R                  XXA5      $ [        TU ]  XX45      $ )Nrun)rB  call_runr   r   r   s        ra   r    TritonKernelVariable.call_method  s3     5=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$ )Nr:   r?   )r  )r  r@   rG  r  r   r   evaluate_expr)r   r  r@   r  s       ra   specialize_symbolic(TritonKernelVariable.specialize_symbolic  s4    .+ c++#**3+<+<+>??
r`   )r  )r[   r\   r]   r^   r   r.  r   r   r   r   r>   r   rJ  r:  rI  r   r   rQ  r_   rM  rN  s   @ra   r#  r#    s   
d
D= WW'*TzW9<WHKW	W
#
 '
 S/)*	

 

J#J J 
	J;#; ; ?#	;
 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$ )r1  i  data_ptrzvariables.DataPtrVariabledims
block_dimselement_sizer   rb   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   rT  rU  rV  rW  )r   rT  rU  rV  rW  r   r   s         ra   r   *TMADescriptorExperimentalVariable.__init__  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   )r  rU  r  rV  rW  )r   dims     ra   r3  -TMADescriptorExperimentalVariable.to_metadata  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   rU  r,  s   ra   r)  ?TMADescriptorExperimentalVariable.reconstruct.<locals>.<lambda>  s)    G,,6#dii.))9:r`   r:   F)	r0  rT  r  rU  rV  rW  r~  r:  r   )r   r  r   s   `` ra   r  -TMADescriptorExperimentalVariable.reconstruct  sp    	
 	!!'*@@T__@d.?.?@c$i!mU3r`   c                 .    U R                   R                  $ r   )rT  from_tensorr   s    ra   r4  ,TMADescriptorExperimentalVariable.get_tensor  s    }}(((r`   )rV  rT  rU  rW  r  )r[   r\   r]   r^   r   r>   r   r   r3  r  r4  r_   rM  rN  s   @ra   r1  r1    sn    )-) ?#) )	)
 &) ) 
)
S 

4)O ) )r`   r1  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
$ )r2  i  rG  rN   block_shaperM   r   rb   Nc                 l   > UR                  5       (       d   e[        TU ]  " S0 UD6  Xl        X l        g r   )rC  r   r   rG  rh  )r   rG  rh  r   r   s       ra   r   $TMADescriptorStableVariable.__init__  s5     !!!!"6"&r`   c                 H    [        U R                  R                  5       5      $ r   )r  rh  r  r   s    ra   r3  '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   ra   r)  9TMADescriptorStableVariable.reconstruct.<locals>.<lambda>  s    G,,0"r`   re  r   )r0  load_methodrG  r  rh  r   r2  s    `ra   r  'TMADescriptorStableVariable.reconstruct  sT    	
 	M*(  !Ar`   c                     U R                   $ r   )rG  r   s    ra   r4  &TMADescriptorStableVariable.get_tensor"  r   r`   )rh  rG  r  )r[   r\   r]   r^   r   r   r3  r  r4  r_   rM  rN  s   @ra   r2  r2    sP    	' 	' $	' 		'
 
	'
S 


C  r`   r2  c                   p   ^  \ rS rS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$ )'CreateTMADescriptorExperimentalVariablei&  rankr   rb   Nc                 B   > US;   d   e[         TU ]  " S0 UD6  Xl        g )N)r:   r   rZ   )r   r   rw  )r   rw  r   r   s      ra   r   0CreateTMADescriptorExperimentalVariable.__init__'  s&    
 v~~"6"	r`   c                 "    [         R                  $ r   r}  r   s    ra   r~  3CreateTMADescriptorExperimentalVariable.python_type0  r  r`   r   rF   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.rb  r:   rQ   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_dim0rW  r  )rT  rU  rV  rW  )r   r   rY  r%   rw  r   r#   r1  )r   r   r   r   r}  rU  rV  rW  s           ra   r:  5CreateTMADescriptorExperimentalVariable.call_function3  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`   )rw  )r[   r\   r]   r^   r.  r   r   r  r~  r   r>   r   rJ  r:  r_   rM  rN  s   @ra   rv  rv  &  so      
	"T ":
#:
 ':
 S/)*	:

 
:
 :
r`   rv  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
)!CreateTMADescriptorStableVariableip  rb   c                 "    [         R                  $ r   r}  r   s    ra   r~  -CreateTMADescriptorStableVariable.python_typeq  r  r`   r   rF   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$ )NrG  r   rh  r:   )rG  rh  )r2  )r   r   r   r   rG  rh  s         ra   r:  /CreateTMADescriptorStableVariable.call_functiont  sF     &.%7!T!W/</Ff]+DQRG*#
 	
r`   rZ   N)r[   r\   r]   r^   r  r~  r   r>   r   rJ  r:  r_   rZ   r`   ra   r  r  p  sH    "T "
#
 '
 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   rF   r   r   rb   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      (       a1  [        [        S5      S5      n[        R                  " U[        U5      $ [        R                  " XUS9$ )Nr:   z6pytree_get_node_type requires exactly 1 argument, got r   collectionsr   r   )r   r#   r   r(   rZ  r'   
TYPE_MATCHr1   r~  r   r)   r/   r>   r   r   )r   r   r   r   type_sourcer~  s         ra   r:  /PyTreeGetNodeTypeFunctionVariable.call_function  s     t9>LSQUYKX 7>>$q'..33L4K4KLM$T!W^^4K1g))+{++$\-%@,OK"((ZEE$$R[IIr`   rZ   N)r[   r\   r]   r^   r   r   r>   r   rJ  r:  r_   rZ   r`   ra   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   rF   r   r   rb   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                  " XR$                  (       + 5      $ )Nr:   r   z,tree_is_leaf requires 1 or 2 arguments, got rS   r   r   rT  )r   r#   r   r@   r   r  r   r:  r  rd   re   rf   _get_node_typer>   r   rg   rh   r   rV  )
r   r   r   r   rS   treenode_type_varsupported_nodes_varoutr   s
            ra   r:  .PyTreeTreeIsLeafFunctionVariable.call_function  s     t9q=CIMB3t9+N
 **Y(8(?(?(EFt9>1gG''))7(6:: Aw9KK..

-FB
' 	 .33KK//46

 "--b.=/SUV$$RYY77r`   rZ   r7  rN  s   @ra   r  r    sG    #8##8 '#8 S/)*	#8
 
#8 #8r`   r  c                   f   ^  \ rS 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$ ) SparseTensorCreationSkipVariablei  zc
Skip variable for sparse tensor factory functions with clear messaging regarding lack of support.
rV  r   rb   Nc                 0   > Sn[         TU ]  " U4SU0UD6  g )Nz8sparse tensor creation is not supported in torch.compiler  )r   r   )r   rV  r   r  r   s       ra   r   )SparseTensorCreationSkipVariable.__init__  s    K8v88r`   r   rF   r   c                     SSK Jn  [        U R                  S[	        U R                  5      5      n[        SSU 3SU S3/ UR                  QS9  g )	Nr   )r   r[   z$Sparse tensor creation not supportedz
function: zEtorch.compile does not support sparse tensor creation functions like zZ. Sparse tensors require specialized handling that is not yet implemented in the compiler.rb  )r  r   r   rV  rJ  r%   SPARSE_TENSOR)r   r   r   r   r   r  s         ra   r:  .SparseTensorCreationSkipVariable.call_function  s]     	)$**j#djj/B: 	*WX_W` ak k 5%334	
r`   rZ   )r[   r\   r]   r^   r   r   r   r   r>   r   rJ  r:  r_   rM  rN  s   @ra   r  r    sa    9c 9S 9T 9
#
 '
 S/)*	

 

 
r`   r  	real_implc                 P  ^  SSK Js  Jn  SSKJnJnJn  S nS/nU" XU5      u  pU" U	5      nU" U
5      nUR                  S0 45      S   nS[        S[        S	[        4U 4S
 jjnU" U S3U5      U" U S3U5      U" U S3U5      S4nT R                  R                  SX_0 5        g)a%  Emit an invoke_leaf_function node for a side-effectful function with no
tensor inputs or outputs.

The function is captured as a closure inside _LeafCallable objects and
registered as a static attribute on the graph module.  Because
invoke_leaf_function is registered as EffectType.ORDERED, effect tokens
prevent DCE and maintain execution ordering relative to other ops.

Use this when Dynamo needs to preserve a pure-side-effect call (like
setting global runtime state) in the compiled graph so that it replays
at the correct position at runtime.
r   N)_LeafCallableinvoke_leaf_functionmake_leaf_function_wrappersc                      g r   rZ   rZ   r`   ra   	fake_impl5emit_noargs_leaf_function_to_graph.<locals>.fake_impl  rI  r`   rZ   r:   	attr_namer   rb   c                 r   > TR                   R                  X5      n[        U5      UR                  l        U$ r   )r   %register_static_attr_and_return_proxyr  r=  )r  r   r>  r   s      ra   
make_proxy6emit_noargs_leaf_function_to_graph.<locals>.make_proxy  s+    		??	Os)

r`   _real_fn_fake_fn_input_specr  r:  )torch.utils._pytreere   rf   ,torch._higher_order_ops.invoke_leaf_functionr  r  r  tree_flattenrJ  r   r   r  )r   r  r   pytreer  r  r  r  captured_out_specwrapped_realwrapped_fakereal_callablefake_callable
input_specr  invoke_argss   `               ra   "emit_noargs_leaf_function_to_graphr    s    " )(  8<f!</"L ",/M!,/M$$b"X.q1Jc    	dV8$m4dV8$m4dV;'4
	K II?,@rRr`   c                   t   ^  \ rS rSrS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$ )TritonSetAllocatorVariablei,  zTrace triton.set_allocator as an invoke_leaf_function node in the
graph so that it executes at the right point at runtime, ordered by
effect tokens.rV  r   rb   Nc                 2   > [         TU ]  " S0 UD6  Xl        g r   r  r  s      ra   r   #TritonSetAllocatorVariable.__init__1  r  r`   c                 ,    [        U R                  5      $ r   )r  rV  r   s    ra   r~  &TritonSetAllocatorVariable.python_type5  r  r`   r   rF   r   c                    ^^ [        U5      S:X  a  U(       a   eUS   R                  5       mU R                  mUU4S jn[        XS5        [        R
                  " S 5      $ )Nr:   r   c                     > T" T 5        g r   rZ   )alloc_fnset_allocators   ra   r  ;TritonSetAllocatorVariable.call_function.<locals>.real_implD  s    (#r`   	set_alloc)r   rn  rV  r  r@   r   )r   r   r   r   r  r  r  s        @@ra   r:  (TritonSetAllocatorVariable.call_function8  sW     4yA~f,,7--/ 

	 	+2+F&&t,,r`   r  )r[   r\   r]   r^   r   r   r   r  r~  r   r>   r   rJ  r:  r_   rM  rN  s   @ra   r  r  ,  sm    c S T  T  -#- '- S/)*	-
 
- -r`   r  r   )r   r  r  r   r   r  r   r  r  r   r  r   collections.abcr   r   r   r   typingr   r	   r
   r   r   r   typing_extensionsr   weakrefr   rd   torch._dynamo.excr   torch._guardsr   r  r   r  r   r   r   r   bytecode_transformationr   r   r   r  r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   guardsr'   r(   r   r)   r*   r+   r,   r-   r.   r/   r0   r1   re   r2   r3   r4   r5   r6   r7   r8   r9   rZ  r;   r<   r=   r>   r  r@   user_definedrA   #torch.distributed.fsdp._fully_shardrB   ModuleNotFoundErrortorch._dynamo.codegenrC   r{  rD   rE   rF   rG   #torch._dynamo.variables.ctx_managerrH   r5  rI   rJ   r0  rK   r  rL   rM   rG  rN   rO   r|   r~   r,  r  r  r  rV   r   r  rX   	lru_cacherh   rj   r   r   rJ  r   r   r   rK  r   r   r/  r   r   r   r   rP  r1  r  rK  r`  r  r  r  r  r  r7  r  rK  r  r  r  r  r  r(  r.  r0  rj  rr  r  r  r  r  r  r  r  rB  r#  r1  r2  rv  r  r  r  r  r  r  rZ   r`   ra   <module>r     s  .      	 
   " . ( G G # %  4   3 > > V V    1
 
 
	 	 	  ' 3E
 /  L
 *5& T#s(+,
&'OP "+YK"8  D  ,=+>sCx( >	0 	 :  
 
>L \ U
UU }U 3-	U
 cNU 
#
Ur 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--:D DNOY3 OYd!73 !7H)": )X :4  :FJ;? J;Z

 
S6%= S6l " Fw- wt > 2  >F>"6 >D$$c$ $ ?
#	$
 o%&$ $NK)!9 K)\.>(B .>bv
? v
r >"6  >FV=": V=r ; 0d38n //%-c3h%7//hD(< hDV"
$8 "
J@
 @
FC CL#"/ #"L w
> w
t $8#9  4? 4n$) $)N/ BG
o G
T
 
&!J(< !JH38'; 38l
'; 
:/S/SD!/S /S 
	/Sd- -[n  s   -P' 'P32P3