o
    ߥi*%                     @   s   d dl Z d dlmZ d dlm  mZ d dl mZ d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G dd dejZG dd dejZG dd dejZdS )    N)Tensor:0yE>c                       s*   e Zd ZdZd fdd	Zdd Z  ZS )	GlobalLayerNorma?  Calculate Global Layer Normalization.

    Args:
       dim : (int or list or torch.Size)
           Input shape from an expected input of size.
       eps : float
           A value added to the denominator for numerical stability.
       elementwise_affine : bool
          A boolean value that when set to True,
          this module has learnable per-element affine parameters
          initialized to ones (for weights) and zeros (for biases).

    Example:
    -------
    >>> x = torch.randn(5, 10, 20)
    >>> GLN = GlobalLayerNorm(10, 3)
    >>> x_norm = GLN(x)
    r   Tc                    s   t t|   || _|| _|| _| jrM|dkr-tt	| jd| _
tt| jd| _|dkrKtt	| jdd| _
tt| jdd| _d S d S | dd  | dd  d S )N         weightbias)superr   __init__dimepselementwise_affinenn	Parametertorchonesr   zerosr	   register_parameter)selfr   shaper   r   	__class__ e/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/audio/separation/m2/conv_module.pyr      s   zGlobalLayerNorm.__init__c                 C   s   |  dkr<tj|ddd}tj|| d ddd}| jr0| j||  t|| j  | j }n|| t|| j  }|  dkrytj|ddd}tj|| d ddd}| jrm| j||  t|| j  | j }|S || t|| j  }|S )zReturns the normalized tensor.

        Args:
            x : torch.Tensor
                Tensor of size [N, C, K, S] or [N, C, L].
        r   r      T)keepdimr   r   )r   r   r   )r   r   meanr   r   sqrtr   r	   )r   xr   varr   r   r   forward0   s$   zGlobalLayerNorm.forward)r   T__name__
__module____qualname____doc__r   r"   __classcell__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 )CumulativeLayerNorma^  Calculate Cumulative Layer Normalization.

       Args:
           dim : int
               Dimension that you want to normalize.
           elementwise_affine : True
               Learnable per-element affine parameters.

    Example:
    -------
    >>> x = torch.randn(5, 10, 20)
    >>> CLN = CumulativeLayerNorm(10)
    >>> x_norm = CLN(x)
    Tc                    s   t t| j||dd d S )Nr   )r   r   )r
   r)   r   )r   r   r   r   r   r   r   c   s   

zCumulativeLayerNorm.__init__c                    sx   |  dkr |dddd }t |}|dddd }|  dkr:t|dd}t |}t|dd}|S )zReturns the normalized tensor.

        Args:
            x : torch.Tensor
                Tensor size [N, C, K, S] or [N, C, L]
        r   r   r   r   r   )r   permute
contiguousr
   r"   r   	transposer   r    r   r   r   r"   g   s   	zCumulativeLayerNorm.forward)Tr#   r   r   r   r   r)   S   s    r)   c                       s8   e Zd ZdZdef fddZdedefddZ  ZS )		Transposez; Wrapper class of torch.transpose() for Sequential module. r   c                    s   t t|   || _d S N)r
   r.   r   r   )r   r   r   r   r   r      s   
zTranspose.__init__r    returnc                 C   s   |j | j S r/   )r,   r   r-   r   r   r   r"      s   zTranspose.forward)	r$   r%   r&   r'   tupler   r   r"   r(   r   r   r   r   r.      s    r.   c                       sX   e Zd ZdZ			ddedededed	ed
eddf fddZdedefddZ  Z	S )DepthwiseConv1daY  When groups == in_channels and out_channels == K * in_channels, where K is a positive integer,
    this operation is termed in literature as depthwise convolution.

    Args:
        in_channels (int): Number of channels in the input
        out_channels (int): Number of channels produced by the convolution
        kernel_size (int or tuple): Size of the convolving kernel
        stride (int, optional): Stride of the convolution. Default: 1
        padding (int or tuple, optional): Zero-padding added to both sides of the input. Default: 0
        bias (bool, optional): If True, adds a learnable bias to the output. Default: True
    Inputs: inputs
        - **inputs** (batch, in_channels, time): Tensor containing input vector
    Returns: outputs
        - **outputs** (batch, out_channels, time): Tensor produces by depthwise 1-D convolution.
    r   r   Fin_channelsout_channelskernel_sizestridepaddingr	   r0   Nc              	      s@   t t|   || dksJ dtj|||||||d| _d S )Nr   z7out_channels should be constant multiple of in_channels)r3   r4   r5   groupsr6   r7   r	   )r
   r2   r   r   Conv1dconv)r   r3   r4   r5   r6   r7   r	   r   r   r   r      s   	zDepthwiseConv1d.__init__inputsc                 C   s
   |  |S r/   )r:   r   r;   r   r   r   r"      s   
