o
    iJ                     @   s   d Z ddlZddlmZ 											d d
edededededededededededejj	fddZ
G dd dejj	ZG dd dejj	ZG dd dejj	ZG dd dejj	ZdS )!z$Activation functions for Transducer.    N)parse皙ɿ                    ?{Gz?   activation_typeftswish_thresholdftswish_mean_shifthardtanh_min_valhardtanh_max_valleakyrelu_neg_slopesmish_alpha
smish_betasoftplus_betasoftplus_threshold
swish_betareturnc                 C   s   t tj}t||dftjj||dftjjd|ift||	|t dkdftjji ftjj	i ft
||dft|
|t dkdftjji ftjji fd	
}||  \}}|di |S )a  Return activation function.

    Args:
        activation_type: Activation function type.
        ftswish_threshold: Threshold value for FTSwish activation formulation.
        ftswish_mean_shift: Mean shifting value for FTSwish activation formulation.
        hardtanh_min_val: Minimum value of the linear region range for HardTanh.
        hardtanh_max_val: Maximum value of the linear region range for HardTanh.
        leakyrelu_neg_slope: Negative slope value for LeakyReLU activation formulation.
        smish_alpha: Alpha value for Smish activation fomulation.
        smish_beta: Beta value for Smish activation formulation.
        softplus_beta: Beta value for softplus activation formulation in Mish.
        softplus_threshold: Values above this revert to a linear function in Mish.
        swish_beta: Beta value for Swish variant formulation.

    Returns:
        : Activation function.

    )	threshold
mean_shift)min_valmax_valnegative_slopez1.9)r   r   use_builtin)alphabetaz1.8)r   r   )
ftswishhardtanh
leaky_relumishreluselusmishswishtanhidentityN )Vtorch__version__FTSwishnnHardtanh	LeakyReLUMishReLUSELUSmishSwishTanhIdentity)r	   r
   r   r   r   r   r   r   r   r   r   torch_versionactivationsact_funcact_argsr'   r'   U/home/ubuntu/.local/lib/python3.10/site-packages/espnet2/asr_transducer/activation.pyget_activation   s2   
 




r;   c                       sF   e Zd ZdZddededdf fdd	Zd
ejdejfddZ  Z	S )r+   a  Flatten-T Swish activation definition.

    FTSwish(x) = x * sigmoid(x) + threshold
                  where FTSwish(x) < 0 = threshold

    Reference: https://arxiv.org/abs/1812.06247

    Args:
        threshold: Threshold value for FTSwish activation formulation. (threshold < 0)
        mean_shift: Mean shifting value for FTSwish activation formulation.
                       (applied only if != 0, disabled by default)

    r   r   r   r   r   Nc                    s*   t    |dk sJ d|| _|| _d S )Nr   z*FTSwish threshold parameter should be < 0.)super__init__r   r   )selfr   r   	__class__r'   r:   r=   Z   s   

zFTSwish.__init__xc                 C   sP   |t | | j }t |dk|t j| jg|jd}| jdkr&|| j |S )Forward computation.r   )device)r)   sigmoidr   wheretensorrC   r   sub_r>   rA   r'   r'   r:   forwardb   s
   "
zFTSwish.forward)r   r   
__name__
__module____qualname____doc__floatr=   r)   TensorrI   __classcell__r'   r'   r?   r:   r+   K   s    r+   c                	       sP   e Zd ZdZ			ddedededd	f fd
dZdej	dej	fddZ
  ZS )r/   a  Mish activation definition.

    Mish(x) = x * tanh(softplus(x))

    Reference: https://arxiv.org/abs/1908.08681.

    Args:
        softplus_beta: Beta value for softplus activation formulation.
                         (Usually 0 > softplus_beta >= 2)
        softplus_threshold: Values above this revert to a linear function.
                         (Usually 10 > softplus_threshold >= 20)
        use_builtin: Whether to use PyTorch activation function if available.

    r   r   Fr   r   r   r   Nc                    sN   t    |rtj  _d S tj  _tjj||d _	 fdd _d S )N)r   r   c                    s   |    |  S N)r%   softplusrA   r>   r'   r:   <lambda>       zMish.__init__.<locals>.<lambda>)
r<   r=   r)   r,   r/   r    r4   r%   SoftplusrS   )r>   r   r   r   r?   rU   r:   r=   }   s   
zMish.__init__rA   c                 C   
   |  |S rB   )r    rH   r'   r'   r:   rI         
zMish.forward)r   r   F)rK   rL   rM   rN   rO   intboolr=   r)   rP   rI   rQ   r'   r'   r?   r:   r/   m   s    r/   c                       sF   e Zd ZdZddededdf fddZd	ejdejfd
dZ  Z	S )r2   a  Smish activation definition.

    Smish(x) = (alpha * x) * tanh(log(1 + sigmoid(beta * x)))
                 where alpha > 0 and beta > 0

    Reference: https://www.mdpi.com/2079-9292/11/4/540/htm.

    Args:
        alpha: Alpha value for Smish activation fomulation.
                 (Usually, alpha = 1. If alpha <= 0, set value to 1).
        beta: Beta value for Smish activation formulation.
                (Usually, beta = 1. If beta <= 0, set value to 1).

    r   r   r   r   Nc                    sL   t    tj  _|dkr|nd _|dkr|nd _ fdd _d S )Nr      c              
      s*    j |   tdt j|    S )Nr^   )r   r%   r)   logrD   r   rT   rU   r'   r:   rV      s    z Smish.__init__.<locals>.<lambda>)	r<   r=   r)   r,   r4   r%   r   r   r#   )r>   r   r   r?   rU   r:   r=      s
   
zSmish.__init__rA   c                 C   rY   rZ   )r#   rH   r'   r'   r:   rI      r[   zSmish.forward)r   r   rJ   r'   r'   r?   r:   r2      s    r2   c                       sF   e Zd ZdZddededdf fdd	Zd
ejdejfddZ	  Z
S )r3   a  Swish activation definition.

    Swish(x) = (beta * x) * sigmoid(x)
                 where beta = 1 defines standard Swish activation.

    References:
        https://arxiv.org/abs/2108.12943 / https://arxiv.org/abs/1710.05941v1.
        E-swish variant: https://arxiv.org/abs/1801.07145.

    Args:
        beta: Beta parameter for E-Swish.
                (beta >= 1. If beta < 1, use standard Swish).
        use_builtin: Whether to use PyTorch function if available.

    r   Fr   r   r   Nc                    sL   t    | _|dkr fdd _d S |rtj  _d S dd  _d S )Nr^   c                    s    j |  t|  S rR   )r   r)   rD   rT   rU   r'   r:   rV      rW   z Swish.__init__.<locals>.<lambda>c                 S   s   | t |  S rR   )r)   rD   rT   r'   r'   r:   rV      s    )r<   r=   r   r$   r)   r,   SiLU)r>   r   r   r?   rU   r:   r=      s   
zSwish.__init__rA   c                 C   rY   rZ   )r$   rH   r'   r'   r:   rI      r[   zSwish.forward)r   F)rK   rL   rM   rN   rO   r]   r=   r)   rP   rI   rQ   r'   r'   r?   r:   r3      s    r3   )
r   r   r   r   r   r   r   r   r   r   )rN   r)   packaging.versionr   r(   strrO   r\   r,   Moduler;   r+   r/   r2   r3   r'   r'   r'   r:   <module>   sT    	

D"'!