o
    )j*%                     @   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torchZonesr   Zzerosr	   Zregister_parameter)selfr   shaper   r   	__class__ r/var/www/html/Deteccion_Ine/venv/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)Z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	   r,   Nc              	      s@   t t|   || dksJ dtj|||||||d| _d S )Nr   z7out_channels should be constant multiple of in_channels)r/   r0   r1   groupsr2   r3   r	   )r
   r.   r   r   ZConv1dconv)r   r/   r0   r1   r2   r3   r	   r   r   r   r      s   	zDepthwiseConv1d.__init__inputsc                 C   s
   |  |S r+   )r5   r   r6   r   r   r   r      s   
zDepthwiseConv1d.forward)r   r   F)
r    r!   r"   r#   intboolr   r   r   r$   r   r   r   r   r.      s*    r.   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   皙?r/   r1   expansion_factor	dropout_pr,   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   )r2   r3   )r
   r:   r   r   
Sequentialr*   r.   
sequential)r   r/   r1   r=   r>   r   r   r   r      s&   

zConvModule.__init__r6   c                 C   s   ||  |dd S )Nr   r   )r@   r(   r7   r   r   r   r      s   zConvModule.forward)r;   r   r<   )
r    r!   r"   r#   r8   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)r1   Zdilationr4   r	   norm{}T)Zaffineprelu{})r
   rB   r   depthr/   r   ZConstantPad2dpadZtwidthr1   rangesetattrformatZConv2dZInstanceNorm2dZPReLU)r   rJ   lorderr/   iZdilZ
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   rF   rG   rH   rI   )r   )	r   Z	unsqueezer&   rL   rJ   getattrrN   catZsqueeze)r   r   Zx_perskiprP   outZout1r   r   r   r      s   
zDilatedDenseNet.forward)r   rC   rD   )r    r!   r"   r   r   r$   r   r   r   r   rB      s    rB   c                       s,   e Zd Zejdf fdd	Zdd Z  ZS )FFConvMDilatedr<   c              
      sB   t    t||t||t tdd|dt|| _d S )Nr   r;   )rJ   rO   r/   )	r
   r   r   r?   ZLinearZSiLUrB   ZDropoutmdl)r   Zdim_inZdim_outZ
norm_klassZdropoutr   r   r   r   
  s   

zFFConvMDilated.__init__c                 C   s   |  |}|S r+   )rV   )r   r   outputr   r   r   r     s   
zFFConvMDilated.forward)r    r!   r"   r   	LayerNormr   r   r$   r   r   r   r   rU     s    rU   )r   Ztorch.nnr   Ztorch.nn.initinitr   ZEPSModuler   rX   r%   r*   r.   r:   rB   rU   r   r   r   r   <module>   s   H,*+)