
    ^jq                    0
   U d Z ddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlZddlZddlZddlmZmZ ddlmZmZ ddlmZmZmZmZmZmZ ddlmZ ddlmZ ddl Z ddl!m"Z" dd	l#m$Z$ dd
l%m&Z& ddl'm(Z(m)Z)m*Z*m+Z+ ddl,m-Z-m.Z.m/Z/ ddl0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z; ddl<m=Z=m>Z> ddl?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZI ddlJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZTmUZU ddlVmWZWmXZXmYZYmZZZ ddl[m\Z\ ddl]m^Z^ 	 ddl_m`Z` er2ddlbmcZc ddldmeZemfZfmgZg ddlhmiZi ddljmkZkmlZl ddlmmnZn ddlompZpmqZq ddlrmsZs  ededef          Ztd!Zud"Zv ewh d#      Zx ewd$h      Zyd%Zz e       Z{eeef   e|d&<    G d' d(e}      Z~ej                  d)e@fd*       Z G d+ d,      Zd-ed)efd.Zd-ed/d0d1e$dz  d2ee   d3eeef   d)eeeZf   fd4Z	 dd/d0d5ed6e$dz  d)eZfd7Zd/d0d8eeef   d)dfd9Zd:d0d8eeeZf   d;ej                  d)dfd<Zd;ej                  d=eeef   d>ed?eedf   dz  d@ee   dz  dAeeef   dz  dBeeef   dz  d)ej*                  fdCZh dDZd/d0dEed6e$dz  d>ed)eZf
dFZ G dG dHeZ      Z G dI dJe      Z G dK dLe      Z G dM dNe      Z G dO dPeZ      Z G dQ dRe      Z G dS dTe      Z G dU dVe      Z G dW dXe      Z G dY dZe      Z G d[ d\e      Zd/d0dEedef   d>ed2eeZ   d3eeeZf   d)eZfd]Z G d^ d_e      Z G d` dae      Zej<                  ej>                  ej@                  ejB                  ejD                  ejF                  ejH                  ejJ                  ejL                  h	Z G db dceZ      Z G dd dee      Z G df dge      Z G dh die      Zd)eeef   fdjZd/d0dEedef   d)e@fdkZ G dl dme      Z G dn doe      Z G dp dqeZ      Z G dr dseZ      Z G dt dueZ      ZddvljmZmZmZmZ  G dw dxe      Z e       Z G dy dzeZ      Z G d{ d|eZ      Z G d} d~eZ      Z G d deZ      Z G d deZ      Z G d de      Z G d de      Z G d de      Zd/d0deg df   d>ed)dfdZ G d deZ      Z G d deZ      Z G d deZ      Z G d deZ      Z G d deZ      Z G d deZ      Z G d deZ      Z G d deZ      Z G d deZ      Z G d deZ      Z G d deZ      Z G d deZ      Zy# ea$ r dZ`Y 	w xY w)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)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InfiniteGeneratorErrorObservedExceptionObservedGeneratorExitObservedUserStopIterationraise_observed_exceptionraise_type_errorStepUnsupportedunimplementedUnsupported)GuardBuilderinstall_guard)

AttrSourceCellContentsSourceClosureSourceConstantSourceDefaultsSourceGetItemSourceImportSourceSkipGuardSourceTypeMROSource
TypeSource)check_constant_argscheck_unspec_or_constant_argscmp_name_to_op_mappingidentityis_function2is_lru_cache_wrapper_trace_without_warning_allowedis_tensor_base_attr_getteris_wrapper_or_member_descriptoristype	make_cellunpack_iterable   )#AsPythonConstantNotImplementedErrorAttributeMutationNewValueMutationNewVariableTrackerConstantVariable)UserDefinedObjectVariable)_fsdp_param_group)	PyCodegen)&InliningGeneratorInstructionTranslatorInliningInstructionTranslatorInstructionTranslatorBase)ContextWrappingVariable)TritonGridTypeTritonKernelType)DunderDictVariable)BaseListVariableListVariable)TensorVariable_F.)bound      >   is_leaf	namespacenone_is_leafrT   zihttps://github.com/pytorch/pytorch/issues/new?&labels=oncall%3A+pt2&projects=&template=pt2-bug-report.yml_spec_cachec                       e Zd Zy)ClosureConversionErrorN)__name__
__module____qualname__     l/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/_dynamo/variables/functions.pyrY   rY      s    r^   rY   returnc            	      T    t        t        t        t        d      d      d      d      S )Ntorchutils_pytreeSUPPORTED_NODES)r'   r-   r]   r^   r_   !get_pytree_SUPPORTED_NODES_sourcerf      s(    :l73W=yI r^   c                   ,    e Zd ZdeddfdZdeddfdZy)FunctionSpecfuncr`   Nc                 p   |j                   }|j                  }|j                  | _        |j                  | _        |j                  | _        |d | j                   | _        || j                  | j
                   | _	        | j                  | j                  z   | _
        || j
                  | j
                  | j                  z    | _        | j
                  | j                  z   }|j                  t        z  r||   nd | _        || j                  rdndz  }|j                  t        z  r||   | _        y d | _        y 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)selfri   codevnoffs        r_   __init__zFunctionSpec.__init__   s   }}!44)) 22 4$"4"45!$"4"4t~~F!//$2F2FFt~~ARAR0RSnnt000'+}}z'ABsGtD%%q1,%)]]^%C"S'r^   c           
         |j                   xs d| _        |j                  xs i | _        t	        t        | j                  t        | j                         d  t        t        | j                                          | _	        y Nr]   )
__defaults__defaults__kwdefaults__
kwdefaultsdictziprv   lenrangepos_default_map)r}   ri   s     r_   update_defaultszFunctionSpec.update_defaults   si     ))/R--3  $""C$6#6#895T]]AS;TU 
r^   )rZ   r[   r\   r   r   r   r]   r^   r_   rh   rh      s+    N\ Nd N$	
L 	
T 	
r^   rh   ri   c                 \    t         j                  |       }|t        |       }|t         | <   |S N)rW   getrh   )ri   specs     r_   	_get_specr      s.    ??4 D|D! DKr^   txrH   	fn_sourceargskwargsc           
      8   t        |       }t        |      |j                  k(  rh| j                  s\|sZ|j                  sN|j
                  sB|j                  s6t        |j                        D ci c]  \  }}|t        |||          c}}S |j                  |        i }t        |      }	d}
t        |j                        D ]  \  }}|t        |      k  rt        |||         ||<   (||	v r-||j                  vrt        ||	j                  |            ||<   Y||j                  v r|j                  |   }|r6|
s4t        t!        |d      j#                  t$        j&                               d}
d }|r>t)        j*                  |j,                  |         rt.        j0                  st3        ||      }t        ||j,                  |   |      ||<   t5        d|        |t        |j                        d  }|j                  r#t        |t7        |            ||j                  <   n/|r-t5        dt        |       dt        |j                               |j                  D ]p  }||	v rt        ||	j                  |            ||<   &||j8                  v r0d }|rt3        ||d      }t        ||j8                  |   |      ||<   dt5        d|        |j
                  rt        ||	      ||j
                  <   |S |	rt5        d	t;        |	             |S c c}}w )
NFr   Tz&missing required positional argument: z#Too many positional arguments: got z, expected )is_kwz(Missing required keyword-only argument: zUnexpected keyword arguments: )r   r   rq   r   rz   r|   rw   	enumeraterv   wrap_bound_argr   r   rt   popr   r&   r'   
make_guardr%   SEQUENCE_LENGTHrB   
is_literalr   r   %skip_guards_on_constant_func_defaultsr+   	TypeErrortupler   list)ri   r   r   r   r   r   inamebarem_kwguarded_pos_defaults_lenidxdefault_sourceextrakwdefault_sources                  r_   bind_args_cachedr      s    T?D
 	D	T^^#!!!!!! %T%7%78
4 .T!W--
 	
 		B&\F$ T//0  M4s4y=%b$q'2BtHV^ ***%b&**T*:;BtHT)))&&t,C!9 y.9DD$44
 ,0(!N ++DMM#,>?@@!/	3!?%b$--*<nMBtHDTFKLLA MF T''(*+E .r5< @4	1#d)KDL^L^H_G`a
 	

 !! 	O6>%b&**T*:;BtHT__$##1)T#N %b$//$*?AQRBtHFtfMNN	O ,R84?? I 
8fGHHIS
s   .Lvalsourcec                     t        |t              r|S |st        j                  | |      S t        j                  j                  |||       S )Nr   )
isinstancer@   buildr   LazyVariableTrackercreate)r   r   r   s      r_   r   r     sI     #'
$$R-- ,,33CB3GGr^   resultc                     t        |j                               D ]+  \  }}t        |t        t        f      st        | |      ||<   - y r   )r   itemsr   r   r   r   )r   r   kvs       r_   wrap_args_kwargsr   *  s>    V\\^$ .1a%'&r1-F1I.r^   parentr~   c                     | j                   j                  }|j                  D ]<  }|j                         }||v r!|j	                  ||j                  |             |||<   > y)z
    Update `result` to add mapping from local name to new cells created
    directly by `code`, or update SideEffects in `parent` if a local cell is
    already in `result` (cell argument).
    N)outputside_effectsco_cellvarstrack_cell_new
store_cellr   )r   r   r~   r   r   new_cells         r_   init_cellvarsr   1  sa     ==--L    ..06> ##Hfjj.>?t r^   	f_globalsr   r   closurer   annotationsc                     ddl m}  || ||||      }||_        t        |t              rddlm}	 t         |	|            }|'t        |t              st        dt        |             ||_
        |S )Nr   r   pairwisez(annotations must be None or a dict, got )typesr   r   r   r   	itertoolsr   r   AssertionErrortype__annotations__)
r~   r   r   r   r   r   r   r   ri   r   s
             r_   _create_nested_fnr   F  sz     #ixAD$D+u%&8K01 :k4#@6tK7H6IJ
 	
 'DKr^   >
   __doc__rl   rZ   r[   __closure____globals____builtins__r   r   r   fnc                 R   |xr t        ||      }|r|dk(  rt        |      }d }	 t        j                  ||      }|t        v rt        ||      }|r"t        j                  j                  |||       S t        j                  | |      S # t        $ r t        t        |        Y iw xY w)Nr   r   )r'   r.   inspectgetattr_staticAttributeErrorr    fn_known_dunder_attrsgetattrr   r   r   r@   r   )r   r   r   r   subobjs        r_   fn_var_getattrr   q  s     0
640F$++ !(F5''D1 $$T",,33FFr3JJ  V,,  5 	!45s   B
 
B&%B&c            
           e Zd Z	 d dddeddf fdZd Zdee   fdZde	dz  fd	Z
d!dZd
eddfdZd
ddedee   deeef   def
 fdZdefdZdefdZdefdZdedz  fdZdeeef   fdZdej,                  fdZdefdZdej4                  fdZdefdZd
ddefdZd
ddee   deeef   defdZd
ddedefdZ d
ddeeef   fdZ!defdZ" xZ#S )"BaseUserFunctionVariableNdict_vtzDunderDictVariable | Noner   r`   c                 2    t        |   di | || _        y r   )superr   r   )r}   r   r   	__class__s      r_   r   z!BaseUserFunctionVariable.__init__  s     	"6"29r^   c                 $    ddl m}  || |||      S Nr<   )object_richcompareobject_protocolr   r}   r   otheropr   s        r_   richcompare_implz)BaseUserFunctionVariable.richcompare_impl      7!$E266r^   c                     g S r   r]   r}   s    r_   	self_argsz"BaseUserFunctionVariable.self_args      	r^   c                     | j                   S r   r   r   s    r_   
get_sourcez#BaseUserFunctionVariable.get_source      {{r^   r   rH   c                 |    | j                   %t        j                  j                  ||       | _         | j                   S r   )r   r   rL   r   r}   r   s     r_   get_dict_vtz$BaseUserFunctionVariable.get_dict_vt  s0    <<$77>>r4HDL||r^   r@   c                 \    t        j                  |t        | j                                     S r   )r@   r   repras_python_constantr   s     r_   	repr_implz"BaseUserFunctionVariable.repr_impl  s#    $$Rd.E.E.G)HIIr^   r   r   c                     |dk(  r,| j                  |      j                  |dt        |      |      S |dk(  r,| j                  |      j                  |dt        |      i       S t        |   ||t        |      |      S )N__setattr____setitem____delattr____delitem__)r   call_methodr   r   r}   r   r   r   r   r   s        r_   r  z$BaseUserFunctionVariable.call_method  s     = ##B'33M4:v  ]"##B'33BtDzSUVVw"2tT$Z@@r^   c                 6    | j                         j                  S r   get_codeco_filenamer   s    r_   get_filenamez%BaseUserFunctionVariable.get_filename      }}***r^   c                 6    | j                         j                  S r   r  co_namer   s    r_   get_namez!BaseUserFunctionVariable.get_name      }}&&&r^   c                 |    t         j                  dk\  r| j                         j                  S | j	                         S )N      )sysversion_infor  co_qualnamer  r   s    r_   get_qualnamez%BaseUserFunctionVariable.get_qualname  s.    w&==?...==?"r^   c                 <    | j                         j                  d   S )Nr   )r  	co_constsr   s    r_   get_docz BaseUserFunctionVariable.get_doc  s    }}((++r^   c                     t         r   NotImplementedErrorr   s    r_   get_globalsz$BaseUserFunctionVariable.get_globals      !!r^   c                     t         r   r  r   s    r_   r  z!BaseUserFunctionVariable.get_code  r  r^   c                     t         r   r  r   s    r_   has_selfz!BaseUserFunctionVariable.has_self  r  r^   c                     t         r   r  r   s    r_   get_functionz%BaseUserFunctionVariable.get_function  r  r^   c                 (    | j                         d   S )NrZ   )r  r   s    r_   
get_modulez#BaseUserFunctionVariable.get_module  s    !*--r^   c                    | j                  |      }|dv rr t        | d|dd               }|j                  |      r|j                  |      S t	        j
                  || j                  xr t        | j                  |            S |dk(  r|S |dk(  r|j                  |d       S |d	k(  r|j                  |d
       S |j                  |      r|j                  |      S t        t        |       y )N)rZ   r\   r   r[   rl   get_r   r   __dict__r   c                  @    t        j                  i t                     S Nmutation_type)r   ConstDictVariabler?   r]   r^   r_   <lambda>z6BaseUserFunctionVariable.var_getattr.<locals>.<lambda>  s    	33"2"4 r^   __type_params__c                  @    t        j                  g t                     S r+  )r   TupleVariabler?   r]   r^   r_   r/  z6BaseUserFunctionVariable.var_getattr.<locals>.<lambda>  s    	//"2"4 r^   )r   r   containsgetitemrB   r   r   r'   getitem_or_defaultr    r   )r}   r   r   fn_dictr   s        r_   var_getattrz$BaseUserFunctionVariable.var_getattr  s   ""2& TT4'$$tAbzl 346C%t,,#**DKKIJt{{D,I  ZN&&--  &&--  %t,,(<r^   c                     | j                         dk(  r4| j                         j                  d      rt        j                  d       S |j                  | g | j                         ||d      S )Npatch_track_step_calledztorch/optim/lr_scheduler.pyT)allow_nested_graph_breaks)r  r  endswithrB   r   inline_user_function_returnr   r}   r   r   r   s       r_   call_functionz&BaseUserFunctionVariable.call_function  sq     MMO88!!#,,-JK#**400--&dnn&&&*	 . 
 	
r^   c                     d}|t         v s|dk(  rd}n	 t        | j                         |      }t	        j
                  ||      S # t        $ r d}Y #w xY w)NFr)  T)r   hasattrr#  r  r@   r   r}   r   r   r   s       r_   call_obj_hasattrz)BaseUserFunctionVariable.call_obj_hasattr  sc     ((DJ,>F !2!2!4d; $$R00 ' s   A AAc                     i S r   r]   r   s     r_   closure_varsz%BaseUserFunctionVariable.closure_vars  	     	r^   c                      yNTr]   r   s    r_    should_allow_nested_graph_breaksz9BaseUserFunctionVariable.should_allow_nested_graph_breaks$      r^   r   )r   rH   r`   rL   )$rZ   r[   r\   r   r   r   r   r@   r   r   r   r   r   strr   r  r  r  r  r  r  r   CodeTyper  boolr!  r   r#  r%  r7  r>  rB   rB  rD  rH  __classcell__r   s   @r_   r   r     s   59:2:EH:	:7
40 FTM 
JC J$5 JA'A A ?#	A
 S/)*A 
A+c +'# '#c #,t ,"T#s(^ ""%.. ""$ ""e00 ".C .!=9 != !=F
'
 ?#
 S/)*	

 

