U
    KcW                     @   s  d dl mZ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 d dlmZ d dlmZmZmZmZmZmZ d dlmZmZmZ d dl
mZmZ dd	d
ddddddddddddddddddddgZejZeejj j!edej"d dqeee e#ed"d#dZ!eejj j$dree%e#e#ed&d'd	Z$edej"d dseee e#ed"d(d
Z&eejj j'edej"d dtee#ed)d*dZ'dueeee ee e%ed,d-d.Z(eejj j)edej"d dvee%e#ed0d1d2Z)eejj j*edej"d dwee#ed)d3dZ*eejj j+edej"d dxee#ed)d4dZ+eejj j,e edej"d dyeee ee#ed6d7dZ,eejj j-e dzee%d8d9dZ-eejj j.e d{ee%d8d:dZ.e/e0d;d<d=Z1ee0ed>d?d@Z2e0dAdBdCZ3ee# ee# e0dDdEdFZ4edGej5d d|eeee# ee# e0edIdJdZ6eejj j7d}eeee%e0edLdMdZ7d~eeee# ee# e0edIdNdZ8eejj j9deee%e0edPdQdZ9eejj j:e edGej"d deeee0e/f e%edRdSdZ:eede	jj;d eedTdUdZ<eejj j=edej"d deeee#e/e%f e#edVdWdZ=eejj j>eedXej"d deeee#ed[d\dZ>eejj j?e eede	jj"d dee0ed^d_d`Z?edGej;d deee#e#ee# e%ee# e0edb	dcdZ@eejj jAedde	jj"d eeededfdZAeejj jBdee#ed)dgdZBeejj jCede	jj"d e dee/edhdidZCeejj jDe deeeeedldmdZDeejj jEede	jj"d e dee%edodpdZEdS )    )OptionalUnionN)register_decomposition)	Reduction)checkELEMENTWISE_TYPE_PROMOTION_KIND
NumberType	ShapeType
TensorLikeTensorLikeType)"elementwise_type_promotion_wrapper elementwise_unary_scalar_wrapperout_wrapper)"_make_elementwise_binary_reference!_make_elementwise_unary_referenceceludropoutelu
hardshrinkhardtanhhinge_embedding_loss
huber_lossl1_lossmargin_ranking_lossmishmse_losspoisson_nll_lossprelurelurelu6selusoftplus
softshrink
tanhshrink	thresholdglupairwise_distancepdista)Ztype_promoting_argsZtype_promotion_kindF)r)   alphainplacereturnc                 C   sx   |rt |dk	r\t| j}tt||sDdt||}t||t	t
| | }n
t	| }t| dk| |S )z>
    Reference implementation of torch.nn.functional.celu
    N=alpha argument of type {0} cannot be safely cast to type {1}!r   )NotImplementedErrorutilsdtype_to_typedtypeis_weakly_lesser_typetypeformat
ValueErrortorchexpm1true_dividewherer)   r*   r+   python_typemsgrhs r>   F/tmp/pip-unpacked-wheel-gikjz4vx/torch/_refs/nn/functional/__init__.pyr   :   s     
      ?T)r)   ptrainingr+   r,   c              	   C   s   |rt |s| S |dkst|dks(t|dkr:t| S |dkrF| S d| }d| }ttj| jddtj| j	d|}t
t
| ||S )N   r                 ?)lowhighr1   device)r.   AssertionErrorrefsZ
zeros_likeltuniformshaper6   Zfloat32rH   mul)r)   rA   rB   r+   Zp1mscalemaskr>   r>   r?   r   [   s"    
c                 C   sp   |rt |dk	rTt| j}tt||sDdt||}t||t	|  }n
t	| }t
| dk| |S )z=
    Reference implementation of torch.nn.functional.elu
    Nr-   r   )r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r9   r:   r>   r>   r?   r   z   s    
 
)r)   r+   r,   c                 C   s   |rt tt| dd| S )z>
    Reference implementation of torch.nn.functional.relu
    r   )r.   r6   r9   ler)   r+   r>   r>   r?   r      s    
