
    a,j"                    	   U d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZ ddlmZmZ ddl	mZmZ ddlmZmZmZmZmZmZ ddlmZ ddlmZ ddlZdd	lmZ dd
lm Z  ddl!m"Z" ddl#m$Z$m%Z%m&Z&m'Z' ddl(m)Z)m*Z*m+Z+ ddl,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8 ddl9m:Z:m;Z; ddl<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZE ddlFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZN ddlOmPZPmQZQmRZRmSZS ddlTmUZU ddlVmWZW 	 ddlXmYZY n# eZ$ r dZYY nw xY wer4ddl[m\Z\ ddl]m^Z^m_Z_m`Z`maZa ddlbmcZc ddldmeZemfZf ddlgmhZh ddlimjZjmkZk ddllmmZm  eded ef         !          Znd"Zod#Zp eqh d$          Zr eqd%h          Zsd&Zt e            Zueeef         evd'<    G d( d)ew          Zxejy        d*e=fd+            Zz G d, d-          Z{d.ed*e{fd/Z|d.ed0d1d2e dz  d3ee         d4e}e~ef         d*e}e~eSf         fd5Z	 dd0d1d6ed7e dz  d*eSfd8Zd0d1d9e}e~ef         d*dfd:Zd;d1d9e}e~eSf         d<e	j        d*dfd=Zd<e	j        d>e}e~ef         d?e~d@eed f         dz  dAee         dz  dBe}e~ef         dz  dCe}e~ef         dz  d*e	j        fdDZh dEZd0d1dFed7e dz  d?e~d*eSf
dGZ G dH dIeS          Z G dJ dKe          Z G dL dMe          Z G dN dOe          Z G dP dQe          Z G dR dSeS          Z G dT dUe          Z G dV dWe          Z G dX dYe          Z G dZ d[e          Z G d\ d]e          Z G d^ d_e          Zd0d1dFed ef         d?e~d3eeS         d4e}e~eSf         d*eSfd`Z G da dbe          Z G dc dde          Z G de dfeS          Z G dg dhe          Z G di dje          Z G dk dle          Zd*e}eef         fdmZd0d1dFed ef         d*e=fdnZ G do dpe          Z G dq dre          Z G ds dteS          Z G du dveS          Z G dw dxeS          ZddyldmZmZmZmZ  G dz d{e          Z e            Z G d| d}eS          Z G d~ deS          Z G d deS          Z G d deS          Z G d deS          Z G d de          Z G d de          Z G d de          Zd0d1deg df         d?e~d*dfdZ G d deS          ZdS )a  
Function-related variable tracking classes for Dynamo's symbolic execution.

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

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

The variable trackers here work together with the rest of Dynamo to enable
accurate graph capture while handling Python's various function-related behaviors.
    N)
namedtuple)CallableSequence)CellTypeFunctionType)AnycastLiteralOptionalTYPE_CHECKINGTypeVar)Never)WeakKeyDictionary)get_stack_above_dynamo)Source)is_namedtuple_class   )configgraph_break_hints	polyfills	variables)create_call_functioncreate_rot_nis_generator)format_frame_infoget_dynamo_observed_exceptionhandle_observed_exceptionInfiniteGeneratorErrorObservedExceptionObservedGeneratorExitObservedUserStopIterationraise_observed_exceptionraise_type_errorStepUnsupportedunimplementedUnsupported)GuardBuilderinstall_guard)	
AttrSourceCellContentsSourceClosureSourceConstantSourceDefaultsSourceGetItemSourceImportSourceSkipGuardSource
TypeSource)check_constant_argscheck_unspec_or_constant_argscmp_name_to_op_mappingidentityis_functionis_wrapper_or_member_descriptoristype	make_cell   )#AsPythonConstantNotImplementedErrorAttributeMutationNewValueMutationNewVariableTrackerConstantVariable)UserDefinedObjectVariable)_fsdp_param_group)	PyCodegen)&InliningGeneratorInstructionTranslatorInliningInstructionTranslatorInstructionTranslatorInstructionTranslatorBase)ContextWrappingVariable)TritonGridTypeTritonKernelType)DunderDictVariable)BaseListVariableListVariable)TensorVariable_F.)bound      >   is_leaf	namespacenone_is_leafrS   zihttps://github.com/pytorch/pytorch/issues/new?&labels=oncall%3A+pt2&projects=&template=pt2-bug-report.yml_spec_cachec                       e Zd ZdS )ClosureConversionErrorN)__name__
__module____qualname__     a/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/torch/_dynamo/variables/functions.pyrX   rX      s        Dr]   rX   returnc            	      t    t          t          t          t          d          d          d          d          S )Ntorchutils_pytreeSUPPORTED_NODES)r)   r/   r\   r]   r^   !get_pytree_SUPPORTED_NODES_sourcere      s6    :l733W==yII  r]   c                   .    e Zd ZdeddfdZdeddfdZdS )FunctionSpecfuncr_   Nc                    |j         }|j        }|j        | _        |j        | _        |j        | _        |d | j                 | _        || j        | j                 | _	        | j        | j	        z   | _
        || j        | j        | j        z            | _        | j        | j        z   }|j        t          z  r||         nd | _        || j        rdndz  }|j        t          z  r||         nd | _        d S Nr:   r   )__code__co_varnamesco_posonlyargcountposonly_countco_argcount	arg_countco_kwonlyargcountkwonly_countposonly_namespos_or_kw_namesall_pos_nameskwonly_namesco_flags
CO_VARARGSvarargs_nameCO_VARKEYWORDS
varkw_name)selfrh   codevnoffs        r^   __init__zFunctionSpec.__init__   s    }!4) 2 4$"4 45!$"4t~"EF!/$2FFt~AR0RRSnt00'+}z'AKBsGGtD%,qq1,%)]^%CM"S''r]   c           
          |j         pd| _        |j        pi | _        t	          t          | j        t          | j                   d          t          t          | j                                                | _	        d S Nr\   )
__defaults__defaults__kwdefaults__
kwdefaultsdictzipru   lenrangepos_default_map)r|   rh   s     r^   update_defaultszFunctionSpec.update_defaults   sv     )/R-3  $"C$6$6#6#8#895T]ASAS;T;TUU 
  
r]   )rY   rZ   r[   r   r   r   r\   r]   r^   rg   rg      s^        N\ Nd N N N N$	
L 	
T 	
 	
 	
 	
 	
 	
r]   rg   rh   c                 p    t                               |           }|t          |           }|t           | <   |S N)rV   getrg   )rh   specs     r^   	_get_specr      s4    ??4  D|D!! DKr]   txrF   	fn_sourceargskwargsc                    t          |           }t                    |j        k    r?| j        s8|s6|j        s/|j        s(|j        s!fdt          |j                  D             S |	                    |            i }t          |          }|j                                        D ]c\  }}	d }
|r;t          j        |j        |	                   rt           j        st%          ||	          }
t'          |j        |	         |
          ||<   dt          |j                  D ]|\  }}|t                    k     rt'          |                   ||<   2||v r0||j        vr't'          |                    |                    ||<   f||vrt-          d|           }t          |j                  d          }|j        r&t'          t/          |                    ||j        <   n6|r4t-          dt                     dt          |j                             |j        D ]}}||v r't'          |                    |                    ||<   -||j        v r6d }|rt%          ||d          }t'          |j        |         |          ||<   lt-          d|           |j        rt'          |          ||j        <   n!|rt-          dt3          |                     |S )	Nc                 D    i | ]\  }}|t          |                   S r\   )wrap_bound_arg).0inamer   r   s      r^   
<dictcomp>z$bind_args_cached.<locals>.<dictcomp>   s=     
 
 
4 .T!W--
 
 
r]   z&missing required positional argument: z#Too many positional arguments: got z, expected T)is_kwz(Missing required keyword-only argument: zUnexpected keyword arguments: )r   r   rp   r   ry   r{   rv   	enumerateru   r   r   r   itemsr@   
is_literalr   r   %skip_guards_on_constant_func_defaultsr-   r   rs   pop	TypeErrortupler   list)rh   r   r   r   r   r   barem_kwr   idxdefault_sourcer   extrakwdefault_sources    ` `          r^   bind_args_cachedr      sn    T??D
 	D		T^##! 	$ 	$ ! 	$ 	 	$
 ! 	$
 
 
 
 
$T%788
 
 
 	
 		B&\\F )//11 J J	c 	<'c(:;;	<<	< ,Is;;N!"dmC&8.II4T/00 M M4s4yy==%b$q'22BtHHV^^ *** &b&**T*:*:;;BtHH^^KTKKLLL  T'((**+E 
 .r5<< @ @4	 
a#d))aaDL^H_H_aa
 
 	

 ! 	O 	O6>>%b&**T*:*:;;BtHHT_$$# O#1)T#N#N#N %b$/$*?AQRRBtHHMtMMNNN  I,R884?	 IGfGGHHHIr]   valsourcec                     t          |t                    r|S |st          j        | |          S t          j                            ||          S r   )
isinstancer>   buildr   LazyVariableTrackercreate)r   r   r   s      r^   r   r     sS     #'' A
 A$R--- ,33C@@@r]   resultc                     t          |                                          D ]4\  }}t          |t          t          f          rt          | |          ||<   5d S r   )r   r   r   r   r   r   )r   r   kvs       r^   wrap_args_kwargsr     sY    V\\^^$$ . .1a%'' 	.&r1--F1I. .r]   parentr}   c                     | j         j        }|j        D ]H}|                                }||v r)|                    ||                    |                     |||<   IdS )z
    Update `result` to add mapping from local name to new cells created
    directly by `code`, or update SideEffects in `parent` if the a local cell is
    already in `result` (cell argument).
    N)outputside_effectsco_cellvarstrack_cell_new
store_cellr   )r   r   r}   r   r   new_cells         r^   init_cellvarsr   #  ss     =-L     ..006>> ##Hfjj.>.>???t   r]   	f_globalsr   r   closurer   annotationsc                     ddl m}  || ||||          }||_        t          |t                    rddlm}	 t           |	|                    }|t          |t                    sJ ||_        |S )Nr   r   pairwise)	typesr   r   r   r   	itertoolsr   r   __annotations__)
r}   r   r   r   r   r   r   r   rh   r   s
             r^   _create_nested_fnr   8  s     #"""""<ixAAD$D+u%% 2&&&&&&88K0011 *[$"?"??&DKr]   >	   __doc__rk   rY   rZ   __closure____globals__r   r   r   fnc                 p   |ot          ||          }|r|dk    rt          |          }d }	 t          j        ||          }n%# t          $ r t          t          |            Y nw xY w|t          v rt          ||          }|r t          j	        
                    ||          S t          j        | |          S )Nr   )r)   r0   inspectgetattr_staticAttributeErrorr"   fn_known_dunder_attrsgetattrr   r   r   r>   r   )r   r   r   r   subobjs        r^   fn_var_getattrr   _  s     0
6400F )$+++ !((F5'D11 5 5 5 	!444445 $$$T"" D,33FFCCC V,,,s   A A%$A%c            
           e Zd Z	 ddddeddf fdZdedz  fdZddZdd	dede	e
         deee
f         de
f
 fdZdefdZdefdZdefdZdedz  fdZdeeef         fdZdej        fdZdefdZdej        fdZdefdZdd	defdZdd	de	e
         deee
f         de
fdZdd	dedefdZdd	deee
f         fdZdefdZ xZ S )BaseUserFunctionVariableNdict_vtzDunderDictVariable | Noner   r_   c                 H     t                      j        di | || _        d S r   )superr   r   )r|   r   r   	__class__s      r^   r   z!BaseUserFunctionVariable.__init__}  s-     	""6"""29r]   c                     | j         S r   r   r|   s    r^   
get_sourcez#BaseUserFunctionVariable.get_source  
    {r]   r   rF   rK   c                 h    | j         %t          j                            ||           | _         | j         S r   )r   r   rK   r   r|   r   s     r^   get_dict_vtz$BaseUserFunctionVariable.get_dict_vt  s,    <$7>>r4HHDL|r]   r   r   c                 ^   |dk    r8|                      |                              |dt          |          |          S |dk    r8|                      |                              |dt          |          i           S t                                          ||t          |          |          S )N__setattr____setitem____delattr____delitem__)r   call_methodr   r   r|   r   r   r   r   r   s        r^   r   z$BaseUserFunctionVariable.call_method  s     =  ##B''33M4::v   ]""##B''33BtDzzSUVVVww""2tT$ZZ@@@r]   c                 4    |                                  j        S r   get_codeco_filenamer   s    r^   get_filenamez%BaseUserFunctionVariable.get_filename      }}**r]   c                 4    |                                  j        S r   r   co_namer   s    r^   get_namez!BaseUserFunctionVariable.get_name      }}&&r]   c                 |    t           j        dk    r|                                 j        S |                                 S )N)      )sysversion_infor   co_qualnamer   r   s    r^   get_qualnamez%BaseUserFunctionVariable.get_qualname  s/    w&&==??..==??"r]   c                 @    |                                  j        d         S Nr   )r   	co_constsr   s    r^   get_docz BaseUserFunctionVariable.get_doc  s    }}(++r]   c                     t           r   NotImplementedErrorr   s    r^   get_globalsz$BaseUserFunctionVariable.get_globals      !!r]   c                     t           r   r  r   s    r^   r   z!BaseUserFunctionVariable.get_code  r  r]   c                     t           r   r  r   s    r^   has_selfz!BaseUserFunctionVariable.has_self  r  r]   c                     t           r   r  r   s    r^   get_functionz%BaseUserFunctionVariable.get_function  r  r]   c                 6    |                                  d         S )NrY   )r  r   s    r^   
get_modulez#BaseUserFunctionVariable.get_module  s    !!*--r]   c                 4   |                      |          }|dv r} t          | d|dd                                }|                    |          r|                    |          S t	          j        || j        ot          | j        |                    S |dk    r|S |dk    r|                    |d           S |d	k    r|                    |d
           S |                    |          r|                    |          S t          t          |           d S )N)rY   r[   r   rZ   rk   get_r   r   __dict__r   c                  F    t          j        i t                                S Nmutation_type)r   ConstDictVariabler=   r\   r]   r^   <lambda>z6BaseUserFunctionVariable.var_getattr.<locals>.<lambda>  s$    	3"2"4"4   r]   __type_params__c                  F    t          j        g t                                S r  )r   TupleVariabler=   r\   r]   r^   r  z6BaseUserFunctionVariable.var_getattr.<locals>.<lambda>  s$    	/"2"4"4   r]   )r   r   containsgetitemr@   r   r   r)   getitem_or_defaultr"   r   )r|   r   r   fn_dictr   s        r^   var_getattrz$BaseUserFunctionVariable.var_getattr  s[   ""2&& TTT4'$ 3tAbDz 3 34466C%% -t,,,#*DKIJt{D,I,I    ZN&&&--    &&&--    %% =t,,,(<<<<<r]   c                    |                                  dk    r;|                                                     d          rt          j        d           S |                    | g |                                 ||          S )Npatch_track_step_calledztorch/optim/lr_scheduler.py)r   r   endswithr@   r   inline_user_function_return	self_argsr|   r   r   r   s       r^   call_functionz&BaseUserFunctionVariable.call_function  s|     MMOO888!!##,,-JKK 9 $*4000--d4Ndnn6F6F4N4NPVWWWr]   c                     d}|t           v s|dk    rd}n6	 t          |                                 |          }n# t          $ r d}Y nw xY wt	          j        ||          S )NFr  T)r   hasattrr  r  r>   r   r|   r   r   r   s       r^   call_obj_hasattrz)BaseUserFunctionVariable.call_obj_hasattr  s     (((DJ,>,>FF !2!2!4!4d;;&   $R000s   "9 AAc                     i S r   r\   r   s     r^   closure_varsz%BaseUserFunctionVariable.closure_vars      	r]   c                     dS NTr\   r   s    r^    should_allow_nested_graph_breaksz9BaseUserFunctionVariable.should_allow_nested_graph_breaks      tr]   r   )r   rF   r_   rK   )!rY   rZ   r[   r   r   r   r   r   strr   r>   r   r   r   r   r   r  r  r   CodeTyper   boolr  r   r  r  r!  r(  r@   r,  r.  r2  __classcell__r   s   @r^   r   r   |  s       59: :2:EH:	: : : : : :FTM       
A#A A '	A
 S/)*A 
A A A A A A+c + + + +'# ' ' ' '#c # # # #,t , , , ,"T#s(^ " " " ""%. " " " ""$ " " " ""e0 " " " ".C . . . .!=5 !=S != != != !=FX#X 'X S/)*	X
 
X X X X1)1141	1 1 1 16 4_@T;U    $        r]   r   c                   h    e Zd ZdZej        Zddhej        Z e	h d          Z
edededd fd            Z	 d.dej        ej        j        z  ded
eddf fdZdef fdZdef fdZdee         fdZdej        fdZdej        fdZdefdZdefdZdeeef         fdZ de!fdZ"ddde#e         d
eeef         deeef         fdZ$dddedef fdZ%dddede&fdZ'ddde#e         d
eeef         def fdZ(ddde#e         d
eeef         dedz  fdZ)defd Z*defd!Z+defd"Z,ddde#e         d
eeef         de-d e#e         eeef         f         dz  fd#Z.dej        dz  fd$Z/d%ede-ed&f         dz  fd'Z0dedee         dz  fd(Z1de2d)         fd*Z3de4fd+Z5d,e6defd-Z7 xZ8S )/UserFunctionVariablez-Some unsupported user-defined global functionr   is_constant>   
optree.opstorch.utils._pytreetorch.utils._cxx_pytreeoptreevaluer   r_   c                 t    t          |                    t          j                              | ||          S Nr   r(   
make_guardr'   CLOSURE_MATCHclsr@  r   s      r^   create_with_sourcez'UserFunctionVariable.create_with_source  s6    f''(BCCDDDs5((((r]   Fr   Nc                 L    t                      j        d	i | t          |dd          rd| _        nd| _        t	          |t
          j        t          j        j	        f          s!t          d| ddgt          j                   t          j        |d|          }|| _        d S )
N_dynamo_marked_constantFTz1can't handle functions not implemented in python z2Dynamo can only handle functions defined in pythonz9Move usage of this function out of `torch.compile` regiongb_typecontextexplanationhints_torchdynamo_inliner\   )r   r   r   r;  r   r   r   ra   jitScriptFunctionr%   r   INFERENCE_MODEr   r   r   r|   r   r;  r   r   s       r^   r   zUserFunctionVariable.__init__  s     	""6"""20%88 	%#D$D "u1593KLMM 		KPO&5	    #B(=rBBr]   c                 |    t          | t                    r| j        S t                                                      S r   )r8   r:  r   r   as_python_constantr|   r   s    r^   rV  z'UserFunctionVariable.as_python_constant<  s3    $,-- 	7Nww))+++r]   c                 |    t          | t                    r| j        S t                                                      S r   )r8   r:  r   r   get_real_python_backed_valuerW  s    r^   rY  z1UserFunctionVariable.get_real_python_backed_valueB  s3    $,-- 	7Nww33555r]   c                     g S r   r\   r   s    r^   r&  zUserFunctionVariable.self_argsG  r/  r]   c                     | j         S r   r   r   s    r^   r  z!UserFunctionVariable.get_functionJ  	    wr]   c                     | j         j        S r   )r   rk   r   s    r^   r   zUserFunctionVariable.get_codeM  s    wr]   c                     t           j        S r   r   r   r   s    r^   python_typez UserFunctionVariable.python_typeP      !!r]   c                 2    t          | j        dd           d uS )N__self__)r   r   r   s    r^   r  zUserFunctionVariable.has_selfS  s    tw
D11==r]   c                     | j         j        S r   )r   r   r   s    r^   r  z UserFunctionVariable.get_globalsV  s    w""r]   c                 Z    | j         }|r!t          | t          j                  r| j        }|S r   )r   r   r   UserMethodVariable	source_fn)r|   r   s     r^   r   zUserFunctionVariable.get_sourceY  s1     	$jy'CDD 	$^Fr]   r   rF   r   c                    | j         rJ | j        }t          |t                    st	          d          |j        j        }|                                 }t          |||||          }t          |||j
                   | j        j        pd}t          |          t          | j        j
        j                  k    sJ t          t          j                    | j        j
        j        |          D ]\  }	}
}|j        j        }||v r	||         }n|rt%          t'          |          |	          }t)          |d|
          }	 t+          j        ||j        |          }n## t0          $ r t3          j                    }Y nw xY w|                    |||          }nV	 t+          j        ||j                  }n## t0          $ r t3          j                    }Y nw xY w|                    d||          }|||
<   |S )z
        Assume `args` and `kwargs` are VariableTracker arguments for a call to
        this function, create new bindings for initial locals.
        z'Only supports regular Python functions.r\   cell_contents)freevar_nameN)r;  r   r   r   r   r   root_txr   r   r   rk   r   r   co_freevarsr   r   countr   r.   r+   r*   r>   r   rj  
ValueErrorr   DeletedVariabletrack_cell_existing)r|   r   r   r   r   rl  r   r   r   r   r   cellr   cell_varclosure_cellclosure_cell_contentscontents_vars                    r^   	bind_argszUserFunctionVariable.bind_args`  s.    ####!%"l++ 	GEFFF-'""!"gvtVDDffbk222'%+7||s47#3#?@@@@@@"Otw/;W 
  
 "	$ "	$OCt "=5L|##'- V,]6-B-BCHH(: /) ) )%?#2#8 24I$ $LL " ? ? ?#,#<#>#>LLL? (;; $ ?#2#8AS#T#TLL! ? ? ?#,#<#>#>LLL? (;;D$UU#F4LLs$    EE<;E<F33GGr   r   c           
      *   |dk    r"t                                          ||          S |t          v r7t          j        | |t          t          | j        |                              S |                                 }t          || j        ||          S )Nr  py_type)