(1-1581	1-	c?"	#$ r^   r   c                   R    e Zd ZdZej
                  Zddhej                  Z e	h d      Z
edededd fd       Z	 d/dej
                  ej                  j                   z  ded	edd
f fdZdef fdZd Zdef fdZdee   fdZdej
                  fdZdej4                  fdZdefdZdefdZdee ef   fdZ!defdZ"de#fdZ$dddee   d	ee ef   dee ef   fdZ%ddde def fdZ&ddde de'fdZ(ddded edefd!Z)dddee   d	ee ef   def fd"Z*dddee   d	ee ef   ded
z  fd#Z+defd$Z,defd%Z-defd&Z.dddee   d	ee ef   de/d ee   ee ef   f   d
z  fd'Z0dej
                  d
z  fd(Z1d)ede/ed*f   d
z  fd+Z2dedee   d
z  fd,Z3d-e4defd.Z5 xZ6S )0UserFunctionVariablez-Some unsupported user-defined global functionr   is_constant>   
optree.opstorch.utils._pytreetorch.utils._cxx_pytreeoptreevaluer   r`   c                 f    t        |j                  t        j                                | ||      S Nr   r&   r   r%   CLOSURE_MATCHclsrV  r   s      r_   create_with_sourcez'UserFunctionVariable.create_with_source=  s(    f''(B(BCD5((r^   r   Nc                 B   t        |   d	i | t        |dd      rd| _        nd| _        t	        |t
        j                  t        j                  j                  f      s!t        d| ddgt        j                         t        j                  |d|      }|| _        y )
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_inliner]   )r   r   r   rQ  r   r   r   rb   jitScriptFunctionr#   r   INFERENCE_MODEr   r   r   )r}   r   rQ  r   r   s       r_   r   zUserFunctionVariable.__init__B  s     	"6"20%8#D$D "u115993K3KLMK$PO&55	 ##B(=rBr^   c                 X    t        | t              r| j                  S t        |          S r   )r9   rP  r   r   r   r}   r   s    r_   r   z'UserFunctionVariable.as_python_constanta  s%    $,-77Nw)++r^   c                 f    | j                   r| j                   j                  |      S t        d      )NzBPython codegen not implemented for sourceless UserFunctionVariabler   reconstruct_pycoder  r}   codegens     r_   rm  z'UserFunctionVariable.reconstruct_pycodeg  .    ;;;;11'::!P
 	
r^   c                 X    t        | t              r| j                  S t        |          S r   )r9   rP  r   r   get_real_python_backed_valuerj  s    r_   rr  z1UserFunctionVariable.get_real_python_backed_valuen  s%    $,-77Nw355r^   c                     g S r   r]   r   s    r_   r   zUserFunctionVariable.self_argss  r   r^   c                     | j                   S r   r   r   s    r_   r#  z!UserFunctionVariable.get_functionv      wwr^   c                 .    | j                   j                  S r   )r   rl   r   s    r_   r  zUserFunctionVariable.get_codey  s    wwr^   c                 "    t         j                  S r   r   r   r   s    r_   python_typez UserFunctionVariable.python_type|      !!!r^   c                 4    t        | j                  dd       d uS )N__self__)r   r   r   s    r_   r!  zUserFunctionVariable.has_self  s    tww
D1==r^   c                 .    | j                   j                  S r   )r   r   r   s    r_   r  z UserFunctionVariable.get_globals  s    ww"""r^   c                 \    ddl m} | j                         t        fd|D              ryy)Nr   )BUILTIN_INLINE_WHEN_CALLEDc              3   @   K   | ]  }j                  |        y wr   )
startswith).0dfilenames     r_   	<genexpr>zHUserFunctionVariable.should_allow_nested_graph_breaks.<locals>.<genexpr>  s     J!x""1%Js   FT)torch._dynamo.trace_rulesr  r  any)r}   r  r  s     @r_   rH  z5UserFunctionVariable.should_allow_nested_graph_breaks  s)    H$$&J/IJJr^   c                 n    | j                   }|r&t        | t        j                        r| j                  }|S r   )r   r   r   UserMethodVariable	source_fn)r}   r   s     r_   r   zUserFunctionVariable.get_source  s+    jy'C'CD^^Fr^   r   rH   r   c                    | j                   rt        d      | j                  }t        |t              st        d      |j                  j                  }| j                         }t        |||||      }t        |||j                         | j                  j                  xs d}t        |      t        | j                  j                  j                        k7  rAt        dt        |       dt        | j                  j                  j                               t        t!        j"                         | j                  j                  j                  |      D ]  \  }	}
}|j                  j$                  }||v r||   }n|rYt'        t)        |      |	      }t+        |d|
      }	 t-        j.                  ||j0                  |      }|j9                  |||      }n4	 t-        j.                  ||j0                        }|j9                  d||      }|||
<    |S # t2        $ r t5        j6                         }Y pw xY w# t2        $ r t5        j6                         }Y ^w xY w)	z
        Assume `args` and `kwargs` are VariableTracker arguments for a call to
        this function, create new bindings for initial locals.
        z5bind_args should not be called on a constant functionz'Only supports regular Python functions.r]   zclosure length z# does not match co_freevars length cell_contents)freevar_nameN)rQ  r   r   r   r   r   r   root_txr   r   r   rl   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                    r_   	bind_argszUserFunctionVariable.bind_args  s/     G  "&"l+EFF--''"!"gvtVDffbkk2''%%+w<3tww//;;<< !#g, 0&&)$''*:*:*F*F&G%HJ   #OOtww//;;W 
 "	$OCt "==55L|#'-,]6-BCH(: /)%?#2#8#8 2 24I$L (;; $?#2#8#8ASAS#TL (;;D$U#F4LE"	$H ' " ?#,#<#<#>L? " ?#,#<#<#>L?s$   %!H H:H76H7:IIr   r   c           
         |dk(  rt         |   ||      S |dk(  rK| j                         }|xr t        |d      }t	        j
                  || j                  j                  |      S |t        v r5t        j                  | |t        t        | j                  |                  S | j                         }t        || j                  ||      S )Nr)  __get__py_type)r   r7  r   r'   r@   r   r   r  r3   r   GetAttrVariabler   r   r   r}   r   r   r   r   s       r_   r7  z UserFunctionVariable.var_getattr  s     :7&r400Y__&F=
69 =F"((TWW__fEE++,,dD$)?$@  "b$''6488r^   c                 Z    t        | j                  |      }t        j                  ||      S r   )r@  r   r@   r   rA  s       r_   rB  z%UserFunctionVariable.call_obj_hasattr  s'     $'$$R00r^   objownerc                     |j                   xr* t        |j                   | j                  j                        }t	        | j                  || j                   |      S )Nr  r   )r   r'   r   rZ   r  )r}   r   r  r  r   s        r_   tp_descr_get_implz&UserFunctionVariable.tp_descr_get_impl  s@     H
3::tww7G7G H!$''3$++fUUr^   c           
      T   | j                   t        j                  j                  u r	 |D cg c]  }|j	                          }}|j                         D ci c]  \  }}||j	                          }}}t        j                  j                  |i |j                  }	t        j                  |	      S | j                   t        j                  j                  u r	  t        j                  | j                         j                  |i |}|j                  d   j	                         }t!        |t"              st%        dt'        |             t        j(                  |      S | j                   t        j                  j,                  u r	  t        j                  | j                         j                  |i |}|j/                          |j                  d   }|j                  d   }t!        |t0              r|j	                         }t!        |t0              r|j	                         }t        j2                  ||      S | j                   t        j                  j4                  j6                  u r0d	|j8                  _        t        j<                  j?                  d       S | j                   t        j                  j@                  u r" t        j                  | j                         j                  |i |}|jB                  d
   }t!        |tD              sS|jG                         }d|jH                   d}tK        dd| jL                   d| d| d|g tN        jP                         t!        |tR              s,|jU                         }d| d}tK        d|  |d| ddg       |j                   }t        jV                  |t        j                  jX                  jZ                        S | j\                  r't_        || j                   | jU                         ||      S |j8                  j`                  jb                  st| j                   t        j                  jd                  jf                  u rDt        j                  jh                  jk                  |      5  tl        |   |||      cd d d        S tq        |j8                  j`                  dd       dk(  r|j8                  j`                  jr                  sq	 d
dl:m;} |h| j                   |jx                  |jz                  fv rDt        j                  jh                  js                  |      5  tl        |   |||      cd d d        S | j}                  |||      }||S tl        |   |||      S c c}w c c}}w # t        $ r}
t        d| d|       |
d }
~
ww xY w# t*        $ r}
t        d| d|       |
d }
~
ww xY w# t*        $ r}
t        d| d|       |
d }
~
ww xY w# 1 sw Y   [xY w# t*        $ r d }Y !w xY w# 1 sw Y   xY w)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)error_on_graph_break must be a bool, got z\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(, )r`  z(Applying `nonstrict_trace` to function <zk>; however, `nonstrict_trace` currently requires the function to be defined outside `torch.compile` region.zLimitation of `nonstrict_tracezmake sure definition of z is outside z`torch.compile` regionkinddescriptionz!torch.utils.checkpoint.checkpoint)	FSDPState)?r   rb   _dynamopatch_dynamo_configr   r   changesr   DynamoConfigPatchVariabler=   RuntimeErrorr  r   	signaturebind	argumentsr   rL  r   r   ErrorOnGraphBreakVariable	Exceptionoverride_cudagraphsapply_defaultsr@   CudagraphOverrideVariablebytecode_debugger
breakpointr   _emit_debugger_breakpointrB   r   nonstrict_tracer   r   rz  rZ   r#   rV  r   
USER_ERRORrP  r  TorchInGraphFunctionVariableAllowInGraphKindNONSTRICT_TRACErQ  invoke_and_store_as_constantcurrent_tracer,unsafe_allow_externally_visible_side_effectsrc   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  erQ   r  r  r  fn_vartypmsgfn_namer   r  tree_map_resultr   s                         r_   r>  z"UserFunctionVariable.call_function  s    77emm777BFG3c446G
GBH,,. 6>c3C//11     --;;#/'  !::7CC WW:::7))$''277HH',*($$& % ""6=(CDI]D^C_`  !::;OPP WW9997))$''277HH$$&ooe,ooe,c?3002Cc?3002C ::3DD WW77BBB26BII/--44T::WW5553G%%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  /DGGT]]_dF 
 		((UU}}""[[\ ++^^ ? w,Rv>? ? BII,,mTB23II,,FF!U $&&''5 * ]]//II"M C 70T6BC C <<RvN&""w$Rv66m H  7 "!F*VH6 	"  "!F*VH6    "!F*VH6 h? ?  ! 	!C Cs   V VV V4A V BV9 B2W W?3X XV 	V6V11V69	WWW	W<%W77W<?X	XXX'c                    | j                  |||      }||\  }}}n| }|}|}t        |t              xr |j                         }t        |t              xr |j	                         }	|s|	sy h |t
        z
  ry t        |      dk  ry |d   }
|d   }|dd  }|	r|j                  |||
||d      S |j                  |||
||      S )Nr   r   r<   r]   )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                r_   r  z2UserFunctionVariable._maybe_call_tree_map_fastpath|  s    222tVD:A7KK M$O {$89 4113 	
 {$89 >;;= 	
 4_ ::}!q!"1%
QR  55 6   ++ r^   c                     t        | j                  dd       dk(  xr# t        | j                  dd       | j                  v S )NrZ   tree_mapr[   r   r   _TREE_MAP_MODULESr   s    r_   r  z*UserFunctionVariable._is_tree_map_function  s=    DGGZ.*< Ot48N8NN	
r^   c                     t        | j                  dd       dk(  xr# t        | j                  dd       | j                  v S )NrZ   tree_map_with_pathr[   r  r   s    r_   r  z4UserFunctionVariable._is_tree_map_with_path_function  s>    DGGZ.2FF Ot48N8NN	
r^   c                     t        | j                  dd       dk(  xr# t        | j                  dd       | j                  v S )NrZ   tree_map_onlyr[   r  r   s    r_   _is_tree_map_only_functionz/UserFunctionVariable._is_tree_map_only_function  s=    DGGZ./A Ot48N8NN	
r^   c           	      2   | j                         sy t        |      dk7  ry h |t        z
  ry |\  }}}| j                  |      }|y | j	                         }|y t        ||t        |dd             }	t        j                  ||      }
|
|	|gt        |      fS )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              r_   r  z0UserFunctionVariable._rewrite_tree_map_only_call  s     ..0t9>V966*.'vx99-H  ::<$468T2

 ,11"6GH >8"<d6lJJr^   c                     t        | j                  dd       }|sy t        j                  j	                  |      }|y t        |dd       }t        |t        j                        r|S y )Nr[   r  )r   r   r  modulesr   r   r   r   )r}   module_namemoduler  s       r_   r  z.UserFunctionVariable._lookup_tree_map_function  sZ    dgg|T:->6:t4h 2 23Or^   selector.c                     |j                         sy 	 |j                         }| j                  |      }|sy t	        d |D              sy t        t        j                  |            S # t        $ r Y y w xY w)Nc              3   <   K   | ]  }t        |t                y wr   )r   r   )r  r  s     r_   r  zDUserFunctionVariable._extract_tree_map_only_types.<locals>.<genexpr>  s     >S:c4(>   )is_python_constantr   r  _flatten_type_specallr   r   fromkeys)r}   r  	raw_value	flatteneds       r_   r  z1UserFunctionVariable._extract_tree_map_only_types  sw     **,	 335I ++I6	>I>>T]]9-.. # 		s   A( (	A43A4c                 r   t        |t              r|gS t        |t              r1g }|D ](  }| j                  |      }| y |j	                  |       * |S t        t        dd       }|Pt        ||      rDg }t        j                  |      D ](  }| j                  |      }| y |j	                  |       * |S y )N	UnionType)	r   r   r   r
  extendr   r   typingget_args)r}   rV  	collectedentryflat
union_types         r_   r
  z'UserFunctionVariable._flatten_type_spec  s    eT"7NeU#$&I '..u5<  &	'
 UK6
!j
&CI/ '..u5<  &	'
 r^   r   c                 j    t        |t        j                        xr | j                  |j                  u S r   )r   r   rP  r   r}   r   s     r_   is_python_equalz$UserFunctionVariable.is_python_equal  s*    %!?!?@XTWWPUPXPXEXXr^   )F)7rZ   r[   r\   r   r   r   _cpython_typer   _nonvar_fields	frozensetr  classmethodr   r]  rb   rf  rg  rL  r   r   rm  rr  r   r@   r   r#  rK  r  r   rz  r!  r   rJ  r  rH  r   r   r  r7  rB   rB  r  r>  r  r  r  r  r   r  r  r  r
  objectr  rM  rN  s   @r_   rP  rP  (  sw   7 &&M 	 
"	0	0N "	
 )s )C )<R ) ) "!9!99  	
 
>,C ,
6c 6
40 e00  %..  "T ">$ >#T#s(^ #$ F C+C ?#C S/)*	C
 
c?"	#CJ9-9589	9 1-1581	1
V'
V 
V 	
V
 

V7'7 ?#7 S/)*	7
 
7B2'2 ?#2 S/)*	2
 
4	2h
t 

 

D 
$K'$K ?#$K S/)*	$K 	"!o%&(	

 	$KL
5+=+=+D 
/'/	tSy	D	 /" T
T0A ,YV Y Yr^   rP  c                   D     e Zd ZdZdddee   deeef   def fdZ xZ	S )InspectSignatureVariablea  
    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   rH   r   r   r`   c                 ^   t        |      dk(  r|s|d   }d }t        |t        t        f      r|j	                         }|]||j
                  j                  v r|j
                  j                  |   S t        | !  |||      }||j
                  j                  |<   |S t        | !  |||      S rk   )	r   r   rP  r  r#  r   signature_cacher   r>  )r}   r   r   r   
target_arg	cache_keyr   r   s          r_   r>  z&InspectSignatureVariable.call_function*  s     t9>&aJI*';=O&PQ&335	$		 9 999944Y??.r4@7=		)))4w$Rv66r^   
rZ   r[   r\   r   r   r@   r   rJ  r>  rM  rN  s   @r_   r!  r!    sG    7'7 ?#7 S/)*	7
 
7 7r^   r!  c                        e Zd Zdhej                  Zdeedf   dededdf fdZ	defdZ
d	edefd
Zdddee   deeef   defdZ xZS )r  r  .r  r   r`   Nc                 @    t        |   di | || _        || _        y r   )r   r   r  r  )r}   r  r  r   r   s       r_   r   z$TreeMapOnlyFunctionVariable.__init__I  s$     	"6"*r^   c                     t         S r   r   r   s    r_   rz  z'TreeMapOnlyFunctionVariable.python_typeS      r^   nodec                     	 |j                         t        fd| j                  D              S # t        $ r Y yw xY w)NFc              3   6   K   | ]  }t        |        y wr   )
issubclass)r  allowed	node_types     r_   r  zDTreeMapOnlyFunctionVariable._matches_allowed_type.<locals>.<genexpr>[  s     Tg:i1Ts   )rz  r  r  r  )r}   r+  r0  s     @r_   _matches_allowed_typez1TreeMapOnlyFunctionVariable._matches_allowed_typeV  sC    	((*I TASASTTT # 		s   1 	==r   rH   r   c                    |s| j                   j                  |||      S |d   }| j                  |      r| j                   j                  |||      S t        |      dk7  s|r| j                   j                  |||      S |S )Nr   r<   )r  r>  r1  r   )r}   r   r   r   leafs        r_   r>  z)TreeMapOnlyFunctionVariable.call_function]  s}     ;;,,Rv>>Aw%%d+;;,,Rv>>t9>V ;;,,Rv>>r^   )rZ   r[   r\   r   r  r   r   r@   r   r   rz  rL  r1  r   r   rJ  r>  rM  rN  s   @r_   r  r  C  s    	!	0	0N
T3Y'   	
 
T U/ Ud U' ?# S/)*	
 
r^   r  c            
           e Zd Zej                  Zdej                  deee	f   ddde	ddf
 fdZ
dej                  fd	Zdefd
ZdefdZdefdZdefdZdefdZ defdZeZd#dZdeee	f   fdZdefdZdddedefdZdddefdZdddedefdZdddefdZded   fdZdddeddfdZ defdZ!defd Z"ddded!e#e   deeef   def
 fd"Z$ xZ%S )$LocalGeneratorObjectVariabler~   r   inline_tracerrF   r   r`   Nc                 N    t        |   di | || _        || _        || _        y r   )r   r   r~   r   r6  )r}   r~   r   r6  r   r   s        r_   r   z%LocalGeneratorObjectVariable.__init__s  s+     	"6"	"*r^   c                     | j                   S r   )r~   r   s    r_   r  z%LocalGeneratorObjectVariable.get_code  s    yyr^   c                 6    | j                         j                  S r   r  r   s    r_   r  z)LocalGeneratorObjectVariable.get_filename  r	  r^   c                 6    | j                         j                  S r   r  r   s    r_   r  z%LocalGeneratorObjectVariable.get_name  r  r^   c                     t        d      )Nr#  r  r   s    r_   r#  z)LocalGeneratorObjectVariable.get_function  s    !.11r^   c                      yNFr]   r   s    r_   r!  z%LocalGeneratorObjectVariable.has_self      r^   c                 "    | j                         S r   )r  r   s    r_   rZ   z%LocalGeneratorObjectVariable.__name__  s    }}r^   c                 V    | j                   j                   d| j                          dS )N(r  )r   rZ   r  r   s    r_   __str__z$LocalGeneratorObjectVariable.__str__  s'    ..))*!DMMO+<A>>r^   c                    ddl m} ddlm}m} |j
                  } ||      } ||      } ||      }|5  |5  |5  | j                  }	|	j                  st        ||       | _	        t        j                  | j                        j                  |       d d d        d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)Nr   )"disallow_side_effects_in_generator) save_and_restart_speculation_log(temporarely_allow_writes_to_output_graph)torch._dynamo.side_effectsrD  torch._dynamo.symbolic_convertrE  rF  r   r6  generator_exhaustedr;   remaining_itemsr   ListIteratorVariablereconstruct)
