
    io                    0   d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
 ddlmZ ddlmZmZmZmZmZ ddlmZ ddlmZ ddlmZ ddlZdd	lmZ d
dlmZmZmZmZ d
dl m!Z!m"Z"m#Z# d
dl$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z. d
dl/m0Z0m1Z1 d
dl2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8 d
dl9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZB ddlCmDZDmEZEmFZFmGZG ddlHmIZI 	 ddlJmKZK erddlMmNZN ddlOmPZP ddlQmRZRmSZS  ede      ZTdZUdZV e       ZW G d d      ZXdedeXfd ZYd! ZZdgdhd"Z[dhd#Z\d$e]e^eGf   fd%Z_d& Z`h d'Zad( Zb G d) d*eG      Zc G d+ d,ec      Zd G d- d.ec      Ze G d/ d0eG      Zf G d1 d2ef      Zg G d3 d4ec      Zh G d5 d6eh      Zi G d7 d8ed      Zj G d9 d:ej      Zk G d; d<ed      Zldhd=Zm G d> d?ec      Zn G d@ dAen      Zo G dB dCeG      Zp G dD dEep      Zq G dF dGeG      Zr G dH dIer      ZsdJ ZtdhdKZu G dL dMed      Zv G dN dOed      Zw G dP dQed      Zx G dR dSeG      Zy G dT dUeG      Zz G dV dWeG      Z{ G dX dYeG      Z|ddZlQm}Z}m~Z~mZmZ  G d[ d\e      Z e       Z G d] d^eG      Z G d_ d`eG      Z G da dbeG      Z G dc ddeG      Z G de dfeG      Zy# eL$ r dZKY w xY w)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                   $    e Zd ZdefdZdefdZy)FunctionSpecfuncc                 p   |j                   }|j                  }|j                  | _        |j                  | _        |j                  | _        |d | j                   | _        || j                  | j
                   | _	        | j                  | j                  z   | _
        || j
                  | j
                  | j                  z    | _        | j
                  | j                  z   }|j                  t        z  r||   nd | _        || j                  rdndz  }|j                  t        z  r||   | _        y d | _        y )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)selfrE   codevnoffs        [/var/www/html/engine/venv/lib/python3.12/site-packages/torch/_dynamo/variables/functions.py__init__zFunctionSpec.__init__p   s   }}!44)) 22 4$"4"45!$"4"4t~~F!//$2F2FFt~~ARAR0RSnnt000'+}}z'ABsGtD%%q1,%)]]^%C"S'    c           
         |j                   xs d| _        |j                  xs i | _        t	        t        | j                  t        | j                         d  t        t        | j                                          | _	        y N )
__defaults__defaults__kwdefaults__
kwdefaultsdictziprQ   lenrangepos_default_map)rX   rE   s     r\   update_defaultszFunctionSpec.update_defaults   si     ))/R--3  $""C$6#6#895T]]AS;TU 
r^   N)__name__
__module____qualname__r   r]   rk   ra   r^   r\   rD   rD   o   s    N\ N$	
L 	
r^   rD   rE   returnc                 \    t         j                  |       }|t        |       }|t         | <   |S N)_spec_cachegetrD   )rE   specs     r\   	_get_specru      s.    ??4 D|D! DKr^   c                    t        |       }|j                  |        i }t        |      }t        |j                        D ]'  \  }}	|t        |      k  rt        |||         ||	<   (|	|v rV|	|j                  v r)t        t        |t        j                  |	 d      g       t        ||j                  |	            ||	<   |	|j                  v ro|j                  |	   }
d }|r>t        j                  |j                  |
         rt         j"                  st%        ||
      }t        ||j                  |
   |      ||	<   t        t        |t        j                  d|	       g       * |t        |j                        d  }|j&                  r#t        |t)        |            ||j&                  <   nJ|rHt        t        |t        j                  dt        |       dt        |j                               g       |j*                  D ]  }	|	|v rt        ||j                  |	            ||	<   &|	|j,                  v r0d }|rt%        ||	d      }t        ||j,                  |	   |      ||	<   dt        t        |t        j                  d|	       g        |j.                  rt        ||      ||j.                  <   |S |r2t        t        |t        j                  d	t1        |             g       |S )
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: )ru   rk   rf   	enumeraterQ   rh   wrap_bound_argrO   r   	TypeErrorr8   createpoprj   
is_literalrc   r   %skip_guards_on_constant_func_defaultsr&   rU   tuplerR   re   rW   list)rE   tx	fn_sourcerx   kwargsrt   barem_kwinameidxdefault_sourceextrakwdefault_sources                 r\   bind_args_cachedr      s   T?D	B&\F T//0 4s4y=%b$q'2BtHV^t)))(*11TF:M2NOP
 &b&**T*:;BtHT)))&&t,C!N ++DMM#,>?@@!/	3!?%b$--*<nMBtH$$++@G+@ T''(*+E .r5< @4	  ''9#d)KPSTXTfTfPgOhi	
 !! 6>%b&**T*:;BtHT__$##1)T#N %b$//$*?AQRBtH$$++B4&I( ,R84?? I 
  ''*Hf(WX	
 Ir^   c                     t        |t              r|S |st        j                  | |      S t        j                  j                  ||      S rq   )
isinstancer6   buildr   LazyVariableTrackerr}   )r   valsources      r\   r{   r{      sB    #'
$$R-- ,,33C@@r^   c                     t        |j                               D ]+  \  }}t        |t        t        f      st        | |      ||<   - y rq   )r   itemsr   r   rf   r{   )r   resultkvs       r\   wrap_args_kwargsr      s>    V\\^$ .1a%'&r1-F1I.r^   r   c                     | j                   j                  }|j                  D ]<  }|j                         }||v r!|j	                  ||j                  |             |||<   > y)z
    Update `result` to add mapping from local name to new cells created
    directly by `code`, or update SideEffects in `parent` if the a local cell is
    already in `result` (cell argument).
    N)outputside_effectsco_cellvarstrack_cell_new
store_cellr~   )parentr   rY   r   r   new_cells         r\   init_cellvarsr     sa     ==--L    ..06> ##Hfjj.>?t r^   c                     ddl m}  || ||||      }||_        t        |t              rddlm}	 t         |	|            }|t        |t              sJ ||_        |S )Nr   r   pairwise)	typesr   rd   r   r   	itertoolsr   rf   __annotations__)
rY   	f_globalsr   rc   closurere   annotationsr   rE   r   s
             r\   _create_nested_fnr     sa     #ixAD$D+u%&8K01 *[$"???&DKr^   >   __doc__rG   __closure____globals__rb   rd   r   c                 J   |xr t        ||      }|r|dk(  rt        |      }	 t        j                  ||      }|t        v rt        ||      }|r t        j                  j                  |      S t        j                  |       S # t        $ r t        t        |        Y gw xY w)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
640F$++ !(5''D1 $$T",,33FFCC  V,,  5 	!45s   B B"!B"c                   P    e Zd Zd Zd Z	 	 	 	 	 	 	 	 ddZdddedefdZd	 Z	d
 Z
y)BaseUserFunctionVariablec                 6    | j                         j                  S rq   get_codeco_filenamerX   s    r\   get_filenamez%BaseUserFunctionVariable.get_filenameJ      }}***r^   c                 6    | j                         j                  S rq   r   co_namer   s    r\   get_namez!BaseUserFunctionVariable.get_nameM      }}&&&r^   r   r<   ro   c                 L    |j                  | g | j                         ||      S rq   )inline_user_function_return	self_argsrX   r   rx   r   s       r\   call_functionz&BaseUserFunctionVariable.call_functionP  s,     --d4Ndnn6F4N4NPVWWr^   r   c                     d}	 t        | j                         |      }t
        j                  j                  |      S # t        $ r |dk(  rt        | t              rd}Y Aw xY w)NFrl   T)hasattrget_functionNotImplementedErrorr   NestedUserFunctionVariabler   r8   r}   rX   r   r   r   s       r\   call_obj_hasattrz)BaseUserFunctionVariable.call_obj_hasattrX  se     	T..0$7F ))0088 # 	z!j7Q&R	s   =  A A c                 n    t        t        j                  | j                               j                        S rq   )r   r   	signaturer   
parametersr   s    r\   inspect_parameter_namesz0BaseUserFunctionVariable.inspect_parameter_namesd  s'    G%%d&7&7&9:EEFFr^   c                     i S rq   ra   rX   r   s     r\   closure_varsz%BaseUserFunctionVariable.closure_varsg      	r^   Nr   r<   rx   list[VariableTracker]r   dict[str, VariableTracker]ro   r6   )rl   rm   rn   r   r   r   strr6   r   r   r   ra   r^   r\   r   r   I  sd    +'X#X &X -	X
 
X
9)
914
9	
9Gr^   r   c                        e Zd ZdZddhej
                  Zed        Zdd fdZ fdZ	d Z
d	 Zd
 Zd Zd Zd Zd Zdeeef   fdZdddefdZdddedefdZ	 	 	 	 	 	 	 	 d fdZ xZS )UserFunctionVariablez-Some unsupported user-defined global functionr   is_constantc                 f    t        |j                  t        j                                | ||      S Nr   )r"   
make_guardr!   CLOSURE_MATCHclsvaluer   s      r\   create_with_sourcez'UserFunctionVariable.create_with_sourcet  s(    f''(B(BCD5((r^   ro   c                 B   t        |   d	i | t        |dd      rd| _        nd| _        t	        |t
        j                  t        j                  j                  f      s!t        d| ddgt        j                         t        j                  |d|      }|| _        y )
