
    %Vjio                       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 n# eL$ r dZKY nw xY w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! ZZdidjd$Z[djd%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/ d0ec          Ze G d1 d2eG          Zf G d3 d4ef          Zg G d5 d6ec          Zh G d7 d8eh          Zi G d9 d:ed          Zj G d; d<ej          Zk G d= d>ed          Zldjd?Zm G d@ dAec          Zn G dB dCen          Zo G dD dEeG          Zp G dF dGep          Zq G dH dIeG          Zr G dJ dKer          ZsdL ZtdjdMZu G dN dOed          Zv G dP dQed          Zw G dR dSed          Zx G dT dUeG          Zy G dV dWeG          Zz G dX dYeG          Z{ G dZ d[eG          Z|dd\lQm}Z}m~Z~mZmZ  G d] d^e          Z e            Z G d_ d`eG          Z G da dbeG          Z G dc ddeG          Z G de dfeG          Z G dg dheG          ZdS )ka  
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dS )FunctionSpecfuncc                    |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||         nd | _        d S )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        [/root/voice-cloning/.venv/lib/python3.11/site-packages/torch/_dynamo/variables/functions.py__init__zFunctionSpec.__init__p   s    }!4) 2 4$"4 45!$"4t~"EF!/$2FFt~AR0RRSnt00'+}z'AKBsGGtD%,qq1,%)]^%CM"S''    c           
          |j         pd| _        |j        pi | _        t	          t          | j        t          | j                   d          t          t          | j                                                | _	        d S N )
__defaults__defaults__kwdefaults__
kwdefaultsdictziprQ   lenrangepos_default_map)rX   rE   s     r\   update_defaultszFunctionSpec.update_defaults   sv     )/R-3  $"C$6$6#6#8#895T]ASAS;T;TUU 
  
r^   N)__name__
__module____qualname__r   r]   rk   ra   r^   r\   rD   rD   o   sO        N\ N N N N$	
L 	
 	
 	
 	
 	
 	
r^   rD   rE   returnc                 p    t                               |           }|t          |           }|t           | <   |S N)_spec_cachegetrD   )rE   specs     r\   	_get_specru      s4    ??4  D|D!! DKr^   c                 8   t          |           }|                    |            i }t          |          }t          |j                  D ]7\  }}	|t          |          k     rt          |||                   ||	<   3|	|v r]|	|j        v r-t          t          |t          j        |	 d          g           t          ||                    |	                    ||	<   |	|j        v rm|j        |	         }
d }|r;t          j        |j        |
                   rt           j        st%          ||
          }t          ||j        |
         |          ||	<   
t          t          |t          j        d|	           g           9|t          |j                  d          }|j        r&t          |t)          |                    ||j        <   nQ|rOt          t          |t          j        dt          |           dt          |j                             g           |j        D ]}	|	|v r't          ||                    |	                    ||	<   -|	|j        v r6d }|rt%          ||	d          }t          ||j        |	         |          ||	<   lt          t          |t          j        d|	           g           |j        rt          ||          ||j        <   n<|r:t          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0  4s4yy==%b$q'22BtHHV^^t)))(*1T2N2N2NOOP   
 &b&**T*:*:;;BtHHT)))&t,C!N @ +DM#,>??@@@ "0	3!?!?%b$-*<nMMBtHH$$+GGG      T'((**+E 
 .r5<< @ @4	 	
  'i#d))iiPSTXTfPgPgii 	
 	
 	
 	
 !  6>>%b&**T*:*:;;BtHHT_$$# O#1)T#N#N#N %b$/$*?AQRRBtHH$$+I4II       	
,R884?	 
  '(Wf(W(WXX	
 	
 	
 	
 Ir^   r   r<   c                     t          |t                    r|S |st          j        | |          S t          j                            ||          S rq   )
isinstancer6   buildr   LazyVariableTrackerr}   )r   valsources      r\   r{   r{      sQ    #'' A
 A$R--- ,33C@@@r^   c                     t          |                                          D ]4\  }}t          |t          t          f          rt          | |          ||<   5d S rq   )r   itemsr   r   rf   r{   )r   resultkvs       r\   wrap_args_kwargsr      sY    V\\^^$$ . .1a%'' 	.&r1--F1I. .r^   r   c                     | j         j        }|j        D ]H}|                                }||v r)|                    ||                    |                     |||<   IdS )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     ss     =-L     ..0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     s     #"""""<ixAAD$D+u%% 2&&&&&&88K0011 *[$"?"??&DKr^   >   __doc__rG   __closure____globals__rb   rd   r   c                 l   |ot          ||          }|r|dk    rt          |          }	 t          j        ||          }n%# t          $ r t          t          |            Y nw xY w|t          v rt          ||          }|r t          j	        
                    ||          S t          j        | |          S )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00F )$+++ !((5'D11 5 5 5 	!444445 $$$T"" D,33FFCCC V,,,s   A A#"A#c                   R    e Zd Zd Zd Z	 	 	 	 	 	 	 	 ddZddded	efdZd Z	d Z
dS )BaseUserFunctionVariablec                 4    |                                  j        S rq   get_codeco_filenamerX   s    r\   get_filenamez%BaseUserFunctionVariable.get_filenameJ      }}**r^   c                 4    |                                  j        S rq   r   co_namer   s    r\   get_namez!BaseUserFunctionVariable.get_nameM      }}&&r^   r   r<   rx   list[VariableTracker]r   dict[str, VariableTracker]ro   r6   c                 \    |                     | g |                                 ||          S rq   )inline_user_function_return	self_argsrX   r   rx   r   s       r\   call_functionz&BaseUserFunctionVariable.call_functionP  s2     --d4Ndnn6F6F4N4NPVWWWr^   r   c                     d}	 t          |                                 |          }n-# t          $ r  |dk    rt          | t                    rd}Y nw xY wt
          j                            |          S )NFrl   T)hasattrget_functionNotImplementedErrorr   NestedUserFunctionVariabler   r8   r}   rX   r   r   r   s       r\   call_obj_hasattrz)BaseUserFunctionVariable.call_obj_hasattrX  s     	T..00$77FF" 	 	 	z!!j7Q&R&R!	 )00888s   "' 'AAc                 r    t          t          j        |                                           j                  S rq   )r   r   	signaturer   
parametersr   s    r\   inspect_parameter_namesz0BaseUserFunctionVariable.inspect_parameter_namesd  s*    G%d&7&7&9&9::EFFFr^   c                     i S rq   ra   rX   r   s     r\   closure_varsz%BaseUserFunctionVariable.closure_varsg      	r^   Nr   r<   rx   r   r   r   ro   r6   )rl   rm   rn   r   r   r   strr6   r   r   r   ra   r^   r\   r   r   I  s        + + +' ' 'X#X &X -	X
 
X X X X
9)
914
9	
9 
9 
9 
9G G 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                 t    t          |                    t          j                              | ||          S Nr   )r"   
make_guardr!   CLOSURE_MATCHclsvaluer   s      r\   create_with_sourcez'UserFunctionVariable.create_with_sourcet  s6    f''(BCCDDDs5((((r^   Fro   Nc                 L    t                      j        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|          }|| _        d S )
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8 	%#D$D "u1593KLMM 		KPO&5	    #B(=rBB&(r^   c                 |    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  s3    $,-- 	7Nww))+++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  	    wr^   c                     | j         j        S rq   )r   rG   r   s    r\   r   zUserFunctionVariable.get_code  s    wr^   c                     t           j        S rq   r   r   r   s    r\   python_typez UserFunctionVariable.python_type      !!r^   c                 2    t          | j        dd           d uS N__self__)r   r   r   s    r\   has_selfzUserFunctionVariable.has_self  s    tw
D11==r^   c                     | j         j        S rq   )r   r   r   s    r\   get_globalsz UserFunctionVariable.get_globals  s    w""r^   c                 Z    | j         }|r!t          | t          j                  r| j        }|S rq   )r   r   r   UserMethodVariable	source_fn)rX   r   s     r\   
get_sourcezUserFunctionVariable.get_source  s1     	$jy'CDD 	$^Fr^   c                    | j         rJ | j        }t          |t                    st	          d          |j        j        }|                                 }t          |||||          }t          |||j
                   | j        j        pd}t          |          t          | j        j
        j                  k    sJ t          t          j                    | j        j
        j        |          D ]\  }	}
}|j        j        }||v r	||         }n|rt%          t'          |          |	          }t)          |d          }	 t+          j        ||j        |          }n## t0          $ r t3          j                    }Y nw xY w|                    |||          }nV	 t+          j        ||j                  }n## t0          $ r t3          j                    }Y nw xY w|                    d||          }|||
<   |S )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++ 	GEFFF-'""!"gvtVDDffbk222'%+7||s47#3#?@@@@@@"Otw/;W 
  
  	$  	$OCt "=5L|##'- V,]6-B-BCHH(2<(Q(Q%?#2#8 24I$ $LL " ? ? ?#,#<#>#>LLL? (;; $ ?#2#8AS#T#TLL! ? ? ?#,#<#>#>LLL? (;;D$UU#F4LLs$   >EE:9E:F11GGr   r<   r   c                     |t           v rt          j        | |          S |                                 }t	          || j        ||          S rq   )r+   r   GetAttrVariabler  r   r   )rX   r   r   r   s       r\   var_getattrz UserFunctionVariable.var_getattr  sF    ))),T4888""b$'64888r^   c                 j    t          | j        |          }t          j                            |          S rq   )r   r   r   r8   r}   r   s       r\   r   z%UserFunctionVariable.call_obj_hasattr  s,     $'')00888r^   rx   r   r   r   r6   c           
         | j         t          j        j        u r	 d |D             }d |                                D             }t          j        j        |i |j        }t          j        |          S # t          $ r}t          d| d|           |d }~ww xY w| j         t          j        j
        u r	  t          j        | j                   j        |i |}|j        d                                         }	t!          |	t"                    sJ t          j        |	          S # t&          $ r}t          d| d|           |d }~ww xY w| j         t          j        j        u r t          j        | j                   j        |i |}|j        d         }
