o
    "i                     @   s  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mZm	Z	m
Z
m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 ddlmZmZmZmZ dd	lmZmZm Z m!Z!m"Z" d
dl#m$Z$m%Z%m&Z& d
dl'm(Z( e
rzd dl)m*Z* d0ddZ+dd Z,dd Z-dd Z.G dd de&Z/G dd de/Z0G dd de0Z1G dd de1Z2G dd de0Z3d d! Z4G d"d# d#e/Z5G d$d% d%e&Z6d&d' Z7d(d) Z8G d*d+ d+e0Z9G d,d- d-e&Z:G d.d/ d/e&Z;dS )1    N)DictListOptionalTYPE_CHECKINGUnion   )	variables)create_call_functioncreate_rot_n)unimplementedUnsupported)GuardBuilderinstall_guard)
AttrSourceConstantSourceDefaultsSourceGetItemSource)check_constant_argsget_first_attridentityistype	make_cell   )MutableLocaltypestrVariableTrackerConstantVariable)Sourcec                 C   s8   t |tr|S |sddlm} || |S tj||S )Nr   SourcelessBuilder)
isinstancer   torch._dynamo.variables.builderr    creater   LazyVariableTracker)txvalsourcer     r(   _/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/torch/_dynamo/variables/functions.pywrap_bound_arg   s   
r*   c                 C   s6   t | D ]\}}t|ttfrt| |||< qd S N)listitemsr!   tupledictr*   )r%   resultkvr(   r(   r)   wrap_args_kwargs(   s
   r3   c                 C   sH   t  }| jj}|jD ]}| ||< ||v r!||| || q
|S r+   )r/   outputside_effectsco_cellvarstrack_cell_new
store_cellpop)parentr0   codeclosure_cellsr5   namer(   r(   r)   init_cellvars/   s   
r>   c           
      C   sd   ddl m} || ||||}||_t|tr"ddlm}	 t|	|}|d u s-t|ts-J ||_|S )Nr   )FunctionTypepairwise)	typesr?   __kwdefaults__r!   r.   	itertoolsrA   r/   __annotations__)