N_dynamo_marked_constantFTz1can't handle functions not implemented in python z2Dynamo can only handle functions defined in pythonz9Move usage of this function out of `torch.compile` regiongb_typecontextexplanationhints_torchdynamo_inlinera   )superr]   r   r   r   r   r   torchjitScriptFunctionr   r   INFERENCE_MODEr   r   r   rX   r   r   r   	__class__s       r\   r]   zUserFunctionVariable.__init__y  s    "6"20%8#D$D "u115993K3KLMK$PO&55	 ##B(=rB&(r^   c                 X    t        | t              r| j                  S t        |          S rq   )r0   r   r   r   as_python_constantrX   r   s    r\   r   z'UserFunctionVariable.as_python_constant  s%    $,-77Nw)++r^   c                     g S rq   ra   r   s    r\   r   zUserFunctionVariable.self_args  r   r^   c                     | j                   S rq   r   r   s    r\   r   z!UserFunctionVariable.get_function      wwr^   c                 .    | j                   j                  S rq   )r   rG   r   s    r\   r   zUserFunctionVariable.get_code  s    wwr^   c                 "    t         j                  S rq   r   r   r   s    r\   python_typez UserFunctionVariable.python_type      !!!r^   c                 4    t        | j                  dd       d uS N__self__)r   r   r   s    r\   has_selfzUserFunctionVariable.has_self  s    tww
D1==r^   c                 .    | j                   j                  S rq   )r   r   r   s    r\   get_globalsz UserFunctionVariable.get_globals  s    ww"""r^   c                 n    | j                   }|r&t        | t        j                        r| j                  }|S rq   )r   r   r   UserMethodVariable	source_fn)rX   r   s     r\   
get_sourcezUserFunctionVariable.get_source  s+    jy'C'CD^^Fr^   c                 &   | j                   rJ | j                  }t        |t              st	        d      |j
                  j                  }| j                         }t        |||||      }t        |||j                         | j                  j                  xs d}t        |      t        | j                  j                  j                        k(  sJ t        t        j                          | j                  j                  j                  |      D ]  \  }	}
}|j
                  j"                  }||v r||   }n|rWt%        t'        |      |	      }t)        |d      }	 t+        j,                  ||j.                  |      }|j7                  |||      }n4	 t+        j,                  ||j.                        }|j7                  d||      }|||
<    |S # t0        $ r t3        j4                         }Y pw xY w# t0        $ r t3        j4                         }Y ^w xY w)z
        Assume `args` and `kwargs` are VariableTracker arguments for a call to
        this function, create new bindings for initial locals.
        z'Only supports regular Python functions.ra   cell_contentsN)r   r   r   r   r|   r   root_txr  r   r   rG   r   rh   co_freevarsrg   r   countr   r'   r$   r#   r6   r   r  
ValueErrorr   DeletedVariabletrack_cell_existing)rX   r   rx   r   r   r  r   r   r   r   r   cellr   cell_varclosure_cellclosure_cell_contentscontents_vars                    r\   	bind_argszUserFunctionVariable.bind_args  s   
 ####!%"l+EFF--''"!"gvtVDffbkk2''%%+7|s477#3#3#?#?@@@@"OOtww//;;W 
  	$OCt "==55L|#'-,]6-BCH(2<(Q%?#2#8#8 2 24I$L (;; $?#2#8#8ASAS#TL (;;D$U#F4LA 	$D ' " ?#,#<#<#>L? " ?#,#<#<#>L?s$   !G G0G-,G-0HHr   r<   r   c                     |t         v rt        j                  | |      S | j                         }t	        || j
                  ||      S rq   )r+   r   GetAttrVariabler  r   r   )rX   r   r   r   s       r\   var_getattrz UserFunctionVariable.var_getattr  s?    )),,T488"b$''6488r^   c                 l    t        | j                  |      }t        j                  j	                  |      S rq   )r   r   r   r8   r}   r   s       r\   r   z%UserFunctionVariable.call_obj_hasattr  s+     $'))0088r^   c           
         | j                   t        j                  j                  u r	 |D cg c]  }|j	                          }}|j                         D ci c]  \  }}||j	                          }}}t        j                  j                  |i |j                  }	t        j                  |	      S | j                   t        j                  j                  u rt	  t        j                  | j                         j                  |i |}|j                  d   j	                         }t!        |t"              sJ t        j$                  |      S | j                   t        j                  j(                  u r  t        j                  | j                         j                  |i |}|j*                  d   }t!        |t,              sS|j/                         }d|j0                   d}t3        dd	| j4                   d
| d
| d|g t6        j8                         t!        |t:              s,|j=                         }d| d}t3        d|  |d| ddg       |j                   }t        j>                  |d      S | j@                  r'tC        || j                   | j=                         ||      S |jD                  jF                  jH                  st| j                   t        j                  jJ                  jL                  u rDt        j                  jN                  jQ                  |      5  tR        |   |||      cd d d        S |jD                  jF                  jV                  r|jD                  jF                  jX                  sq	 ddl-m.} |h| j                   |j^                  |j`                  fv rDt        j                  jN                  jY                  |      5  tR        |   |||      cd d d        S tR        |   |||      S c c}w c c}}w # t        $ r}
t        d| d|       |
d }
~
ww xY w# t&        $ r}
t        d| d|       |
d }
~
ww xY w# 1 sw Y   xY w# t&        $ r d }Y w xY w# 1 sw Y   xY w)NzCannot convert patch_dynamo_config args/kwargs to constants. Please fix your call to patch_dynamo_config by using simpler inputs. args: 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_tracerx   r   r  rl   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_stater*  _pre_forward_post_forward)rX   r   rx   r   arg
args_constkeyr   kwargs_constr-  er@   r$  fn_vartypmsgfn_namer   r*  r   s                      r\   r   z"UserFunctionVariable.call_function  sR    77emm777BFG3c446G
GBH,,. 6>c3C//11     --;;#/'  !::7CC WW:::7))$''277HH',*($$& % ""6=== ::;OPP WW5553G%%dgg.33TDVDEZZ]Ff&>?((*UVYVbVbUccde 6,TZZL4&6(!L #*55	 f&:; //+@	  Ju  v <#f #27)<H0	 B99"RVWW/DGGT]]_dF 
 		((UU}}""[[\ ++^^ ? w,Rv>? ? II$$@@II,,PP!U $&&''5 * ]]//SSTVW C 70T6BC Cw$Rv66q H  7 "!F*VH6 	  "!F*VH6 ^? ?  ! 	!C Csx   P
 O?P
 P4A P
 A2P- "Q>Q Q.?P
 
	P*P%%P*-	Q6QQQQ+*Q+.Q7Fro   Nr   )rl   rm   rn   r   r   _nonvar_fieldsclassmethodr   r]   r   r   r   r   r  r
  r  r  rf   r   r6   r  r!  r   r   __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                   J     e Zd Zdd fdZed        Z	 	 	 	 	 	 	 	 ddZ xZS )BuiltinMethodVariablec                 j    t        |   di | t        |t        j                        sJ || _        y r`   )r   r]   r   r   BuiltinMethodTyper   r   s       r\   r]   zBuiltinMethodVariable.__init__Z  s0    "6""e55666r^   c                     | j                   }| j                  }|t        u xr |dk(  xs t        |      t        u xr |dk(  S )N__new____contains__)r	  rl   r   type	frozenset)objmethod_selfmethod_names      r\   is_supported_builtin_methodz1BuiltinMethodVariable.is_supported_builtin_method_  sI    llll u$A	)A 
*L{n/L	
r^   c                     | j                   j                  }| j                   j                  }| j                  xr t	        | j                  d      }t        j                  |||      }|j                  ||||      S r  )r   r	  rl   r   r#   r6   r   call_method)rX   r   rx   r   r\  r   
obj_sourceobj_vts           r\   r   z#BuiltinMethodVariable.call_functionj  sf     gg&&ww[[HZZ%H
 &&r;
C!!"dD&99r^   rL  rM  r   )rl   rm   rn   r]   staticmethodr^  r   rP  rQ  s   @r\   rS  rS  Y  sG    
 
 

:#
: &
: -	
:
 

:r^   rS  c            
           e Zd Zdej                  ded   f fdZd Zd Zd Z	d Z
d	 Zd
 Z d ZeZd#dZd Zd Zd Zd Zd Zd Zd Zdej.                  fdZdee   fdZd$dZd Zd Zd Zddde dddd dd!f
 fd"Z! xZ"S )%LocalGeneratorObjectVariablerY   inline_tracerr<   c                 N    t        |   di | || _        || _        || _        y r`   )r   r]   rY   r   rf  )rX   rY   r   rf  r   r   s        r\   r]   z%LocalGeneratorObjectVariable.__init__x  s+     	"6"	"*r^   c                     | j                   S rq   )rY   r   s    r\   r   z%LocalGeneratorObjectVariable.get_code  s    yyr^   c                 6    | j                         j                  S rq   r   r   s    r\   r   z)LocalGeneratorObjectVariable.get_filename  r   r^   c                 6    | j                         j                  S rq   r   r   s    r\   r   z%LocalGeneratorObjectVariable.get_name  r   r^   c                     t         rq   )r   r   s    r\   r   z)LocalGeneratorObjectVariable.get_function  s    !!r^   c                      yNFra   r   s    r\   r
  z%LocalGeneratorObjectVariable.has_self      r^   c                 "    | j                         S rq   )r   r   s    r\   rl   z%LocalGeneratorObjectVariable.__name__  s    }}r^   c                 V    | j                   j                   d| j                          dS )N(r'  )r   rl   r   r   s    r\   __str__z$LocalGeneratorObjectVariable.__str__  s'    ..))*!DMMO+<A>>r^   c                    ddl m} ddlm}m}m}  |j                         } ||      } ||      } ||      }	|5  |5  |	5  | j                  |      }
|
j                  s| j                  |      | _
        t        j                  | j                        j                  |       d d d        d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)Nr   )"disallow_side_effects_in_generator)r<    save_and_restart_speculation_log(temporarely_allow_writes_to_output_graph)torch._dynamo.side_effectsrt  torch._dynamo.symbolic_convertr<   ru  rv  
current_tx_get_inline_tracergenerator_exhaustedforce_unpack_var_sequenceremaining_itemsr   ListIteratorVariablereconstruct)rX   codegenrt  r<   ru  rv  r   savedisallowtemptracers              r\   r  z(LocalGeneratorObjectVariable.reconstruct  s    Q	
 	
 ."--//35b97; 	V8 	VT 	V,,R0F--'+'E'Eb'I$**4+?+?@LLWU		V 	V 	V 	V 	V 	V 	V 	V 	Vs;   CC	A"B=$C	,C=CC		C	CCc                 <    | j                   j                  |||      S rq   )r   r  r   s       r\   r  z&LocalGeneratorObjectVariable.bind_args  s    ww  T622r^   c                     | j                   S rq   r   r   s    r\   r  z(LocalGeneratorObjectVariable.get_globals      ~~r^   c                 "    t         j                  S rq   )r   GeneratorTyper   s    r\   r  z(LocalGeneratorObjectVariable.python_type  s    """r^   c                 p    ddl m} | j                  |j                  || g i       | _        | j                  S Nr   )InliningInstructionTranslator)rx  r  rf  build_inline_tracer)rX   r   r  s      r\   rz  z/LocalGeneratorObjectVariable._get_inline_tracer  s;    P%!>!R!RD"b"D !!!r^   c                 r   | j                  |      }| j                         rt        t        |       	 t	        j
                  t        dt        d   i      5  |j                         cd d d        t        dxx   t        d   z  cc<   S # 1 sw Y   nxY wnm# t        $ r}d|_	        |d }~wt        $ r  t        $ rC}t        j                  j                  j                  | j!                                t"        |d }~ww xY w	 t        dxx   t        d   z  cc<   y # t        dxx   t        d   z  cc<   w xY w)Nunimplementedinline_callT)rz  _is_generator_exhaustedr   StopIterationr   rf   r,   inline_call_r   r{  r   r    r   r+  
