
    9io                    D   S r SSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSK	J
r
  SSKJr  SSKJrJrJrJrJr  SSKJr  SSKJr  SSKJr  SSKrSS	KJr  S
SKJrJrJrJr  S
SK J!r!J"r"J#r#  S
SK$J%r%J&r&J'r'J(r(J)r)J*r*J+r+J,r,J-r-J.r.  S
SK/J0r0J1r1  S
SK2J3r3J4r4J5r5J6r6J7r7J8r8  S
SK9J:r:J;r;J<r<J=r=J>r>J?r?J@r@JArAJBrB  SSKCJDrDJErEJFrFJGrG  SSKHJIrI   SSKJJKrK  \(       a  SSKMJNrN  SSKOJPrP  SSKQJRrRJSrS  \" S\S9rTSrUSrV\" 5       rW " S S5      rXS\S\X4S  jrYS! rZSgShS" jjr[ShS# jr\S$\]\^\G4   4S% jr_S& r`1 S'kraS( rb " S) S*\G5      rc " S+ S,\c5      rd " S- S.\c5      re " S/ S0\G5      rf " S1 S2\f5      rg " S3 S4\c5      rh " S5 S6\h5      ri " S7 S8\d5      rj " S9 S:\j5      rk " S; S<\d5      rlShS= jrm " S> S?\c5      rn " S@ SA\n5      ro " SB SC\G5      rp " SD SE\p5      rq " SF SG\G5      rr " SH SI\r5      rsSJ rtShSK jru " SL SM\d5      rv " SN SO\d5      rw " SP SQ\d5      rx " SR SS\G5      ry " ST SU\G5      rz " SV SW\G5      r{ " SX SY\G5      r|SSZKQJ}r}J~r~JrJr   " S[ S\\5      r\" 5       r " S] S^\G5      r " S_ S`\G5      r " Sa Sb\G5      r " Sc Sd\G5      r " Se Sf\G5      rg! \L a    SrK GNf = f)ia  
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)SequenceFunctionType)AnyCallableOptionalTYPE_CHECKINGTypeVar)Never)patch)WeakKeyDictionary)get_stack_above_dynamo   )configgraph_break_hints	polyfills	variables)create_call_functioncreate_rot_nis_generator)
get_dynamo_observed_exceptionhandle_observed_exceptionInfiniteGeneratorErrorObservedExceptionObservedGeneratorExitObservedUserStopIterationraise_observed_exception	SkipFrameunimplemented_v2Unsupported)GuardBuilderinstall_guard)
AttrSourceClosureSourceConstantSourceDefaultsSourceGetItemSourceSkipGuardSource)	check_constant_argscheck_unspec_or_constant_argscmp_name_to_op_mappingcountersidentityis_functionis_wrapper_or_member_descriptoristype	make_cell   )#AsPythonConstantNotImplementedErrorAttributeMutationNewValueMutationNewVariableTrackerConstantVariable)_fsdp_param_group)	PyCodegenInstructionTranslator)TritonGridTypeTritonKernelType_F)bound      c                   0    \ rS rSrS\4S jrS\4S jrSrg)FunctionSpeco   funcc                    UR                   nUR                  nUR                  U l        UR                  U l        UR                  U l        US U R                   U l        X0R                  U R
                   U l	        U R                  U R                  -   U l
        X0R
                  U R
                  U R                  -    U l        U R
                  U R                  -   nUR                  [        -  (       a  X4   OS U l        X@R                  (       a  SOS-  nUR                  [        -  (       a
  X4   U l        g S U l        g )Nr2   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)selfrF   codevnoffs        a/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/torch/_dynamo/variables/functions.py__init__FunctionSpec.__init__p   s    }}!44)) 22 4$"4"45!"4"4t~~F!//$2F2FF~~ARAR0RSnnt000'+}}z'ABGt%%q1,%)]]^%C"'    c           
         UR                   =(       d    SU l        UR                  =(       d    0 U l        [	        [        U R                  [        U R                  5      * S  [        [        U R                  5      5      5      5      U l	        g N )
__defaults__defaults__kwdefaults__
kwdefaultsdictziprR   lenrangepos_default_map)rY   rF   s     r]   update_defaultsFunctionSpec.update_defaults   si     ))/R--3  $""C$6#6#895T]]AS;TU 
r`   )rR   rM   re   rg   rO   rS   rl   rQ   rK   rP   rV   rX   N)__name__
__module____qualname____firstlineno__r   r^   rm   __static_attributes__rc   r`   r]   rD   rD   o   s    N\ N$	
L 	
r`   rD   rF   returnc                 ^    [         R                  U 5      nUc  [        U 5      nU[         U '   U$ N)_spec_cachegetrD   )rF   specs     r]   	_get_specrz      s.    ??4 D|D! DKr`   c                    [        U 5      nUR                  U 5        0 n[        U5      n[        UR                  5       GH4  u  pU[        U5      :  a  [        XU   5      Xi'   M'  X;   aT  XR                  ;   a'  [        [        U[        R                  " U	 S35      /S9  [        XR                  U	5      5      Xi'   M  XR                  ;   a~  UR                  U	   n
S nU(       aH  [        R                  " UR                  U
   5      (       a  [         R"                  (       d  [%        X*5      n[        XR                  U
   U5      Xi'   GM  [        [        U[        R                  " SU	 35      /S9  GM7     U[        UR                  5      S  nUR&                  (       a"  [        U[)        U5      5      XeR&                  '   OMU(       aF  [        [        U[        R                  " S[        U5       S[        UR                  5       35      /S9  UR*                   H  n	X;   a  [        XR                  U	5      5      Xi'   M&  XR,                  ;   a0  S nU(       a
  [%        X)SS9n[        XR,                  U	   U5      Xi'   Me  [        [        U[        R                  " SU	 35      /S9  M     UR.                  (       a  [        X5      XeR.                  '   U$ U(       a0  [        [        U[        R                  " S	[1        U5       35      /S9  U$ )
Nz is positional-onlyargsz&Missing required positional argument: z#Too many positional arguments: got z, expected T)is_kwz(Missing required keyword-only argument: zUnexpected keyword arguments: )rz   rm   rh   	enumeraterR   rj   wrap_bound_argrP   r   	TypeErrorr8   createpoprl   
is_literalre   r   %skip_guards_on_constant_func_defaultsr&   rV   tuplerS   rg   rX   list)rF   tx	fn_sourcer}   kwargsry   barem_kwinameidxdefault_sourceextrakwdefault_sources                 r]   bind_args_cachedr      s   T?D	B&\F T//0s4y=%bq'2BH^)))(*11TF:M2NOP
 &b**T*:;BH)))&&t,C!N ++DMM#,>??@@!/	!?%b--*<nMBH$$++@G+ 1@ T''(*+E .r5< @	  ''9#d)KPSTXTfTfPgOhi	
 !!>%b**T*:;BH__$##1)#N %b//$*?AQRBH$$++B4&I "( ,R8?? I 
  ''*Hf(WX	
 Ir`   c                     [        U[        5      (       a  U$ U(       d  [        R                  " X5      $ [        R                  R                  X5      $ rv   )
isinstancer6   buildr   LazyVariableTrackerr   )r   valsources      r]   r   r      sA    #''
$$R-- ,,33C@@r`   c                     [        UR                  5       5       H/  u  p#[        U[        [        45      (       d  M"  [        X5      X'   M1     g rv   )r   itemsr   r   rh   r   )r   resultkvs       r]   wrap_args_kwargsr      s6    V\\^$a%''&r-FI %r`   r   c                     U R                   R                  nUR                   H<  nUR                  5       nXA;   a   UR	                  XQR                  U5      5        XQU'   M>     g)z
Update `result` to add mapping from local name to new cells created
directly by `code`, or update SideEffects in `parent` if the a local cell is
already in `result` (cell argument).
N)outputside_effectsco_cellvarstrack_cell_new
store_cellr   )parentr   rZ   r   r   new_cells         r]   init_cellvarsr     sV     ==--L  ..0> ##Hjj.>?t !r`   c                     SSK Jn  U" XX#U5      nXXl        [        U[        5      (       a  SSKJn	  [        U	" U5      5      nUb  [        U[        5      (       d   eXhl        U$ )Nr   r   pairwise)	typesr   rf   r   r   	itertoolsr   rh   __annotations__)
rZ   	f_globalsr   re   closurerg   annotationsr   rF   r   s
             r]   _create_nested_fnr     s_     #AD$+u%%&8K01 *[$"?"???&Kr`   >   __doc__rH   __closure____globals__rd   rf   r   c                 n   U=(       a    [        X#5      nU(       a  US:X  a  [        U5      n [        R                  " X5      nU[        ;   a  [        X5      nU(       a   [        R                  R                  WU5      $ [        R                  " U W5      $ ! [         a    [        [        U 5         Nof = f)Nr   )r#   r(   inspectgetattr_staticAttributeErrorr   fn_known_dunder_attrsgetattrr   r   r   r6   r   )r   fnr   r   subobjs        r]   fn_var_getattrr   1  s    0
60F$++ !(5''1 $$",,33FFCC  V,,  5 	!45s   B B43B4c                   \    \ rS rSrS rS r        SS jrSSS\S\4S	 jr	S
 r
S rSrg)BaseUserFunctionVariableiI  c                 6    U R                  5       R                  $ rv   get_codeco_filenamerY   s    r]   get_filename%BaseUserFunctionVariable.get_filenameJ      }}***r`   c                 6    U R                  5       R                  $ rv   r   co_namer   s    r]   get_name!BaseUserFunctionVariable.get_nameM      }}&&&r`   r   r<   rt   c                 L    UR                  U / U R                  5       QUQU5      $ rv   )inline_user_function_return	self_argsrY   r   r}   r   s       r]   call_function&BaseUserFunctionVariable.call_functionP  s,     --d4Ndnn6F4N4NPVWWr`   r   c                     Sn [        U R                  5       U5      n[
        R                  R                  U5      $ ! [         a     US:X  a  [        U [        5      (       a  Sn NHf = f)NFro   T)hasattrget_functionNotImplementedErrorr   NestedUserFunctionVariabler   r8   r   rY   r   r   r   s       r]   call_obj_hasattr)BaseUserFunctionVariable.call_obj_hasattrX  sh     	T..0$7F ))0088 # 	z!j7Q&R&R	s   = 'A'&A'c                 p    [        [        R                  " U R                  5       5      R                  5      $ rv   )r   r   	signaturer   
parametersr   s    r]   inspect_parameter_names0BaseUserFunctionVariable.inspect_parameter_namesd  s'    G%%d&7&7&9:EEFFr`   c                     0 $ rv   rc   rY   r   s     r]   closure_vars%BaseUserFunctionVariable.closure_varsg      	r`   rc   Nr   r<   r}   list[VariableTracker]r   dict[str, VariableTracker]rt   r6   )ro   rp   rq   rr   r   r   r   strr6   r   r   r   rs   rc   r`   r]   r   r   I  sd    +'X#X &X -	X
 
X
9)
914
9	
9Gr`   r   c                      ^  \ rS rSrSrSS1\R                  kr\S 5       rSSU 4S jjjr	U 4S jr
S	 rS
 rS rS rS rS rS rS\\\4   4S jrSSS\4S jrSSS\S\4S jr        SU 4S jjrSrU =r$ )UserFunctionVariableik  z-Some unsupported user-defined global functionr   is_constantc                 ^    [        UR                  [        R                  5      5        U " XS9$ Nr   )r"   
make_guardr!   CLOSURE_MATCHclsvaluer   s      r]   create_with_source'UserFunctionVariable.create_with_sourcet  s&    f''(B(BCD5((r`   rt   c                 R  > [         TU ]  " S	0 UD6  [        USS5      (       a  SU l        OSU l        [	        U[
        R                  [        R                  R                  45      (       d  [        SU SS/[        R                  QS9  [        R                  " USU5      nXl        g )
N_dynamo_marked_constantFTz1can't handle functions not implemented in python z2Dynamo can only handle functions defined in pythonz9Move usage of this function out of `torch.compile` regiongb_typecontextexplanationhints_torchdynamo_inlinerc   )superr^   r   r   r   r   r   torchjitScriptFunctionr   r   INFERENCE_MODEr   r   r   rY   r   r   r   	__class__s       r]   r^   UserFunctionVariable.__init__y  s    "6"20%88#D$D "u115993K3KLMMK$PO&55	 ##B(=rB&(r`   c                 b   > [        U [        5      (       a  U R                  $ [        TU ]  5       $ rv   )r0   r   r   r   as_python_constantrY   r  s    r]   r	  'UserFunctionVariable.as_python_constant  s(    $,--77Nw)++r`   c                     / $ rv   rc   r   s    r]   r   UserFunctionVariable.self_args  r   r`   c                     U R                   $ rv   r   r   s    r]   r   !UserFunctionVariable.get_function      wwr`   c                 .    U R                   R                  $ rv   )r   rH   r   s    r]   r   UserFunctionVariable.get_code  s    wwr`   c                 "    [         R                  $ rv   r   r   r   s    r]   python_type UserFunctionVariable.python_type      !!!r`   c                 4    [        U R                  SS 5      S L$ N__self__)r   r   r   s    r]   has_selfUserFunctionVariable.has_self  s    tww
D1==r`   c                 .    U R                   R                  $ rv   )r   r   r   s    r]   get_globals UserFunctionVariable.get_globals  s    ww"""r`   c                     U R                   nU(       a+  [        U [        R                  5      (       a  U R                  nU$ rv   )r   r   r   UserMethodVariable	source_fn)rY   r   s     r]   
get_sourceUserFunctionVariable.get_source  s.    jy'C'CDD^^Fr`   c                 R   U R                   (       a   eU R                  n[        U[        5      (       d  [	        S5      eUR
                  R                  nU R                  5       n[        XEXbU5      n[        XUR                  5        U R                  R                  =(       d    Sn[        U5      [        U R                  R                  R                  5      :X  d   e[        [        R                   " 5       U R                  R                  R                  U5       H  u  pnUR
                  R"                  nX;   a  X   nOU(       aV  [%        ['        U5      U	5      n[)        US5      n [*        R,                  " XR.                  U5      nUR7                  XU5      nO4 [*        R,                  " XR.                  5      nUR7                  SUU5      nXU
'   M     U$ ! [0         a    [2        R4                  " 5       n Nrf = f! [0         a    [2        R4                  " 5       n Ncf = f)z
Assume `args` and `kwargs` are VariableTracker arguments for a call to
this function, create new bindings for initial locals.
z'Only supports regular Python functions.rc   cell_contentsN)r   r   r   r   r   r   root_txr$  r   r   rH   r   rj   co_freevarsri   r   countr   r'   r$   r#   r6   r   r'  
ValueErrorr   DeletedVariabletrack_cell_existing)rY   r   r}   r   r   r(  r   r   r   r   r   cellr   cell_varclosure_cellclosure_cell_contentscontents_vars                    r]   	bind_argsUserFunctionVariable.bind_args  s   
 ####!%"l++EFF--''"!"vVDfbkk2''%%+7|s477#3#3#?#?@@@@"OOtww//;;W 
OCt "==55L#'-,]6-BCH(2<(Q%?#2#8#8 2 24I$L (;; ?#2#8#8ASAS#TL (;;D$U#4LA 
D ' " ?#,#<#<#>L? " ?#,#<#<#>L?s$   -!G" HH HH&%H&r   r<   r   c                     U[         ;   a  [        R                  " X5      $ U R                  5       n[	        XR
                  X25      $ rv   )r+   r   GetAttrVariabler$  r   r   )rY   r   r   r   s       r]   var_getattr UserFunctionVariable.var_getattr  s9    )),,T88"b''688r`   c                 l    [        U R                  U5      n[        R                  R	                  U5      $ rv   )r   r   r   r8   r   r   s       r]   r   %UserFunctionVariable.call_obj_hasattr  s+     $'))0088r`   c           
      X	  > U R                   [        R                  R                  L a   U Vs/ s H  oDR	                  5       PM     nnUR                  5        VVs0 s H  u  pgXgR	                  5       _M     nnn[        R                  R                  " U0 UD6R                  n	[        R                  " U	5      $ U R                   [        R                  R                  L a{   [        R                  " U R                   5      R                  " U0 UD6nUR                  S   R	                  5       n[!        U["        5      (       d   e[        R$                  " U5      $ U R                   [        R                  R(                  L Ga  [        R                  " U R                   5      R                  " U0 UD6nUR*                  S   n[!        U[,        5      (       dP  UR/                  5       nSUR0                   S3n[3        SS	U R4                   S
U S
U S3U/ [6        R8                  QS9  [!        U[:        5      (       d)  UR=                  5       nSU S3n[3        SU  USU S3S/S9  UR                   n[        R>                  " USS9$ U R@                  (       a%  [C        XR                   U R=                  5       X#5      $ URD                  RF                  RH                  (       dt  U R                   [        R                  RJ                  RL                  L aC  [        R                  RN                  RQ                  U5         [R        TU ]  XU5      sS S S 5        $ URD                  RF                  RV                  (       a  URD                  RF                  RX                  (       ds   SSK-J.n  Ubi  U R                   UR^                  UR`                  4;   aC  [        R                  RN                  RY                  U5         [R        TU ]  XU5      sS S S 5        $ [R        TU ]  XU5      $ s  snf s  snnf ! [         a  n
[        SU SU 35      U
eS n
A
ff = f! [&         a  n
[        SU SU 35      U
eS n
A
ff = f! , (       d  f       GN/= f! [&         a    S n Nf = f! , (       d  f       N= f)NzCannot convert patch_dynamo_config args/kwargs to constants. Please fix your call to patch_dynamo_config by using simpler inputs. args: z
, kwargs: error_on_graph_breakz\Improper error_on_graph_break() call. Please fix your call to error_on_graph_break(). args: r   z=`nonstrict_trace` expects a callable, but got value of type <>zTypeError from user codezcall_function(, )r   z(Applying `nonstrict_trace` to function <zk>; however, `nonstrict_trace` currently requires the function to be defined outside `torch.compile` region.zLimitation of `nonstrict_tracezmake sure definition of z is outside z`torch.compile` regionTnonstrict_traceable)	FSDPState)1r   r  _dynamopatch_dynamo_configr	  r   changesr   DynamoConfigPatchVariabler3   RuntimeErrorr<  r   r   bind	argumentsr   boolErrorOnGraphBreakVariable	Exceptionnonstrict_tracer}   r   r  ro   r   r   r   
USER_ERRORr   r   TorchInGraphFunctionVariabler   invoke_and_store_as_constantr   current_tracer,unsafe_allow_externally_visible_side_effectsutils8_disable_side_effect_safety_checks_for_current_subtracerr   2allow_externally_visible_side_effects_in_subtracerr   r   under_activation_checkpoint#allow_side_effects_under_checkpoint/torch.distributed.fsdp._fully_shard._fsdp_staterB  _pre_forward_post_forward)rY   r   r}   r   arg
args_constkeyr   kwargs_constrE  er@   r<  fn_vartypmsgfn_namer   rB  r  s                      r]   r   "UserFunctionVariable.call_function  s9    77emm777BFG$3446$
GBH,,. BPhcC//11.     --;;#/'  !::7CC WW:::))$''277HH',*($$& % ""6==== ::;OPP WW555%%dgg.33TDVDEZZ]Ff&>??((*UVYVbVbUccde 6,TZZL4&6(!L #*55	 f&:;; //+@	  Ju  v <#f #27)<H0	 B99"RVWW/GGT]]_d 
 		((UU}}""[[\ ++^^ w,Rv>  II$$@@II,,PP!U $&&''5 * ]]//SSTVW 706B XWw$Rv66q H  7 "!F*VH6 	  "!F*VH6 ^   ! 	! XWsx   P/ P$P/ P)8AP/ #A9Q =Q7"R	 :R$P/ /
Q9QQ
Q4Q//Q47
R	RR
R))r   r   Frt   Nr   )ro   rp   rq   rr   r   r   _nonvar_fieldsclassmethodr   r^   r	  r   r   r   r  r  r  r$  rh   r   r6   r3  r7  r   r   rs   __classcell__r  s   @r]   r   r   k  s    7 	 
"	0	0N ) )) )4, ">#5c?6J1K 5n95 9S 99)9149	9a7#a7 &a7 -	a7
 
a7 a7r`   r   c                   Z   ^  \ rS rSrSSU 4S jjjr\S 5       r        SS jrSrU =r	$ )	BuiltinMethodVariableiY  c                 t   > [         TU ]  " S0 UD6  [        U[        R                  5      (       d   eXl        g rb   )r   r^   r   r   BuiltinMethodTyper   r  s       r]   r^   BuiltinMethodVariable.__init__Z  s1    "6""e556666r`   c                     U R                   nU R                  nU[        L =(       a    US:H  =(       d    [        U5      [        L =(       a    US:H  $ )N__new____contains__)r  ro   r   type	frozenset)objmethod_selfmethod_names      r]   is_supported_builtin_method1BuiltinMethodVariable.is_supported_builtin_method_  sI    llll u$A	)A 
*L{n/L	
r`   c                     U R                   R                  nU R                   R                  nU R                  =(       a    [	        U R                  S5      n[
        R                  " XU5      nUR                  XX#5      $ r  )r   r  ro   r   r#   r6   r   call_method)rY   r   r}   r   rv  r   
obj_sourceobj_vts           r]   r   #BuiltinMethodVariable.call_functionj  s`     gg&&ww[[HZZ%H
 &&r
C!!"D99r`   r  re  rf  r   )
ro   rp   rq   rr   r^   staticmethodrx  r   rs   ri  rj  s   @r]   rl  rl  Y  sQ     
 
 

:#
: &
: -	
:
 

: 
:r`   rl  c            
       &  ^  \ rS rSrS\R
                  S\S   4U 4S jjrS rS r	S r
S	 rS
 rS r S r\rS%S jrS rS rS rS rS rS rS rS\R0                  4S jrS\\   4S jrS&S jrS rS rS r SSS\!SSS S!SS"4
U 4S# jjr"S$r#U =r$$ )'LocalGeneratorObjectVariableiw  rZ   inline_tracerr<   c                 J   > [         TU ]  " S0 UD6  Xl        X l        X0l        g rb   )r   r^   rZ   r   r  )rY   rZ   r   r  r   r  s        r]   r^   %LocalGeneratorObjectVariable.__init__x  s%     	"6"	"*r`   c                     U R                   $ rv   )rZ   r   s    r]   r   %LocalGeneratorObjectVariable.get_code  s    yyr`   c                 6    U R                  5       R                  $ rv   r   r   s    r]   r   )LocalGeneratorObjectVariable.get_filename  r   r`   c                 6    U R                  5       R                  $ rv   r   r   s    r]   r   %LocalGeneratorObjectVariable.get_name  r   r`   c                     [         erv   )r   r   s    r]   r   )LocalGeneratorObjectVariable.get_function  s    !!r`   c                     gNFrc   r   s    r]   r  %LocalGeneratorObjectVariable.has_self      r`   c                 "    U R                  5       $ rv   )r   r   s    r]   ro   %LocalGeneratorObjectVariable.__name__  s    }}r`   c                 V    U R                   R                   SU R                  5        S3$ )N(r?  )r  ro   r   r   s    r]   __str__$LocalGeneratorObjectVariable.__str__  s'    ..))*!DMMO+<A>>r`   c                    SSK Jn  SSKJnJnJn  UR                  " 5       nU" U5      nU" U5      nU" U5      n	U   U   U	   U R                  U5      n
U
R                  (       d  U R                  U5      U l
        [        R                  " U R                  5      R                  U5        S S S 5        S S S 5        S S S 5        g ! , (       d  f       N= f! , (       d  f       N(= f! , (       d  f       g = f)Nr   )"disallow_side_effects_in_generator)r<    save_and_restart_speculation_log(temporarely_allow_writes_to_output_graph)torch._dynamo.side_effectsr  torch._dynamo.symbolic_convertr<   r  r  
current_tx_get_inline_tracergenerator_exhaustedforce_unpack_var_sequenceremaining_itemsr   ListIteratorVariablereconstruct)rY   codegenr  r<   r  r  r   savedisallowtemptracers              r]   r  (LocalGeneratorObjectVariable.reconstruct  s    Q	
 	
 #--//35b97;8T,,R0F--'+'E'Eb'I$**4+?+?@LLWU	 "8TTTT88TTs;   C%CA(C*C2C%
CC
C"	C%%
C3c                 :    U R                   R                  XU5      $ rv   )r   r3  r   s       r]   r3  &LocalGeneratorObjectVariable.bind_args  s    ww  622r`   c                     U R                   $ rv   r   r   s    r]   r  (LocalGeneratorObjectVariable.get_globals      ~~r`   c                 "    [         R                  $ rv   )r   GeneratorTyper   s    r]   r  (LocalGeneratorObjectVariable.python_type  s    """r`   c                 p    SSK Jn  U R                  c  UR                  X/ 0 5      U l        U R                  $ Nr   )InliningInstructionTranslator)r  r  r  build_inline_tracer)rY   r   r  s      r]   r  /LocalGeneratorObjectVariable._get_inline_tracer  s9    P%!>!R!R"b"D !!!r`   c                    U R                  U5      nU R                  5       (       a  [        [        U5         [        R
                  " [        S[        S   05         UR                  5       sS S S 5        [        S==   [        S   -  ss'   $ ! , (       d  f       Os= f! [         a  nSUl	        UeS nAf[         a    e [         aC  n[        R                  R                  R                  U R!                  5       5        ["        UeS nAff = f [        S==   [        S   -  ss'   g ! [        S==   [        S   -  ss'   f = f)Nunimplementedinline_callT)r  _is_generator_exhaustedr   StopIterationr   rh   r,   inline_call_r   r  r   r    r  rC  
eval_frame	skip_coder   r   )rY   r   r  r_  s       r]   next_variable*LocalGeneratorObjectVariable.next_variable  s
   ((,''))$]B7	A H8O&PQ**, RQ _%-)@@% RQ  	)-F&G% 	 	#MM$$..t}}?"	# R _%-)@@%H_%-)@@%sS   $B B,	B 
BB D) B 
D)	B22D	>DDD) )Ec                     X R                  5       R                  ;   a  [        R                  " S5      $ [        R                  " S5      $ )NTF)r  __dict__r8   r   rY   r   r   s      r]   r   -LocalGeneratorObjectVariable.call_obj_hasattr  s9    ##%...#**400&&u--r`   c                     gr  rc   r   s     r]   has_unpack_var_sequence4LocalGeneratorObjectVariable.has_unpack_var_sequence  r  r`   rt   c                     gNTrc   r   s     r]   has_force_unpack_var_sequence:LocalGeneratorObjectVariable.has_force_unpack_var_sequence  s    r`   c                 @    / nU R                  XR                  5        U$ rv   )force_apply_to_var_sequenceappend)rY   r   r   s      r]   r  6LocalGeneratorObjectVariable.force_unpack_var_sequence  s    ((]];r`   c                 n      U" U R                  U5      5        M  ! [         a    [        U5         g f = frv   )r  r   r   )rY   r   r   s      r]   r  8LocalGeneratorObjectVariable.force_apply_to_var_sequence  s>    4%%b)*  - )"-s    44c                     U R                  U5      n UR                  U5        g ! [         a  nUR                  U5         S nAg S nAff = frv   )r  _raise_exception_variabler   exception_handler)rY   r   excr  r_  s        r]   _setup_exception-LocalGeneratorObjectVariable._setup_exception  sH    ((,	(,,S1  	( $$Q''	(s   % 
A
AA
c                 ^    U R                   S L =(       d    U R                   R                  S:H  $ )Nr   )r  instruction_pointerr   s    r]   _is_generator_just_started7LocalGeneratorObjectVariable._is_generator_just_started  s*    !!T)XT-?-?-S-SWX-XXr`   c                 0    [        U R                  SS5      $ )Nr  F)r   r  r   s    r]   r  4LocalGeneratorObjectVariable._is_generator_exhausted  s    t))+@%HHr`   r   r   r}   r   r   r   r6   c                   > US:X  a  U R                  U5      $ US:X  a  U $ US:X  a  U R                  5       (       a7  [        U5      (       a'  [        S U 5       5      (       d  [	        [
        U5        U R                  U5      nUR                  U5        U R                  U5      $ US:X  a  U R                  U5      nU R                  5       (       d  U R                  5       (       a  SUl	        [        R                  " S 5      $  U R                  U[        R                  " [        S5      5        [        R                   S:  a7  UR"                  R$                  S	:X  a  SUl	        [        R                  " S 5      $  U R                  U5      (       a  [	        [(        U5        OUS
:X  a  U R                  U5      n U R                  U[        U5      S:X  a  US   OUS   5        U R                  U5      n[1        S[2        40 5      n U R                  U[        R                  " US5      5        U R                  U5        [	        [(        U5        U$ [6        TU ]q  XX45        g ! [&         a     SUl	        [        R                  " S 5      s $ f = f! [&         a     SUl	        [        R                  " S 5      s $ [*         a    UR,                  c   eUR,                  s $ f = f! [.         a    e f = f! [5        U5       a     U$ f = f)N__next____iter__sendc              3   l   #    U  H*  n[        U[        5      =(       a    UR                  S L v   M,     g 7frv   )r   r8   r   ).0r[  s     r]   	<genexpr>;LocalGeneratorObjectVariable.call_method.<locals>.<genexpr>  s0      # s$45K#))t:KK#s   24closeTrc   )      CALL_INTRINSIC_1throwr  r2   r   __InternalThrowException)r  r  rj   allr   r   r  	push_manyr  r  r   r8   r  ExceptionVariableGeneratorExitsysversion_infonext_instructionopnamer   rG  r   symbolic_resultr   rs  rL  r   r   r{  )	rY   r   r   r}   r   r  retvalexc_typer  s	           r]   r{  (LocalGeneratorObjectVariable.call_method  s    :%%b))ZKV^ ..00SYY  #   -Y;,,R0FT"%%b))W_ ,,R0F..00D4P4P4R4R-1* 11$778%%	33M2F( $$///66:LL15F.$55d;;.%%b)),\2> W_ ,,R0F
 %%bSY!^$q'aQ
 ''+F| 6bIH?%%b)*E*EhPR*ST""2&
 )v>MBd3a ) 8-1* 11$778 ) 8-1* 11$77, .
 --999---., % N 1:  MsI   A5I# <&J ;)K# 8K2 #'JJ'K 9$K K #K/2LL)rZ   r   r  r  r  r:   rf  )%ro   rp   rq   rr   r   CodeTyper   r^   r   r   r   r   r  r  __repr__r  r3  r  r  r  r  r   r  builtinsrJ  r  r   r6   r  r  r  r  r  r   r{  rs   ri  rj  s   @r]   r  r  w  s    
+nn
+   78	
++'"? HV&3#"A0.
8== tO/D 
(YIz4#z4 z4 &	z4
 -z4 
z4 z4r`   r  c                       \ rS rSrSrSrg)4ContextlibContextManagerLocalGeneratorObjectVariablei  z
.. note::

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

    It is a special case of a generator function as we do not allow return a context manager
    from a torch.compile function.
rc   N)ro   rp   rq   rr   r   rs   rc   r`   r]   r  r    s    r`   r  c                   b   ^  \ rS rSrSr\S.S\4U 4S jjjrS rS r	        S
S jr
S	rU =r$ )LocalGeneratorFunctionVariablei  zlfunctions that behaves like iterators

.. note::

    This is a wrapper around (Nested)UserFunctionVariable
)generator_clsvtc                >   > [         TU ]  " S0 UD6  Xl        X l        g rb   )r   r^   r  r  )rY   r  r  r   r  s       r]   r^   'LocalGeneratorFunctionVariable.__init__  s      	"6"*r`   c                     XR                   R                  R                  5       ;   a  [        X5      $ [        U R                  U5      $ rv   )r  r  keysr   r  )rY   r   s     r]   __getattr__*LocalGeneratorFunctionVariable.__getattr__  s7    >>**//114&&tww%%r`   c                 6    SSK Jn  UR                  UU UU5      $ r  )r  r  r  )rY   r   r}   r   r  s        r]   _build_inline_tracer3LocalGeneratorFunctionVariable._build_inline_tracer  s$    P,@@	
 	
r`   c                 t   [        U R                  R                  5       5      (       d/  [        S[	        U R                  R                  5       5      SSS/S9  U R                  XU5      nU R                  R                  5       nU R                  R                  5       nU R                  UUUU R                  S9$ )Nz'non-generator contextlib.contextmanagerzvCannot compile function decorated with `@contextlib.contextmanager` that is not a generator, i.e. does not use `yield`z5Use `yield` in the function body instead of `return`.z2Remove the `@contextlib.contextmanager` decorator.r   r   )	r   r  r   r   r   r
  r  r  r   )rY   r   r}   r   r  rZ   r   s          r]   r   ,LocalGeneratorFunctionVariable.call_function  s     DGG,,.//ADGG,,./. LH	 11"FCww!GG'')	 !!;;	 " 
 	
r`   )r  r  r   )ro   rp   rq   rr   r   r  r6   r^   r  r
  r   rs   ri  rj  s   @r]   r   r     sY     3		+	+ 	+&


#
 &
 -	

 

 
r`   r   c                   8   ^  \ rS rSrSrU 4S jrU 4S jrSrU =r$ )3FunctionDecoratedByContextlibContextManagerVariablei
  za
.. note::

    This is only used when the function is annotated with @contextlib.contextmanager
c                 4   > [         TU ]  " U4S[        0UD6  g )Nr  )r   r^   r  )rY   r  r   r  s      r]   r^   <FunctionDecoratedByContextlibContextManagerVariable.__init__  s%    	
N	
 	
r`   c                    > [         TU ]  XU5      n[        U[        R                  R
                  R                  5      (       d   eSUl        U$ r  )r   r
  r   r  rC  symbolic_convert&InliningGeneratorInstructionTranslatoris_generator_from_ctx_manager)rY   r   r}   r   r  r  s        r]   r
  HFunctionDecoratedByContextlibContextManagerVariable._build_inline_tracer  sR    
 -b?MM**QQ
 
 	
 
 04,r`   rc   )	ro   rp   rq   rr   r   r^   r
  rs   ri  rj  s   @r]   r  r  
  s    
 r`   r  c                      ^  \ rS rSrSrSSU 4S jjjrS\4S jrS rS r	        SU 4S
 jjr
U 4S jrSS	S\4U 4S jjrSrU =r$ )r"  i(  z$Some unsupported user-defined methodrt   c                    > [         TU ]  " SSU0UD6  X l        X0l        Uc4  UR	                  S5      b!  [        UR	                  S5      S5      U l        g g g )Nr   r   __func__rc   )r   r^   ru  r#  rx   r#   )rY   r   ru  r#  r   r  s        r]   r^   UserMethodVariable.__init__+  sX    )B)&)" H!5!A'

8(<jIDN "Br`   c                 h    U R                   R                   SU R                   SU R                   S3$ )Nr  r>  r?  )r  ro   r   ru  r   s    r]   r  UserMethodVariable.__repr__>  s.    ..))*!DGG9BtxxjBBr`   c                     U R                   /$ rv   ru  r   s    r]   r   UserMethodVariable.self_argsA      zr`   c                 "    [         R                  $ rv   )r   
MethodTyper   s    r]   r  UserMethodVariable.python_typeD  s    r`   r   r<   c                   > SSK Jn  U" U R                  5      (       aE  / U R                  5       QUQn[        R
                  " U R                  SS9nUR                  XU5      $ UR                  R                  5       (       a  [        U R                  [        R                  5      (       a  [        U R                  SS5      nUb  UR                  S5      (       a  US:w  d  U R                  (       a9  U R                  R                  XR                  R                   X#U R                  S	9$ Ol["        be  U R                  ["        R$                  R&                  L a>  [        R(                  " U R                  5      R                  XR                  /UQ7U5      $ U R                  (       aO  [        U R                  R*                  U R                  R                   5      n[-        XU R/                  5       X#5      $ [0        T	U ]  XU5      $ )
Nr   )is_nonstrict_trace_callableTr@  rp    z	torch.nn.ztorch.nn.utils.parametrize)constant)trace_rulesr%  r   r   r   rO  r   r   is_root_tracerr   ru  NNModuleVariabler   
startswithr   r{  ro   r9   FSDPParamGroupuse_training_stateTorchCtxManagerClassVariabler   rP  r   r   )
rY   r   r}   r   r%  	call_argsvarmodule_attrr   r  s
            r]   r    UserMethodVariable.call_functionG  s    	>&tww//2$..*2T2I88TC $$RF;; 99##%%*HHi00+
 +
 "$''<<K '**;77#??##xx++(($AQAQ ,   $ ),;;NNN99$''BPPXX%%v  )9)9:B/VVw$Rv66r`   c                 &   > [         TU ]  5       SS  $ Nr2   )r   r   r
  s    r]   r   *UserMethodVariable.inspect_parameter_names  s    w.044r`   r   c                    > US:X  a  U R                   $ US:X  a+  [        R                  " XR                  U R                  5      $ [
        TU ]  X5      $ )Nr  r  )ru  r6   r   r   r#  r   r7  rY   r   r   r  s      r]   r7  UserMethodVariable.var_getattr  sI    :88O: #((WWdnnEEw"2,,r`   )ru  r#  rv   rf  r   )ro   rp   rq   rr   r   r^   r   r  r   r  r   r   r7  rs   ri  rj  s   @r]   r"  r"  (  st    .J J&C# C ;7#;7 &;7 -	;7
 
;7z5-5 -S - -r`   r"  c                   R   ^  \ rS rSrSU 4S jjr        SU 4S jjrS rSrU =r$ )WrappedUserMethodVariablei  c                    > UR                  SS 5        UR                  SS 5        [        TU ]  " UR                  UR                  40 UD6  Xl        X l        g )Nr   ru  )r   r   r^   r   ru  wrappedr   rY   r<  r   r   r  s       r]   r^   "WrappedUserMethodVariable.__init__  sE    

4

5$W[[;F;r`   c                    > U R                   R                  U5        [        TU ]  XU5      nU R                   R	                  U5        U$ rv   r   enterr   r   exitrY   r   r}   r   r   r  s        r]   r   'WrappedUserMethodVariable.call_function  ?     	2&r8"r`   c                    ^ ^ TR                  UU 4S j5        T" T R                  5        TR                  [        SS5      5        g )Nc                  (   > T " TR                   5      $ rv   r   r  rY   s   r]   <lambda>7WrappedUserMethodVariable.reconstruct.<locals>.<lambda>      gdll&;r`   r2   Fadd_push_nullr<  extend_outputr   rY   r  s   ``r]   r  %WrappedUserMethodVariable.reconstruct  4    ;<21e<=r`   r   r<  rf  r   	ro   rp   rq   rr   r^   r   r  rs   ri  rj  s   @r]   r:  r:    =    	#	 &	 -		
 
	> >r`   r:  c                   R   ^  \ rS rSrSU 4S jjr        SU 4S jjrS rSrU =r$ )WrappedUserFunctionVariablei  c                 x   > UR                  SS 5        [        TU ]  " UR                  40 UD6  Xl        X l        g )Nr   )r   r   r^   r   r<  r   r=  s       r]   r^   $WrappedUserFunctionVariable.__init__  s1    

4.v.r`   c                    > U R                   R                  U5        [        TU ]  XU5      nU R                   R	                  U5        U$ rv   r@  rC  s        r]   r   )WrappedUserFunctionVariable.call_function  rE  r`   c                    ^ ^ TR                  UU 4S j5        T" T R                  5        TR                  [        SS5      5        g )Nc                  (   > T " TR                   5      $ rv   rH  rI  s   r]   rJ  9WrappedUserFunctionVariable.reconstruct.<locals>.<lambda>  rL  r`   r2   FrM  rP  s   ``r]   r  'WrappedUserFunctionVariable.reconstruct  rR  r`   rS  rf  r   rT  rj  s   @r]   rW  rW    s=    	#	 &	 -		
 
	> >r`   rW  c                     S nU Vs/ s H
  oe" U5      PM     nnUR                  5        VVs0 s H  u  pxXu" U5      _M     nnnU" U0 UD6n	U R                  R                  U	U[        U5      S9$ s  snf s  snnf )Nc                     [        U [        R                  5      (       a  U R                  5       $ U R	                  5       $ rv   )r   r   TensorVariableget_real_valuer	  )xs    r]   convert-invoke_and_store_as_constant.<locals>.convert  s3    a1122##%%##%%r`   r   )r   r   register_attr_or_moduler%   )
r   r   r   r}   r   re  rd  r   r   ress
             r]   rP  rP    s    &
 !%%1GAJD%(.7amF7
d
f
C99,,d# -   &7s
   A1A6c                      ^  \ rS rSrS1\R
                  kr S SU 4S jjjrS rS rS r	S r
SS	S
\S\4S jrU 4S jrS rU 4S jrS rS rS rSS jrSrU =r$ )r   i  r   c	                   > U	R                  S5      c  U	R                  [        5       S9  [        T
U ]  " S0 U	D6  [        UR                  5       [        5      (       d   e[        UR                  5       [        R                  5      (       d   e[        U[        5      (       d   eXl        X l        X0l        X@l        XPl        X`l        Xpl        Xl        g )Nmutation_typerk  rc   )rx   updater4   r   r^   r   r	  r   r   r  rh   rc  rZ   r   re   rg   r   r   
wrapped_fn)rY   rc  rZ   r   re   rg   r   r   rn  r   r  s             r]   r^   #NestedUserFunctionVariable.__init__  s     ::o&.MM(<(>M?"6"'446<<<<$113U^^DDDD)T****	" $&5?r`   c                     / $ rv   rc   r   s    r]   r   $NestedUserFunctionVariable.self_args  r   r`   c                 6    U R                   R                  5       $ rv   )rZ   r	  r   s    r]   r   #NestedUserFunctionVariable.get_code  s    yy++--r`   c                 "    [         R                  $ rv   r  r   s    r]   r  &NestedUserFunctionVariable.python_type  r  r`   c                    U R                   (       a  [        e[        R                  " U R                  R                  5       U R                  U R                  R                  5       5      nU R                  (       a  U R                  R                  5       Ul	        U R                  (       a  U R                  R                  5       Ul        U R                  (       ac  U R                  R                  5       n[        U[        5      (       a  SSKJn  [#        U" U5      5      n[        U["        5      (       d   eX!l        U$ )Nr   r   )r   r   r   r   rZ   r	  r   rc  re   rd   rg   rf   r   r   r   r   r   rh   r   )rY   rF   r   r   s       r]   r   'NestedUserFunctionVariable.get_function  s    <<%%!!II((*NNLL++-

 == $ @ @ BD??"&//"D"D"FD**==?K+u--."8K#89 k40000#. r`   r   r<   name_varr   c                 x    UR                   R                  R                  XR                  U5        [	        S 5      $ rv   )r   r   
store_attrr   r8   )rY   r   rx  r   s       r]   call_setattr'NestedUserFunctionVariable.call_setattr  s-     			))$D%%r`   c                 V   > US:X  a  U R                   " U/UQ76 $ [        TU ]	  XX45      $ )N__setattr__)r{  r   r{  rY   r   r   r}   r   r  s        r]   r{  &NestedUserFunctionVariable.call_method   s3    = $$R/$//w"2T::r`   c                     U R                   S L$ rv   )r   r   s    r]   has_closure&NestedUserFunctionVariable.has_closure%  s    ||4''r`   c                 b   > US:X  a  U R                   R                  5       $ [        TU ]  X5      $ )Nro   )rc  r	  r   const_getattrr7  s      r]   r  (NestedUserFunctionVariable.const_getattr(  s.    :<<2244w$R..r`   c                     gr  rc   r   s    r]   r  #NestedUserFunctionVariable.has_self-  r  r`   c                     U R                   $ rv   r  r   s    r]   r  &NestedUserFunctionVariable.get_globals0  r  r`   c                 `   U R                  5       n[        R                  " UU R                  U R                  R                  5       U R                  (       a  [        U R                  R                  5      OS [        S [        [        U R                  5       R                  5      5       5       5      5      nU R                  (       a  U R                  R                  5       Ul        [        R                   " U5      R"                  " U0 UD6nUR%                  5         ['        UR(                  R                  5       5      n[+        UR,                  R.                  U5        [1        XU5        [3        UR                  5       H)  u  pX;  d   eU R4                  R                  U   n
XU	'   M+     U$ )Nc              3   8   #    U  H  n[        S 5      v   M     g 7frv   )r1   )r  _s     r]   r  7NestedUserFunctionVariable.bind_args.<locals>.<genexpr>:  s     S+Ra)D//+Rs   )r   r   r   r   rc  r	  re   r   r   rk   rj   r)  rg   keys_as_python_constantrf   r   r   rH  apply_defaultsrh   rI  r   r   r(  r   r   r   )rY   r   r}   r   rZ   rF   r@   r   r   r   r.  s              r]   r3  $NestedUserFunctionVariable.bind_args3  s9   }}!!NNLL++-*.--E$--%%&TS5T]]_5P5P1Q+RSS
 ??"&//"I"I"KD!!$',,d=f=eoo++-...7fd+"4#3#34IC%%%<<%%c*D4L 5
 r`   c                   ^ TR                  U4S j5        T" U R                  5        TR                  TR                  U R                  5      /5        T" [
        R                  " U R                  R                  R                  5      5        U R                  (       a  T" U R                  5        O!TR                  TR                  S 5      /5        U R                  (       a  T" U R                  5        O!TR                  TR                  S 5      /5        U R                  (       a  T" U R                  5        O!TR                  TR                  S 5      /5        U R                  (       a=   U R                  R                  5       nTR                  TR                  U5      /5        O!TR                  TR                  S 5      /5        TR                  [!        SS5      5        U R"                  (       aw  TR                  U4S j5        T" U R"                  5        TR                  [!        SS5      5        TR                  [%        S5      5        TR                  [!        SS5      5        SS	KJn  UR*                  " 5       nUR,                  R.                  R1                  U 5      (       a~  UR,                  R.                  R2                  U    R5                  5        HH  u  pVTR7                  5         T" U5        TR                  [%        S5      5        TR9                  U5        MJ     g g ! [         a    T" U R                  5         GNf = f)
Nc                  0   > T R                  [        S5      $ )Nr   )load_import_fromro   r  s   r]   rJ  8NestedUserFunctionVariable.reconstruct.<locals>.<lambda>M  s    G,,X7JKr`      Fc                  (   > T R                  SS5      $ )N	functoolswrapsr  r  s   r]   rJ  r  q  s    00gFr`   r2   r   Tr   r;   )rN  rZ   rO  create_load_const_uncheckedr   r8   r   r   r   re   create_load_constr   rg   r   r	  r   r   rn  r   r  r<   r  r   r   has_pending_mutationstore_attr_mutationsr   dup_toprz  )rY   r  r   r<   r   r   r   s    `     r]   r  &NestedUserFunctionVariable.reconstructK  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*"..AAC%%88EF !!7#<#<T#B"CD21e<=??!!F DOO$!!"6q%"@A!!,q/2!!"6q$"?@ 	I"--/99!!66t<<!yy55JJ eg  !%%l1o6""4(  =) ' *(()*s   /;L/ /MM)r   r   rZ   re   r   rc  rg   rn  rv   rf  r  )ro   rp   rq   rr   r   rg  r^   r   r   r  r   r6   r{  r{  r  r  r  r  r3  r  rs   ri  rj  s   @r]   r   r     s    	!	0	0N  @ 
@ @:."0&#& "& 	&;
(/
08) 8)r`   r   c                   R   ^  \ rS rSrSU 4S jjr        SU 4S jjrS rSrU =r$ )!WrappedNestedUserFunctionVariablei  c           
      
  > UR                  SS 5        UR                  SS 5        UR                  SS 5        UR                  SS 5        UR                  SS 5        UR                  SS 5        UR                  SS 5        UR                  SS 5        [        TU ]	  UR                  UR                  UR
                  UR                  UR                  UR                  UR                  UR                  5        Xl        X l        g )	Nrc  rZ   r   re   rg   r   r   rn  )r   r   r^   rc  rZ   r   re   rg   r   r   rn  r<  r   r=  s       r]   r^   *WrappedNestedUserFunctionVariable.__init__  s    

9d#

64 

;%

:t$

<&

=$'

9d#

<&OOLLOO		
 r`   c                    > U R                   R                  U5        [        TU ]  XU5      nU R                   R	                  U5        U$ rv   r@  rC  s        r]   r   /WrappedNestedUserFunctionVariable.call_function  rE  r`   c                    ^ ^ TR                  UU 4S j5        T" T R                  5        TR                  [        SS5      5        g )Nc                  (   > T " TR                   5      $ rv   rH  rI  s   r]   rJ  ?WrappedNestedUserFunctionVariable.reconstruct.<locals>.<lambda>  rL  r`   r2   FrM  rP  s   ``r]   r  -WrappedNestedUserFunctionVariable.reconstruct  rR  r`   rS  rf  r   rT  rj  s   @r]   r  r    s=    ,	#	 &	 -		
 
	> >r`   r  c                      ^  \ rS rSrSS1\R
                  krSSU 4S jjjrS r\S 5       r	        SS	 jr
SS
 jrSSS\4S jrSrU =r$ )SkipFunctionVariablei  r   reasonc                 >   > [         TU ]  " S0 UD6  Xl        X l        g rb   )r   r^   r   r  )rY   r   r  r   r  s       r]   r^   SkipFunctionVariable.__init__  s    "6"
r`   c                     U R                   $ rv   r   r   s    r]   r	  'SkipFunctionVariable.as_python_constant  s    zzr`   c                 |   [         R                  " USS5      (       ab  UnUn[        USS5      (       a,  UR                  n[	        US5      n[        USS5      (       a  M,  UR                  [        R                  5        O8[        U5      (       d(  [        UR                  [        R                  5      5        U " XS9$ )N_torchdynamo_disableF_torchdynamo_orig_callabler   )
r   r   r   r  r#   r   r!   r   r/   r"   )r   r   r   guard_on_sourceguard_on_values        r]   r   'SkipFunctionVariable.create_with_source  s     !!%)?GG %O"N.*FNN!/!J!J",#%A# .*FNN &&|'A'AB077 &++L,F,FGH5((r`   r   r<   c           	      n   [         R                  " U R                  SS5      (       aT  [         R                  " U R                  SS 5      n[        S[	        U R                  5      SU R                   SU S3S/S	9  g U R                  [
        R                  R                  L aA  UR                  S