r;   	f_globalsr=   defaultsclosure
kwdefaultsannotationsr?   funcrA   r(   r(   r)   _create_nested_fn<   s   
rL   c                   @   sT   e Zd Zdd Zdd Z							
dddZded	efddZdd Z	dd Z
dS )BaseUserFunctionVariablec                 C   
   |   jS r+   )get_codeco_filenameselfr(   r(   r)   get_filenameQ      
z%BaseUserFunctionVariable.get_filenamec                 C   rN   r+   )rO   co_namerQ   r(   r(   r)   get_nameT   rT   z!BaseUserFunctionVariable.get_nameargsList[VariableTracker]kwargsDict[str, VariableTracker]returnr   c                 C   s   | | g |  ||S r+   )inline_user_function_return	self_argsrR   r%   rW   rY   r(   r(   r)   call_functionW   s   z&BaseUserFunctionVariable.call_functionr=   c                 C   sL   d}z	t |  |}W n ty   |dkrt| trd}Y nw tj|S )NF__name__T)hasattrget_functionNotImplementedErrorr!   NestedUserFunctionVariabler   r   r#   rR   r%   r=   r0   r(   r(   r)   call_hasattr\   s   z%BaseUserFunctionVariable.call_hasattrc                 C   s   t t|  jS r+   )r,   inspect	signaturerb   
parametersrQ   r(   r(   r)   inspect_parameter_namesf   s   z0BaseUserFunctionVariable.inspect_parameter_namesc                 C   s   i S r+   r(   )rR   r%   r(   r(   r)   closure_varsi      z%BaseUserFunctionVariable.closure_varsNrW   rX   rY   rZ   r[   r   )r`   
__module____qualname__rS   rV   r_   strr   rf   rj   rk   r(   r(   r(   r)   rM   P   s    

rM   c                       s   e Zd ZdZddhejZedd Zd& fdd	Z fd	d
Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd ZdedefddZ		 	!	"		#d' fd$d%Z  ZS )(UserFunctionVariablez-Some unsupported user-defined global functionfnis_constantc                 C      t |tj | ||dS Nr'   )r   
make_guardr   CLOSURE_MATCHclsvaluer'   r(   r(   r)   create_with_sourcev   
   z'UserFunctionVariable.create_with_sourceFc                    s   t  jd	i | t|ddrd| _nd| _t|tjtjj	fs,J dt
| d| t|d|}t|ddrAt|d|}|| _d S )
N_dynamo_marked_constantFTzexpected FunctionType found  _torchdynamo_inline__script_if_tracing_wrapper__original_fnr(   )super__init__getattrrs   r!   rB   r?   torchjitScriptFunctionr   rg   getattr_staticrr   )rR   rr   rs   rY   	__class__r(   r)   r   ~   s   
zUserFunctionVariable.__init__c                    s   t | tr| jS t  S r+   )r   rq   rr   r   as_python_constantrQ   r   r(   r)   r      s   

z'UserFunctionVariable.as_python_constantc                 C      g S r+   r(   rQ   r(   r(   r)   r]      rl   zUserFunctionVariable.self_argsc                 C      | j S r+   )rr   rQ   r(   r(   r)   rb         z!UserFunctionVariable.get_functionc                 C      | j jS r+   )rr   __code__rQ   r(   r(   r)   rO         zUserFunctionVariable.get_codec                 C      t jS r+   )rB   r?   rQ   r(   r(   r)   python_type   r   z UserFunctionVariable.python_typec                 C   s   t | jdd d uS )N__self__)r   rr   rQ   r(   r(   r)   has_self      zUserFunctionVariable.has_selfc                 C   r   r+   )rr   __globals__rQ   r(   r(   r)   get_globals   r   z UserFunctionVariable.get_globalsc              
      sj  j rJ |jj}tjt|dj}|jpg }fddt|D }t	
|j|j|jtfddt||D |j}|jrWfdd|jD   fdd|j D |_t|j|i |}	|	  t|	j }
t||
 t||
|j}jjp~d}t|tjjjksJ tt jjj|D ]\}}}|d	krjrt jd	nd }t!j"|j#|d
|
|< q|$||}|d ur||
|< qjr!ddl%m&} |jj'}||v r|| }n@t(t jd|}t |d}z
||||j#}W n t)y   t!* }Y nw |+ |j,vr||
|< q|-||}|.|| ||
|< qddl%m/} |0||j#|
|< q|
|fS )N)r%   c                    s*   g | ]\}} j d u rd nt j |qS r+   r'   r   ).0idx_rQ   r(   r)   
<listcomp>   s    z2UserFunctionVariable.bind_args.<locals>.<listcomp>c                    s   g | ]
\}} ||d qS )r&   r'   r(   )r   argr'   )wrapr(   r)   r      s    
c                    s,   i | ]}| j d u rd nt j |ddqS )NT)is_kwr   )r   r1   rQ   r(   r)   
<dictcomp>   s    
z2UserFunctionVariable.bind_args.<locals>.<dictcomp>c                    s"   i | ]\}}|| | d qS r   r(   r   r1   r2   )kwdefaults_sourcesr   r(   r)   r          r(   r   rv   r   )VariableBuilder__closure__cell_contentsr   )1rs   r4   root_tx	functoolspartialr*   rr   __defaults__	enumeraterB   r?   r   r   r`   r.   zipr   rC   r-   rg   rh   bindapply_defaultsr/   	argumentsr3   r>   lenco_freevarsrD   countr'   r   r   UserDefinedClassVariabler   match_nested_cellbuilderr   r5   r   
ValueErrorDeletedVariabler=   mutated_closure_cell_contentstrack_cell_existingr8   r    r#   )rR   r:   rW   rY   r%   rr   rG   defaults_sources	fake_funcboundr0   r<   rH   r   r=   cellr'   varr   r5   outclosure_cellclosure_cell_contentscontents_varr    r(   )r   rR   r   r)   	bind_args   s   








zUserFunctionVariable.bind_argsc                 C   s   d S r+   r(   )rR   r:   childr(   r(   r)   export_freevars  rl   z$UserFunctionVariable.export_freevarsr=   r[   c                 C   s   t | j|}tj|S r+   )ra   rr   r   r   r#   re   r(   r(   r)   rf     s   z!UserFunctionVariable.call_hasattrrW   rX   rY   rZ   r   c                    s,   | j rt|| j|  ||S t |||S r+   )rs   invoke_and_store_as_constantrr   rV   r   r_   r^   r   r(   r)   r_     s
   z"UserFunctionVariable.call_function)Frm   )r`   rn   ro   __doc__rM   _nonvar_fieldsclassmethodr|   r   r   r]   rb   rO   r   r   r   r   r   rp   r   rf   r_   __classcell__r(   r(   r   r)   rq   m   s6    
nrq   c                       s^   e Zd ZdZ fddZdd Zdd Zdd	 Z	
					d fddZ fddZ	  Z
S )UserMethodVariablez$Some unsupported user-defined methodc                    s    t  jdd|i| || _d S )Nrr   r(   )r   r   obj)rR   rr   r   rY   r   r(   r)   r   +  s   
zUserMethodVariable.__init__c                 C   s   | j j d| j d| j dS )N(z, ))r   r`   rr   r   rQ   r(   r(   r)   __str__/  s   zUserMethodVariable.__str__c                 C   s   | j gS r+   )r   rQ   r(   r(   r)   r]   2  r   zUserMethodVariable.self_argsc                 C   r   r+   )rB   
MethodTyperQ   r(   r(   r)   r   5  r   zUserMethodVariable.python_typerW   rX   rY   rZ   r[   r   c                    s   |j  r1t| jtjr1t| jdd}|d ur |dr |dks#| j	r1| jj
|| jj||| j	dS | j	rGt| jj| jj}t|||  ||S t |||S )Nrn    z	torch.nn.ztorch.nn.utils.parametrize)constant)r4   is_root_tracerr!   r   r   NNModuleVariabler   rr   
startswithrs   call_methodr`   r{   r   rV   r   r_   )rR   r%   rW   rY   module_attrrr   r   r(   r)   r_   8  s"   z UserMethodVariable.call_functionc                    s   t   dd  S )Nr   )r   rj   rQ   r   r(   r)   rj   Z  r   z*UserMethodVariable.inspect_parameter_namesrm   )r`   rn   ro   r   r   r   r]   r   r_   rj   r   r(   r(   r   r)   r   (  s    "r   c                       6   e Zd Z fddZ						d fd	d
Z  ZS )WrappedUserMethodVariablec                    sB   | dd  | dd  t j|j|jfi | || _|| _d S Nrr   r   )r9   r   r   rr   r   wrappedcontextrR   r   r   rY   r   r(   r)   r   _  s
   