t!          |
t,                    sM|
                                }d|j         d	}t3          d
d| j         d| d| d|g t6          j                   t!          |
t:                    s4|
                                }d| d}t3          d|  |d| ddg           |
j         }t          j        |d          S | j         r*tC          || j         |                                 ||          S |j"        j#        j$        s|| j         t          j        j%        j&        u r_t          j        j'        (                    |          5  tS                      *                    |||          cd d d            S # 1 swxY w Y   |j"        j#        j+        r|j"        j#        j,        s	 ddl-m.} n# t&          $ r d }Y nw xY w|t| j         |j/        |j0        fv r_t          j        j'        ,                    |          5  tS                      *                    |||          cd d d            S # 1 swxY w Y   tS                      *                    |||          S )Nc                 6    g | ]}|                                 S ra   r   .0args     r\   
<listcomp>z6UserFunctionVariable.call_function.<locals>.<listcomp>  s$    GGG3c4466GGGr^   c                 >    i | ]\  }}||                                 S ra   r%  )r'  keyr   s      r\   
<dictcomp>z6UserFunctionVariable.call_function.<locals>.<dictcomp>  s7          6>c3C//11     r^   zCannot 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   changesr   DynamoConfigPatchVariabler3   RuntimeErrorr-  r   r   bind	argumentsr   r   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_stater3  _pre_forward_post_forward)rX   r   rx   r   
args_constkwargs_constr6  er@   r-  fn_vartypmsgfn_namer   r3  r   s                   r\   r   z"UserFunctionVariable.call_function  sX    7em777GG$GGG
   BH,,..       -;#/   !:7CCC6   "6!6 6-36 6  	 W:::7)$'227HHH',*($$&& % ""6===== :;OPPP   "6!6 6-36 6   W5553G%dg..3TDVDDEZ]Ff&>?? 
((**eVYVbeee 6LTZLL4LL6LLL #*5	    f&:;;  //++ v  v  v  v <#I #H7HHH0	    B9"RVWWWW 	/DGT]]__dF  
 	(U	?}"[\ \ +^^  ? ? ww,,Rv>>? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? I$@	CI,P	C!UUUUUUU ! ! ! 			!$&'5 * * ]/SSTVWW C C 7700T6BBC C C C C C C C C C C C C C C Cww$$Rv666sg   AA4 4
B>BB5A-D# #
E-EE#LLL=M MM#OOO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7 	 
"	0N ) ) [)) ) ) ) ) ) )4, , , , ,         " " "> > ># # #  5c?6J1K 5 5 5 5n95 9S 9 9 9 99)9149	9 9 9 9a7#a7 &a7 -	a7
 
a7 a7 a7 a7 a7 a7 a7 a7 a7 a7r^   r   c                   P     e Zd Zdd fdZed             Z	 	 	 	 	 	 	 	 ddZ xZS )BuiltinMethodVariableFro   Nc                      t                      j        di | t          |t          j                  sJ || _        d S r`   )r   r]   r   r   BuiltinMethodTyper   r   s       r\   r]   zBuiltinMethodVariable.__init__Z  sD    ""6""""e566666r^   c                 t    | j         }| j        }|t          u r|dk    pt          |          t          u o|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_  sH    ll u$A	)A 
*L{n/L	
r^   r   r<   rx   r   r   r   r6   c                     | j         j        }| j         j        }| j        ot	          | j        d          }t          j        |||          }|                    ||||          S r  )r   r	  rl   r   r#   r6   r   call_method)rX   r   rx   r   rc  r   
obj_sourceobj_vts           r\   r   z#BuiltinMethodVariable.call_functionj  s^     g&w[HZZ%H%H
 &r;
CC!!"dD&999r^   rS  rT  r   )rl   rm   rn   r]   staticmethodre  r   rW  rX  s   @r\   rZ  rZ  Y  s              
 
 
 \

:#
: &
: -	
:
 

: 
: 
: 
: 
: 
: 
: 
:r^   rZ  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                 d     t                      j        di | || _        || _        || _        d S r`   )r   r]   rY   r   rm  )rX   rY   r   rm  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
    yr^   c                 4    |                                  j        S rq   r   r   s    r\   r   z)LocalGeneratorObjectVariable.get_filename  r   r^   c                 4    |                                  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                     dS NFra   r   s    r\   r
  z%LocalGeneratorObjectVariable.has_self      ur^   c                 *    |                                  S rq   )r   r   s    r\   rl   z%LocalGeneratorObjectVariable.__name__  s    }}r^   c                 J    | j         j         d|                                  dS )N(r0  )r   rl   r   r   s    r\   __str__z$LocalGeneratorObjectVariable.__str__  s&    .)>>DMMOO>>>>r^   codegenr:   c                    ddl m} ddlm}m}m}  |j                    } ||          } ||          } ||          }	|5  |5  |	5  |                     |          }
|
j        s| 	                    |          | _
        t          j        | j
                                      |           d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nr   )"disallow_side_effects_in_generator)r<    save_and_restart_speculation_log(temporarely_allow_writes_to_output_graph)torch._dynamo.side_effectsr|  torch._dynamo.symbolic_convertr<   r}  r~  
current_tx_get_inline_tracergenerator_exhaustedforce_unpack_var_sequenceremaining_itemsr   ListIteratorVariablereconstruct)rX   rz  r|  r<   r}  r~  r   savedisallowtemptracers              r\   r  z(LocalGeneratorObjectVariable.reconstruct  s4   QQQQQQ	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 ."-////3355b9977;; 	V 	V8 	V 	VT 	V 	V,,R00F- J'+'E'Eb'I'I$*4+?@@LLWUUU		V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	Vs[   C'C	A#B8,C8B<<C?B< CC'C	C'C	C''C+.C+c                 :    | j                             |||          S rq   )r   r  r   s       r\   r  z&LocalGeneratorObjectVariable.bind_args  s    w  T62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                 d    ddl m} | j        |                    || g i           | _        | j        S Nr   )InliningInstructionTranslator)r  r  rm  build_inline_tracer)rX   r   r  s      r\   r  z/LocalGeneratorObjectVariable._get_inline_tracer  sI    PPPPPP%!>!R!RD"b" "D !!r^   c                    |                      |          }|                                 rt          t          |           	 t	          j        t          dt          d         i          5  |                                cd d d            t          dxx         t          d         z  cc<   S # 1 swxY w Y   np# t          $ r}d|_	        |d }~wt          $ r  t          $ rC}t          j        j                            |                                            t"          |d }~ww xY w	 t          dxx         t          d         z  cc<   d S # t          dxx         t          d         z  cc<   w xY w)Nunimplementedinline_callT)r  _is_generator_exhaustedr   StopIterationr   rf   r,   inline_call_r   r  r   r    r   r4  
eval_frame	skip_coder   r   )rX   r   r  rN  s       r\   next_variablez*LocalGeneratorObjectVariable.next_variable  s   ((,,'')) 	8$]B777	A H8O&PQQ - -**,,- - - - - - - _%%%-)@@%%%%- - - - - - - - -  	 	 	)-F&G% 	 	 	 	# 	# 	#M$..t}}???"	#- _%%%-)@@%%%%%H_%%%-)@@%%%%%%%sZ    'B4 'B(;B4 (B,,B4 /B,0B4 3E 4
D!>	CD!>DD!!E "E)c                     ||                                  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0&u---r^   c                     dS rt  ra   r   s     r\   has_unpack_var_sequencez4LocalGeneratorObjectVariable.has_unpack_var_sequence  ru  r^   ro   c                     dS NTra   r   s     r\   has_force_unpack_var_sequencez:LocalGeneratorObjectVariable.has_force_unpack_var_sequence  s    tr^   c                 @    g }|                      ||j                   |S rq   )force_apply_to_var_sequenceappend)rX   r   r   s      r\   r  z6LocalGeneratorObjectVariable.force_unpack_var_sequence  s$    ((V];;;r^   Nc                     	 	  ||                      |                     n # t          $ r t          |           Y d S w xY wArq   )r  r   r   )rX   r   r   s      r\   r  z8LocalGeneratorObjectVariable.force_apply_to_var_sequence  sb    	4%%b))****,   )"---	s   " ??c                     |                      |          }	 |                    |           d S # t          $ r }|                    |           Y d }~d S d }~ww xY wrq   )r  _raise_exception_variabler   exception_handler)rX   r   excr  rN  s        r\   _setup_exceptionz-LocalGeneratorObjectVariable._setup_exception  s{    ((,,	(,,S11111  	( 	( 	( $$Q'''''''''	(s   . 
AAAc                 4    | j         d u p| j         j        dk    S )Nr   )rm  instruction_pointerr   s    r\   _is_generator_just_startedz7LocalGeneratorObjectVariable._is_generator_just_started  s!    !T)XT-?-SWX-XXr^   c                 .    t          | j        dd          S )Nr  F)r   rm  r   s    r\   r  z4LocalGeneratorObjectVariable._is_generator_exhausted  s    t)+@%HHHr^   r   r   rx   r   r   r   r6   c                    |dk    r|                      |          S |dk    r| S |dk    r|                                 r=t          |          r.t          d |D                       st	          t
          |           |                     |          }|                    |           |                      |          S |dk    ra|                     |          }|                                 s|                                 rd|_	        t          j        d           S 	 |                     |t          j        t          d                     t          j        dk    r+|j        j        d	k    rd|_	        t          j        d           S n+# t&          $ r d|_	        t          j        d           cY S w xY w	 |                      |          rt	          t(          |           nD# t&          $ r d|_	        t          j        d           cY S t*          $ r |j        J |j        cY S w xY w|d