r}   ro  rD  rE  rF  r   savedisallowtemptracers
             r_   rL  z(LocalGeneratorObjectVariable.reconstruct  s    Q	

 ZZ/35b97; 	V8 	VT 	V''F--'6r4'@$**4+?+?@LLWU		V 	V 	V 	V 	V 	V 	V 	V 	Vs:   CB8AB,B8C,B51B88C	=CCc                     | j                   S r   r   r   s    r_   r  z(LocalGeneratorObjectVariable.get_globals      ~~r^   c                 "    t         j                  S r   )r   GeneratorTyper   s    r_   rz  z(LocalGeneratorObjectVariable.python_type  s    """r^   r   rH   r  c           	      &   | j                   }| j                         rt        t        |       	 |j	                  |       |j                         S # t        $ r}d|_        |d }~wt        $ r+ t        ddddt         dgt        j                         Y y t        $ rq}t        j                  j                   j#                  | j%                                d|_        |j(                  s!|j*                  s|xj,                  dz  c_         d }~ww xY w)	NTzinfinite generator detected z}Dynamo traced the YIELD_VALUE bytecode too many times. This could mean that we have attempted to trace an infinite generator.zLIf you are sure that your generator is not infinite, please report a bug at .r`  zA

Skipping frame due to graph break in a generator's next() call.)r6  _is_generator_exhaustedr    StopIterationpushinline_call_r   rI  r   r#   PT2_ISSUE_TRACKER_URLr   r  r$   rb   r  
eval_frame	skip_coder  
skip_frame	one_graphr  r  )r}   r   r  rP  r  s        r_   gen_send_ex2z)LocalGeneratorObjectVariable.gen_send_ex2  s     ##'')$]B7	 KK&&((  	)-F&G% 	5I ccxbyyz{&11	  	MM$$..t}}?AL<<(?(?^^	s*    A 	D	A!!4DDA,DDc                 L    | j                  |t        j                  d             S r   )rb  rB   r   r   s     r_   tp_iternext_implz-LocalGeneratorObjectVariable.tp_iternext_impl  s!      %5%<%<T%BCCr^   r   c                     || j                         j                  v rt        j                  d      S t        j                  d      S NTF)rz  r)  rB   r   r}   r   r   s      r_   rB  z-LocalGeneratorObjectVariable.call_obj_hasattr  s=     4##%...#**400&&u--r^   c                     | S r   r]   r   s     r_   tp_iter_implz)LocalGeneratorObjectVariable.tp_iter_impl  s    r^   Fc                      yr=  r]   r   s    r_   rH  z=LocalGeneratorObjectVariable.should_allow_nested_graph_breaks  r>  r^   excc                     | j                   }	 |j                  |d       y # t        $ r}|j                  |       Y d }~y d }~ww xY w)NT)set_context)r6  _raise_exception_variabler   exception_handler)r}   r   rk  rP  r  s        r_   _setup_exceptionz-LocalGeneratorObjectVariable._setup_exception  sJ     ##	(,,Sd,C  	( $$Q''	(s   " 	AAAc                     t         j                  dk  rd}nd}| j                  d u xs | j                  j                  |k(  S )Nr  r   r<   )r  r  r6  instruction_pointer)r}   
first_insts     r_   _is_generator_just_startedz7LocalGeneratorObjectVariable._is_generator_just_started  sG    g%JJ$& D!!55C	
r^   c                 0    t        | j                  dd      S )NrI  F)r   r6  r   s    r_   rY  z4LocalGeneratorObjectVariable._is_generator_exhausted  s    t))+@%HHr^   r   c                    |dk(  rR| j                         r-t        |      r"t        d |D              st        t        |       | j                  ||d         S |dk(  r| j                  }| j                         s| j                         r&d|_        t        j                  j                  d       S 	 | j                  |t        j                  t        g              t        j                   dk\  r?|j"                  j$                  dk(  r&d|_        t        j                  j                  d       S 	 |j)                         rt        t*        |       n|d	k(  r| j                  }	 | j                  |t        |      d
k(  r|d   n|d          |j)                         }t5        dt6        fi       }	 | j                  |t        j                  |g              |j)                          t        t*        |       |S t:        | y  ||||      S # t&        $ r) d|_        t        j                  j                  d       cY S w xY w# t&        $ r) d|_        t        j                  j                  d       cY S t,        $ r' |j.                  t1        d      d |j.                  cY S w xY w# t2        $ r  w xY w# t9        |      $ r Y |S w xY w)Nsendc              3   <   K   | ]  }|j                           y wr   )is_constant_none)r  r  s     r_   r  z;LocalGeneratorObjectVariable.call_method.<locals>.<genexpr>  s     Bc3//1Br  r   closeT)r     CALL_INTRINSIC_1z6expected symbolic_result to be set after StopIterationthrowr  r<   __InternalThrowException)rt  r   r  r    r   rb  r6  rY  rI  r   rB   r   rp  ExceptionVariableGeneratorExitr  r  next_instructionopnamer   r\  r  r   symbolic_resultr   r   r   r  r   r   r  )	r}   r   r   r   r   rP  retvalexc_typer   s	           r_   r  z(LocalGeneratorObjectVariable.call_method  s    6> ..0SY BTBB,Y;$$Ra11W_ ''F..0D4P4P4R-1* 1188>>?%%	33M2F( $$///66:LL15F.$55<<TBB. &&(,\2> W_ ''F
 %%bSY!^$q'aQ
 ((*F| 6bIH?%%b)*E*EhPR*ST##%
 )v>Mw"2tT6::i ) ?-1* 1188>>? ) ?-1* 1188>>, 	.
 ))1(P  ---	.2 % N 1:  MsI   1A;H . H; !(J ,6J, /H87H8;/J,,JJJ),J>=J>ro  rE   r`   N)&rZ   r[   r\   r   rU  r  rK  r   rJ  r   r   r  r  r  r   r#  rL  r!  rB  __repr__rL  r  r   rz  r@   rb  rd  rB   rB  ri  r	   rH  rp  rt  rY  r   r  rM  rN  s   @r_   r5  r5  o  s   ''M
+nn
+ S>
+ @	
+
 
+ 

+%.. +c +'# '2e 2$ # ? ? HV$T#s(^ #T ##-#4C#	#JD#> D? D.-.58.	.:  
'%. 	(-	(4C	(		(
D 
I It;'t; t; ?#	t;
 S/)*t; 
t; t;r^   r5  c                       e Zd ZdZy)4ContextlibContextManagerLocalGeneratorObjectVariablez
    .. 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.
    N)rZ   r[   r\   r   r]   r^   r_   r  r    s    r^   r  c            	            e Zd ZdZdefdZeddedededdf fd	Z	d
e
defdZdej                  fdZdee
ef   fdZdefdZdddee   dee
ef   ddfdZdddee   dee
ef   defdZdefdZ xZS )LocalGeneratorFunctionVariablezxfunctions that behaves like iterators

    .. note::

        This is a wrapper around (Nested)UserFunctionVariable
    r`   c                 "    t         j                  S r   ry  r   s    r_   rz  z*LocalGeneratorFunctionVariable.python_type  r{  r^   )generator_clsvtr  r   Nc                @    t        |   di | || _        || _        y r   )r   r   r  r  )r}   r  r  r   r   s       r_   r   z'LocalGeneratorFunctionVariable.__init__  s$     	"6"*r^   r   c                 v    || j                   j                  v rt        | |      S t        | j                  |      S r   )r   r)  r   r  )r}   r   s     r_   __getattr__z*LocalGeneratorFunctionVariable.__getattr__  s2    4>>***4&&tww%%r^   c                 6    | j                   j                         S r   )r  r  r   s    r_   r  z'LocalGeneratorFunctionVariable.get_code      ww!!r^   c                 6    | j                   j                         S r   )r  r  r   s    r_   r  z*LocalGeneratorFunctionVariable.get_globals  s    ww""$$r^   c                 6    | j                   j                         S r   )r  r!  r   s    r_   r!  z'LocalGeneratorFunctionVariable.has_self  r  r^   r   rH   r   rG   c                 8    ddl m}  |j                  || ||      S )Nr   )rG   )rH  rG   build_inline_tracer)r}   r   r   r   rG   s        r_   _build_inline_tracerz3LocalGeneratorFunctionVariable._build_inline_tracer  s*     	Q@,@@	
 	
r^   c                    t        | j                  j                               s2t        dt	        | j                  j                               dddg       | j                  |t        |      |      }| j                  j                         }| j                  j                         }t        j                  dk\  r|j                          | j                  |||| j                        S )Nz'non-generator contextlib.contextmanagerzvCannot compile function decorated with `@contextlib.contextmanager` that is not a generator, i.e. does not use `yield`z5Use `yield` in the function body instead of `return`.z2Remove the `@contextlib.contextmanager` decorator.r`  r  r   )r   r  r  r#   rJ  r  r   r  r  r  r\  r  r   )r}   r   r   r   r6  r~   r   s          r_   r>  z,LocalGeneratorFunctionVariable.call_function  s     DGG,,./ADGG,,./. LH	 11"d4j&Iww!GG'')	w&&&(!!;;	 " 
 	
r^   c                 6    | j                   j                         S r   )r  rr  r   s    r_   rr  z;LocalGeneratorFunctionVariable.get_real_python_backed_value  s    ww3355r^   )rZ   r[   r\   r   r   rz  r5  r   r   r   rJ  r  r   rK  r  r   r  rL  r!  r   r@   r  r>  r  rr  rM  rN  s   @r_   r  r    s   "T " ;		+$	+ 		+
 	+ 
	+& & &"%.. "%T#s(^ %"$ "
'
 ?#
 S/)*	

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

 

@6f 6r^   r  c                   \     e Zd ZdZdededdf fdZddd	ee   de	e
ef   dd
f fdZ xZS )3FunctionDecoratedByContextlibContextManagerVariablezm
    .. note::

        This is only used when the function is annotated with @contextlib.contextmanager
    r  r   r`   Nc                 2    t        |   |fdt        i| y )Nr  )r   r   r  )r}   r  r   r   s      r_   r   z<FunctionDecoratedByContextlibContextManagerVariable.__init__#  s%    	
N	
 	
r^   r   rH   r   rF   c                     t         |   |||      }t        |t        j                  j
                  j                        st        dt        |             d|_	        |S )Nz5expected InliningGeneratorInstructionTranslator, got T)
r   r  r   rb   r  symbolic_convertrF   r   r   is_generator_from_ctx_manager)r}   r   r   r   rP  r   s        r_   r  zHFunctionDecoratedByContextlibContextManagerVariable._build_inline_tracer*  sb     -b$?MM**QQ
 !GV~V  04,r^   )rZ   r[   r\   r   r   r   r   r   r@   r   rJ  r  rM  rN  s   @r_   r  r    sb    
3 
s 
t 
' ?# S/)*	
 
2 r^   r  c                       e Zd ZdZej
                  Z	 ddedef   de	de
dz  deddf
 fd	Zdefd
Zdedeeef   fdZdee	   fdZdeej
                     fdZdddee	   deee	f   de	f fdZdddede	f fdZdefdZ xZS )r  z$Some unsupported user-defined methodNr   .r  r  r   r`   c                     t        |   dd|i| || _        || _        |3|j	                  d      !t        |j	                  d      d      | _        y y y )Nr   r   __func__r]   )r   r   r  r  r   r'   )r}   r   r  r  r   r   s        r_   r   zUserMethodVariable.__init__F  s^     	)B)&)" H!5!A'

8(<jIDN "Br^   c                 h    | j                   j                   d| j                   d| j                   dS )NrA  r  r  )r   rZ   r   r  r   s    r_   r  zUserMethodVariable.__repr___  s.    ..))*!DGG9BtxxjBBr^   r   c                     | j                   r2|j                  j                  | j                         }t        |      dfS ddlm}  ||| j                        \  }}t        | j                        }||z  }|dk(  rd}||fS )NFr<   )generic_hash_implr(  )r   r   resolve_source_valuehashr   r  r  r   )r}   r   real_valr  	self_hash	self_fake	func_hashhs           r_   	hash_implzUserMethodVariable.hash_implb  sw     ;;yy55dkkBH>5((60TXX>	9M		!7A)|r^   c                     | j                   gS r   r  r   s    r_   r   zUserMethodVariable.self_argsr      zr^   c                 "    t         j                  S r   r   
MethodTyper   s    r_   rz  zUserMethodVariable.python_typeu      r^   rH   r   c                 2   ddl m}m}  || j                        rjg | j	                         |}t        j                  | j                  t
        j                  j                  j                        }|j                  |||      S  || j                        rjg | j	                         |}t        j                  | j                  t
        j                  j                  j                        }|j                  |||      S |j                  j                         rt        | j                  t
        j                         rt#        | j                  dd      }||j%                  d      r|dk7  s| j&                  r| j                  j)                  || j                  j*                  t-        |      || j&                        S t.        c| j                  t.        j0                  j2                  u r=t        j4                  | j                        j                  || j                  g||      S | j&                  rQt#        | j                  j6                  | j                  j*                        }	t9        ||	| j;                         ||      S t<        
| )  |||      S )	Nr   )is_leaf_functionis_nonstrict_trace_callabler  r[   rW  z	torch.nn.ztorch.nn.utils.parametrize)constant)trace_rulesr  r  r   r   r   r  rb   r  r  r>  LEAF_FUNCTIONr   is_root_tracerr   r  NNModuleVariabler   r  rQ  r  rZ   r   rD   FSDPParamGroupuse_training_stateTorchCtxManagerClassVariablerV  r  r  r   )r}   r   r   r   r  r  	call_argsvarmodule_attrr   r   s             r_   r>  z UserMethodVariable.call_functionx  s    	P&tww/2$..*2T2I88ioo>>NNC $$RF;;DGG$2$..*2T2I88ioo>>LLC $$RF;; 99##%*HHi00+
 "$''<<K '**;7#??##xx++(($t*ftGWGW ,   ),;;NNN99$''BPPTXX%%v  )9)9:B/BvVVw$Rv66r^   r   c                     |dk(  r| j                   S |dk(  r+t        j                  || j                  | j                        S t
        |   ||      S )Nr}  r  )r  r@   r   r   r  r   r7  r}   r   r   r   s      r_   r7  zUserMethodVariable.var_getattr  sO     :88O: #((TWWdnnEEw"2t,,r^   c                     | j                   S r   ru  r   s    r_   rr  z/UserMethodVariable.get_real_python_backed_value  rv  r^   r   )rZ   r[   r\   r   r   r  r  r   r   r@   r   r   rJ  r  r   intrL  r  r   r   r   rz  r   r>  r7  rr  rM  rN  s   @r_   r  r  @  s   . $$M $(	JS#XJ J D=	J
 J 
J2C# CC E#t)$4  40  T%"2"23  B7'B7 ?#B7 S/)*	B7
 
B7H
--
-58
-	
-c r^   r  c                   d     e Zd Zdedddeddf fdZdd	d
ee   dee	ef   def fdZ
ddZ xZS )WrappedUserMethodVariablewrappedrb  rI   r   r`   Nc                     |j                  dd        |j                  dd        t        |   |j                  |j                  fi | || _        || _        y )Nr   r  )r   r   r   r   r  r  rb  r}   r  rb  r   r   s       r_   r   z"WrappedUserMethodVariable.__init__  sK     	

4

5$W[[;F;r^   r   rH   r   c                 R   t         j                  rNt        t        j                        }|j                  || j                  | j                  gt        |      z   |      S | j                  j                  |       t        |   |||      }| j                  j                  |       |S r   r   nested_graph_breaksrP  r   _fn_with_ctxr>  rb  r  r   enterr   exitr}   r   r   r   
wrapper_fnr   r   s         r_   r>  z'WrappedUserMethodVariable.call_function       %%-i.D.DEJ++T\\4<<04:=v  	2&r48"r^   c                      j                   fd         j                         j                  t        dd             y )Nc                  (      j                         S r   rb  ro  r}   s   r_   r/  z7WrappedUserMethodVariable.reconstruct.<locals>.<lambda>      gdll&; r^   r<   Fadd_push_nullr  extend_outputr   rn  s   ``r_   rL  z%WrappedUserMethodVariable.reconstruct  4    ;<21e<=r^   r  )rZ   r[   r\   r  r   r   r   r@   r   rJ  r>  rL  rM  rN  s   @r_   r  r    sm    
#
 +
 	

 

' ?# S/)*	
 
 >r^   r  c                   d     e Zd Zdedddeddf fdZdd	d
ee   dee	ef   def fdZ
ddZ xZS )WrappedUserFunctionVariabler  rb  rI   r   r`   Nc                 z    |j                  dd        t        |   |j                  fi | || _        || _        y )Nr   )r   r   r   r   r  rb  r  s       r_   r   z$WrappedUserFunctionVariable.__init__  s7     	

4.v.r^   r   rH   r   c                 R   t         j                  rNt        t        j                        }|j                  || j                  | j                  gt        |      z   |      S | j                  j                  |       t        |   |||      }| j                  j                  |       |S r   r  r  s         r_   r>  z)WrappedUserFunctionVariable.call_function  r  r^   c                      j                   fd         j                         j                  t        dd             y )Nc                  (      j                         S r   r  r  s   r_   r/  z9WrappedUserFunctionVariable.reconstruct.<locals>.<lambda>  r  r^   r<   Fr  rn  s   ``r_   rL  z'WrappedUserFunctionVariable.reconstruct  r  r^   r  )rZ   r[   r\   rP  r   r   r   r@   r   rJ  r>  rL  rM  rN  s   @r_   r  r    sm    	%	 +	 		
 
	' ?# S/)*	
 
 >r^   r  c                    dt         dt        ffd}|D cg c]
  } ||       }}|j                         D ci c]  \  }}| ||       }}} ||i |}	| j                  j	                  |	t                    S c c}w c c}}w )Nxr`   c           
         | j                         rt        d|       j                         S t        | t              r@| j
                  (t        | j                  t        j                               | j                  S 	 | j                         S # t        $ rG t        dd dt        |       j                   dt        |       j                   d ddd	g
       Y y w xY w)NrO   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 constantsr`  )	is_tensorr   get_real_valuer   rC   r   r&   r   r%   ID_MATCHrV  r   r=   r#   r   rZ   )r  r   s    r_   convertz-invoke_and_store_as_constant.<locals>.convert  s    ;;=(!,;;==a2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   r  r  r   r   ress
     `       r_   r  r    s    ? s , !%%1GAJ%D%(.71am7F7
d
f
C99,,d# -   &7s
   BBc                   ,    e Zd Zdhej                  Z	 d#dededeeef   dedz  dedz  dedz  dedz  d	ed
df fdZ	d
e
e   fdZd
ej                  fdZded
df fdZd
ej                   fdZd
eej                     fdZd#dee   dz  d
ej                  fdZd
efdZdee   d
ej                  fdZddded
ef fdZd
efdZddded
ef fdZddded
ef fdZd
efdZd
eeef   fdZ ddd e
e   d	eeef   d
eeef   fd!Z!d$d"Z" xZ#S )%NestedUserFunctionVariabler   Nr  r~   r   r   r   
wrapped_fnr   r`   c                 J   |j                  d      |j                  t                      t        	|   di | t        |j                         t              s%t        dt        |j                                      t        |j                         t        j                        s%t        dt        |j                                      t        |t              st        dt        |             || _        || _        || _        || _        || _        || _        || _        y )Nr-  r,  zfn_name must be a str, got zcode must be a CodeType, got zf_globals must be a dict, got r]   )r   updater>   r   r   r   r   rJ  r   r   r   rK  r   r  r~   r   r   r   r   r  )
r}   r  r~   r   r   r   r   r  r   r   s
            r_   r   z#NestedUserFunctionVariable.__init__>  s    ::o&.MM(<(>M?"6"'446< -d73M3M3O.P-QR  $113U^^D /T5L5L5N0O/PQ  )T* #A$y/AR!STT	" $2<r^   c                     g S r   r]   r   s    r_   r   z$NestedUserFunctionVariable.self_args`  r   r^   c                 "    | j                         S r   )r#  r   s    r_   r   z-NestedUserFunctionVariable.as_python_constantc  s      ""r^   r   r@   c                     	 t         |   |      S # t        $ rG}t        dd| j                  j                          d| dg t        j                         Y d }~y d }~ww xY w)Nz8repr() on nested function with non-constructible closurezrepr() on nested function z: zDynamo could not safely evaluate repr() for this nested function because it could not reconstruct its closure as Python constants.r`  )r   r   rY   r#   r  r   r   SUPPORTABLE)r}   r   r  r   s      r_   r   z$NestedUserFunctionVariable.repr_implf  so    
	7$R((% 	R4T\\5T5T5V4WWYZ[Y\]' 7)556 	s    	A"=AA"c                 6    | j                   j                         S r   )r~   r   r   s    r_   r  z#NestedUserFunctionVariable.get_codes  s    yy++--r^   c                 "    t         j                  S r   ry  r   s    r_   rz  z&NestedUserFunctionVariable.python_typev  r{  r^   _convertingc                     ddl m} t        |       }|
t               }||v rt	        d      |j                  |       	 | j                  |      |j                  |       S # |$ r}t	        d      |d }~ww xY w# |j                  |       w xY w)Nr<   )r=   z-cycle detected in mutually recursive closuresz1failed to convert closure cell to Python constant)baser=   idsetrY   add_get_function_impldiscard)r}   r  r=   self_idr  s        r_   r#  z'NestedUserFunctionVariable.get_functiony  s     	>T(%Kk!(?  	 	)**;7 ( 3 	(C	
 (s#   A! !A7&A22A77A: :Bc                 P    	 | j                          y# t        t        f$ r Y yw xY wrf  )r   r  r$   r   s    r_   r	  z-NestedUserFunctionVariable.is_python_constant  s-    	##%#[1 		s    %%c                 <   d }| j                   rddlm} |j                         }g }| j                   j                  D ]  }|j
                  j                  j                  |      }|| u rt        d      t        |t              r|j                  |      }n|j                         }|j                  t        |              t        |      }t!        j"                  | j$                  j                         | j&                  | j(                  j                         d |      }	| j*                  r| j*                  j                         |	_        | j.                  r| j.                  j                         |	_        | j2                  r| j2                  j5                  d      r~| j2                  j7                  d      j                         }