h㈵>)inputnormalized_shapeweightbiasepsr,   c                 C   s   t | ||||d S )zM
    Reference implementation of :func:`torch.nn.functional.layer_norm`.
    r   )r6   Znative_layer_norm)rT   rU   rV   rW   rX   r>   r>   r?   
layer_norm   s    
rY   {Gz?)r)   negative_sloper+   r,   c                 C   s`   |rt t| j}tt||sBdt| d| d}t|tt	| d| t
| |S )zD
    Reference implementation of torch.nn.functional.leaky_relu
    z negative_slope argument of type z cannot be safely cast to type !r   )r.   r/   r0   r1   r2   r3   r5   r6   r9   gtrN   )r)   r[   r+   r;   r<   r>   r>   r?   
leaky_relu   s    r^   c                 C   s    |rt | ttjj|  S )z>
    Reference implementation of torch.nn.functional.mish
    )r.   r6   tanhnn
functionalr!   rR   r>   r>   r?   r      s    
c                 C   s4   |rt d}d}|t|  }|t| dk| | S )z>
    Reference implementation of torch.nn.functional.selu
    g,x?g2֫?r   )r.   r6   r7   r9   )r)   r+   r*   rO   r=   r>   r>   r?   r       s    	   )r)   betar$   r+   r,   c                 C   s   |rt |dk	rft| j}tt||sDdt||}t|| | }t	t
t||}n| }t
t|}t||k| |S )zB
    Reference implementation of torch.nn.functional.softplus
    Nz<beta argument of type {0} cannot be safely cast to type {1}!)r.   r/   r0   r1   r2   r3   r4   r5   r6   r8   log1pexpr9   )r)   rc   r$   r+   r;   r<   Zscaled_inputr=   r>   r>   r?   r!      s     r)   lambdc                 C   s    t t | | k| |kd| S )Nr   )rJ   r9   logical_andrf   r>   r>   r?   r     s    c                    sl   t  dk fdd |  k}|   k }tt||}t||   | }t||   |}t|d|S )Nr   c                      s
   d  S )Nz6lambda must be greater or equal to 0, but found to be r>   r>   rg   r>   r?   <lambda>'      zsoftshrink.<locals>.<lambda>)r   r6   Zlogical_notrJ   
logical_orr9   )r)   rg   Zge_maskZle_maskZ	zero_maskresultr>   ri   r?   r"     s    

)	reductionr,   c                 C   sB   | t jjkrdS | t jjkr dS | t jjkr0dS t|  dd S )Nnonemeansum# is not a valid value for reduction)r   NONEvalueZMEANZSUMr5   rn   r>   r>   r?   _reduction_int_to_str2  s    rv   )lossrn   r,   c                 C   s,   |dkrt | S |dkr$t | S | S d S )Nrq   rp   )rJ   rq   rp   )rw   rn   r>   r>   r?   _apply_loss_reduction=  s
    

rx   ru   c                 C   s   | dkrt |  dd S )N)rp   rq   ro   rr   )r5   ru   r>   r>   r?   _check_reduction_valueF  s    ry   )size_averagereducer,   c                 C   s8   | d krd} |d krd}| r&|r&d}n|r0d}nd}|S )NTrp   rq   ro   r>   )rz   r{   retr>   r>   r?   _get_string_reduction_argM  s    r}   )rT   targetrp   )rT   r~   rz   r{   rn   r,   c                 C   s<   |dk	s|dk	rt ||d}t| t| | }t||S )zA
    Reference implementation of torch.nn.functional.l1_loss
    Nrz   r{   )r}   ry   r6   absrx   rT   r~   rz   r{   rn   rw   r>   r>   r?   r   ^  s
    rD   )input1input2r~   marginrn   r,   c           
      C   s|   | j |j ks| j |j kr0td| j|j|jt| t|}t| |}t||}t	||}t
|d}	t|	|S )Nzumargin_ranking_loss : All input tensors should have same dimension but got sizes: input1: {}, input2: {}, target: {} r   )ndimRuntimeErrorr4   rM   ry   rJ   negsubrN   addmaximumrx   )
r   r   r~   r   rn   Z
neg_targetZ
input_diffZmul_target_inputZ
add_marginrw   r>   r>   r?   r   u  s    
  
c                 C   s>   |d k	s|d k	rt ||d}t| t| | d}t||S )Nr      )r}   ry   r6   powrx   r   r>   r>   r?   r     s
    rE   )rT   r~   r   rn   r,   c                 C   s^   t | tt|| d}tt|d|d}tt|d| d}t||}t||S )Nr   rC   )ry   rJ   r   r   r9   ner   rx   )rT   r~   r   rn   Zmargin_clampZoutput_marginZoutput_selfrw   r>   r>   r?   r     s    
)rT   r~   rn   deltar,   c                 C   sj   t |tkrt|}t| t|dkdd  | |  }t||k d| | ||d|   }t||S )zD
    Reference implementation of torch.nn.functional.huber_loss
    r   c                   S   s   dS )Nz:huber_loss does not support non-positive values for delta.r>   r>   r>   r>   r?   rj     rk   zhuber_loss.<locals>.<lambda>r@   )	r3   intrv   ry   r   r   r6   r9   rx   )rT   r~   rn   r   zrw   r>   r>   r?   r     s    &)r)   r,   c                 C   s$   t | tstdt| t| S )zD
    Reference implementation of torch.nn.functional.tanhshrink
    ;Expected a tensor input for an elementwise unary operation!)
isinstancer
   r   rJ   r   r_   r(   r>   r>   r?   r#     s
    	
)r)   r$   rt   r+   r,   c                 C   s   |rt t| |k|| S )zC
    Reference implementation of torch.nn.functional.threshold
    )r.   r6   r9   )r)   r$   rt   r+   r>   r>   r?   r$     s    r)   r   rC   )r)   min_valmax_valr+   r,   c                 C   sj   |rt t| jrtdt| jr\t|}t|}| jtjks\|dkrT|dks\tdt	| ||S )zB
    Reference implementation of torch.nn.functional.hardtanh
    z&Bool inputs not supported for hardtanhr   z;Cannot do hardtanh on an unsigned type with negative limits)
r.   r/   Zis_boolean_dtyper1   r   Zis_integer_dtyper   r6   Zuint8clamp)r)   r   r   r+   r>   r>   r?   r     s    ro   )r)   approximater,   c           
      C   s   t | tstdd}d}d}|dkrh|| d }d}| |  |  }|| ||   }d|  dt|  S |d	kr|}	| d dt| |	   S td
dS )z>
    Reference implementation of torch.nn.functional.gelu
    r   g;f?g;f?gmBP?r_   r@   gHm?rC   ro   z1approximate argument must be either none or tanh.N)r   r
   r   r6   r_   erf)
r)   r   ZM_SQRT2Z	M_SQRT1_2Z
M_2_SQRTPIZkBetaZkKappaZa_cubeinnerZkAlphar>   r>   r?   gelu  s"    
r   :0yE>)	rT   r~   	log_inputfullrz   rX   r{   rn   r,   c           
      C   s   |dk	s|dk	rt ||d}t| |r<t| ||   }n| |t| |   }|r|t| | dtdtj |   }	||	|dkd }t||S )zJ
    Reference implementation of torch.nn.functional.poisson_nll_loss
    Nr   r@   r   rC   r   )r}   ry   r6   re   logpiZmasked_fillrx   )
rT   r~   r   r   rz   rX   r{   rn   rw   Zstirling_termr>   r>   r?   r   7  s    (r)   rV   )r)   rV   r,   c                    s   t t t fdd t ttfdd  dkrt  jdkdd   jdkrd jd ndt  kfdd t jdkpjdkfd	d t jjdkrt nd
t	
 dk   S )z?
    Reference implementation of torch.nn.functional.prelu
    c                      s   dt   S )Nz+prelu: Expected `a` to be tensor, but got: r3   r>   r(   r>   r?   rj   f  rk   zprelu.<locals>.<lambda>c                      s   dt   S )Nz0prelu: Expected `weight` to be tensor, but got: r   r>   rV   r>   r?   rj   j  rk   rC   r   c                   S   s   dS )Nz Not allow zero-dim input tensor.r>   r>   r>   r>   r?   rj   n  rk   r   c                      s   d   d  dS )NzPMismatch of parameter numbers and input channel size. Found parameter numbers = z and channel size = .)numelr>   )channel_sizerV   r>   r?   rj   r  rk   c                      s   d j  S )NzFprelu: Expected `weight` to be a scalar or 1D tensor, but got: ndim = r   r>   r   r>   r?   rj   x  rk   )rC   )r   r   r
   r   r   rM   primsZbroadcast_in_dimtuplerJ   r9   r   r>   )r)   r   rV   r?   r   [  s2    	



  c                 C   s   |rt tjj| ddS )z?
    Reference implementation of torch.nn.functional.relu6
    r      )r.   rJ   r`   ra   r   rR   r>   r>   r?   r     s    )r)   dimr,   c                    sP   t  jt j d dk fdd t d\}}|t| S )Nr   r   c                      s   d d j   S )Nz.Halving dimension must be even, but dimension z	 is size )rM   r>   r)   r   r>   r?   rj     rk   zglu.<locals>.<lambda>)r/   Zcanonicalize_dimsr   r   rM   r6   Ztensor_splitZsigmoid)r)   r   bcr>   r   r?   r%     s           @ư>)x1x2rA   rX   r,   c                 C   s   t jj| | | |d|dS )Nr   )ordr   keepdim)r6   linalgvector_norm)r   r   rA   rX   r   r>   r>   r?   r&     s    	r   )r)   rA   r,   c                    s   t  jdk fdd t |dkdd  |dkrrt  j}t|}ttj||d d|  dd}ntj	j
 d  |dd	}tj|jd |jd d jd
}| d|d |jd  |d  S )Nr   c                      s   d j  dS )Nz%pdist only supports 2D tensors, got: Dr   r>   r(   r>   r?   rj     rk   zpdist.<locals>.<lambda>r   c                   S   s   dS )Nz)pdist only supports non-negative p valuesr>   r>   r>   r>   r?   rj     rk   r   )minrC   )r   r   )offsetrH   )r   r   r6   mmTZdiagsqrtr   Z	unsqueezer   r   Ztriu_indicesrM   rH   flattenZindex_select)r)   rA   ZaTaZaTa_diagtir>   r(   r?   r'     s    
( )NF)r@   TF)NF)F)NNrS   )rZ   F)F)F)Nrb   F)r@   )r@   )NNrp   )rD   rp   )NNrp   )rE   rp   )rp   rE   )F)r   rC   F)ro   )TFNr   Nrp   )F)r   )r   r   F)r   )Ftypingr   r   r6   Ztorch._primsZ_primsr   Ztorch._prims_commonZ_prims_commonr/   Ztorch._refsZ_refsrJ   Ztorch._decompr   Ztorch._decomp.decompositionsr   r   r   r   r	   r
   r   Ztorch._prims_common.wrappersr   r   r   r   r   __all__ZTensoropsZatenr   DEFAULTboolr   floatr   r   rY   r^   r   r    r!   r   r"   r   strrv   rx   ry   r}   ZCOMPLEX_TO_FLOATr   r   r   r   r   ZINT_TO_FLOATr#   r$   r   r   r   r   r   r%   r&   r'   r>   r>   r>   r?   <module>   sZ                             
   	             
           "   