k    r|                     |          }	 |                     |t          |          dk    r|d         n|d                    n# t.          $ r  w xY w|                      |          }t1          dt2          fi           }	 |                     |t          j        |d                     |                      |           t	          t(          |           n# t5          |          $ r Y nw xY w|S t7                                          ||||           d S )N__next____iter__sendc              3   R   K   | ]"}t          |t                    o|j        d u V  #d S rq   )r   r8   r   r&  s     r\   	<genexpr>z;LocalGeneratorObjectVariable.call_method.<locals>.<genexpr>  sM         s$455K#)t:K     r^   closeTra   )      CALL_INTRINSIC_1throwr  r2   r   __InternalThrowException)r  r  rh   allr   r|   r  	push_manyr  r  r   r8   r  ExceptionVariableGeneratorExitsysversion_infonext_instructionopnamer   r8  r   symbolic_resultr   r`  r=  r   r   rg  )	rX   r   r   rx   r   r  retvalexc_typer   s	           r\   rg  z(LocalGeneratorObjectVariable.call_method  s    :%%b)))ZKV^^ ..00 <SYY <   #     < -Y;;;,,R00FT"""%%b)))W__ ,,R00F..00 8D4P4P4R4R 8-1* 1$7778%%	3M2FF  ( $///6:LLL15F.$5d;;;( 8 8 8-1* 1$777778
.%%b)) ?,\2>>>( 8 8 8-1* 1$77777, . . .
 -999----. W__ ,,R00F
 %%bSYY!^^$q''aQQQQ$    ''++F| 6bIIH?%%b)*EhPR*S*STTT""2&&&
 )v>>>>	 1::   
 MBdF33333sI   A(F %F-,F-1*G %H H H ?7I7 7J3>L LLrz  r:   rT  )#rl   rm   rn   r   CodeTyper   r]   r   r   r   r   r
  ry  __repr__r  r  r  r  r  r  r   r  builtinsr;  r  r   r6   r  r  r  r  r  r   rg  rW  rX  s   @r\   rl  rl  w  s,       