t        |
t              rddlm} t=         ||
            }
t        |
t<              st?        dtA        |
             |
|	_!        |	S )Nr   )InstructionTranslatorz self-referential nested function)argdefsr   r   r   z annotations must be a dict, got )"r   rH  r  
current_txr   r   r   	load_cellrY   r   r  r#  r   appendr:   r   r   r   r~   r   r  r   r   r   r   r   r3  r4  r   r   r   r   r   r   )r}   r  closure_cellsr  r   cellsr  r  rV  ri   r   r   s               r_   r   z-NestedUserFunctionVariable._get_function_impl  s   <<L&113BE LL.. / !#		 6 6 @ @ J !D(01STT m-GH)66{CE)<<>EYu-.-/. "%LM!!II((*NNLL++-!
 == $ @ @ BD??"&//"D"D"FD<<DLL112CD,,../@ATTVK+u-."8K#89 k40$6tK7H6IJ  $/D r^   rH   r   c           
      @   |dv rt         |   ||      S |dk(  r4t        | dd       }|r|j                         S t	        j
                  d       S |t        v r9t        j                  | |t        t        t        j                  |                  S t         |   ||      S )N)r   r)  r   rl   r[   rZ   r\   r0  r   r   r  )r   r7  r   r   rB   r   r3   r   r  r   r   r   r}   r   r   r  r   s       r_   r7  z&NestedUserFunctionVariable.var_getattr  s      	
 	
 7&r400>!j$/A-.1'')Q4D4K4KD4QQ++,,dD1C1CT)J$K  7&r400r^   c                     | j                   d uS r   )r   r   s    r_   has_closurez&NestedUserFunctionVariable.has_closure  s    ||4''r^   c                     |dk(  r| j                         S |dk(  r| j                         S |dk(  r!t        | dd       }|r|j                         S d S t        |   ||      S )NrZ   rl   r   r   )r  r  r   r   r   const_getattrr  s       r_   r  z(NestedUserFunctionVariable.const_getattr  si    :==?":==?">!j$/A-.1'')8D8w$R..r^   c                 2   |dk(  r t        j                  |t        | d            S |dk(  r t        j                  |t        | d            S t        j                  |      }|| j                  |      v rt        j                  d      S t        |   ||      S )Nrl   r~   r   r   T)r@   r   r@  rB   r   r   r   rB  )r}   r   r   r  r   s       r_   rB  z+NestedUserFunctionVariable.call_obj_hasattr  s     :"((WT6-BCC>!"((WT:-FGG$$T*!!"%%#**400w'D11r^   c                      yr=  r]   r   s    r_   r!  z#NestedUserFunctionVariable.has_self  r>  r^   c                     | j                   S r   rR  r   s    r_   r  z&NestedUserFunctionVariable.get_globals  rS  r^   r   r   c                 b   | j                         }t        j                  || j                  | j                  j                         | j                  rt        | j                  j                        nd t        d t        t        | j                         j                              D                    }| j                  r| j                  j                         |_         t        j                   |      j"                  |i |}|j%                          t'        |j(                  j                               }t+        |j,                  j.                  |       t1        |||       t3        |j                        D ]6  \  }}	|	|v rt5        d|	d      | j6                  j                  |   }
|
||	<   8 |S )Nc              3   2   K   | ]  }t        d         y wr   )r:   )r  _s     r_   r  z7NestedUserFunctionVariable.bind_args.<locals>.<genexpr>  s     Sa)D/Ss   zfree variable z already in result)r  r   r   r   r  r   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   r~   ri   rQ   r   r   r   r  s              r_   r  z$NestedUserFunctionVariable.bind_args  sX    }}!!NNLL++-*.--E$--%%&TS5T]]_5P5P1Q+RSS
 ??"&//"I"I"KD,!!$',,d=f=eoo++-...7ffd+"4#3#34 	 ICv~$~dX=O%PQQ<<%%c*DF4L		  r^   c                    j                  fd        | j                         j                  j                  | j                        g        t        j                  | j                  j                  j                               | j                  r | j                         n!j                  j                  d       g       | j                  r | j                         n!j                  j                  d       g       | j                  r | j                         n!j                  j                  d       g       | j                  ri| j                  j                  d      rN| j                  j                  d      }	 |j!                         }j                  j                  |      g       n!j                  j                  d       g       j                  t%        dd             | j&                  rvj                  fd        | j&                         j                  t%        dd             j                  t)        d             j                  t%        dd             j*                  }|j,                  j.                  j1                  |       r}|j,                  j.                  j2                  |    j5                         D ]H  \  }}j7                           |       j                  t)        d             j9                  |       J y y # t"        $ r  |       Y `w xY w)	Nc                  0     j                  t        d      S )Nr   )load_import_fromrZ   ro  s   r_   r/  z8NestedUserFunctionVariable.reconstruct.<locals>.<lambda>(  s    G,,X7JK r^   r      Fc                  (     j                  dd      S )N	functoolswrapsr  r  s   r_   r/  z8NestedUserFunctionVariable.reconstruct.<locals>.<lambda>M  s    00gF r^   r<   r   T)r  r~   r  create_load_const_uncheckedr   rB   r   rV  r  r   create_load_constr   r   r   r3  r4  r   r  r   r  r   r   r   r   has_pending_mutationstore_attr_mutationsr   dup_top
store_attr)r}   ro  r   r   r   rV  s    `    r_   rL  z&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,,../@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)e !%%l1o6""4() =% ' %$%s   1L. .MMr   r  )$rZ   r[   r\   r   r  r@   r   rJ  r   r   r   r   r   r   r   r   rK  r  r   rz  r  r  r#  rL  r	  r   r7  r  r  rB   rB  r!  r  r  rL  rM  rN  s   @r_   r  r  8  s   	!	0	0N .2 =  =  = S>	 =
 "D( = $d* = !4' = $d* =  = 
 =D40 #E$6$6 #C $5 .%.. ."T%"4"45 ")C4 )5CUCU ),D 9c#h 95;M;M 9v1-1581	10(T (/ ; /3 /3 /
2-
258
2	
2$ T#s(^ + ?# S/)*	
 
c?"	#<7)r^   r  c                   d     e Zd Zdedddeddf fdZdd	d
ee   dee	ef   def fdZ
ddZ xZS )!WrappedNestedUserFunctionVariabler  rb  rI   r   r`   Nc           	         |j                  dd        |j                  dd        |j                  dd        |j                  dd        |j                  dd        |j                  dd        |j                  dd        |j                  dd        t        | 	  |j                  |j                  |j
                  |j                  |j                  |j                  |j                         || _
        || _        y )	Nr  r~   r   r   r   r   r   r  )r   r   r   r  r~   r   r   r   r   r  r  rb  r  s       r_   r   z*WrappedNestedUserFunctionVariable.__init__a  s     	

9d#

64 

;%

:t$

<&

=$'

9d#

<&OOLLOO	
 r^   r   rH   r   c                 R   t         j                  rNt        t        j                        }|j                  || j                  | j                  gt        |      z   |      S | j                  j                  |       t        |   |||      }| j                  j                  |       |S r   r  r  s         r_   r>  z/WrappedNestedUserFunctionVariable.call_function{  r  r^   c                      j                   fd         j                         j                  t        dd             y )Nc                  (      j                         S r   r  r  s   r_   r/  z?WrappedNestedUserFunctionVariable.reconstruct.<locals>.<lambda>  r  r^   r<   Fr  rn  s   ``r_   rL  z-WrappedNestedUserFunctionVariable.reconstruct  r  r^   r  )rZ   r[   r\   r  r   r   r   r@   r   rJ  r>  rL  rM  rN  s   @r_   r)  r)  `  sm    + + 	
 
4' ?# S/)*	
 
 >r^   r)  c            	            e Zd Zddhej                  Zddededz  deddf fdZd ZdefdZ	defd	Z
eded
edd fd       Zdddee   deeef   defdZd ZdddedefdZdddedefdZdedefdZ xZS )SkipFunctionVariablerV  reasonNr   r`   c                 @    t        |   di | || _        || _        y r   )r   r   rV  r0  )r}   rV  r0  r   r   s       r_   r   zSkipFunctionVariable.__init__  s!    "6"
r^   c                 $    ddl m}  || |||      S r   r   r   s        r_   r   z%SkipFunctionVariable.richcompare_impl  r   r^   c                     | j                   S r   rV  r   s    r_   r   z'SkipFunctionVariable.as_python_constant      zzr^   c                     | j                   S r   r4  r   s    r_   rr  z1SkipFunctionVariable.get_real_python_backed_value  r5  r^   r   c                 4   t        j                  |dd      rW|}|}t        |dd      r&|j                  }t	        |d      }t        |dd      r&|j                  t        j                         nt        j                  |      rYt        |d      r$t        |j                  t        j                        r\t        |j                  t        j                               n3t!        |      s(t        |j                  t        j                                | ||      S )N_torchdynamo_disableF_torchdynamo_orig_callabler}  r   )r   r   r   r9  r'   r   r%   rZ  	isbuiltinr@  r   r}  r   
ModuleTyper&   BUILTIN_MATCHr8   )r\  rV  r   guard_on_sourceguard_on_values        r_   r]  z'SkipFunctionVariable.create_with_source  s     !!%)?G %O"N.*FN!/!J!J",#%A# .*FN &&|'A'ABu%
 5*- 0 02 f//0J0JKL07 &++L,F,FGH5((r^   r   rH   r   c                 
   | j                   t        j                  j                  t        j                  j
                  fv r<t        d |D              r*t        j                  | | j                   d |D               S | j                   t        v rt        d |D              rt        d |j                         D              r_ | j                   d |D        i |j                         D ci c]  \  }}||j                          c}}}t        j                  ||      S t        j                  | j                   dd      rVt        j                  | j                   dd       }t        d	t!        | j                         d
| j                    d| ddg       y | j                   t"        j$                  j&                  u r>|j)                  d      }|r|j                         }t        dd| d| dd| dg       y | j                   t"        j$                  j*                  u rX|j)                  d      }	|	r|	j                         }	nd}	t        dd| d| dt-        |j.                         dd|	 dgd       y | j                   t"        j$                  j0                  u r	 t        ddd d!g       y | j                   t8        j:                  j<                  u rtt?        |      d"v r>|s<|d#   }|d$   }tA        |tB              r"tE        |jF                  ||jH                  %      S t        d&d'|  d(| d)| d*g tJ        jL                         y tN        jP                  rEd$d+l)m*} |jW                  || j                         }tA        |tX              s|j[                  |||      S t]        | j                   d,d-      }t]        | j                   d.d       }|d/n
t!        |      }	 t        j^                  | j                         }d0| d1| d2}d3| d4g}d5|vr|d6| d7d8gz  }|dFk(  rdG}dHg}| jj                  r| jj                  }ndIdJl6m7}  || j                         }t        dKdL| dM| dN| ||       y c c}}w # t2        $ r}
t5        |
j6                        d d }
~
ww xY w# t`        $ r d9d:h}||v rd;| d| d4}d<d=g}n|\|jc                  d>      rKd?| d| d}d@g}t"        j$                  jd                  jg                  |dAz   dAji                  |      z          nKdB| d| dC}dDdEg}t"        j$                  jd                  jg                  |dAz   dAji                  |      z          Y Lw xY w)ONc              3   <   K   | ]  }|j                           y wr   r	  r  as     r_   r  z5SkipFunctionVariable.call_function.<locals>.<genexpr>  s       X
'(A  "X
r  c              3   <   K   | ]  }|j                           y wr   r   rB  s     r_   r  z5SkipFunctionVariable.call_function.<locals>.<genexpr>  s      FA!5!5!7 Fr  c              3   <   K   | ]  }|j                           y wr   rA  rB  s     r_   r  z5SkipFunctionVariable.call_function.<locals>.<genexpr>  s     9qA((*9r  c              3   <   K   | ]  }|j                           y wr   rA  )r  r   s     r_   r  z5SkipFunctionVariable.call_function.<locals>.<genexpr>  s     DqA((*Dr  c              3   <   K   | ]  }|j                           y wr   rE  rB  s     r_   r  z5SkipFunctionVariable.call_function.<locals>.<genexpr>  s     7Q!&&(7r  r8  F_torchdynamo_disable_msgz1Skip calling `torch.compiler.disable()`d functionzSkip calling function `z>` since it was wrapped with `torch.compiler.disable` (reason: r  z(Remove the `torch.compiler.disable` callr`  r  z%Call to `torch._dynamo.graph_break()`z0Called `torch._dynamo.graph_break()` with args `z`, kwargs ``z$User-inserted graph break. Message: z.Remove the `torch._dynamo.graph_break()` call.rW  z$Call to `torch._dynamo.skip_frame()`z/Called `torch._dynamo.skip_frame()` with args `z`. Skipping frame rX  z#User-inserted skip frame. Message: z-Remove the `torch._dynamo.skip_frame()` call.T)ra  rb  rc  rd  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. rU  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: )8rV  	importlibutil	find_specmetadataversionr  r@   r   RE_CONSTANT_FOLD_FNSvaluesr   r   r   r   r#   rJ  rb   r  graph_breakr   r`  r   f_codestep_unsupportedr$   r"   r  r   r   r  r   r   rP  r  r   r   r   r  r   dont_skip_tracingbuilderrL  r   r/  r>  r   getfiler   r  rc   	warn_oncejoinr0  r  rQ  )r}   r   r   r   r   r   r   r  graph_break_msgskip_frame_msgr  func_varobj_varrL  
rebuilt_fnqualname	module_orr  pathrc  rd  known_python_builtin_modulesr0  rQ  s                           r_   r>  z"SkipFunctionVariable.call_function  s    ::)..22I4F4F4N4NOOTW X
,0X
 U
 #((JDJJ F FG 
 JJ..9D99DFMMODDTZZ7$79?HA1a**,,HF #((V44!!$**.De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%33Bf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\]'	E Ij  7%aee,$67n  %Y06/D, <<'=(27  
iGE *y/C/CH/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s1   )Q0 Q6 >R 6	R?RRCU)(U)c                 f    | j                   r| j                   j                  |      S t        d      )NzBPython codegen not implemented for sourceless SkipFunctionVariablerl  rn  s     r_   rm  z'SkipFunctionVariable.reconstruct_pycode	  rp  r^   r   c                 V    t        j                  |t        | j                  |            S r   )r@   r   r@  rV  rg  s      r_   rB  z%SkipFunctionVariable.call_obj_hasattr	  s"     $$RT)BCCr^   c           
          |t         v r5t        j                  | |t        t	        | j
                  |                  S t        || j
                  | j                  |      S )Nr  )r3   r   r  r   r   rV  r   r   rg  s      r_   r7  z SkipFunctionVariable.var_getattr	  sR     )),,dDT)B$C  b$**dkk4@@r^   r   c                 h    t        |t              xr! | j                         |j                         k(  S r   )r   r@   r   r  s     r_   r  z$SkipFunctionVariable.is_python_equal	  s1    uo. H'')U-E-E-GG	
r^   r   )rZ   r[   r\   r@   r  r   rJ  r   r   r   rr  r  r   r]  r   r   r>  rm  rB   rB  r7  r  rL  r  rM  rN  s   @r_   r/  r/    s(    
	'	'Nc 3:  PT 
7
C c  )s )F )?U ) )B~'~ ?#~ S/)*	~
 
~@
D-D58D	D
A-A58A	A
V 
 
r^   r/  c                   d     e Zd Zdedddeddf fdZdd	d
ee   dee	ef   def fdZ
ddZ xZS )WrappedSkipFunctionVariabler  rb  rI   r   r`   Nc                     |j                  dd        |j                  dd        t        |   |j                  fd|j                  i| || _        || _        y )NrV  r0  )r   r   r   rV  r0  r  rb  r  s       r_   r   z$WrappedSkipFunctionVariable.__init__	  sN     	

7D!

8T"Hw~~HHr^   r   rH   r   c                 R   t         j                  rNt        t        j                        }|j                  || j                  | j                  gt        |      z   |      S | j                  j                  |       t        |   |||      }| j                  j                  |       |S r   r  r  s         r_   r>  z)WrappedSkipFunctionVariable.call_function	  r  r^   c                      j                   fd         j                         j                  t        dd             y )Nc                  (      j                         S r   r  r  s   r_   r/  z9WrappedSkipFunctionVariable.reconstruct.<locals>.<lambda>	  r  r^   r<   Fr  rn  s   ``r_   rL  z'WrappedSkipFunctionVariable.reconstruct	  r  r^   r  )rZ   r[   r\   r/  r   r   r   r@   r   rJ  r>  rL  rM  rN  s   @r_   ro  ro  	  sm    
%
 +
 	

 

' ?# S/)*	
 
 >r^   ro  c                        e Zd ZdZdefdZdedededdf fdZdefd	Z	defd
Z
dej                  fdZdddedef fdZd Zdee   fdZdddee   deeef   defdZdefdZ xZS )WrapperUserFunctionVariablea3  
    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".
    r`   c                 "    t         j                  S r   ry  r   s    r_   rz  z'WrapperUserFunctionVariable.python_type	  r{  r^   wrapper_objattr_to_tracer   Nc                 @    t        |   di | || _        || _        y r   )r   r   rw  rx  )r}   rw  rx  r   r   s       r_   r   z$WrapperUserFunctionVariable.__init__	  s#    "6"&*r^   c                 .    | j                   j                  S r   )rw  r[   r   s    r_   r%  z&WrapperUserFunctionVariable.get_module	  s    ***r^   c                 .    | j                   j                  S r   )rw  rZ   r   s    r_   r  z$WrapperUserFunctionVariable.get_name	  s    (((r^   c                 6    | j                         j                  S r   )r#  rl   r   s    r_   r  z$WrapperUserFunctionVariable.get_code	  s      "+++r^   r   rH   r   c                     || j                   k(  r[t        | j                  | j                         }| j                  xr t	        | j                  |      }t        j                  |||      S t        | !  ||      S r   )	rx  r   rw  r   r'   r@   r   r   r7  )r}   r   r   r   r   r   s        r_   r7  z'WrapperUserFunctionVariable.var_getattr	  sk     4%%%$**D,>,>?C[[BZT%BF"((S&99w"2t,,r^   c                 B    t        | j                  | j                        S r   r   rw  rx  r   s    r_   r#  z(WrapperUserFunctionVariable.get_function	      t''););<<r^   c                     g S r   r]   r   s    r_   r   z%WrapperUserFunctionVariable.self_args	  r   r^   r   c           
         t        | j                  d      rt        | j                  | j                  d       }t        |dd      xs d}t	        | j                        }|j                  dd      d   dk7  r|s|j                         }t        j                  j                  |j                        }|j                  }	d	| d
