o
    I&ir                     @   sf  d dl Z d dlZd dlZd dlZd dl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mZmZ ddlmZ d	d
lmZmZ d+ddZdd Zdd Zdd ZG dd deZG dd deZG dd deZ G dd de Z!G dd deZ"dd Z#G dd  d eZ$d!d" Z%d#d$ Z&G d%d& d&eZ'G d'd( d(eZ(G d)d* d*eZ)dS ),    N)DictList   )	variables)create_call_functioncreate_rot_n)unimplementedUnsupported)
AttrSourceConstantSourceDefaultsSourceGetItemSource	make_cell   )typestrVariableTrackerc                 C   sF   t |tr|S |sddlm} | | |S ddlm} || |d|S )Nr   SourcelessBuilderVariableBuildersource)
isinstancer   Ztorch._dynamo.variables.builderr   r   )txvalr   r   r    r   LC:\wamp64\www\opt\env\Lib\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
   r'   c                 C   sH   t  }| jj}|jD ]}| ||< ||v r!||| || q
|S r   )r#   outputside_effectsco_cellvarsZtrack_cell_new
store_cellpop)parentr$   codeclosure_cellsr)   namer   r   r   init_cellvars&   s   
r1   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)	typesr2   __kwdefaults__r   r"   	itertoolsr4   r#   __annotations__)
r.   	f_globalsr0   defaultsclosure
kwdefaultsannotationsr2   funcr4   r   r   r   _create_nested_fn3   s   
r?   c                   @   sB   e Zd Zdd Zdd Z							
dddZdd Zdd ZdS )BaseUserFunctionVariablec                 C   
   |   jS r   )get_codeco_filenameselfr   r   r   get_filenameH      
z%BaseUserFunctionVariable.get_filenamec                 C   rA   r   )rB   co_namerD   r   r   r   get_nameK   rG   z!BaseUserFunctionVariable.get_nameargsList[VariableTracker]kwargsDict[str, VariableTracker]returnr   c                 C   s   | | t|  t| |S r   )Zinline_user_function_returnr    	self_argsrE   r   rJ   rL   r   r   r   call_functionN   s   z&BaseUserFunctionVariable.call_functionc                 C   s   t t|  jS r   )r    inspect	signatureget_function
parametersrD   r   r   r   inspect_parameter_namesU   s   z0BaseUserFunctionVariable.inspect_parameter_namesc                 C   s   i S r   r   )rE   r   r   r   r   closure_varsX      z%BaseUserFunctionVariable.closure_varsNrJ   rK   rL   rM   rN   r   )__name__
__module____qualname__rF   rI   rQ   rV   rW   r   r   r   r   r@   G   s    
r@   c                       s|   e Zd Zd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 fddZ  ZS )UserFunctionVariablez-Some unsupported user-defined global functionFc                    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 )
NZ_dynamo_marked_constantFTzexpected FunctionType found  Z_torchdynamo_inlineZ__script_if_tracing_wrapperZ__original_fnr   )super__init__getattris_constantr   r5   r2   torchZjitZScriptFunctionr   rR   getattr_staticfn)rE   re   rb   rL   	__class__r   r   r`   _   s   
zUserFunctionVariable.__init__c                 C      g S r   r   rD   r   r   r   rO   q   rX   zUserFunctionVariable.self_argsc                 C      | j S r   )re   rD   r   r   r   rT   t      z!UserFunctionVariable.get_functionc                 C      | j jS r   )re   __code__rD   r   r   r   rB   w      zUserFunctionVariable.get_codec                 C      t jS r   )r5   r2   rD   r   r   r   python_typez   rj   z UserFunctionVariable.python_typec                 C   s   t | jdd d uS )N__self__)ra   re   rD   r   r   r   has_self}      zUserFunctionVariable.has_selfc                 C   rk   r   )re   __globals__rD   r   r   r   get_globals   rm   z UserFunctionVariable.get_globalsc              
      sF  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}||||j#}|) |j*vr||
|< q|+||}|,|| ||
|< qddl%m-} | ||j#|
|< q|
|fS )N)r   c                    s*   g | ]\}} j d u rd nt j |qS r   r   r   ).0idx_rD   r   r   
<listcomp>   s    z2UserFunctionVariable.bind_args.<locals>.<listcomp>c                    s   g | ]
\}} ||d qS )r   r   r   )rv   argr   )wrapr   r   ry      s    
c                    s,   i | ]}| j d u rd nt j |ddqS )NT)Zis_kwru   )rv   r%   rD   r   r   
<dictcomp>   s    
z2UserFunctionVariable.bind_args.<locals>.<dictcomp>c                    s"   i | ]\}}|| | d qS rz   r   rv   r%   r&   )kwdefaults_sourcesr|   r   r   r}      s    r   rg   r   r   r   __closure__cell_contentsr   ).rb   r(   root_tx	functoolspartialr   re   __defaults__	enumerater5   r2   rl   rs   rZ   r"   zipr   r6   r!   rR   rS   bindapply_defaultsr#   	argumentsr'   r1   lenco_freevarsr7   countr   r
   r   ZUserDefinedClassVariabler   Zmatch_nested_cellZbuilderr   r)   r   r0   Zmutated_closure_cell_contentsZtrack_cell_existingr+   r   )rE   r-   rJ   rL   r   re   r:   Zdefaults_sourcesZ	fake_funcboundr$   r/   r;   rw   r0   cellr   varr   r)   outZclosure_cellZclosure_cell_contentsZcontents_varr   r   )r   rE   r|   r   	bind_args   s   








zUserFunctionVariable.bind_argsc                 C   s   d S r   r   )rE   r-   childr   r   r   export_freevars   rX   z$UserFunctionVariable.export_freevarsrJ   rK   rL   rM   rN   r   c                    s,   | j rt|| j|  ||S t |||S r   )rb   invoke_and_store_as_constantre   rI   r_   rQ   rP   rf   r   r   rQ      s
   z"UserFunctionVariable.call_function)FrY   )rZ   r[   r\   __doc__r`   rO   rT   rB   ro   rq   rt   r   r   rQ   __classcell__r   r   rf   r   r]   \   s$    jr]   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 )Nre   r   )r_   r`   obj)rE   re   r   rL   rf   r   r   r`      s   
