o
    
j                     @   s`   d dl Z d dl mZ G dd dejZG dd dejZG dd dejZG d	d
 d
ejZdS )    N)nnc                       s&   e Zd Zd fdd	Zdd Z  ZS )	LayerNorm-C6?c                    sJ   t    || _|| _ttd|dd | _tt	d|d| _
dS )a  Layer norm for the 2nd dimension of the input.
        Args:
            channels (int): number of channels (2nd dimension) of the input.
            eps (float): to prevent 0 division

        Shapes:
            - input: (B, C, T)
            - output: (B, C, T)
           皙?Nsuper__init__channelsepsr   	Parametertorchonesgammazerosbetaselfr
   r   	__class__ W/home/kuhnn/.local/lib/python3.10/site-packages/TTS/tts/layers/generic/normalization.pyr	      s
   

zLayerNorm.__init__c                 C   sT   t j|ddd}t j|| d ddd}|| t || j  }|| j | j }|S )Nr   T)keepdim   )r   meanrsqrtr   r   r   )r   xr   variancer   r   r   forward   s
   zLayerNorm.forward)r   )__name__
__module____qualname__r	   r   __classcell__r   r   r   r   r      s    r   c                       s*   e Zd ZdZd fdd	Zdd Z  ZS )
LayerNorm2a  Layer norm for the 2nd dimension of the input using torch primitive.
    Args:
        channels (int): number of channels (2nd dimension) of the input.
        eps (float): to prevent 0 division

    Shapes:
        - input: (B, C, T)
        - output: (B, C, T)
    h㈵>c                    s>   t    || _|| _tt|| _tt	|| _
d S Nr   r   r   r   r   r	   *   s
   
zLayerNorm2.__init__c                 C   s8   | dd}tjj|| jf| j| j| j}| ddS )Nr   )		transposer   r   
functional
layer_normr
   r   r   r   r   r   r   r   r   r   2   s    zLayerNorm2.forward)r$   r   r    r!   __doc__r	   r   r"   r   r   r   r   r#      s    
r#   c                       s.   e Zd ZdZd fdd	Z fddZ  ZS )	TemporalBatchNorm1dz6Normalize each channel separately over time and batch.Tr   c                    s   t  j||||d d S )N)affinetrack_running_statsmomentum)r   r	   )r   r
   r.   r/   r0   r   r   r   r	   ;   s   zTemporalBatchNorm1d.__init__c                    s   t  |ddddS )Nr   r   )r   r   r'   r*   r   r   r   r   >   s   zTemporalBatchNorm1d.forward)TTr   r+   r   r   r   r   r-   8   s    r-   c                       sD   e Zd ZdZd fdd	ZdddZdd	 Zd
d Zdd Z  Z	S )ActNormao  Activation Normalization bijector as an alternative to Batch Norm. It computes
    mean and std from a sample data in advance and it uses these values
    for normalization at training.

    Args:
        channels (int): input channels.
        ddi (False): data depended initialization flag.

    Shapes:
        - inputs: (B, C, T)
        - outputs: (B, C, T)
    Fc                    sH   t    || _| | _ttd|d| _ttd|d| _	d S )Nr   )
r   r	   r
   initializedr   r   r   r   logsbias)r   r
   ddikwargsr   r   r   r	   P   s
   
zActNorm.__init__Nc                 K   s   |d u rt |dd|dj|j|jd}t |ddg}| js,| || d| _|rB|| j	 t 
| j  | }d }||fS | j	t 
| j|  | }t | j| }||fS )Nr   r   r   )devicedtypeT)r   r   sizetor7   r8   sumr2   
initializer4   expr3   )r   r   x_maskreverser6   x_lenzlogdetr   r   r   r   X   s   (zActNorm.forwardc                 C   s   d S r%   r   )r   r   r   r   store_inversei   s   zActNorm.store_inversec                 C   s   | | _ d S r%   )r2   )r   r5   r   r   r   set_ddil   s   zActNorm.set_ddic           
      C   s   t  l t |ddg}t || ddg| }t || | ddg| }||d  }dt t |d }| t |  j| jj j	| jj
d}| j| jj j	| jj
d}	| jj| | jj|	 W d    d S 1 ssw   Y  d S )Nr   r   g      ?gư>)r8   )r   no_gradr;   log	clamp_minr=   viewr4   shaper:   r8   r3   datacopy_)
r   r   r>   denommm_sqvr3   	bias_init	logs_initr   r   r   r<   o   s   
("zActNorm.initialize)F)NF)
r   r    r!   r,   r	   r   rC   rD   r<   r"   r   r   r   r   r1   B   s    
r1   )r   r   Moduler   r#   BatchNorm1dr-   r1   r   r   r   r   <module>   s    