+n
+   78	
+ 
+ 
+ 
+ 
+ 
+  + + +' ' '" " "    ? ? ? HV V V V&3 3 3  # # #" " "A A A0. . .
  8=    tO/D    
   ( ( (Y Y YI I Iz4#z4 z4 &	z4
 -z4 
z4 z4 z4 z4 z4 z4 z4 z4 z4 z4r^   rl  c                       e Zd ZdZdS )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                V     t                      j        di | || _        || _        d S r`   )r   r]   r  r  )rX   r  r  r   r   s       r\   r]   z'LocalGeneratorFunctionVariable.__init__  s7     	""6"""*r^   c                     || 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114&&&tw%%%r^   c                 >    ddl m} |                    || ||          S r  )r  r  r  )rX   r   rx   r   r  s        r\   _build_inline_tracerz3LocalGeneratorFunctionVariable._build_inline_tracer  s8    PPPPPP,@@	
 
 	
r^   r   r<   rx   r   r   r   ro   r6   c                    t          | j                                                  s9t          dt	          | j                                                  dddg           |                     |||          }| 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   rm  rY   r   s          r\   r   z,LocalGeneratorFunctionVariable.call_function  s     DG,,..// 
	ADG,,..//. LH	 	 	 	 11"dFCCw!!G''))	 !!;	 " 
 
 	
r^   r   )rl   rm   rn   r   rl  r6   r]   r  r  r   rW  rX  s   @r\   r  r    s          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                 J     t                      j        |fdt          i| d S )Nr  )r   r]   r  )rX   r  r   r   s      r\   r]   z<FunctionDecoratedByContextlibContextManagerVariable.__init__  sB    	
 	
N	
 	
 	
 	
 	
 	
r^   c                     t                                          |||          }t          |t          j        j        j                  sJ d|_        |S r  )r   r  r   r   r4  symbolic_convert&InliningGeneratorInstructionTranslatoris_generator_from_ctx_manager)rX   r   rx   r   r  r   s        r\   r  zHFunctionDecoratedByContextlibContextManagerVariable._build_inline_tracer  sY    
 --b$??M*Q
 
 	
 	
 
 04,r^   )rl   rm   rn   r   r]   r  rW  rX  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 methodNro   c                      t                      j        dd|i| || _        || _        |?|                    d          ,t          |                    d          d          | _        d S d S d S )Nr   r   __func__ra   )r   r]   rb  r  rs   r#   )rX   r   rb  r  r   r   s        r\   r]   zUserMethodVariable.__init__+  s{    ))B)&)))" H!5!5!A'

8(<(<jIIDNNN !A!Ar^   c                 @    | j         j         d| j         d| j         dS )Nrx  r/  r0  )r   rl   r   rb  r   s    r\   r  zUserMethodVariable.__repr__>  s*    .)BBDGBBtxBBBBr^   c                     | j         gS rq   rb  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<   rx   r   r   r   r6   c                    ddl m}  || j                  rJg |                                 |}t	          j        | j        d          }|                    |||          S |j                                        rt          | j
        t          j                  rit          | j        dd          }||                    d          r|dk    s| j        r.| j
                            || j        j        ||| j        	          S nVt"          O| j        t"          j        j        u r7t	          j        | j                                      || j
        g|R |          S | j        rIt          | j
        j        | j        j                  }t-          |||                                 ||          S t1                                          |||          S )
Nr   )is_nonstrict_trace_callableTr1  rm    z	torch.nn.ztorch.nn.utils.parametrize)constant)trace_rulesr  r   r   r   r@  r   r   is_root_tracerr   rb  NNModuleVariabler   
startswithr   rg  rl   r9   FSDPParamGroupuse_training_stateTorchCtxManagerClassVariabler   rA  r   r   )
rX   r   rx   r   r  	call_argsvarmodule_attrr   r   s
            r\   r   z UserMethodVariable.call_functionG  s    	>=====&&tw// 	<2$..**2T2I8T  C $$RF;;; 9##%% 	*Hi0+
 +
 	 "$'<<<K '**;77 (#???# @ x++($AQ ,    @ ),;NNN9$'BBPPTX%%%v    	W)9::B/BvVVVww$$Rv666r^   c                 T    t                                                      dd          S Nr2   )r   r   r   s    r\   r   z*UserMethodVariable.inspect_parameter_names  s"    ww..0044r^   r   c                     |dk    r| j         S |dk    r t          j        || j        | j                  S t                                          ||          S )Nr	  r  )rb  r6   r   r   r  r   r!  rX   r   r   r   s      r\   r!  zUserMethodVariable.var_getattr  sU    :8O: #(TWdnEEEww""2t,,,r^   rq   rT  r   )rl   rm   rn   r   r]   r   r  r   r  r   r   r!  rW  rX  s   @r\   r  r  (  s       ..J J J J J J J&C# C C C C       ;7#;7 &;7 -	;7
 
;7 ;7 ;7 ;7 ;7 ;7z5 5 5 5 5-5 -S - - - - - - - - - -r^   r  c                   B     e Zd Zd fdZ	 	 	 	 	 	 	 	 d fdZd Z xZS )WrappedUserMethodVariablero   Nc                     |                     dd            |                     dd             t                      j        |j        |j        fi | || _        || _        d S )Nr   rb  )r~   r   r]   r   rb  wrappedr   rX   r  r   r   r   s       r\   r]   z"WrappedUserMethodVariable.__init__  sb    

4

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

4..v...r^   r   r<   rx   r   r   r   r6   c                     | j                             |           t                                          |||          }| j                             |           |S rq   r  r  s        r\   r   z)WrappedUserFunctionVariable.call_function  r  r^   c                                            fd             j                                       t          dd                     d S )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^   rT  r   r  rX  s   @r\   r  r    s             	#	 &	 -		
 
	 	 	 	 	 	> > > > > > >r^   r  c                     d fd|D             }fd|                                 D             } ||i |}| j                            ||t          |                    S )Nc                     t          | t          j                  r|                                 S |                                 S rq   )r   r   TensorVariableget_real_valuer   )xs    r\   convertz-invoke_and_store_as_constant.<locals>.convert  s;    a122 	&##%%%##%%%r^   c                 &    g | ]} |          S ra   ra   )r'  r   r!  s     r\   r)  z0invoke_and_store_as_constant.<locals>.<listcomp>  s!    %%%1GGAJJ%%%r^   c                 .    i | ]\  }}| |          S ra   ra   )r'  r   r   r!  s      r\   r,  z0invoke_and_store_as_constant.<locals>.<dictcomp>  s'    7771a777r^   r   )r   r   register_attr_or_moduler%   )r   r   r   rx   r   resr!  s         @r\   rA  rA    s    & & &
 &%%%%%%D7777777F
"d
f

C9,,d## -   r^   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   Nro   c	                    |	                     d          "|	                    t                                  t                      j        di |	 t          |                                t                    sJ t          |                                t          j	                  sJ t          |t                    sJ || _        || _        || _        || _        || _        || _        || _        || _        d S )Nmutation_typer(  ra   )rs   updater4   r   r]   r   r   r   r   r  rf   rR  rY   r   rc   re   r   r   
wrapped_fn)rX   rR  rY   r   rc   re   r   r   r+  r   r   s             r\   r]   z#NestedUserFunctionVariable.__init__  s     ::o&&.MM(<(>(>M???""6"""'4466<<<<<$1133U^DDDDD)T*****	" $&5?r^   c                     g S rq   ra   r   s    r\   r   z$NestedUserFunctionVariable.self_args  r   r^   c                 4    | j                                         S rq   )rY   r   r   s    r\   r   z#NestedUserFunctionVariable.get_code  s    y++---r^   c                     t           j        S rq   r  r   s    r\   r  z&NestedUserFunctionVariable.python_type  r  r^   c                 *   | j         rt          t          j        | j                                        | j        | j                                                  }| j        r| j                                        |_	        | j
        r| j
                                        |_        | j        rj| j                                        }t          |t                    rddlm} t#           ||                    }t          |t"                    sJ ||_        |S )Nr   r   )r   r   r   r   rY   r   r   rR  rc   rb   re   rd   r   r   r   r   r   rf   r   )rX   rE   r   r   s       r\   r   z'NestedUserFunctionVariable.get_function  s   < 	&%%!I((**NL++--
 

 = 	C $ @ @ B BD? 	G"&/"D"D"F"FD 		/*==??K+u-- :......"88K#8#899 k400000#.D r^   r   r<   name_varr   c                 l    |j         j                            | |j        |           t	          d           S rq   )r   r   
store_attrr   r8   )rX   r   r0  r   s       r\   call_setattrz'NestedUserFunctionVariable.call_setattr  s1     		))$DDD%%%r^   c                 t    |dk    r | j         |g|R  S t                                          ||||          S )N__setattr__)r3  r   rg  rX   r   r   rx   r   r   s        r\   rg  z&NestedUserFunctionVariable.call_method   sG    =  $4$R/$////ww""2tT6:::r^   c                     | j         d uS rq   )r   r   s    r\   has_closurez&NestedUserFunctionVariable.has_closure%  s    |4''r^   c                     |dk    r| j                                         S t                                          ||          S )Nrl   )rR  r   r   const_getattrr  s      r\   r:  z(NestedUserFunctionVariable.const_getattr(  s<    :<22444ww$$R...r^   c                     dS rt  ra   r   s    r\   r
  z#NestedUserFunctionVariable.has_self-  ru  r^   c                     | j         S rq   r  r   s    r\   r  z&NestedUserFunctionVariable.get_globals0  r  r^   c                 &   |                                  }t          j        || j        | j                                        | j        rt          | j        j                  nd t          d t          t          |                                  j                            D                                 }| j        r| j                                        |_         t          j        |          j        |i |}|                                 t'          |j                                                  }t+          |j        j        |           t1          |||           t3          |j                  D ]"\  }}	|	|vsJ | j        j        |         }
|
||	<   #|S )Nc              3   4   K   | ]}t          d           V  d S rq   )r1   )r'  _s     r\   r  z7NestedUserFunctionVariable.bind_args.<locals>.<genexpr>:  s(      SSa)D//SSSSSSr^   )r   r   r   r   rR  r   rc   r   r   ri   rh   r  re   keys_as_python_constantrd   r   r   r9  apply_defaultsrf   r:  r   r   r  r   rz   r   )rX   r   rx   r   rY   rE   r@   r   r   r   r  s              r\   r  z$NestedUserFunctionVariable.bind_args3  sr   }}!NL++--*.-AE$-%&&&TSS5T]]__5P1Q1Q+R+RSSSSS
 
 ? 	L"&/"I"I"K"KD,!$'',d=f==eo++--...777ffd+++"4#344 	  	 ICv%%%%<%c*DF4LLr^   rz  r:   c                                         fd            | j                                                           | j                  g            t          j        | j        j        j                             | j	        r | j	                   n)                    
                    d           g           | j        r | j                   n)                    
                    d           g           | j        r | j                   n)                    
                    d           g           | j        rd	 | j                                        }                                        |          g           nI# t          $ r  | j                   Y n-w xY w                    
                    d           g                               t!          dd                     | j        r                     fd            | j                                       t!          dd                                         t%          d                                         t!          dd                     dd	lm}  |j                    }|j        j                            |           r|j        j        j        |                                          D ]]\  }}                                  |                               t%          d                                         |           \d S d S )
Nc                  :                          t          d          S )Nr   )load_import_fromrl   rz  s   r\   r  z8NestedUserFunctionVariable.reconstruct.<locals>.<lambda>M  s    G,,X7JKK r^      Fc                  0                          dd          S )N	functoolswrapsrD  rE  s   r\   r  z8NestedUserFunctionVariable.reconstruct.<locals>.<lambda>q  s    00gF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   r  r<   r  r   r   has_pending_mutationstore_attr_mutationsr   dup_topr2  )rX   rz  r   r<   r   r   r   s    `     r\   r  z&NestedUserFunctionVariable.reconstructK  s   KKKK	
 	
 	
 		wBB4>RRSTTT '	(?@@AAA= 	EGDM""""!!7#<#<T#B#B"CDDD< 	EGDL!!!!!!7#<#<T#B#B"CDDD? 	EGDO$$$$!!7#<#<T#B#B"CDDD 		E*".AACC%%88EEF    ' * * *()))))* !!7#<#<T#B#B"CDDD21e<<===? 	A!!FFFF   GDO$$$!!"6q%"@"@AAA!!,q//222!!"6q$"?"?@@@ 	IHHHHH-"-//9!66t<< 	)!y5J egg) )e !!!%%l1oo666""4((((	) 	)) )s   AF F/.F/rq   rT  r  )rl   rm   rn   r   rU  r]   r   r   r  r   r6   r3  rg  r8  r:  r
  r  r  r  rW  rX  s   @r\   r   r     sV       	!	0N  @ 
@ @ @ @ @ @:  . . ." " "  0&#& "& 	& & & &; ; ; ; ;
( ( (/ / / / /
      08) 8) 8) 8) 8) 8) 8) 8)r^   r   c                   B     e Zd Zd fdZ	 	 	 	 	 	 	 	 d fdZd Z xZS )!WrappedNestedUserFunctionVariablero   Nc           
      $   |                     dd            |                     dd            |                     dd            |                     dd            |                     dd            |                     dd            |                     dd            |                     dd            t                                          |j        |j        |j        |j        |j        |j        |j	        |j
                   || _        || _        d S )	NrR  rY   r   rc   re   r   r   r+  )r~   r   r]   rR  rY   r   rc   re   r   r   r+  r  r   r  s       r\   r]   z*WrappedNestedUserFunctionVariable.__init__  s    

9d###

64   

;%%%

:t$$$

<&&&