z"WrappedUserMethodVariable.__init__rW   rX   rY   rZ   r[   r   c                    ,   | j | t |||}| j | |S r+   r   enterr   r_   exitrR   r%   rW   rY   r0   r   r(   r)   r_   f     z'WrappedUserMethodVariable.call_functionrm   r`   rn   ro   r   r_   r   r(   r(   r   r)   r   ^      r   c                       r   )WrappedUserFunctionVariablec                    s>   | dd  | dd  t j|jfi | || _|| _d S r   )r9   r   r   rr   r   r   r   r   r(   r)   r   p  s
   
z$WrappedUserFunctionVariable.__init__rW   rX   rY   rZ   r[   r   c                    r   r+   r   r   r   r(   r)   r_   w  r   z)WrappedUserFunctionVariable.call_functionrm   r   r(   r(   r   r)   r   o  r   r   c                    sT   dd   fdd|D } fdd|  D }||i |}| jj||t|dS )Nc                 S   s   t | tjr
|  S |  S r+   )r!   r   TensorVariableget_real_valuer   )xr(   r(   r)   convert  s   z-invoke_and_store_as_constant.<locals>.convertc                    s   g | ]} |qS r(   r(   r   r   r   r(   r)   r         z0invoke_and_store_as_constant.<locals>.<listcomp>c                    s   i | ]	\}}| |qS r(   r(   r   r   r(   r)   r         z0invoke_and_store_as_constant.<locals>.<dictcomp>rv   )r-   r4   register_attr_or_moduler   )r%   rr   r=   rW   rY   resr(   r   r)   r     s   r   c                       sv   e Zd ZddhejZ	d fdd	Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Z  ZS )rd   closure_scoperF   Nc
                    s   t  jdi |
 t| tsJ t| tjsJ t|ts#J || _|| _	|| _