zUserMethodVariable.__init__c                 C   s   | j j d| j d| j dS )N(z, ))rg   rZ   re   r   rD   r   r   r   __str__  s   zUserMethodVariable.__str__c                 C   s   | j gS r   )r   rD   r   r   r   rO     rm   zUserMethodVariable.self_argsc                 C   rn   r   )r5   
MethodTyperD   r   r   r   ro     rj   zUserMethodVariable.python_typerJ   rK   rL   rM   rN   r   c                    sj   |j  r-t| jtjr-t| jdd}|d ur|ds| j	r-| jj
|| jj||| j	dS t |||S )Nr[    z	torch.nn.)constant)r(   Zis_root_tracerr   r   r   ZNNModuleVariablera   re   
startswithrb   call_methodrZ   r_   rQ   )rE   r   rJ   rL   Zmodule_attrrf   r   r   rQ     s   z UserMethodVariable.call_functionc                    s   t   dd  S )Nr   )r_   rV   rD   rf   r   r   rV   (  rr   z*UserMethodVariable.inspect_parameter_namesrY   )rZ   r[   r\   r   r`   r   rO   ro   rQ   rV   r   r   r   rf   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 Nre   r   )r,   r_   r`   re   r   wrappedcontextrE   r   r   rL   rf   r   r   r`   -  s
   
z"WrappedUserMethodVariable.__init__rJ   rK   rL   rM   rN   r   c                    ,   | j | t |||}| j | |S r   r   Zenterr_   rQ   exitrE   r   rJ   rL   r$   rf   r   r   rQ   4     z'WrappedUserMethodVariable.call_functionrY   rZ   r[   r\   r`   rQ   r   r   r   rf   r   r   ,      r   c                       r   )WrappedUserFunctionVariablec                    s>   | dd  | dd  t j|jfi | || _|| _d S r   )r,   r_   r`   re   r   r   r   rf   r   r   r`   >  s
   
z$WrappedUserFunctionVariable.__init__rJ   rK   rL   rM   rN   r   c                    r   r   r   r   rf   r   r   rQ   E  r   z)WrappedUserFunctionVariable.call_functionrY   r   r   r   rf   r   r   =  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   ZTensorVariableZget_real_valueas_python_constant)xr   r   r   convertO  s   z-invoke_and_store_as_constant.<locals>.convertc                       g | ]} |qS r   r   )rv   r   r   r   r   ry   T      z0invoke_and_store_as_constant.<locals>.<listcomp>c                       i | ]	\}}| |qS r   r   r~   r   r   r   r}   U      z0invoke_and_store_as_constant.<locals>.<dictcomp>r   )r!   r(   Zregister_attr_or_moduler   )r   re   r0   rJ   rL   resr   r   r   r   N  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 )NestedUserFunctionVariableclosure_scoper9   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   strr5   CodeTyper#   fn_namer.   r9   r:   r<   r=   r;   r   wrapped_reconstructible)rE   r   r.   r9   r:   r<   r=   r;   r   r   rL   rf   r   r   r`   e  s    z#NestedUserFunctionVariable.__init__c                 C   rh   r   r   rD   r   r   r   rO     rX   z$NestedUserFunctionVariable.self_argsc                 C   s
   | j  S r   )r.   r   rD   r   r   r   rB     rG   z#NestedUserFunctionVariable.get_codec                 C   s   | j rt t| j | j| j }| jr| j |_	| j