S 5      nU(       a  UR                  5       n[        SSU SU S3SU 3S/S	9  g U R                  [
        R                  R                  L a7  UR                  S
S 5      nU(       a  UR                  5       n[        SU 35      e[        R                  (       aH  SSKJn  UR!                  XR                  5      n[#        U[$        5      (       d  UR'                  XU5      $ [)        U R                  SS5      n	[)        U R                  SS 5      n
U
c  SO
[	        U
5      n [         R*                  " U R                  5      nSU	 SU S3nSU	 S3/nSU;  a  USU	 S3S /-  nU	S/:X  a  S0n/ nU R6                  (       a  U R6                  OS1n[        S2S3U S4U	 S5U 3UUS	9  g ! [,         a    S!S"1nX;   a  S#U S$U	 S3nS%S&/n NaU
bb  U
R/                  S'5      (       aL  S(U S$U	 S$3nS)/n[
        R                  R0                  R3                  US*-   S*R5                  U5      -   5         NS+U S$U	 S,3nS-S./n[
        R                  R0                  R3                  US*-   S*R5                  U5      -   5         GNf = f)6Nr  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   rb  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.z9Skip frame due to `torch._dynamo.skip_frame()`. Message: r2   SourcelessBuilderrq   z<unknown qualname>rp   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`.rC  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. optreez*Dynamo cannot trace optree C/C++ function zi Consider using torch.utils._pytree - https://github.com/pytorch/pytorch/blob/main/torch/utils/_pytree.py
z/Dynamo does not know how to trace the builtin `z.` This function is either a Python builtin (e.g. _warnings.warn) or a third-party C/C++ Python extension (perhaps created with pybind).zIf it is a Python builtin, please file an issue on GitHub so the PyTorch team can add support for it and see the next case for a workaround.a  If it is a third-party C/C++ Python extension, please either wrap it into a PyTorch-understood custom operator (see https://pytorch.org/tutorials/advanced/custom_ops_landing_page.html for more details) or, if it is traceable, use `torch.compiler.allow_in_graph`.allow_in_graphzFound an allow_in_graph decorator to a function which is created inside the parent function that is getting compiled. This is not supported for now.z<missing reason>z,Attempted to call function marked as skippedzmodule: z, qualname: z, skip reason: )r   r   r   r   r   r  rC  graph_breakrx   r	  
skip_framer   r   dont_skip_tracingbuilderr  r   r   r  r   r   getfiler   r+  rS  	warn_oncejoinr  )rY   r   r}   r   rb  graph_break_msgskip_frame_msgr  
rebuilt_fnqualname	module_ormodule_namepathr   r   known_python_builtin_modulesr  s                    r]   r   "SkipFunctionVariable.call_function  s    !!$**.DeLL((5OQUVCKDJJ5djj\ B::=aA ? ZZ5==444$jj5O"1"D"D"F?J4&P[\b[ccdeB?BSTD	 ZZ5==333#ZZt4N!/!B!B!DKNK[\  ''6 /55b**E
!*.BCC%33BfEEtzz>;OPH

L$?I090A,s9~K8Ytzz2UV^U_ `  $v%<> 
 38*B? D(TU]T^ _l l ;	 EX ++? 
 $(KKT[[5GFF";-|H:_U[T\]'	]  %Y06/D,<'=(27  
iGE *y/C/CH/M/M$N{m[\]e\ffg"hK^E
 MM''11+2DtyyQVGW2WX J+VWX`Wa ba b  m;E MM''11+2DtyyQVGW2WXK%Ys!   AI  L4 A#L4%AL43L4c                 h    [         R                  R                  [        U R                  U5      5      $ rv   )r   r8   r   r   r   r  s      r]   r   %SkipFunctionVariable.call_obj_hasattrN  s$    ))00T1JKKr`   r   c                     U[         ;   a  [        R                  " X5      $ [        XR                  U R
                  U5      $ rv   )r+   r   r6  r   r   r   r  s      r]   r7   SkipFunctionVariable.var_getattrQ  s3    )),,T88b**dkk4@@r`   )r  r   rv   rf  r   r   r<   )ro   rp   rq   rr   r6   rg  r^   r	  rh  r   r   r   r   r7  rs   ri  rj  s   @r]   r  r    s     
	'	'N 
 ) )0v#v &v -	v
 
vpLA5 AS A Ar`   r  c                   R   ^  \ rS rSrSU 4S jjr        SU 4S jjrS rSrU =r$ )WrappedSkipFunctionVariableiX  c                    > UR                  SS 5        UR                  SS 5        [        TU ]  " UR                  4SUR                  0UD6  Xl        X l        g )Nr   r  )r   r   r^   r   r  r<  r   r=  s       r]   r^   $WrappedSkipFunctionVariable.__init__Y  sH    

7D!

8T"Hw~~HHr`   c                    > U R                   R                  U5        [        TU ]  XU5      nU R                   R	                  U5        U$ rv   r@  rC  s        r]   r   )WrappedSkipFunctionVariable.call_function`  rE  r`   c                    ^ ^ TR                  UU 4S j5        T" T R                  5        TR                  [        SS5      5        g )Nc                  (   > T " TR                   5      $ rv   rH  rI  s   r]   rJ  9WrappedSkipFunctionVariable.reconstruct.<locals>.<lambda>l  rL  r`   r2   FrM  rP  s   ``r]   r  'WrappedSkipFunctionVariable.reconstructk  rR  r`   rS  rf  r   rT  rj  s   @r]   r  r  X  rU  r`   r  c                   `   ^  \ rS rSrSrSU 4S jjrS	U 4S jjrS r        S
S jrSr	U =r
$ )WrapperUserFunctionVariableiq  a  
Used to represent a wrapper object that contains the actual callable as an
attribute. For example, torch.jit.script/trace have the original function at
their _torchdynamo_inline attribute. Similarly, functions with
__script_if_tracing_wrapper have the original attr at "__original_fn".
c                 >   > [         TU ]  " S0 UD6  Xl        X l        g rb   )r   r^   wrapper_objattr_to_trace)rY   r  r  r   r  s       r]   r^   $WrapperUserFunctionVariable.__init__y  s    "6"&*r`   c                    > X R                   :X  a`  [        U R                  U R                   5      nU R                  =(       a    [	        U R                  U5      n[
        R                  " XU5      $ [        TU ]!  X5      $ rv   )	r  r   r  r   r#   r6   r   r   r7  )rY   r   r   r   r   r  s        r]   r7  'WrapperUserFunctionVariable.var_getattr~  sc    %%%$**D,>,>?C[[BZT%BF"((&99w"2,,r`   c                     / $ rv   rc   r   s    r]   r   %WrapperUserFunctionVariable.self_args  r   r`   c                    [        U R                  S5      (       Gaw  [        U R                  U R                  S 5      n[        USS5      =(       d    SnUR	                  SSS9S   S:w  Ga(  S	n[
        R                  R                  R                  U5        [
        R                  R                  R                  R                  S
5      nUR                  [        R                  5      (       a  [
        R                  R                  R                  5       n[!        5       U-   nUS   R"                  US   R$                  4n	SR'                  [(        R*                  " U5      5      n
SU	S    SU	S    S3nU[-        U
5      -  nUR/                  U5        U R1                  5       U-   n[2        R4                  " [6        R8                  5      R;                  UU [2        R<                  " U R                  5      /UQU5      $ )N
cache_inforp   r&  r  r2   )maxsplitr   r  a  Dynamo detected a call to a `functools.lru_cache`-wrapped function. 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.ztorch._dynamoz)call to a lru_cache wrapped function at: :r  )r   r  r   r  splitr  rC  rS  r  logging	getLoggerisEnabledForDEBUG_guardsTracingContextextract_stackr   filenamelinenor  	tracebackformat_listr   debugr   r   r   r   getattr_and_tracer   r8   )rY   r   r}   r   	target_fnr  rb  dynamo_logger
user_stack	frame_locuser_stack_formatteduser_stack_traceall_argss                r]   r   )WrapperUserFunctionVariable.call_function  s    4##\22 0 0$2D2DdKI!)\2>D"K  q 1!4?K  ##--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>>#d*--''

-9--d.@.@AMHM
	
r`   )r  r  rf  r  r   )ro   rp   rq   rr   r   r^   r7  r   r   rs   ri  rj  s   @r]   r  r  q  sG    +
-&
#&
 &&
 -	&

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

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

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

 #JJt,M !, ##))$/77 
 0 #>yk!JKK$55<< +F4L ##11"FCCr`   )r   rf  r  r   )ro   rp   rq   rr   r   r^   r  r   r,  r#  r   rs   ri  rj  s   @r]   r  r    s    /
 
 
 
 

 A A,D#,D &,D -	,D
 
,D ,Dr`   r  c                   <   ^  \ rS rSr        SU 4S jjrSrU =r$ )FunctoolsWrapsVariablei)  c                    >^ U(       d+  [        T5      S:X  a  U4S jn[        R                  " U5      $ [        TU ]  UTU5      $ )Nr2   c                    > [        U [        R                  5      (       a  U R                  TS   S9$ [	        SU  S/ [
        R                  QS9  g )Nr   )rn  zfunctools.wrapsz]`torch.compile` can't trace `functools.wraps` on functions defined outside the compile regionr   )r   r   r   cloner   r   r6  )r   r}   s    r]   r  3FunctoolsWrapsVariable.call_function.<locals>.wraps2  sQ    b)"F"FGG88tAw877 -!d *66	r`   )rj   r   LambdaVariabler   r   )rY   r   r}   r   r  r  s     `  r]   r   $FunctoolsWrapsVariable.call_function*  s>     #d)q.
 ++E22w$Rv66r`   rc   r   )ro   rp   rq   rr   r   rs   ri  rj  s   @r]   r@  r@  )  s3    7#7 &7 -	7
 
7 7r`   r@  c                   4    \ rS rSrS r        SS jrSrg)CollectionsNamedTupleFunctioniC  c                     U R                   $ rv   r  r   s    r]   r	  0CollectionsNamedTupleFunction.as_python_constantD  r  r`   c                 0   [        X#5      nU(       a   U R                  " U Vs/ s H  oUR                  5       PM     sn0 UR                  5        VVs0 s H  u  pgXgR                  5       _M     snnD6n[        R                  " W[        5       S9$ [        SSU< SU< 3S/ [         R"                  QS9  g s  snf s  snnf ! [         aI  n	[        [        U	5      U[        [        [        R                  U	R                  5      5      S9   S n	A	NS n	A	ff = f)Nr|   rl  znamedtuple constructionzargs=r3  z?`torch.compile` only support certain input types for namedtupler   )r)   r   r	  r   r   r   rs  r   mapr8   r   r}   r   UserDefinedClassVariabler5   r   r   r6  )
rY   r   r}   r   constant_argsrd  r   r   r   r  s
             r]   r   +CollectionsNamedTupleFunction.call_functionG  s     ,D9
6:;d**,d;=C\\^L^TQq..00^L 55%5%7  	-tgZy)Y"..		
 <L (Ic"2"9"9388DEs3   C B7C B</C 7C 
D?DDrc   Nr   )ro   rp   rq   rr   r	  r   rs   rc   r`   r]   rH  rH  C  s2    
#
 &
 -	

 

r`   rH  c                      ^  \ rS rSrS\SS4U 4S jjrS rSS jrS r        SS jr	S	S
S\
S\4S jrS	S
S\
4S jrS rS rSrU =r$ )FunctoolsPartialVariableig  rF   rt   Nc                    > [         TU ]  " S0 UD6  Xl        [        U[        5      (       d   eX l        [        U[        5      (       d   eX0l        [        R                  " [        5      U l        g rb   )r   r^   rF   r   r   r}   rh   keywordsr  partialr-   
fake_value)rY   rF   r}   rS  r   r  s        r]   r^   !FunctoolsPartialVariable.__init__h  s\    "6"	$%%%%	(D))))  $++H5r`   c                 "    [         R                  $ rv   )r  rT  r   s    r]   r  $FunctoolsPartialVariable.python_types  s       r`   c                 R  ^ TR                  U4S j5        T" U R                  5        U R                  (       a  TR                  U R                  5        U R                  (       d2  TR                  [        [        U R                  5      S-   S5      5        g TR                  U R                  R                  5       5        [        U R                  R                  5       5      nTR                  TR                  [        U5      [        U R                  5      -   S-   US5      5        g )Nc                  (   > T R                  SS5      $ )Nr  rT  r  r  s   r]   rJ  6FunctoolsPartialVariable.reconstruct.<locals>.<lambda>w  s    g&>&>{I&Vr`   r2   F)rN  rF   r}   foreachrS  rO  r   rj   r  r   r  create_call_function_kw)rY   r  r  s    ` r]   r  $FunctoolsPartialVariable.reconstructv  s    VW		99OODII&}}!!"6s499~7I5"QR,,./T]]'')*++CIDII,F,JDRWX	
r`   c                 "    U R                  5       $ rv   r	  r   s    r]   r   %FunctoolsPartialVariable.get_function      &&((r`   r   r<   c                 x    U R                   U-   n0 U R                  EUEnU R                  R                  XU5      $ rv   )r}   rS  rF   r   )rY   r   r}   r   merged_argsmerged_kwargss         r]   r   &FunctoolsPartialVariable.call_function  s<     ii$&34==3F3yy&&rFFr`   r   c                     [         R                  R                  [        [        R
                  " [        5      U5      5      $ rv   )r   r8   r   r   r  rT  r-   r  s      r]   r   )FunctoolsPartialVariable.call_obj_hasattr  s2     ))00I%%h/6
 	
r`   c                    U R                   =(       a    [        U R                   U5      nUS:X  a  U R                  $ US:X  a  [        R                  " U R
                  US9$ US:X  aW  U R                  R                  5        VVs0 s H  u  pE[        R                  " U5      U_M     nnn[        R                  " XcS9$ U[        ;   a  [        R                  " X5      $ [        [        U5        g s  snnf )NrF   r}   r   rS  )r   r#   rF   r   ListVariabler}   rS  r   r8   r   ConstDictVariabler+   r6  r   r   )rY   r   r   r   r   r   r   s          r]   r7  $FunctoolsPartialVariable.var_getattr  s    >DKK!>6>996>))$))FCC:?C}}?R?R?TU?Ttq%,,Q/2?TEU..uDD)),,T88 4	 Vs   #C/c                 B   [         R                  " U R                  R                  5       /U R                   Vs/ s H  oR                  5       PM     snQ70 U R
                  R                  5        VVs0 s H  u  p#X#R                  5       _M     snnD6$ s  snf s  snnf rv   )r  rT  rF   r	  r}   rS  r   )rY   r[  r   r   s       r]   r	  +FunctoolsPartialVariable.as_python_constant  s      II((*
26))<)3$$&)<
 6:]]5H5H5JK5JTQq&&((5JK
 	
<K   B
4Bc                 B   [         R                  " U R                  R                  5       /U R                   Vs/ s H  oR                  5       PM     snQ70 U R
                  R                  5        VVs0 s H  u  p!X!R                  5       _M     snnD6$ s  snf s  snnf )zcSimilar to as_python_constant(), but add ID_MATCH guards to try to force things to become constants)r  rT  rF   guard_as_python_constantr}   rS  r   )rY   r   r   s      r]   rq  1FunctoolsPartialVariable.guard_as_python_constant  s      II..0