eval_frame	skip_coder   r   )rX   r   r  rG  s       r\   next_variablez*LocalGeneratorObjectVariable.next_variable  s   ((,'')$]B7	A H8O&PQ -**,- - _%-)@@%- - -  	)-F&G% 	 	#MM$$..t}}?"	#- _%-)@@%H_%-)@@%sM   #B B&	B BB D 	C?	B''C?<>C::C??D D6c                     || j                         j                  v rt        j                  d      S t        j                  d      S )NTF)r  __dict__r8   r}   rX   r   r   s      r\   r   z-LocalGeneratorObjectVariable.call_obj_hasattr  s;    4##%...#**400&&u--r^   c                      yrm  ra   r   s     r\   has_unpack_var_sequencez4LocalGeneratorObjectVariable.has_unpack_var_sequence  rn  r^   ro   c                      yNTra   r   s     r\   has_force_unpack_var_sequencez:LocalGeneratorObjectVariable.has_force_unpack_var_sequence  s    r^   c                 B    g }| j                  ||j                         |S rq   )force_apply_to_var_sequenceappend)rX   r   r   s      r\   r|  z6LocalGeneratorObjectVariable.force_unpack_var_sequence  s     ((V]];r^   c                 j    	 	  || j                  |             # t        $ r t        |       Y y w xY wrq   )r  r   r   )rX   r   r   s      r\   r  z8LocalGeneratorObjectVariable.force_apply_to_var_sequence  s>    4%%b)*  - )"-s    22c                     | j                  |      }	 |j                  |       y # t        $ r}|j                  |       Y d }~y d }~ww xY wrq   )rz  _raise_exception_variabler   exception_handler)rX   r   excr  rG  s        r\   _setup_exceptionz-LocalGeneratorObjectVariable._setup_exception  sH    ((,	(,,S1  	( $$Q''	(s   % 	A	AA	c                 T    | j                   d u xs | j                   j                  dk(  S )Nr   )rf  instruction_pointerr   s    r\   _is_generator_just_startedz7LocalGeneratorObjectVariable._is_generator_just_started  s*    !!T)XT-?-?-S-SWX-XXr^   c                 0    t        | j                  dd      S )Nr{  F)r   rf  r   s    r\   r  z4LocalGeneratorObjectVariable._is_generator_exhausted  s    t))+@%HHr^   r   r   rx   r   r   r   r6   c                    |dk(  r| j                  |      S |dk(  r| S |dk(  rp| j                         r-t        |      r"t        d |D              st	        t
        |       | j                  |      }|j                  |       | j                  |      S |dk(  r| j                  |      }| j                         s| j                         rd|_	        t        j                  d       S 	 | j                  |t        j                  t        d             t        j                   dk\  r5|j"                  j$                  d	k(  rd|_	        t        j                  d       S 	 | j                  |      rt	        t(        |       n|d
k(  r| j                  |      }	 | j                  |t        |      dk(  r|d   n|d          | j                  |      }t1        dt2        fi       }	 | j                  |t        j                  |d             | j                  |       t	        t(        |       |S t6        | q  ||||       y # t&        $ r d|_	        t        j                  d       cY S w xY w# t&        $ r d|_	        t        j                  d       cY S t*        $ r |j,                  J |j,                  cY S w xY w# t.        $ r  w xY w# t5        |      $ r Y |S w xY w)N__next____iter__sendc              3   \   K   | ]$  }t        |t              xr |j                  d u  & y wrq   )r   r8   r   ).0rC  s     r\   	<genexpr>z;LocalGeneratorObjectVariable.call_method.<locals>.<genexpr>  s1       s$45K#))t:KKs   *,closeTra   )      CALL_INTRINSIC_1throwr  r2   r   __InternalThrowException)r  r  rh   allr   r|   rz  	push_manyr  r{  r   r8   r  ExceptionVariableGeneratorExitsysversion_infonext_instructionopnamer   r/  r   symbolic_resultr   rY  r4  r   r   r`  )	rX   r   r   rx   r   r  retvalexc_typer   s	           r\   r`  z(LocalGeneratorObjectVariable.call_method  s    :%%b))ZKV^ ..0SY  #  -Y;,,R0FT"%%b))W_ ,,R0F..0D4P4P4R-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BdF3a ) 8-1* 11$778 ) 8-1* 11$77, .
 --999---., % N 1:  MsI   'A1H; !I& (J5 7K ;%I#"I#&%J2"J21J25K KKr  r:   rM  )#rl   rm   rn   r   CodeTyper   r]   r   r   r   r   r
  rr  __repr__r  r  r  r  rz  r  r   r  builtinsr2  r  r   r6   r|  r  r  r  r  r   r`  rP  rQ  s   @r\   re  re  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^   re  c                       e Zd ZdZy)4ContextlibContextManagerLocalGeneratorObjectVariablez
    .. note::

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

        It is a special case of a generator function as we do not allow return a context manager
        from a torch.compile function.
    N)rl   rm   rn   r   ra   r^   r\   r  r    s    r^   r  c                   R     e Zd ZdZeddef fdZd Zd Z	 	 	 	 	 	 	 	 ddZ	 xZ
S )	LocalGeneratorFunctionVariablezxfunctions that behaves like iterators

    .. note::

        This is a wrapper around (Nested)UserFunctionVariable
    )generator_clsvtc                @    t        |   di | || _        || _        y r`   )r   r]   r  r  )rX   r  r  r   r   s       r\   r]   z'LocalGeneratorFunctionVariable.__init__  s$     	"6"*r^   c                     || j                   j                  j                         v rt        | |      S t        | j                  |      S rq   )r   r  keysr   r  )rX   r   s     r\   __getattr__z*LocalGeneratorFunctionVariable.__getattr__  s;    4>>**//114&&tww%%r^   c                 6    ddl m} |j                  || ||      S r  )rx  r  r  )rX   r   rx   r   r  s        r\   _build_inline_tracerz3LocalGeneratorFunctionVariable._build_inline_tracer  s$    P,@@	
 	
r^   c                 x   t        | j                  j                               s2t        dt	        | j                  j                               dddg       | j                  |||      }| j                  j                         }| j                  j                         }| j                  |||| j                        S )Nz'non-generator contextlib.contextmanagerzvCannot compile function decorated with `@contextlib.contextmanager` that is not a generator, i.e. does not use `yield`z5Use `yield` in the function body instead of `return`.z2Remove the `@contextlib.contextmanager` decorator.r   r   )	r   r  r   r   r   r  r  r  r   )rX   r   rx   r   rf  rY   r   s          r\   r   z,LocalGeneratorFunctionVariable.call_function  s     DGG,,./ADGG,,./. LH	 11"dFCww!GG'')	 !!;;	 " 
 	
r^   r   )rl   rm   rn   r   re  r6   r]   r  r  r   rP  rQ  s   @r\   r  r    sO     3		+	+&


#
 &
 -	

 

r^   r  c                   ,     e Zd ZdZ fdZ fdZ xZS )3FunctionDecoratedByContextlibContextManagerVariablezm
    .. note::

        This is only used when the function is annotated with @contextlib.contextmanager
    c                 2    t        |   |fdt        i| y )Nr  )r   r]   r  )rX   r  r   r   s      r\   r]   z<FunctionDecoratedByContextlibContextManagerVariable.__init__  s%    	
N	
 	
r^   c                     t         |   |||      }t        |t        j                  j
                  j                        sJ d|_        |S r  )r   r  r   r   r+  symbolic_convert&InliningGeneratorInstructionTranslatoris_generator_from_ctx_manager)rX   r   rx   r   r  r   s        r\   r  zHFunctionDecoratedByContextlibContextManagerVariable._build_inline_tracer  sO    
 -b$?MM**QQ
 	
 
 04,r^   )rl   rm   rn   r   r]   r  rP  rQ  s   @r\   r  r  
  s    
 r^   r  c                   x     e Zd ZdZdd fdZdefdZd Zd Z	 	 	 	 	 	 	 	 d fd	Z	 fd