r   r!  r4   r   GetAttrVariabletyper   r   r   r   )r|   r   r   r   r   s       r^   r!  z UserFunctionVariable.var_getattr  s    :77&&r4000+++,dD$)?)?$@$@    ""b$'64888r]   c                 V    t          | j        |          }t          j        ||          S r   )r*  r   r>   r   r+  s       r^   r,  z%UserFunctionVariable.call_obj_hasattr  s(     $''$R000r]   c           
      
   | j         t          j        j        u r	 d |D             }d |                                D             }t          j        j        |i |j        }t          j        |          S # t          $ r}t          d| d|           |d }~ww xY w| j         t          j        j
        u r	  t          j        | j                   j        |i |}|j        d                                         }	t!          |	t"                    sJ t          j        |	          S # t&          $ r}t          d| d|           |d }~ww xY w| j         t          j        j        u r	  t          j        | j                   j        |i |}|                                 |j        d         }
|j        d         }t!          |
t,                    r|
                                }
t!          |t,                    r|                                }t          j        |
|          S # t&          $ r}t          d	| d|           |d }~ww xY w| j         t          j        j        j        u r+d
|j        _        t          j                            d           S | j         t          j        j        u r t          j        | j                   j        |i |}|j        d         }t!          |t@                    sM|!                                }d|j"         d}tG          dd| j$         d| d| d|g tJ          j&                   t!          |tN                    s4|(                                }d| d}tG          d|  |d| ddg           |j         }t          j)        |t          j        j*        j+                  S | j,        r*t[          || j         | (                                ||          S |j        j.        j/        s|| j         t          j        j0        j1        u r_t          j        j2        3                    |          5  ti                      5                    |||          cd d d            S # 1 swxY w Y   tm          |j        j.        dd           dk    r|j        j.        j7        s	 ddl8m9} n# t&          $ r d }Y nw xY w|t| j         |j:        |j;        fv r_t          j        j2        7                    |          5  ti                      5                    |||          cd d d            S # 1 swxY w Y   | <                    |||          }||S ti                      5                    |||          S )Nc                 6    g | ]}|                                 S r\   rV  r   args     r^   
<listcomp>z6UserFunctionVariable.call_function.<locals>.<listcomp>  s$    GGG3c4466GGGr]   c                 >    i | ]\  }}||                                 S r\   r  )r   keyr   s      r^   r   z6UserFunctionVariable.call_function.<locals>.<dictcomp>  s7          6>c3C//11     r]   zCannot convert patch_dynamo_config args/kwargs to constants. Please fix your call to patch_dynamo_config by using simpler inputs. args: 
, kwargs: error_on_graph_breakz\Improper error_on_graph_break() call. Please fix your call to error_on_graph_break(). args: fwdbwdzZImproper override_cudagraphs() call. Please fix your call to override_cudagraphs(). args: Tr   z=`nonstrict_trace` expects a callable, but got value of type <>zTypeError from user codezcall_function(, )rK  z(Applying `nonstrict_trace` to function <zk>; however, `nonstrict_trace` currently requires the function to be defined outside `torch.compile` region.zLimitation of `nonstrict_tracezmake sure definition of z is outside z`torch.compile` regionkinddescriptionz!torch.utils.checkpoint.checkpoint)	FSDPState)=r   ra   _dynamopatch_dynamo_configr   changesr   DynamoConfigPatchVariabler;   RuntimeErrorr  r   	signaturebind	argumentsrV  r   r6  ErrorOnGraphBreakVariable	Exceptionoverride_cudagraphsapply_defaultsr>   CudagraphOverrideVariablebytecode_debugger
breakpointr   _emit_debugger_breakpointr@   r   nonstrict_tracer   r   ra  rY   r%   r@  r   
USER_ERRORr:  r   TorchInGraphFunctionVariableAllowInGraphKindNONSTRICT_TRACEr;  invoke_and_store_as_constantcurrent_tracer,unsafe_allow_externally_visible_side_effectsrb   8_disable_side_effect_safety_checks_for_current_subtracerr   2allow_externally_visible_side_effects_in_subtracerr   r(  r   allow_side_effects_in_hop/torch.distributed.fsdp._fully_shard._fsdp_stater  _pre_forward_post_forward_maybe_call_tree_map_fastpath)r|   r   r   r   
args_constkwargs_constr  erP   r  r  r  fn_vartypmsgfn_namer   r  tree_map_resultr   s                      r^   r(  z"UserFunctionVariable.call_function  s    7em777GG$GGG
   BH,,..       -;#/   !:7CCC6   "6!6 6-36 6  	 W:::7)$'227HHH',*($$&& % ""6===== :;OPPP   "6!6 6-36 6  
 W9997)$'227HHH$$&&&oe,oe,c?33 30022Cc?33 30022C :3DDD   "6!6 6-36 6  
 W7BBB26BI/-44T:::W5553G%dg..3TDVDDEZ]Ff&>?? 
((**eVYVbeee6LTZLL4LL6LLL #*5	    f&:;;  //++ v  v  v  v<#I #H7HHH0	    B99I     	/DGT]]__dF  
 	(U	?}"[\ \ +^^  ? ? ww,,Rv>>? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? BI,mTBB23 3I,F3!UUUUUUU ! ! ! 			!$&'5 * * ]/II"MM C C 7700T6BBC C C C C C C C C C C C C C C C <<RvNN&""ww$$Rv666s   AA4 4
B>BB5A-D# #
E-EE$B8H 
I'H==I6#Q&&Q*-Q*"R) )R87R86#T&&T*-T*c                    |                      |||          }||\  }}}n| }|}|}t          |t                    o|                                }t          |t                    o|                                }	|s|	sd S h |t
          z
  rd S t          |          dk     rd S |d         }
|d         }|dd          }|	r|                    |||
||d          S |                    |||
||          S )Nr   r   r:   r\   )keypath)	_rewrite_tree_map_only_callr   r:  _is_tree_map_function_is_tree_map_with_path_function_SUPPORTED_TREE_MAP_KWARGSr   call_tree_map_with_pathcall_tree_map)r|   r   r   r   rewritetree_map_fntree_map_argstree_map_kwargsis_tree_mapis_tree_map_with_pathmap_fn
first_treerests                r^   r  z2UserFunctionVariable._maybe_call_tree_map_fastpath,  sY    222tVDD:A7KK M$O {$899 41133 	
 {$899 >;;== 	
  	4 	4_ :: 	4}!!4q!"1%
QRR   	55 6    ++  r]   c                 p    t          | j        dd           dk    ot          | j        dd           | j        v S )NrY   tree_maprZ   r   r   _TREE_MAP_MODULESr   s    r^   r  z*UserFunctionVariable._is_tree_map_function`  s<    DGZ..*< Ot448NN	
r]   c                 p    t          | j        dd           dk    ot          | j        dd           | j        v S )NrY   tree_map_with_pathrZ   r  r   s    r^   r  z4UserFunctionVariable._is_tree_map_with_path_functionf  s=    DGZ..2FF Ot448NN	
r]   c                 p    t          | j        dd           dk    ot          | j        dd           | j        v S )NrY   tree_map_onlyrZ   r  r   s    r^   _is_tree_map_only_functionz/UserFunctionVariable._is_tree_map_only_functionl  s<    DGZ../A Ot448NN	
r]   c           	      v   |                                  sd S t          |          dk    rd S h |t          z
  rd S |\  }}}|                     |          }|d S |                                 }|d S t          ||t          |dd                     }	t          j        ||          }
|
|	|gt          |          fS )Nr   r   r   )
r  r   _TREE_MAP_ONLY_SUPPORTED_KWARGS_extract_tree_map_only_types_lookup_tree_map_functionTreeMapOnlyFunctionVariabler   r>   r   r   )r|   r   r   r   type_selectorr  tree_argallowed_typestree_map_callablewrapped_map_fntree_map_variables              r^   r  z0UserFunctionVariable._rewrite_tree_map_only_callr  s     ..00 	4t99>>4V966 	4*.'vx99-HH 4 ::<<$4468T22
 
 

 ,1"6GHH >8"<d6llJJr]   c                     t          | j        dd           }|sd S t          j                            |          }|d S t          |dd           }t          |t          j                  r|S d S )NrZ   r  )r   r   r   modulesr   r   r   r   )r|   module_namemoduler  s       r^   r  z.UserFunctionVariable._lookup_tree_map_function  so    dg|T:: 	4-->46:t44h 233 	Otr]   selector.c                 2   |                                 sd S 	 |                                }n# t          $ r Y d S w xY w|                     |          }|sd S t	          d |D                       sd S t          t                              |                    S )Nc              3   @   K   | ]}t          |t                    V  d S r   )r   r|  )r   r  s     r^   	<genexpr>zDUserFunctionVariable._extract_tree_map_only_types.<locals>.<genexpr>  s,      >>S:c4((>>>>>>r]   )is_python_constantrV  r  _flatten_type_specallr   r   fromkeys)r|   r  	raw_value	flatteneds       r^   r  z1UserFunctionVariable._extract_tree_map_only_types  s     **,, 	4	 3355II" 	 	 	44	 ++I66	 	4>>I>>>>> 	4T]]9--...s   - 
;;c                    t          |t                    r|gS t          |t                    r8g }|D ]1}|                     |          }| d S |                    |           2|S t          t          dd           }|Mt          ||          r=g }|j        D ]1}|                     |          }| d S |                    |           2|S d S )N	UnionType)r   r|  r   r  extendr   r   __args__)r|   r@  	collectedentryflat
union_types         r^   r  z'UserFunctionVariable._flatten_type_spec  s    eT"" 	7NeU## 	$&I ' '..u55<44  &&&&UK66
!j
&C&C!I ' '..u55<44  &&&&tr]   Tc                     dS r1  r\   r   s    r^   is_python_hashablez'UserFunctionVariable.is_python_hashable  r3  r]   c                 *    t          | j                  S r   )hashr   r   s    r^   get_python_hashz$UserFunctionVariable.get_python_hash  s    DG}}r]   otherc                 R    t          |t          j                  o| j        |j        u S r   )r   r   r:  r   r|   r  s     r^   is_python_equalz$UserFunctionVariable.is_python_equal  s%    %!?@@XTWPUPXEXXr]   F)9rY   rZ   r[   r   r   r   _cpython_typer   _nonvar_fields	frozensetr  classmethodr   rH  ra   rQ  rR  r6  r   rV  rY  r   r>   r&  r  r5  r   r|  ra  r  r   r4  r  r   r   r   rw  r!  r@   r,  r(  r  r  r  r  r   r  r  r  r  r
   r  intr  objectr  r7  r8  s   @r^   r:  r:    s.       77 &M 	 
"	0N "		
 	
 	
  )s )C )<R ) ) ) [) " !99  	
 
     >,C , , , , , ,6c 6 6 6 6 6 6
40    e0     %.        "T " " " ">$ > > > >#T#s(^ # # # #F    <'< '< S/)*	<
 
c?"	#< < < <|95 9S 9_ 9 9 9 9 9 91)1141	1 1 1 1|7#|7 '|7 S/)*	|7
 
|7 |7 |7 |7 |7 |7|2#2 '2 S/)*	2
 
4	2 2 2 2h
t 
 
 
 

 
 
 
 

D 
 
 
 
$K#$K '$K S/)*	$K 	"_%o%&(	

 	$K $K $K $KL
5+=+D 
 
 
 
/'/	tSy	D	 / / / /" T
T0A    ,GDM        YV Y Y Y Y Y Y Y Y Yr]   r:  c                   P     e Zd ZdZdddee         deeef         def fdZ xZ	S )InspectSignatureVariablea  
    Variable tracker for inspect.signature with caching support.

    inspect.Signature is expensive to trace. When inspect.signature is called
    repeatedly on the same function during tracing, we cache the result to avoid
    retracing the signature construction each time. Although this is different
    from CPython behavior, it is safe to do so because inspect.signature does
    not change across different calls to the same function.
    r   rF   r   r   r_   c                    t          |          dk    r|s|d         }d }t          |t          t          f          r|                                }|T||j        j        v r|j        j        |         S t                                          |||          }||j        j        |<   |S t                                          |||          S rj   )	r   r   r:  rg  r  r   signature_cacher   r(  )r|   r   r   r   