=$'''

9d###

<&&&OLO		
 		
 		
 r^   r   r<   rx   r   r   r   r6   c                     | j                             |           t                                          |||          }| j                             |           |S rq   r  r  s        r\   r   z/WrappedNestedUserFunctionVariable.call_function  r  r^   c                                            fd             j                                       t          dd                     d S )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^   rT  r   r  rX  s   @r\   rQ  rQ    s             ,	#	 &	 -		
 
	 	 	 	 	 	> > > > > > >r^   rQ  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   reasonNro   c                 V     t                      j        di | || _        || _        d S r`   )r   r]   r   rX  )rX   r   rX  r   r   s       r\   r]   zSkipFunctionVariable.__init__  s2    ""6"""
r^   c                     | j         S rq   r   r   s    r\   r   z'SkipFunctionVariable.as_python_constant  s
    zr^   c                 x   t          j        |dd          r]|}|}t          |dd          r(|j        }t	          |d          }t          |dd          (|                    t          j                   n;t          |          s,t          |                    t          j                              | ||          S )N_torchdynamo_disableF_torchdynamo_orig_callabler   )
r   r   r   r^  r#   r   r!   r   r/   r"   )r   r   r   guard_on_sourceguard_on_values        r\   r   z'SkipFunctionVariable.create_with_source  s     !%)?GG 	I %O"N.*FNN !/!J",#%A# # .*FNN  &&|'ABBBB077 	I &++L,FGGHHHs5((((r^   r   r<   rx   r   r   r   r6   c           	          t          j        | j        dd          rOt          j        | j        dd           }t          dt	          | j                  d| j         d| ddg	           d S | j        t
          j        j        u rL|                    d
d           }|r|	                                }t          dd| d| dd| dg	           d S | j        t
          j        j
        u r>|                    d
d           }|r|	                                }t          d|           t          j        rMddlm} |                    || j                  }t#          |t$                    s|                    |||          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  }n# t,          $ r d!d"h}|
|v rd#| d$|	 d}d%d&g}n|
_|
                    d'          rJd(| d$|	 d$}d)g}t
          j        j                            |d*z   d*                    |          z              nJd+| d$|	 d,}d-d.g}t
          j        j                            |d*z   d*                    |          z              Y nw xY w|	d/k    rd0}g }| j        r| j        nd1}t          d2d3| d4|	 d5| ||	           d S )6Nr]  F_torchdynamo_disable_msgz1Skip calling `torch.compiler.disable()`d functionzSkip calling function `z>` since it was wrapped with `torch.compiler.disable` (reason: r0  z(Remove the `torch.compiler.disable` callr   rQ  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`.r4  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   r4  graph_breakrs   r   
skip_framer   r   dont_skip_tracingbuilderre  r}   r   rW  r   r   getfiler|   r  rD  	warn_oncejoinrX  )rX   r   rx   r   rQ  graph_break_msgskip_frame_msgre  
rebuilt_fnqualname	module_ormodule_namepathr   r   known_python_builtin_modulesrX  s                    r\   r   z"SkipFunctionVariable.call_function  s    !$*.DeLL p	(5OQUVVCKDJAdj A A:=A A A ?      Z5=444$jj55O G"1"D"D"F"F?e4ee\beeeT?TTD	      Z5=333#ZZt44N E!/!B!B!D!D\N\\   ' F666666 /55b$*EE
!*.BCC F%33BfEEEtz>;OPPH
L$??I090A,,s9~~K8Ytz22>V^ > > $> > > 
 @8??? D((lU] l l l ;	 E  %Y %Y %Y06/D, <<<7'7 7*27 7 7  
iGEE *y/C/CH/M/M*"h{"h"h]e"h"h"hK^E
 M'11+2DtyyQVGWGW2WXXXXb+ b bX` b b b  m;E M'11+2DtyyQVGWGW2WXXXK%YL +++? 
 $(KGT[[5GFF];]]H]]U[]]'	     s   >8G7 7CKKc                 f    t           j                            t          | j        |                    S rq   )r   r8   r}   r   r   r  s      r\   r   z%SkipFunctionVariable.call_obj_hasattrN  s%    )00T1J1JKKKr^   r   c                 v    |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  s9    ))),T4888b$*dk4@@@r^   rq   rT  r   r   r<   )rl   rm   rn   r6   rU  r]   r   rV  r   r   r   r   r!  rW  rX  s   @r\   rW  rW    s        
	'N      
   ) ) [)0v#v &v -	v
 
v v v vpL L L LA5 AS A A A A A A A Ar^   rW  c                   B     e Zd Zd fdZ	 	 	 	 	 	 	 	 d fdZd Z xZS )WrappedSkipFunctionVariablero   Nc                     |                     dd            |                     dd             t                      j        |j        fd|j        i| || _        || _        d S )Nr   rX  )r~   r   r]   r   rX  r  r   r  s       r\   r]   z$WrappedSkipFunctionVariable.__init__Y  se    

7D!!!

8T"""HHw~HHHHr^   r   r<   rx   r   r   r   r6   c                     | j                             |           t                                          |||          }| j                             |           |S rq   r  r  s        r\   r   z)WrappedSkipFunctionVariable.call_function`  r  r^   c                                            fd             j                                       t          dd                     d S )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^   rT  r   r  rX  s   @r\   r  r  X  r  r^   r  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".
    ro   Nc                 V     t                      j        di | || _        || _        d S r`   )r   r]   wrapper_objattr_to_trace)rX   r  r  r   r   s       r\   r]   z$WrapperUserFunctionVariable.__init__y  s6    ""6"""&*r^   r   r<   c                     || j         k    rLt          | j        | j                   }| j        o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~  sm    4%%%$*D,>??C[BZT%B%BF"(S&999ww""2t,,,r^   c                     g S rq   ra   r   s    r\   r   z%WrapperUserFunctionVariable.self_args  r   r^   rx   r   r   r   r6   c                    t          | j        d          rot          | j        | j        d           }t          |dd          pd}|                    dd          d         dk    rd	}t
          j        j                            |           t
          j        j        j	        
                    d
          }|                    t          j                  rt
          j        j                                        }t!                      |z   }|d         j        |d         j        f}	d                    t)          j        |                    }
d|	d          d|	d          d}|t-          |
          z  }|                    |           |                                 |z   }t3          j        t6          j                                      || t3          j        | j                  g||          S )N
cache_inform   r  rh  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: :rj  )r   r  r   r  splitr   r4  rD  rq  logging	getLoggerisEnabledForDEBUG_guardsTracingContextextract_stackr   filenamelinenorr  	tracebackformat_listr   debugr   r   r   r   getattr_and_tracer   r8   )rX   r   rx   r   	target_fnrx  rQ  dynamo_logger
user_stack	frame_locuser_stack_formatteduser_stack_traceall_argss                r\   r   z)WrapperUserFunctionVariable.call_function  s    4#\22 	: 0$2DdKKI!)\2>>D"K  q 11!4??K  #--c222 % 3 ; E Eo V V --gm<< :!&!=!K!K!M!MJ!7!9!9J!FJ!+B!8*R.:O PI+-7793H3T3T+U+U('rS\]^S_'r'rbklmbn'r'r'r$$,@(A(AA$!''(8999>>##d*-'
 

-9-d.@AAMHM
 
	
r^   rT  r}  r   )	rl   rm   rn   r   r]   r!  r   r   rW  rX  s   @r\   r  r  q  s         + + + + + +
- - - - - -  &
#&
 &&
 -	&

 
&
 &
 &
 &
 &
 &
 &
 &
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.
    ro   Nc                 L     t                      j        ||fi | || _        d S rq   )r   r]   rb  )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^   rT  )rl   rm   rn   r   r]   r   rW  rX  s   @r\   r  r    sV                    r^   r  c                  R    t           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    sD    %%'' +	
 	
 	
 	
 	
 	
 +*Ir^   c                     t           j                                        s
J d            |t                                                      v s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    su    ))++BB-BBB+-//66888888J""#NOOKk:...r^   c                        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.
    ro   Nc                x     t                      j        |fi | t          |t                    sJ || _        d S rq   )r   r]   r   r   replacement_var)rX   r   r  r   r   s       r\   r]   z*CollectiveFunctionRewriteVariable.__init__  sG    &&v&&&/+?@@@@@.r^   r   r<   c                 x    t                               | |          \  }}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  s`    >FFr6RR
0
0VV
VgVV
 
 	
 
 	
r^   c                 J    t          j        |           o| t                      v S rq   )r   
isfunctionr  )variables    r\   can_rewritez-CollectiveFunctionRewriteVariable.can_rewrite  s(     x((WX9U9W9W-W	
r^   c                 N    t                      |         }|t          | |          fS rq   )r  r  )r   r   r  s      r\   r  z)CollectiveFunctionRewriteVariable.rewrite  s(    -//34R@@@@r^   rx   r   r   r   r6   c                 |   dd l m} ddlm} t	          j        | j                  }t           |j        |i |j	                  }d}d|v rN|d         
                                r4t          d| j         d|d|d| j         g t          j        	           | j        |j        |j        |j        fv rn|                    d
          }||j        n|j        d
         j        }||vrt+          d|           t,          j                            ||                   |d
<   | j                            |||          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   r9  r:  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  s    	)(((((NNNNNN %dg..	nind5f55?@@F:$6$I$I$K$KC788d88v88[RVRY[[&2	    7O&%
 
 

 #JJt,,M !, ##)$/7 
  000 !Jy!J!JKKK$5<< + F4L #11"dFCCCr^   rT  r}  r   )rl   rm   rn   r   r]   rj  r}   r  r  r   rW  rX  s   @r\   r  r    s         / / / / / /
 
 
 
 \
 
 
 \

 A A A \A,D#,D &,D -	,D
 
,D ,D ,D ,D ,D ,D ,D ,Dr^   r  c                   0     e Zd Z	 	 	 	 	 	 	 	 d
 fd	Z xZS )FunctoolsWrapsVariabler   r<   rx   r   r   r   ro   r6   c                     |s,t                    dk    rfd}t          j        |          S t                                          ||          S )Nr2   c                     t          | t          j                  r|                     d                   S t	          d|  dg t
          j                   d S )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\   rI  z3FunctoolsWrapsVariable.call_function.<locals>.wraps2  sl    b)"FGG 888tAw8777 -!G *6	     r^   )rh   r   LambdaVariabler   r   )rX   r   rx   r   rI  r   s     `  r\   r   z$FunctoolsWrapsVariable.call_function*  sd      	3#d))q..
 
 
 
 
 +E222ww$$Rv666r^   r   )rl   rm   rn   r   rW  rX  s   @r\   r  r  )  sc        7#7 &7 -	7
 
7 7 7 7 7 7 7 7 7 7r^   r  c                   ,    e Zd Zd Z	 	 	 	 	 	 	 	 dd
ZdS )CollectionsNamedTupleFunctionc                     | j         S rq   r   r   s    r\   r   z0CollectionsNamedTupleFunction.as_python_constantD  r  r^   r   r<   rx   r   r   r   ro   r6   c                    t          ||          }|r	  | j        d |D             i d |                                D             }n`# t          $ rS}t	          t          |          |t          t          t          j	        |j
                                       Y d }~nd }~ww xY wt          j        |t                                S t          dd|d|dg t          j        	           d S )
Nc                 6    g | ]}|                                 S ra   r%  r'  r   s     r\   r)  z?CollectionsNamedTupleFunction.call_function.<locals>.<listcomp>Q  $    ;;;a**,,;;;r^   c                 >    i | ]\  }}||                                 S ra   r%  r'  r   r   s      r\   r,  z?CollectionsNamedTupleFunction.call_function.<locals>.<dictcomp>R  *    LLLTQq!..00LLLr^   rw   r)  znamedtuple constructionzargs=r  z?`torch.compile` only support certain input types for namedtupler   )r)   r   r   r|   r   r`  r   mapr8   r}   rx   r   UserDefinedClassVariabler5   r   r   r  )rX   r   rx   r   constant_argsr   r  s          r\   r   z+CollectionsNamedTupleFunction.call_functionG  sP    ,D&99 	
;;d;;;LLV\\^^LLL     (IIc"2"938DDEE         5%5%7%7    	-)t))))Y".		
 	
 	
 	
 	
 	
s   3A 
B%A	B  B%Nr   )rl   rm   rn   r   r   ra   r^   r\   r  r  C  s]          
#
 &
 -	

 

 
 
 
 
 
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                      j        di | || _        t          |t                    sJ || _        t          |t                    sJ || _        t          j	        t                    | _        d S r`   )r   r]   rE   r   r   rx   rf   keywordsrH  partialr-   
fake_value)rX   rE   rx   r  r   r   s        r\   r]   z!FunctoolsPartialVariable.__init__h  sy    ""6"""	$%%%%%	(D)))))  $+H55r^   c                     t           j        S rq   )rH  r  r   s    r\   r  z$FunctoolsPartialVariable.python_types  s      r^   rz  r:   c                 ^                        fd            | j                   | j        r                    | j                   | j        s:                    t          t          | j                  dz   d                     d S                     | j                                                   t          | j        
                                          }                                        t          |          t          | j                  z   dz   |d                     d S )Nc                  0                          dd          S )NrH  r  rJ  rE  s   r\   r  z6FunctoolsPartialVariable.reconstruct.<locals>.<lambda>w  s    g&>&>{I&V&V r^   r2   F)r  rE   rx   foreachr  r  r   rh   r  r   r  create_call_function_kw)rX   rz  r  s    ` r\   r  z$FunctoolsPartialVariable.reconstructv  s   VVVVWWW	9 	'OODI&&&} 	!!"6s49~~7I5"Q"QRRRF,,..///T]''))**++CIIDI,F,JDRWXX	
 	
 	
 	
 	
r^   c                 *    |                                  S rq   r%  r   s    r\   r   z%FunctoolsPartialVariable.get_function      &&(((r^   r   r<   rx   r   r   r   r6   c                 d    | j         |z   }i | j        |}| j                            |||          S rq   )rx   r  rE   r   )rX   r   rx   r   merged_argsmerged_kwargss         r\   r   z&FunctoolsPartialVariable.call_function  s;     i$&34=3F3y&&r;FFFr^   r   c                     t           j                            t          t	          j        t                    |                    S rq   )r   r8   r}   r   rH  r  r-   r  s      r\   r   z)FunctoolsPartialVariable.call_obj_hasattr  s7     )00I%h//66
 
 	
r^   c                 ~   | j         ot          | j         |          }|dk    r| j        S |dk    rt          j        | j        |          S |dk    r9d | j                                        D             }t          j        ||          S |t          v rt          j
        | |          S t          t          |           d S )NrE   rx   r   r  c                 >    i | ]\  }}t          j        |          |S ra   )r8   r}   r  s      r\   r,  z8FunctoolsPartialVariable.var_getattr.<locals>.<dictcomp>  s*    UUUtq!%,Q//UUUr^   )r   r#   rE   r   ListVariablerx   r  r   ConstDictVariabler+   r   r   r   )rX   r   r   r   r   s        r\   r!  z$FunctoolsPartialVariable.var_getattr  s    >DK!>!>6>>96>>)$)FCCCC:UUt}?R?R?T?TUUUE.uVDDDD))),T4888 44444r^   c                     t          j        | j                                        gd | j        D             R i d | j                                        D             S )Nc                 6    g | ]}|                                 S ra   r%  r&  s     r\   r)  z?FunctoolsPartialVariable.as_python_constant.<locals>.<listcomp>  s$    <<<3c$$&&<<<r^   c                 >    i | ]\  }}||                                 S ra   r%  r  s      r\   r,  z?FunctoolsPartialVariable.as_python_constant.<locals>.<dictcomp>  s*    KKKTQq!&&((KKKr^   )rH  r  rE   r   rx   r  r   r   s    r\   r   z+FunctoolsPartialVariable.as_python_constant  sq     I((**
<<$)<<<
 
 
 LKT]5H5H5J5JKKK
 
 	
r^   c                     t          j        | j                                        gd | j        D             R i d | j                                        D             S )zcSimilar to as_python_constant(), but add ID_MATCH guards to try to force things to become constantsc                 6    g | ]}|                                 S ra   guard_as_python_constant)r'  r   s     r\   r)  zEFunctoolsPartialVariable.guard_as_python_constant.<locals>.<listcomp>  s$    >>>qa((**>>>r^   c                 >    i | ]\  }}||                                 S ra   r  r  s      r\   r,  zEFunctoolsPartialVariable.guard_as_python_constant.<locals>.<dictcomp>  s*    QQQ41aq!,,..QQQr^   )rH  r  rE   r  rx   r  r   r   s    r\   r  z1FunctoolsPartialVariable.guard_as_python_constant  sq     I..00
>>DI>>>
 
 
 RQ4=;N;N;P;PQQQ
 
 	
r^   r  r   )rl   rm   rn   r6   r]   r  r  r   r   r   r   r!  r   r  rW  rX  s   @r\   r  r  g  s,       	6_ 	64 	6 	6 	6 	6 	6 	6! ! !
 
 
 
) ) )G#G &G -	G
 
G G G G
)
14
	
 
 
 
55 5S 5 5 5 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                 t    t          |                    t          j                              | ||          S r   )r"   r   r!   FUNCTION_MATCHr   s      r\   r   z-PolyfilledFunctionVariable.create_with_source  s6    f''(CDDEEEs5((((r^   Nc                 r    t                      j        di | || _        |                                                     ||          }t          |          sJ d| d|             dD ]*}t          ||d           }|rt          |          sJ |} n+t          d| d          || _        || _	        d S )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   r8  r+  r  )rX   r   r   handlercandidate_attr	candidater  r   s          r\   r]   z#PolyfilledFunctionVariable.__init__  s    ""6"""--//33B;;  XX"Xg"X"XTV"X"XXX 
 	 	N  >>I 	*****(
 PGPPP   & ,r^   c                     | j         S rq   )r  r   s    r\   polyfill_fnz&PolyfilledFunctionVariable.polyfill_fn  s      r^   c                 .    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    OI5
 
 	
r^   c                 *    |                                  S rq   r%  r   s    r\   r   z'PolyfilledFunctionVariable.get_function  r  r^   r   r<   rx   r   r   r   r6   c                 N   |                                  rXt          ||          rH | j        d |D             i d |                                D             }t	          j        ||          S | j        t          j        u rt          |          dk    r|st          |d         t          j        t          j        f          rt          d |d         j        D                       rt          j                            ||j                            dt$          j        t)          d |d         j        D                       fi           t%          j        d |d         j        D                       	          S t	          j        || j                  }|                    |||          S )
Nc                 6    g | ]}|                                 S ra   r%  r  s     r\   r)  z<PolyfilledFunctionVariable.call_function.<locals>.<listcomp>  r  r^   c                 >    i | ]\  }}||                                 S ra   r%  r  s      r\   r,  z<PolyfilledFunctionVariable.call_function.<locals>.<dictcomp>  r  r^   r2   r   c              3      K   | ]m}t          |t          j                  rt          |j        t                    p4t          |t          j                  o|                                t          u V  nd S rq   )r   r   r8   r   intSymNodeVariabler  r  s     r\   r  z;PolyfilledFunctionVariable.call_function.<locals>.<genexpr>  s          Ay9::Wz!'SV?W?W Yq)";<<WTWAW     r^   r   c              3   >   K   | ]}|                                 V  d S rq   as_proxy)r'  as     r\   r  z;PolyfilledFunctionVariable.call_function.<locals>.<genexpr>  s*      ??A1::<<??????r^   c                 ^    g | ]*}t          |t          j                  r|j        n|j        +S ra   )r   r   r8   r   sym_numr  s     r\   r)  z<PolyfilledFunctionVariable.call_function.<locals>.<listcomp>  sG          *!Y-GHH+AGG!"	  r^   )r#  )r  r*   r   r   r6   r   r  sumrh   r   r   r  TupleVariabler  r  r}   r   create_proxyr   sym_sumr   r  r   )rX   r   rx   r   r   traceable_function_variables         r\   r   z(PolyfilledFunctionVariable.call_function  s    ))++ 		50M&1
 1
 		5 ;;d;;;LLV\\^^LLL   #(V444 Gx|##D		Q 47Y%;Y=T$UVV     a      ,33	&&#M??a?????A	    "&a  	 	 4   ( '6&;B@Q&R&R#*88T6JJJr^   c                 h   |dk    r|                      |||          S t          | j        |d           }|J d| d| j                     t          |          sJ d| d| j                     i }| j        rt          | j        |          |d<   t          |fi |}|                     |||          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\   rg  z&PolyfilledFunctionVariable.call_method"  s     :%%b$777$--!!#JT#J#J#J#J!!!6""QQ$Qd$Q$Q$Q$QQQ"; 	> *4; = =GH%?%R%R'%R%R")77D&I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   rU  rV  rH  cacherf   r   r   r   r   r  r   r?   r]   propertyr  r  r   r   rg  r   rW  rX  s   @r\   r  r    s        
	'	N _tHS#X,>@R,R'S    _ [ ) ) [)
-2 -D - - - - - -. !R ! ! ! X!
 
 

) ) )2K#2K &2K -	2K
 
2K 2K 2K 2KhJ &	J
 -J 
J J J J&      r^   r  c                       e Zd Zd ZdS )TracebackVariablec                     d S rq   ra   r   s       r\   r   zTracebackVariable.call_function;  s      r^   Nrl   rm   rn   r   ra   r^   r\   r2  r2  9  s        22222r^   r2  c                   0     e Zd Z fdZd Zd Zd Z xZS )SysFunctionVariablec                 H     t                      j        di | || _        d S r`   )r   r]   r   )rX   r   r   r   s      r\   r]   zSysFunctionVariable.__init__?  s+    ""6"""


r^   c                 L   t          |j                  rB|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   exnrP  tbr   s         r\   exc_infozSysFunctionVariable.exc_infoC  s    r 	/"%C,CB%b#..%b"--EE *400*400*400E
 &u---r^   c                 B    |                      |          j        d         S r  )r<  r   r   s     r\   	exceptionzSysFunctionVariable.exceptionU  s    }}R  &q))r^   c                     | j         t          j        u r|                     |          S | j         t          j        u sJ |                     |          S rq   )r   r  r<  r>  r   s       r\   r   z!SysFunctionVariable.call_functionX  sJ    :%%==$$$zS]****~~b!!!r^   )rl   rm   rn   r]   r<  r>  r   rW  rX  s   @r\   r6  r6  >  se            . . .$* * *" " " " " " "r^   r6  ) 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dS )DynamoTritonHOPifierrQ  ro   c                      t          |          rq   )r    )rX   rQ  s     r\   raise_unsupportedz&DynamoTritonHOPifier.raise_unsupportedh  s    #r^   maybe_callablec                 :    t          |t          t          f          S rq   )r   r   r   )rX   rH  s     r\   is_callablez DynamoTritonHOPifier.is_callablek  s     79MN
 
 	
r^   r   c                     |j         S rq   r[  )rX   r   s     r\   	get_valuezDynamoTritonHOPifier.get_valuep  s
    yr^   .c                     ddl m} t          ||          r|                                S t	          ddt          |           dg t          j                   d S )Nr2   )BaseListVariablez/unsupported grid type for triton hop check_gridzgrid type = z;`torch.compile` only supports list-like grid for check_gridr   )listsrN  r   r   r   r`  r   r  )rX   gridrN  s      r\   
check_gridzDynamoTritonHOPifier.check_grids  s}    ++++++d,-- 
	==??"I3tDzz33Y&2	     r^   c                 r    d |                                 D             }|                    ||gi           }|S )Nc                 T    i | ]%\  }}t           j                            |          |&S ra   r   r8   r}   r  s      r\   r,  z2DynamoTritonHOPifier.call_grid.<locals>.<dictcomp>  s/    QQQDAq	*11!44aQQQr^   )r   r   )rX   rP  metar   s       r\   	call_gridzDynamoTritonHOPifier.call_grid  s;    QQDJJLLQQQ!!"tfb11r^   c                 l    ddl m} |                    ||          }|                    |||          }|S )Nr2   rd  )ro  re  r}   r   )	rX   user_fnrx   r   r   r  re  wrapped_user_functionr   s	            r\   call_user_defined_fnz)DynamoTritonHOPifier.call_user_defined_fn  sF    ...... 1 8 8W E E&44RvFFr^   c                 x    ddl m}  ||t          |j        |                                         |          }|S )Nr2   )VariableBuilder)ro  r\  r#   kernel_source_wrap)rX   user_objr   r  r   r\  wrapped_user_objs          r\   wrap_user_defined_objz*DynamoTritonHOPifier.wrap_user_defined_obj  sO    ,,,,,,*?
81d9==
 

%// 	  r^   c                 H    |                     |          }d |D             }|S )Nc                 ,    g | ]} |j                     S ra   r  )r'  r   s     r\   r)  z=DynamoTritonHOPifier.maybe_unpack_configs.<locals>.<listcomp>  s$    KKK26244KKKr^   )unpack_var_sequence)rX   configsr   s      r\   maybe_unpack_configsz)DynamoTritonHOPifier.maybe_unpack_configs  s0    --b11 LK7KKKr^   r   c                 |    |                                 s|                     d           |                                S )Nz`@triton.heuristics must return constant values because configs can only contain constant values.)is_python_constantrG  r  )rX   r   s     r\   maybe_unpack_heuristic_resultz2DynamoTritonHOPifier.maybe_unpack_heuristic_result  sE    ((** 	""r   ..000r^   r  TritonKernelVariablerx   c                     |j         t          |          dk    r|                     d            t          |          |j        |j        |d         |j                  S )Nr2   z7Triton kernels should be called with only a single gridr   )kernel
kernel_idxrP  r]  )rP  rh   rG  r`  rl  rm  r   )rX   r  rx   s      r\   call_getitemz!DynamoTritonHOPifier.call_getitem  sm     =$D		Q""I   tH~~?*a"/	
 
 
 	
r^   c                    ddl m ddlm} i }t	          |                                          D ]T}||         }t          |t          t          f          r.|	                                ||<   |
                                ||<   Ud |                                D             }	ddlm}
m} fd|                                D             }fd|	                                D             }|                                D ]b}|                                }t          |t"          j        t"          j        f          s&|                     d	t+          |           d
           c|
                    |          } ||t.                    }|j                            d|d|j        ||||                                d           t#          j        d           S )Nr2   r7   )r  c                 T    i | ]%\  }}t           j                            |          |&S ra   rT  r  s      r\   r,  z1DynamoTritonHOPifier.call_HOP.<locals>.<dictcomp>  s?     
 
 
1 &--a00!
 
 
r^   r   )kernel_side_tabletriton_kernel_wrapper_mutationc                 `    i | ]*\  }}t          |          ||                                +S ra   )r   r   r'  r   r   r8   s      r\   r,  z1DynamoTritonHOPifier.call_HOP.<locals>.<dictcomp>  sL     
 
 
1!-..
q##%%
 
 
r^   c                 <    i | ]\  }}t          |          ||S ra   )r   rt  s      r\   r,  z1DynamoTritonHOPifier.call_HOP.<locals>.<dictcomp>  s@     
 
 
1a!122
q
 
 
r^   z.Unexpected argument type for a Triton kernel: rh  r   ra   )rm  constant_args_idxrP  tma_descriptor_metadatar   )r  r8   dictsr  r   r  r   !TMADescriptorExperimentalVariableTMADescriptorStableVariableto_metadata
get_tensorr   *torch._higher_order_ops.triton_kernel_wraprq  rr  r  realizer   r  r  rG  repradd_constant_argsrf   r   r&  rm  r   )rX   r  gridscombined_args_rawr   r  rw  r   r   combined_argsrq  rr  r  non_constant_argsrv  rU  r8   s                   @r\   call_HOPzDynamoTritonHOPifier.call_HOP  s]   ......,,,,,, :<',,..// 	6 	6A!!$A57RS  6 ./]]__'*'(||~~!!$
 
)//11
 
 

	
 	
 	
 	
 	
 	
 	
 	

 
 
 
)//11
 
 


 
 
 
%++--
 
 
 #))++ 	 	A		Aa)":I<U!VWW &&OT!WWOOO   .??NN  !2D99
	*&1%6+B--// 		
 	
 	
 )
 
 	
r^   N)rl   rm   rn   r   r   rG  r   r;  rJ  rL  r   r   fxproxyProxyrQ  rV  rZ  ra  rf  ri  r   rn  r8   r  ra   r^   r\   rE  rE  g  s[       S U    
# 
$ 
 
 
 

S S    %(<c(A"B               1C 1C 1 1 1 1
(
 sm
 
 	
 
 
 
$C
BR C
 C
 C
 C
 C
 C
r^   rE  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 )rj  r=   rP  r>   rl  rm  r#   r]  ro   Nc                     |                     dd           | _         t                      j        di | t                              | |||           d S )Nr]  ra   )r~   r]  r   r]    dynamo_triton_hopifier_singletoninit_variable)rX   rl  rm  rP  r   r   s        r\   r]   zTritonKernelVariable.__init__	  sW    #ZZ>>""6"""(66tVZQUVVVVVr^   r   r<   rx   r   r   r   r6   c                 <    t                               | |||          S rq   )r  call_triton_kernelr   s       r\   r   z"TritonKernelVariable.call_function	  s%     0BB$
 
 	
r^   c                     |dk    rt                               | |          S |dk    rt                               | |||          S t                                          ||||          S )N__getitem__run)r  rn  call_runr   rg  r6  s        r\   rg  z TritonKernelVariable.call_method	  sj     =  3@@tLLLU]]3<<T4QSTTT ww""2tT6:::r^   r(  c                     ddl m} ddlm} t	          ||          r" |j        |                                          S |S )Nr2   r7   )r  )r  r8   tensorr  r   r}   evaluate_expr)rX   r(  r8   r  s       r\   specialize_symbolicz(TritonKernelVariable.specialize_symbolic)	  s`    ......++++++ c?++ 	@*#*3+<+<+>+>???
r^   rT  r   r.  )rl   rm   rn   r   r   r  r]   r   rg  r   r  rW  rX  s   @r\   rj  rj  	  s        
W W W W W W

#
 &
 -	

 

 
 
 
; &	;
 -; 
; ; ; ; ; ;s s        r^   rj  c                   D     e Zd Z	 	 	 	 	 	 	 	 d fdZd	 ZddZd Z xZS )ry  data_ptrvariables.DataPtrVariabledimslist[ConstantVariable]
block_dimselement_sizer8   c                     t          |t          j                  sJ  t                      j        di | || _        || _        || _        || _        d S r`   )	r   r   DataPtrVariabler   r]   r  r  r  r  )rX   r  r  r  r  r   r   s         r\   r]   z*TMADescriptorExperimentalVariable.__init__4	  s^     (I$=>>>>>""6""" 	$(r^   c                     t          d | j        D             d | j        D             | j                                                  S )Nc                 6    g | ]}|                                 S ra   r  r'  dims     r\   r)  zATMADescriptorExperimentalVariable.to_metadata.<locals>.<listcomp>E	  s     111S\\^^111r^   c                 6    g | ]}|                                 S ra   r  r  s     r\   r)  zATMADescriptorExperimentalVariable.to_metadata.<locals>.<listcomp>F	  s     777S\\^^777r^   )r@  r  r  r  r   r   s    r\   r{  z-TMADescriptorExperimentalVariable.to_metadataC	  sL    /11ty11177t777&&((
 
 	
r^   rz  r:   c                                           fd            j                                       g  j         j         j        }                    |                               t          |          dz   d           d S )Nc                  \                          ddt          j                   d          S )Nz$triton.tools.experimental_descriptorcreate_d_tma_descriptor)rD  rh   r  r  s   r\   r  z?TMADescriptorExperimentalVariable.reconstruct.<locals>.<lambda>L	  s0    G,,6:#di..:::  r^   r2   F)	r  r  r  r  r  r  r  r   rh   )rX   rz  rx   s   `` r\   r  z-TMADescriptorExperimentalVariable.reconstructJ	  s        	
 	
 	
 	!!'***@@T_@d.?@c$ii!mU33333r^   c                     | j         j        S rq   )r  from_tensorr   s    r\   r|  z,TMADescriptorExperimentalVariable.get_tensorV	  s    }((r^   )r  r  r  r  r  r  r  r8   r  rl   rm   rn   r]   r{  r  r|  rW  rX  s   @r\   ry  ry  3	  s        )-) ') -	)
 )) ) ) ) ) )
 
 

4 
4 
4 
4) ) ) ) ) ) )r^   ry  c                   >     e Zd Z	 	 	 	 d fdZd Zdd	ZddZ xZS )rz  r  variables.TensorVariableblock_shapevariables.ListVariablec                     t          |t          j                  sJ  t                      j        di | || _        || _        d S r`   )r   r   r  r   r]   r  r  )rX   r  r  r   r   s       r\   r]   z$TMADescriptorStableVariable.__init__[	  sP     &)":;;;;;""6"""&r^   c                 N    t          | j                                                  S rq   )rA  r  r   r   s    r\   r{  z'TMADescriptorStableVariable.to_metadataf	  s&    )%%''
 
 	
r^   rz  r:   c                                          fd                               d           | j                                        | j                                       d           d S )Nc                  0                          dd          S )Nztriton.tools.tensor_descriptorTensorDescriptorrJ  rE  s   r\   r  z9TMADescriptorStableVariable.reconstruct.<locals>.<lambda>m	  s    G,,0"  r^   r  r   )r  load_methodr  r  r  rg  r  s    `r\   r  z'TMADescriptorStableVariable.reconstructk	  s       	
 	
 	
 	M***((( !!!Ar^   ro   c                     | j         S rq   )r  r   s    r\   r|  z&TMADescriptorStableVariable.get_tensorw	  s
    {r^   )r  r  r  r  r  )ro   r  r  rX  s   @r\   rz  rz  Z	  s        	'*	' .	' 	' 	' 	' 	' 	'
 
 


 
 
 
       r^   rz  c                   @     e Zd Zdeddf fdZ	 	 	 	 	 	 	 	 ddZ xZS )'CreateTMADescriptorExperimentalVariablerankro   Nc                 T    |dv sJ  t                      j        di | || _        d S )N)r2   r   ra   )r   r]   r  )rX   r  r   r   s      r\   r]   z0CreateTMADescriptorExperimentalVariable.__init__|	  s:    
 v~~~~""6"""			r^   r   r<   rx   r   r   r   r6   c                 |   d|v r|d         n|d         }t          |t          j                  st          d| j         d          | j        dk    rPt          |          t          |          z   dk    sJ d|v r|d         n|d         g}d|v r|d         n|d	         g}nut          |          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   ry  )rX   r   rx   r   r  r  r  r  s           r\   r   z5CreateTMADescriptorExperimentalVariable.call_function	  s     %fUmmDG#y899 	$)$ $ $   9>>t99s6{{*a////!&&ud1gD (3f'<'<{##$q'JJ t99s6{{*a////"(F"2"2vQ"(F"2"2vQD
 )5(>(>|$$DG(4(>(>|$$DGJ 2@61I1Ivn--tTVx0!%	
 
 
 	
r^   r   )rl   rm   rn   r  r]   r   rW  rX  s   @r\   r  r  {	  s         
	     (
#(
 &(
 -	(

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

 

 
 
 
 
 
r^   r  rq   r}  )r   r  rH  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(   rD  r)   r*   r+   r,   r-   r.   r/   r0   r1   baser3   r4   r5   r6   r  r8   #torch.distributed.fsdp._fully_shardr9   ModuleNotFoundErrortorch._dynamo.codegenr:   r  r<   r}  r=   r>   r?   rT   rV   rr   rD   ru   r   r{   r   rf   r   r   r   r   r   r   r   rZ  rl  r  r  r  r  r  r  rA  r   rQ  rW  r  r  r  r  r  r  r  r  r  r  r2  r6  r@  rA  rB  rC  rE  r  rj  ry  rz  r  r  ra   r^   r\   <module>r     sX
   .            



      $ $ $ $ $ $       B B B B B B B B B B B B B B # # # # # #       % % % % % %  4 4 4 4 4 4 > > > > > > > > > > > > V V V V V V V V V V                        1 0 0 0 0 0 0 0               
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
            ' & & & & &EEEEEEE     //////DDDDDD        WT"""
  !!
 
 
 
 
 
 
 
>L \    U U Up	A 	A 	A 	A 	A. . . . $sO';"<        "  (   - - -0       Dk7 k7 k7 k7 k73 k7 k7 k7\: : : : :4 : : :<B4 B4 B4 B4 B4? B4 B4 B4J

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