|	 ddj                  t        j                  |g             }
t        j                   j"                  j%                  |
       t        j                   j"                  j&                  j)                  d      }|j+                  t&        j,                        rt        j.                  j0                  j3                         }t5               |z   }|d   j                  |d   j                  f}dj                  t        j                  |            }d|d    d
|d    d}|t7        |      z  }|j9                  |       | j;                         t=        |      z   }| j                  dk(  xr[ t        | j                  dd      xrB t        t        | j                  dd       dd       |j>                  j@                  jB                  u}|rtD        jF                  ntD        jH                  }tK        jL                  ||      jO                  || tK        jL                  || j                        g||      S )N
cache_infor[   rW  rX  r<   )maxsplitr   rb   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._dynamor  z)call to a lru_cache wrapped function at: rO  re  _is_torch_compileFr9  rl   )(r@  rw  r   rx  r6   splitframe_summaryosrh  basenamer  linenor`  	tracebackformat_listrb   r  rc   r_  logging	getLoggerisEnabledForDEBUG_guardsTracingContextextract_stackr   rJ  debugr   r   r   r  rZ  r   (getattr_and_trace_no_nested_graph_breaksgetattr_and_tracer@   r   r>  )r}   r   r   r   	target_fnr  is_allowed_lru_cache_wrapperr  r  r  r  dynamo_logger
user_stack	frame_locuser_stack_formatteduser_stack_traceall_argsis_inner_torch_compilepolyfills                      r_   r>  z)WrapperUserFunctionVariable.call_function	  s    4##\2 0 0$2D2DdKI!)\2>D"KB4CSCST )
 !!#!215@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 "77 ,((*=uE,((*FM
 99$$++, 	 & >>,, 	
 $$
 -?((T-?-?@L8L
	
r^   c                 B    t        | j                  | j                        S r   r  r   s    r_   rr  z8WrapperUserFunctionVariable.get_real_python_backed_valueI
  r  r^   )rZ   r[   r\   r   r   rz  r   rJ  r   r%  r  r   rK  r  r@   r7  r#  r   r   r   r>  r  rr  rM  rN  s   @r_   ru  ru  	  s    "T "+C + +s +t +
+C +)# ),%.. ,---58-	-=40 H
'H
 ?#H
 S/)*	H

 
H
T=f =r^   ru  c            
       V     e Zd ZdZdefdZdededededdf
 fd	Z	de
e   fd
Z xZS )WrapperUserMethodVariablez
    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.
    r`   c                 "    t         j                  S r   r  r   s    r_   rz  z%WrapperUserMethodVariable.python_typeT
  r  r^   rw  rx  self_objr   Nc                 6    t        |   ||fi | || _        y r   )r   r   r  )r}   rw  rx  r  r   r   s        r_   r   z"WrapperUserMethodVariable.__init__W
  s      	m>v>r^   c                     | j                   gS r   r  r   s    r_   r   z#WrapperUserMethodVariable.self_argsa
  r  r^   )rZ   r[   r\   r   r   rz  r   rJ  r@   r   r   r   rM  rN  s   @r_   r  r  M
  sZ     T    "	
  
40 r^   r  c                  R    t         j                  j                         rddlm}  | S i S )Nr   traceable_collective_remaps)rb   distributedis_available)torch.distributed._functional_collectivesr  r  s    r_   _traceable_collective_remapsr  e
  s&    %%'	
 +*Ir^   c                     t         j                  j                         st        d      |t	               j                         vrt        | d      |j                  }| j                  d      }t        ||      S )NzIllegal invocation.z$ is not a traceable collective remapz)torch.distributed._functional_collectives)	rb   r  r  r   r  rX  rZ   import_sourcer'   )r   r   
inner_namepath_sources       r_   _traceable_collectives_sourcer  p
  sq     ))+233	-/6688t#GHIIJ""#NOKk:..r^   c                        e Zd ZdZdedef   dededdf fdZed	d
dedef   de	dedd f
d       Z
ededefd       Zed	d
dedef   deeef   fd       Zd	d
dee   deeef   defdZ xZS )!CollectiveFunctionRewriteVariablea  
    Some of the torch.distributed.* collective APIs are possible to rewrite to 'traceable' collectives.

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

    In the case that a function is 'remappable' but only for some combinations of call-time arguments,
    we check the args at `call_function` time and fall back to graph-breaking if needed.  This is no worse
    than status-quo as we currently graph-break on all distributed.* collectives.
    r   .replacement_varr   r`   Nc                    t        |   |fi | t        |t              st	        dt        |             || _        y )Nz4replacement_var must be a UserFunctionVariable, got )r   r   r   rP  r   r   r  )r}   r   r  r   r   s       r_   r   z*CollectiveFunctionRewriteVariable.__init__
  sI     	&v&/+?@ FtOG\F]^   /r^   r   rH   old_fnr   optionsc                 j    t         j                  | |      \  }}t        |ft        |fd|i||d|S )Nr   )r  r   )r  r  rP  )r   r  r   r  new_fn
new_sources         r_   r   z(CollectiveFunctionRewriteVariable.create
  sO     ?FFr6R
0
0V
VgV
 	
 	
r^   variablec                 H    t        j                  |       xr | t               v S r   )r   
isfunctionr  )r  s    r_   can_rewritez-CollectiveFunctionRewriteVariable.can_rewrite
  s%     x(WX9U9W-W	
r^   c                 8    t               |   }|t        | |      fS r   )r  r  )r   r   r  s      r_   r  z)CollectiveFunctionRewriteVariable.rewrite
  s$     ./34R@@@r^   r   c                 	   dd l m} ddlm} t	        j
                  | j                        }t         |j                  |i |j                        }g }d|v rQ|d   j                         r>t        d| j                   d|d|d| j                   g t        j                         | j                  |j                  k(  rt        j                   st        d	| j                   d
dg       |d   }t#        |t$        j&                        s)t(        j*                  j,                  j/                  d      t1               }t1               }	t1               }
t1               }d }|j2                  D ]  }|j5                         |j6                  ur)t(        j*                  j,                  j/                  d      |j9                  |d      }|j;                         r|j                         }||j<                  |j>                  fvr,t(        j*                  j,                  j/                  d|       t$        j@                  jC                  |jD                        }nftG        |d      r.t$        j@                  jC                  |jI                               }n,t(        j*                  j,                  j/                  d|       |jK                  |       |jK                  |j9                  |d             |	jK                  |j9                  |d             |
jK                  |j9                  |d             ||j9                  |d      } |tM        d      g }t%        j&                  |      t%        j&                  |	      t%        j&                  |
      t%        j&                  |      |d}| jN                  jQ                  |||      S | j                  |j<                  |j>                  fv rHt        j                   st        d| j                   d
dg       | jN                  jQ                  |||      S | j                  |jR                  |jT                  |jV                  |jX                  fv rf|j[                  d      }||j\                  n|j^                  d   j`                  }||vrtc        d|       te        jf                  |||         |d<   | jN                  jQ                  |||      S )Nr   )REDUCE_OP_TO_STRasync_opz)async_op=True for distributed collectivesz, args=	, kwargs=z3`torch.compile` doesn't support `async_op=True for r`  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 incorrectlyr   zunexpected P2POp op r  zunexpected P2POp op variable tensorpeertaggroupz&group_var must be set from P2POp items)op_list	peer_listtag_listtensors
group_namez(P2P compilation disabled for isend/irecvzUnsupported all_reduce op: )4torch.distributedr  r  r  r   r  r   r   r  r  r   r#   r   r  batch_isend_irecvr   enable_p2p_compilationr   r   rN   rb   r  rk  InternalTorchDynamoErrorr   r   rz  P2POpr7  r	  isendirecvrB   r   rZ   r@  r  r	  r   r  r>  
all_reducereduce_scatter_singlereduce_scatter_tensor_reduce_scatter_baser   rV  
parametersdefaultr  r@   r   )r}   r   r   r   distr  r  p2p_opsopspeerstagsr  	group_varitemop_varr   new_args
new_kwargsreduce_op_var	reduce_ops                       r_   r>  z/CollectiveFunctionRewriteVariable.call_function
  sg    	)N %%dgg.	ninnd5f5??@F:$6$I$I$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&77>>v?PQF--++DD7x@  

6"t//H=>T--b&9:D,,R78$ $ 0 0W =I5>8  $%MNN.0H$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5Ei5PQF4L##11"dFCCr^   )rZ   r[   r\   r   r   r   rP  r   staticmethodr   r   rL  r  r   r'   r  r   r@   r   rJ  r>  rM  rN  s   @r_   r  r  }
  s0   /S#X/ .	/
 / 
/ 
'
c"
 
 	

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

 A'A-5c3h-?A	sJ	A AyD'yD ?#yD S/)*	yD
 
yDr^   r  c                   @    e Zd ZdefdZdddee   deeef   defdZ	y)	CollectionsNamedTupleFunctionr`   c                     | j                   S r   ru  r   s    r_   r   z0CollectionsNamedTupleFunction.as_python_constant/  rv  r^   r   rH   r   r   c                    t        ||      }|r~	  | j                  |D cg c]  }|j                          c}i |j                         D ci c]  \  }}||j                          c}}}t        j                  t                     S t        dd|d|dg t        j                         y c c}w c c}}w # t        $ r4}	t        t        |	      |t        |	j                               Y d }	~	d }	~	ww xY w)Nr   r,  znamedtuple constructionzargs=r  z?`torch.compile` only support certain input types for namedtupler`  )r1   r   r   r   r   r    r   r   r   r   UserDefinedClassVariabler?   r#   r   r  )
r}   r   r   r   constant_argsr  r   r   rV  rk  s
             r_   r>  z+CollectionsNamedTupleFunction.call_function2  s     ,D&9
6:;a**,;=C\\^LTQq!..00L 55.0  	-tgZy)Y"..		
 <L (Ichh s3   B> B3B> B8(B> 3B> >	C;*C66C;N)
rZ   r[   r\   r   r   r   r@   r   rJ  r>  r]   r^   r_   r  r  .  sH    C 
'
 ?#
 S/)*	

 

r^   r  c                        e Zd Zej                  Zdhej                  Z	 ddedee   de	e
ef   dededdf fdZd	 Zdefd
ZddZdefdZdddee   de	e
ef   defdZddde
defdZddde
defdZdefdZdefdZdedefdZ xZS )FunctoolsPartialVariableoriginal_cache_hashNri   r   keywordsr   r`   c                     t        |   di | || _        t        |t              st        dt        |             || _        t        |t              st        dt        |             || _	        || _
        y )Nzargs must be a list, got zkeywords must be a dict, got r]   )r   r   ri   r   r   r   r   r   r   r  r  )r}   ri   r   r  r  r   r   s         r_   r   z!FunctoolsPartialVariable.__init__\  su     	"6"	$% #<T$ZL!IJJ	(D) #@h@P!QRR #6 r^   c                 $    ddl m}  || |||      S r   r   r   s        r_   r   z)FunctoolsPartialVariable.richcompare_implo  r   r^   c                 "    t         j                  S r   )r  partialr   s    r_   rz  z$FunctoolsPartialVariable.python_typet  s       r^   c                 <   j                  fd        | j                         | j                  rj                  | j                         | j                  s2j                  t        t        | j                        dz   d             y j                  | j                  j                                t        | j                  j                               }j                  j                  t        |      t        | j                        z   dz   |d             y )Nc                  (     j                  dd      S )Nr  r  r!  r  s   r_   r/  z6FunctoolsPartialVariable.reconstruct.<locals>.<lambda>x  s    g&>&>{I&V r^   r<   F)r  ri   r   foreachr  r  r   r   rX  r   keyscreate_call_function_kw)r}   ro  r  s    ` r_   rL  z$FunctoolsPartialVariable.reconstructw  s    VW		99OODII&}}!!"6s499~7I5"QR,,./T]]'')*++CIDII,F,JDRWX	
r^   c                 "    | j                         S r   rE  r   s    r_   r#  z%FunctoolsPartialVariable.get_function      &&((r^   r   rH   c                 z    | j                   |z   }i | j                  |}| j                  j                  |||      S r   )r   r  ri   r>  )r}   r   r   r   merged_argsmerged_kwargss         r_   r>  z&FunctoolsPartialVariable.call_function  s>     ii$&34==3F3yy&&r;FFr^   r   c                 p    t        j                  |t        t        j                  t
              |            S r   )r@   r   r@  r  r  r4   rg  s      r_   rB  z)FunctoolsPartialVariable.call_obj_hasattr  s)     $$R1B1B81Ld)STTr^   c           
      "   | j                   xr t        | j                   |      }|dk(  r| j                  S |dk(  r!t        j                  | j
                  |      S |dk(  rX| j                  j                         D ci c]  \  }}t        j                  ||      | }}}t        j                  ||      S |t        v r9t        j                  | |t        t        t        j                   |                  S t#        t$        |       y c c}}w )Nri   r   r   r  r  )r   r'   ri   r   r2  r   r  r   r@   r   r.  r3   r  r   r   r  r  r    r   )r}   r   r   r   r   r   r   s          r_   r7  z$FunctoolsPartialVariable.var_getattr  s     >DKK!>6>996>**499VDD:AEATATAVWA_**2q114WEW..uVDD)),,dD1B1BD)I$J  	!4 Xs   >!Dc                 :   t        j                  | j                  j                         g| j                  D cg c]  }|j                          c}i | j
                  j                         D ci c]  \  }}||j                          c}}S c c}w c c}}w r   )r  r  ri   r   r   r  r   )r}   r  r   r   s       r_   r   z+FunctoolsPartialVariable.as_python_constant  s      II((*
26))<3c$$&<
 6:]]5H5H5JKTQq!&&((K
 	
<Ks   B
1Bc                 x   t        j                  | j                  j                         g| j                  D cg c]  }|j                          c}i | j
                  j                         D ci c]  \  }}||j                          c}}}| j                  | j                  |_        |S c c}w c c}}w )zcSimilar to as_python_constant(), but add ID_MATCH guards to try to force things to become constants)	r  r  ri   guard_as_python_constantr   r  r   r  
cache_hash)r}   r   r   r   s       r_   r  z1FunctoolsPartialVariable.guard_as_python_constant  s    ""II..0
48II>qa((*>
 <@==;N;N;PQ41aq!,,..Q
 ##/ $ 8 8F ?Qs   B1
1B6r   c                 p   t        |t              xr | j                  j                  |j                        xr~ t	        d t        | j                  |j                        D              xrL t	        d t        | j                  j                         |j                  j                               D              S )Nc              3   D   K   | ]  \  }}|j                  |        y wr   r  )r  arg_aarg_bs      r_   r  z;FunctoolsPartialVariable.is_python_equal.<locals>.<genexpr>  s'      "UE %%e,    c              3   D   K   | ]  \  }}|j                  |        y wr   r  )r  value_avalue_bs      r_   r  z;FunctoolsPartialVariable.is_python_equal.<locals>.<genexpr>  s'      &Wg ''0r  )	r   r  ri   r  r  r   r   r  rX  r  s     r_   r  z(FunctoolsPartialVariable.is_python_equal  s    u67 		))%**5 &)$))UZZ&@   *-MM((*ENN,A,A,C+ 	
r^   r   r  )rZ   r[   r\   r  r  r  r@   r  r   r   rJ  r   r   r   r   rz  rL  r#  r>  rB   rB  r7  r   r  r  rL  r  rM  rN  s   @r_   r  r  S  sE   %%M 			'	'N $(77 ?#7 sO+,	7
 !7 7 
7&7
!T !
)c )G'G ?#G S/)*	G
 
GU-U58U	U5-5585	5$
C 

# 

V 
 
r^   r  c            
       `    e Zd Zdddhej                  Zeej                  dee	de
f   ej                  f   fd              Zede
dedd fd	       Zded
e
ddf fdZede	de
f   fd       ZdefdZde
fdZdddee   d
eeef   defdZdddedee   d
eeef   def
dZde
fdZ xZS )PolyfilledFunctionVariabler   r  traceable_fnr`   .c                     i S r   r]   )r\  s    r_   _get_polyfill_handlersz1PolyfilledFunctionVariable._get_polyfill_handlers  rE  r^   rV  r   c                 f    t        |j                  t        j                                | ||      S rX  rY  r[  s      r_   r]  z-PolyfilledFunctionVariable.create_with_source  s*     	f''(B(BCD5((r^   r   Nc                 P   t        |   di | || _        | j                         j	                  ||      }d }t        |      st        d| d|       dD ]/  }t        ||d       }|st        |      st        d| d      |} n t        d| d      || _	        || _
        y )NzPolyfill handler z is not callable for )__torch_dynamo_polyfill____python_implementation__zPolyfill candidate z is not callablez# does not have a traceable functionr]   )r   r   r   r  r   callabler   r   r  r  r  )r}   r   r   handlerr  candidate_attr	candidater   s          r_   r   z#PolyfilledFunctionVariable.__init__  s    "6"--/33B;  #4WI=RSURV!WXX
 	N  >I	*(-i[8HI   )	 #G9,OP  " ,r^   c                     | j                   S r   )r  r   s    r_   polyfill_fnz&PolyfilledFunctionVariable.polyfill_fn  s       r^   c                 0    t        | j                  dd      S )N*__torch_dynamo_can_constant_fold_through__F)r   r  r   s    r_   can_constant_fold_throughz4PolyfilledFunctionVariable.can_constant_fold_through  s    OOI5
 	
r^   c                 "    | j                         S r   rE  r   s    r_   r#  z'PolyfilledFunctionVariable.get_function  r   r^   r   rH   r   c                 F   | j                         rt        ||      rt | j                  |D cg c]  }|j                          c}i |j	                         D ci c]  \  }}||j                          c}}}t        j                  ||      S | j                  t        j                  u r t        |      dk(  r|st        |d   t        j                  t        j                  f      rt        d |d   j                  D              rt        j                  j!                  ||j"                  j%                  dt&        j(                  t+        d |d   j                  D              fi       t'        j(                  |d   j                  D cg c].  }|j-                         r|j                         n|j.                  0 c}            S t        j                  || j0                        }|j3                  |t5        |      t7        |            S c c}w c c}}w c c}w )Nr<   r   c              3      K   | ]h  }|j                         xr t        |j                         t              xs2 t        |t        j
                        xr |j                         t        u  j y wr   )r	  r   r   r  r   SymNodeVariablerz  )r  r  s     r_   r  z;PolyfilledFunctionVariable.call_function.<locals>.<genexpr>%  se        %%'SJq7K7K7Ms,S Yq)";";<WTWAWYs   A.A0r>  c              3   <   K   | ]  }|j                           y wr   )as_proxyrB  s     r_   r  z;PolyfilledFunctionVariable.call_function.<locals>.<genexpr>0  s     ?A1::<?r  )sym_num)r$  r2   r   r   r   r@   r   builtinssumr   r   r   rN   r2  r  r(  r   r   create_proxyrb   sym_sumr   r	  r+  r  r<  r   r   )	r}   r   r   r   r  r   r   r   traceable_function_variables	            r_   r>  z(PolyfilledFunctionVariable.call_function  s    ))+0M&1
 6:;a**,;=C\\^LTQq!..00L  #((V44 GGx||#D	Q47Y%;%;Y=T=T$UV  a  ,,33		&&#MM?a??A	  "&a   !335 002!"+	 4  ( '6&;&;B@Q@Q&R#--'JL
 	
