
    rh'[                    (   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l2m3Z3m4Z4m5Z5m6Z6 d
dl7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@ ddlAmBZBmCZCmDZDmEZE ddlFmGZG 	 ddlHmIZI erddlKmLZL ddlMmNZN ddlOmPZPmQZQ  ede      ZRdZSdZT e       ZU G d d      ZVdedeVfd ZWd! ZXdgdhd"ZYdhd#ZZd$e[e\eEf   fd%Z]d& Z^h d'Z_d( Z` G d) d*eE      Za G d+ d,ea      Zb G d- d.ea      Zc G d/ d0eE      Zd G d1 d2ed      Ze G d3 d4ea      Zf G d5 d6ef      Zg G d7 d8eb      Zh G d9 d:eh      Zi G d; d<eb      Zjdhd=Zk G d> d?ea      Zl G d@ dAel      Zm G dB dCeE      Zn G dD dEen      Zo G dF dGeE      Zp G dH dIep      ZqdJ ZrdhdKZs G dL dMeb      Zt G dN dOeb      Zu G dP dQeb      Zv G dR dSeE      Zw G dT dUeE      Zx G dV dWeE      Zy G dX dYeE      ZzddZlOm{Z{m|Z|m}Z}m~Z~  G d[ d\e~      Z e       Z G d] d^eE      Z G d_ d`eE      Z G da dbeE      Z G dc ddeE      Z G de dfeE      Zy# eJ$ r dZIY w xY w)ia  
Function-related variable tracking classes for Dynamo's symbolic execution.

This module contains classes that track different types of functions during graph
compilation, including:
- User-defined functions and methods
- Built-in functions and methods
- Wrapped functions (e.g. from decorators)
- Special function types (e.g. functools.partial)
- Triton kernels and related function types

These classes are responsible for:
- Tracking function calls and their arguments
- Managing function closures and cell variables
- Handling function attributes and special methods
- Maintaining guards for function identity and closure contents
- Supporting function inlining and specialization
- Enabling proper symbolic execution of different function types

The variable trackers here work together with the rest of Dynamo to enable
accurate graph capture while handling Python's various function-related behaviors.
    N)SequenceFunctionType)AnyCallableOptionalTYPE_CHECKINGTypeVar)Never)patch)WeakKeyDictionary)get_stack_above_dynamo   )configgraph_break_hints	polyfills	variables)create_call_functioncreate_rot_nis_generator)
get_dynamo_observed_exceptionhandle_observed_exceptionInfiniteGeneratorErrorObservedExceptionObservedGeneratorExitObservedUserStopIterationraise_observed_exception	SkipFrameunimplemented_v2Unsupported)GuardBuilderinstall_guard)
AttrSourceConstantSourceDefaultsSourceGetItemSource)	check_constant_argscheck_unspec_or_constant_argscmp_name_to_op_mappingcountersidentityis_functionis_wrapper_or_member_descriptoristype	make_cell   )#AsPythonConstantNotImplementedErrorAttributeMutationNewValueMutationNewVariableTrackerConstantVariable)_fsdp_param_group)	PyCodegenInstructionTranslator)TritonGridTypeTritonKernelType_F)bound      c                   $    e Zd ZdefdZdefdZy)FunctionSpecfuncc                 p   |j                   }|j                  }|j                  | _        |j                  | _        |j                  | _        |d | j                   | _        || j                  | j
                   | _	        | j                  | j                  z   | _
        || j
                  | j
                  | j                  z    | _        | j
                  | j                  z   }|j                  t        z  r||   nd | _        || j                  rdndz  }|j                  t        z  r||   | _        y d | _        y )Nr0   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)selfrC   codevnoffs        t/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/torch/_dynamo/variables/functions.py__init__zFunctionSpec.__init__i   s   }}!44)) 22 4$"4"45!$"4"4t~~F!//$2F2FFt~~ARAR0RSnnt000'+}}z'ABsGtD%%q1,%)]]^%C"S'    c           
         |j                   xs d| _        |j                  xs i | _        t	        t        | j                  t        | j                         d  t        t        | j                                          | _	        y N )
__defaults__defaults__kwdefaults__
kwdefaultsdictziprO   lenrangepos_default_map)rV   rC   s     rZ   update_defaultszFunctionSpec.update_defaults{   si     ))/R--3  $""C$6#6#895T]]AS;TU 
r\   N)__name__
__module____qualname__r   r[   ri   r_   r\   rZ   rB   rB   h   s    N\ N$	
L 	
r\   rB   rC   returnc                 \    t         j                  |       }|t        |       }|t         | <   |S N)_spec_cachegetrB   )rC   specs     rZ   	_get_specrs      s.    ??4 D|D! DKr\   c                 l   t        |       }|j                  |        i }t        |      }t        |j                        D ]  \  }}	|t        |      k  rt        |||         ||	<   '|	|v r;|	|j                  v rt        |	 d      t        ||j                  |	            ||	<   f|	|j                  v r=|j                  |	   }
d }|rt        ||
      }t        ||j                  |
   |      ||	<   t        d|	        |t        |j                        d  }|j                  r#t        |t        |            ||j                  <   n/|r-t        dt        |       dt        |j                               |j                  D ]p  }	|	|v rt        ||j                  |	            ||	<   &|	|j                   v r0d }|rt        ||	d      }t        ||j                   |	   |      ||	<   dt        d|	        |j"                  rt        ||      ||j"                  <   |S |rt        dt%        |             |S )	Nz is positional-onlyz&Missing required positional argument: z#Too many positional arguments: got z, expected T)is_kwz(Missing required keyword-only argument: zUnexpected keyword arguments: )rs   ri   rd   	enumeraterO   rf   wrap_bound_argrM   	TypeErrorpoprh   r%   ra   rS   tuplerP   rc   rU   list)rC   tx	fn_sourceargskwargsrr   barem_kwinameidxdefault_sourceextrakwdefault_sources                 rZ   bind_args_cachedr      sI   T?D	B&\F T//0 M4s4y=%b$q'2BtHV^t)))4&(; <==%b&**T*:;BtHT)))&&t,C!N!/	3!?%b$--*<nMBtHDTFKLLM" T''(*+E .r5< @4	1#d)KDL^L^H_G`a
 	

 !! 	O6>%b&**T*:;BtHT__$##1)T#N %b$//$*?AQRBtHFtfMNN	O ,R84?? I 
8fGHHIr\   c                     t        |t              r|S |st        j                  | |      S t        j                  j                  ||      S ro   )
isinstancer4   buildr   LazyVariableTrackercreate)r|   valsources      rZ   rw   rw      sB    #'
$$R-- ,,33C@@r\   c                     t        |j                               D ]+  \  }}t        |t        t        f      st        | |      ||<   - y ro   )r{   itemsr   rz   rd   rw   )r|   resultkvs       rZ   wrap_args_kwargsr      s>    V\\^$ .1a%'&r1-F1I.r\   r   c                     | j                   j                  }|j                  D ]<  }|j                         }||v r!|j	                  ||j                  |             |||<   > y)z
    Update `result` to add mapping from local name to new cells created
    directly by `code`, or update SideEffects in `parent` if the a local cell is
    already in `result` (cell argument).
    N)outputside_effectsco_cellvarstrack_cell_new
store_cellry   )parentr   rW   r   r   new_cells         rZ   init_cellvarsr      sa     ==--L    ..06> ##Hfjj.>?t r\   c                     ddl m}  || ||||      }||_        t        |t              rddlm}	 t         |	|            }|t        |t              sJ ||_        |S )Nr   r   pairwise)	typesr   rb   r   rz   	itertoolsr   rd   __annotations__)
rW   	f_globalsr   ra   closurerc   annotationsr   rC   r   s
             rZ   _create_nested_fnr      sa     #ixAD$D+u%&8K01 *[$"???&DKr\   >   __doc__rE   __closure____globals__r`   rb   r   c                 &   |xr t        ||      }	 t        j                  ||      }|t
        v rt        ||      }|r t        j                  j                  |      S t        j                  |       S # t        $ r t	        t        |        Y gw xY wro   )r#   inspectgetattr_staticAttributeErrorr   fn_known_dunder_attrsgetattrr   r   r   r4   r   )r|   fnr   r   subobjs        rZ   fn_var_getattrr     s    0
640F5''D1 $$T",,33FFCC  V,,  5 	!45s   A4 4BBc                   P    e Zd Zd Zd Z	 	 	 	 	 	 	 	 ddZdddedefdZd	 Z	d
 Z
y)BaseUserFunctionVariablec                 6    | j                         j                  S ro   get_codeco_filenamerV   s    rZ   get_filenamez%BaseUserFunctionVariable.get_filename      }}***r\   c                 6    | j                         j                  S ro   r   co_namer   s    rZ   get_namez!BaseUserFunctionVariable.get_name      }}&&&r\   r|   r:   rm   c                 L    |j                  | g | j                         ||      S ro   )inline_user_function_return	self_argsrV   r|   r~   r   s       rZ   call_functionz&BaseUserFunctionVariable.call_function  s,     --d4Ndnn6F4N4NPVWWr\   r   c                     d}	 t        | j                         |      }t
        j                  j                  |      S # t        $ r |dk(  rt        | t              rd}Y Aw xY w)NFrj   T)hasattrget_functionNotImplementedErrorr   NestedUserFunctionVariabler   r6   r   rV   r|   r   r   s       rZ   call_obj_hasattrz)BaseUserFunctionVariable.call_obj_hasattr'  se     	T..0$7F ))0088 # 	z!j7Q&R	s   =  A A c                 n    t        t        j                  | j                               j                        S ro   )r{   r   	signaturer   
parametersr   s    rZ   inspect_parameter_namesz0BaseUserFunctionVariable.inspect_parameter_names3  s'    G%%d&7&7&9:EEFFr\   c                     i S ro   r_   rV   r|   s     rZ   closure_varsz%BaseUserFunctionVariable.closure_vars6      	r\   Nr|   r:   r~   list[VariableTracker]r   dict[str, VariableTracker]rm   r4   )rj   rk   rl   r   r   r   strr4   r   r   r   r_   r\   rZ   r   r     sd    +'X#X &X -	X
 