|| _|| _|| _|| _|d u r>d }|| _|	| _d S Nr(   )r   r   r!   r   rp   rB   CodeTyper/   fn_namer;   rF   rG   rI   rJ   rH   r   wrapped_reconstructible)rR   r   r;   rF   rG   rI   rJ   rH   r   r   rY   r   r(   r)   r     s    z#NestedUserFunctionVariable.__init__c                 C   r   r+   r(   rQ   r(   r(   r)   r]     rl   z$NestedUserFunctionVariable.self_argsc                 C   s
   | j  S r+   )r;   r   rQ   r(   r(   r)   rO     rT   z#NestedUserFunctionVariable.get_codec                 C   s   | j rtt| j | j| j }| jr| j |_	| j
r%| j
 |_| jrH| j }t|tr>ddlm} t||}t|tsEJ ||_|S )Nr   r@   )rH   rc   rB   r?   r;   r   rF   r   rG   r   rI   rC   rJ   r!   r.   rD   rA   r/   rE   )rR   rK   rJ   rA   r(   r(   r)   rb     s&   

z'NestedUserFunctionVariable.get_functionc                 C   s
   | j d uS r+   )rH   rQ   r(   r(   r)   has_closure  rT   z&NestedUserFunctionVariable.has_closurec                 C   s   dS )NFr(   rQ   r(   r(   r)   r     rl   z#NestedUserFunctionVariable.has_selfc                 C   r   r+   )rF   rQ   r(   r(   r)   r     r   z&NestedUserFunctionVariable.get_globalsc                 C   sd  ddl m} |  }t|| j| j | jrt	| jj
nd t	dd tt|  jD }| jr7| j |_t|j|i |}|  t|j
 }t|jj| t|||}	t|jD ]N\}
}| jj
|
 }t||||kssJ ||vsyJ t||r|}|r||jvr|j }|r||jvs|d u rt!d| d|j| ||< q_| jj
|
 |	|< q_||	fS )Nr   )InlinedClosureVariablec                 s   s    | ]}t d V  qd S r+   )r   )r   r   r(   r(   r)   	<genexpr>  s    z7NestedUserFunctionVariable.bind_args.<locals>.<genexpr>zCouldn't find z7 in the symbolic_locals of the inline interpreter stack)"miscr   rO   rB   r?   rF   r   r   rG   r.   r-   ranger   r   rI   keys_as_python_constantrC   rg   rh   r   r   r/   r   r3   r4   r   r>   r   rH   r   r!   symbolic_localsr:   RuntimeError)rR   r:   rW   rY   r   r;   rK   r   r0   r<   r   r=   r   candr(   r(   r)   r     s@   

z$NestedUserFunctionVariable.bind_argsc                 C   s2   |   }|jD ]}||jv r|j| |j|< qd S r+   )rO   r   r   )rR   r:   r   r;   r   r(   r(   r)   r   	  s   

z*NestedUserFunctionVariable.export_freevarsc                 C   sd  | td || j ||| jg |t| jjj	 | j
r(|| j
 n	||d g | jr:|| j n	||d g | jrL|| j n	||d g | jrwz| j }|||g W n tyv   || j Y n
w ||d g |tddd | jr| dd || j |tdd |td |tdd d S d S )	NrL      T)	push_nullr   wrapsr   r   )load_import_fromr`   r;   extend_output_create_load_constrF   r   r#   r{   rU   rG   create_load_constrH   rI   rJ   r   rc   r	   r   r
   )rR   codegenrJ   r(   r(   r)   reconstruct  s:   


z&NestedUserFunctionVariable.reconstructr+   )r`   rn   ro   rM   r   r   r]   rO   rb   r   r   r   r   r   r
  r   r(   r(   r   r)   rd     s"     +rd   c                       st   e Zd ZddhejZd fdd	Zdd Zdd	 Zed
d Z	e
eddd Z						dddZ  ZS )SkipFunctionVariabler{   reasonNc                    s"   t  jdi | || _|| _d S r   )r   r   r{   r  )rR   r{   r  rY   r   r(   r)   r   >  s   
zSkipFunctionVariable.__init__c                 C   s
   t | jS r+   )typer{   rQ   r(   r(   r)   r   C  rT   z SkipFunctionVariable.python_typec                 C   r   r+   )r{   rQ   r(   r(   r)   r   F  r   z'SkipFunctionVariable.as_python_constantc                 C   rt   ru   )r   rw   r   FUNCTION_MATCHry   r(   r(   r)   r|   I  r}   z'SkipFunctionVariable.create_with_sourcec                   C   s   t jtjiS r+   )collections
namedtupler   r   r(   r(   r(   r)    fold_through_function_to_wrapperQ  s   z5SkipFunctionVariable.fold_through_function_to_wrapperrW   rX   rY   rZ   r[   r   c           	         s  t | jddrtd| j  d S | j|   v rAt |rA| jdd  D i dd | D }|  | j|t	 dS | jt
ju rj|sjt d	krj d
 jd us_ d
 |jjrj fdd}t|S zt | j}d| jj d| d}W n5 ty   ddh}| jj|v rd| jj d| jj d}nd| jj d| jj d}tjj| Y nw || jrd| j dnd7 }t| d S )N_torchdynamo_disableFz.call torch._dynamo.disable() wrapped function c                 S      g | ]}|  qS r(   r   r   r(   r(   r)   r   d  r   z6SkipFunctionVariable.call_function.<locals>.<listcomp>c                 S      i | ]	\}}||  qS r(   r  r   r(   r(   r)   r   e  r   z6SkipFunctionVariable.call_function.<locals>.<dictcomp>)mutable_localr   r   c                    sJ   t | tjr d jr d j}n d }| j|dS td|  d d S )Nr   )r   zfunctools.wraps(r   )r!   r   rd   r'   cloner   )rr   reconstructiblerW   r(   r)   r  s  s   