target_arg	cache_keyr   r   s          r^   r(  z&InspectSignatureVariable.call_function  s     t99>>&>aJI*';=O&PQQ 6&3355	$	 99994Y??..r4@@7=	))4ww$$Rv666r]   
rY   rZ   r[   r   r   r>   r   r4  r(  r7  r8  s   @r^   r  r    s|         7#7 '7 S/)*	7
 
7 7 7 7 7 7 7 7 7 7r]   r  c                        e Zd Zdhej        Zdeedf         dededdf fdZ	defdZ
d	edefd
Zdddee         deeef         defdZ xZS )r  r  .r  r   r_   Nc                 V     t                      j        di | || _        || _        d S r   )r   r   r  r  )r|   r  r  r   r   s       r^   r   z$TreeMapOnlyFunctionVariable.__init__  s5     	""6"""*r]   c                     t           S r   r   r   s    r^   ra  z'TreeMapOnlyFunctionVariable.python_type	  s    r]   nodec                     	 |                                 n# t          $ r Y dS w xY wt          fd| j        D                       S )NFc              3   8   K   | ]}t          |          V  d S r   )
issubclass)r   allowed	node_types     r^   r  zDTreeMapOnlyFunctionVariable._matches_allowed_type.<locals>.<genexpr>  s-      TTg:i11TTTTTTr]   )ra  r  anyr  )r|   r  r  s     @r^   _matches_allowed_typez1TreeMapOnlyFunctionVariable._matches_allowed_type  sc    	((**II" 	 	 	55	TTTTASTTTTTTs    
&&r   rF   r   c                    |s| j                             |||          S |d         }|                     |          r| j                             |||          S t          |          dk    s|r| j                             |||          S |S )Nr   r:   )r  r(  r  r   )r|   r   r   r   leafs        r^   r(  z)TreeMapOnlyFunctionVariable.call_function  s      	?;,,Rv>>>Aw%%d++ 	?;,,Rv>>>t99>>V> ;,,Rv>>>r]   )rY   rZ   r[   r   r  r   r|  r>   r   r   ra  r6  r  r   r   r4  r(  r7  r8  s   @r^   r  r    s       	!	0N
T3Y'   	
 
     T    U/ Ud U U U U# ' S/)*	
 
       r]   r  c            	            e Zd Z	 ddej        dededdf fdZdefdZ	e
d	edefd
            Zdddee         deeef         defdZ xZS )BuiltinMethodVariableFr   r;  r   r_   Nc                      t                      j        di | t          |t          j                  sJ || _        d S r   )r   r   r   r   BuiltinMethodTyper   rT  s       r^   r   zBuiltinMethodVariable.__init__&  sF     	""6""""e566666r]   c                     t           j        S r   )r   r  r   s    r^   ra  z!BuiltinMethodVariable.python_type-  s    &&r]   objc                 t    | j         }| j        }|t          u r|dk    pt          |          t          u o|dk    S )N__new____contains__)rd  rY   r   r|  r  )r  method_selfmethod_names      r^   is_supported_builtin_methodz1BuiltinMethodVariable.is_supported_builtin_method0  sH    ll u$A	)A 
*L{n/L	
r]   r   rF   r   c                     | j         j        }| j         j        }| j        ot	          | j        d          }t          j        |||d          }|                    ||||          S )Nrd  T)realize)r   rd  rY   r   r)   r>   r   r   )r|   r   r   r   r   r   
obj_sourceobj_vts           r^   r(  z#BuiltinMethodVariable.call_function;  sc     g&w[HZZ%H%H
 &r;
DQQQ!!"dD&999r]   r  )rY   rZ   r[   r   r  r6  r   r   r|  ra  staticmethodr"  r   r>   r   r4  r(  r7  r8  s   @r^   r  r  %  s        ?D )8<PS	     'T ' ' ' ' 
 
 
 
 
 \

:#
: '
: S/)*	
:
 

: 
: 
: 
: 
: 
: 
: 
:r]   r  c            
           e Zd Zej        Zdej        deee	f         ddde	ddf
 fdZ
dej        fd	Zdefd
ZdefdZdefdZdefdZdefdZ defdZeZd(dZdeee	f         fdZdefdZdddefdZdddedefdZdddefdZdddefdZdddee         fdZddde ege	f         ddfdZ!de"d          fd!Z#ddd"eddfd#Z$defd$Z%defd%Z&ddded&ee         deeef         def
 fd'Z' xZ(S ))LocalGeneratorObjectVariabler}   r   inline_tracerrD   r   r_   Nc                 d     t                      j        di | || _        || _        || _        d S r   )r   r   r}   r   r*  )r|   r}   r   r*  r   r   s        r^   r   z%LocalGeneratorObjectVariable.__init__L  s>     	""6"""	"*r]   c                     | j         S r   )r}   r   s    r^   r   z%LocalGeneratorObjectVariable.get_codeX  s
    yr]   c                 4    |                                  j        S r   r   r   s    r^   r   z)LocalGeneratorObjectVariable.get_filename[  r   r]   c                 4    |                                  j        S r   r   r   s    r^   r   z%LocalGeneratorObjectVariable.get_name^  r   r]   c                      t          d          )Nr  r  r   s    r^   r  z)LocalGeneratorObjectVariable.get_functiona  s    !.111r]   c                     dS NFr\   r   s    r^   r  z%LocalGeneratorObjectVariable.has_selfd      ur]   c                 *    |                                  S r   )r   r   s    r^   rY   z%LocalGeneratorObjectVariable.__name__g  s    }}r]   c                 J    | j         j         d|                                  dS )N(r  )r   rY   r   r   s    r^   __str__z$LocalGeneratorObjectVariable.__str__j  s&    .)>>DMMOO>>>>r]   codegenrC   c                    ddl m} ddlm}m} |j        } ||          } ||          } ||          }|5  |5  |5  | j        }	|	j        s|                     |          | _	        t          j        | j	                                      |           d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nr   )"disallow_side_effects_in_generator) save_and_restart_speculation_log(temporarely_allow_writes_to_output_graph)torch._dynamo.side_effectsr9  torch._dynamo.symbolic_convertr:  r;  r   r*  generator_exhaustedforce_unpack_var_sequenceremaining_itemsr   ListIteratorVariablereconstruct)
r|   r7  r9  r:  r;  r   savedisallowtemptracers
             r^   rB  z(LocalGeneratorObjectVariable.reconstructo  s   QQQQQQ	
 	
 	
 	
 	
 	
 	
 	

 Z//3355b9977;; 	V 	V8 	V 	VT 	V 	V'F- J'+'E'Eb'I'I$*4+?@@LLWUUU		V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	VsX   CB7AB B7 B$$B7'B$(B7+C7B;	;C>B;	?CCCc                     | j         S r   r   r   s    r^   r  z(LocalGeneratorObjectVariable.get_globals  
    ~r]   c                     t           j        S r   )r   GeneratorTyper   s    r^   ra  z(LocalGeneratorObjectVariable.python_type  s    ""r]   r   rG   c           	         | j         }|                                 rt          t          |           	 |                                S # t
          $ r}d|_        |d }~wt          $ r- t          ddddt           dgt          j                   Y d S t          $ ra}t          j        j                            |                                            d|_        |j        s|j        s|xj        dz  c_         d }~ww xY w)	NTzinfinite generator detected z}Dynamo traced the YIELD_VALUE bytecode too many times. This could mean that we have attempted to trace an infinite generator.zLIf you are sure that your generator is not infinite, please report a bug at .rK  zA

Skipping frame due to graph break in a generator's next() call.)r*  _is_generator_exhaustedr"   StopIterationinline_call_r   r>  r   r%   PT2_ISSUE_TRACKER_URLr   r  r&   ra   r  
eval_frame	skip_coder   
skip_frame	one_graphr  r  )r|   r   rF  r  s       r^   next_variablez*LocalGeneratorObjectVariable.next_variable  sC   #'')) 	8$]B777	 &&(((  	 	 	)-F&G% 	 	 	5I |cx{{{&1	 	 	 	 	 	 	  	 	 	M$..t}}???AL< _(? _^^	s*   A 
C<	A6C<	C<AC77C<rF   r   c                     ||                                  j        v rt          j        d          S t          j        d          S NTF)ra  r  r@   r   r|   r   r   s      r^   r,  z-LocalGeneratorObjectVariable.call_obj_hasattr  s@     4##%%...#*4000&u---r]   c                     dS r1  r\   r   s     r^   has_unpack_var_sequencez4LocalGeneratorObjectVariable.has_unpack_var_sequence  r2  r]   c                     dS r1  r\   r   s     r^   has_force_unpack_var_sequencez:LocalGeneratorObjectVariable.has_force_unpack_var_sequence  r3  r]   c                 @    g }|                      ||j                   |S r   )force_apply_to_var_sequenceappend)r|   r   r   s      r^   r?  z6LocalGeneratorObjectVariable.force_unpack_var_sequence  s&     )+((V];;;r]   r   c                     	 	  ||                      |                     n # t          $ r t          |           Y d S w xY wAr   )rW  r!   r   )r|   r   r   s      r^   r`  z8LocalGeneratorObjectVariable.force_apply_to_var_sequence  sb    	4%%b))****,   )"---	s   " ??Fc                     dS r1  r\   r   s    r^   r2  z=LocalGeneratorObjectVariable.should_allow_nested_graph_breaks  r2  r]   excc                     | j         }	 |                    |           d S # t          $ r }|                    |           Y d }~d S d }~ww xY wr   )r*  _raise_exception_variabler   exception_handler)r|   r   rd  rF  r  s        r^   _setup_exceptionz-LocalGeneratorObjectVariable._setup_exception  sr     #	(,,S11111  	( 	( 	( $$Q'''''''''	(s     
A
AA
c                 4    | j         d u p| j         j        dk    S r  )r*  instruction_pointerr   s    r^   _is_generator_just_startedz7LocalGeneratorObjectVariable._is_generator_just_started  s!    !T)XT-?-SWX-XXr]   c                 .    t          | j        dd          S )Nr>  F)r   r*  r   s    r^   rO  z4LocalGeneratorObjectVariable._is_generator_exhausted  s    t)+@%HHHr]   r   c                    |dk    r|                      |          S |dk    r| S |dk    r|                                 r=t          |          r.t          d |D                       st	          t
          |           | j        }|                    |           |                      |          S |dk    r| j        }|                                 s|                                 r&d|_	        t          j                            d           S 	 |                     |t          j        t          g                      t           j        dk    r6|j        j        dk    r&d|_	        t          j                            d           S n6# t(          $ r) d|_	        t          j                            d           cY S w xY w	 |                      |          rt	          t*          |           nA# t(          $ r) d|_	        t          j                            d           cY S t,          $ r |j        J |j        cY S w xY w|d	k    r| j        }	 |                     |t          |          d
k    r|d         n|d                    n# t0          $ r  w xY w|                      |          }t3          dt4          fi           }	 |                     |t          j        |g                      |                      |           t	          t*          |           n# t7          |          $ r Y nw xY w|S t9                                          ||||          S )N__next____iter__sendc              3   >   K   | ]}|                                 V  d S r   )is_constant_noner  s     r^   r  z;LocalGeneratorObjectVariable.call_method.<locals>.<genexpr>  s.      BBc3//11BBBBBBr]   closeT)r      CALL_INTRINSIC_1throwr   r:   r   __InternalThrowException)rW  rk  r   r  r"   r   r*  	push_manyrO  r>  r   r@   r   rh  ExceptionVariableGeneratorExitr   r   next_instructionopnamer    r  r!   symbolic_resultr   r|  r  r   r   r   )	r|   r   r   r   r   rF  retvalexc_typer   s	           r^   r   z(LocalGeneratorObjectVariable.call_method  s    :%%b)))ZKV^^ ..00 <SYY < BBTBBBBB <,Y;;;'FT"""%%b)))W__ 'F..00 ?D4P4P4R4R ?-1* 188>>>?%%	3M2FF  ( $///6:LLL15F.$5<<TBBB( ? ? ?-1* 188>>>>>?
.%%b)) ?,\2>>>( ? ? ?-1* 188>>>>>, . . .
 -999----. W__ 'F
 %%bSYY!^^$q''aQQQQ$    ''++F| 6bIIH?%%b)*EhPR*S*STTT""2&&&
 )v>>>>	 1::   
 Mww""2tT6:::sI   
A3E? ?0F21F26*G" "0H0H0/H07I9 9J5>L	 	LLr7  rC   r_   N))rY   rZ   r[   r   rK  r  r5  r   r4  r   r   r   r   r   r   r  r6  r  r6  __repr__rB  r  r|  ra  r>   rW  r@   r,  r\  r^  r   r?  r   r`  r
   r2  rh  rk  rO  r   r7  r8  s   @r^   r)  r)  H  s       'M
+n
+ S>
+ @	
+
 
+ 

+ 
+ 
+ 
+ 
+ 
+%.    +c + + + +'# ' ' ' '2e 2 2 2 2$    #    ? ? ? ? ? HV V V V$T#s(^    #T # # # # ;     B.).14.	. . . .*A d    0G D    -	o	   -3;_<Ms<R3S	   '%.    	()	(0?	(		( 	( 	( 	(YD Y Y Y YI I I I Iw;#w; w; ?#	w;
 S/)*w; 
w; w; w; w; w; w; w; w; w; w;r]   r)  c                       e Zd ZdZdS )4ContextlibContextManagerLocalGeneratorObjectVariablez
    .. note::

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

        It is a special case of a generator function as we do not allow return a context manager
        from a torch.compile function.
    N)rY   rZ   r[   r   r\   r]   r^   r  r    s           r]   r  c            	           e Zd ZdZdefdZeddedededdf fd	Z	d
e
defdZdej        fdZdee
ef         fdZdefdZdddee         dee
ef         ddfdZdddee         dee
ef         defdZdefdZ xZS )LocalGeneratorFunctionVariablezxfunctions that behaves like iterators

    .. note::

        This is a wrapper around (Nested)UserFunctionVariable
    r_   c                     t           j        S r   r`  r   s    r^   ra  z*LocalGeneratorFunctionVariable.python_type  rb  r]   )generator_clsvtr  r   Nc                V     t                      j        di | || _        || _        d S r   )r   r   r  r  )r|   r  r  r   r   s       r^   r   z'LocalGeneratorFunctionVariable.__init__  s7     	""6"""*r]   r   c                 h    || j         j        v rt          | |          S t          | j        |          S r   )r   r  r   r  )r|   r   s     r^   __getattr__z*LocalGeneratorFunctionVariable.__getattr__  s4    4>***4&&&tw%%%r]   c                 4    | j                                         S r   )r  r   r   s    r^   r   z'LocalGeneratorFunctionVariable.get_code      w!!!r]   c                 4    | j                                         S r   )r  r  r   s    r^   r  z*LocalGeneratorFunctionVariable.get_globals  s    w""$$$r]   c                 4    | j                                         S r   )r  r  r   s    r^   r  z'LocalGeneratorFunctionVariable.has_self  r  r]   r   rG   r   rE   c                 4    ddl m}  |j        || ||          S )Nr   )rE   )r=  rE   build_inline_tracer)r|   r   r   r   rE   s        r^   _build_inline_tracerz3LocalGeneratorFunctionVariable._build_inline_tracer  s;     	QPPPPP@,@	
 
 	
r]   rF   c                    t          | j                                                  s9t          dt	          | j                                                  dddg           |                     |t          |          |          }| j                                        }| j                                        }|                     |||| j	                  S )Nz'non-generator contextlib.contextmanagerzvCannot compile function decorated with `@contextlib.contextmanager` that is not a generator, i.e. does not use `yield`z5Use `yield` in the function body instead of `return`.z2Remove the `@contextlib.contextmanager` decorator.rK  r   )
r   r  r   r%   r4  r  r   r  r  r   )r|   r   r   r   r*  r}   r   s          r^   r(  z,LocalGeneratorFunctionVariable.call_function  s     DG,,..// 
	ADG,,..//. LH	 	 	 	 11"d4jj&IIw!!G''))	 !!;	 " 
 
 	
r]   c                 4    | j                                         S r   )r  rY  r   s    r^   rY  z;LocalGeneratorFunctionVariable.get_real_python_backed_value  s    w33555r]   )rY   rZ   r[   r   r|  ra  r)  r   r   r   r4  r  r   r5  r   r   r  r6  r  r   r>   r  r   r(  r  rY  r7  r8  s   @r^   r  r    s        "T " " " " ;		+ 	+ 	+$	+ 		+
 	+ 
	+ 	+ 	+ 	+ 	+ 	+& & & & & &"%. " " " "%T#s(^ % % % %"$ " " " "
'
 ?#
 S/)*	

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

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

        This is only used when the function is annotated with @contextlib.contextmanager
    r  r   r_   Nc                 J     t                      j        |fdt          i| d S )Nr  )r   r   r  )r|   r  r   r   s      r^   r   z<FunctionDecoratedByContextlibContextManagerVariable.__init__  sB    	
 	
N	
 	
 	
 	
 	
 	
r]   r   rG   r   rD   c                     t                                          |||          }t          |t          j        j        j                  sJ d|_        |S r1  )r   r  r   ra   r  symbolic_convertrD   is_generator_from_ctx_manager)r|   r   r   r   rF  r   s        r^   r  zHFunctionDecoratedByContextlibContextManagerVariable._build_inline_tracer  sY     --b$??M*Q
 
 	
 	
 
 04,r]   )rY   rZ   r[   r   r   r   r   r   r>   r   r4  r  r7  r8  s   @r^   r  r    s         
3 
s 
t 
 
 
 
 
 
' ?# S/)*	
 
2         r]   r  c                       e Zd ZdZej        Z	 ddedef         de	de
dz  deddf
 fd	Zdefd