X
9)
914
9	
9Gr\   r   c                        e Zd ZdZddhej
                  Zed        Zdd fdZ fdZ	d Z
d	 Zd
 Zd Zd Zd Zdeeef   fdZdddefdZdddedefdZ	 	 	 	 	 	 	 	 d fdZ xZS )UserFunctionVariablez-Some unsupported user-defined global functionr   is_constantc                 f    t        |j                  t        j                                | ||      S Nr   )r"   
make_guardr!   CLOSURE_MATCHclsvaluer   s      rZ   create_with_sourcez'UserFunctionVariable.create_with_sourceC  s(    f''(B(BCD5((r\   rm   c                 B   t        |   d	i | t        |dd      rd| _        nd| _        t	        |t
        j                  t        j                  j                  f      s!t        d| ddgt        j                         t        j                  |d|      }|| _        y )
N_dynamo_marked_constantFTz1can't handle functions not implemented in python z2Dynamo can only handle functions defined in pythonz9Move usage of this function out of `torch.compile` regiongb_typecontextexplanationhints_torchdynamo_inliner_   )superr[   r   r   r   r   r   torchjitScriptFunctionr   r   INFERENCE_MODEr   r   r   rV   r   r   r   	__class__s       rZ   r[   zUserFunctionVariable.__init__H  s    "6"20%8#D$D "u115993K3KLMK$PO&55	 ##B(=rB&(r\   c                 X    t        | t              r| j                  S t        |          S ro   )r.   r   r   r   as_python_constantrV   r   s    rZ   r   z'UserFunctionVariable.as_python_constantb  s%    $,-77Nw)++r\   c                     g S ro   r_   r   s    rZ   r   zUserFunctionVariable.self_argsh  r   r\   c                     | j                   S ro   r   r   s    rZ   r   z!UserFunctionVariable.get_functionk      wwr\   c                 .    | j                   j                  S ro   )r   rE   r   s    rZ   r   zUserFunctionVariable.get_coden  s    wwr\   c                 "    t         j                  S ro   r   r   r   s    rZ   python_typez UserFunctionVariable.python_typeq      !!!r\   c                 4    t        | j                  dd       d uS N__self__)r   r   r   s    rZ   has_selfzUserFunctionVariable.has_selft  s    tww
D1==r\   c                 .    | j                   j                  S ro   )r   r   r   s    rZ   get_globalsz UserFunctionVariable.get_globalsw  s    ww"""r\   c                 D   | j                   rJ | j                  }t        |t              st	        d      |j
                  j                  }t        ||| j                  ||      }t        |||j                         | j                  j                  xs d}t        |      t        | j                  j                  j                        k(  sJ t        t        j                          | j                  j                  j                  |      D ]  \  }}	}
|j
                  j"                  }|
|v r||
   }n| j                  rbt%        t'        | j                  d      |      }t'        |d      }	 t)        j*                  ||
j,                  |      }|j5                  ||
|      }n4	 t)        j*                  ||
j,                        }|j5                  d|
|      }|||	<    |S # t.        $ r t1        j2                         }Y pw xY w# t.        $ r t1        j2                         }Y ^w xY w)z
        Assume `args` and `kwargs` are VariableTracker arguments for a call to
        this function, create new bindings for initial locals.
        z'Only supports regular Python functions.r_   r   cell_contentsN)r   r   r   r   rx   r   root_txr   r   r   rE   r   rf   co_freevarsre   r   countr   r&   r#   r4   r   r	  
ValueErrorr   DeletedVariabletrack_cell_existing)rV   r   r~   r   r   r
  r   r   r   r   cellr   cell_varclosure_cellclosure_cell_contentscontents_vars                   rZ   	bind_argszUserFunctionVariable.bind_argsz  s   
 ####!%"l+EFF--''!"gt{{D&Iffbkk2''%%+7|s477#3#3#?#?@@@@"OOtww//;;W 
 "	$OCt "==55L|#'-,t{{M:C  )3<(Q%?#2#8#8 2 24I$L (;; $?#2#8#8ASAS#TL (;;D$U#F4LE"	$H ' " ?#,#<#<#>L? " ?#,#<#<#>L?s$   *!G  G?G<;G<?HHr|   r:   r   c                     |t         v rt        j                  | |      S t        || j                  | j
                  |      S ro   )r)   r   GetAttrVariabler   r   r   rV   r|   r   s      rZ   var_getattrz UserFunctionVariable.var_getattr  s7    )),,T488b$''4;;==r\   c                 l    t        | j                  |      }t        j                  j	                  |      S ro   )r   r   r   r6   r   r   s       rZ   r   z%UserFunctionVariable.call_obj_hasattr  s+     $'))0088r\   c           
      x   | j                   t        j                  j                  u r	 |D cg c]  }|j	                          }}|j                         D ci c]  \  }}||j	                          }}}t        j                  j                  |i |j                  }	t        j                  |	      S | j                   t        j                  j                  u r  t        j                  | j                         j                  |i |}|j                  d   }t!        |t"              sS|j%                         }d|j&                   d}t)        dd| j*                   d| d| d	|g t,        j.                  
       t!        |t0              s,|j3                         }d| d}t)        d|  |d| ddg
       |j                   }t        j4                  |d      S | j6                  r't9        || j                   | j3                         ||      S |j:                  j<                  j>                  st| j                   t        j                  j@                  jB                  u rDt        j                  jD                  jG                  |      5  tH        |   |||      cd d d        S |j:                  j<                  jL                  r|j:                  j<                  jN                  sq	 ddl(m)} |h| j                   |jV                  |jX                  fv rDt        j                  jD                  jO                  |      5  tH        |   |||      cd d d        S tH        |   |||      S c c}w c c}}w # t        $ r}
t        d| d|       |
d }
~
ww xY w# 1 sw Y   xY w# tT        $ r d }Y w xY w# 1 sw Y   exY w)NzCannot convert patch_dynamo_config args/kwargs to constants. Please fix your call to patch_dynamo_config by using simpler inputs. args: z
, kwargs: 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)-r   r   _dynamopatch_dynamo_configr   r   changesr   DynamoConfigPatchVariabler1   RuntimeErrornonstrict_tracer   r   bindr~   r   r   r   rj   r   r   r   
USER_ERRORr   r   TorchInGraphFunctionVariabler   invoke_and_store_as_constantr   current_tracer,unsafe_allow_externally_visible_side_effectsutils8_disable_side_effect_safety_checks_for_current_subtracerr   2allow_externally_visible_side_effects_in_subtracerr   r   under_activation_checkpoint#allow_side_effects_under_checkpoint/torch.distributed.fsdp._fully_shard._fsdp_stater!  	Exception_pre_forward_post_forward)rV   r|   r~   r   arg
args_constkeyr   kwargs_constr$  er>   fn_vartypmsgfn_namer   r!  r   s                     rZ   r   z"UserFunctionVariable.call_function  s    77emm777BFG3c446G
GBH,,. 6>c3C//11     --;;#/'  !::7CC 77emm3333G%%dgg.33TDVDEZZ]Ff&>?((*UVYVbVbUccde 6,TZZL4&6(!L #*55	 f&:; //+@	  Ju  v <#f #27)<H0	 B99"RVWW/DGGT]]_dF 
 		((UU}}""[[\ ++^^ ? w,Rv>? ? II$$@@II,,PP!U $&&''5 * ]]//SSTVW C 70T6BC Cw$Rv66W H  7 "!F*VH6 	`? ?  ! 	!C Cs_   M0 M%M0 M*4A M0 N$N 9N0%M0 0	N9NNNN-,N-0N9Frm   Nr   )rj   rk   rl   r   r   _nonvar_fieldsclassmethodr   r[   r   r   r   r   r   r  r  rd   r   r4   r  r  r   r   __classcell__r   s   @rZ   r   r   :  s    7 	 
"	0	0N ) ))4, ">#5c?6J1K 5n>5 >S >
9)9149	9U7#U7 &U7 -	U7
 
U7 U7r\   r   c                   J     e Zd Zdd fdZed        Z	 	 	 	 	 	 	 	 ddZ xZS )BuiltinMethodVariablec                 j    t        |   di | t        |t        j                        sJ || _        y r^   )r   r[   r   r   BuiltinMethodTyper   r   s       rZ   r[   zBuiltinMethodVariable.__init__  s0    "6""e55666r\   c                     | j                   }| j                  }|t        u xr |dk(  xs t        |      t        u xr |dk(  S )N__new____contains__)r  rj   rz   type	frozenset)objmethod_selfmethod_names      rZ   is_supported_builtin_methodz1BuiltinMethodVariable.is_supported_builtin_method  sI    llll u$A	)A 
*L{n/L	
r\   c                     | j                   j                  }| j                   j                  }| j                  xr t	        | j                  d      }t        j                  |||      }|j                  ||||      S r  )r   r  rj   r   r#   r4   r   call_method)rV   r|   r~   r   rP  r   
obj_sourceobj_vts           rZ   r   z#BuiltinMethodVariable.call_function%  sf     gg&&ww[[HZZ%H
 &&r;
C!!"dD&99r\   r@  rA  r   )rj   rk   rl   r[   staticmethodrR  r   rD  rE  s   @rZ   rG  rG    sG    
 
 

:#
: &
: -	
:
 

:r\   rG  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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 )$LocalGeneratorObjectVariablerW   inline_tracerr:   c                 N    t        |   di | || _        || _        || _        y r^   )r   r[   rW   r   rZ  )rV   rW   r   rZ  r   r   s        rZ   r[   z%LocalGeneratorObjectVariable.__init__3  s+     	"6"	"*r\   c                     | j                   S ro   )rW   r   s    rZ   r   z%LocalGeneratorObjectVariable.get_code?  s    yyr\   c                 6    | j                         j                  S ro   r   r   s    rZ   r   z)LocalGeneratorObjectVariable.get_filenameB  r   r\   c                 6    | j                         j                  S ro   r   r   s    rZ   r   z%LocalGeneratorObjectVariable.get_nameE  r   r\   c                     t         ro   )r   r   s    rZ   r   z)LocalGeneratorObjectVariable.get_functionH  s    !!r\   c                      yNFr_   r   s    rZ   r  z%LocalGeneratorObjectVariable.has_selfK      r\   c                 "    | j                         S ro   )r   r   s    rZ   rj   z%LocalGeneratorObjectVariable.__name__N  s    }}r\   c                 V    | j                   j                   d| j                          dS )N(r  )r   rj   r   r   s    rZ   __str__z$LocalGeneratorObjectVariable.__str__Q  s'    ..))*!DMMO+<A>>r\   c                    ddl m} ddlm}m}m}  |j                         } ||      } ||      } ||      }	|5  |5  |	5  | j                  |      }
|
j                  s| j                  |      | _
        t        j                  | j                        j                  |       d d d        d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)Nr   )"disallow_side_effects_in_generator)r:    save_and_restart_speculation_log(temporarely_allow_writes_to_output_graph)torch._dynamo.side_effectsrh  torch._dynamo.symbolic_convertr:   ri  rj  
current_tx_get_inline_tracergenerator_exhaustedforce_unpack_var_sequenceremaining_itemsr   ListIteratorVariablereconstruct)rV   codegenrh  r:   ri  rj  r|   savedisallowtemptracers              rZ   rs  z(LocalGeneratorObjectVariable.reconstructV  s    Q	
 	
 ."--//35b97; 	V8 	VT 	V,,R0F--'+'E'Eb'I$**4+?+?@LLWU		V 	V 	V 	V 	V 	V 	V 	V 	Vs;   CC	A"B=$C	,C=CC		C	CCc                 <    | j                   j                  |||      S ro   )r   r  r   s       rZ   r  z&LocalGeneratorObjectVariable.bind_argsi  s    ww  T622r\   c                     | j                   S ro   r   r   s    rZ   r  z(LocalGeneratorObjectVariable.get_globalsl      ~~r\   c                 "    t         j                  S ro   )r   GeneratorTyper   s    rZ   r   z(LocalGeneratorObjectVariable.python_typeo  s    """r\   c                 p    ddl m} | j                  |j                  || g i       | _        | j                  S Nr   )InliningInstructionTranslator)rl  r  rZ  build_inline_tracer)rV   r|   r  s      rZ   rn  z/LocalGeneratorObjectVariable._get_inline_tracerr  s;    P%!>!R!RD"b"D !!!r\   c                 r   | j                  |      }| j                         rt        t        |       	 t	        j
                  t        dt        d   i      5  |j                         cd d d        t        dxx   t        d   z  cc<   S # 1 sw Y   nxY wnm# t        $ r}d|_	        |d }~wt        $ r  t        $ rC}t        j                  j                  j                  | j!                                t"        |d }~ww xY w	 t        dxx   t        d   z  cc<   y # t        dxx   t        d   z  cc<   w xY w)Nunimplementedinline_callT)rn  _is_generator_exhaustedr   StopIterationr   rd   r*   inline_call_r   ro  r   r    r   r"  