48II>Iq((*I>
 <@==;N;N;PQ;P41q,,..;PQ
 	
>Qro  )r}   rU  rF   rS  r  r   )ro   rp   rq   rr   r6   r^   r  r  r   r   r   r   r7  r	  rq  rs   ri  rj  s   @r]   rQ  rQ  g  s    	6_ 	64 	6!
)G#G &G -	G
 
G
)
14
	
55 5S 5

 
r`   rQ  c                     ^  \ rS rSrSSS1\R
                  kr\\R                  S\	\
S\4   \R                  4   4S j5       5       r\S 5       rS\SS	4U 4S
 jjr\S\4S j5       rS rS r        SS jr      SS jrS rSrU =r$ )PolyfilledFunctionVariablei  r   rn  traceable_fnrt   .c                     0 $ rv   rc   )r   s    r]   _get_polyfill_handlers1PolyfilledFunctionVariable._get_polyfill_handlers  s	     	r`   c                 ^    [        UR                  [        R                  5      5        U " XS9$ r   )r"   r   r!   FUNCTION_MATCHr   s      r]   r   -PolyfilledFunctionVariable.create_with_source  s&    f''(C(CDE5((r`   Nc                 D  > [         TU ]  " S0 UD6  Xl        U R                  5       R	                  X5      n[        U5      (       d   SU SU 35       eS H,  n[        X4S 5      nU(       d  M  [        U5      (       d   eUn  O   [        SU S35      eX0l        X`l	        g )NzPolyfill handler z is not callable for )__torch_dynamo_polyfill____python_implementation__z# does not have a traceable functionrc   )
r   r^   r   rw  rx   callabler   rG  rn  ru  )rY   r   r   handlercandidate_attr	candidateru  r  s          r]   r^   #PolyfilledFunctionVariable.__init__  s    "6"--/33B;  X$5gY>STVSW"XX 
N  >Iy	****(
 #G9,OP  & ,r`   c                     U R                   $ rv   )ru  r   s    r]   polyfill_fn&PolyfilledFunctionVariable.polyfill_fn  s       r`   c                 0    [        U R                  SS5      $ )N*__torch_dynamo_can_constant_fold_through__F)r   rn  r   s    r]   can_constant_fold_through4PolyfilledFunctionVariable.can_constant_fold_through  s    OOI5
 	
r`   c                 "    U R                  5       $ rv   r`  r   s    r]   r   'PolyfilledFunctionVariable.get_function  rb  r`   c                 r   U R                  5       (       a  [        X#5      (       aw  U R                  " U Vs/ s H  oDR                  5       PM     sn0 UR	                  5        VVs0 s H  u  pVXVR                  5       _M     snnD6n[
        R                  " X5      $ U R                  [        R                  L Ga<  [        U5      S:X  Ga,  U(       Gd$  [        US   [        R                  [        R                  45      (       a  [        S US   R                   5       5      (       a  [        R                  R!                  UUR"                  R%                  S[&        R(                  [+        S US   R                   5       5      40 5      [&        R(                  " US   R                   Vs/ s H:  n[        U[        R,                  5      (       a  UR.                  OUR0                  PM<     sn5      S9$ [
        R                  " XR2                  5      nUR5                  XU5      $ s  snf s  snnf s  snf )Nr2   r   c              3     #    U  H~  n[        U[        R                  5      =(       a    [        UR                  [        5      =(       d7    [        U[        R
                  5      =(       a    UR                  5       [        L v   M     g 7frv   )r   r   r8   r   intSymNodeVariabler  )r  rd  s     r]   r  ;PolyfilledFunctionVariable.call_function.<locals>.<genexpr>  sd       'A Ay99:Wz!''SV?W Yq)";";<WTWAWY&s   BBr   c              3   @   #    U  H  oR                  5       v   M     g 7frv   )as_proxy)r  as     r]   r  r    s     ?A::<<s   )sym_num)r  r*   r   r	  r   r6   r   r  sumrj   r   r   rj  TupleVariabler  r  r   r   create_proxyr  sym_sumr   r8   r   r  ru  r   )	rY   r   r}   r   rd  r   r   r   traceable_function_variables	            r]   r   (PolyfilledFunctionVariable.call_function  s    ))++0M1
 1
 6:;d**,d;=C\\^L^TQq..00^L  #((44 GGx||#D	Q47Y%;%;Y=T=T$UVV  a   ,,33		&&#MM?a??A	  "&a "/A  *!Y-G-GHH GG!"+ "/	 4  ( '6&;&;B@Q@Q&R#*886JJO <L4s   H)%H.-AH4c                 v   US:X  a  U R                  XU5      $ [        U R                  US 5      nUc   SU SU R                   35       e[        U5      (       d   SU SU R                   35       e0 nU R                  (       a  [        U R                  U5      US'   [        U40 UD6nUR                  XU5      $ )N__call__zMember z not found in z is not callable in r   )r   r   r   r.   r   r#   rt  )rY   r   r   r}   r   methodr%  polyfilled_method_variables           r]   r{  &PolyfilledFunctionVariable.call_method"  s     :%%b77$-!JWTF.	#JJ!6""QgdV3Gy$QQ";; *4;; =GH%?%R'%R")77&IIr`   c                     U R                   $ rv   r  r   s    r]   r	  -PolyfilledFunctionVariable.as_python_constant5  r  r`   )r   ru  rn  r   r}   r   r   r   rt   r6   )ro   rp   rq   rr   r6   rg  rh  r  cacherh   r   r   r   r   rw  r   r?   r^   propertyr  r  r   r   r{  r	  rs   ri  rj  s   @r]   rt  rt    s    
	'	'	N __tHS#X,>@R@R,R'S    ) )
-2 -D -. !R ! !

)2K#2K &2K -	2K
 
2KhJ &	J
 -J 
J& r`   rt  c                       \ rS rSrS rSrg)TracebackVariablei9  c                     g rv   rc   r   s       r]   r   TracebackVariable.call_function;  s    sr`   rc   Nro   rp   rq   rr   r   rs   rc   r`   r]   r  r  9  s    2r`   r  c                   :   ^  \ rS rSrU 4S jrS rS rS rSrU =r	$ )SysFunctionVariablei>  c                 2   > [         TU ]  " S0 UD6  Xl        g rb   )r   r^   r   )rY   r   r   r  s      r]   r^   SysFunctionVariable.__init__?  s    "6"
r`   c                 z   [        UR                  5      (       aK  UR                  S   nUR                  nS n[        R                  " X5      U[        R                  " X5      /nOA[
        R                  " S 5      [
        R                  " S 5      [
        R                  " S 5      /n[
        R                  " U5      $ )Nr  )rj   exn_vt_stackr  r6   r   r   r8   r  )rY   r   exnra  tbr   s         r]   exc_infoSysFunctionVariable.exc_infoC  s    r//"%C,,CB%%b.%%b-E **40**40**40E
 &&u--r`   c                 >    U R                  U5      R                  S   $ r4  )r  r   r   s     r]   	exceptionSysFunctionVariable.exceptionU  s    }}R &&q))r`   c                     U R                   [        R                  L a  U R                  U5      $ U R                   [        R                  L d   eU R                  U5      $ rv   )r   r  r  r  r   s       r]   r   !SysFunctionVariable.call_functionX  sF    ::%==$$zzS]]***~~b!!r`   r  )