Zdee	         fdZdeej                 fdZdddee	         deee	f         de	f fdZdddede	f fdZdefdZ xZS )rg  z$Some unsupported user-defined methodNr   .r  rh  r   r_   c                      t                      j        dd|i| || _        || _        |?|                    d          ,t          |                    d          d          | _        d S d S d S )Nr   r   __func__r\   )r   r   r  rh  r   r)   )r|   r   r  rh  r   r   s        r^   r   zUserMethodVariable.__init__!  s}     	))B)&)))" H!5!5!A'

8(<(<jIIDNNN !A!Ar]   c                 @    | j         j         d| j         d| j         dS )Nr5  r  r  )r   rY   r   r  r   s    r^   r  zUserMethodVariable.__repr__:  s*    .)BBDGBBtxBBBBr]   c                     | j         gS r   r  r   s    r^   r&  zUserMethodVariable.self_args=      zr]   c                     t           j        S r   r   
MethodTyper   s    r^   ra  zUserMethodVariable.python_type@      r]   r   rF   r   c                    ddl m}m}  || j                  r^g |                                 |}t          j        | j        t
          j        j        j	                  }|
                    |||          S  || j                  r^g |                                 |}t          j        | j        t
          j        j        j                  }|
                    |||          S |j                                        rt          | j        t
          j                  rvt#          | j        dd          }||                    d          r|dk    s| j        r;| j                            || j        j        t-          |          || j                  S nVt.          O| j        t.          j        j        u r7t          j        | j                  
                    || j        g|R |          S | j        rIt#          | j        j        | j        j                  }	t9          ||	|                                 ||          S t=                      
                    |||          S )	Nr   )is_leaf_functionis_nonstrict_trace_callabler  rZ   rM  z	torch.nn.ztorch.nn.utils.parametrize)constant)trace_rulesr  r  r   r&  r   r  ra   r  r  r(  LEAF_FUNCTIONr   is_root_tracerr   r  NNModuleVariabler   
startswithr;  r   rY   r   rB   FSDPParamGroupuse_training_stateTorchCtxManagerClassVariabler@  r  r   r   )r|   r   r   r   r  r  	call_argsvarmodule_attrr   r   s             r^   r(  z UserMethodVariable.call_functionC  sm    	POOOOOOO&&tw// 	<2$..**2T2I8io>N  C $$RF;;;DG$$ 	<2$..**2T2I8io>L  C $$RF;;; 9##%% 	*Hi0+
 +
 	 "$'<<<K '**;77 (#???# @ x++($t**ftGW ,    @ ),;NNN9$'BBPPTX%%%v    	W)9::B/BvVVVww$$Rv666r]   r   c                     |dk    r| j         S |dk    r t          j        || j        | j                  S t                                          ||          S )Nrd  r  )r  r>   r   r   rh  r   r!  )r|   r   r   r   s      r^   r!  zUserMethodVariable.var_getattr  sU    :8O: #(TWdnEEEww""2t,,,r]   c                     | j         S r   r\  r   s    r^   rY  z/UserMethodVariable.get_real_python_backed_value  r]  r]   r   )rY   rZ   r[   r   r   r  r  r   r   r>   r   r   r4  r  r   r&  r|  ra  r   r   r(  r!  rY  r7  r8  s   @r^   rg  rg    s       .. $M $(	J JS#XJ J D=	J
 J 
J J J J J J2C# C C C C40     T%"23        B7#B7 'B7 S/)*	B7
 
B7 B7 B7 B7 B7 B7H-5 -S -_ - - - - - -c        r]   rg  c                   p     e Zd Zdedddeddf fdZdd	d
ee         dee	ef         def fdZ
ddZ xZS )WrappedUserMethodVariablewrappedrM  rH   r   r_   Nc                     |                     dd            |                     dd             t                      j        |j        |j        fi | || _        || _        d S )Nr   r  )r   r   r   r   r  r  rM  r|   r  rM  r   r   s       r^   r   z"WrappedUserMethodVariable.__init__  sd     	

4

5$W[;;F;;;r]   r   rF   r   c                 f   t           j        rLt          t          j                  }|                    || j        | j        gt          |          z   |          S | j        	                    |           t                                          |||          }| j                            |           |S r   r   nested_graph_breaksr:  r   _fn_with_ctxr(  rM  r  r   enterr   exitr|   r   r   r   
wrapper_fnr   r   s         r^   r(  z'WrappedUserMethodVariable.call_function       % 	-i.DEEJ++T\4<04::=v   	2&&r488"r]   r7  rC   c                                            fd             j                                       t          dd                     d S )Nc                  $      j                   S r   rM  r7  r|   s   r^   r  z7WrappedUserMethodVariable.reconstruct.<locals>.<lambda>      ggdl&;&; r]   r:   Fadd_push_nullr  extend_outputr   r|   r7  s   ``r^   rB  z%WrappedUserMethodVariable.reconstruct  \    ;;;;;<<<21e<<=====r]   r  )rY   rZ   r[   rg  r   r   r   r>   r   r4  r(  rB  r7  r8  s   @r^   r  r    s        
#
 +
 	

 

 
 
 
 
 
# ' S/)*	
 
      > > > > > > > >r]   r  c                   p     e Zd Zdedddeddf fdZdd	d
ee         dee	ef         def fdZ
ddZ xZS )WrappedUserFunctionVariabler  rM  rH   r   r_   Nc                     |                     dd             t                      j        |j        fi | || _        || _        d S )Nr   )r   r   r   r   r  rM  r  s       r^   r   z$WrappedUserFunctionVariable.__init__  sL     	

4..v...r]   r   rF   r   c                 f   t           j        rLt          t          j                  }|                    || j        | j        gt          |          z   |          S | j        	                    |           t                                          |||          }| j                            |           |S r   r  r  s         r^   r(  z)WrappedUserFunctionVariable.call_function  r  r]   r7  rC   c                                            fd             j                                       t          dd                     d S )Nc                  $      j                   S r   r  r  s   r^   r  z9WrappedUserFunctionVariable.reconstruct.<locals>.<lambda>  r  r]   r:   Fr  r  s   ``r^   rB  z'WrappedUserFunctionVariable.reconstruct  r  r]   r  )rY   rZ   r[   r:  r   r   r   r>   r   r4  r(  rB  r7  r8  s   @r^   r  r    s        	%	 +	 		
 
	 	 	 	 	 	# ' S/)*	
 
      > > > > > > > >r]   r  c                     dt           dt          ffdfd|D             }fd|                                D             } ||i |}| j                            |t                              S )Nxr_   c           
         |                                  r"t          d|                                           S t          | t                    r:| j        ,t          |                     t          j	                             | j
        S 	 |                                 S # t          $ rJ t          dd dt          |           j         dt          |           j         d ddd	g
           Y d S w xY w)NrN   z1assume_constant_result argument conversion failedz	function z, variable type z Cannot convert argument of type z# to a Python constant for function zm marked with torch._dynamo.assume_constant_result. The variable tracker does not support constant conversion.z>Remove torch._dynamo.assume_constant_result from this functionzIEnsure all arguments passed to the function can be converted to constantsrK  )	is_tensorr	   get_real_valuer   rA   r   r(   rD  r'   ID_MATCHr@  rV  r;   r%   r|  rY   )r  r   s    r^   convertz-invoke_and_store_as_constant.<locals>.convert  s&   ;;== 	>(!,,;;===a233 	x#all<+@AABBB7N	'')))2 	 	 	KLDLL$q'':JLLNtAww?O N N $N N N U_
 
 
 
 
 
 
	s   B AC0/C0c                 &    g | ]} |          S r\   r\   )r   r  r  s     r^   r  z0invoke_and_store_as_constant.<locals>.<listcomp>  s!    %%%1GGAJJ%%%r]   c                 .    i | ]\  }}| |          S r\   r\   )r   r   r   r  s      r^   r   z0invoke_and_store_as_constant.<locals>.<dictcomp>  s'    7771a777r]   r   )r>   r   r   r   register_attr_or_moduler,   )r   r   r   r   r   resr  s     `   @r^   r  r    s    ? s      , &%%%%%%D7777777F
"d
f

C9,,d## -   r]   c                       e Zd Zdhej        Z	 d#dededeeef         dedz  dedz  dedz  dedz  d	ed
df fdZ	d
e
e         fdZd
ej        fdZd
ej        fdZd
eej                 fdZd#dee         dz  d
ej        fdZd
efdZdee         d
ej        fdZddded
ef fdZd
efdZddded
ef fdZddded
ef fdZd
efdZd
eeef         fdZddde e         d	eeef         d
eeef         fdZ!d$d"Z" xZ#S )%NestedUserFunctionVariabler   Nr  r}   r   r   r   
wrapped_fnr   r_   c                    |                     d          "|                    t                                  t                      j        di | t          |                                t                    sJ t          |                                t          j	                  sJ t          |t                    sJ || _        || _        || _        || _        || _        || _        || _        d S )Nr  r  r\   )r   updater<   r   r   r   rV  r4  r   r5  r   r  r}   r   r   r   r   r  )
r|   r  r}   r   r   r   r   r  r   r   s
            r^   r   z#NestedUserFunctionVariable.__init__  s     ::o&&.MM(<(>(>M???""6"""'4466<<<<<$1133U^DDDDD)T*****	" $2<r]   c                     g S r   r\   r   s    r^   r&  z$NestedUserFunctionVariable.self_args"  r/  r]   c                 *    |                                  S r   )r  r   s    r^   rV  z-NestedUserFunctionVariable.as_python_constant%  s      """r]   c                 4    | j                                         S r   )r}   rV  r   s    r^   r   z#NestedUserFunctionVariable.get_code(  s    y++---r]   c                     t           j        S r   r`  r   s    r^   ra  z&NestedUserFunctionVariable.python_type+  rb  r]   _convertingc                 `   ddl m} t          |           }|t                      }||v rt	          d          |                    |           	 |                     |          |                    |           S # |$ r}t	          d          |d }~ww xY w# |                    |           w xY w)Nr:   )r;   z-cycle detected in mutually recursive closuresz1failed to convert closure cell to Python constant)baser;   idsetrX   add_get_function_impldiscard)r|   r  r;   self_idr  s        r^   r  z'NestedUserFunctionVariable.get_function.  s     	>=====T((%%Kk!!(?   	   	)**;77 (((( 3 	 	 	(C 	
 ((((s$   A9 9B>BBB B-c                 `    	 |                                   dS # t          t          f$ r Y dS w xY wrY  )rV  r  r&   r   s    r^   r  z-NestedUserFunctionVariable.is_python_constantD  sE    	##%%%4#[1 	 	 	55	s    --c                    d }| j         rddlm}  |j                    }g }| j         j        D ]}|j        j                            |          }|| u rt          d          t          |t                    r|                    |          }n|                                }|                    t          |                     t          |          }t!          j        | j                                        | j        | j                                        d |          }	| j        r| j                                        |	_        | j        r| j                                        |	_        | j        r| j                            d          r}| j                            d                                          }
t          |
t                    rddlm} t=           ||
                    }
t          |
t<                    sJ |
|	_        |	S )Nr   )rF   z self-referential nested function)argdefsr   r   r   ) r   r=  rF   
current_txr   r   r   	load_cellrX   r   r  r  rV  ra  r9   r   r   r   r}   r   r  r   r   r   r   r   r  r  r   r   r   r   )r|   r  closure_cellsrF   r   cellsrs  rj  r@  rh   r   r   s               r^   r  z-NestedUserFunctionVariable._get_function_implK  s   < 	)LLLLLL1&133BE L. / / !#	 6 @ @ J J !D((01STTT m-GHH ?)66{CCEE)<<>>EYu--....!%LLM!I((**NL++--!
 
 
 = 	C $ @ @ B BD? 	G"&/"D"D"F"FD< 		/DL112CDD 		/,../@AATTVVK+u-- :......"88K#8#899 k400000#.D r]   r   rF   r   c           
         |dv r"t                                          ||          S |dk    r;t          | dd           }|r|                                nt	          j        d           S |t          v r<t          j        | |t          t          t          j        |                              S t                                          ||          S )N)r   r  r   rk   rZ   rY   r[   r  r   r   ry  )r   r!  r   rV  r@   r   r4   r   r{  r|  r   r   r|   r   r   dr   s       r^   r!  z&NestedUserFunctionVariable.var_getattr  s     	
 	
 	
 77&&r4000>!!j$//A-.Q1'')))4D4KD4Q4QQ+++,dD1CT)J)J$K$K    77&&r4000r]   c                     | j         d uS r   )r   r   s    r^   has_closurez&NestedUserFunctionVariable.has_closure  s    |4''r]   c                    |dk    r|                                  S |dk    r|                                 S |dk    r)t          | dd           }|r|                                nd S t	                                          ||          S )NrY   rk   r   r   )r   r   r   rV  r   const_getattrr  s       r^   r  z(NestedUserFunctionVariable.const_getattr  s    :==??":==??">!!j$//A-.81'')))D8ww$$R...r]   c                 j   |dk    r#t          j        |t          | d                    S |dk    r#t          j        |t          | d                    S t          j        |          }||                     |          v rt          j        d          S t                                          ||          S )Nrk   r}   r   r   T)r>   r   r*  r@   r   r   r   r,  )r|   r   r   r  r   s       r^   r,  z+NestedUserFunctionVariable.call_obj_hasattr  s     :"(WT6-B-BCCC>!!"(WT:-F-FGGG$T**!!"%%%%#*4000ww''D111r]   c                     dS r1  r\   r   s    r^   r  z#NestedUserFunctionVariable.has_self  r2  r]   c                     | j         S r   rH  r   s    r^   r  z&NestedUserFunctionVariable.get_globals  rI  r]   r   r   c                 &   |                                  }t          j        || j        | j                                        | j        rt          | j        j                  nd t          d t          t          |                                  j                            D                                 }| j        r| j                                        |_         t          j        |          j        |i |}|                                 t'          |j                                                  }t+          |j        j        |           t1          |||           t3          |j                  D ]"\  }}	|	|vsJ | j        j        |         }
|
||	<   #|S )Nc              3   4   K   | ]}t          d           V  d S r   )r9   )r   _s     r^   r  z7NestedUserFunctionVariable.bind_args.<locals>.<genexpr>  s(      SSa)D//SSSSSSr]   )r   r   r   r   r  rV  r   r   r   r   r   rm  r   keys_as_python_constantr   r   r  r  r  r   r  r   r   rl  r   r   r   )r|   r   r   r   r}   rh   rP   r   r   r   rr  s              r^   rw  z$NestedUserFunctionVariable.bind_args  st    }}!NL++--*.-AE$-%&&&TSS5T]]__5P1Q1Q+R+RSSSSS
 
 ? 	L"&/"I"I"K"KD,!$'',d=f==eo++--...777ffd+++"4#344 	  	 ICv%%%%<%c*DF4LLr]   r7  rC   c                                         fd            | j                                                           | j                  g            t          j        | j        j        j                             | j	        r | j	                   n)                    
                    d           g           | j        r | j                   n)                    
                    d           g           | j        r | j                   n)                    
                    d           g           | j        r| j                            d          rt| j                            d          }	 |                                }                                        |          g           nD# t"          $ r  |           Y n-w xY w                    
                    d           g                               t%          dd                     | j        r                     fd            | j                                       t%          dd                                         t)          d                                         t%          dd                     j        }|j        j                            |           r|j        j        j        |                                          D ]]\  }}                                  |                               t)          d                                         |           \d S d S )	Nc                  :                          t          d          S )Nr   )load_import_fromrY   r7  s   r^   r  z8NestedUserFunctionVariable.reconstruct.<locals>.<lambda>  s    G,,X7JKK r]   r      Fc                  0                          dd          S )N	functoolswrapsr	  r
  s   r^   r  z8NestedUserFunctionVariable.reconstruct.<locals>.<lambda>  s    00gFF r]   r:   r   T)r  r}   r  create_load_const_uncheckedr   r@   r   r@  r   r   create_load_constr   r   r   r  r  rV  r  r   r  r   r   r   r   has_pending_mutationstore_attr_mutationsr   dup_top
store_attr)r|   r7  r   r   r   r@  s    `    r^   rB  z&NestedUserFunctionVariable.reconstruct  s   KKKK	
 	
 	
 		wBB4>RRSTTT '	(?@@AAA= 	EGDM""""!!7#<#<T#B#B"CDDD< 	EGDL!!!!!!7#<#<T#B#B"CDDD? 	EGDO$$$$!!7#<#<T#B#B"CDDD< 
	EDL112CDD 
	E,../@AAK%)<<>>%%88EEF    ' % % %$$$$$% !!7#<#<T#B#B"CDDD21e<<===? 	A!!FFFF   GDO$$$!!"6q%"@"@AAA!!,q//222!!"6q$"?"?@@@ Z9!66t<< 	)!y5J egg) )e !!!%%l1oo666""4((((	) 	)) )s   =G GGr   r  )$rY   rZ   r[   r   r  r>   r   r4  r   r   r   r&  r   r   rV  r5  r   r|  ra  r  r  r  r6  r  r  r!  r  r  r@   r,  r  r  r   rw  rB  r7  r8  s   @r^   r  r    s#       	!	0N .2= = = = S>	=
 "D(= $d*= !4'= $d*= = 
= = = = = =640    #E$6 # # # #.%. . . . ."T%"45 " " " ") )C4 )5CU ) ) ) ),D    6c#h 65;M 6 6 6 6p15 1S 1_ 1 1 1 1 1 1,(T ( ( ( (/ 7 /s /s / / / / / /
2)
214
2	
2 
2 
2 
2 
2 
2$    T#s(^    ' ' S/)*	
 
c?"	#   :7) 7) 7) 7) 7) 7) 7) 7)r]   r  c                   p     e Zd Zdedddeddf fdZdd	d
ee         dee	ef         def fdZ
ddZ xZS )!WrappedNestedUserFunctionVariabler  rM  rH   r   r_   Nc           	         |                     dd            |                     dd            |                     dd            |                     dd            |                     dd            |                     dd            |                     dd            |                     dd            t                                          |j        |j        |j        |j        |j        |j        |j	                   || _
        || _        d S )	Nr  r}   r   r   r   r   r   r  )r   r   r   r  r}   r   r   r   r   r  r  rM  r  s       r^   r   z*WrappedNestedUserFunctionVariable.__init__  s     	