eval_frame	skip_coder   r   )rV   r|   rx  r;  s       rZ   next_variablez*LocalGeneratorObjectVariable.next_variable{  s   ((,'')$]B7	A H8O&PQ -**,- - _%-)@@%- - -  	)-F&G% 	 	#MM$$..t}}?"	#- _%-)@@%H_%-)@@%sM   #B B&	B BB D 	C?	B''C?<>C::C??D D6c                      yra  r_   r   s     rZ   has_unpack_var_sequencez4LocalGeneratorObjectVariable.has_unpack_var_sequence  rb  r\   rm   c                      yNTr_   r   s     rZ   has_force_unpack_var_sequencez:LocalGeneratorObjectVariable.has_force_unpack_var_sequence  s    r\   c                 B    g }| j                  ||j                         |S ro   )force_apply_to_var_sequenceappend)rV   r|   r   s      rZ   rp  z6LocalGeneratorObjectVariable.force_unpack_var_sequence  s     ((V]];r\   c                 j    	 	  || j                  |             # t        $ r t        |       Y y w xY wro   )r  r   r   )rV   r|   r   s      rZ   r  z8LocalGeneratorObjectVariable.force_apply_to_var_sequence  s>    4%%b)*  - )"-s    22c                     | j                  |      }	 |j                  |       y # t        $ r}|j                  |       Y d }~y d }~ww xY wro   )rn  _raise_exception_variabler   exception_handler)rV   r|   excrx  r;  s        rZ   _setup_exceptionz-LocalGeneratorObjectVariable._setup_exception  sH    ((,	(,,S1  	( $$Q''	(s   % 	A	AA	c                 T    | j                   d u xs | j                   j                  dk(  S )Nr   )rZ  instruction_pointerr   s    rZ   _is_generator_just_startedz7LocalGeneratorObjectVariable._is_generator_just_started  s*    !!T)XT-?-?-S-SWX-XXr\   c                 0    t        | j                  dd      S )Nro  F)r   rZ  r   s    rZ   r  z4LocalGeneratorObjectVariable._is_generator_exhausted  s    t))+@%HHr\   r|   r   r~   r   r   r   r4   c                    |dk(  r| j                  |      S |dk(  r| S |dk(  rp| j                         r-t        |      r"t        d |D              st	        t
        |       | j                  |      }|j                  |       | j                  |      S |dk(  r| j                  |      }| j                         s| j                         rd|_	        t        j                  d       S 	 | j                  |t        j                  t        d             t        j                   dk\  r5|j"                  j$                  d	k(  rd|_	        t        j                  d       S 	 | j                  |      rt	        t(        |       n|d
k(  r| j                  |      }	 | j                  |t        |      dk(  r|d   n|d          | j                  |      }t1        dt2        fi       }	 | j                  |t        j                  |d             | j                  |       t	        t(        |       |S t6        | q  ||||       y # t&        $ r d|_	        t        j                  d       cY S w xY w# t&        $ r d|_	        t        j                  d       cY S t*        $ r |j,                  J |j,                  cY S w xY w# t.        $ r  w xY w# t5        |      $ r Y |S w xY w)N__next____iter__sendc              3   \   K   | ]$  }t        |t              xr |j                  d u  & y wro   )r   r6   r   ).0r7  s     rZ   	<genexpr>z;LocalGeneratorObjectVariable.call_method.<locals>.<genexpr>  s1       s$45K#))t:KKs   *,closeTr_   )      CALL_INTRINSIC_1throwr  r0   r   __InternalThrowException)r  r  rf   allr   rx   rn  	push_manyr  ro  r   r6   r  ExceptionVariableGeneratorExitsysversion_infonext_instructionopnamer   r&  r   symbolic_resultr   rM  r4  r   r   rT  )	rV   r|   r   r~   r   rx  retvalexc_typer   s	           rZ   rT  z(LocalGeneratorObjectVariable.call_method  s    :%%b))ZKV^ ..0SY  #  -Y;,,R0FT"%%b))W_ ,,R0F..0D4P4P4R-1* 11$778%%	33M2F( $$///66:LL15F.$55d;;.%%b),\2> W_ ,,R0F
 %%bSY!^$q'aQ
 ''+F| 6bIH?%%b)*E*EhPR*ST""2&
 )v>MBdF3a ) 8-1* 11$778 ) 8-1* 11$77, .
 --999---., % N 1:  MsI   'A1H; !I& (J5 7K ;%I#"I#&%J2"J21J25K KKrt  r8   rA  )"rj   rk   rl   r   CodeTyper   r[   r   r   r   r   r  rf  __repr__rs  r  r  r   rn  r  r  builtinsboolr  r{   r4   rp  r  r  r  r  r   rT  rD  rE  s   @rZ   rY  rY  2  s    
+nn
+   78	
++'"? HV&3#"A08== tO/D 
(YIz4#z4 z4 &	z4
 -z4 
z4 z4r\   rY  c                       e Zd ZdZy)4ContextlibContextManagerLocalGeneratorObjectVariablez
    .. note::

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

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

    .. note::

        This is a wrapper around (Nested)UserFunctionVariable
    )generator_clsvtc                @    t        |   di | || _        || _        y r^   )r   r[   r  r  )rV   r  r  r   r   s       rZ   r[   z'LocalGeneratorFunctionVariable.__init__  s$     	"6"*r\   c                     || j                   j                  j                         v rt        | |      S t        | j                  |      S ro   )r   __dict__keysr   r  )rV   r   s     rZ   __getattr__z*LocalGeneratorFunctionVariable.__getattr__  s;    4>>**//114&&tww%%r\   c                 6    ddl m} |j                  || ||      S r  )rl  r  r  )rV   r|   r~   r   r  s        rZ   _build_inline_tracerz3LocalGeneratorFunctionVariable._build_inline_tracer  s$    P,@@	
 	
r\   c                    t        | j                  j                               sJ | j                  |||      }| j                  j                         }| j                  j	                         }| j                  |||| j                        S r   )r   r  r   r  r  r  r   )rV   r|   r~   r   rZ  rW   r   s          rZ   r   z,LocalGeneratorFunctionVariable.call_function  s     DGG,,.///11"dFCww!GG'')	 !!;;	 " 
 	
r\   r   )rj   rk   rl   r   rY  r4   r[   r  r  r   rD  rE  s   @rZ   r  r    sO     3		+	+&


#
 &
 -	

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

        This is only used when the function is annotated with @contextlib.contextmanager
    c                 2    t        |   |fdt        i| y )Nr  )r   r[   r  )rV   r  r   r   s      rZ   r[   z<FunctionDecoratedByContextlibContextManagerVariable.__init__  s%    	
N	
 	
r\   c                     t         |   |||      }t        |t        j                  j
                  j                        sJ d|_        |S r  )r   r  r   r   r"  symbolic_convert&InliningGeneratorInstructionTranslatoris_generator_from_ctx_manager)rV   r|   r~   r   rx  r   s        rZ   r  zHFunctionDecoratedByContextlibContextManagerVariable._build_inline_tracer  sO    
 -b$?MM**QQ
 	
 
 04,r\   )rj   rk   rl   r   r[   r  rD  rE  s   @rZ   r  r    s    
 r\   r  c                   v     e Zd ZdZ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 )UserMethodVariablez$Some unsupported user-defined methodrm   c                 6    t        |   dd|i| || _        y )Nr   r_   r   r[   rO  )rV   r   rO  r   r   s       rZ   r[   zUserMethodVariable.__init__  s    )B)&)r\   c                 h    | j                   j                   d| j                   d| j                   dS )Nre  r  r  )r   rj   r   rO  r   s    rZ   r  zUserMethodVariable.__repr__  s.    ..))*!DGG9BtxxjBBr\   c                     | j                   gS ro   rO  r   s    rZ   r   zUserMethodVariable.self_args      zr\   c                 "    t         j                  S ro   )r   