z1SkipFunctionVariable.call_function.<locals>.wrapsz'skip function z	 in file '_abc	_warningsz.Graph break due to unsupported Python builtin .zM. Please file an issue on GitHub so the PyTorch team can add support for it. z'Graph break due to unsupported builtin a  . This function is either a Python builtin (e.g. _warnings.warn) or a third-party C/C++ Python extension (perhaps created with pybind). If 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. 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.z', r   )rg   r   r{   r   r  keysr   r-   getr   r   r  r   r'   can_reconstructr4   r   r   LambdaVariablegetfilero   	TypeErrorrn   r   _dynamoutils	warn_oncer  )	rR   r%   rW   rY   r{   r  pathmsgknown_python_builtin_modulesr(   r  r)   r_   X  sF    
	z"SkipFunctionVariable.call_functionr+   rm   )r`   rn   ro   r   r   r   r   r   r   r|   staticmethodr   	lru_cacher  r_   r   r(   r(   r   r)   r  7  s*    
r  c                  C   s   t j rddlm}  | S i S )Nr   traceable_collective_remaps)r   distributedis_available)torch.distributed._functional_collectivesr-  r,  r(   r(   r)   _traceable_collective_remaps  s   
r1  c                 C   s>   t j s	J d|t  v sJ |j}| d}t||S )NzIllegal invocation.z)torch.distributed._functional_collectives)r   r.  r/  r1  valuesr`   import_sourcer   )r%   rr   
inner_namepath_sourcer(   r(   r)   _traceable_collectives_source  s
   

r6  c                       sZ   e Zd ZdZ fddZedd Zedd Zedd	 Z	
					dddZ	  Z
S )!CollectiveFunctionRewriteVariablea  
    Some of the torch.distributed.* collective APIs are possible to rewrite to 'traceable' collectives.

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

    In the case that a function is 'remappable' but only for some combinations of call-time arguments,
    we check the args at `call_function` time and fall back to graph-breaking if needed.  This is no worse
    than status-quo as we currently graph-break on all distributed.* collectives.
    c                   s,   t  j|fi | t|tsJ || _d S r+   )r   r   r!   rq   replacement_var)rR   rr   r8  rY   r   r(   r)   r     s   
z*CollectiveFunctionRewriteVariable.__init__c                 K   s6   t | |\}}t |ft|fd|i||d|S )Nr'   )r8  r'   )r7  rewriterq   )r%   old_fnr'   optionsnew_fn
new_sourcer(   r(   r)   r#     s   z(CollectiveFunctionRewriteVariable.createc                 C   s   t | o	| t v S r+   )rg   
isfunctionr1  )variabler(   r(   r)   can_rewrite  s   z-CollectiveFunctionRewriteVariable.can_rewritec                 C   s   t  | }|t| |fS r+   )r1  r6  )r%   rr   r<  r(   r(   r)   r9    s   
z)CollectiveFunctionRewriteVariable.rewriterW   rX   rY   rZ   r[   r   c           	      C   s   dd l m} ddlm} t| j}t|j|i |j	}d}d|v r1|d 
 r1td| j  | j|j|j|jfv rc|d}|d urH|jn|jd j}||vrYtd| tj|| |d< | j|||S )Nr   )REDUCE_OP_TO_STRr(   async_opzBCollectiveFunctionRewriteVariable can't support async_op=True for opzUnsupported all_reduce op: )torch.distributedr.  r0  rA  rg   rh   rr   r/   r   r   r   r   
all_reducereduce_scatter_tensor_reduce_scatter_baser  r{   ri   defaultr   r   r   r#   r8  r_   )	rR   r%   rW   rY   distrA  rh   reduce_op_var	reduce_opr(   r(   r)   r_     s2   


z/CollectiveFunctionRewriteVariable.call_functionrm   )r`   rn   ro   r   r   r*  r#   r@  r9  r_   r   r(   r(   r   r)   r7    s     

	