ro   rp   rq   rr   r^   r  r  r   rs   ri  rj  s   @r]   r  r  >  s    .$*" "r`   r  ) create_tma_experimental_metadatacreate_tma_stable_metadataTMADescriptorMetadataTritonHOPifierc                       \ rS rSrS\S\4S jrS\S\4S jr	S\S\4S jr
S\\R                  R                  R                  S	4   4S
 jrS rS rS rS rS\S\4S jrSSS\\   SS4S jrS\4S jrSrg)DynamoTritonHOPifierig  rb  rt   c                     [        U5      erv   )r    )rY   rb  s     r]   raise_unsupported&DynamoTritonHOPifier.raise_unsupportedh  s    #r`   maybe_callablec                 .    [        U[        [        45      $ rv   )r   r   r   )rY   r  s     r]   is_callable DynamoTritonHOPifier.is_callablek  s    79MN
 	
r`   r   c                     UR                   $ rv   r  )rY   r   s     r]   	get_valueDynamoTritonHOPifier.get_valuep  s    yyr`   .c                     SSK Jn  [        X5      (       a  UR                  5       $ [	        SS[        U5       3S/ [        R                  QS9  g )Nr2   )BaseListVariablez/unsupported grid type for triton hop check_gridzgrid type = z;`torch.compile` only supports list-like grid for check_gridr   )listsr  r   r  r   rs  r   r6  )rY   gridr  s      r]   
check_gridDynamoTritonHOPifier.check_grids  sK    +d--==?"I&tDzl3Y&22	r`   c                     UR                  5        VVs0 s H%  u  pE[        R                  R                  U5      U_M'     nnnUR	                  X2/0 5      nU$ s  snnf rv   )r   r   r8   r   r   )rY   r  metar   r   r   s         r]   	call_gridDynamoTritonHOPifier.call_grid  sS    DHJJLQLDA	**11!4a7LQ!!"fb1 Rs   ,Ac                 X    SSK Jn  UR                  XA5      nUR                  XBU5      nU$ )Nr2   r  )r  r  r   r   )	rY   user_fnr}   r   r   r+  r  wrapped_user_functionr   s	            r]   call_user_defined_fn)DynamoTritonHOPifier.call_user_defined_fn  s-    . 1 8 8 E&44RvFr`   c                 l    SSK Jn  U" U[        UR                  U 5      5      R	                  U5      nU$ )Nr2   )VariableBuilder)r  r  r#   kernel_source_wrap)rY   user_objr   r+  r   r  wrapped_user_objs          r]   wrap_user_defined_obj*DynamoTritonHOPifier.wrap_user_defined_obj  s7    ,*
811dV=

%/ 	  r`   c                 r    UR                  U5      nU Vs/ s H  o3R                  " 5       PM     nnU$ s  snf rv   )unpack_var_sequencerq  )rY   configsr   r   s       r]   maybe_unpack_configs)DynamoTritonHOPifier.maybe_unpack_configs  s<    --b1 DKK72247K Ls   4r   c                 n    UR                  5       (       d  U R                  S5        UR                  5       $ )Nz`@triton.heuristics must return constant values because configs can only contain constant values.)is_python_constantr  rq  )rY   r   s     r]   maybe_unpack_heuristic_result2DynamoTritonHOPifier.maybe_unpack_heuristic_result  s3    ((**""r ..00r`   r+  TritonKernelVariabler}   c                     UR                   c  [        U5      S:w  a  U R                  S5        [        U5      " UR                  UR
                  US   UR                  S9$ )Nr2   z7Triton kernels should be called with only a single gridr   )kernel