MethodTyper   s    rZ   r   zUserMethodVariable.python_type  s    r\   r|   r:   c                    ddl m}  || j                        rHg | j                         |}t	        j
                  | j                  d      }|j                  |||      S |j                  j                         rt        | j                  t        j                        ryt        | j                  dd      }||j                  d      r|dk7  s| j                  r| j                  j                  || j                  j                   ||| j                  	      S t"        d| j                  t"        j$                  j&                  u r>t	        j(                  | j                        j                  || j                  g||      S | j                  rQt        | j                  j*                  | j                  j                         }t-        ||| j/                         ||      S t0        	|   |||      S )
Nr   )is_nonstrict_trace_callableTr  rk    z	torch.nn.ztorch.nn.utils.parametrize)constant)trace_rulesr  r   r   r   r*  r   r   is_root_tracerr   rO  NNModuleVariabler   
startswithr   rT  rj   r7   FSDPParamGroupuse_training_stateTorchCtxManagerClassVariabler   r+  r   r   )
rV   r|   r~   r   r  	call_argsvarmodule_attrr   r   s
            rZ   r   z UserMethodVariable.call_function  s    	>&tww/2$..*2T2I88TC $$RF;; 99##%*HHi00+
 "$''<<K '**;7#??##xx++(($AQAQ ,   ),;;NNN99$''BPPTXX%%v  )9)9:B/BvVVw$Rv66r\   c                 &    t         |          dd  S Nr0   )r   r   r   s    rZ   r   z*UserMethodVariable.inspect_parameter_names!  s    w.044r\   r   c                     | j                   xr t        | j                   |      }|dk(  r| j                  S |dk(  r!t        j                  || j
                  |      S t        |   ||      S )Nr  __func__)r   r#   rO  r4   r   r   r   r  )rV   r|   r   r   r   s       rZ   r  zUserMethodVariable.var_getattr$  s`    >DKK!>:88O:"((TWWf==w"2t,,r\   rA  r   )rj   rk   rl   r   r[   r   r  r   r   r   r   r  rD  rE  s   @rZ   r  r    sm    .C# C ;7#;7 &;7 -	;7
 
;7z5-5 -S - -r\   r  c                   B     e Zd Zd fdZ	 	 	 	 	 	 	 	 d fdZd Z xZS )WrappedUserMethodVariablec                     |j                  dd        |j                  dd        t        |   |j                  |j                  fi | || _        || _        y )Nr   rO  )ry   r   r[   r   rO  wrappedr   rV   r  r   r   r   s       rZ   r[   z"WrappedUserMethodVariable.__init__.  sI    

4

5$W[[;F;r\   c                     | j                   j                  |       t        |   |||      }| j                   j	                  |       |S ro   r   enterr   r   exitrV   r|   r~   r   r   r   s        rZ   r   z'WrappedUserMethodVariable.call_function5  A     	2&r48"r\   c                      j                   fd         j                         j                  t        dd             y )Nc                  (      j                         S ro   r   rt  rV   s   rZ   <lambda>z7WrappedUserMethodVariable.reconstruct.<locals>.<lambda>A      gdll&; r\   r0   Fadd_push_nullr  extend_outputr   rV   rt  s   ``rZ   rs  z%WrappedUserMethodVariable.reconstruct@  4    ;<21e<=r\   rA  r   rj   rk   rl   r[   r   rs  rD  rE  s   @rZ   r  r  -  8    	#	 &	 -		
 
	>r\   r  c                   B     e Zd Zd fdZ	 	 	 	 	 	 	 	 d fdZd Z xZS )WrappedUserFunctionVariablec                 z    |j                  dd        t        |   |j                  fi | || _        || _        y )Nr   )ry   r   r[   r   r  r   r  s       rZ   r[   z$WrappedUserFunctionVariable.__init__G  s5    

4.v.r\   c                     | j                   j                  |       t        |   |||      }| j                   j	                  |       |S ro   r  r  s        rZ   r   z)WrappedUserFunctionVariable.call_functionM  r  r\   c                      j                   fd         j                         j                  t        dd             y )Nc                  (      j                         S ro   r  r  s   rZ   r  z9WrappedUserFunctionVariable.reconstruct.<locals>.<lambda>Y  r  r\   r0   Fr  r   s   ``rZ   rs  z'WrappedUserFunctionVariable.reconstructX  r  r\   rA  r   r  rE  s   @rZ   r  r  F  s8    	#	 &	 -		
 
	>r\   r  c                     d }|D cg c]
  } ||       }}|j                         D ci c]  \  }}| ||       }}} ||i |}	| j                  j                  |	|t        |            S c c}w c c}}w )Nc                 v    t        | t        j                        r| j                         S | j	                         S ro   )r   r   TensorVariableget_real_valuer   )xs    rZ   convertz-invoke_and_store_as_constant.<locals>.convert_  s0    a112##%%##%%r\   r   )r   r   register_attr_or_moduler$   )
r|   r   r   r~   r   r  r  r   r   ress
             rZ   r+  r+  ^  s    &
 !%%1GAJ%D%(.71am7F7
d
f
C99,,d# -   &7s
   A1A6c                        e Zd Zdhej                  Z	 d	 d fdZd Zd Zd Zd Z	ddd	e
d
e
fdZ fdZd Z fdZd Zd Zd ZddZ xZS )r   r   c	                    |	j                  d      |	j                  t                      t        
|   di |	 t        |j                         t              sJ t        |j                         t        j                        sJ t        |t              sJ || _        || _        || _        || _        || _        || _        || _        || _        y )Nmutation_typer  r_   )rq   updater2   r   r[   r   r   r   r   r  rd   r?  rW   r   ra   rc   r   r   
wrapped_fn)rV   r?  rW   r   ra   rc   r   r   r  r   r   s             rZ   r[   z#NestedUserFunctionVariable.__init__t  s     ::o&.MM(<(>M?"6"'446<<<$113U^^DDD)T***	" $&5?r\   c                     g S ro   r_   r   s    rZ   r   z$NestedUserFunctionVariable.self_args  r   r\   c                 6    | j                   j                         S ro   )rW   r   r   s    rZ   r   z#NestedUserFunctionVariable.get_code  s    yy++--r\   c                 "    t         j                  S ro   r   r   s    rZ   r   z&NestedUserFunctionVariable.python_type  r  r\   c                 D   | j                   rt        t        j                  | j                  j                         | j                  | j                  j                               }| j                  r| j                  j                         |_	        | j                  r| j                  j                         |_        | j                  rZ| j                  j                         }t        |t              rddlm} t#         ||            }t        |t"              sJ ||_        |S )Nr   r   )r   r   r   r   rW   r   r   r?  ra   r`   rc   rb   r   r   rz   r   r   rd   r   )rV   rC   r   r   s       rZ   r   z'NestedUserFunctionVariable.get_function  s    <<%%!!II((*NNLL++-

 == $ @ @ BD??"&//"D"D"FD**==?K+u-."8K#89 k4000#.D r\   r|   r:   name_varr   c                 z    |j                   j                  j                  | |j                  |       t	        d       S ro   )r   r   
store_attrr   r6   )rV   r|   r  r   s       rZ   call_setattrz'NestedUserFunctionVariable.call_setattr  s/     			))$D%%r\   c                 X    |dk(  r | j                   |g| S t        | 	  ||||      S )N__setattr__)r  r   rT  rV   r|   r   r~   r   r   s        rZ   rT  z&NestedUserFunctionVariable.call_method  s9    = $4$$R/$//w"2tT6::r\   c                     | j                   d uS ro   )r   r   s    rZ   has_closurez&NestedUserFunctionVariable.has_closure  s    ||4''r\   c                 b    |dk(  r| j                   j                         S t        |   ||      S )Nrj   )r?  r   r   const_getattr)rV   r|   r   r   s      rZ   r&  z(NestedUserFunctionVariable.const_getattr  s0    :<<2244w$R..r\   c                      yra  r_   r   s    rZ   r  z#NestedUserFunctionVariable.has_self  rb  r\   c                     | j                   S ro   r{  r   s    rZ   r  z&NestedUserFunctionVariable.get_globals  r|  r\   c                 H   | j                         }t        j                  || j                  | j                  j                         | j                  rt        | j                  j                        nd t        d t        t        | j                         j                              D                    }| j                  r| j                  j                         |_         t        j                   |      j"                  |i |}|j%                          t'        |j(                  j                               }t+        |j,                  j.                  |       t1        |||       t3        |j                        D ])  \  }}	|	|vsJ | j4                  j                  |   }
|
||	<   + |S )Nc              3   2   K   | ]  }t        d         y wro   )r/   )r  _s     rZ   r  z7NestedUserFunctionVariable.bind_args.<locals>.<genexpr>  s     Sa)D/Ss   )r   r   r   r   r?  r   ra   rz   r   rg   rf   r  rc   keys_as_python_constantrb   r   r   r(  apply_defaultsrd   	argumentsr   r   r
  r   rv   r   )rV   r   r~   r   rW   rC   r>   r   r   r   r  s              rZ   r  z$NestedUserFunctionVariable.bind_args  sH   }}!!NNLL++-*.--E$--%%&TS5T]]_5P5P1Q+RSS
 ??"&//"I"I"KD,!!$',,d=f=eoo++-...7ffd+"4#3#34 	 ICv%%%<<%%c*DF4L	 
 r\   c                 ^   j                  fd        | j                         j                  j                  | j                        g        t        j                  | j                  j                  j                               | j                  r | j                         n!j                  j                  d       g       | j                  r | j                         n!j                  j                  d       g       | j                  r | j                         n!j                  j                  d       g       | j                  r=	 | j                  j                         }j                  j                  |      g       n!j                  j                  d       g       j                  t!        dd             | j"                  rvj                  fd        | j"                         j                  t!        dd             j                  t%        d             j                  t!        dd             dd	lm}  |j*                         }|j,                  j.                  j1                  |       r}|j,                  j.                  j2                  |    j5                         D ]H  \  }}j7                           |       j                  t%        d             j9                  |       J y y # t        $ r  | j                         Y uw xY w)