r&| j
 |_| jrI| j }t|tr?ddlm} t||}t|tsFJ ||_|S )Nr   r3   )r;   NotImplementedErrorr5   r2   r.   r   r9   r   r:   r   r<   r6   r=   r   r"   r7   r4   r#   r8   )rE   r>   r=   r4   r   r   r   rT     s&   

z'NestedUserFunctionVariable.get_functionc                 C   s
   | j d uS r   )r;   rD   r   r   r   has_closure  rG   z&NestedUserFunctionVariable.has_closurec                 C   s   dS )NFr   rD   r   r   r   rq     rX   z#NestedUserFunctionVariable.has_selfc                 C   ri   r   )r9   rD   r   r   r   rt     rj   z&NestedUserFunctionVariable.get_globalsc                 C   sb  ddl m} |  }t|| j| j | jrt	| jj
nd t	dd tt|  jD }| jr6| jj
|_t|j|i |}|  t|j
 }t|jj| t|||}	t|jD ]N\}
}| jj
|
 }t||||ksrJ ||vsxJ 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   )rv   rx   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   rB   r5   r2   r9   r   r   r:   r"   r!   ranger   r   r<   r6   rR   rS   r   r   r#   r   r'   r(   r   r1   r   r;   ra   r   symbolic_localsr-   RuntimeError)rE   r-   rJ   rL   r   r.   r>   r   r$   r/   rw   r0   r   candr   r   r   r     s@   


z$NestedUserFunctionVariable.bind_argsc                 C   s2   |   }|jD ]}||jv r|j| |j|< qd S r   )rB   r   r   )rE   r-   r   r.   r   r   r   r   r     s   

z*NestedUserFunctionVariable.export_freevarsc                 C   s  | td || j ||| jg || j | jr#|| j n	||d g | j	r5|| j	 n	||d g | j
rG|| j
 n	||d g | jrz)t| jtjrgdd | jj D }ntdd | jjD }|||g W n ty   || j Y n
w ||d g |tddd | jr| d	d
 || j |tdd |td |tdd g S )Nr?   c                 S   s   i | ]	\}}||  qS r   r   r~   r   r   r   r}     s    z:NestedUserFunctionVariable.reconstruct.<locals>.<dictcomp>c                 S   s   g | ]}|  qS r   r   )rv   r&   r   r   r   ry     r   z:NestedUserFunctionVariable.reconstruct.<locals>.<listcomp>   T)Z	push_nullr   wrapsr   r   )Zload_import_fromrZ   r.   Zextend_outputZ_create_load_constr9   r   r:   Zcreate_load_constr;   r<   r=   r   r   ConstDictVariabler!   r"   r   r   r   r   )rE   Zcodegenr=   r   r   r   reconstruct  sF   



z&NestedUserFunctionVariable.reconstructr   )rZ   r[   r\   r@   Z_nonvar_fieldsr`   rO   rB   rT   r   rq   rt   r   r   r   r   r   r   rf   r   r   ^  s"     +r   c                  C   s   t j rddlm}  | S i S )Nr   traceable_collective_remaps)rc   distributedis_available)torch.distributed._functional_collectivesr   r   r   r   r   _traceable_collective_remaps  s   
r   c                 C   sP   t j s	J dddlm}m} ||h}||v sJ |j}| d}t||S )NzIllegal invocation.r   )all_gather_tensor_inplacereduce_scatter_tensor_inplacez)torch.distributed._functional_collectives)	rc   r   r   r   r   r   rZ   Zimport_sourcer
   )r   re   r   r   Zvalid_valuesZ
inner_nameZpath_sourcer   r   r   _traceable_collectives_source  s   

r   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   r]   replacement_var)rE   re   r   rL   rf   r   r   r`   3  s   
z*CollectiveFunctionRewriteVariable.__init__c                 K   s6   t | |\}}t |ft|fd|i||d|S )Nr   )r   r   )r   rewriter]   )r   Zold_fnr   optionsnew_fnZ
new_sourcer   r   r   create8  s   z(CollectiveFunctionRewriteVariable.createc                 C   s   t | o	| t v S r   )rR   
isfunctionr   )variabler   r   r   can_rewriteB  s   z-CollectiveFunctionRewriteVariable.can_rewritec                 C   s   t  | }|t| |fS r   )r   r   )r   re   r   r   r   r   r   H  s   
z)CollectiveFunctionRewriteVariable.rewriterJ   rK   rL   rM   rN   r   c                 C   s,   | ddrtd| j  | j|||S )NZasync_opFzBCollectiveFunctionRewriteVariable can't support async_op=True for )getr   re   r   rQ   rP   r   r   r   rQ   M  s
   