kernel_idxr  r  )r  rj   r  rs  r  r  r   )rY   r+  r}   s      r]   call_getitem!DynamoTritonHOPifier.call_getitem  sZ     ==$D	Q""I H~??**a"//	
 	
r`   c                 `   SSK Jn  SSKJn  0 n[	        UR                  5       5       HH  nX8   n	[        U	[        [        45      (       d  M$  U	R                  5       Xx'   U	R                  5       X8'   MJ     UR                  5        VV	s0 s H%  u  p[        R                  R                  U5      U	_M'     n
nn	SSKJnJn  UR                  5        VV	s0 s H'  u  p[        X5      (       d  M  XR#                  5       _M)     nnn	U
R                  5        VV	s0 s H  u  p[        X5      (       a  M  X_M     nnn	UR%                  5        Hb  n	U	R'                  5       n	[        U	[        R(                  [        R*                  45      (       a  MD  U R-                  S[/        U	5       S35        Md     UR1                  U5      nU" U[2        5      nUR4                  R7                  SUS	UR8                  UUUUR;                  5       S
.5        [        R                  " S 5      $ s  sn	nf s  sn	nf s  sn	nf )Nr2   r7   )rk  r   )kernel_side_tabletriton_kernel_wrapper_mutationz.Unexpected argument type for a Triton kernel: r  r   rc   )r  constant_args_idxr  tma_descriptor_metadatar   )r'  r8   dictsrk  r   r  r   !TMADescriptorExperimentalVariableTMADescriptorStableVariableto_metadata
get_tensorr   r   r   *torch._higher_order_ops.triton_kernel_wrapr  r  r	  r  realizerb  r  r  repradd_constant_argsrh   r   r  r  r  )rY   r+  gridscombined_args_rawr   r8   rk  r  r   r   combined_argsr  r  rN  non_constant_argsr  r  s                    r]   call_HOPDynamoTritonHOPifier.call_HOP  s   ., :<',,./A!$A57RS  ./]]_'*'(||~!$ 0 *//1
1 &&--a0!31 	 

	
 *//1
1!. &A##%%1 	 
 &++-
-a2 AD- 	 
 #))+A		Aa)":":I<U<U!VWW&&DT!WIQO , .??N !2D9
		*&11%6+B--/		
 ))
 	
]



s   ,HH$+H$H*/H*rc   N)ro   rp   rq   rr   r   r   r  r   rJ  r  r  r   r  fxproxyProxyr  r  r  r  r  r  r   r  r8   r  rs   rc   r`   r]   r  r  g  s    S U 
# 
$ 

S S %(<(<c(A"B  1C 1C 1
(
 sm
 
 	
$C
BR C
r`   r  c                      ^  \ rS rSr% S\S'   S\S'   \\   \S'   S\S'   SU 4S
 jjr        SS jr      SU 4S jjr	S\
S	\
4S jrSrU =r$ )r  i	  r=   r  r>   r  r  r#   r  rt   c                    > UR                  SS 5      U l        [        TU ]  " S0 UD6  [        R                  XX#5        g )Nr  rc   )r   r  r   r^    dynamo_triton_hopifier_singletoninit_variable)rY   r  r  r  r   r  s        r]   r^   TritonKernelVariable.__init__	  s6    #ZZ>"6"(66tZVr`   c                 .    [         R                  XX15      $ rv   )r	  call_triton_kernelr   s       r]   r   "TritonKernelVariable.call_function	  s     0BB
 	
r`   c                    > US:X  a  [         R                  X5      $ US:X  a  [         R                  XXA5      $ [        TU ]  XX45      $ )N__getitem__run)r	  r  call_runr   r{  r  s        r]   r{   TritonKernelVariable.call_method	  sL     = 3@@LLU]3<<TTT w"2T::r`   r[  c                 ~    SSK Jn  SSKJn  [	        X5      (       a   UR
                  " UR                  5       5      $ U$ )Nr2   r7   )r  )r'  r8   tensorr  r   r   evaluate_expr)rY   r[  r8   r  s       r]   specialize_symbolic(TritonKernelVariable.specialize_symbolic)	  s4    .+ c++#**3+<+<+>??
r`   )r  rf  r   r  )ro   rp   rq   rr   r   r   r  r^   r   r{  r   r  rs   ri  rj  s   @r]   r  r  	  s    
W

#
 &
 -	

 

; &	;
 -; 
;s s  r`   r  c                   R   ^  \ rS rSr        SU 4S jjrS rSS jrS rSrU =r	$ )	r  i3	  c                    > [        U[        R                  5      (       d   e[        TU ]  " S0 UD6  Xl        X l        X0l        X@l        g rb   )	r   r   DataPtrVariabler   r^   data_ptrdims
block_dimselement_size)rY   r  r  r  r  r   r  s         r]   r^   *TMADescriptorExperimentalVariable.__init__4	  sC     (I$=$=>>>>"6" 	$(r`   c                     [        U R                   Vs/ s H  oR                  5       PM     snU R                   Vs/ s H  oR                  5       PM     snU R                  R                  5       5      $ s  snf s  snf rv   )r  r  r  r  r  )rY   dims     r]   r  -TMADescriptorExperimentalVariable.to_metadataC	  s[    /'+yy1y\\^y1'+7\\^7&&(
 	
17s
   A6A;
c                   ^ ^ TR                  UU 4S j5        T R                  R                  T5        / T R                  QT R                  QT R
                  PnTR                  U5        TR                  [        U5      S-   S5        g )Nc                  V   > T R                  SS[        TR                  5       S35      $ )Nz$triton.tools.experimental_descriptorcreate_d_tma_descriptor)r  rj   r  rI  s   r]   rJ  ?TMADescriptorExperimentalVariable.reconstruct.<locals>.<lambda>L	  s)    G,,6#dii.))9:r`   r2   F)	rN  r  r  r  r  r  r\  r   rj   )rY   r  r}   s   `` r]   r  -TMADescriptorExperimentalVariable.reconstructJ	  sp    	
 	!!'*@@T__@d.?.?@c$i!mU3r`   c                 .    U R                   R                  $ rv   )r  from_tensorr   s    r]   r  ,TMADescriptorExperimentalVariable.get_tensorV	  s    }}(((r`   )r  r  r  r  )r  zvariables.DataPtrVariabler  list[ConstantVariable]r  r-  r  r8   r  
ro   rp   rq   rr   r^   r  r  r  rs   ri  rj  s   @r]   r  r  3	  sB    )-) ') -	)
 ))