Nc                  0     j                  t        d      S )Nr   )load_import_fromrj   rt  s   rZ   r  z8NestedUserFunctionVariable.reconstruct.<locals>.<lambda>  s    G,,X7JK r\      Fc                  (     j                  dd      S )N	functoolswrapsr1  r2  s   rZ   r  z8NestedUserFunctionVariable.reconstruct.<locals>.<lambda>  s    00gF r\   r0   r   Tr   r9   )r  rW   r  create_load_const_uncheckedr   r6   r   r   r   ra   create_load_constr   rc   r   r   r   r   r  r   rl  r:   rm  r   r   has_pending_mutationstore_attr_mutationsr   dup_topr  )rV   rt  r   r:   r|   r   r   s    `     rZ   rs  z&NestedUserFunctionVariable.reconstruct  s   K	
 			wBB4>>RST ''		(?(?@A==DMM"!!7#<#<T#B"CD<<DLL!!!7#<#<T#B"CD??DOO$!!7#<#<T#B"CD*"..AAC%%88EF !!7#<#<T#B"CD21e<=??!!F DOO$!!"6q%"@A!!,q/2!!"6q$"?@ 	I-"--/99!!66t<!yy55JJ eg)e !%%l1o6""4() =) ' *(()*s   ;L L,+L,ro   rA  r  )rj   rk   rl   r   rB  r[   r   r   r   r   r4   r  rT  r$  r&  r  r  r  rs  rD  rE  s   @rZ   r   r   n  s    	!	0	0N  @ 
@:."0&#& "& 	&;
(/
08)r\   r   c                   B     e Zd Zd fdZ	 	 	 	 	 	 	 	 d fdZd Z xZS )!WrappedNestedUserFunctionVariablec           
         |j                  dd        |j                  dd        |j                  dd        |j                  dd        |j                  dd        |j                  dd        |j                  dd        |j                  dd        t        | 	  |j                  |j                  |j
                  |j                  |j                  |j                  |j                  |j                         || _        || _        y )	Nr?  rW   r   ra   rc   r   r   r  )ry   r   r[   r?  rW   r   ra   rc   r   r   r  r  r   r  s       rZ   r[   z*WrappedNestedUserFunctionVariable.__init__"  s    

9d#

64 

;%

:t$

<&

=$'

9d#

<&OOLLOO		
 r\   c                     | j                   j                  |       t        |   |||      }| j                   j	                  |       |S ro   r  r  s        rZ   r   z/WrappedNestedUserFunctionVariable.call_function8  r  r\   c                      j                   fd         j                         j                  t        dd             y )Nc                  (      j                         S ro   r  r  s   rZ   r  z?WrappedNestedUserFunctionVariable.reconstruct.<locals>.<lambda>D  r  r\   r0   Fr  r   s   ``rZ   rs  z-WrappedNestedUserFunctionVariable.reconstructC  r  r\   rA  r   r  rE  s   @rZ   r>  r>  !  s8    ,	#	 &	 -		
 
	>r\   r>  c                        e Zd Zddhej                  Zdd fdZd Zed        Z	 	 	 	 	 	 	 	 ddZ	dd	Z
ddd
efdZ xZS )SkipFunctionVariabler   reasonc                 @    t        |   di | || _        || _        y r^   )r   r[   r   rE  )rV   r   rE  r   r   s       rZ   r[   zSkipFunctionVariable.__init__P  s!    "6"
r\   c                     | j                   S ro   r   r   s    rZ   r   z'SkipFunctionVariable.as_python_constantU  s    zzr\   c                 |    t        |      s(t        |j                  t        j                                | ||      S r   )r-   r"   r   r!   FUNCTION_MATCHr   s      rZ   r   z'SkipFunctionVariable.create_with_sourceX  s2    .u5 &++L,G,GHI5((r\   r|   r:   c           	      "   t        j                  | j                  dd      rVt        j                  | j                  dd       }t        dt	        | j                        d| j                   d| ddg	       y | j                  t
        j                  j                  u r?|j                  d
d       }|r|j                         }t        dd| d| dd| dg	       y | j                  t
        j                  j                  u r2|j                  d
d       }|r|j                         }t        d|       t        j                  rEddlm} |j!                  || j                        }t#        |t$              s|j'                  |||      S t)        | j                  dd      }	t)        | j                  dd       }
|
dn
t	        |
      }	 t        j*                  | j                        }d|	 d| d}d|	 dg}d|vr|d|	 dd gz  }|	d/k(  rd0}g }| j6                  r| j6                  nd1}t        d2d3| d4|	 d5| ||	       y # t,        $ r d!d"h}|
|v rd#| d$|	 d}d%d&g}n|
\|
j/                  d'      rKd(| d$|	 d$}d)g}t
        j                  j0                  j3                  |d*z   d*j5                  |      z          nKd+| d$|	 d,}d-d.g}t
        j                  j0                  j3                  |d*z   d*j5                  |      z          Y w xY w)6N_torchdynamo_disableF_torchdynamo_disable_msgz1Skip calling `torch.compiler.disable()`d functionzSkip calling function `z>` since it was wrapped with `torch.compiler.disable` (reason: r  z(Remove the `torch.compiler.disable` callr   r>  z%Call to `torch._dynamo.graph_break()`z0Called `torch._dynamo.graph_break()` with args `z`, kwargs ``z$User-inserted graph break. Message: z.Remove the `torch._dynamo.graph_break()` call.z9Skip frame due to `torch._dynamo.skip_frame()`. Message: r0   SourcelessBuilderrl   z<unknown qualname>rk   z<unknown module>z?Dynamo developers have intentionally marked that the function `z` in file `z` should not be traced.zAvoid calling the function `z`.r"  z:Apply `@torch._dynamo.dont_skip_tracing` to the function `zw` to force tracing into the function. More graph breaks may occur as a result of attempting to trace into the function.z Please file an issue to PyTorch._abc	_warningsz6Dynamo does not know how to trace the Python builtin `.zIf you are attempting to call a logging function (e.g. `_warnings.warn`), you can try adding it to `torch._dynamo.config.reorderable_logging_functions`.zKPlease file an issue on GitHub so the PyTorch team can add support for it. optreez*Dynamo cannot trace optree C/C++ function zi Consider using torch.utils._pytree - https://github.com/pytorch/pytorch/blob/main/torch/utils/_pytree.py
z/Dynamo does not know how to trace the builtin `z.` This function is either a Python builtin (e.g. _warnings.warn) or a third-party C/C++ Python extension (perhaps created with pybind).zIf it is a Python builtin, please file an issue on GitHub so the PyTorch team can add support for it and see the next case for a workaround.a  If it is a third-party C/C++ Python extension, please either wrap it into a PyTorch-understood custom operator (see https://pytorch.org/tutorials/advanced/custom_ops_landing_page.html for more details) or, if it is traceable, use `torch.compiler.allow_in_graph`.allow_in_graphzFound an allow_in_graph decorator to a function which is created inside the parent function that is getting compiled. This is not supported for now.z<missing reason>z,Attempted to call function marked as skippedzmodule: z, qualname: z, skip reason: )r   r   r   r   r   r   r"  graph_breakrq   r   
skip_framer   r   dont_skip_tracingbuilderrP  r   r   rD  r   r   getfilerx   r  r.  	warn_oncejoinrE  )rV   r|   r~   r   r>  graph_break_msgskip_frame_msgrP  
rebuilt_fnqualname	module_ormodule_namepathr   r   known_python_builtin_modulesrE  s                    rZ   r   z"SkipFunctionVariable.call_functiona  s    !!$**.DeL((5OQUVCKDJJ5djj\ B::=aA ? ZZ5==444$jj5O"1"D"D"F?J4&P[\b[ccdeB?BSTD	 ZZ5==333#ZZt4N!/!B!B!DKNK[\  ''6 /55b$**E
!*.BC%33BfEEtzz>;OPH

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

7D!

8T"Hw~~HHr\   c                     | j                   j                  |       t        |   |||      }| j                   j	                  |       |S ro   r  r  s        rZ   r   z)WrappedSkipFunctionVariable.call_function  r  r\   c                      j                   fd         j                         j                  t        dd             y )Nc                  (      j                         S ro   r  r  s   rZ   r  z9WrappedSkipFunctionVariable.reconstruct.<locals>.<lambda>  r  r\   r0   Fr  r   s   ``rZ   rs  z'WrappedSkipFunctionVariable.reconstruct  r  r\   rA  r   r  rE  s   @rZ   rj  rj    r  r\   rj  c                   N     e Zd ZdZd fdZd fdZd Z	 	 	 	 	 	 	 	 ddZ xZS )	WrapperUserFunctionVariablea3  
    Used to represent a wrapper object that contains the actual callable as an
    attribute. For example, torch.jit.script/trace have the original function at
    their _torchdynamo_inline attribute. Similarly, functions with
    __script_if_tracing_wrapper have the original attr at "__original_fn".
    c                 @    t        |   di | || _        || _        y r^   )r   r[   wrapper_objattr_to_trace)rV   rr  rs  r   r   s       rZ   r[   z$WrapperUserFunctionVariable.__init__  s#    "6"&*r\   c                     || j                   k(  r[t        | j                  | j                         }| j                  xr t	        | j                  |      }t        j                  |||      S t        | !  ||      S ro   )	rs  r   rr  r   r#   r4   r   r   r  )rV   r|   r   r   r   r   s        rZ   r  z'WrapperUserFunctionVariable.var_getattr	  si    4%%%$**D,>,>?C[[BZT%BF"((S&99w"2t,,r\   c                     g S ro   r_   r   s    rZ   r   z%WrapperUserFunctionVariable.self_args  r   r\   c                    t        | j                  d      rnt        | j                  | j                  d       }t        |dd      xs d}|j	                  dd      d   dk7  r"d	}t
        j                  j                  j                  |       t
        j                  j                  j                  j                  d
      }|j                  t        j                        rt
        j                  j                  j                         }t!               |z   }|d   j"                  |d   j$                  f}	dj'                  t)        j*                  |            }