Z
dddef fdZ xZS )r  z$Some unsupported user-defined methodro   c                     t        |   dd|i| || _        || _        |3|j	                  d      !t        |j	                  d      d      | _        y y y )Nr   r   __func__ra   )r   r]   r[  r  rs   r#   )rX   r   r[  r  r   r   s        r\   r]   zUserMethodVariable.__init__+  s\    )B)&)" H!5!A'

8(<jIDN "Br^   c                 h    | j                   j                   d| j                   d| j                   dS )Nrq  r&  r'  )r   rl   r   r[  r   s    r\   r  zUserMethodVariable.__repr__>  s.    ..))*!DGG9BtxxjBBr^   c                     | j                   gS rq   r[  r   s    r\   r   zUserMethodVariable.self_argsA      zr^   c                 "    t         j                  S rq   )r   
MethodTyper   s    r\   r  zUserMethodVariable.python_typeD  s    r^   r   r<   c                    ddl m}  || j                        rHg | j                         |}t	        j
                  | j                  d      }|j                  |||      S |j                  j                         rt        | j                  t        j                        ryt        | j                  dd      }||j                  d      r|dk7  s| j                  r| j                  j                  || j                  j                   ||| j                  	      S t"        d| j                  t"        j$                  j&                  u r>t	        j(                  | j                        j                  || j                  g||      S | j                  rQt        | j                  j*                  | j                  j                         }t-        ||| j/                         ||      S t0        	|   |||      S )
Nr   )is_nonstrict_trace_callableTr(  rm    z	torch.nn.ztorch.nn.utils.parametrize)constant)trace_rulesr  r   r   r   r7  r   r   is_root_tracerr   r[  NNModuleVariabler   
startswithr   r`  rl   r9   FSDPParamGroupuse_training_stateTorchCtxManagerClassVariabler   r8  r   r   )
rX   r   rx   r   r  	call_argsvarmodule_attrr   r   s
            r\   r   z UserMethodVariable.call_functionG  s    	>&tww/2$..*2T2I88TC $$RF;; 99##%*HHi00+
 "$''<<K '**;7#??##xx++(($AQAQ ,   ),;;NNN99$''BPPTXX%%v  )9)9:B/BvVVw$Rv66r^   c                 &    t         |          dd  S Nr2   )r   r   r   s    r\   r   z*UserMethodVariable.inspect_parameter_names  s    w.044r^   r   c                     |dk(  r| j                   S |dk(  r+t        j                  || j                  | j                        S t
        |   ||      S )Nr	  r  )r[  r6   r   r   r  r   r!  rX   r   r   r   s      r\   r!  zUserMethodVariable.var_getattr  sM    :88O: #((TWWdnnEEw"2t,,r^   rq   rM  r   )rl   rm   rn   r   r]   r   r  r   r  r   r   r!  rP  rQ  s   @r\   r  r  (  sn    .J&C# C ;7#;7 &;7 -	;7
 
;7z5-5 -S - -r^   r  c                   B     e Zd Zd fdZ	 	 	 	 	 	 	 	 d fdZd Z xZS )WrappedUserMethodVariablec                     |j                  dd        |j                  dd        t        |   |j                  |j                  fi | || _        || _        y )Nr   r[  )r~   r   r]   r   r[  wrappedr   rX   r  r   r   r   s       r\   r]   z"WrappedUserMethodVariable.__init__  sI    

4

5$W[[;F;r^   c                     | j                   j                  |       t        |   |||      }| j                   j	                  |       |S rq   r   enterr   r   exitrX   r   rx   r   r   r   s        r\   r   z'WrappedUserMethodVariable.call_function  A     	2&r48"r^   c                      j                   fd         j                         j                  t        dd             y )Nc                  (      j                         S rq   r   r  rX   s   r\   <lambda>z7WrappedUserMethodVariable.reconstruct.<locals>.<lambda>      gdll&; r^   r2   Fadd_push_nullr  extend_outputr   rX   r  s   ``r\   r  z%WrappedUserMethodVariable.reconstruct  4    ;<21e<=r^   rM  r   rl   rm   rn   r]   r   r  rP  rQ  s   @r\   r  r    8    	#	 &	 -		
 
	>r^   r  c                   B     e Zd Zd fdZ	 	 	 	 	 	 	 	 d fdZd Z xZS )WrappedUserFunctionVariablec                 z    |j                  dd        t        |   |j                  fi | || _        || _        y )Nr   )r~   r   r]   r   r  r   r  s       r\   r]   z$WrappedUserFunctionVariable.__init__  s5    

4.v.r^   c                     | j                   j                  |       t        |   |||      }| j                   j	                  |       |S rq   r  r  s        r\   r   z)WrappedUserFunctionVariable.call_function  r  r^   c                      j                   fd         j                         j                  t        dd             y )Nc                  (      j                         S rq   r  r  s   r\   r  z9WrappedUserFunctionVariable.reconstruct.<locals>.<lambda>  r  r^   r2   Fr	  r  s   ``r\   r  z'WrappedUserFunctionVariable.reconstruct  r  r^   rM  r   r  rQ  s   @r\   r  r    s8    	#	 &	 -		
 
	>r^   r  c                     d }|D cg c]
  } ||       }}|j                         D ci c]  \  }}| ||       }}} ||i |}	| j                  j                  |	|t        |            S c c}w c c}}w )Nc                 v    t        | t        j                        r| j                         S | j	                         S rq   )r   r   TensorVariableget_real_valuer   )xs    r\   convertz-invoke_and_store_as_constant.<locals>.convert  s0    a112##%%##%%r^   r   )r   r   register_attr_or_moduler%   )
r   r   r   rx   r   r  r  r   r   ress
             r\   r8  r8    s    &
 !%%1GAJ%D%(.71am7F7
d
f
C99,,d# -   &7s
   A1A6c                        e Zd Zdhej                  Z	 d	 d fdZd Zd Zd Zd Z	ddd	e
d
e
fdZ fdZd Z fdZd Zd Zd ZddZ xZS )r   r   c	                    |	j                  d      |	j                  t                      t        
|   di |	 t        |j                         t              sJ t        |j                         t        j                        sJ t        |t              sJ || _        || _        || _        || _        || _        || _        || _        || _        y )Nmutation_typer   ra   )rs   updater4   r   r]   r   r   r   r   r  rf   rK  rY   r   rc   re   r   r   
wrapped_fn)rX   rK  rY   r   rc   re   r   r   r#  r   r   s             r\   r]   z#NestedUserFunctionVariable.__init__  s     ::o&.MM(<(>M?"6"'446<<<$113U^^DDD)T***	" $&5?r^   c                     g S rq   ra   r   s    r\   r   z$NestedUserFunctionVariable.self_args  r   r^   c                 6    | j                   j                         S rq   )rY   r   r   s    r\   r   z#NestedUserFunctionVariable.get_code  s    yy++--r^   c                 "    t         j                  S rq   r  r   s    r\   r  z&NestedUserFunctionVariable.python_type  r  r^   c                 D   | j                   rt        t        j                  | j                  j                         | j                  | j                  j                               }| j                  r| j                  j                         |_	        | j                  r| j                  j                         |_        | j                  rZ| j                  j                         }t        |t              rddlm} t#         ||            }t        |t"              sJ ||_        |S )Nr   r   )r   r   r   r   rY   r   r   rK  rc   rb   re   rd   r   r   r   r   r   rf   r   )rX   rE   r   r   s       r\   r   z'NestedUserFunctionVariable.get_function  s    <<%%!!II((*NNLL++-

 == $ @ @ BD??"&//"D"D"FD**==?K+u-."8K#89 k4000#.D r^   r   r<   name_varr   c                 z    |j                   j                  j                  | |j                  |       t	        d       S rq   )r   r   
store_attrr   r8   )rX   r   r(  r   s       r\   call_setattrz'NestedUserFunctionVariable.call_setattr  s/     			))$D%%r^   c                 X    |dk(  r | j                   |g| S t        | 	  ||||      S )N__setattr__)r+  r   r`  rX   r   r   rx   r   r   s        r\   r`  z&NestedUserFunctionVariable.call_method   s9    = $4$$R/$//w"2tT6::r^   c                     | j                   d uS rq   )r   r   s    r\   has_closurez&NestedUserFunctionVariable.has_closure%  s    ||4''r^   c                 b    |dk(  r| j                   j                         S t        |   ||      S )Nrl   )rK  r   r   const_getattrr  s      r\   r2  z(NestedUserFunctionVariable.const_getattr(  s0    :<<2244w$R..r^   c                      yrm  ra   r   s    r\   r
  z#NestedUserFunctionVariable.has_self-  rn  r^   c                     | j                   S rq   r  r   s    r\   r  z&NestedUserFunctionVariable.get_globals0  r  r^   c                 H   | j                         }t        j                  || j                  | j                  j                         | j                  rt        | j                  j                        nd t        d t        t        | j                         j                              D                    }| j                  r| j                  j                         |_         t        j                   |      j"                  |i |}|j%                          t'        |j(                  j                               }t+        |j,                  j.                  |       t1        |||       t3        |j                        D ])  \  }}	|	|vsJ | j4                  j                  |   }
|
||	<   + |S )Nc              3   2   K   | ]  }t        d         y wrq   )r1   )r  _s     r\   r  z7NestedUserFunctionVariable.bind_args.<locals>.<genexpr>:  s     Sa)D/Ss   )r   r   r   r   rK  r   rc   r   r   ri   rh   r  re   keys_as_python_constantrd   r   r   r0  apply_defaultsrf   r1  r   r   r  r   rz   r   )rX   r   rx   r   rY   rE   r@   r   r   r   r  s              r\   r  z$NestedUserFunctionVariable.bind_args3  sH   }}!!NNLL++-*.--E$--%%&TS5T]]_5P5P1Q+RSS
 ??"&//"I"I"KD,!!$',,d=f=eoo++-...7ffd+"4#3#34 	 ICv%%%<<%%c*DF4L	 
 r^   c                 ^   j                  fd        | j                         j                  j                  | j                        g        t        j                  | j                  j                  j                               | j                  r | j                         n!j                  j                  d       g       | j                  r | j                         n!j                  j                  d       g       | j                  r | j                         n!j                  j                  d       g       | j                  r=	 | j                  j                         }j                  j                  |      g       n!j                  j                  d       g       j                  t!        dd             | j"                  rvj                  fd        | j"                         j                  t!        dd             j                  t%        d             j                  t!        dd             dd	lm}  |j*                         }|j,                  j.                  j1                  |       r}|j,                  j.                  j2                  |    j5                         D ]H  \  }}j7                           |       j                  t%        d             j9                  |       J y y # t        $ r  | j                         Y uw xY w)
Nc                  0     j                  t        d      S )Nr   )load_import_fromrl   r  s   r\   r  z8NestedUserFunctionVariable.reconstruct.<locals>.<lambda>M  s    G,,X7JK r^      Fc                  (     j                  dd      S )N	functoolswrapsr<  r=  s   r\   r  z8NestedUserFunctionVariable.reconstruct.<locals>.<lambda>q  s    00gF r^   r2   r   Tr   r;   )r
  rY   r  create_load_const_uncheckedr   r8   r}   r   r   rc   create_load_constr   re   r   r   r   r   r#  r   rx  r<   ry  r   r   has_pending_mutationstore_attr_mutationsr   dup_topr*  )rX   r  r   r<   r   r   r   s    `     r\   r  z&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)e !%%l1o6""4() =) ' *(()*s   ;L L,+L,rq   rM  r  )rl   rm   rn   r   rN  r]   r   r   r  r   r6   r+  r`  r0  r2  r
  r  r  r  rP  rQ  s   @r\   r   r     s    	!	0	0N  @ 