9d###

64   

;%%%

:t$$$

<&&&

=$'''

9d###

<&&&OLO	
 	
 	
 r]   r   rF   r   c                 f   t           j        rLt          t          j                  }|                    || j        | j        gt          |          z   |          S | j        	                    |           t                                          |||          }| j                            |           |S r   r  r  s         r^   r(  z/WrappedNestedUserFunctionVariable.call_function*  r  r]   r7  rC   c                                            fd             j                                       t          dd                     d S )Nc                  $      j                   S r   r  r  s   r^   r  z?WrappedNestedUserFunctionVariable.reconstruct.<locals>.<lambda>;  r  r]   r:   Fr  r  s   ``r^   rB  z-WrappedNestedUserFunctionVariable.reconstruct:  r  r]   r  )rY   rZ   r[   r  r   r   r   r>   r   r4  r(  rB  r7  r8  s   @r^   r  r    s        + + 	
 
     4# ' S/)*	
 
      > > > > > > > >r]   r  c            	           e Zd Zddhej        Zddededz  deddf fdZdefdZdefdZ	e
ded	edd fd
            Zdddee         deeef         defdZdddedefdZdddedefdZdefdZdefdZdedefdZ xZS )SkipFunctionVariabler@  reasonNr   r_   c                 V     t                      j        di | || _        || _        d S r   )r   r   r@  r  )r|   r@  r  r   r   s       r^   r   zSkipFunctionVariable.__init__G  s2    ""6"""
r]   c                     | j         S r   r@  r   s    r^   rV  z'SkipFunctionVariable.as_python_constantL  
    zr]   c                     | j         S r   r!  r   s    r^   rY  z1SkipFunctionVariable.get_real_python_backed_valueO  r"  r]   r   c                 X   t          j        |dd          r]|}|}t          |dd          r(|j        }t	          |d          }t          |dd          (|                    t          j                   nt          j        |          r\t          |d          rt          |j        t          j                  r,t          |                    t          j                             n;t!          |          s,t          |                    t          j                              | ||          S )N_torchdynamo_disableF_torchdynamo_orig_callablerd  r   )r   r   r   r&  r)   rD  r'   rE  	isbuiltinr*  r   rd  r   
ModuleTyper(   BUILTIN_MATCHr7   )rG  r@  r   guard_on_sourceguard_on_values        r^   rH  z'SkipFunctionVariable.create_with_sourceR  s<    !%)?GG 	I %O"N.*FNN !/!J",#%A# # .*FNN  &&|'ABBBBu%% 	I
 5*-- M 02 2 M f//0JKKLLL077 	I &++L,FGGHHHs5((((r]   r   rF   r   c                 N   t          j        | j        dd          rOt          j        | j        dd           }t          dt	          | j                  d| j         d| ddg	           d S | j        t
          j        j        u rK|                    d
          }|r|	                                }t          dd| d| dd| dg	           d S | j        t
          j        j
        u rd|                    d
          }|r|	                                }nd}t          dd| d| dt          |j                   dd| dgd           d S | j        t
          j        j        u r>	 t          ddddg	           d S # t          $ r}t          |j                  d d }~ww xY w| j        t"          j        j        u r~t)          |          dv rC|sA|d         }|d         }	t+          |t,                    rt/          |j        |	|j                   S t          d!d"|  d#| d$| d%g t4          j        	           d S t8          j        rMdd&lm}
 |
                     || j                  }t+          |tB                    s|"                    |||          S tG          | j        d'd(          }tG          | j        d)d           }|d*nt	          |          }	 t          j$        | j                  }d+| d,| d-}d.| d/g}d0|vr|d1| d2d3gz  }n# tJ          $ r d4d5h}||v rd6| d| d/}d7d8g}n|_|&                    d9          rJd:| d| d}d;g}t
          j        j'        (                    |d<z   d<)                    |          z              nJd=| d| d>}d?d@g}t
          j        j'        (                    |d<z   d<)                    |          z              Y nw xY w|dAk    rdB}dCg}| j*        r| j*        }ndDdEl+m,}  || j                  }t          dFdG| dH| dI| ||	           d S )JNr%  F_torchdynamo_disable_msgz1Skip calling `torch.compiler.disable()`d functionzSkip calling function `z>` since it was wrapped with `torch.compiler.disable` (reason: r  z(Remove the `torch.compiler.disable` callrK  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.rM  z$Call to `torch._dynamo.skip_frame()`z/Called `torch._dynamo.skip_frame()` with args `z`. Skipping frame rN  z#User-inserted skip frame. Message: z-Remove the `torch._dynamo.skip_frame()` call.T)rL  rM  rN  rO  rU  z*Call to `torch._dynamo.step_unsupported()`zUser-inserted step_unsupported.z3Remove the `torch._dynamo.step_unsupported()` call.)r   r   r   r:   )rh  z!unsupported function.__get__ callzcall_function z, args: r  z^Dynamo only supports function.__get__(func, obj[, cls]) where func is a user-defined function.SourcelessBuilderr[   z<unknown qualname>rZ   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. r?  z*Dynamo cannot trace optree C/C++ function zi Consider using torch.utils._pytree - https://github.com/pytorch/pytorch/blob/main/torch/utils/_pytree.py
z/Dynamo does not know how to trace the builtin `z.` This function is either a Python builtin (e.g. _warnings.warn) or a third-party C/C++ Python extension (perhaps created with pybind).zIf it is a Python builtin, please file an issue on GitHub so the PyTorch team can add support for it and see the next case for a workaround.a  If it is a third-party C/C++ Python extension, please either wrap it into a PyTorch-understood custom operator (see https://pytorch.org/tutorials/advanced/custom_ops_landing_page.html for more details) or, if it is traceable, use `torch.compiler.allow_in_graph`.allow_in_graphztorch.compiler.allow_in_graph (or torch._dynamo.allow_in_graph) was called inside a compiled region. Dynamically annotating functions inside a compiled region is not supported.ziApply @torch.compiler.allow_in_graph as a decorator before compilation, not inside the compiled function.r   )get_skip_reasonz,Attempted to call function marked as skippedzmodule: z, qualname: z, skip reason: )-r   r   r@  r%   r4  ra   r  graph_breakr   rV  rU  r   f_codestep_unsupportedr&   r$   r  r   r   __get__r   r   r:  rg  r   r   r   SUPPORTABLEr   dont_skip_tracingbuilderr0  r   r  r(  r   getfiler   r  rb   	warn_oncejoinr  r  r5  )r|   r   r   r   r  graph_break_msgskip_frame_msgr  func_varobj_varr0  
rebuilt_fnqualname	module_orr  pathrN  rO  known_python_builtin_modulesr  r5  s                        r^   r(  z"SkipFunctionVariable.call_functiont  si    !$*.DeLL c	(5OQUVVCKDJAdj A A:=A A A ?      Z5=444$jj//O G"1"D"D"F"F?e4ee\beeeT?TTD	      Z5=333#ZZ..N $!/!B!B!D!D!#>B$ B B[a B B"3BI">">B B BR.RRC  	 	 	 	 	 	 Z5=999
7H AM	       7 7 7%ae,,$67Z5-555 4yyF""6"7q'h(<== - W    ;OOOtOOvOO96)56      ' F666666 /55b$*EE
!*.BCC F%33BfEEEtz>;OPPH
L$??I090A,,s9~~K8Ytz22>V^ > > $> > > 
 @8??? D((lU] l l l ;	 E  %Y %Y %Y06/D, <<<7'7 7*27 7 7  
iGEE *y/C/CH/M/M*"h{"h"h]e"h"h"hK^E
 M'11+2DtyyQVGWGW2WXXXXb+ b bX` b b b  m;E M'11+2DtyyQVGWGW2WXXXK%YL +++A 8 { 5999999(44F];]]H]]U[]]'	     s+   #E9 9
FFF8L COOr   c                 R    t          j        |t          | j        |                    S r   )r>   r   r*  r@  rZ  s      r^   r,  z%SkipFunctionVariable.call_obj_hasattr	  s#     $RT)B)BCCCr]   c           
          |t           v r7t          j        | |t          t	          | j        |                              S t          || j        | j        |          S )Nry  )r4   r   r{  r|  r   r@  r   r   rZ  s      r^   r!  z SkipFunctionVariable.var_getattr$	  s]    ))),dDT)B)B$C$C    b$*dk4@@@r]   c                     dS r1  r\   r   s    r^   r  z'SkipFunctionVariable.is_python_hashable,	  r3  r]   c                 *    t          | j                  S r   )r  r@  r   s    r^   r  z$SkipFunctionVariable.get_python_hash/	      DJr]   r  c                     t          |t                    o)|                                 |                                k    S r   )r   r>   rV  r  s     r^   r  z$SkipFunctionVariable.is_python_equal2	  s:    uo.. H''))U-E-E-G-GG	
r]   r   )rY   rZ   r[   r>   r  r   r4  r   rV  rY  r   r   rH  r   r   r(  r@   r,  r!  r6  r  r  r  r  r  r7  r8  s   @r^   r  r  @  s        
	'N c 3:  PT      
C    c     )s )F )?U ) ) ) [)Bi#i 'i S/)*	i
 
i i i iVD)D14D	D D D D
A5 AS A_ A A A AD             
V 
 
 
 
 
 
 
 
 
r]   r  c                   p     e Zd Zdedddeddf fdZdd	d
ee         dee	ef         def fdZ
ddZ xZS )WrappedSkipFunctionVariabler  rM  rH   r   r_   Nc                     |                     dd            |                     dd             t                      j        |j        fd|j        i| || _        || _        d S )Nr@  r  )r   r   r   r@  r  r  rM  r  s       r^   r   z$WrappedSkipFunctionVariable.__init__:	  sg     	

7D!!!

8T"""HHw~HHHHr]   r   rF   r   c                 f   t           j        rLt          t          j                  }|                    || j        | j        gt          |          z   |          S | j        	                    |           t                                          |||          }| j                            |           |S r   r  r  s         r^   r(  z)WrappedSkipFunctionVariable.call_functionF	  r  r]   r7  rC   c                                            fd             j                                       t          dd                     d S )Nc                  $      j                   S r   r  r  s   r^   r  z9WrappedSkipFunctionVariable.reconstruct.<locals>.<lambda>W	  r  r]   r:   Fr  r  s   ``r^   rB  z'WrappedSkipFunctionVariable.reconstructV	  r  r]   r  )rY   rZ   r[   r  r   r   r   r>   r   r4  r(  rB  r7  r8  s   @r^   rP  rP  9	  s        
%
 +
 	

 

 
 
 
 
 
# ' S/)*	
 
      > > > > > > > >r]   rP  c                        e Zd ZdZdefdZdedededdf fdZdefd	Z	defd
Z
dej        fdZdddedef fdZd Zdee         fdZdddee         deeef         defdZdefdZ xZS )WrapperUserFunctionVariablea3  
    Used to represent a wrapper object that contains the actual callable as an
    attribute. For example, torch.jit.script/trace have the original function at
    their _torchdynamo_inline attribute. Similarly, functions with
    __script_if_tracing_wrapper have the original attr at "__original_fn".
    r_   c                     t           j        S r   r`  r   s    r^   ra  z'WrapperUserFunctionVariable.python_typed	  rb  r]   wrapper_objattr_to_tracer   Nc                 V     t                      j        di | || _        || _        d S r   )r   r   rX  rY  )r|   rX  rY  r   r   s       r^   r   z$WrapperUserFunctionVariable.__init__g	  s6    ""6"""&*r]   c                     | j         j        S r   )rX  rZ   r   s    r^   r  z&WrapperUserFunctionVariable.get_modulel	  s    **r]   c                     | j         j        S r   )rX  rY   r   s    r^   r   z$WrapperUserFunctionVariable.get_nameo	  s    ((r]   c                 4    |                                  j        S r   )r  rk   r   s    r^   r   z$WrapperUserFunctionVariable.get_coder	  s      ""++r]   r   rF   r   c                     || j         k    rLt          | j        | j                   }| j        ot	          | j        |          }t          j        |||          S t                                          ||          S r   )	rY  r   rX  r   r)   r>   r   r   r!  )r|   r   r   r   r   r   s        r^   r!  z'WrapperUserFunctionVariable.var_getattru	  sm    4%%%$*D,>??C[BZT%B%BF"(S&999ww""2t,,,r]   c                 6    t          | j        | j                  S r   r   rX  rY  r   s    r^   r  z(WrapperUserFunctionVariable.get_function|	      t');<<<r]   c                     g S r   r\   r   s    r^   r&  z%WrapperUserFunctionVariable.self_args	  r/  r]   r   c           
         t          | j        d          rt          | j        | j        d           }t          |dd          pd}|                    dd          d         dk    r|                                }t          j                            |j	                  }|j
        }d	| d
| dd                    t          j        |g                     }	t          j        j                            |	           t          j        j        j                            d          }
|
                    t$          j                  rt          j        j                                        }t3                      |z   }|d         j	        |d         j
        f}d                    t          j        |                    }d|d          d
|d          d}|t5          |          z  }|
                    |           |                                 t;          |          z   }t=          j        |t@          j!                  "                    || t=          j        || j                  g||          S )N
cache_inforZ   rM  rN  r:   )maxsplitr   ra   zGDynamo detected a call to a `functools.lru_cache`-wrapped function at ':z'. Dynamo ignores the cache wrapper and directly traces the wrapped function. Silent incorrectness is only a *potential* risk, not something we have observed. Enable TORCH_LOGS=+dynamo for a DEBUG stack trace.

This call originates from:
ztorch._dynamoz)call to a lru_cache wrapped function at: r3  )#r*  rX  r   rY  splitframe_summaryosrG  basenamefilenamelinenor?  	tracebackformat_listra   r  rb   r>  logging	getLoggerisEnabledForDEBUG_guardsTracingContextextract_stackr   r4  debugr&  r   r>   r   r   getattr_and_tracer(  )r|   r   r   r   	target_fnr  ri  rl  rm  r  dynamo_logger
user_stack	frame_locuser_stack_formatteduser_stack_traceall_argss                   r^   r(  z)WrapperUserFunctionVariable.call_function	  sR    4#\22 	: 0$2DdKKI!)\2>>D"K  q 11!4?? " 0 0 2 27++M,BCC&-I$,I I/5I I wwy4m_EEFFI I  #--c222 % 3 ; E Eo V V --gm<< :!&!=!K!K!M!MJ!7!9!9J!FJ!+B!8*R.:O PI+-7793H3T3T+U+U('rS\]^S_'r'rbklmbn'r'r'r$$,@(A(AA$!''(8999>>##d4jj0$'
 
 -?(T-?@@L8L
 
	
r]   c                 6    t          | j        | j                  S r   r`  r   s    r^   rY  z8WrapperUserFunctionVariable.get_real_python_backed_value	  ra  r]   )rY   rZ   r[   r   r|  ra  r   r4  r   r  r   r   r5  r   r>   r!  r  r   r&  r   r   r(  r  rY  r7  r8  s   @r^   rV  rV  \	  s        "T " " " "+C + +s +t + + + + + +
+C + + + +)# ) ) ) ),%. , , , ,-5 -S -_ - - - - - -= = =40    -
#-
 '-
 S/)*	-

 
-
 -
 -
 -
^=f = = = = = = = =r]   rV  c            
       \     e Zd ZdZdefdZdededededdf
 fd	Z	de
e         fd
Z xZS )WrapperUserMethodVariablez
    Similar to WrapperUserFunctionVariable, but for methods. The only delta is
    saving the vt for `self` object of the method which is then used by
    WrapperUserFunctionVariable in `call_function` method.
    r_   c                     t           j        S r   r  r   s    r^   ra  z%WrapperUserMethodVariable.python_type	  r  r]   rX  rY  self_objr   Nc                 L     t                      j        ||fi | || _        d S r   )r   r   r  )r|   rX  rY  r  r   r   s        r^   r   z"WrapperUserMethodVariable.__init__	  s1     	m>>v>>>r]   c                     | j         gS r   r  r   s    r^   r&  z#WrapperUserMethodVariable.self_args	  r  r]   )rY   rZ   r[   r   r|  ra  r   r4  r>   r   r   r&  r7  r8  s   @r^   r  r  	  s          T          "	
  
     40        r]   r  c                  R    t           j                                        rddlm}  | S i S )Nr   traceable_collective_remaps)ra   distributedis_available)torch.distributed._functional_collectivesr  r  s    r^   _traceable_collective_remapsr  	  sD    %%'' +	
 	
 	
 	
 	
 	
 +*Ir]   c                     t           j                                        s
J d            |t                                                      v sJ |j        }|                     d          }t          ||          S )NzIllegal invocation.z)torch.distributed._functional_collectives)ra   r  r  r  valuesrY   import_sourcer)   )r   r   
inner_namepath_sources       r^   _traceable_collectives_sourcer  	  sw     ))++BB-BBB+-//66888888J""#NOOKk:...r]   c                       e Zd ZdZdedef         dededdf fdZed	d
dedef         de	dedd f
d            Z
ededefd            Zed	d
dedef         deeef         fd            Zd	d
dee         deeef         defdZ xZS )!CollectiveFunctionRewriteVariablea  
    Some of the torch.distributed.* collective APIs are possible to rewrite to 'traceable' collectives.

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

    In the case that a function is 'remappable' but only for some combinations of call-time arguments,
    we check the args at `call_function` time and fall back to graph-breaking if needed.  This is no worse
    than status-quo as we currently graph-break on all distributed.* collectives.
    r   .replacement_varr   r_   Nc                x     t                      j        |fi | t          |t                    sJ || _        d S r   )r   r   r   r:  r  )r|   r   r  r   r   s       r^   r   z*CollectiveFunctionRewriteVariable.__init__	  sI     	&&v&&&/+?@@@@@.r]   r   rF   old_fnr   optionsc                 x    t                               | |          \  }}t          |ft          |fd|i||d|S )Nr   )r  r   )r  r  r:  )r   r  r   r  new_fn
new_sources         r^   r   z(CollectiveFunctionRewriteVariable.create	  sb     ?FFr6RR
0
0VV
VgVV
 
 	
 
 	
r]   variablec                 J    t          j        |           o| t                      v S r   )r   
isfunctionr  )r  s    r^   can_rewritez-CollectiveFunctionRewriteVariable.can_rewrite
  s(     x((WX9U9W9W-W	
r]   c                 N    t                      |         }|t          | |          fS r   )r  r  )r   r   r  s      r^   r  z)CollectiveFunctionRewriteVariable.rewrite
  s*     .//34R@@@@r]   r   c                 T	   dd l m} ddlm} t	          j        | j                  }t           |j        |i |j	                  }d}d|v rN|d         
                                r4t          d| j         d|d|d| j         g t          j        	           | j        |j        k    rt          j        st          d
| j         ddg	           |d         }t#          |t$          j                  s$t(          j        j                            d          t1                      }t1                      }	t1                      }
t1                      }d }|j        D ]}|                                |j        ur$t(          j        j                            d          |                    |d          }|                                rp|
                                }||j        |j        fvr't(          j        j                            d|           t$          j         !                    |j"                  }nitG          |d          r2t$          j         !                    |$                                          }n't(          j        j                            d|           |%                    |           |%                    |                    |d                     |	%                    |                    |d                     |
%                    |                    |d                     ||                    |d          }|J d}t%          j        |          t%          j        |	          t%          j        |
          t%          j        |          |d}| j&        '                    |||          S | j        |j        |j        fv rBt          j        st          d| j         ddg	           | j&        '                    |||          S | j        |j(        |j)        |j*        fv rd|+                    d          }||j,        n|j-        d         j.        }||vrt_          d|           ta          j1        |||                   |d<   | j&        '                    |||          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 rK  z.P2P compilation disabled for batch_isend_irecvzP2P compilation is disabled.z3Set TORCHDYNAMO_ENABLE_P2P_COMPILATION=1 to enable.p2p_op_listz`P2POp` used incorrectlyopzunexpected P2POp op r   zunexpected P2POp op variable tensorpeertaggroup)op_list	peer_listtag_listtensors
group_namez(P2P compilation disabled for isend/irecvzUnsupported all_reduce op: )2torch.distributedr  r  r  r   r  r   r   r  r  rV  r%   r   r:  batch_isend_irecvr   enable_p2p_compilationr   r   rM   ra   r  rd  InternalTorchDynamoErrorr   r   ra  P2POpr!  r  isendirecvr@   r   rY   r*  r   ra  r  r(  
all_reducereduce_scatter_tensor_reduce_scatter_baser   r@  
parametersdefaultro  r>   r   )r|   r   r   r   distr  r  p2p_opsopspeerstagsr  	group_varitemop_varr  new_args
new_kwargsreduce_op_var	reduce_ops                       r^   r(  z/CollectiveFunctionRewriteVariable.call_function
  s    	)(((((NNNNNN %dg..	nind5f55?@@F:$6$I$I$K$KC788d88v88[RVRY[[&2	    7d,,,0 L#wL >M	    ]+Ggy'=>> m'@@.   *.CFFE66DffG04I > >##%%TZ77-+DD2   ))"d33,,.. 2244B$*dj!999#m/HH7277   '7>>r{KKFFVZ00 &7>>v?P?PQQFF-+DD@@@   

6"""t//H==>>>T--b&99:::D,,R77888$ $ 0 0W = =I(((46H$1#66&3E::%2488$1'::'6 6J '55b(JOOO7tz4:...0 F#wL >M	    '55b$GGG7O&%	
 
 
 #JJt,,M !, ##)$/7 
  000 !Jy!J!JKKK*05Ei5PQQF4L#11"dFCCCr]   )rY   rZ   r[   r   r   r   r:  r   r'  r   r   r6  r  r   r)   r  r   r>   r   r4  r(  r7  r8  s   @r^   r  r  	  s        	/S#X	/ .		/
 	/ 
	/ 	/ 	/ 	/ 	/ 	/ 
#
c"
 
 	

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

 A#A)1#s();A	sJ	A A A \AvD#vD 'vD S/)*	vD
 
vD vD vD vD vD vD vD vDr]   r  c                   N    e Zd ZdefdZdddee         deeef         defdZ	dS )	CollectionsNamedTupleFunctionr_   c                     | j         S r   r\  r   s    r^   rV  z0CollectionsNamedTupleFunction.as_python_constant
  r]  r]   r   rF   r   r   c                    t          ||          }|r	  | j        d |D             i d |                                D             }nH# t          $ r;}t	          t          |          |t          |j                             Y d }~nd }~ww xY wt          j	        |t                                S t          dd|d|dg t          j        	           d S )
Nc                 6    g | ]}|                                 S r\   r  r   r  s     r^   r  z?CollectionsNamedTupleFunction.call_function.<locals>.<listcomp>
  $    ;;;a**,,;;;r]   c                 >    i | ]\  }}||                                 S r\   r  r   r   r   s      r^   r   z?CollectionsNamedTupleFunction.call_function.<locals>.<dictcomp>
  *    LLLTQq!..00LLLr]   )r   r  znamedtuple constructionzargs=r  z?`torch.compile` only support certain input types for namedtuplerK  )r2   r   r   r   r"   r|  r   r   r   UserDefinedClassVariabler=   r%   r   r:  )r|   r   r   r   constant_argsr@  rd  s          r^   r(  z+CollectionsNamedTupleFunction.call_function
  s@    ,D&99 	
;;d;;;LLV\\^^LLL     (IIch         5.00    	-)t))))Y".		
 	
 	
 	
 	
 	
s   3A 
B1BBN)
rY   rZ   r[   r   rV  r   r>   r   r4  r(  r\   r]   r^   r  r  
  sx        C    
#
 '
 S/)*	

 

 
 
 
 
 
r]   r  c                   6    e Zd Zej        Zdhej        Z	 ddedee         de	e
ef         dededdf fdZdefd	ZddZdefdZdddee         de	e
ef         defdZddde
defdZddde
defdZdefdZdefdZdefdZdefdZdedefdZ xZS )FunctoolsPartialVariableoriginal_cache_hashNrh   r   keywordsr   r_   c                 
    t                      j        di | || _        t          |t                    sJ || _        t          |t                    sJ || _        t          j	        t                    | _        || _        d S r   )r   r   rh   r   r   r   r   r  r  partialr5   
fake_valuer  )r|   rh   r   r  r  r   r   s         r^   r   z!FunctoolsPartialVariable.__init__
  s     	""6"""	$%%%%%	(D)))))  $+H55#6   r]   c                     t           j        S r   )r  r  r   s    r^   ra  z$FunctoolsPartialVariable.python_type
  s      r]   r7  rC   c                 ^                        fd            | j                   | j        r                    | j                   | j        s:                    t          t          | j                  dz   d                     d S                     | j                                                   t          | j        
                                          }                                        t          |          t          | j                  z   dz   |d                     d S )Nc                  0                          dd          S )Nr  r  r  r
  s   r^   r  z6FunctoolsPartialVariable.reconstruct.<locals>.<lambda>
  s    g&>&>{I&V&V r]   r:   F)r  rh   r   foreachr  r  r   r   r  r   keyscreate_call_function_kw)r|   r7  r  s    ` r^   rB  z$FunctoolsPartialVariable.reconstruct
  s   VVVVWWW	9 	'OODI&&&} 	!!"6s49~~7I5"Q"QRRRF,,..///T]''))**++CIIDI,F,JDRWXX	
 	
 	
 	
 	
r]   c                 *    |                                  S r   r  r   s    r^   r  z%FunctoolsPartialVariable.get_function
      &&(((r]   r   rF   c                 ~    | j         t          |          z   }i | j        |}| j                            |||          S r   )r   r   r  rh   r(  )r|   r   r   r   merged_argsmerged_kwargss         r^   r(  z&FunctoolsPartialVariable.call_function
  sA     i$t**,34=3F3y&&r;FFFr]   r   c                 v    t          j        |t          t          j        t
                    |                    S r   )r>   r   r*  r  r  r5   rZ  s      r^   r,  z)FunctoolsPartialVariable.call_obj_hasattr
  s,     $R1B81L1Ld)S)STTTr]   c           
         | j         ot          | j         |          }|dk    r| j        S |dk    rt          j        | j        |          S |dk    r;fd| j                                        D             }t          j        ||          S |t          v r<t          j
        | |t          t          t          j        |                              S t          t                      d S )Nrh   r   r   r  c                 B    i | ]\  }}t          j        |          |S r\   r>   r   r   r   r   r   s      r^   r   z8FunctoolsPartialVariable.var_getattr.<locals>.<dictcomp>
  s,    WWWA_*2q111WWWr]   ry  )r   r)   rh   r   rM   r   r  r   r  r4   r{  r|  r   r  r  r"   r   )r|   r   r   r   r   s    `   r^   r!  z$FunctoolsPartialVariable.var_getattr
  s    >DK!>!>6>>96>>)$)FCCCC:WWWWATATAVAVWWWE.uVDDDD))),dD1BD)I)I$J$J    	!44444r]   c                     t          j        | j                                        gd | j        D             R i d | j                                        D             S )Nc                 6    g | ]}|                                 S r\   r  r  s     r^   r  z?FunctoolsPartialVariable.as_python_constant.<locals>.<listcomp>  s$    <<<3c$$&&<<<r]   c                 >    i | ]\  }}||                                 S r\   r  r  s      r^   r   z?FunctoolsPartialVariable.as_python_constant.<locals>.<dictcomp>	  s*    KKKTQq!&&((KKKr]   )r  r  rh   rV  r   r  r   r   s    r^   rV  z+FunctoolsPartialVariable.as_python_constant  sq     I((**
<<$)<<<
 
 
 LKT]5H5H5J5JKKK
 
 	
r]   c                     t          j        | j                                        gd | j        D             R i d | j                                        D             }| j        | j        |_        |S )zcSimilar to as_python_constant(), but add ID_MATCH guards to try to force things to become constantsc                 6    g | ]}|                                 S r\   guard_as_python_constant)r   r   s     r^   r  zEFunctoolsPartialVariable.guard_as_python_constant.<locals>.<listcomp>  s$    >>>qa((**>>>r]   c                 >    i | ]\  }}||                                 S r\   r  r  s      r^   r   zEFunctoolsPartialVariable.guard_as_python_constant.<locals>.<dictcomp>  s*    QQQ41aq!,,..QQQr]   )	r  r  rh   r  r   r  r   r  
cache_hashr|   r   s     r^   r  z1FunctoolsPartialVariable.guard_as_python_constant  s    "I..00
>>DI>>>
 
 
 RQ4=;N;N;P;PQQQ
 
 #/ $ 8Fr]   c                     | j                                         oMt          d | j        D                       o/t          d | j                                        D                       S )Nc              3   >   K   | ]}|                                 V  d S r   r  r  s     r^   r  z>FunctoolsPartialVariable.is_python_hashable.<locals>.<genexpr>  s.      BBC**,,BBBBBBr]   c              3   >   K   | ]}|                                 V  d S r   r  r   r@  s     r^   r  z>FunctoolsPartialVariable.is_python_hashable.<locals>.<genexpr>  s.      SS5E,,..SSSSSSr]   )rh   r  r  r   r  r  r   s    r^   r  z+FunctoolsPartialVariable.is_python_hashable  sh    I((** TBB	BBBBBTSSDM<P<P<R<RSSSSS	
r]   c                     | j                                         }d | j        D             }d | j                                        D             }t          |g||R           S )Nc              3   >   K   | ]}|                                 V  d S r   r  r  s     r^   r  z;FunctoolsPartialVariable.get_python_hash.<locals>.<genexpr>!  s.      @@sS((**@@@@@@r]   c              3   >   K   | ]}|                                 V  d S r   r  r  s     r^   r  z;FunctoolsPartialVariable.get_python_hash.<locals>.<genexpr>"  s.      SS5u,,..SSSSSSr]   )rh   r  r   r  r  r  )r|   	func_hash	args_hashvalues_hashs       r^   r  z(FunctoolsPartialVariable.get_python_hash  si    I--//	@@di@@@	SSDM<P<P<R<RSSSY99[99:::r]   r  c                 v   t          |t                    o| j                            |j                  ot	          d t          | j        |j                  D                       oTt	          d t          | j                                        |j                                                  D                       S )Nc              3   F   K   | ]\  }}|                     |          V  d S r   r  )r   arg_aarg_bs      r^   r  z;FunctoolsPartialVariable.is_python_equal.<locals>.<genexpr>)  sH        "UE %%e,,     r]   c              3   F   K   | ]\  }}|                     |          V  d S r   r  )r   value_avalue_bs      r^   r  z;FunctoolsPartialVariable.is_python_equal.<locals>.<genexpr>-  sH        &Wg ''00     r]   )	r   r  rh   r  r  r   r   r  r  r  s     r^   r  z(FunctoolsPartialVariable.is_python_equal%  s    u677 	))%*55  &)$)UZ&@&@       *-M((**EN,A,A,C,C+ +    	
r]   r   r  )rY   rZ   r[   r  r  r  r>   r  r   r   r4  r   r   r|  ra  rB  r  r(  r@   r,  r!  rV  r  r6  r  r  r  r  r  r7  r8  s   @r^   r  r  
  s5       %M 			'N $(7 77 '7 sO+,	7
 !7 7 
7 7 7 7 7 7(!T ! ! ! !
 
 
 
)c ) ) ) )G#G 'G S/)*	G
 
G G G GU)U14U	U U U U55 5S 5_ 5 5 5 5 
C 
 
 
 

# 
 
 
 

D 
 
 
 
; ; ; ; ;
V 
 
 
 
 
 
 
 
 
r]   r  c            
           e Zd Zdddhej        Zeej        dee	de
f         ej        f         fd                        Zede
dedd fd	            Zded
e
ddf fdZede	de
f         fd            ZdefdZde
fdZdddee         d
eeef         defdZdddedee         d
eeef         def
dZde
fdZ xZS )PolyfilledFunctionVariabler   r  traceable_fnr_   .c                     i S r   r\   )rG  s    r^   _get_polyfill_handlersz1PolyfilledFunctionVariable._get_polyfill_handlers>  s	     	r]   r@  r   c                 t    t          |                    t          j                              | ||          S rB  rC  rF  s      r^   rH  z-PolyfilledFunctionVariable.create_with_sourceC  s8     	f''(BCCDDDs5((((r]   r   Nc                 v    t                      j        di | || _        |                                                     ||          }d }t          |          sJ d| d|             dD ]*}t          ||d           }|rt          |          sJ |} n+t          d| d          || _        || _	        d S )NzPolyfill handler z is not callable for )__torch_dynamo_polyfill____python_implementation__z# does not have a traceable functionr\   )
r   r   r   r  r   callabler   r  r  r  )r|   r   r   handlerr  candidate_attr	candidater   s          r^   r   z#PolyfilledFunctionVariable.__init__K  s   ""6"""--//33B;;  XX"Xg"X"XTV"X"XXX 
 	 	N  >>I 	*****(
 PGPPP   " ,r]   c                     | j         S r   )r  r   s    r^   polyfill_fnz&PolyfilledFunctionVariable.polyfill_fne  s      r]   c                 .    t          | j        dd          S )N*__torch_dynamo_can_constant_fold_through__F)r   r  r   s    r^   can_constant_fold_throughz4PolyfilledFunctionVariable.can_constant_fold_throughi  s    OI5
 
 	
r]   c                 *    |                                  S r   r  r   s    r^   r  z'PolyfilledFunctionVariable.get_functionn  r  r]   r   rF   r   c                 N   |                                  rXt          ||          rH | j        d |D             i d |                                D             }t	          j        ||          S | j        t          j        u rt          |          dk    r|st          |d         t          j        t          j        f          rt          d |d         j        D                       rt          j                            ||j                            dt$          j        t)          d |d         j        D                       fi           t%          j        d |d         j        D                       	          S t	          j        || j                  }|                    |||          S )
Nc                 6    g | ]}|                                 S r\   r  r  s     r^   r  z<PolyfilledFunctionVariable.call_function.<locals>.<listcomp>|  r  r]   c                 >    i | ]\  }}||                                 S r\   r  r  s      r^   r   z<PolyfilledFunctionVariable.call_function.<locals>.<dictcomp>}  r  r]   r:   r   c              3      K   | ]t}|                                 r't          |                                t                    p4t          |t          j                  o|                                t          u V  ud S r   )r  r   rV  r  r   SymNodeVariablera  r  s     r^   r  z;PolyfilledFunctionVariable.call_function.<locals>.<genexpr>  s          %%''SJq7K7K7M7Ms,S,S Yq)";<<WTWAW     r]   r(  c              3   >   K   | ]}|                                 V  d S r   as_proxy)r   as     r^   r  z;PolyfilledFunctionVariable.call_function.<locals>.<genexpr>  s*      ??A1::<<??????r]   c                 l    g | ]1}|                                 r|                                n|j        2S r\   )r  rV  sym_numr  s     r^   r  z<PolyfilledFunctionVariable.call_function.<locals>.<listcomp>  sQ          !3355+A00222!"	  r]   )r'  )r  r3   r   r   r>   r   builtinssumr   r   r   rM   r  r  r!  r   r   create_proxyra   sym_sumr   r  r(  )r|   r   r   r   r   traceable_function_variables         r^   r(  z(PolyfilledFunctionVariable.call_functionq  s    ))++ 		50M&1
 1
 		5 ;;d;;;LLV\\^^LLL   #(V444 Gx|##D		Q 47Y%;Y=T$UVV     a      ,33	&&#M??a?????A	    "&a  	 	 4   ( '6&;B@Q&R&R#*88T6JJJr]   r   c                 J   |dk    r|                      |||          S t          | j        |d           }|s*t          |          st	          |d| d| j                    i }| j        rt          | j        |          |d<   t          |fi |}|                     |||          S )N__call__zCannot find callable z in r   )r(  r   r   r6   r#   r   r)   r  )r|   r   r   r   r   methodr  polyfilled_method_variables           r^   r   z&PolyfilledFunctionVariable.call_method  s     :%%b$777$-- 	N+f-- 	NR!L!L!L47!L!LMMM; 	> *4; = =GH%?%R%R'%R%R")77D&IIIr]   c                     | j         S r   r\  r   s    r^   rV  z-PolyfilledFunctionVariable.as_python_constant  r]  r]   )rY   rZ   r[   r>   r  r   r  cacher   r   r   r   r   r  r   rH  rO   r   propertyr  r6  r  r  r   r4  r(  r   r   rV  r7  r8  s   @r^   r  r  6  s        
	'	N _tHS#X,>@R,R'S    _ [ ))!')	%) ) ) [)-2 - - - - - - - -4 !Xc3h/ ! ! ! X!
4 
 
 
 

)c ) ) ) )2K#2K '2K S/)*	2K
 
2K 2K 2K 2KhJ#J J ?#	J
 S/)*J 
J J J J&C        r]   r  c                        e Zd Zdededdf fdZdefdZdd
ZdddefdZ	ddde
e         deeef         defdZ xZS )SysFunctionVariabler@  r   r_   Nc                 H     t                      j        di | || _        d S r   r   r   r@  r|   r@  r   r   s      r^   r   zSysFunctionVariable.__init__  +    ""6"""


r]   c                     t           j        S r   )r   BuiltinFunctionTyper   s    r^   ra  zSysFunctionVariable.python_type  s    ((r]   r   rF   variables.TupleVariablec                 N   t          |j                  rC|j        d         }|j        }|                    |d          }t	          j        ||          ||g}n;t          j        d           t          j        d           t          j        d           g}t          j	        |          S )Nrg  __traceback__)
r   exn_vt_stackr  r!  r>   r   r@   r   r   r  )r|   r   exnr  tbr   s         r^   exc_infozSysFunctionVariable.exc_info  s    r 
	/"%C,C_55B$*2s33S"=EE !'-- '-- '--E
 &u---r]   c                 B    |                      |          j        d         S )Nr:   )rB  r   r   s     r^   	exceptionzSysFunctionVariable.exception  s    }}R  &q))r]   r   c                     | j         t          j        u r|                     |          S | j         t          j        u sJ |                     |          S r   )r@  r   rB  rD  r'  s       r^   r(  z!SysFunctionVariable.call_function  sL     :%%==$$$zS]****~~b!!!r]   )r   rF   r_   r<  )rY   rZ   r[   r   r   r|  ra  rB  r>   rD  r   r   r4  r(  r7  r8  s   @r^   r5  r5    s        c S T      )T ) ) ) ). . . .*3 * * * * *
"#
" '
" S/)*	
"
 

" 
" 
" 
" 
" 
" 
" 
"r]   r5  ) create_tma_experimental_metadatacreate_tma_stable_metadataTMADescriptorMetadataTritonHOPifierc                      e Zd ZdedefdZdedefdZdede	fdZ
dd	deej        j        j        d
f         fdZde	deee	f         ddde	fdZded
e	f         dee         deeef         ded         de	defdZde	ded         de	dedef
dZde	ded         dee	         fdZdede	fdZdddee	         ddfdZddde	deee	f         dddd f
d!Zd"S )#DynamoTritonHOPifierr  r_   c                 2    t          ddd| g            d S )Nz!triton kernel unsupported featurerM  z/Encountered triton kernel unsupported feature: rK  )r%   )r|   r  s     r^   raise_unsupportedz&DynamoTritonHOPifier.raise_unsupported  s7    7O#OO		
 	
 	
 	
 	
 	
r]   maybe_callablec                 :    t          |t          t          f          S r   )r   r  r:  )r|   rN  s     r^   is_callablez DynamoTritonHOPifier.is_callable  s     79MN
 
 	
r]   r   c                     |j         S r   r!  )r|   r   s     r^   	get_valuezDynamoTritonHOPifier.get_value  s
    yr]   gridrL   .c                     ddl m} t          ||          r|                                S t	          ddt          |           dg t          j                   d S )Nr:   )rL   z/unsupported grid type for triton hop check_gridzgrid type = z;`torch.compile` only supports list-like grid for check_gridrK  )listsrL   r   r$  r%   r|  r   r:  )r|   rS  rL   s      r^   
check_gridzDynamoTritonHOPifier.check_grid  s}    ++++++d,-- 
	==??"I3tDzz33Y&2	     r]   metar   rF   c                 x    fd|                                 D             }|                    |gi           }|S )Nc                 B    i | ]\  }}t          j        |          |S r\   r  r  s      r^   r   z2DynamoTritonHOPifier.call_grid.<locals>.<dictcomp>  s,    MMM1O)"a00!MMMr]   )r   r(  )r|   rS  rW  r   meta_vars      ` r^   	call_gridzDynamoTritonHOPifier.call_grid
  sE     NMMM

MMM!!"xj"55r]   user_fnr   r   r  c                 l    ddl m} |                    ||          }|                    |||          }|S )Nr:   r/  )r<  r0  r   r(  )	r|   r\  r   r   r   r  r0  wrapped_user_functionr   s	            r^   call_user_defined_fnz)DynamoTritonHOPifier.call_user_defined_fn  sH     	/..... 1 8 8W E E&44RvFFr]   user_objr   c                 l    ddl m} |J   ||t          |j        |                     |          }|S )Nr:   )VariableBuilder)r<  rb  r)   kernel_source)r|   r`  r   r  r   rb  wrapped_user_objs          r^   wrap_user_defined_objz*DynamoTritonHOPifier.wrap_user_defined_obj   s^     	-,,,,,~~~
??
81d9==
 

   r]   configsc                 H    |                     |          }d |D             }|S )Nc                 ,    g | ]} |j                     S r\   r  )r   r   s     r^   r  z=DynamoTritonHOPifier.maybe_unpack_configs.<locals>.<listcomp>9  s$    KKK26244KKKr]   )unpack_var_sequence)r|   rf  r   s      r^   maybe_unpack_configsz)DynamoTritonHOPifier.maybe_unpack_configs2  s2     --b11 LK7KKKr]   r   c                 |    |                                 s|                     d           |                                S )Nz`@triton.heuristics must return constant values because configs can only contain constant values.)r  rM  r  r  s     r^   maybe_unpack_heuristic_resultz2DynamoTritonHOPifier.maybe_unpack_heuristic_result=  sE    ((** 	""r   ..000r]   TritonKernelVariablec                     |j         t          |          dk    r|                     d            t          |          |j        |j        |d         |j                  S )Nr:   z7Triton kernels should be called with only a single gridr   )kernel
kernel_idxrS  rc  )rS  r   rM  r|  ro  rp  rc  )r|   r  r   s      r^   call_getitemz!DynamoTritonHOPifier.call_getitemG  sn     =$D		Q""I   tH~~?*a"0	
 
 
 	
r]   gridscombined_argszvariables.ConstantVariablec                    ddl m} i }t          |                                          D ]T}||         }t	          |t
          t          f          r.|                                ||<   |                                ||<   Ufd|	                                D             }	ddl
m}
m} d |	                                D             }d |		                                D             }|                                D ]d}|                                }|                                s:|                                s&|                     dt%          |           d	           e|
                    |          } ||t(                    }j                            d
|d|j        ||||                                d           t3          j        d           S )Nr:   )r  c                 B    i | ]\  }}t          j        |          |S r\   r  r  s      r^   r   z1DynamoTritonHOPifier.call_HOP.<locals>.<dictcomp>q  s9     
 
 
041O!"a((!
 
 
r]   r   )kernel_side_tabletriton_kernel_wrapper_mutationc                     i | ]C\  }}t          |t                    |                                .||                                DS r\   )r   r>   r  rV  r  s      r^   r   z1DynamoTritonHOPifier.call_HOP.<locals>.<dictcomp>}  s`     
 
 
1!_--
 342F2F2H2H
q##%%
 
 
r]   c                 l    i | ]1\  }}t          |t                    r|                                .||2S r\   )r   r>   r  r  s      r^   r   z1DynamoTritonHOPifier.call_HOP.<locals>.<dictcomp>  sT     
 
 
1q/22
 897K7K7M7M
q
 
 
r]   z.Unexpected argument type for a Triton kernel: rN  r(  r\   )rp  constant_args_idxrS  tma_descriptor_metadatar   )dictsr  r   r  r   !TMADescriptorExperimentalVariableTMADescriptorStableVariableto_metadata
get_tensorr   *torch._higher_order_ops.triton_kernel_wraprv  rw  r  r$  r  is_symnode_likerM  repradd_constant_argsr   r   r*  rp  r$  r>   r   )r|   r  rr  rs  r   r  r{  r   r   combined_args_vtrv  rw  r  non_constant_argsrz  rW  s       `           r^   call_HOPzDynamoTritonHOPifier.call_HOPY  sK    	-,,,,, :<m((**++ 	2 	2Aa A57RS  2 ./]]__'*#$<<>>a 
 
 
 
8E8K8K8M8M
 
 
	
 	
 	
 	
 	
 	
 	
 	

 
%++--
 
 


 
(..00
 
 
 #))++ 	 	A		AKKMM Q%6%6%8%8 &&OT!WWOOO   .??NN  !2D99
	*&1%6+B--// 		
 	
 	
 $
 
 	
r]   N)rY   rZ   r[   r4  r   rM  r>   r6  rP  r   rR  r   ra   fxproxyProxyrV  r   r[  r   r   r   r_  re  r   rj  rl  rq  r  r\   r]   r^   rK  rK    s       
S 
U 
 
 
 

/ 
d 
 
 
 

_     1 eEHN<PRU<U6V    #CH~3J	   #s(# ' S/)*	
 ,-  
      ,-  	 
   
       $		 ()@ A		c	 	 	 	1O 1 1 1 1 1
(
 sm
 
 	
 
 
 
$H
(H
 H
 CH~	H

 $H
 
&H
 H
 H
 H
 H
 H
r]   rK  c            
           e Zd ZU ded<   ded<   edz  ed<   edz  ed<   dededz  deded	df
 fd
Zdddee	         de
ee	f         d	e	fdZddde	d	e	fdZdddedee	         de
ee	f         d	e	f
 fdZded	efdZ xZS )rm  rI   rS  rJ   ro  Nrp  rc  r   r_   c                     |                     d|                    d                    | _         t                      j        di | t
                              | |||           d S )Nrc  r   r\   )r   r   rc  r   r    dynamo_triton_hopifier_singletoninit_variable)r|   ro  rp  rS  r   r   s        r^   r   zTritonKernelVariable.__init__  se     $ZZH9M9MNN""6"""(66tVZQUVVVVVr]   r   rF   r   c                 <    t                               | |||          S r   )r  call_triton_kernelr'  s       r^   r(  z"TritonKernelVariable.call_function  s%     0BB$
 
 	
r]   r  c                 :    t                               | |g          S r   )r  rq  )r|   r   r  s      r^   mp_subscript_implz&TritonKernelVariable.mp_subscript_impl  s     0<<TC5IIIr]   r   c                     |dk    rt                               | |||          S t                                          ||||          S )Nrun)r  call_runr   r   r   s        r^   r   z TritonKernelVariable.call_method  sI     5==3<<T4QSTTT ww""2tT6:::r]   r  c                     ddl m} ddlm} t	          ||          r" |j        |                                          S |S )Nr:   r?   )r!  )r  r@   r  r!  r   r   evaluate_expr)r|   r  r@   r!  s       r^   specialize_symbolicz(TritonKernelVariable.specialize_symbolic  s`    ......++++++ c?++ 	@*#*3+<+<+>+>???
r]   )rY   rZ   r[   r   r  r   r   r   r   r>   r   r4  r(  r  r   r   r  r7  r8  s   @r^   rm  rm    s        
d
D=   WW'*TzW9<WHKW	W W W W W W
#
 '
 S/)*	

 

 
 
 
J#J J 
	J J J J;#; ; ?#	;
 S/)*; 
; ; ; ; ; ;s s        r]   rm  c                   p     e Zd Zdddee         dee         dededdf fd	Zdefd
ZddZdefdZ	 xZ
S )r}  data_ptrzvariables.DataPtrVariabledims
block_dimselement_sizer   r_   Nc                     t          |t          j                  sJ  t                      j        di | || _        || _        || _        || _        d S r   )	r   r   DataPtrVariabler   r   r  r  r  r  )r|   r  r  r  r  r   r   s         r^   r   z*TMADescriptorExperimentalVariable.__init__  s^     (I$=>>>>>""6""" 	$(r]   c                     t          d | j        D             d | j        D             | j                                                  S )Nc                 6    g | ]}|                                 S r\   r#  r   dims     r^   r  zATMADescriptorExperimentalVariable.to_metadata.<locals>.<listcomp>  s     111S\\^^111r]   c                 6    g | ]}|                                 S r\   r#  r  s     r^   r  zATMADescriptorExperimentalVariable.to_metadata.<locals>.<listcomp>  s     777S\\^^777r]   )rF  r  r  r  r$  r   s    r^   r  z-TMADescriptorExperimentalVariable.to_metadata  sL    /11ty11177t777&&((
 
 	
r]   r7  rC   c                                           fd            j                                       g  j         j         j        }                    |                               t          |          dz   d           d S )Nc                  \                          ddt          j                   d          S )Nz$triton.tools.experimental_descriptorcreate_d_tma_descriptor)r	  r   r  r  s   r^   r  z?TMADescriptorExperimentalVariable.reconstruct.<locals>.<lambda>  s0    G,,6:#di..:::  r]   r:   F)	r  r  rB  r  r  r  r  r(  r   )r|   r7  r   s   `` r^   rB  z-TMADescriptorExperimentalVariable.reconstruct  s        	
 	
 	
 	!!'***@@T_@d.?@c$ii!mU33333r]   c                     | j         j        S r   )r  from_tensorr   s    r^   r  z,TMADescriptorExperimentalVariable.get_tensor  s    }((r]   r  )rY   rZ   r[   r   r>   r   r   r  rB  r  r7  r8  s   @r^   r}  r}    s        )-) ?#) )	)
 &) ) 
) ) ) ) ) )
S 
 
 
 

4 
4 
4 
4)O ) ) ) ) ) ) ) )r]   r}  c                   P     e Zd Zdddddeddf fdZdefd	ZddZdefdZ xZS )r~  r  rN   block_shaperM   r   r_   Nc                     |                                 sJ  t                      j        di | || _        || _        d S r   )r  r   r   r  r  )r|   r  r  r   r   s       r^   r   z$TMADescriptorStableVariable.__init__  sO     !!!!!""6"""&r]   c                 N    t          | j                                                  S r   )rG  r  r$  r   s    r^   r  z'TMADescriptorStableVariable.to_metadata  s&    )%%''
 
 	
r]   r7  rC   c                                          fd                               d           | j                                        | j                                       d           d S )Nc                  0                          dd          S )Nztriton.tools.tensor_descriptorTensorDescriptorr  r
  s   r^   r  z9TMADescriptorStableVariable.reconstruct.<locals>.<lambda>  s    G,,0"  r]   r  r   )r  load_methodr  rB  r  r   r  s    `r^   rB  z'TMADescriptorStableVariable.reconstruct  s       	
 	
 	
 	M***((( !!!Ar]   c                     | j         S r   )r  r   s    r^   r  z&TMADescriptorStableVariable.get_tensor"  r   r]   r  )	rY   rZ   r[   r   r   r  rB  r  r7  r8  s   @r^   r~  r~    s        	' 	' $	' 		'
 
	' 	' 	' 	' 	' 	'
S 
 
 
 


 
 
 
C        r]   r~  c                   l     e Zd Zdededdf fdZdefdZddd	ee	         de
ee	f         de	fd
Z xZS )'CreateTMADescriptorExperimentalVariablerankr   r_   Nc                 T    |dv sJ  t                      j        di | || _        d S )N)r:   r   r\   )r   r   r  )r|   r  r   r   s      r^   r   z0CreateTMADescriptorExperimentalVariable.__init__'  s:    
 v~~~~""6"""			r]   c                     t           j        S r   r`  r   s    r^   ra  z3CreateTMADescriptorExperimentalVariable.python_type0  rb  r]   r   rF   r   c                    d|v r|d         n|d         }t          |t          j                  s,t          dd| d| d| j         dd| j         d	g
           | j        dk    r~t          |          t          |          z   dk    r0t          |dt          |          t          |          z               d|v r|d         n|d         g}d|v r|d         n|d         g}nt          |          t          |          z   dk    r0t          |dt          |          t          |          z               d|v r|d         n|d         d|v r|d         n|d         g}d|v r|d         n|d         d|v r|d         n|d         g}d|v r|d         n|d         }t          |t          j                  sJ t          ||||          S )Nptrr   z.invalid ptr argument for create_tma_descriptorzargs = z, kwargs = z#Expected `ptr` argument of `create_z]d_tma_descriptor`to be from a `.data_ptr()` call, represented internally by `DataPtrVariable`z`torch.compile` may fail to internally represent result of `.data_ptr()` with `DataPtrVariable` due to a graph break between the `.data_ptr()` call and `create_zTd_tma_descriptor`. Please ensure there were no graph breaks between these two calls.rK  r:   rQ   z6TMA metadata rank=1 requires exactly 4 arguments, got r  	block_dimr      z6TMA metadata rank=2 requires exactly 6 arguments, got dim1dim0
block_dim1r   
block_dim0r  rg  )r  r  r  r  )r   r   r  r%   r  r   r#   r}  )r|   r   r   r   r  r  r  r  s           r^   r(  z5CreateTMADescriptorExperimentalVariable.call_function3  sk     %fUmmDG#y899 	H;$;;6;;_$) _ _ _/#y/ / /    9>>4yy3v;;&!++ fSQUYYY\]cYdYdMdff  
 "'&ud1gD (3f'<'<{##$q'JJ 4yy3v;;&!++ fSQUYYY\]cYdYdMdff  
 #)F"2"2vQ"(F"2"2vQD
 )5(>(>|$$DG(4(>(>|$$DGJ 2@61I1Ivn--tTVx #y8999990!%	
 
 
 	
r]   )rY   rZ   r[   r  r   r   r|  ra  r   r>   r   r4  r(  r7  r8  s   @r^   r  r  &  s          
	     "T " " " ":
#:
 ':
 S/)*	:

 
:
 :
 :
 :
 :
 :
 :
 :
r]   r  c                   N    e Zd ZdefdZdddee         deeef         defdZ	dS )	!CreateTMADescriptorStableVariabler_   c                     t           j        S r   r`  r   s    r^   ra  z-CreateTMADescriptorStableVariable.python_typeq  rb  r]   r   rF   r   r   c                 t    d|v r|d         n|d         }d|v r|d         n|d         }t          ||          S )Nr  r   r  r:   )r  r  )r~  )r|   r   r   r   r  r  s         r^   r(  z/CreateTMADescriptorStableVariable.call_functiont  s\     &.%7%7!!T!W/</F/Ff]++DQRG*#
 
 
 	
r]   N)
rY   rZ   r[   r|  ra  r   r>   r   r4  r(  r\   r]   r^   r  r  p  sx        "T " " " "
#
 '
 S/)*	

 

 
 
 
 
 
r]   r  c                   F    e Zd ZdZdddee         deeef         defdZdS )	!PyTreeGetNodeTypeFunctionVariableaK  
    `torch.utils._pytree._get_node_type` function is very hot function. We want to special case it to reduce Dynamo tracing time.

    def _get_node_type(tree: Any) -> Any:
        node_type = type(tree)
        # All namedtuple types are implicitly registered as pytree nodes.
        # XXX: Other parts of the codebase expect namedtuple types always return
        #      `namedtuple` instead of the actual namedtuple type. Even if the type
        #      is explicitly registered.
        if is_namedtuple_class(node_type):
            return namedtuple
        return node_type
    r   rF   r   r   r_   c                    t          |          dk    r t          |dt          |                      d }|d         j        rQt          |d         j                            t
          j                             t          |d         j                  }|d                                         }t          |          r8t          t          d          d          }t          j        |t          |          S t          j        |||          S )Nr:   z6pytree_get_node_type requires exactly 1 argument, got r   collectionsr   r   )r   r#   r   r(   rD  r'   
TYPE_MATCHr1   ra  r   r)   r/   r>   r   r   )r|   r   r   r   type_sourcera  s         r^   r(  z/PyTreeGetNodeTypeFunctionVariable.call_function  s     t99>>XSQUYYXX   7> 	5$q'.33L4KLLMMM$T!W^44K1g))++{++ 	F$\-%@%@,OOK"(ZEEE$R[IIIIr]   N)	rY   rZ   r[   r   r   r>   r   r4  r(  r\   r]   r^   r  r    sq         J#J 'J S/)*	J
 
J J J J J Jr]   r  c                   P     e Zd ZdZdddee         deeef         def fdZ xZ	S ) PyTreeTreeIsLeafFunctionVariablea@  
    `torch.utils._pytree.tree_is_leaf` function is a hot function. We want to special case it to reduce Dynamo tracing time.

    def tree_is_leaf(
        tree: PyTree,
        is_leaf: Callable[[PyTree], bool] | None = None,
    ) -> bool:
        if is_leaf is not None and is_leaf(tree):
            return True
        return _get_node_type(tree) not in SUPPORTED_NODES

    When is_leaf is None (the common case), we can optimize by not tracing into the function.
    When is_leaf is not None, we fall back to regular tracing since it requires executing user code.
    r   rF   r   r   r_   c                    t          |          dk     st          |          dk    r t          |dt          |                      |                    dt          j        d                     }t          |          dk    r|d         }|                                s#t                                          |||          S |d         }t          t          j
        j        j                                      ||gi           }t          j        |t          j
        j        j        t!                                }|                    |d|gi           }t          j        ||j                   S )Nr:   r   z,tree_is_leaf requires 1 or 2 arguments, got rS   r   r   r  )r   r#   r   r@   r   rr  r   r(  r  ra   rb   rc   _get_node_typer>   r   rd   re   r   r@  )
r|   r   r   r   rS   treenode_type_varsupported_nodes_varoutr   s
            r^   r(  z.PyTreeTreeIsLeafFunctionVariable.call_function  sR    t99q==CIIMMN3t99NN  
 **Y(8(?(E(EFFt99>>1gG'')) 	;77((T6::: Aw9K.
 

-TFB
'
' 	 .3K/466
 
 

 "--b.=/SUVV$RSY777r]   r	  r8  s   @r^   r  r    s|         #8##8 '#8 S/)*	#8
 
#8 #8 #8 #8 #8 #8 #8 #8 #8 #8r]   r  c                   d     e Zd ZdZdededdf fdZddd	ee         dee	ef         defd
Z
 xZS ) SparseTensorCreationSkipVariablezk
    Skip variable for sparse tensor factory functions with clear messaging regarding lack of support.
    r@  r   r_   Nc                 D    d} t                      j        |fd|i| d S )Nz8sparse tensor creation is not supported in torch.compiler  )r   r   )r|   r@  r   r  r   s       r^   r   z)SparseTensorCreationSkipVariable.__init__  s2    K88v888888r]   r   rF   r   c                     ddl m} t          | j        dt	          | j                            }t          dd| d| dg |j                   d S )	Nr   )r   rY   z$Sparse tensor creation not supportedz
function: zEtorch.compile does not support sparse tensor creation functions like zZ. Sparse tensors require specialized handling that is not yet implemented in the compiler.rK  )rM  r   r   r@  r4  r%   SPARSE_TENSOR)r|   r   r   r   r   r  s         r^   r(  z.SparseTensorCreationSkipVariable.call_function  s     	)((((($*j#dj//BB:***kX_ k k k 5%34	
 	
 	
 	
 	
 	
r]   )rY   rZ   r[   r   r   r   r   r>   r   r4  r(  r7  r8  s   @r^   r  r    s         9c 9S 9T 9 9 9 9 9 9
#
 '
 S/)*	

 

 
 
 
 
 
 
 
r]   r  	real_implc                     ddl mc m} ddlm}m}m} d }dg} ||||          \  }	}
 ||	          } ||
          }|                    di f          d         }dt          dt          d	t          f fd
} || d|           || d|           || d|          df} j
                            d||i            dS )aI  Emit an invoke_leaf_function node for a side-effectful function with no
    tensor inputs or outputs.

    The function is captured as a closure inside _LeafCallable objects and
    registered as a static attribute on the graph module.  Because
    invoke_leaf_function is registered as EffectType.ORDERED, effect tokens
    prevent DCE and maintain execution ordering relative to other ops.

    Use this when Dynamo needs to preserve a pure-side-effect call (like
    setting global runtime state) in the compiled graph so that it replays
    at the correct position at runtime.
    r   N)_LeafCallableinvoke_leaf_functionmake_leaf_function_wrappersc                      d S r   r\   r\   r]   r^   	fake_implz5emit_noargs_leaf_function_to_graph.<locals>.fake_impl  r3  r]   r\   r:   	attr_namer   r_   c                 p    j                             | |          }t          |          |j        _        |S r   )r   %register_static_attr_and_return_proxyr|  r  )r  r   r  r   s      r^   
make_proxyz6emit_noargs_leaf_function_to_graph.<locals>.make_proxy  s.    	??	3OOs))
r]   _real_fn_fake_fn_input_specrM  r(  )torch.utils._pytreerb   rc   ,torch._higher_order_ops.invoke_leaf_functionr  r  r  tree_flattenr4  r   r   r*  )r   r  r   pytreer  r  r  r  captured_out_specwrapped_realwrapped_fakereal_callablefake_callable
input_specr  invoke_argss   `               r^   "emit_noargs_leaf_function_to_graphr    sr   " )((((((((            8<f!<!<9/" "L, "M,//M!M,//M$$b"X..q1Jc         	
d$$$m44
d$$$m44
d'''44
	K I?,@+rRRRRRr]   c                   p     e Zd ZdZdededdf fdZdefdZdd	d