d|	d    d|	d    d}|t-        |
      z  }|j/                  |       | j1                         |z   }t3        j4                  t6        j8                        j;                  || t3        j<                  | j                        g||      S )N
cache_infork   r  rS  r0   )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: :rU  )r   rr  r   rs  splitr   r"  r.  r\  logging	getLoggerisEnabledForDEBUG_guardsTracingContextextract_stackr   filenamelinenor]  	tracebackformat_listr   debugr   r   r   r   getattr_and_tracer   r6   )rV   r|   r~   r   	target_fnrc  r>  dynamo_logger
user_stack	frame_locuser_stack_formatteduser_stack_traceall_argss                rZ   r   z)WrapperUserFunctionVariable.call_function  s    4##\2 0 0$2D2DdKI!)\2>D"K  q 1!4?K  ##--c2 % 3 3 ; ; E Eo V --gmm<!&!=!=!K!K!MJ!7!9J!FJ!+B!8!8*R.:O:O PI+-7793H3H3T+U()RS\]^S_R``abklmbnaooq'r$$,@(AA$!''(89>>#d*--''

-9--d.@.@AMHM
	
r\   rA  rh  r   )	rj   rk   rl   r   r[   r  r   r   rD  rE  s   @rZ   rp  rp    sB    +
-&
#&
 &&
 -	&

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

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

    In the case that a function is 'remappable' but only for some combinations of call-time arguments,
    we check the args at `call_function` time and fall back to graph-breaking if needed.  This is no worse
    than status-quo as we currently graph-break on all distributed.* collectives.
    c                X    t        |   |fi | t        |t              sJ || _        y ro   )r   r[   r   r   replacement_var)rV   r   r  r   r   s       rZ   r[   z*CollectiveFunctionRewriteVariable.__init__k  s.    &v&/+?@@@.r\   c                 j    t         j                  | |      \  }}t        |ft        |fd|i||d|S )Nr   )r  r   )r  rewriter   )r|   old_fnr   optionsnew_fn
new_sources         rZ   r   z(CollectiveFunctionRewriteVariable.createp  sM    >FFr6R
0
0V
VgV
 	
 	
r\   c                 H    t        j                  |       xr | t               v S ro   )r   
isfunctionr  )variables    rZ   can_rewritez-CollectiveFunctionRewriteVariable.can_rewritez  s%     x(WX9U9W-W	
r\   c                 8    t               |   }|t        | |      fS ro   )r  r  )r|   r   r  s      rZ   r  z)CollectiveFunctionRewriteVariable.rewrite  s"    -/34R@@@r\   c                    dd l m} ddlm} t	        j
                  | j                        }t         |j                  |i |j                        }d}d|v rQ|d   j                         r>t        d| j                   d|d|d| j                   g t        j                  	       | j                  |j                  |j                  |j                   fv ro|j#                  d
      }||j$                  n|j&                  d
   j(                  }||vrt+        d|       t,        j.                  j1                  ||         |d
<   | j2                  j5                  |||      S )Nr   )REDUCE_OP_TO_STRr_   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   rd   r(  r.  r   r   r   SUPPORTABLE
all_reducereduce_scatter_tensor_reduce_scatter_baserq   r   r   defaultr  r   r6   r   r  r   )	rV   r|   r~   r   distr  r   reduce_op_var	reduce_ops	            rZ   r   z/CollectiveFunctionRewriteVariable.call_function  sj    	)N %%dgg.	ninnd5f5??@F:$6$I$I$KC77)8dWJvi8QRVRYRYQZ[&22	 77OO&&%%
 

 #JJt,M !, ##))$/77 
  00 #>yk!JKK$55<< +F4L ##11"dFCCr\   rA  rh  r   )rj   rk   rl   r   r[   rW  r   r  r  r   rD  rE  s   @rZ   r  r  `  s{    /
 
 
 
 

 A A,D#,D &,D -	,D
 
,Dr\   r  c                   0     e Zd Z	 	 	 	 	 	 	 	 d fdZ xZS )FunctoolsWrapsVariablec                 |    |s(t              dk(  rfd}t        j                  |      S t        |   ||      S )Nr0   c                     t        | t        j                        r| j                  d         S t	        d|  dg t
        j                         y )Nr   )r  zfunctools.wrapsz]`torch.compile` can't trace `functools.wraps` on functions defined outside the compile regionr   )r   r   r   cloner   r   r  )r   r~   s    rZ   r6  z3FunctoolsWrapsVariable.call_function.<locals>.wraps  sN    b)"F"FG88tAw877 -!d *66	r\   )rf   r   LambdaVariabler   r   )rV   r|   r~   r   r6  r   s     `  rZ   r   z$FunctoolsWrapsVariable.call_function  s>     #d)q.
 ++E22w$Rv66r\   r   )rj   rk   rl   r   rD  rE  s   @rZ   r  r    s3    7#7 &7 -	7
 
7 7r\   r  c                   *    e Zd Zd Z	 	 	 	 	 	 	 	 ddZy)CollectionsNamedTupleFunctionc                     | j                   S ro   r   r   s    rZ   r   z0CollectionsNamedTupleFunction.as_python_constant  r   r\   c                 z   t        ||      }|r} | j                  |D cg c]  }|j                          c}i |j                         D ci c]  \  }}||j                          c}}}t	        j
                  |t                     S t        dd|d|dg t        j                         y c c}w c c}}w )Nr  znamedtuple constructionzargs=r  z?`torch.compile` only support certain input types for namedtupler   )
r'   r   r   r   r   UserDefinedClassVariabler3   r   r   r  )	rV   r|   r~   r   constant_argsr  r   r   r   s	            rZ   r   z+CollectionsNamedTupleFunction.call_function  s     ,D&9DGG267Q!&&(79?HA1a**,,HE 55%5%7  	-tgZy)Y"..		
 8Hs   B2B7Nr   )rj   rk   rl   r   r   r_   r\   rZ   r  r    s2    
#
 &
 -	

 

r\   r  c                        e Zd Zdeddf fdZd ZddZd Z	 	 	 	 	 	 	 	 dd
Zdd	de	defdZ
dd	de	fdZd Zd Z xZS )FunctoolsPartialVariablerC   rm   Nc                     t        |   di | || _        t        |t              sJ || _        t        |t              sJ || _        t        j                  t              | _        y r^   )r   r[   rC   r   r{   r~   rd   keywordsr5  partialr+   
fake_value)rV   rC   r~   r  r   r   s        rZ   r[   z!FunctoolsPartialVariable.__init__  s\    "6"	$%%%	(D)))  $++H5r\   c                 "    t         j                  S ro   )r5  r  r   s    rZ   r   z$FunctoolsPartialVariable.python_type  s       r\   c                 <   j                  fd        | j                         | j                  rj                  | j                         | j                  s2j                  t        t        | j                        dz   d             y j                  | j                  j                                t        | j                  j                               }j                  j                  t        |      t        | j                        z   dz   |d             y )Nc                  (     j                  dd      S )Nr5  r  r7  r2  s   rZ   r  z6FunctoolsPartialVariable.reconstruct.<locals>.<lambda>  s    g&>&>{I&V r\   r0   F)r  rC   r~   foreachr  r  r   rf   r  rz   r  create_call_function_kw)rV   rt  r  s    ` rZ   rs  z$FunctoolsPartialVariable.reconstruct  s    VW		99OODII&}}!!"6s499~7I5"QR,,./T]]'')*++CIDII,F,JDRWX	
r\   c                 "    | j                         S ro   r   r   s    rZ   r   z%FunctoolsPartialVariable.get_function	      &&((r\   r|   r:   c                 z    | j                   |z   }i | j                  |}| j                  j                  |||      S ro   )r~   r  rC   r   )rV   r|   r~   r   merged_argsmerged_kwargss         rZ   r   z&FunctoolsPartialVariable.call_function  s>     ii$&34==3F3yy&&r;FFr\   r   c                     t         j                  j                  t        t	        j
                  t              |            S ro   )r   r6   r   r   r5  r  r+   r  s      rZ   r   z)FunctoolsPartialVariable.call_obj_hasattr  s2     ))00I%%h/6
 	
r\   c                    | j                   xr t        | j                   |      }|dk(  r| j                  S |dk(  r!t        j                  | j
                  |      S |dk(  rW| j                  j                         D ci c]  \  }}t        j                  |      | }}}t        j                  ||      S |t        v rt        j                  | |      S t        t        |       y c c}}w )NrC   r~   r   r  )r   r#   rC   r   ListVariabler~   r  r   r6   r   ConstDictVariabler)   r  r   r   )rV   r|   r   r   r   r   r   s          rZ   r  z$FunctoolsPartialVariable.var_getattr  s    >DKK!>6>996>))$))FCC:?C}}?R?R?TUtq!%,,Q/2UEU..uVDD)),,T488 4	 Vs   > C'c                 :   t        j                  | j                  j                         g| j                  D cg c]  }|j                          c}i | j
                  j                         D ci c]  \  }}||j                          c}}S c c}w c c}}w ro   )r5  r  rC   r   r~   r  r   )rV   r7  r   r   s       rZ   r   z+FunctoolsPartialVariable.as_python_constant,  s      II((*
26))<3c$$&<
 6:]]5H5H5JKTQq!&&((K
 	
<K   B
1Bc                 :   t        j                  | j                  j                         g| j                  D cg c]  }|j                          c}i | j
                  j                         D ci c]  \  }}||j                          c}}S c c}w c c}}w )zcSimilar to as_python_constant(), but add ID_MATCH guards to try to force things to become constants)r5  r  rC   guard_as_python_constantr~   r  r   )rV   r   r   s      rZ   r  z1FunctoolsPartialVariable.guard_as_python_constant3  s      II..0
48II>qa((*>
 <@==;N;N;PQ41aq!,,..Q
 	
>Qr  r  r   )rj   rk   rl   r4   r[   r   rs  r   r   r   r   r  r   r  rD  rE  s   @rZ   r  r    s    	6_ 	64 	6!
)G#G &G -	G
 
G
)
14
	