zDepthwiseConv1d.forward)r   r   F)
r$   r%   r&   r'   intboolr   r   r"   r(   r   r   r   r   r2      s*    r2   c                       sP   e Zd ZdZ			ddedededed	d
f
 fddZded	efddZ  Z	S )
ConvModulea  
    Conformer convolution module starts with a pointwise convolution and a gated linear unit (GLU).
    This is followed by a single 1-D depthwise convolution layer. Batchnorm is  deployed just after the convolution
    to aid training deep models.

    Args:
        in_channels (int): Number of channels in the input
        kernel_size (int or tuple, optional): Size of the convolving kernel Default: 17
        dropout_p (float, optional): probability of dropout
    Inputs: inputs
        inputs (batch, time, dim): Tensor contains input sequences
    Outputs: outputs
        outputs (batch, time, dim): Tensor produces by conformer convolution module.
       r   皙?r3   r5   expansion_factor	dropout_pr0   Nc              
      sd   t t|   |d d dksJ d|dksJ dttddt|||d|d d d| _d S )	Nr   r   r   z5kernel_size should be a odd number for 'SAME' paddingz+Currently, Only Supports expansion_factor 2r   )r   )r6   r7   )r
   r?   r   r   
Sequentialr.   r2   
sequential)r   r3   r5   rB   rC   r   r   r   r      s&   

zConvModule.__init__r;   c                 C   s   ||  |dd S )Nr   r   )rE   r,   r<   r   r   r   r"      s   zConvModule.forward)r@   r   rA   )
r$   r%   r&   r'   r=   floatr   r   r"   r(   r   r   r   r   r?      s"    r?   c                       s&   e Zd Zd fdd	Zdd Z  ZS )	DilatedDenseNetr      @   c                    s  t t|   || _|| _tjddd| _|d d | _| jdf| _	t
| jD ]c}d| }||d |d   d }t| d|d tjdd||fdd t| d|d tj| j|d  | j| j	|df| jd	d
 t| d|d tj|dd t| d|d t| j q'd S )N)r   r   r   r   g        )valuer   r   pad{}r   conv{}F)r5   dilationr8   r	   norm{}T)affineprelu{})r
   rG   r   depthr3   r   ConstantPad2dpadtwidthr5   rangesetattrformatConv2dInstanceNorm2dPReLU)r   rQ   lorderr3   idil
pad_lengthr   r   r   r      s8   	 zDilatedDenseNet.__init__c                 C   s   t |d}|dddd}|}t| jD ];}t| d|d |}t| d|d |}t| d|d |}t| d|d |}t j||gdd	}q|dddd}|dS )
Nr   r   r   r   rK   rL   rN   rP   )r   )	r   	unsqueezer*   rU   rQ   getattrrW   catsqueeze)r   r    x_perskipr\   outout1r   r   r   r"      s   
zDilatedDenseNet.forward)r   rH   rI   )r$   r%   r&   r   r"   r(   r   r   r   r   rG      s    rG   c                       s,   e Zd Zejdf fdd	Zdd Z  ZS )FFConvMDilatedrA   c              
      sB   t    t||t||t tdd|dt|| _d S )Nr   r@   )rQ   r[   r3   )	r
   r   r   rD   LinearSiLUrG   Dropoutmdl)r   dim_indim_out
norm_klassdropoutr   r   r   r   
  s   

zFFConvMDilated.__init__c                 C   s   |  |}|S r/   )rk   )r   r    outputr   r   r   r"     s   
zFFConvMDilated.forward)r$   r%   r&   r   	LayerNormr   r"   r(   r   r   r   r   rg     s    rg   )r   torch.nnr   torch.nn.initinitr   EPSModuler   rq   r)   r.   r2   r?   rG   rg   r   r   r   r   <module>   s   H,*+)