
    Pi                     z    d dl Z d dl mZmZmZ d dlmZ  G d dej                  Z G d dej                  ZdS )    N)nnsinpow)	Parameterc                   ,     e Zd ZdZ	 d fd	Zd Z xZS )Snakea  
    Implementation of a sine-based periodic activation function
    Shape:
        - Input: (B, C, T)
        - Output: (B, C, T), same shape as the input
    Parameters:
        - alpha - trainable parameter
    References:
        - This activation function is from this paper by Liu Ziyin, Tilman Hartwig, Masahito Ueda:
        https://arxiv.org/abs/2006.08195
    Examples:
        >>> a1 = snake(256)
        >>> x = torch.randn(256)
        >>> x = a1(x)
          ?TFc                 L   t          t          |                                            || _        || _        | j        r*t          t          j        |          |z            | _        n)t          t          j	        |          |z            | _        || j        _
        d| _        dS )a   
        Initialization.
        INPUT:
            - in_features: shape of the input
            - alpha: trainable parameter
            alpha is initialized to 1 by default, higher values = higher-frequency.
            alpha will be trained along with the rest of your model.
        &.>N)superr   __init__in_featuresalpha_logscaler   torchzerosalphaonesrequires_gradno_div_by_zeroselfr   r   alpha_trainabler   	__class__s        h/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/neucodec/activations.pyr   zSnake.__init__   s     	eT##%%%& - 	D"5;{#;#;e#CDDDJJ"5:k#:#:U#BCCDJ#2
 )    c                     | j                             d                              d          }| j        rt          j        |          }|d|| j        z   z  t          t          ||z            d          z  z   }|S )u   
        Forward pass of the function.
        Applies the function to the input elementwise.
        Snake ∶= x + 1/a * sin^2 (xa)
        r   r	      )r   	unsqueezer   r   expr   r   r   )r   xr   s      r   forwardzSnake.forward3   sv     
$$Q''11"55 	%Ie$$E 334CE	NNA8N8NNNr   r	   TF__name__
__module____qualname____doc__r   r"   __classcell__r   s   @r   r   r   	   s]         " LQ* * * * * *2      r   r   c                   ,     e Zd ZdZ	 d fd	Zd Z xZS )	SnakeBetaam  
    A modified Snake function which uses separate parameters for the magnitude of the periodic components
    Shape:
        - Input: (B, C, T)
        - Output: (B, C, T), same shape as the input
    Parameters:
        - alpha - trainable parameter that controls frequency
        - beta - trainable parameter that controls magnitude
    References:
        - This activation function is a modified version based on this paper by Liu Ziyin, Tilman Hartwig, Masahito Ueda:
        https://arxiv.org/abs/2006.08195
    Examples:
        >>> a1 = snakebeta(256)
        >>> x = torch.randn(256)
        >>> x = a1(x)
    r	   TFc                    t          t          |                                            || _        || _        | j        rSt          t          j        |          |z            | _        t          t          j        |          |z            | _	        nRt          t          j
        |          |z            | _        t          t          j
        |          |z            | _	        || j        _        || j	        _        d| _        dS )a  
        Initialization.
        INPUT:
            - in_features: shape of the input
            - alpha - trainable parameter that controls frequency
            - beta - trainable parameter that controls magnitude
            alpha is initialized to 1 by default, higher values = higher-frequency.
            beta is initialized to 1 by default, higher values = higher-magnitude.
            alpha will be trained along with the rest of your model.
        r   N)r   r,   r   r   r   r   r   r   r   betar   r   r   r   s        r   r   zSnakeBeta.__init__S   s     	i'')))& - 	C"5;{#;#;e#CDDDJ!%+k":":U"BCCDII"5:k#:#:U#BCCDJ!%*["9"9E"ABBDI#2
 "1	)r   c                 z   | j                             d                              d          }| j                            d                              d          }| j        r(t	          j        |          }t	          j        |          }|d|| j        z   z  t          t          ||z            d          z  z   }|S )u   
        Forward pass of the function.
        Applies the function to the input elementwise.
        SnakeBeta ∶= x + 1/b * sin^2 (xa)
        r   r   r	   r   )	r   r   r.   r   r   r    r   r   r   )r   r!   r   r.   s       r   r"   zSnakeBeta.forwardq   s     
$$Q''11"55y""1%%//33 	#Ie$$E9T??Dt223s3q5y>>17M7MMMr   r#   r$   r*   s   @r   r,   r,   A   s]         $ LQ* * * * * *<      r   r,   )	r   r   r   r   torch.nnr   Moduler   r,    r   r   <module>r3      s                    5 5 5 5 5BI 5 5 5p= = = = =	 = = = = =r   