55 5S 5

r\   r  c                   
    e Zd Zdddhej                  Zeej                  dee	de
f   ej                  f   fd              Zed        Zdeddf fd	Zedefd
       Zd Zd Z	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZd Z xZS )PolyfilledFunctionVariabler   r  traceable_fnrm   .c                     i S ro   r_   )r   s    rZ   _get_polyfill_handlersz1PolyfilledFunctionVariable._get_polyfill_handlersD  s	     	r\   c                 f    t        |j                  t        j                                | ||      S r   )r"   r   r!   rJ  r   s      rZ   r   z-PolyfilledFunctionVariable.create_with_sourceI  s(    f''(C(CDE5((r\   Nc                 *   t        |   di | || _        | j                         j	                  ||      }t        |      sJ d| d|        dD ]"  }t        ||d       }|st        |      sJ |} n t        d| d      || _        || _	        y )NzPolyfill handler z is not callable for )__torch_dynamo_polyfill____python_implementation__z# does not have a traceable functionr_   )
r   r[   r   r  rq   callabler   r&  r  r  )rV   r   r   handlercandidate_attr	candidater  r   s          rZ   r[   z#PolyfilledFunctionVariable.__init__O  s    "6"--/33B; X$5gY>STVSW"XX 
 	N  >I	***(	 #G9,OP  & ,r\   c                     | j                   S ro   )r  r   s    rZ   polyfill_fnz&PolyfilledFunctionVariable.polyfill_fnf  s       r\   c                 0    t        | j                  dd      S )N*__torch_dynamo_can_constant_fold_through__F)r   r  r   s    rZ   can_constant_fold_throughz4PolyfilledFunctionVariable.can_constant_fold_throughj  s    OOI5
 	
r\   c                 "    | j                         S ro   r  r   s    rZ   r   z'PolyfilledFunctionVariable.get_functiono  r  r\   c                 .   | j                         rt        ||      rt | j                  |D cg c]  }|j                          c}i |j	                         D ci c]  \  }}||j                          c}}}t        j                  ||      S | j                  t        j                  u r&t        |      dk(  r|st        |d   t        j                  t        j                  f      rt        d |d   j                  D              rt        j                  j!                  ||j"                  j%                  dt&        j(                  t+        d |d   j                  D              fi       t'        j(                  |d   j                  D cg c]4  }t        |t        j,                        r|j.                  n|j0                  6 c}            S t        j                  || j2                        }|j5                  |||      S c c}w c c}}w c c}w )Nr0   r   c              3      K   | ]n  }t        |t        j                        xr t        |j                  t              xs2 t        |t        j
                        xr |j                         t        u  p y wro   )r   r   r6   r   intSymNodeVariabler   )r  r  s     rZ   r  z;PolyfilledFunctionVariable.call_function.<locals>.<genexpr>  se        Ay99:Wz!''SV?W Yq)";";<WTWAWYs   A4A6r   c              3   <   K   | ]  }|j                           y wro   )as_proxy)r  as     rZ   r  z;PolyfilledFunctionVariable.call_function.<locals>.<genexpr>  s     ?A1::<?s   )sym_num)r  r(   r   r   r   r4   r   r  sumrf   r   r   r  TupleVariabler  r  r   r   create_proxyr   sym_sumrz   r6   r   r  r  r   )	rV   r|   r~   r   r  r   r   r   traceable_function_variables	            rZ   r   z(PolyfilledFunctionVariable.call_functionr  s    ))+0M&1
 6:;a**,;=C\\^LTQq!..00L  #((V44 GGx||#D	Q47Y%;%;Y=T=T$UV  a  ,,33		&&#MM?a??A	  "&a   *!Y-G-GH GG!"+	 4  ( '6&;&;B@Q@Q&R#*88T6JJO <L4s   HH9Hc                 b   |dk(  r| j                  |||      S t        | j                  |d       }|J d| d| j                          t        |      sJ d| d| j                          i }| j                  rt        | j                  |      |d<   t        |fi |}|j                  |||      S )N__call__zMember z not found in z is not callable in r   )r   r   r   r,   r   r#   r  )rV   r|   r   r~   r   methodr  polyfilled_method_variables           rZ   rT  z&PolyfilledFunctionVariable.call_method  s     :%%b$77$-!JWTF.	#JJ!6"QgdV3Gy$QQ";; *4;; =GH%?%R'%R")77D&IIr\   c                     | j                   S ro   r   r   s    rZ   r   z-PolyfilledFunctionVariable.as_python_constant  r   r\   r   r~   r   r   r   rm   r4   )rj   rk   rl   r4   rB  rC  r5  cacherd   r   r   r   r   r  r   r=   r[   propertyr  r  r   r   rT  r   rD  rE  s   @rZ   r  r  <  s    
	'	'	N __tHS#X,>@R@R,R'S    ) )
-2 -D -. !R ! !

)2K#2K &2K -	2K
 
2KhJ &	J
 -J 
J&r\   r  c                       e Zd Zd Zy)TracebackVariablec                      y ro   r_   r   s       rZ   r   zTracebackVariable.call_function  s    r\   Nrj   rk   rl   r   r_   r\   rZ   r  r    s    2r\   r  c                   0     e Zd Z fdZd Zd Zd Z xZS )SysFunctionVariablec                 2    t        |   di | || _        y r^   )r   r[   r   )rV   r   r   r   s      rZ   r[   zSysFunctionVariable.__init__  s    "6"
r\   c                 h   t        |j                        rK|j                  d   }|j                  }d }t        j                  ||      |t        j                  ||      g}n>t        j                  d       t        j                  d       t        j                  d       g}t        j                  |      S )Nry  )rf   exn_vt_stackr  r4   r   r   r6   r   )rV   r|   exnr=  tbr   s         rZ   exc_infozSysFunctionVariable.exc_info  s    r//"%C,,CB%%b#.%%b"-E **40**40**40E
 &&u--r\   c                 >    | j                  |      j                  d   S r  )r  r   r   s     rZ   	exceptionzSysFunctionVariable.exception  s    }}R &&q))r\   c                     | j                   t        j                  u r| j                  |      S | j                   t        j                  u sJ | j                  |      S ro   )r   r  r  r  r   s       rZ   r   z!SysFunctionVariable.call_function  sF    ::%==$$zzS]]***~~b!!r\   )rj   rk   rl   r[   r  r  r   rD  rE  s   @rZ   r  r    s    .$*"r\   r  ) create_tma_experimental_metadatacreate_tma_stable_metadataTMADescriptorMetadataTritonHOPifierc                       e Zd ZdedefdZdedefdZdedefdZ	de
ej                  j                  j                  df   fd	Zd
 Zd Zd Zd ZdedefdZdddee   ddfdZdefdZy)DynamoTritonHOPifierr>  rm   c                     t        |      ro   )r    )rV   r>  s     rZ   raise_unsupportedz&DynamoTritonHOPifier.raise_unsupported  s    #r\   maybe_callablec                 .    t        |t        t        f      S ro   )r   r   r   )rV   r#  s     rZ   is_callablez DynamoTritonHOPifier.is_callable  s    79MN
 	
r\   r   c                     |j                   S ro   rH  )rV   r   s     rZ   	get_valuezDynamoTritonHOPifier.get_value  s    yyr\   .c                     ddl m} t        ||      r|j                         S t	        ddt        |       dg t        j                         y )Nr0   )BaseListVariablez/unsupported grid type for triton hop check_gridzgrid type = z;`torch.compile` only supports list-like grid for check_gridr   )listsr)  r   r  r   rM  r   r  )rV   gridr)  s      rZ   
check_gridzDynamoTritonHOPifier.check_grid  sK    +d,-==?"I&tDzl3Y&22	r\   c                     |j                         D ci c]%  \  }}t        j                  j                  |      |' }}}|j	                  ||gi       }|S c c}}w ro   )r   r   r6   r   r   )rV   r+  metar|   r   r   s         rZ   	call_gridzDynamoTritonHOPifier.call_grid  sV    DHJJLQDAq	**11!4a7QQ!!"tfb1 Rs   *Ac                 \    ddl m} |j                  ||      }|j                  |||      }|S )Nr0   rO  )rZ  rP  r   r   )	rV   user_fnr~   r   r|   r  rP  wrapped_user_functionr   s	            rZ   call_user_defined_fnz)DynamoTritonHOPifier.call_user_defined_fn  s1    . 1 8 8W E&44RvFr\   c                 l    ddl m}  ||t        |j                  |             j	                  |      }|S )Nr0   )VariableBuilder)rZ  r5  r#   kernel_source_wrap)rV   user_objr|   r  r   r5  wrapped_user_objs          rZ   wrap_user_defined_objz*DynamoTritonHOPifier.wrap_user_defined_obj  s7    ,*
811dV=