ee	         de
ee	f         de	fdZ xZS )TritonSetAllocatorVariablezTrace triton.set_allocator as an invoke_leaf_function node in the
    graph so that it executes at the right point at runtime, ordered by
    effect tokens.r@  r   r_   Nc                 H     t                      j        di | || _        d S r   r7  r8  s      r^   r   z#TritonSetAllocatorVariable.__init__1  r9  r]   c                 *    t          | j                  S r   )r|  r@  r   s    r^   ra  z&TritonSetAllocatorVariable.python_type5  rM  r]   r   rF   r   c                     t          |          dk    r|rJ |d                                         | j        fd}t          ||d           t	          j        d           S )Nr:   r   c                                   d S r   r\   )alloc_fnset_allocators   r^   r  z;TritonSetAllocatorVariable.call_function.<locals>.real_implD  s    M(###4r]   	set_alloc)r   rV  r@  r  r@   r   )r|   r   r   r   r  r  r  s        @@r^   r(  z(TritonSetAllocatorVariable.call_function8  s     4yyA~~f~~,7--// 
	 	 	 	 	 	 	+2y+FFF&t,,,r]   )rY   rZ   r[   r   r   r   r|  ra  r   r>   r   r4  r(  r7  r8  s   @r^   r  r  ,  s         c S T       T        -#- '- S/)*	-
 
- - - - - - - -r]   r  r   )r   r(  r  r   r   rp  rj  r   rn  r   r  r   collections.abcr   r   r   r   typingr   r	   r
   r   r   r   typing_extensionsr   weakrefr   ra   torch._dynamo.excr   torch._guardsr   r  r   rM  r   r   r   r   bytecode_transformationr   r   r   rd  r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   guardsr'   r(   r   r)   r*   r+   r,   r-   r.   r/   r0   r1   rb   r2   r3   r4   r5   r6   r7   r8   r9   r  r;   r<   r=   r>   r  r@   user_definedrA   #torch.distributed.fsdp._fully_shardrB   ModuleNotFoundErrortorch._dynamo.codegenrC   r=  rD   rE   rF   rG   #torch._dynamo.variables.ctx_managerrH   r  rI   rJ   r|  rK   rU  rL   rM   r  rN   rO   rx   rz   r  r  r  rR  rV   r   r  rX   	lru_cachere   rg   r   r   r4  r   r   r   r5  r   r   r  r   r   r   r   r:  r  r  r  r)  r  r  r  rg  r  r  r  r  r  r  rP  rV  r  r  r  r  r  r  r  r5  rF  rG  rH  rI  rK  r  rm  r}  r~  r  r  r  r  r  r  r  r\   r]   r^   <module>r	     s1    .            				 



      " " " " " " . . . . . . . . ( ( ( ( ( ( ( ( G G G G G G G G G G G G G G G G # # # # # # % % % % % %  4 4 4 4 4 4             3 3 3 3 3 3 > > > > > > > > > > > > V V V V V V V V V V                            1 0 0 0 0 0 0 0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	            ' & & & & & 3 3 3 3 3 3EEEEEEE     '//////            LKKKKK       
 *)))))55555555&&&&&& WT#s(+,,,
&Y'O'O'OPP "+)YK"8"8  D  ,=+<+>+>sCx( > > >	 	 	 	 	0 	 	 	 :    
 
 
 
 
 
 
 
>L \    U
UU }U 3-	U
 cNU 
#
U U U Ur DHA AA&)A39D=AA A A A.0 .$sCx. .T . . . . # o%&  .  
	       *
.CH~  FCK 4'	
 8_t# S#X% c3h$&    4
 
 
 --%+-5;d]-JM-- - - -:D D D D D D D DNOY OY OY OY OY3 OY OY OYd!7 !7 !7 !7 !73 !7 !7 !7H) ) ) ) )": ) ) )X :  :  :  :  :4  :  :  :FJ; J; J; J; J;? J; J; J;Z

 
 
 
 
 
 
 