@:."0&#& "& 	&;
(/
08)r^   r   c                   B     e Zd Zd fdZ	 	 	 	 	 	 	 	 d fdZd Z xZS )!WrappedNestedUserFunctionVariablec           
         |j                  dd        |j                  dd        |j                  dd        |j                  dd        |j                  dd        |j                  dd        |j                  dd        |j                  dd        t        | 	  |j                  |j                  |j
                  |j                  |j                  |j                  |j                  |j                         || _        || _        y )	NrK  rY   r   rc   re   r   r   r#  )r~   r   r]   rK  rY   r   rc   re   r   r   r#  r  r   r  s       r\   r]   z*WrappedNestedUserFunctionVariable.__init__  s    

9d#

64 

;%

:t$

<&

=$'

9d#

<&OOLLOO		
 r^   c                     | j                   j                  |       t        |   |||      }| j                   j	                  |       |S rq   r  r  s        r\   r   z/WrappedNestedUserFunctionVariable.call_function  r  r^   c                      j                   fd         j                         j                  t        dd             y )Nc                  (      j                         S rq   r  r  s   r\   r  z?WrappedNestedUserFunctionVariable.reconstruct.<locals>.<lambda>  r  r^   r2   Fr	  r  s   ``r\   r  z-WrappedNestedUserFunctionVariable.reconstruct  r  r^   rM  r   r  rQ  s   @r\   rI  rI    s8    ,	#	 &	 -		
 
	>r^   rI  c                        e Zd Zddhej                  Zdd fdZd Zed        Z	 	 	 	 	 	 	 	 ddZ	dd	Z
ddd
efdZ xZS )SkipFunctionVariabler   reasonc                 @    t        |   di | || _        || _        y r`   )r   r]   r   rP  )rX   r   rP  r   r   s       r\   r]   zSkipFunctionVariable.__init__  s!    "6"
r^   c                     | j                   S rq   r   r   s    r\   r   z'SkipFunctionVariable.as_python_constant  s    zzr^   c                 X   t        j                  |dd      rW|}|}t        |dd      r&|j                  }t	        |d      }t        |dd      r&|j                  t        j                         n3t        |      s(t        |j                  t        j                                | ||      S )N_torchdynamo_disableF_torchdynamo_orig_callabler   )
r   r   r   rV  r#   r   r!   r   r/   r"   )r   r   r   guard_on_sourceguard_on_values        r\   r   z'SkipFunctionVariable.create_with_source  s     !!%)?G %O"N.*FN!/!J!J",#%A# .*FN &&|'A'AB07 &++L,F,FGH5((r^   r   r<   c           	      "   t        j                  | j                  dd      rVt        j                  | j                  dd       }t        dt	        | j                        d| j                   d| ddg	       y | j                  t
        j                  j                  u r?|j                  d
d       }|r|j                         }t        dd| d| dd| dg	       y | j                  t
        j                  j                  u r2|j                  d
d       }|r|j                         }t        d|       t        j                  rEddlm} |j!                  || j                        }t#        |t$              s|j'                  |||      S t)        | j                  dd      }	t)        | j                  dd       }
|
dn
t	        |
      }	 t        j*                  | j                        }d|	 d| d}d|	 dg}d|vr|d|	 dd gz  }|	d/k(  rd0}g }| j6                  r| j6                  nd1}t        d2d3| d4|	 d5| ||	       y # t,        $ r d!d"h}|
|v rd#| d$|	 d}d%d&g}n|
\|
j/                  d'      rKd(| d$|	 d$}d)g}t
        j                  j0                  j3                  |d*z   d*j5                  |      z          nKd+| d$|	 d,}d-d.g}t
        j                  j0                  j3                  |d*z   d*j5                  |      z          Y w xY w)6NrU  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   rJ  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   SourcelessBuilderrn   z<unknown qualname>rm   z<unknown module>z?Dynamo developers have intentionally marked that the function `z` in file `z` should not be traced.zAvoid calling the function `z`.r+  z:Apply `@torch._dynamo.dont_skip_tracing` to the function `zw` to force tracing into the function. More graph breaks may occur as a result of attempting to trace into the function.z Please file an issue to PyTorch._abc	_warningsz6Dynamo does not know how to trace the Python builtin `.zIf you are attempting to call a logging function (e.g. `_warnings.warn`), you can try adding it to `torch._dynamo.config.reorderable_logging_functions`.zKPlease file an issue on GitHub so the PyTorch team can add support for it. 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   r+  graph_breakrs   r   
skip_framer   r   dont_skip_tracingbuilderr]  r}   r   rO  r   r   getfiler|   r  r;  	warn_oncejoinrP  )rX   r   rx   r   rJ  graph_break_msgskip_frame_msgr]  
rebuilt_fnqualname	module_ormodule_namepathr   r   known_python_builtin_modulesrP  s                    r\   r   z"SkipFunctionVariable.call_function  s    !!$**.De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%33BfEEtzz>;OPH

L$?I090A,s9~K8Ytzz2UV^U_ `  $v%<> 
 38*B? D(TU]T^ _l l ;	 EX ++? 
 $(KKT[[5GFF";-|H:_U[T\]'	]  %Y06/D, <<'=(27  
iGE *y/C/CH/M$N{m[\]e\ffg"hK^E
 MM''11+2DtyyQVGW2WX J+VWX`Wa ba b  m;E MM''11+2DtyyQVGW2WXK%Ys   >I CLLc                 h    t         j                  j                  t        | j                  |            S rq   )r   r8   r}   r   r   r  s      r\   r   z%SkipFunctionVariable.call_obj_hasattrN  s$    ))00T1JKKr^   r   c                     |t         v rt        j                  | |      S t        || j                  | j
                  |      S rq   )r+   r   r   r   r   r   r  s      r\   r!  z SkipFunctionVariable.var_getattrQ  s7    )),,T488b$**dkk4@@r^   rq   rM  r   r   r<   )rl   rm   rn   r6   rN  r]   r   rO  r   r   r   r   r!  rP  rQ  s   @r\   rO  rO    s     
	'	'N
 ) )0v#v &v -	v
 
vpLA5 AS Ar^   rO  c                   B     e Zd Zd fdZ	 	 	 	 	 	 	 	 d fdZd Z xZS )WrappedSkipFunctionVariablec                     |j                  dd        |j                  dd        t        |   |j                  fd|j                  i| || _        || _        y )Nr   rP  )r~   r   r]   r   rP  r  r   r  s       r\   r]   z$WrappedSkipFunctionVariable.__init__Y  sL    

7D!