%/ 	  r\   c                 n    |j                  |      }|D cg c]  } |j                          }}|S c c}w ro   )unpack_var_sequencer  )rV   configsr|   r   s       rZ   maybe_unpack_configsz)DynamoTritonHOPifier.maybe_unpack_configs  s@    --b1 DKK26224KK Ls   2r   c                 d    |j                         s| j                  d       |j                         S )Nz`@triton.heuristics must return constant values because configs can only contain constant values.)is_python_constantr"  r  )rV   r   s     rZ   maybe_unpack_heuristic_resultz2DynamoTritonHOPifier.maybe_unpack_heuristic_result$  s0    ((*""r ..00r\   r  TritonKernelVariabler~   c                     |j                   t        |      dk7  r| j                  d        t        |      |j                  |j
                  |d   |j                        S )Nr0   z7Triton kernels should be called with only a single gridr   )kernel
kernel_idxr+  r6  )r+  rf   r"  rM  rD  rE  r   )rV   r  r~   s      rZ   call_getitemz!DynamoTritonHOPifier.call_getitem.  s\     ==$D	Q""I tH~??**a"//	
 	
r\   c                 $   ddl m} ddlm} i }t	        |j                               D ]D  }||   }	t        |	t        t        f      s|	j                         ||<   |	j                         ||<   F |j                         D 	ci c]%  \  }}	t        j                  j                  |      |	' }
}}	ddlm}m} |j                         D 	ci c]"  \  }}	t        |	|      r||	j#                         $ }}}	|
j                         D 	ci c]  \  }}	t        |	|      s||	 }}}	|j%                         D ][  }	|	j'                         }	t        |	t        j(                  t        j*                  f      r>| j-                  dt/        |	       d       ] |j1                  |      } ||t2              }|j4                  j7                  d|d	|j8                  ||||j;                         d
       t        j                  d       S c c}	}w c c}	}w c c}	}w )Nr0   r5   )r  r   )kernel_side_tabletriton_kernel_wrapper_mutationz.Unexpected argument type for a Triton kernel: rS  r   r_   )rE  constant_args_idxr+  tma_descriptor_metadatar   )r  r6   dictsr  r{   r  r   !TMADescriptorExperimentalVariableTMADescriptorStableVariableto_metadata
get_tensorr   r   r   *torch._higher_order_ops.triton_kernel_wraprH  rI  r   r  realizer  r  r"  repradd_constant_argsrd   r   r  rE  r  )rV   r  gridscombined_args_rawr|   r6   r  rK  r   r   combined_argsrH  rI  r  non_constant_argsrJ  r.  s                    rZ   call_HOPzDynamoTritonHOPifier.call_HOP@  s   ., :<',,./ 	6A!!$A57RS ./]]_'*'(||~!!$	6 *//1
1 &&--a0!3
 

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


s   *H 
'HHN)rj   rk   rl   r   r   r"  r   r  r%  r'  rz   r   fxproxyProxyr,  r/  r3  r:  r>  rA  r   rF  r6   rY  r_   r\   rZ   r   r     s    S U 
# 
$ 

S S %(<(<c(A"B  1C 1C 1
(
 sm
 
 	
$C
BR C
r\   r   c                        e Zd ZU ded<   ded<   ee   ed<   ded<   d fd	Z	 	 	 	 	 	 	 	 dd
Z	 	 	 	 	 	 d fdZde	de	fdZ
 xZS )rB  r;   r+  r<   rD  rE  r#   r6  rm   c                     |j                  dd       | _        t        |   di | t        j                  | |||       y )Nr6  r_   )ry   r6  r   r[    dynamo_triton_hopifier_singletoninit_variable)rV   rD  rE  r+  r   r   s        rZ   r[   zTritonKernelVariable.__init__  s;    #ZZ>"6"(66tVZQUVr\   c                 2    t         j                  | |||      S ro   )r_  call_triton_kernelr   s       rZ   r   z"TritonKernelVariable.call_function  s      0BB$
 	
r\   c                     |dk(  rt         j                  | |      S |dk(  rt         j                  | |||      S t        |   ||||      S )N__getitem__run)r_  rF  call_runr   rT  r"  s        rZ   rT  z TritonKernelVariable.call_method  sW     = 3@@tLLU]3<<T4QSTT w"2tT6::r\   r7  c                 v    ddl m} ddlm} t	        ||      r  |j
                  |j                               S |S )Nr0   r5   )r  )r  r6   tensorr  r   r   evaluate_expr)rV   r7  r6   r  s       rZ   specialize_symbolicz(TritonKernelVariable.specialize_symbolic  s6    .+ c?+*#**3+<+<+>??
r\   rA  r   r	  )rj   rk   rl   r   r   r  r[   r   rT  r   rj  rD  rE  s   @rZ   rB  rB    s    
W

#
 &
 -	

 

; &	;
 -; 
;s s r\   rB  c                   D     e Zd Z	 	 	 	 	 	 	 	 d fdZd ZddZd Z xZS )rM  c                     t        |t        j                        sJ t        |   di | || _        || _        || _        || _        y r^   )	r   r   DataPtrVariabler   r[   data_ptrdims
block_dimselement_size)rV   rn  ro  rp  rq  r   r   s         rZ   r[   z*TMADescriptorExperimentalVariable.__init__  sH     (I$=$=>>>"6" 	$(r\   c                     t        | j                  D cg c]  }|j                          c}| j                  D cg c]  }|j                          c}| j                  j                               S c c}w c c}w ro   )r  ro  r  rp  rq  )rV   dims     rZ   rO  z-TMADescriptorExperimentalVariable.to_metadata  sW    /'+yy1S\\^1'+7S\\^7&&(
 	
17s
   A2A7
c                     j                   fd        j                  j                         g  j                   j                   j
                  }j                  |       j                  t        |      dz   d       y )Nc                  V     j                  ddt        j                         d      S )Nz$triton.tools.experimental_descriptorcreate_d_tma_descriptor)r1  rf   ro  r  s   rZ   r  z?TMADescriptorExperimentalVariable.reconstruct.<locals>.<lambda>  s+    G,,6#dii.))9: r\   r0   F)	r  rn  rs  ro  rp  rq  r  r   rf   )rV   rt  r~   s   `` rZ   rs  z-TMADescriptorExperimentalVariable.reconstruct  sp    	
 	!!'*@@T__@d.?.?@c$i!mU3r\   c                 .    | j                   j                  S ro   )rn  from_tensorr   s    rZ   rP  z,TMADescriptorExperimentalVariable.get_tensor  s    }}(((r\   )rn  zvariables.DataPtrVariablero  list[ConstantVariable]rp  rz  rq  r6   r  rj   rk   rl   r[   rO  rs  rP  rD  rE  s   @rZ   rM  rM    s=    )-) ') -	)
 ))

4)r\   rM  c                   >     e Zd Z	 	 	 	 d fdZd ZddZddZ xZS )rN  c                 x    t        |t        j                        sJ t        |   di | || _        || _        y r^   )r   r   r  r   r[   rh  block_shape)rV   rh  r~  r   r   s       rZ   r[   z$TMADescriptorStableVariable.__init__  s:     &)":":;;;"6"&r\   c                 H    t        | j                  j                               S ro   )r  r~  r  r   s    rZ   rO  z'TMADescriptorStableVariable.to_metadata  s!    )%%'
 	
r\   c                     j                  fd       j                  d       | j                  j                          | j                         j                  d       y )Nc                  (     j                  dd      S )Nztriton.tools.tensor_descriptorTensorDescriptorr7  r2  s   rZ   r  z9TMADescriptorStableVariable.reconstruct.<locals>.<lambda>  s    G,,0" r\   ry  r   )r  load_methodrh  rs  r~  rT  r   s    `rZ   rs  z'TMADescriptorStableVariable.reconstruct  sT    	
 	M*(  !Ar\   c                     | j                   S ro   )rh  r   s    rZ   rP  z&TMADescriptorStableVariable.get_tensor  s    {{r\   )rh  variables.TensorVariabler~  zvariables.ListVariabler  )rm   r  r{  rE  s   @rZ   rN  rN    s)    	'*	' .	'


r\   rN  c                   @     e Zd Zdeddf fdZ	 	 	 	 	 	 	 	 ddZ xZS )'CreateTMADescriptorExperimentalVariablerankrm   Nc                 >    |dv sJ t        |   di | || _        y )N)r0   r   r_   )r   r[   r  )rV   r  r   r   s      rZ   r[   z0CreateTMADescriptorExperimentalVariable.__init__ 	  s(    
 v~~"6"	r\   c                    d|v r|d   n|d   }t        |t        j                        st        d| j                   d      | j                  dk(  r;t        |      t        |      z   dk(  sJ d|v r|d   n|d   g}d|v r|d   n|d	   g}nTt        |      t        |      z   d
k(  sJ d|v r|d   n|d   d|v r|d   n|d	   g}d|v r|d   n|d   d|v r|d   n|d   g}d|v r|d   n|d   }t        ||||      S )Nptrr   z8Please ensure there were no graph breaks between create_z3d_tma_descriptor and the upstream .data_ptr() call.r0   r?   rs  	block_dimr      dim1dim0
block_dim1r  
block_dim0rq  ry  )rn  ro  rp  rq  )r   r   rm  r    r  rf   rM  )rV   r|   r~   r   r  ro  rp  rq  s           rZ   r   z5CreateTMADescriptorExperimentalVariable.call_function		  sa     %fUmDG#y889)) %$$  99>t9s6{*a///!&&ud1gD (3f'<{#$q'J t9s6{*a///"(F"2vQ"(F"2vQD
 )5(>|$DG(4(>|$DGJ 2@61Ivn-tTVx0!%	
 	
r\   r   )rj   rk   rl   r  r[   r   rD  rE  s   @rZ   r  r    sE     
	(
#(
 &(
 -	(

 
(
r\   r  c                   $    e Zd Z	 	 	 	 	 	 	 	 ddZy)!CreateTMADescriptorStableVariablec                 T    d|v r|d   n|d   }d|v r|d   n|d   }t        ||      S )Nrh  r   r~  r0   )rh  r~  )rN  )rV   r|   r~   r   rh  r~  s         rZ   r   z/CreateTMADescriptorStableVariable.call_function5	  sF     &.%7!T!W/</Ff]+DQRG*#
 	
r\   Nr   r  r_   r\   rZ   r  r  4	  s-    
#
 &
 -	

 

r\   r  ro   rh  )r   r  r5  r   r   r|  r  r  r   collections.abcr   r   typingr   r   r   r	   r
   typing_extensionsr   unittest.mockr   weakrefr   r   torch._dynamo.excr   r  r   r   r   r   bytecode_transformationr   r   r   r  r   r   r   r   r   r   r   r   r   r    guardsr!   r"   r   r#   r$   r%   r&   r.  r'   r(   r)   r*   r+   r,   r-   r.   r/   baser1   r2   r3   r4   r  r6   #torch.distributed.fsdp._fully_shardr7   ModuleNotFoundErrortorch._dynamo.codegenr8   rl  r:   rQ  r;   r<   r=   rR   rT   rp   rB   rs   r   rw   r   rd   r   r   r   r   r   r   r   rG  rY  r  r  r  r  r  r  r+  r   r>  rD  rj  rp  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r_  rB  rM  rN  r  r  r_   r\   rZ   <module>r     s  .      
   $  B B #  %  4 > > V V   1 N N
 
 
  'E
 /D T"
  !
 
>L \ 2j	A. $sO';"<  "( -" DW73 W7t:4 :<}4? }4@

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