O <L4s   HH3Hr   c                 >   |dk(  r| j                  |||      S t        | j                  |d       }|s't        |      st	        |d| d| j                          i }| j
                  rt        | j
                  |      |d<   t        |fi |}|j                  |||      S )N__call__zCannot find callable z in r   )r>  r   r   r5   r!   r   r'   r  )r}   r   r   r   r   methodr  polyfilled_method_variables           r_   r  z&PolyfilledFunctionVariable.call_methodF  s     :%%b$77$-+f-R#8d477)!LM;; *4;; =GH%?%R'%R")77D&IIr^   c                     | j                   S r   ru  r   s    r_   r   z-PolyfilledFunctionVariable.as_python_constantY  rv  r^   )rZ   r[   r\   r@   r  r  r  cacher   r   r   r   r   r  r   r]  rP   r   propertyr!  rL  r$  r#  r   rJ  r>  r  r   rM  rN  s   @r_   r  r    sv    
	'	'	N __tHS#X,>@R@R,R'S    ))!')	%) )-2 - - -< !Xc3h/ ! !
4 

)c )6
'6
 ?#6
 S/)*	6

 
6
pJ'J J ?#	J
 S/)*J 
J&C r^   r  c                   x     e Zd Zdededdf fdZdefdZdd	Zdddefd
Z	ddde
e   deeef   defdZ xZS )SysFunctionVariablerV  r   r`   Nc                 2    t        |   di | || _        y r   r   r   rV  r}   rV  r   r   s      r_   r   zSysFunctionVariable.__init__^      "6"
r^   c                 "    t         j                  S r   )r   BuiltinFunctionTyper   s    r_   rz  zSysFunctionVariable.python_typeb  s    (((r^   r   rH   c                 `   t        |j                        rG|j                  d   }|j                  }|j                  |d      }t	        j
                  ||      ||g}n>t        j                  d       t        j                  d       t        j                  d       g}t        j                  |      S )Nr  __traceback__)
r   exn_vt_stackr  r7  r@   r   rB   r   r   r2  )r}   r   exnr  tbr   s         r_   exc_infozSysFunctionVariable.exc_infoe  s    r//"%C,,C_5B$**2s3S"=E !''- ''- ''-E
 &&u--r^   c                 >    | j                  |      j                  d   S )Nr<   )rE  r   r   s     r_   	exceptionzSysFunctionVariable.exceptions  s    }}R &&q))r^   r   c                     | j                   t        j                  u r| j                  |      S | j                   t        j                  urt	        d| j                          | j                  |      S )Nzexpected sys.exception, got )rV  r  rE  rG  r   r=  s       r_   r>  z!SysFunctionVariable.call_functionv  sY     ::%==$$::S]]* #?

|!LMM~~b!!r^   )r   rH   r`   zvariables.TupleVariable)rZ   r[   r\   r   r   r   rz  rE  r@   rG  r   r   rJ  r>  rM  rN  s   @r_   r9  r9  ]  s|    c S T )T ).*7 *O *"'" ?#" S/)*	"
 
"r^   r9  ) create_tma_experimental_metadatacreate_tma_stable_metadataTMADescriptorMetadataTritonHOPifierc                      e Zd ZdedefdZdedefdZdede	fdZ
dd	deej                  j                  j                  d
f   fdZde	deee	f   ddde	fdZded
e	f   dee   deeef   ded   de	defdZde	ded   de	dedef
dZde	ded   dee	   fdZdede	fdZdddee	   ddfdZddde	deee	f   dddd f
d!Zy")#DynamoTritonHOPifierr  r`   c                 (    t        ddd| g        y )Nz!triton kernel unsupported featurerW  z/Encountered triton kernel unsupported feature: r`  )r#   )r}   r  s     r_   raise_unsupportedz&DynamoTritonHOPifier.raise_unsupported  s    7I#O		
r^   maybe_callablec                 .    t        |t        t        f      S r   )r   r  rP  )r}   rQ  s     r_   is_callablez DynamoTritonHOPifier.is_callable  s    79MN
 	
r^   r   c                     |j                   S r   r4  )r}   r   s     r_   	get_valuezDynamoTritonHOPifier.get_value  s    yyr^   gridrM   .c                     ddl m} t        ||      r|j                         S t	        ddt        |       dg t        j                         y )Nr<   )rM   z/unsupported grid type for triton hop check_gridzgrid type = z;`torch.compile` only supports list-like grid for check_gridr`  )listsrM   r   r*  r#   r   r   r  )r}   rV  rM   s      r_   
check_gridzDynamoTritonHOPifier.check_grid  sK    +d,-==?"I&tDzl3Y&22	r^   metar   rH   c                     |j                         D ci c]  \  }}t        j                  ||      | }}}|j                  ||gi       }|S c c}}w r   )r   r@   r   r>  )r}   rV  rZ  r   r   r   meta_vars          r_   	call_gridzDynamoTritonHOPifier.call_grid  sV     AE

M1O))"a0!3MM!!"xj"5 Ns   !Auser_fnr   r   r  c                 \    ddl m} |j                  ||      }|j                  |||      }|S )Nr<   rK  )r]  rL  r   r>  )	r}   r^  r   r   r   r  rL  wrapped_user_functionr   s	            r_   call_user_defined_fnz)DynamoTritonHOPifier.call_user_defined_fn  s3     	/ 1 8 8W E&44RvFr^   user_objr   c                 t    ddl m} |t        d        ||t        |j                  |             |      }|S )Nr<   )VariableBuildertx must not be None)r]  rd  r   r'   kernel_source)r}   rb  r   r  r   rd  wrapped_user_objs          r_   wrap_user_defined_objz*DynamoTritonHOPifier.wrap_user_defined_obj  sK     	-: !677
?
811dV=

  r^   configsc                 ~    |t        d      t        ||      }|D cg c]  } |j                          }}|S c c}w )Nre  )r   r;   r  )r}   ri  r   r   s       r_   maybe_unpack_configsz)DynamoTritonHOPifier.maybe_unpack_configs  sO     : !677!"g. DKK26224KK Ls   :r   c                 d    |j                         s| j                  d       |j                         S )Nz`@triton.heuristics must return constant values because configs can only contain constant values.)r	  rP  r  )r}   r   s     r_   maybe_unpack_heuristic_resultz2DynamoTritonHOPifier.maybe_unpack_heuristic_result  s0    ((*""r ..00r^   TritonKernelVariablec                     |j                   t        |      dk7  r| j                  d        t        |      |j                  |j
                  |d   |j                        S )Nr<   z7Triton kernels should be called with only a single gridr   )kernel
kernel_idxrV  rf  )rV  r   rP  r   rp  rq  rf  )r}   r  r   s      r_   call_getitemz!DynamoTritonHOPifier.call_getitem  s^     ==$D	Q""I tH~??**a"00	
 	
r^   gridscombined_argszvariables.ConstantVariablec                 F   ddl m} i }t        |j                               D ]D  }||   }t	        |t
        t        f      s|j                         ||<   |j                         ||<   F |j                         D ci c]  \  }}t        j                  ||      | }	}}ddlm}
m} |j                         D ci c]6  \  }}t	        |t              r!|j                         r||j!                         8 }}}|	j                         D ci c](  \  }}t	        |t              r|j                         s||* }}}|j#                         D ]R  }|j%                         }|j'                         r$|j)                         r5| j+                  dt-        |       d       T |
j/                  |      } ||t0              }|j2                  j5                  d|d|j6                  ||||j9                         d	       t        j                  |d       S c c}}w c c}}w c c}}w )
Nr<   )r.  r   )kernel_side_tabletriton_kernel_wrapper_mutationz.Unexpected argument type for a Triton kernel: rX  r>  r]   )rq  constant_args_idxrV  tma_descriptor_metadatar   )dictsr.  r   r  r   !TMADescriptorExperimentalVariableTMADescriptorStableVariableto_metadata
get_tensorr   r@   r   *torch._higher_order_ops.triton_kernel_wraprv  rw  r	  r   rX  realizer  is_symnode_likerP  r   add_constant_argsr   r   r.  rq  r*  )r}   r  rs  rt  r   r.  ry  r   r   combined_args_vtrv  rw  r  non_constant_argsrx  rZ  s                   r_   call_HOPzDynamoTritonHOPifier.call_HOP  s!    	- :<m((*+ 	2Aa A57RS ./]]_'*#$<<>a 	2 9F8K8K8M
041O!!"a(!+
 
	
 &++-
1!_-!2F2F2H q##%%
 
 )..0
1q/2q7K7K7M qD
 
 #))+ 	A		AKKMQ%6%6%8&&DT!WIQO	 .??N !2D9
		*&11%6+B--/		
 $$
 	
[



s   <!H;;H-HN)rZ   r[   r\   rJ  r   rP  r@   rL  rS  r   rU  r   rb   fxproxyProxyrY  r   r]  r   r   r
   ra  rh  rk  rm  r   rr  r  r]   r^   r_   rN  rN    s   
S 
U 

/ 
d 

_  1 eEHHNN<P<PRU<U6V #CH~3N	#s(# ?# S/)*	
 01  
   01  	 
   
 & ()D E	c1O 1 1
(
 sm
 
 	
$H
(H
 H
 CH~	H

 (H
 
&H
r^   rN  c            
            e Zd ZU ded<   ded<   edz  ed<   edz  ed<   dededz  deded	df
 fd
Zd	efdZ	ddde
e   deeef   d	efdZddded	efdZdddede
e   deeef   d	ef
 fdZded	efdZ xZS )rn  rJ   rV  rK   rp  Nrq  rf  r   r`   c                     |j                  d|j                  d            | _        t        |   di | t
        j                  | |||       y )Nrf  r   r]   )r   r   rf  r   r    dynamo_triton_hopifier_singletoninit_variable)r}   rp  rq  rV  r   r   s        r_   r   zTritonKernelVariable.__init__R  sF     $ZZH9MN"6"(66tVZQUVr^   c                 ,    t        | j                        S r   )r   rp  r   s    r_   rz  z TritonKernelVariable.python_typeY  s    DKK  r^   r   rH   r   c                 2    t         j                  | |||      S r   )r  call_triton_kernelr=  s       r_   r>  z"TritonKernelVariable.call_function\  s      0BB$
 	
r^   r  c                 0    t         j                  | |g      S r   )r  rr  )r}   r   r  s      r_   mp_subscript_implz&TritonKernelVariable.mp_subscript_implf  s     0<<TC5IIr^   r   c                 b    |dk(  rt         j                  | |||      S t        |   ||||      S )Nrun)r  call_runr   r  r  s        r_   r  z TritonKernelVariable.call_methodn  s<     5=3<<T4QSTT w"2tT6::r^   r  c                 v    ddl m} ddlm} t	        ||      r  |j
                  |j                               S |S )Nr<   rA   )r(  )r  rB   r  r(  r   r   evaluate_expr)r}   r  rB   r(  s       r_   specialize_symbolicz(TritonKernelVariable.specialize_symbolic{  s6    .+ c?+*#**3+<+<+>??
r^   )rZ   r[   r\   r   r  r   r   r   r   rz  r   r@   r   rJ  r>  r  r  r  rM  rN  s   @r_   rn  rn  L  s   
d
D= WW'*TzW9<WHKW	W!T !
'
 ?#
 S/)*	

 

J'J J 
	J;'; ; ?#	;
 S/)*; 
;s s r^   rn  c                   d     e Zd Zdddee   dee   dededdf fd	Zdefd
ZddZdefdZ	 xZ
S )r{  data_ptrzvariables.DataPtrVariabledims
block_dimselement_sizer   r`   Nc                     t        |t        j                        st        dt	        |             t        |   di | || _        || _        || _	        || _
        y )Nz(data_ptr must be a DataPtrVariable, got r]   )r   r   DataPtrVariabler   r   r   r   r  r  r  r  )r}   r  r  r  r  r   r   s         r_   r   z*TMADescriptorExperimentalVariable.__init__  s`     (I$=$=> :4>:JK  	"6" 	$(r^   c                     t        | j                  D cg c]  }|j                          c}| j                  D cg c]  }|j                          c}| j                  j                               S c c}w c c}w r   )rI  r  r*  r  r  )r}   dims     r_   r}  z-TMADescriptorExperimentalVariable.to_metadata  sW    /'+yy1S\\^1'+7S\\^7&&(
 	
17s
   A2A7
c                     j                   fd        j                  j                         g  j                   j                   j
                  }j                  |       j                  t        |      dz   d       y )Nc                  V     j                  ddt        j                         d      S )Nz$triton.tools.experimental_descriptorcreate_d_tma_descriptor)r  r   r  r  s   r_   r/  z?TMADescriptorExperimentalVariable.reconstruct.<locals>.<lambda>  s+    G,,6#dii.))9: r^   r<   F)	r  r  rL  r  r  r  r  r>  r   )r}   ro  r   s   `` r_   rL  z-TMADescriptorExperimentalVariable.reconstruct  sp    	
 	!!'*@@T__@d.?.?@c$i!mU3r^   c                 .    | j                   j                  S r   )r  from_tensorr   s    r_   r~  z,TMADescriptorExperimentalVariable.get_tensor  s    }}(((r^   r  )rZ   r[   r\   r   r@   r   r   r}  rL  r~  rM  rN  s   @r_   r{  r{    si    )-) ?#) )	)
 &) ) 
)$
S 

4)O )r^   r{  c                   P     e Zd Zdddddeddf fdZdefd	Zdd
ZdefdZ xZS )r|  r  rO   block_shaperN   r   r`   Nc                 v    |j                         st        d      t        |   di | || _        || _        y )Nz tensor argument must be a tensorr]   )r  r   r   r   r  r  )r}   r  r  r   r   s       r_   r   z$TMADescriptorStableVariable.__init__  s<     ! !CDD"6"&r^   c                 H    t        | j                  j                               S r   )rJ  r  r*  r   s    r_   r}  z'TMADescriptorStableVariable.to_metadata  s!    )%%'
 	
r^   c                     j                  fd       j                  d       | j                  j                          | j                         j                  d       y )Nc                  (     j                  dd      S )Nztriton.tools.tensor_descriptorTensorDescriptorr!  r  s   r_   r/  z9TMADescriptorStableVariable.reconstruct.<locals>.<lambda>  s    G,,0" r^   r  r   )r  load_methodr  rL  r  r  rn  s    `r_   rL  z'TMADescriptorStableVariable.reconstruct  sT    	
 	M*(  !Ar^   c                     | j                   S r   )r  r   s    r_   r~  z&TMADescriptorStableVariable.get_tensor  r   r^   r  )	rZ   r[   r\   r   r   r}  rL  r~  rM  rN  s   @r_   r|  r|    sK    
' 
' $
' 	
'
 

'
S 


C r^   r|  c                   `     e Zd Zdededdf fdZdefdZddd	ee	   de
ee	f   de	fd
Z xZS )'CreateTMADescriptorExperimentalVariablerankr   r`   Nc                 V    |dvrt        d|       t        |   di | || _        y )N)r<   r   zrank must be 1 or 2, got r]   )r   r   r   r  )r}   r  r   r   s      r_   r   z0CreateTMADescriptorExperimentalVariable.__init__  s7    
 v #<TF!CDD"6"	r^   c                 "    t         j                  S r   ry  r   s    r_   rz  z3CreateTMADescriptorExperimentalVariable.python_type  r{  r^   r   rH   r   c                    d|v r|d   n|d   }t        |t        j                        s2t        dd| d| d| j                   dd| j                   d	g
       | j                  dk(  r]t        |      t        |      z   dk7  r$t        |dt        |      t        |      z           d|v r|d   n|d   g}d|v r|d   n|d   g}nvt        |      t        |      z   dk7  r$t        |dt        |      t        |      z           d|v r|d   n|d   d|v r|d   n|d   g}d|v r|d   n|d   d|v r|d   n|d   g}d|v r|d   n|d   }t        |t        j                        st        dt        |             t        ||||      S )Nptrr   z.invalid ptr argument for create_tma_descriptorzargs = z, kwargs = z#Expected `ptr` argument of `create_z]d_tma_descriptor`to be from a `.data_ptr()` call, represented internally by `DataPtrVariable`z`torch.compile` may fail to internally represent result of `.data_ptr()` with `DataPtrVariable` due to a graph break between the `.data_ptr()` call and `create_zTd_tma_descriptor`. Please ensure there were no graph breaks between these two calls.r`  r<   rR   z6TMA metadata rank=1 requires exactly 4 arguments, got r  	block_dimr      z6TMA metadata rank=2 requires exactly 6 arguments, got dim1dim0
block_dim1r  
block_dim0r  r  z#ptr must be a DataPtrVariable, got )r  r  r  r  )
r   r   r  r#   r  r   r!   r   r   r{  )r}   r   r   r   r  r  r  r  s           r_   r>  z5CreateTMADescriptorExperimentalVariable.call_function  s     %fUmDG#y88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 #FtCyk!RSS0!%	
 	
r^   )rZ   r[   r\   r  r   r   r   rz  r   r@   r   rJ  r>  rM  rN  s   @r_   r  r    sj      
	"T ";
';
 ?#;
 S/)*	;

 