8T"Hw~~HHr^   c                     | j                   j                  |       t        |   |||      }| j                   j	                  |       |S rq   r  r  s        r\   r   z)WrappedSkipFunctionVariable.call_function`  r  r^   c                      j                   fd         j                         j                  t        dd             y )Nc                  (      j                         S rq   r  r  s   r\   r  z9WrappedSkipFunctionVariable.reconstruct.<locals>.<lambda>l  r  r^   r2   Fr	  r  s   ``r\   r  z'WrappedSkipFunctionVariable.reconstructk  r  r^   rM  r   r  rQ  s   @r\   rw  rw  X  r  r^   rw  c                   N     e Zd ZdZd fdZd fdZd Z	 	 	 	 	 	 	 	 ddZ xZS )	WrapperUserFunctionVariablea3  
    Used to represent a wrapper object that contains the actual callable as an
    attribute. For example, torch.jit.script/trace have the original function at
    their _torchdynamo_inline attribute. Similarly, functions with
    __script_if_tracing_wrapper have the original attr at "__original_fn".
    c                 @    t        |   di | || _        || _        y r`   )r   r]   wrapper_objattr_to_trace)rX   r  r  r   r   s       r\   r]   z$WrapperUserFunctionVariable.__init__y  s#    "6"&*r^   c                     || j                   k(  r[t        | j                  | j                         }| j                  xr t	        | j                  |      }t        j                  |||      S t        | !  ||      S rq   )	r  r   r  r   r#   r6   r   r   r!  )rX   r   r   r   r   r   s        r\   r!  z'WrapperUserFunctionVariable.var_getattr~  si    4%%%$**D,>,>?C[[BZT%BF"((S&99w"2t,,r^   c                     g S rq   ra   r   s    r\   r   z%WrapperUserFunctionVariable.self_args  r   r^   c                    t        | j                  d      rnt        | j                  | j                  d       }t        |dd      xs d}|j	                  dd      d   dk7  r"d	}t
        j                  j                  j                  |       t
        j                  j                  j                  j                  d
      }|j                  t        j                        rt
        j                  j                  j                         }t!               |z   }|d   j"                  |d   j$                  f}	dj'                  t)        j*                  |            }
d|	d    d|	d    d}|t-        |
      z  }|j/                  |       | j1                         |z   }t3        j4                  t6        j8                        j;                  || t3        j<                  | j                        g||      S )N
cache_inform   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: :rb  )r   r  r   r  splitr   r+  r;  ri  logging	getLoggerisEnabledForDEBUG_guardsTracingContextextract_stackr   filenamelinenorj  	tracebackformat_listr   debugr   r   r   r   getattr_and_tracer   r8   )rX   r   rx   r   	target_fnrp  rJ  dynamo_logger
user_stack	frame_locuser_stack_formatteduser_stack_traceall_argss                r\   r   z)WrapperUserFunctionVariable.call_function  s    4##\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^   rM  ru  r   )	rl   rm   rn   r   r]   r!  r   r   rP  rQ  s   @r\   r}  r}  q  sB    +
-&
#&
 &&
 -	&

 
&
r^   r}  c                   *     e Zd ZdZd fdZd Z xZS )WrapperUserMethodVariablez
    Similar to WrapperUserFunctionVariable, but for methods. The only delta is
    saving the vt for `self` object of the method which is then used by
    WrapperUserFunctionVariable in `call_function` method.
    c                 6    t        |   ||fi | || _        y rq   )r   r]   r[  )rX   r  r  self_objr   r   s        r\   r]   z"WrapperUserMethodVariable.__init__  s    m>v>r^   c                     | j                   gS rq   r  r   s    r\   r   z#WrapperUserMethodVariable.self_args  r  r^   rM  )rl   rm   rn   r   r]   r   rP  rQ  s   @r\   r  r    s    r^   r  c                  R    t         j                  j                         rddlm}  | S i S )Nr   traceable_collective_remaps)r   distributedis_available)torch.distributed._functional_collectivesr  r  s    r\   _traceable_collective_remapsr    s&    %%'	
 +*Ir^   c                     t         j                  j                         sJ d       |t               j	                         v sJ |j
                  }| j                  d      }t        ||      S )NzIllegal invocation.z)torch.distributed._functional_collectives)r   r  r  r  valuesrl   import_sourcer#   )r   r   
inner_namepath_sources       r\   _traceable_collectives_sourcer    sb    ))+B-BB+-/668888J""#NOKk:..r^   c                   p     e Zd ZdZd fdZedd       Zed        Zedd       Z	 	 	 	 	 	 	 	 d	dZ	 xZ
S )
!CollectiveFunctionRewriteVariablea  
    Some of the torch.distributed.* collective APIs are possible to rewrite to 'traceable' collectives.

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

    In the case that a function is 'remappable' but only for some combinations of call-time arguments,
    we check the args at `call_function` time and fall back to graph-breaking if needed.  This is no worse
    than status-quo as we currently graph-break on all distributed.* collectives.
    c                X    t        |   |fi | t        |t              sJ || _        y rq   )r   r]   r   r   replacement_var)rX   r   r  r   r   s       r\   r]   z*CollectiveFunctionRewriteVariable.__init__  s.    &v&/+?@@@.r^   c                 j    t         j                  | |      \  }}t        |ft        |fd|i||d|S )Nr   )r  r   )r  rewriter   )r   old_fnr   optionsnew_fn
new_sources         r\   r}   z(CollectiveFunctionRewriteVariable.create  sM    >FFr6R
0
0V
VgV
 	
 	
r^   c                 H    t        j                  |       xr | t               v S rq   )r   
isfunctionr  )variables    r\   can_rewritez-CollectiveFunctionRewriteVariable.can_rewrite  s%     x(WX9U9W-W	
r^   c                 8    t               |   }|t        | |      fS rq   )r  r  )r   r   r  s      r\   r  z)CollectiveFunctionRewriteVariable.rewrite  s"    -/34R@@@r^   c                    dd l m} ddlm} t	        j
                  | j                        }t         |j                  |i |j                        }d}d|v rQ|d   j                         r>t        d| j                   d|d|d| j                   g t        j                  	       | j                  |j                  |j                  |j                   fv ro|j#                  d
      }||j$                  n|j&                  d
   j(                  }||vrt+        d|       t,        j.                  j1                  ||         |d
<   | j2                  j5                  |||      S )Nr   )REDUCE_OP_TO_STRra   async_opz)async_op=True for distributed collectivesz, args=	, kwargs=z3`torch.compile` doesn't support `async_op=True for r   opzUnsupported all_reduce op: )torch.distributedr  r  r  r   r   r   rf   r0  r1  r   r   r   SUPPORTABLE
all_reducereduce_scatter_tensor_reduce_scatter_basers   r   r   defaultr  r   r8   r}   r  r   )	rX   r   rx   r   distr  r   reduce_op_var	reduce_ops	            r\   r   z/CollectiveFunctionRewriteVariable.call_function  sj    	)N %%dgg.	ninnd5f5??@F:$6$I$I$KC77)8dWJvi8QRVRYRYQZ[&22	 77OO&&%%
 

 #JJt,M !, ##))$/77 
  00 #>yk!JKK$55<< +F4L ##11"dFCCr^   rM  ru  r   )rl   rm   rn   r   r]   rc  r}   r  r  r   rP  rQ  s   @r\   r  r    s{    /
 
 
 
 

 A A,D#,D &,D -	,D
 
,Dr^   r  c                   0     e Zd Z	 	 	 	 	 	 	 	 d fdZ xZS )FunctoolsWrapsVariablec                 |    |s(t              dk(  rfd}t        j                  |      S t        |   ||      S )Nr2   c                     t        | t        j                        r| j                  d         S t	        d|  dg t
        j                         y )Nr   )r#  zfunctools.wrapsz]`torch.compile` can't trace `functools.wraps` on functions defined outside the compile regionr   )r   r   r   cloner   r   r  )r   rx   s    r\   rA  z3FunctoolsWrapsVariable.call_function.<locals>.wraps2  sN    b)"F"FG88tAw877 -!d *66	r^   )rh   r   LambdaVariabler   r   )rX   r   rx   r   rA  r   s     `  r\   r   z$FunctoolsWrapsVariable.call_function*  s>     #d)q.
 ++E22w$Rv66r^   r   )rl   rm   rn   r   rP  rQ  s   @r\   r  r  )  s3    7#7 &7 -	7
 
7 7r^   r  c                   *    e Zd Zd Z	 	 	 	 	 	 	 	 ddZy)CollectionsNamedTupleFunctionc                     | j                   S rq   r   r   s    r\   r   z0CollectionsNamedTupleFunction.as_python_constantD  r  r^   c                 ,   t        ||      }|r~	  | j                  |D cg c]  }|j                          c}i |j                         D ci c]  \  }}||j                          c}}}t        j                  t                     S t        dd|d|dg t         j"                         y c c}w c c}}w # t        $ rL}	t        t        |	      |t        t        t        j                  |	j                                     Y d }	~	d }	~	ww xY w)Nrw   r!  znamedtuple constructionzargs=r  z?`torch.compile` only support certain input types for namedtupler   )r)   r   r   r   r|   r   rY  r   mapr8   r}   rx   r   UserDefinedClassVariabler5   r   r   r  )
rX   r   rx   r   constant_argsr  r   r   r   r  s
             r\   r   z+CollectionsNamedTupleFunction.call_functionG  s    ,D&9
6:;a**,;=C\\^LTQq!..00L 55%5%7  	-tgZy)Y"..		
 <L (Ic"2"9"9388DE s4   B> B3B> B8(B> 3B> >	DADDNr   )rl   rm   rn   r   r   ra   r^   r\   r  r  C  s2    
#
 &
 -	

 

r^   r  c                        e Zd Zdeddf fdZd ZddZd Z	 	 	 	 	 	 	 	 dd
Zdd	de	defdZ
dd	de	fdZd Zd Z xZS )FunctoolsPartialVariablerE   ro   Nc                     t        |   di | || _        t        |t              sJ || _        t        |t              sJ || _        t        j                  t              | _        y r`   )r   r]   rE   r   r   rx   rf   keywordsr@  partialr-   
fake_value)rX   rE   rx   r  r   r   s        r\   r]   z!FunctoolsPartialVariable.__init__h  s\    "6"	$%%%	(D)))  $++H5r^   c                 "    t         j                  S rq   )r@  r  r   s    r\   r  z$FunctoolsPartialVariable.python_types  s       r^   c                 <   j                  fd        | j                         | j                  rj                  | j                         | j                  s2j                  t        t        | j                        dz   d             y j                  | j                  j                                t        | j                  j                               }j                  j                  t        |      t        | j                        z   dz   |d             y )Nc                  (     j                  dd      S )Nr@  r  rB  r=  s   r\   r  z6FunctoolsPartialVariable.reconstruct.<locals>.<lambda>w  s    g&>&>{I&V r^   r2   F)r
  rE   rx   foreachr  r  r   rh   r  r   r  create_call_function_kw)rX   r  r  s    ` r\   r  z$FunctoolsPartialVariable.reconstructv  s    VW		99OODII&}}!!"6s499~7I5"QR,,./T]]'')*++CIDII,F,JDRWX	
r^   c                 "    | j                         S rq   r   r   s    r\   r   z%FunctoolsPartialVariable.get_function      &&((r^   r   r<   c                 z    | j                   |z   }i | j                  |}| j                  j                  |||      S rq   )rx   r  rE   r   )rX   r   rx   r   merged_argsmerged_kwargss         r\   r   z&FunctoolsPartialVariable.call_function  s>     ii$&34==3F3yy&&r;FFr^   r   c                     t         j                  j                  t        t	        j
                  t              |            S rq   )r   r8   r}   r   r@  r  r-   r  s      r\   r   z)FunctoolsPartialVariable.call_obj_hasattr  s2     ))00I%%h/6
 	