r7  c                       sj   e Zd Zdef fddZdd Zdd Z				
			dddZdedefddZ	dd Z
dd Z  ZS )FunctoolsPartialVariablerK   c                    sD   t  jdi | || _t|tsJ || _t|tsJ || _d S r   )r   r   rK   r!   r,   rW   r/   keywords)rR   rK   rW   rM  rY   r   r(   r)   r     s   
z!FunctoolsPartialVariable.__init__c                 C   s   | dd || j | jr|| j | js&|tt| jd d d S || j  t	| j
 }||t|t| j d |d d S )Nr   r   r   T)r  rK   rW   foreachrM  r  r	   r   r2  r.   r  create_call_function_kw)rR   r	  r  r(   r(   r)   r
    s   
z$FunctoolsPartialVariable.reconstructc                 C   s   |   S r+   r  rQ   r(   r(   r)   rb     r   z%FunctoolsPartialVariable.get_functionrW   rX   rY   rZ   r[   r   c                 C   s(   | j | }i | j|}| j|||S r+   )rW   rM  rK   r_   )rR   r%   rW   rY   merged_argsmerged_kwargsr(   r(   r)   r_     s   
z&FunctoolsPartialVariable.call_functionr=   c                 C   s   t jttt|S r+   )r   r   r#   ra   r   r   r   )rR   r%   r=   r(   r(   r)   rf     s   z%FunctoolsPartialVariable.call_hasattrc                 C   :   t j| j gdd | jD R i dd | j D S )Nc                 S   r  r(   r  )r   r   r(   r(   r)   r   '  r   z?FunctoolsPartialVariable.as_python_constant.<locals>.<listcomp>c                 S   r  r(   r  r   r(   r(   r)   r   (  r   z?FunctoolsPartialVariable.as_python_constant.<locals>.<dictcomp>)r   r   rK   r   rW   rM  r-   rQ   r(   r(   r)   r   $  s   z+FunctoolsPartialVariable.as_python_constantc                 C   rR  )zcSimilar to as_python_constant(), but add ID_MATCH guards to try to force things to become constantsc                 S   r  r(   guard_as_python_constant)r   r2   r(   r(   r)   r   /  r   zEFunctoolsPartialVariable.guard_as_python_constant.<locals>.<listcomp>c                 S   r  r(   rS  r   r(   r(   r)   r   0  r   zEFunctoolsPartialVariable.guard_as_python_constant.<locals>.<dictcomp>)r   r   rK   rT  rW   rM  r-   rQ   r(   r(   r)   rT  +  s   z1FunctoolsPartialVariable.guard_as_python_constantrm   )r`   rn   ro   r   r   r
  rb   r_   rp   rf   r   rT  r   r(   r(   r   r)   rL    s    
rL  c                       sL   e Zd Z fddZ						dd	d
Z						d fddZ  ZS )TritonKernelVariablec                    s   ddl m} ddlm} t jd
i | |d usJ || _||| _|d u s/| j|ks/J || _	t
||rzt|jj}d|v rM|d jt|ddksvd|v r\|d jt|ddksvd|v rh|d j|jksvt|jdksvt|jdkr|td	d S d S )Nr   )	Autotuner)kernel_side_tablewarmupnum_warmupsrepnum_repsprune_configs_byz7Only configs and keys are supported for triton.autotuner(   )triton.runtime.autotunerrV  *torch._higher_order_ops.triton_kernel_wraprW  r   r   kernel
add_kernel
kernel_idxgridr!   rg   rh   ri   rH  r   early_config_pruner   	reset_idxrestore_idxr   )rR   r_  ra  rb  rY   rV  rW  rG   r   r(   r)   r   5  s:   