4) )r`   r  c                   N   ^  \ rS rSr    SU 4S jjrS rSS jrS	S jrSrU =r	$ )
r  iZ	  c                    > [        U[        R                  5      (       d   e[        TU ]  " S0 UD6  Xl        X l        g rb   )r   r   rb  r   r^   r  block_shape)rY   r  r1  r   r  s       r]   r^   $TMADescriptorStableVariable.__init__[	  s9     &)":":;;;;"6"&r`   c                 H    [        U R                  R                  5       5      $ rv   )r  r1  r  r   s    r]   r  'TMADescriptorStableVariable.to_metadataf	  s!    )%%'
 	
r`   c                    ^ TR                  U4S j5        TR                  S5        U R                  R                  T5        T" U R                  5        TR                  S5        g )Nc                  (   > T R                  SS5      $ )Nztriton.tools.tensor_descriptorTensorDescriptorr  r  s   r]   rJ  9TMADescriptorStableVariable.reconstruct.<locals>.<lambda>m	  s    G,,0"r`   r+  r   )rN  load_methodr  r  r1  r{  rP  s    `r]   r  'TMADescriptorStableVariable.reconstructk	  sT    	
 	M*(  !Ar`   c                     U R                   $ rv   )r  r   s    r]   r  &TMADescriptorStableVariable.get_tensorw	  s    {{r`   )r1  r  )r  variables.TensorVariabler1  zvariables.ListVariabler  )rt   r=  r.  rj  s   @r]   r  r  Z	  s.    	'*	' .	'


 r`   r  c                   N   ^  \ rS rSrS\SS4U 4S jjr        SS jrSrU =r$ )	'CreateTMADescriptorExperimentalVariablei{	  rankrt   Nc                 B   > US;   d   e[         TU ]  " S0 UD6  Xl        g )N)r2   r   rc   )r   r^   r@  )rY   r@  r   r  s      r]   r^   0CreateTMADescriptorExperimentalVariable.__init__|	  s&    
 v~~"6"	r`   c                 $   SU;   a  US   OUS   n[        U[        R                  5      (       d  [        SU R                   S35      eU R                  S:X  a@  [        U5      [        U5      -   S:X  d   eSU;   a  US   OUS   /nSU;   a  US   OUS	   /nO][        U5      [        U5      -   S
:X  d   eSU;   a  US   OUS   SU;   a  US   OUS	   /nSU;   a  US   OUS   SU;   a  US   OUS   /nSU;   a  US   OUS   n[        UUUUS9$ )Nptrr   z8Please ensure there were no graph breaks between create_z3d_tma_descriptor and the upstream .data_ptr() call.r2   rA   r"  	block_dimr      dim1dim0
block_dim1r  
block_dim0r  r  )r  r  r  r  )r   r   r  r    r@  rj   r  )rY   r   r}   r   rD  r  r  r  s           r]   r   5CreateTMADescriptorExperimentalVariable.call_function	  sd     %fUmDG#y8899)) %$$  99>t9s6{*a///!&&ud1gD (3f'<{#$q'J t9s6{*a///"(F"2vQ"(F"2vQD
 )5(>|$DG(4(>|$DGJ 2@61Ivn-tTVx0!%	
 	
r`   )r@  r   )	ro   rp   rq   rr   r  r^   r   rs   ri  rj  s   @r]   r?  r?  {	  sJ     
	(
#(
 &(
 -	(

 
(
 (
r`   r?  c                   .    \ rS rSr        SS jrSrg)!CreateTMADescriptorStableVariablei	  c                 V    SU;   a  US   OUS   nSU;   a  US   OUS   n[        UUS9$ )Nr  r   r1  r2   )r  r1  )r  )rY   r   r}   r   r  r1  s         r]   r   /CreateTMADescriptorStableVariable.call_function	  sF     &.%7!T!W/</Ff]+DQRG*#
 	
r`   rc   Nr   r  rc   r`   r]   rM  rM  	  s-    
#
 &
 -	

 

r`   rM  rv   r  )r   r  r  r   r   r  r  r  r   collections.abcr   r   typingr   r   r   r	   r
   typing_extensionsr   unittest.mockr   weakrefr   r  torch._dynamo.excr   r&  r   r   r   r   bytecode_transformationr   r   r   r  r   r   r   r   r   r   r   r   r   r    guardsr!   r"   r   r#   r$   r%   r&   r'   r(   rS  r)   r*   r+   r,   r-   r.   r/   r0   r1   baser3   r4   r5   r6   r'  r8   #torch.distributed.fsdp._fully_shardr9   ModuleNotFoundErrortorch._dynamo.codegenr:   r  r<   r  r=   r>   r?   rU   rW   rw   rD   rz   r   r   r   rh   r   r   r   r   r   r   r   rl  r  r  r   r  r"  r:  rW  rP  r   r  r  r  r  r
  r  r  r  r@  rH  rQ  rt  r  r  r  r  r  r  r  r	  r  r  r  r?  rM  rc   r`   r]   <module>r\     s  .      
   $  B B #  %  4 > > V V   1 
 
 
  'E
 /D T"
  !
 
>L \ Up	A. $sO';"<  "( -0 Dk73 k7\:4 :<B4? B4J

 
>
%= >
B"<g-- g-T> 2 >2>"6 >0 p)!9 p)f%>(B %>PgA? gAT>"6 >2>
/ >
B ; /QD(< QDh71 74!
$8 !
HN
 N
b~ ~B3 3
"/ "B X
> X
v $8#9  +? +\$) $)N/ B2
o 2
j
 
oJ  s   J JJ