r^   c                    | j                   xr t        | j                   |      }|dk(  r| j                  S |dk(  r!t        j                  | j
                  |      S |dk(  rW| j                  j                         D ci c]  \  }}t        j                  |      | }}}t        j                  ||      S |t        v rt        j                  | |      S t        t        |       y c c}}w )NrE   rx   r   r  )r   r#   rE   r   ListVariablerx   r  r   r8   r}   ConstDictVariabler+   r   r   r   )rX   r   r   r   r   r   r   s          r\   r!  z$FunctoolsPartialVariable.var_getattr  s    >DKK!>6>996>))$))FCC:?C}}?R?R?TUtq!%,,Q/2UEU..uVDD)),,T488 4	 Vs   > C'c                 :   t        j                  | j                  j                         g| j                  D cg c]  }|j                          c}i | j
                  j                         D ci c]  \  }}||j                          c}}S c c}w c c}}w rq   )r@  r  rE   r   rx   r  r   )rX   rC  r   r   s       r\   r   z+FunctoolsPartialVariable.as_python_constant  s      II((*
26))<3c$$&<
 6:]]5H5H5JKTQq!&&((K
 	
<K   B
1Bc                 :   t        j                  | j                  j                         g| j                  D cg c]  }|j                          c}i | j
                  j                         D ci c]  \  }}||j                          c}}S c c}w c c}}w )zcSimilar to as_python_constant(), but add ID_MATCH guards to try to force things to become constants)r@  r  rE   guard_as_python_constantrx   r  r   )rX   r   r   s      r\   r  z1FunctoolsPartialVariable.guard_as_python_constant  s      II..0
48II>qa((*>
 <@==;N;N;PQ41aq!,,..Q
 	
>Qr  r  r   )rl   rm   rn   r6   r]   r  r  r   r   r   r   r!  r   r  rP  rQ  s   @r\   r  r  g  s    	6_ 	64 	6!
)G#G &G -	G
 
G
)
14
	
55 5S 5

r^   r  c                   
    e Zd Zdddhej                  Zeej                  dee	de
f   ej                  f   fd              Zed        Zdeddf fd	Zedefd
       Zd Zd Z	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZd Z xZS )PolyfilledFunctionVariabler   r#  traceable_fnro   .c                     i S rq   ra   )r   s    r\   _get_polyfill_handlersz1PolyfilledFunctionVariable._get_polyfill_handlers  s	     	r^   c                 f    t        |j                  t        j                                | ||      S r   )r"   r   r!   FUNCTION_MATCHr   s      r\   r   z-PolyfilledFunctionVariable.create_with_source  s(    f''(C(CDE5((r^   Nc                 *   t        |   di | || _        | j                         j	                  ||      }t        |      sJ d| d|        dD ]"  }t        ||d       }|st        |      sJ |} n t        d| d      || _        || _	        y )NzPolyfill handler z is not callable for )__torch_dynamo_polyfill____python_implementation__z# does not have a traceable functionra   )
r   r]   r   r  rs   callabler   r/  r#  r  )rX   r   r   handlercandidate_attr	candidater  r   s          r\   r]   z#PolyfilledFunctionVariable.__init__  s    "6"--/33B; X$5gY>STVSW"XX 
 	N  >I	***(	 #G9,OP  & ,r^   c                     | j                   S rq   )r  r   s    r\   polyfill_fnz&PolyfilledFunctionVariable.polyfill_fn  s       r^   c                 0    t        | j                  dd      S )N*__torch_dynamo_can_constant_fold_through__F)r   r#  r   s    r\   can_constant_fold_throughz4PolyfilledFunctionVariable.can_constant_fold_through  s    OOI5
 	
r^   c                 "    | j                         S rq   r  r   s    r\   r   z'PolyfilledFunctionVariable.get_function  r  r^   c                 .   | j                         rt        ||      rt | j                  |D cg c]  }|j                          c}i |j	                         D ci c]  \  }}||j                          c}}}t        j                  ||      S | j                  t        j                  u r&t        |      dk(  r|st        |d   t        j                  t        j                  f      rt        d |d   j                  D              rt        j                  j!                  ||j"                  j%                  dt&        j(                  t+        d |d   j                  D              fi       t'        j(                  |d   j                  D cg c]4  }t        |t        j,                        r|j.                  n|j0                  6 c}            S t        j                  || j2                        }|j5                  |||      S c c}w c c}}w c c}w )Nr2   r   c              3      K   | ]n  }t        |t        j                        xr t        |j                  t              xs2 t        |t        j
                        xr |j                         t        u  p y wrq   )r   r   r8   r   intSymNodeVariabler  )r  r  s     r\   r  z;PolyfilledFunctionVariable.call_function.<locals>.<genexpr>  se        Ay99:Wz!''SV?W Yq)";";<WTWAWYs   A4A6r   c              3   <   K   | ]  }|j                           y wrq   )as_proxy)r  as     r\   r  z;PolyfilledFunctionVariable.call_function.<locals>.<genexpr>  s     ?A1::<?s   )sym_num)r  r*   r   r   r   r6   r   r  sumrh   r   r   r  TupleVariabler  r	  r}   r   create_proxyr   sym_sumr   r8   r   r  r  r   )	rX   r   rx   r   r  r   r   r   traceable_function_variables	            r\   r   z(PolyfilledFunctionVariable.call_function  s    ))+0M&1
 6:;a**,;=C\\^LTQq!..00L  #((V44 GGx||#D	Q47Y%;%;Y=T=T$UV  a  ,,33		&&#MM?a??A	  "&a   *!Y-G-GH GG!"+	 4  ( '6&;&;B@Q@Q&R#*88T6JJO <L4s   HH9Hc                 b   |dk(  r| j                  |||      S t        | j                  |d       }|J d| d| j                          t        |      sJ d| d| j                          i }| j                  rt        | j                  |      |d<   t        |fi |}|j                  |||      S )N__call__zMember z not found in z is not callable in r   )r   r   r   r.   r   r#   r  )rX   r   r   rx   r   methodr  polyfilled_method_variables           r\   r`  z&PolyfilledFunctionVariable.call_method"  s     :%%b$77$-!JWTF.	#JJ!6"QgdV3Gy$QQ";; *4;; =GH%?%R'%R")77D&IIr^   c                     | j                   S rq   r   r   s    r\   r   z-PolyfilledFunctionVariable.as_python_constant5  r  r^   r   rx   r   r   r   ro   r6   )rl   rm   rn   r6   rN  rO  r@  cacherf   r   r   r   r   r  r   r?   r]   propertyr  r  r   r   r`  r   rP  rQ  s   @r\   r  r    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^   r  c                       e Zd Zd Zy)TracebackVariablec                      y rq   ra   r   s       r\   r   zTracebackVariable.call_function;  s    r^   Nrl   rm   rn   r   ra   r^   r\   r  r  9  s    2r^   r  c                   0     e Zd Z fdZd Zd Zd Z xZS )SysFunctionVariablec                 2    t        |   di | || _        y r`   )r   r]   r   )rX   r   r   r   s      r\   r]   zSysFunctionVariable.__init__?  s    "6"
r^   c                 h   t        |j                        rK|j                  d   }|j                  }d }t        j                  ||      |t        j                  ||      g}n>t        j                  d       t        j                  d       t        j                  d       g}t        j                  |      S )Nr  )rh   exn_vt_stackr  r6   r   r   r8   r  )rX   r   exnrI  tbr   s         r\   exc_infozSysFunctionVariable.exc_infoC  s    r//"%C,,CB%%b#.%%b"-E **40**40**40E
 &&u--r^   c                 >    | j                  |      j                  d   S r  )r&  r   r   s     r\   	exceptionzSysFunctionVariable.exceptionU  s    }}R &&q))r^   c                     | j                   t        j                  u r| j                  |      S | j                   t        j                  u sJ | j                  |      S rq   )r   r  r&  r(  r   s       r\   r   z!SysFunctionVariable.call_functionX  sF    ::%==$$zzS]]***~~b!!r^   )rl   rm   rn   r]   r&  r(  r   rP  rQ  s   @r\   r   r   >  s    .$*"r^   r   ) create_tma_experimental_metadatacreate_tma_stable_metadataTMADescriptorMetadataTritonHOPifierc                       e Zd ZdedefdZdedefdZdedefdZ	de
ej                  j                  j                  df   fd	Zd
 Zd Zd Zd ZdedefdZdddee   ddfdZdefdZy)DynamoTritonHOPifierrJ  ro   c                     t        |      rq   )r    )rX   rJ  s     r\   raise_unsupportedz&DynamoTritonHOPifier.raise_unsupportedh  s    #r^   maybe_callablec                 .    t        |t        t        f      S rq   )r   r   r   )rX   r2  s     r\   is_callablez DynamoTritonHOPifier.is_callablek  s    79MN
 	
r^   r   c                     |j                   S rq   rS  )rX   r   s     r\   	get_valuezDynamoTritonHOPifier.get_valuep  s    yyr^   .c                     ddl m} t        ||      r|j                         S t	        ddt        |       dg t        j                         y )Nr2   )BaseListVariablez/unsupported grid type for triton hop check_gridzgrid type = z;`torch.compile` only supports list-like grid for check_gridr   )listsr8  r   r  r   rY  r   r  )rX   gridr8  s      r\   
check_gridzDynamoTritonHOPifier.check_grids  sK    +d,-==?"I&tDzl3Y&22	r^   c                     |j                         D ci c]%  \  }}t        j                  j                  |      |' }}}|j	                  ||gi       }|S c c}}w rq   )r   r   r8   r}   r   )rX   r:  metar   r   r   s         r\   	call_gridzDynamoTritonHOPifier.call_grid  sV    DHJJLQDAq	**11!4a7QQ!!"tfb1 Rs   *Ac                 \    ddl m} |j                  ||      }|j                  |||      }|S )Nr2   r\  )rg  r]  r}   r   )	rX   user_fnrx   r   r   r  r]  wrapped_user_functionr   s	            r\   call_user_defined_fnz)DynamoTritonHOPifier.call_user_defined_fn  s1    . 1 8 8W E&44RvFr^   c                 l    ddl m}  ||t        |j                  |             j	                  |      }|S )Nr2   )VariableBuilder)rg  rD  r#   kernel_source_wrap)rX   user_objr   r  r   rD  wrapped_user_objs          r\   wrap_user_defined_objz*DynamoTritonHOPifier.wrap_user_defined_obj  s7    ,*
811dV=