;
r^   r  c                   @    e Zd ZdefdZdddee   deeef   defdZ	y)	!CreateTMADescriptorStableVariabler`   c                 "    t         j                  S r   ry  r   s    r_   rz  z-CreateTMADescriptorStableVariable.python_type  r{  r^   r   rH   r   r   c                 T    d|v r|d   n|d   }d|v r|d   n|d   }t        ||      S )Nr  r   r  r<   )r  r  )r|  )r}   r   r   r   r  r  s         r_   r>  z/CreateTMADescriptorStableVariable.call_function!  sF     &.%7!T!W/</Ff]+DQRG*#
 	
r^   N)
rZ   r[   r\   r   rz  r   r@   r   rJ  r>  r]   r^   r_   r  r    sH    "T "
'
 ?#
 S/)*	

 

r^   r  c                   8    e Zd ZdZdddee   deeef   defdZy)	!PyTreeGetNodeTypeFunctionVariableaW  
    `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 collections.namedtuple
        return node_type
    r   rH   r   r   r`   c                    t        |      dk7  rt        |dt        |              d }|d   j                  rMt        |d   j                  j	                  t
        j                               t        |d   j                        }|d   j                         }t        |      r<t        t        d      d      }t        j                  |t        t              d   |      S t        j                  |||      S )Nr<   z6pytree_get_node_type requires exactly 1 argument, got r   collections
namedtupler   )r   r!   r   r&   r   r%   
TYPE_MATCHr0   rz  r   r'   r-   r@   r   varsr  )r}   r   r   r   type_sourcerz  s         r_   r>  z/PyTreeGetNodeTypeFunctionVariable.call_function?  s     t9>LSQUYKX 7>>$q'..33L4K4KLM$T!W^^4K1g))+{+$\-%@,OK"((D%l3[  $$R[IIr^   N)	rZ   r[   r\   r   r   r@   r   rJ  r>  r]   r^   r_   r  r  0  sF    J'J ?#J S/)*	J
 
Jr^   r  c                   D     e Zd ZdZdddee   deeef   def fdZ xZ	S ) PyTreeTreeIsLeafFunctionVariablea@  
    `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   rH   r   r   r`   c                    t        |      dk  st        |      dkD  rt        |dt        |              |j                  dt        j                  d             }t        |      dk(  r|d   }|j                         st        	|   |||      S |d   }t        t        j                  j                  j                        j                  ||gi       }t        j                  |t        j                  j                  j                  t!                     }|j#                  |d|gi       }t        j                  ||j$                         S )Nr<   r   z,tree_is_leaf requires 1 or 2 arguments, got rT   r   r   __contains__)r   r!   r   rB   r   ry  r   r>  r  rb   rc   rd   _get_node_typer@   r   re   rf   r  rV  )
r}   r   r   r   rT   treenode_type_varsupported_nodes_varoutr   s
            r_   r>  z.PyTreeTreeIsLeafFunctionVariable.call_functionf  s#    t9q=CIMB3t9+N
 **Y(8(?(?(EFt9>1gG'')7(T6:: Aw9KK..

-TFB
' 	 .33KK//46

 "--b.=/SUV$$RSYY77r^   r&  rN  s   @r_   r  r  V  sG    #8'#8 ?##8 S/)*	#8
 
#8 #8r^   r  c                   X     e Zd ZdZdededdf fdZddd	ee   dee	ef   defd
Z
 xZS ) SparseTensorCreationSkipVariablezk
    Skip variable for sparse tensor factory functions with clear messaging regarding lack of support.
    rV  r   r`   Nc                 .    d}t        |   |fd|i| y )Nz8sparse tensor creation is not supported in torch.compiler0  )r   r   )r}   rV  r   r0  r   s       r_   r   z)SparseTensorCreationSkipVariable.__init__  s    K8v88r^   r   rH   r   c                     ddl m} t        | j                  dt	        | j                              }t        dd| d| dg |j                         y )	Nr   )r   rZ   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.r`  )rW  r   r   rV  rJ  r#   SPARSE_TENSOR)r}   r   r   r   r   r  s         r_   r>  z.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   rJ  r>  rM  rN  s   @r_   r  r    s\    9c 9S 9T 9
'
 ?#
 S/)*	

 

r^   r  	real_implc                 R    ddl mc m} ddlm}m}m} d }dg} ||||      \  }	}
 ||	      } ||
      }|j                  di f      d   }dt        dt        d	t        f fd
} || d|       || d|       || d|      df} j                  j                  d||i        y)aI  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                       y r   r]   r]   r^   r_   	fake_implz5emit_noargs_leaf_function_to_graph.<locals>.fake_impl  rI  r^   r]   r<   	attr_namer   r`   c                 t    j                   j                  | |      }t        |      |j                  _        |S r   )r   %register_static_attr_and_return_proxyr   r+  )r  r   r  r   s      r_   
make_proxyz6emit_noargs_leaf_function_to_graph.<locals>.make_proxy  s-    		??	3Os)

r^   _real_fn_fake_fn_input_specrW  r>  )torch.utils._pytreerc   rd   ,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   `               r_   "emit_noargs_leaf_function_to_graphr    s    " )(  8<f!<9/"L, ",/M!,/M$$b"X.q1Jc    	dV8$m4dV8$m4dV;'4
	K II?,@+rRr^   c                   d     e Zd ZdZdededdf fdZdefdZdd	d
ee	   de
ee	f   de	fdZ xZS )TritonSetAllocatorVariablezTrace 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   r`   Nc                 2    t        |   di | || _        y r   r;  r<  s      r_   r   z#TritonSetAllocatorVariable.__init__  r=  r^   c                 ,    t        | j                        S r   )r   rV  r   s    r_   rz  z&TritonSetAllocatorVariable.python_type  s    DJJr^   r   rH   r   c                     t        |      dk7  rt        dt        |             |rt        d      |d   j                         | j                  fd}t	        ||d       t        j                  d       S )Nr<   zexpected exactly 1 arg, got zunexpected kwargsr   c                               y r   r]   )alloc_fnset_allocators   r_   r  z;TritonSetAllocatorVariable.call_function.<locals>.real_impl  s    (#r^   	set_alloc)r   r   r   rV  r  rB   r   )r}   r   r   r   r  r  r  s        @@r_   r>  z(TritonSetAllocatorVariable.call_function  sw     t9> #?D	{!KLL !4557--/ 

	 	+2y+F&&t,,r^   )rZ   r[   r\   r   r   r   r   rz  r   r@   r   rJ  r>  rM  rN  s   @r_   r  r    sh    c S T  T  -'- ?#- S/)*	-
 
-r^   r  c                       e Zd ZdZdhej
                  Zdej                  dededdf fdZ	de
fdZdefd	Zdej                  fd
Zdej                  fdZddde
def fdZdddee   dee
ef   defdZdddededdfdZ xZS )WrapperDescriptorVariablea)  Unbound C slot wrapper (wrapper_descriptor on a type).

    CPython types define behavior through C-level slots on PyTypeObject
    (tp_richcompare, sq_length, nb_add, etc.).  When these slots are
    accessed from Python (e.g. list.__add__), CPython exposes them as
    wrapper_descriptor objects (PyWrapperDescr_Type).  A wrapper_descriptor
    is an unbound descriptor living on the type -- it is not tied to any
    instance.
    https://github.com/python/cpython/blob/3.13/Objects/descrobject.c#L867

    When a wrapper_descriptor is accessed on an instance (e.g. [1,2].__add__),
    its tp_descr_get slot (wrapperdescr_get) is invoked, which calls
    PyWrapper_New to produce a bound method-wrapper (_PyMethodWrapper_Type).
    The tp_descr_get_impl method on this class mirrors that binding step.
    
descriptorr  r   r`   Nc                 @    t        |   di | || _        || _        y r   r   r   r  r  r}   r  r  r   r   s       r_   r   z"WrapperDescriptorVariable.__init__  #     	"6"$
r^   c                 |    | j                   j                  j                  }d| d| j                   j                   dS )NzWrapperDescriptorVariable(rX  r  r  __objclass__rZ   r}   cls_names     r_   r  z"WrapperDescriptorVariable.__repr__(  s8    ??//88+H:Qt7O7O6PPQRRr^   c                 "    t         j                  S r   )r   WrapperDescriptorTyper   s    r_   rz  z%WrapperDescriptorVariable.python_type,  s    ***r^   c                     | j                   S r   r  r   s    r_   r   z,WrapperDescriptorVariable.as_python_constant/      r^   c                     | j                   S r   r	  r   s    r_   rr  z6WrapperDescriptorVariable.get_real_python_backed_value2  r
  r^   r   rH   r   c                     |dk(  r*t        j                  || j                  j                        S |dk(  r3t        j
                  j                  | j                  j                        S t        | %  ||      S Nr  rZ   
r@   r   r  r  r   rB   r   rZ   r   r7  r  s      r_   r7  z%WrapperDescriptorVariable.var_getattr5  e     >!"((T__-I-IJJ:--44T__5M5MNNw"2t,,r^   r   c                    |sEt        |d| j                  j                   d| j                  j                  j                   d       |^}}| j                  j                  || j                  j                  |g||      S )Ndescriptor '' of '' object needs an argument)r!   r  rZ   r  r  r  )r}   r   r   r   r  r  s         r_   r>  z'WrapperDescriptorVariable.call_function>  s     t778 9OO0099::TV
 
d
 zz%%((3,,
 	
r^   r  MethodWrapperVariablec                 F    t        | j                  || j                        S rX  )r  r  r   r}   r   r  r  s       r_   r  z+WrapperDescriptorVariable.tp_descr_get_implV  s     %T__c$++NNr^   )rZ   r[   r\   r   r@   r  r   r  r   r   rJ  r  r   rz  r   rr  r7  r   r   r>  r  rM  rN  s   @r_   r  r    s   " 			'	'N
//  	
 
S# S+T +E$?$? e.I.I ---58-	-
'
 ?#
 S/)*	

 

0
O'
O 
O 	
O
 
!
Or^   r  c                   @    e Zd ZdZdhej
                  Zdej                  dededdf fdZ	de
fdZdefd	Zdej                  fd
ZdefdZdej                  fdZdddee   dee
ef   defdZddZdedeeef   f fdZddddde
ddfdZdedefdZ xZS )r  a'  Bound method-wrapper (wrapper_descriptor bound to an instance).

    Produced by WrapperDescriptorVariable.tp_descr_get_impl, mirroring
    PyWrapper_New which stores a reference to the descriptor and the instance.
    https://github.com/python/cpython/blob/3.13/Objects/descrobject.c#L1450
    r  r  r   r`   Nc                 @    t        |   di | || _        || _        y r   r   r   r  r  r}   r  r  r   r   s       r_   r   zMethodWrapperVariable.__init__p  s#     	"6"$r^   c                     | j                   j                  j                  }d| d| j                   j                   d| j                   dS )NzMethodWrapperVariable(rX  r  r  )r  r  rZ   r  r  s     r_   r  zMethodWrapperVariable.__repr__z  sD    ??//88$XJa0H0H/IDHH:UVW	
r^   c                 "    t         j                  S r   )r   MethodWrapperTyper   s    r_   rz  z!MethodWrapperVariable.python_type      &&&r^   c                 "    | j                         S r   rE  r   s    r_   rr  z2MethodWrapperVariable.get_real_python_backed_value  r   r^   c                 6    | j                   j                         S r   )r  r	  r   s    r_   r	  z(MethodWrapperVariable.is_python_constant  s    xx**,,r^   c                 h    | j                   j                  | j                  j                               S r   )r  r  r  r   r   s    r_   r   z(MethodWrapperVariable.as_python_constant  s$    &&txx'B'B'DEEr^   r   rH   r   c                    	 | j                         }|t        |      r|r~t        |d   t        j
                        rat        |      dk(  rt        |      dk(  st        |d       t        t        |j                        }|d   j                  ||j                        S | j                  j                  || j                  j                  t!        |      |      S # t        $ r d }Y w xY w)Nr   r<   z2tensor attribute getter takes exactly one argument)r   r  r7   r   r   rO   r   r!   r   r   r}  r7  rZ   r  r  r  r   )r}   r   r   r   method_wrapperr  s         r_   r>  z#MethodWrapperVariable.call_function  s    	"!446N &*>:47I$<$<=INs6{a'7 L
 c>#:#:;J7&&r:+>+>??xx##B(@(@$t*fUU# # 	"!N	"s   C C('C(c                      || j                          |j                  |j                  | j                  j                               y r   r  r  create_load_attrsr  rZ   rn  s     r_   rL  z!MethodWrapperVariable.reconstruct  1    g778P8PQRr^   c                 x    	 t        | j                               dfS # t        $ r t        |   |      cY S w xY wr=  )r  r   r  r   r  )r}   r   r   s     r_   r  zMethodWrapperVariable.hash_impl  sB    	) //12E99" 	)7$R((	)s    99r   r@   r   c                 $    ddl m}  || |||      S Nr<   ) python_constant_richcompare_implr   r+  r}   r   r   r   r+  s        r_   r   z&MethodWrapperVariable.richcompare_impl       	F/b%DDr^   c                     t        |t              sy	 | j                         |j                         k(  S # t        $ r Y yw xY wr=  )r   r@   r   r  r  s     r_   r  z%MethodWrapperVariable.is_python_equal  sC    %1	**,0H0H0JJJ" 		s    4 	A A r  )rZ   r[   r\   r   r@   r  r   r  r   r   rJ  r  r   rz  r  rr  rL  r	  r   r   r   r>  rL  r   r  r  r   r  r  rM  rN  s   @r_   r  r  c  s;    			'	'N
//  	
 

# 
'T ')e.E.E )-D -FE$;$; FV'V ?#V S/)*	V
 
V6S)C )E#t)$4 )E-E6GEMPE	EV  r^   r  c                       e Zd ZdZdhej
                  Zdej                  dededdf fdZ	de
fdZdefd	Zdej                  fd
Zdej                  fdZddde
def fdZdddee   dee
ef   defdZdddededdfdZ xZS )MethodDescriptorVariablea  Unbound C method descriptor (method_descriptor on a type).

    CPython types expose their PyMethodDef-based C methods as
    method_descriptor objects (PyMethodDescr_Type) in the type's tp_dict.
    For example, list.append and dict.get are method_descriptors.  Like
    wrapper_descriptors, these are unbound descriptors living on the type.
    https://github.com/python/cpython/blob/3.13/Objects/descrobject.c#L716

    When a method_descriptor is accessed on an instance (e.g. [].append),
    its tp_descr_get slot (method_get) is invoked, which calls
    PyCFunction_NewEx to produce a bound builtin_function_or_method
    (PyCFunction_Type).  The tp_descr_get_impl method mirrors that step.
    https://github.com/python/cpython/blob/3.13/Objects/descrobject.c#L137-L159
    r  r  r   r`   Nc                 @    t        |   di | || _        || _        y r   r  r  s       r_   r   z!MethodDescriptorVariable.__init__  r   r^   c                 |    | j                   j                  j                  }d| d| j                   j                   dS )NzMethodDescriptorVariable(rX  r  r  r  s     r_   r  z!MethodDescriptorVariable.__repr__  7    ??//88*8*Adoo6N6N5OqQQr^   c                 "    t         j                  S r   )r   MethodDescriptorTyper   s    r_   rz  z$MethodDescriptorVariable.python_type      )))r^   c                     | j                   S r   r	  r   s    r_   r   z+MethodDescriptorVariable.as_python_constant  r
  r^   c                     | j                   S r   r	  r   s    r_   rr  z5MethodDescriptorVariable.get_real_python_backed_value  r
  r^   r   rH   r   c                     |dk(  r*t        j                  || j                  j                        S |dk(  r3t        j
                  j                  | j                  j                        S t        | %  ||      S r  r  r  s      r_   r7  z$MethodDescriptorVariable.var_getattr  r  r^   r   c           
         |sEt        |d| j                  j                   d| j                  j                  j                   d       |^}}| j                  j                  }	 |j	                         }t        || j                  j                        s>t        |d| d| j                  j                  j                   d|j                   d       | j                  j                  |||g||      S # t        $ r Y ,w xY w)Nr  r  r  z' for 'z' objects doesn't apply to a 'z' object)	r!   r  rZ   r  rz  r.  r  r  r  )r}   r   r   r   r  r  r   obj_types           r_   r>  z&MethodDescriptorVariable.call_function  s    t778 9OO0099::TV
 
d''
	(Hh(D(DE "4& )44==> ?++3+<+<*=XG zz%%b$tfEE # 		s   #A.C2 2	C>=C>r  BoundBuiltinMethodVariablec                 F    t        | j                  || j                        S rX  r=  r  r   r  s       r_   r  z*MethodDescriptorVariable.tp_descr_get_impl  s     *$//3t{{SSr^   )rZ   r[   r\   r   r@   r  r   r6  r   r   rJ  r  r   rz  r   rr  r7  r   r   r>  r  rM  rN  s   @r_   r1  r1    s     			'	'N
..  	
 
R# R*T *E$>$> e.H.H ---58-	-F'F ?#F S/)*	F
 
F@
T'
T 
T 	
T
 
&
Tr^   r1  c                        e Zd ZdZdhej
                  Zdej                  ej                  z  ej                  z  dede
ddf fdZdefdZdefd	Zde
fd
Zdddee   deeef   defdZddZ xZS )r=  a  Bound builtin_function_or_method (PyCFunction_Type).

    Produced by MethodDescriptorVariable.tp_descr_get_impl (binding a
    method_descriptor to an instance, e.g. [].append) or created by the
    builder for bound C methods (e.g. frozenset().__contains__,
    tuple.__new__).  The backing descriptor can be a MethodDescriptorType
    or a BuiltinFunctionType (for methods stored directly in type dicts).
    https://github.com/python/cpython/blob/3.13/Objects/methodobject.c#L331
    r  r  r   r`   Nc                 @    t        |   di | || _        || _        y r   r  r  s       r_   r   z#BoundBuiltinMethodVariable.__init__5  s#     	"6"$r^   c                     t        t        | j                  dd       dd      }d| d| j                  j                   d| j                   dS )Nr  rZ   ?zBoundBuiltinMethodVariable(rX  r  r  )r   r  rZ   r  r  s     r_   r  z#BoundBuiltinMethodVariable.__repr__A  sR    DOO^T:J
 -XJa8P8P7QQSTXT\T\S]]^__r^   c                 "    t         j                  S r   )r   BuiltinMethodTyper   s    r_   rz  z&BoundBuiltinMethodVariable.python_typeG  r  r^   c                 X   | j                   j                         }t        | j                  t        j
                        r| j                  j                  d |      S t        | j                  d      r| j                  j                  |      S t        || j                  j                        S )Nr  )
r  r   r   r  r   ClassMethodDescriptorTyper  r@  r   rZ   )r}   r  s     r_   r   z-BoundBuiltinMethodVariable.as_python_constantJ  sy    hh))+doou'F'FG??**4554??I.??**3//sDOO4455r^   r   rH   r   c                 x    | j                   j                  || j                  j                  t	        |      |      S r   )r  r  r  rZ   r   r=  s       r_   r>  z(BoundBuiltinMethodVariable.call_functionR  s-     xx##B(@(@$t*fUUr^   c                      || j                          |j                  |j                  | j                  j                               y r   r%  rn  s     r_   rL  z&BoundBuiltinMethodVariable.reconstructZ  r'  r^   r  )rZ   r[   r\   r   r@   r  r   r6  r?  rG  r   r   rJ  r  r   rz  r   r   r   r>  rL  rM  rN  s   @r_   r=  r=  %  s     			'	'N