z/CollectiveFunctionRewriteVariable.call_functionrY   )rZ   r[   r\   r   r`   staticmethodr   r   r   rQ   r   r   r   rf   r   r   (  s     

	

r   c                       s<   e Zd Zd fdd	Z							dd
dZdd Z  ZS )FunctoolsPartialVariableNc                    sJ   t  jdi | || _t|tsJ || _t|tsJ || _|| _d S r   )	r_   r`   r>   r   r    rJ   r#   keywordsoriginal)rE   r>   rJ   r   r   rL   rf   r   r   r`   [  s   
z!FunctoolsPartialVariable.__init__rJ   rK   rL   rM   rN   r   c                 C   s(   | j | }i | j|}| j|||S r   )rJ   r   r>   rQ   )rE   r   rJ   rL   Zmerged_argsZmerged_kwargsr   r   r   rQ   d  s   
z&FunctoolsPartialVariable.call_functionc                    sT   | j r| j S dd  tj| jjg fdd| jD R i  fdd| j D S )Nc                 S   s   t | tjr	| jS |  S r   )r   r   ZUserDefinedObjectVariablevaluer   )r&   r   r   r   get_valp  s   z<FunctoolsPartialVariable.as_python_constant.<locals>.get_valc                    r   r   r   )rv   r{   r   r   r   ry   x  r   z?FunctoolsPartialVariable.as_python_constant.<locals>.<listcomp>c                    r   r   r   r~   r   r   r   r}   y  r   z?FunctoolsPartialVariable.as_python_constant.<locals>.<dictcomp>)r   r   r   r>   re   rJ   r   r!   rD   r   r   r   r   k  s   z+FunctoolsPartialVariable.as_python_constantr   rY   )rZ   r[   r\   r`   rQ   r   r   r   r   rf   r   r   Z  s    	
r   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
||ret|j}d|v rI|d j|jksad|v rU|d j|jksad|v rg|d j|jkritdd S d S d S )	Nr   	Autotuner)kernel_side_tablewarmuprepZprune_configs_byz7Only configs and keys are supported for triton.autotuner   )triton.runtime.autotunerr   *torch._higher_order_ops.triton_kernel_wrapr   r_   r`   kernelZ
add_kernel
kernel_idxgridr   rR   rS   rU   defaultr   r   Zearly_config_pruner	   )rE   r   r   r   rL   r   r   r:   rf   r   r   r`   ~  s,   
zTritonKernelVariable.__init__rJ   rK   rL   rM   rN   r   c              	      s  ddl m} ddlm  ddlm} ddlm} | jd u r!t	di t
t| jj||}t| j|r=dd	 | jjD ni g}g }	|D ]>}
| j}t|ttfrl fd
d|
 D }
|i ||
t
}|||gi }t||ry|	|  qDtdt|  qDtt|	D ]E}t|	| tst	dt|	| dkr|	| d ddf|	|< qt|	| dkr|	| d |	| d df|	|< qt|	| dkrt	dqt|	dksJ tt|	dkr|	d g}	ddlm} ||t
}|jd|d| j |	| d t!d S )Nr   r   r   ConstantVariable)r   )BaseListVariablez2Triton kernels should always be called with a gridc                 S   s   g | ]}|j qS r   )rL   )rv   configr   r   r   ry     s    z6TritonKernelVariable.call_function.<locals>.<listcomp>c                    s   i | ]
\}}|  |qS r   )r   r~   r   r   r   r}     s    z6TritonKernelVariable.call_function.<locals>.<dictcomp>zgrid for the triton kernel is zOnly tuple grids are supportedr      zGrid can have at most rank 3)triton_kernel_wrapper_mutationrQ   r   )r   r   rL   )"r   r   r   r   Zdictsr   listsr   r   r	   r#   r   r   	arg_namesr   configsr   r]   r!   rQ   appendZas_proxyr   typer   r   r"   setr   r   r(   Zcreate_proxyr   r   )rE   r   rJ   rL   r   r   r   Znormalized_argsr   ZgridsZconfig_argsr   metair   r   r   r   rQ     sd   



 

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r:d|vr*td|d}| j|d	|||S t	 
||||S )
N__getitem__r   z7Triton kernels should be called with only a single gridr   )r   r   r   runr   z/Triton kernel requires to be called with a grid)r   )r   r   r	   r   r   r   r,   clonerQ   r_   r   )rE   r   r0   rJ   rL   r   rf   r   r   r     s    
z TritonKernelVariable.call_methodrY   )rZ   r[   r\   r`   rQ   r   r   r   r   rf   r   r   }  s     !
Pr   r   )*r   rR   r7   r5   typingr   r   rc   r   r   Zbytecode_transformationr   r   excr   r	   r   r
   r   r   r   utilsr   baser   r   r   r'   r1   r?   r@   r]   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s<    
  1 22#