zTritonKernelVariable.__init__rW   rX   rY   rZ   r[   r   c              
      sD  ddl m}m}m} ddlm  ddlm} ddlm	} d|v r$t
di }	d	D ]}
|
|v r?||
}t| s:J |j|	|
< q(|	rt| j|ret| jj}|D ]}|j|	 qQ||g d
| jj}n|ddi i|	}||gg d
| j}t|d | j}||||S | jd u rt
di tt| jj||}dd | D }t| j|rdd | jjD ni g}g }|D ]>}| j}t|ttfr fdd| D }|i ||t}|||gi }t||r||  qt dt!|  qt"t#|D ]L}t|| t$st
dt#|| dkr$|| d ddf||< q t#|| dkr>|| d || d df||< q t#|| dkrKt
dq t#|dksVJ t#t%|dkrd|d g}ddl&m'}m(}  fdd| D } fdd| D }|)|}||t}|j*+d|d| j,||| d t-d S )Nr   )autotunerV  Configr   r   )ConstDictVariable)BaseListVariablenum_ctaszqPassing num_ctas directly to the Triton kernel is not supported. Please use a Config in @triton.autotune instead.)	num_warps
num_stages)configskeyrY   z2Triton kernels should always be called with a gridc                 S   s   i | ]\}}t j||qS r(   )r   r   r#   r   r(   r(   r)   r     s    z6TritonKernelVariable.call_function.<locals>.<dictcomp>c                 S   s   g | ]}|j qS r(   )rY   )r   configr(   r(   r)   r     s    z6TritonKernelVariable.call_function.<locals>.<listcomp>c                    s"   i | ]\}}  |  |qS r(   )r#   r   r   r(   r)   r     r   zgrid for the triton kernel is zOnly tuple grids are supportedr      zGrid can have at most rank 3)rW  triton_kernel_wrapper_mutationc                    s$   i | ]\}}t | r|| qS r(   )r!   r   r   r   r(   r)   r     s    c                    s    i | ]\}}t | s||qS r(   )r!   r   r   r(   r)   r     s    r_   r(   )ra  constant_args_idxrb  rY   ).r]  rf  rV  rg  r   r   dictsrh  listsri  r   r9   r!   r{   r_  copydeepcopyrm  __dict__updaterr   rU  rb  r_   r/   r   	arg_namesr-   rd   rq   appendas_proxyr   r  r   r   r.   setr^  rW  rq  add_constant_argsr4   create_proxyra  r   )rR   r%   rW   rY   rf  rV  rg  rh  ri  special_kwargsr=   r&   new_configsro  
new_kernel
new_confignew_varcombined_args_rawcombined_argsrm  gridsconfig_argsrb  metairW  rq  constant_argsnon_constant_argsrr  r(   r   r)   r_   d  s   





"




z"TritonKernelVariable.call_functionc                    s   |dkr| j d ust|dkrtdt| j| j|d dS |dkrCd|vr*td|d}|d	d  t| j| j|d|||S t 	||||S )
N__getitem__r   z7Triton kernels should be called with only a single gridr   )r_  ra  rb  runrb  z/Triton kernel requires to be called with a gridrX  )
rb  r   r   rU  r_  ra  r9   r_   r   r   )rR   r%   r=   rW   rY   rb  r   r(   r)   r     s*   


z TritonKernelVariable.call_methodrm   )r`   rn   ro   r   r_   r   r   r(   r(   r   r)   rU  4  s"    /
 rU  r+   )<r  ru  r   rg   rD   rB   typingr   r   r   r   r   r   r   r   bytecode_transformationr	   r
   excr   r   guardsr   r   r'   r   r   r   r   r%  r   r   r   r   r   baser   r   r   r   r   torch._guardsr   r*   r3   r>   rL   rM   rq   r   r   r   r   rd   r  r1  r6  r7  rL  rU  r(   r(   r(   r)   <module>   sJ   
 <6 (e	L8