S6 S6 S6 S6 S6%= S6 S6 S6l         "     Fw w w w w- w w wt >  >  >  >  > 2  >  >  >F> > > > >"6 > > >D$$c$ $ ?
#	$
 o%&$ $ $ $ $NK) K) K) K) K)!9 K) K) K)\.> .> .> .> .>(B .> .> .>bv
 v
 v
 v
 v
? v
 v
 v
r >  >  >  >  >"6  >  >  >FV= V= V= V= V=": V= V= V=r     ;   0d38n    //%-c3h%7// / / /hD hD hD hD hD(< hD hD hDV"
 "
 "
 "
 "
$8 "
 "
 "
J@
 @
 @
 @
 @
 @
 @
 @
FC C C C C C C CL#" #" #" #" #"/ #" #" #"L           w
 w
 w
 w
 w
> w
 w
 w
t $8#7#9#9  4 4 4 4 4? 4 4 4n$) $) $) $) $) $) $) $)N    /   BG
 G
 G
 G
 G
o G
 G
 G
T
 
 
 
 
 
 
 
&!J !J !J !J !J(< !J !J !JH38 38 38 38 38'; 38 38 38l
 
 
 
 
'; 
 
 
:/S/SD!/S /S 
	/S /S /S /Sd- - - - - - - - - -s   -C4 4C>=C>