..

#
#$

)
)*

 
 
 

`# `'T '6C 6V'V ?#V S/)*	V
 
VSr^   r=  c                        e Zd ZdZdhej
                  Zdej                  deddf fdZ	de
fdZdefdZdej                  fd	Zdej                  fd
Zddde
def fdZdddededefdZ xZS )ClassMethodDescriptorVariableay  C-level classmethod descriptor (classmethod_descriptor on a type).

    CPython exposes C classmethods defined via PyMethodDef with METH_CLASS
    as classmethod_descriptor objects (PyClassMethodDescr_Type).  For
    example, dict.fromkeys is a classmethod_descriptor.  Like
    method_descriptor, these live on the type and are unbound.
    https://github.com/python/cpython/blob/3.13/Objects/descrobject.c#L756

    classmethod_get binds the C method to the class (ignoring obj) via
    PyCMethod_New, producing a bound builtin_function_or_method.
    https://github.com/python/cpython/blob/3.13/Objects/descrobject.c#L94-L134
    r  r   r`   Nc                 2    t        |   di | || _        y r   r   r   r  r}   r  r   r   s      r_   r   z&ClassMethodDescriptorVariable.__init__r      
 	"6"$r^   c                 |    | j                   j                  j                  }d| d| j                   j                   dS )NzClassMethodDescriptorVariable(rX  r  r  r  s     r_   r  z&ClassMethodDescriptorVariable.__repr__z  s8    ??//88/z4??;S;S:TTUVVr^   c                 "    t         j                  S r   )r   rG  r   s    r_   rz  z)ClassMethodDescriptorVariable.python_type~  s    ...r^   c                     | j                   S r   r	  r   s    r_   r   z0ClassMethodDescriptorVariable.as_python_constant  r
  r^   c                     | j                   S r   r	  r   s    r_   rr  z:ClassMethodDescriptorVariable.get_real_python_backed_value  r
  r^   r   rH   r   c                     |dk(  r*t        j                  || j                  j                        S |dk(  r3t        j
                  j                  | j                  j                        S t        | %  ||      S r  r  r  s      r_   r7  z)ClassMethodDescriptorVariable.var_getattr  e     >!"((T__-I-IJJ:--44T__5M5MNNw"2t,,r^   r  r  c                 F    t        | j                  || j                        S rX  r?  r  s       r_   r  z/ClassMethodDescriptorVariable.tp_descr_get_impl  s     *$//5UUr^   )rZ   r[   r\   r   r@   r  r   rG  r   r   rJ  r  r   rz  r   rr  r7  r=  r  rM  rN  s   @r_   rK  rK  _  s     			'	'N
%33% % 
	%W# W/T /E$C$C e.M.M 
--
-58
-	
-	V'	V 	V 		V
 
$	Vr^   rK  c                        e Zd ZdZdhej
                  Zdededdf fdZde	fdZ
defdZdefd	Zd
ddedz  dedefdZ xZS )StaticMethodVariablea  staticmethod descriptor wrapping a callable.

    CPython's staticmethod (PyStaticMethod_Type) is a non-data descriptor
    whose tp_descr_get (sm_descr_get) simply returns the wrapped callable,
    ignoring both obj and type.
    https://github.com/python/cpython/blob/3.13/Objects/funcobject.c#L1520
    https://github.com/python/cpython/blob/3.13/Objects/funcobject.c#L1418-L1428
    r  r   r`   Nc                 2    t        |   di | || _        y r   rM  rN  s      r_   r   zStaticMethodVariable.__init__  rO  r^   c                 P    t        | j                  j                  dd      }d| dS )NrZ   rC  zStaticMethodVariable(r  r   r  r  r}   	func_names     r_   r  zStaticMethodVariable.__repr__  s)    DOO44j#F	&yk33r^   c                     t         S r   )r  r   s    r_   rz  z StaticMethodVariable.python_type  r*  r^   c                     | j                   S r   r	  r   s    r_   r   z'StaticMethodVariable.as_python_constant  r
  r^   r   rH   r  r  c                     | j                   rt        | j                   d      nd }t        j                  || j                  j
                  |      S )Nr  )r   r'   r@   r   r  r  )r}   r   r  r  func_sources        r_   r  z&StaticMethodVariable.tp_descr_get_impl  s<     >B[[jj9d$$R)A)A;OOr^   )rZ   r[   r\   r   r@   r  r  r   r   rJ  r  r   rz  r   r  rM  rN  s   @r_   rX  rX    s     			'	'N
% % % 
	%4# 4T L 	P'	P t#	P 		P
 
	Pr^   rX  c                        e Zd ZdZdhej
                  Zdededdf fdZde	fdZ
defdZdefd	Zd
ddededefdZ xZS )ClassMethodVariablea  classmethod descriptor wrapping a callable.

    CPython's classmethod (PyClassMethod_Type) is a non-data descriptor
    whose tp_descr_get (cm_descr_get) creates a bound method of the
    wrapped callable bound to the class (via PyMethod_New).
    https://github.com/python/cpython/blob/3.13/Objects/funcobject.c#L1314
    https://github.com/python/cpython/blob/3.13/Objects/funcobject.c#L1215-L1227
    r  r   r`   Nc                 2    t        |   di | || _        y r   rM  rN  s      r_   r   zClassMethodVariable.__init__  rO  r^   c                 P    t        | j                  j                  dd      }d| dS )NrZ   rC  zClassMethodVariable(r  r[  r\  s     r_   r  zClassMethodVariable.__repr__  s)    DOO44j#F	%i[22r^   c                     t         S r   )r  r   s    r_   rz  zClassMethodVariable.python_type  s    r^   c                     | j                   S r   r	  r   s    r_   r   z&ClassMethodVariable.as_python_constant  r
  r^   r   rH   r  r  c                    | j                   rt        | j                   d      nd }|j                   r4t        |j                   | j                  j                  j                        nd }t        | j                  j                  |||      S )Nr  r  )r   r'   r  r  rZ   r  )r}   r   r  r  ra  bound_sources         r_   r  z%ClassMethodVariable.tp_descr_get_impl  ss     >B[[jj9d || u||T__%=%=%F%FG 	
 "OO$$!	
 	
r^   )rZ   r[   r\   r   r@   r  r  r   r   rJ  r  r   rz  r   r  rM  rN  s   @r_   rc  rc    s     			'	'N
%% % 
	%3# 3T K 
'
 
 	

 

r^   rc  c                        e Zd ZdZdhej
                  Zdej                  deddf fdZ	de
fdZdefdZdej                  fd	Zd
dde
def fdZd
ddededefdZ xZS )MemberDescriptorVariablea  C struct field descriptor (member_descriptor on a type).

    CPython exposes C struct fields defined via PyMemberDef as
    member_descriptor objects (PyMemberDescr_Type).  These are data
    descriptors used by __slots__ and C extension types to provide
    direct access to struct members.
    https://github.com/python/cpython/blob/3.13/Objects/descrobject.c#L793

    member_get reads the field via PyMember_GetOne.
    https://github.com/python/cpython/blob/3.13/Objects/descrobject.c#L162-L180
    r  r   r`   Nc                 2    t        |   di | || _        y r   rM  rN  s      r_   r   z!MemberDescriptorVariable.__init__  rO  r^   c                 |    | j                   j                  j                  }d| d| j                   j                   dS )NzMemberDescriptorVariable(rX  r  r  r  s     r_   r  z!MemberDescriptorVariable.__repr__  r4  r^   c                 "    t         j                  S r   )r   MemberDescriptorTyper   s    r_   rz  z$MemberDescriptorVariable.python_type!  r7  r^   c                     | j                   S r   r	  r   s    r_   r   z+MemberDescriptorVariable.as_python_constant$  r
  r^   r   rH   r   c                     |dk(  r*t        j                  || j                  j                        S |dk(  r3t        j
                  j                  | j                  j                        S t        | %  ||      S r  r  r  s      r_   r7  z$MemberDescriptorVariable.var_getattr'  rU  r^   r  r  c           
         | j                   j                  }t        |dd       }|	 |j                         }	 | j                   j                  |      }|j                  xr t        |j                  |      }t        j                  ||      S # t        $ r |j                  ||      cY S w xY w# t        $ r0 t        t        |dt        |      j                   d| dg       Y w xY w)NrV  '' object has no attribute 'r  )r  rZ   r   r   r  r7  r  r   r    r   r   r'   r@   r   )r}   r   r  r  r  	obj_valueresolvedresult_sources           r_   r  z*MemberDescriptorVariable.tp_descr_get_impl3  s     OO,,	C$/	6224			..y9H 

Hz#**i'H$$R=AA ' 6r9556  	$Y0011LYKWXY	s"   B B/ B,+B,/6C('C()rZ   r[   r\   r   r@   r  r   ro  r   r   rJ  r  r   rz  r   r7  r  rM  rN  s   @r_   rk  rk    s    
 			'	'N
%..% % 
	%R# R*T *E$>$> 
--
-58
-	
-B'B B 	B
 
Br^   rk  c                       e Zd ZdZdhej
                  Zdej                  deddf fdZ	de
fdZdej                  fdZd	d
de
def fdZdefdZdej                  fdZd	d
ddde
ddfdZd	d
dededefdZ xZS )GetSetDescriptorVariablea  C getter/setter descriptor (getset_descriptor on a type).

    CPython exposes C getter/setter pairs defined via PyGetSetDef as
    getset_descriptor objects (PyGetSetDescr_Type).  These are data
    descriptors used for computed attributes backed by C functions
    (e.g. object.__class__, type.__dict__).
    https://github.com/python/cpython/blob/3.13/Objects/descrobject.c#L830

    getset_get calls the C getter function.
    https://github.com/python/cpython/blob/3.13/Objects/descrobject.c#L183-L197
    r  r   r`   Nc                 2    t        |   di | || _        y r   rM  rN  s      r_   r   z!GetSetDescriptorVariable.__init__c  s    "6"$r^   c                 |    | j                   j                  j                  }d| d| j                   j                   dS )NzGetSetDescriptorVariable(rX  r  r  r  s     r_   r  z!GetSetDescriptorVariable.__repr__g  r4  r^   c                     | j                   S r   r	  r   s    r_   rr  z5GetSetDescriptorVariable.get_real_python_backed_valuek  r
  r^   r   rH   r   c                 n   |dk(  rM| j                   rAt        | j                   d      }t        j                  || j                  j
                  |      S |dv rO| j                   xr t        | j                   |      }t        j                  |t        | j                  |      |      S t        | !  ||      S )Nr  )r  rZ   )	r   r'   r@   r   r  r  r   r   r7  r  s       r_   r7  z$GetSetDescriptorVariable.var_getattrn  s     9Y7F"((T__-D-DfMM11[[BZT%BF"((WT__d-KVTT7&r400r^   c                      yrG  r]   r   s    r_   r	  z+GetSetDescriptorVariable.is_python_constantz  rI  r^   c                     | j                   S r   r	  r   s    r_   r   z+GetSetDescriptorVariable.as_python_constant}  r
  r^   r   r@   r   c                 $    ddl m}  || |||      S r*  r,  r-  s        r_   r   z)GetSetDescriptorVariable.richcompare_impl  r.  r^   r  r  c           
         | j                   j                  }t        |dd       }|	 |j                         }	 | j                   j                  |      }|j                  xr t        |j                  |      }|j                  rk| j                   j                  t        u rO|dv rKt        j                  t        |      |d       }|| j                   urd }n|dk(  rt        |j                        }t!        j"                  ||      S # t        $ r |j                  ||      cY S w xY w# t        $ r1 t        t        |dt        |      j                   d| dg       Y w xY w)NrV  rs  rt  r  )r   r)  __mro__r  )r  rZ   r   r   r  r7  r  r   r    r   r   r'   r  r   r   r/   r@   r   )	r}   r   r  r  r  ru  rv  rw  static_descs	            r_   r  z*GetSetDescriptorVariable.tp_descr_get_impl  sQ    OO,,	
 C$/	6224			..y9H 

Hz#**i'HJJ,,4GG
 "00i)TRK$//1 $i' -cjj 9$$R=AA5 ' 6r9556  	$Y0011LYKWXY 	s"   D D& D#"D#&6E E )rZ   r[   r\   r   r@   r  r   GetSetDescriptorTyper   r   rJ  r  rr  r7  rL  r	  r   r   r  rM  rN  s   @r_   ry  ry  Q  s    
 			'	'N
%5#=#= % %QU %R# Re.H.H 
1-
158
1	
1D E$>$> E-E6GEMPE	E+B'+B +B 	+B
 
+Br^   ry  c                        e Zd ZdZdhej
                  Zdededdf fdZde	fdZ
defdZdefd	Zd
ddedz  dedefdZ xZS )PropertyVariableaA  Python property descriptor.

    The property type is a data descriptor with tp_descr_get =
    property_descr_get which calls fget(obj) to compute the value.
    https://github.com/python/cpython/blob/3.13/Objects/descrobject.c#L2073
    https://github.com/python/cpython/blob/3.13/Objects/descrobject.c#L1660-L1693
    r  r   r`   Nc                 2    t        |   di | || _        y r   rM  rN  s      r_   r   zPropertyVariable.__init__  rO  r^   c                 P    t        | j                  j                  dd      }d| dS )NrZ   rC  zPropertyVariable(r  )r   r  fget)r}   	fget_names     r_   r  zPropertyVariable.__repr__  s)    DOO00*cB	"9+Q//r^   c                     t         S r   )r7  r   s    r_   rz  zPropertyVariable.python_type  s    r^   c                     | j                   S r   r	  r   s    r_   r   z#PropertyVariable.as_python_constant  r
  r^   r   rH   r  r  c                     || S | j                   rt        | j                   d      nd }t        j                  || j                  j
                  |d      }|j                  ||gi       S )Nr  T)r   r  )r   r'   r@   r   r  r  r>  )r}   r   r  r  fget_sourcefget_vts         r_   r  z"PropertyVariable.tp_descr_get_impl  sb     ;K9=jf5$!''$$[$
 $$R#33r^   )rZ   r[   r\   r   r@   r  r7  r   r   rJ  r  r   rz  r   r  rM  rN  s   @r_   r  r    s     			'	'N
%% % 
	%0# 0T H 4'4 t#4 	4
 
4r^   r  c                        e Zd ZdZdhej
                  Zdddeddf fdZdefdZ	de
fd	Zdd
Zdddedef fdZdddedz  dedefdZ xZS )TupleGetterVariablea  _tuplegetter descriptor used by namedtuple for field access.

    _tuplegetter is a C data descriptor that stores an index and returns
    self[index] on instance access. When accessed on the class (obj=None),
    it returns the descriptor itself.
    https://github.com/python/cpython/blob/3.13/Modules/_collectionsmodule.c#L2735
    https://github.com/python/cpython/blob/3.13/Modules/_collectionsmodule.c#L2636-L2663
    r  _collections._tuplegetterr   r`   Nc                 2    t        |   di | || _        y r   rM  rN  s      r_   r   zTupleGetterVariable.__init__  rO  r^   c                 T    | j                   j                         \  }\  }}d| d|dS )NzTupleGetterVariable(index=z, doc=r  )r  
__reduce__)r}   r  r   docs       r_   r  zTupleGetterVariable.__repr__  s1    224:C+C5sgQ??r^   c                 "    t         j                  S r   )_collections_tuplegetterr   s    r_   rz  zTupleGetterVariable.python_type  s    (((r^   c                     | j                   S r   r	  r   s    r_   r   z&TupleGetterVariable.as_python_constant  r
  r^   r   rH   r   c                     |dk(  r*t        j                  || j                  j                        S t        |   ||      S )Nr   )r@   r   r  r   r   r7  r  s      r_   r7  zTupleGetterVariable.var_getattr  s<     9"((T__-D-DEEw"2t,,r^   r  r  c                     || S | j                   j                         \  }\  }}|j                  |dt        j                  j                  |      gi       S )N__getitem__)r  r  r  r   rB   r   )r}   r   r  r  r  r   s         r_   r  z%TupleGetterVariable.tp_descr_get_impl  sV     ;Koo0028C	 : : A A# FG
 	
r^   )r`   r  )rZ   r[   r\   r   r@   r  r   r   rJ  r  r   rz  r   r7  r  rM  rN  s   @r_   r  r    s     			'	'N
%/% % 
	%@# @)T )---58-	-
'
 t#
 	

 

r^   r  r   )r   r  r,  r  r  importlib.metadatarR  importlib.utilr   r   r  r  rer  r  r   r  collections.abcr   r   r   r   r   r   r	   r
   r   r   typing_extensionsr   weakrefr   rb   torch._dynamo.excr   torch._guardsr   r  r   rW  r   r   r   r   bytecode_transformationr   r   r   rk  r   r   r   r   r   r   r    r!   r"   r#   r$   guardsr%   r&   r   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   rc   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r  r=   r>   r?   r@   r  rB   user_definedrC   #torch.distributed.fsdp._fully_shardrD   ModuleNotFoundErrortorch._dynamo.codegenrE   rH  rF   rG   rH   #torch._dynamo.variables.ctx_managerrI   r  rJ   rK   rz  rL   rX  rM   rN   r  rO   rP   ry   r{   r  r  r  r]  rW   r   r  rY   	lru_cacherf   rh   r   r   rJ  r   r   r   rK  r   r   r  r   r   r   r   rP  r!  r  r5  r  r  r  r  r  r  r   r  r  r)  searchmatch	fullmatchcompilesubsubnr  findallescaperW  r/  ro  ru  r  r  r  r  r  r  r  r9  rI  rJ  rK  rL  rN  r  rn  r{  r|  r  r  r  r  r  r  r  r  r  r1  r=  rK  rX  rc  rk  ry  r  r  r]   r^   r_   <module>r     s  .          	 	 
    . ( G G # %  4   3 > > V V    1       ' 3E
 / 
 L
 *5& T#s(+,
&'OP "+YK"8  D  ,=+>sCx( >	0 	 :  
 
>L \ \
\#\ }\ 3-	\
 cN\ 
#
\@ HLH#H*-H7=}HH.4 .d38n .QU . ' o%&  ..  
	 *
..CH~  FCK 4'	
 8_t# S#X% c3h$& : -#-)/-9?$-NQ--:W WttY3 tYn!73 !7H)": )XC;? C;L

 
U6%= U6p#"#LI- IX > 2  >F>"6 >D$#$c$ $ 
	$
 o%&$ $Ne)!9 e)P	.>(B .>d IIHHLLJJFFGGHHJJII
 S
? S
l >"6  >Fs=": s=l ; 0d38n 
/#
/)1#s();
/
/nD(< nDb"
$8 "
Jy
 y
xK K\$"/ $"N z
> z
z $8#9  6? 6r') ')T/ DI
o I
X
 
&#J(< #JL38'; 38l
'; 
:/S#/SD!/S /S 
	/Sd!- !-ZXO XOv]O ]@_T _TD7S 7St=VO =V@*P? *PZ4
/ 4
nKB KB\aB aBH.4 .4b4
/ 4
K  s   T
 
TT