%/ 	  r^   c                 n    |j                  |      }|D cg c]  } |j                          }}|S c c}w rq   )unpack_var_sequencer  )rX   configsr   r   s       r\   maybe_unpack_configsz)DynamoTritonHOPifier.maybe_unpack_configs  s@    --b1 DKK26224KK Ls   2r   c                 d    |j                         s| j                  d       |j                         S )Nz`@triton.heuristics must return constant values because configs can only contain constant values.)is_python_constantr1  r  )rX   r   s     r\   maybe_unpack_heuristic_resultz2DynamoTritonHOPifier.maybe_unpack_heuristic_result  s0    ((*""r ..00r^   r  TritonKernelVariablerx   c                     |j                   t        |      dk7  r| j                  d        t        |      |j                  |j
                  |d   |j                        S )Nr2   z7Triton kernels should be called with only a single gridr   )kernel
kernel_idxr:  rE  )r:  rh   r1  rY  rS  rT  r   )rX   r  rx   s      r\   call_getitemz!DynamoTritonHOPifier.call_getitem  s\     ==$D	Q""I tH~??**a"//	
 	
r^   c                 $   ddl m} ddlm} i }t	        |j                               D ]D  }||   }	t        |	t        t        f      s|	j                         ||<   |	j                         ||<   F |j                         D 	ci c]%  \  }}	t        j                  j                  |      |	' }
}}	ddlm}m} |j                         D 	ci c]"  \  }}	t        |	|      r||	j#                         $ }}}	|
j                         D 	ci c]  \  }}	t        |	|      s||	 }}}	|j%                         D ][  }	|	j'                         }	t        |	t        j(                  t        j*                  f      r>| j-                  dt/        |	       d       ] |j1                  |      } ||t2              }|j4                  j7                  d|d	|j8                  ||||j;                         d
       t        j                  d       S c c}	}w c c}	}w c c}	}w )Nr2   r7   )r  r   )kernel_side_tabletriton_kernel_wrapper_mutationz.Unexpected argument type for a Triton kernel: r`  r   ra   )rT  constant_args_idxr:  tma_descriptor_metadatar   )r  r8   dictsr  r   r  r   !TMADescriptorExperimentalVariableTMADescriptorStableVariableto_metadata
get_tensorr   r   r}   *torch._higher_order_ops.triton_kernel_wraprW  rX  r   r  realizer  r	  r1  repradd_constant_argsrf   r   r  rT  r  )rX   r  gridscombined_args_rawr   r8   r  rZ  r   r   combined_argsrW  rX  r  non_constant_argsrY  r=  s                    r\   call_HOPzDynamoTritonHOPifier.call_HOP  s   ., :<',,./ 	6A!!$A57RS ./]]_'*'(||~!!$	6 *//1
1 &&--a0!3
 

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


s   *H 
'HHN)rl   rm   rn   r   r   r1  r   r2  r4  r6  r   r   fxproxyProxyr;  r>  rB  rI  rM  rP  r   rU  r8   rh  ra   r^   r\   r/  r/  g  s    S U 
# 
$ 

S S %(<(<c(A"B  1C 1C 1
(
 sm
 
 	
$C
BR C
r^   r/  c                        e Zd ZU ded<   ded<   ee   ed<   ded<   d fd	Z	 	 	 	 	 	 	 	 dd
Z	 	 	 	 	 	 d fdZde	de	fdZ
 xZS )rQ  r=   r:  r>   rS  rT  r#   rE  ro   c                     |j                  dd       | _        t        |   di | t        j                  | |||       y )NrE  ra   )r~   rE  r   r]    dynamo_triton_hopifier_singletoninit_variable)rX   rS  rT  r:  r   r   s        r\   r]   zTritonKernelVariable.__init__	  s;    #ZZ>"6"(66tVZQUVr^   c                 2    t         j                  | |||      S rq   )rn  call_triton_kernelr   s       r\   r   z"TritonKernelVariable.call_function	  s      0BB$
 	
r^   c                     |dk(  rt         j                  | |      S |dk(  rt         j                  | |||      S t        |   ||||      S )N__getitem__run)rn  rU  call_runr   r`  r.  s        r\   r`  z TritonKernelVariable.call_method	  sW     = 3@@tLLU]3<<T4QSTT w"2tT6::r^   rC  c                 v    ddl m} ddlm} t	        ||      r  |j
                  |j                               S |S )Nr2   r7   )r	  )r  r8   tensorr	  r   r}   evaluate_expr)rX   rC  r8   r	  s       r\   specialize_symbolicz(TritonKernelVariable.specialize_symbolic)	  s6    .+ c?+*#**3+<+<+>??
r^   rM  r   r  )rl   rm   rn   r   r   r  r]   r   r`  r   ry  rP  rQ  s   @r\   rQ  rQ  	  s    
W

#
 &
 -	

 

; &	;
 -; 
;s s r^   rQ  c                   D     e Zd Z	 	 	 	 	 	 	 	 d fdZd ZddZd Z xZS )r\  c                     t        |t        j                        sJ t        |   di | || _        || _        || _        || _        y r`   )	r   r   DataPtrVariabler   r]   data_ptrdims
block_dimselement_size)rX   r}  r~  r  r  r   r   s         r\   r]   z*TMADescriptorExperimentalVariable.__init__4	  sH     (I$=$=>>>"6" 	$(r^   c                     t        | j                  D cg c]  }|j                          c}| j                  D cg c]  }|j                          c}| j                  j                               S c c}w c c}w rq   )r*  r~  r  r  r  )rX   dims     r\   r^  z-TMADescriptorExperimentalVariable.to_metadataC	  sW    /'+yy1S\\^1'+7S\\^7&&(
 	
17s
   A2A7
c                     j                   fd        j                  j                         g  j                   j                   j
                  }j                  |       j                  t        |      dz   d       y )Nc                  V     j                  ddt        j                         d      S )Nz$triton.tools.experimental_descriptorcreate_d_tma_descriptor)r<  rh   r~  r  s   r\   r  z?TMADescriptorExperimentalVariable.reconstruct.<locals>.<lambda>L	  s+    G,,6#dii.))9: r^   r2   F)	r
  r}  r  r~  r  r  r  r   rh   )rX   r  rx   s   `` r\   r  z-TMADescriptorExperimentalVariable.reconstructJ	  sp    	
 	!!'*@@T__@d.?.?@c$i!mU3r^   c                 .    | j                   j                  S rq   )r}  from_tensorr   s    r\   r_  z,TMADescriptorExperimentalVariable.get_tensorV	  s    }}(((r^   )r}  zvariables.DataPtrVariabler~  list[ConstantVariable]r  r  r  r8   r  rl   rm   rn   r]   r^  r  r_  rP  rQ  s   @r\   r\  r\  3	  s=    )-) ') -	)
 ))

4)r^   r\  c                   >     e Zd Z	 	 	 	 d fdZd ZddZddZ xZS )r]  c                 x    t        |t        j                        sJ t        |   di | || _        || _        y r`   )r   r   r  r   r]   rw  block_shape)rX   rw  r  r   r   s       r\   r]   z$TMADescriptorStableVariable.__init__[	  s:     &)":":;;;"6"&r^   c                 H    t        | j                  j                               S rq   )r+  r  r  r   s    r\   r^  z'TMADescriptorStableVariable.to_metadataf	  s!    )%%'
 	
r^   c                     j                  fd       j                  d       | j                  j                          | j                         j                  d       y )Nc                  (     j                  dd      S )Nztriton.tools.tensor_descriptorTensorDescriptorrB  r=  s   r\   r  z9TMADescriptorStableVariable.reconstruct.<locals>.<lambda>m	  s    G,,0" r^   r  r   )r
  load_methodrw  r  r  r`  r  s    `r\   r  z'TMADescriptorStableVariable.reconstructk	  sT    	
 	M*(  !Ar^   c                     | j                   S rq   )rw  r   s    r\   r_  z&TMADescriptorStableVariable.get_tensorw	  s    {{r^   )rw  variables.TensorVariabler  zvariables.ListVariabler  )ro   r  r  rQ  s   @r\   r]  r]  Z	  s)    	'*	' .	'


r^   r]  c                   @     e Zd Zdeddf fdZ	 	 	 	 	 	 	 	 ddZ xZS )'CreateTMADescriptorExperimentalVariablerankro   Nc                 >    |dv sJ t        |   di | || _        y )N)r2   r   ra   )r   r]   r  )rX   r  r   r   s      r\   r]   z0CreateTMADescriptorExperimentalVariable.__init__|	  s(    
 v~~"6"	r^   c                    d|v r|d   n|d   }t        |t        j                        st        d| j                   d      | j                  dk(  r;t        |      t        |      z   dk(  sJ d|v r|d   n|d   g}d|v r|d   n|d	   g}nTt        |      t        |      z   d
k(  sJ d|v r|d   n|d   d|v r|d   n|d	   g}d|v r|d   n|d   d|v r|d   n|d   g}d|v r|d   n|d   }t        ||||      S )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  rh   r\  )rX   r   rx   r   r  r~  r  r  s           r\   r   z5CreateTMADescriptorExperimentalVariable.call_function	  sa     %fUmDG#y88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   )rl   rm   rn   r  r]   r   rP  rQ  s   @r\   r  r  {	  sE     
	(
#(
 &(
 -	(

 
(
r^   r  c                   $    e Zd Z	 	 	 	 	 	 	 	 ddZy)!CreateTMADescriptorStableVariablec                 T    d|v r|d   n|d   }d|v r|d   n|d   }t        ||      S )Nrw  r   r  r2   )rw  r  )r]  )rX   r   rx   r   rw  r  s         r\   r   z/CreateTMADescriptorStableVariable.call_function	  sF     &.%7!T!W/</Ff]+DQRG*#
 	
r^   Nr   r  ra   r^   r\   r  r  	  s-    
#
 &
 -	

 

r^   r  rq   ru  )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(   r;  r)   r*   r+   r,   r-   r.   r/   r0   r1   baser3   r4   r5   r6   r  r8   #torch.distributed.fsdp._fully_shardr9   ModuleNotFoundErrortorch._dynamo.codegenr:   rx  r<   r`  r=   r>   r?   rT   rV   rr   rD   ru   r   r{   r   rf   r   r   r   r   r   r   r   rS  re  r  r  r  r  r  r  r8  r   rI  rO  rw  r}  r  r  r  r  r  r  r  r  r  r   r*  r+  r,  r-  r/  rn  rQ  r\  r]  r  r  ra   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