o
    ib                     @   s   d dl Z d dlmZmZ d dlZd dlmZ 				ddejd	ejd
ee dedede	fddZ
G dd dejjZG dd dejjZdS )    N)SequenceUnion)check_argument_typesr            Tspecspec_lengthsmask_width_rangedimnum_maskreplace_with_zeroc                 C   s<  |   }|  dkr| d|  d|  d} | jd }| j| }tj|d |d ||f| jdd}	tjdtd||	  ||f| jdd}
tj	|| jdddddf }|
|k||
|	 k  }|j
dd	}|dkrs|d}n	|dkr||d}|rd
}n|  }| jr| ||} n| ||} | j| } | |fS )zApply mask along the specified direction.

    Args:
        spec: (Batch, Length, Freq)
        spec_lengths: (Length): Not using lengths in this implementation
        mask_width_range: Select the width randomly between this range
       r      r   r   )deviceN)r           )sizer   viewshapetorchrandintr   	unsqueezemaxarangeanymeanrequires_gradmasked_fillmasked_fill_)r	   r
   r   r   r   r   org_sizeBDmask_lengthmask_posaranmaskvalue r)   R/home/ubuntu/.local/lib/python3.10/site-packages/espnet2/layers/mask_along_axis.pymask_along_axis   sB   



r+   c                	       sl   e Zd Z				ddeeee f dedeeef def fd	d
Zdd Z	dde
jde
jfddZ  ZS )MaskAlongAxisr   r   timeTr   r   r   r   c                    s   t  sJ t|trd|f}t|dkrtd| |d |d ks%J t|tr<|dkr1d}n|dkr8d}ntd|dkrDd| _n|dkrLd| _nd| _t 	  || _
|| _|| _|| _d S )	Nr   r   z8mask_width_range must be a tuple of int and int values: r   r-   freq!dim must be int, 'time' or 'freq'unknown)r   
isinstanceintlen	TypeErrorstr
ValueError	mask_axissuper__init__r   r   r   r   )selfr   r   r   r   	__class__r)   r*   r9   H   4   




zMaskAlongAxis.__init__c                 C      d| j  d| j d| j S )Nzmask_width_range=, num_mask=, axis=)r   r   r7   r:   r)   r)   r*   
extra_reprm      
zMaskAlongAxis.extra_reprNr	   r
   c                 C   s   t ||| j| j| j| jdS )QForward function.

        Args:
            spec: (Batch, Length, Freq)
        r   r   r   r   )r+   r   r   r   r   )r:   r	   r
   r)   r)   r*   forwards   s   zMaskAlongAxis.forward)r   r   r-   TN)__name__
__module____qualname__r   r2   r   r5   boolr9   rB   r   TensorrF   __classcell__r)   r)   r;   r*   r,   G   s     
% r,   c                	       sp   e Zd ZdZ				ddeeee f dedeeef d	e	f fd
dZ
dd ZddejdejfddZ  ZS )MaskAlongAxisVariableMaxWidthzMask input spec along a specified axis with variable maximum width.

    Formula:
        max_width = max_width_ratio * seq_len
    r   g?r   r-   Tmask_width_ratio_ranger   r   r   c                    s   t  sJ t|trd|f}t|dkrtd| |d |d ks%J t|tr<|dkr1d}n|dkr8d}ntd|dkrDd| _n|dkrLd| _nd	| _t 	  || _
|| _|| _|| _d S )
Nr   r   zBmask_width_ratio_range must be a tuple of float and float values: r   r   r-   r.   r/   r0   )r   r1   floatr3   r4   r5   r6   r7   r8   r9   rP   r   r   r   )r:   rP   r   r   r   r;   r)   r*   r9      r=   z&MaskAlongAxisVariableMaxWidth.__init__c                 C   r>   )Nzmask_width_ratio_range=r?   r@   )rP   r   r7   rA   r)   r)   r*   rB      rC   z(MaskAlongAxisVariableMaxWidth.extra_reprNr	   r
   c                 C   sz   |j | j }t|| jd  }td|g}t|| jd  }t||g}||kr9t||||f| j| j| j	dS ||fS )rD   r   r   rE   )
r   r   mathfloorrP   r   minr+   r   r   )r:   r	   r
   max_seq_lenmin_mask_widthmax_mask_widthr)   r)   r*   rF      s   z%MaskAlongAxisVariableMaxWidth.forward)rO   r   r-   TrG   )rH   rI   rJ   __doc__r   rQ   r   r2   r5   rK   r9   rB   r   rL   rF   rM   r)   r)   r;   r*   rN      s"    
% rN   )r   r   r   T)rR   typingr   r   r   	typeguardr   rL   r2   rK   r+   nnModuler,   rN   r)   r)   r)   r*   <module>   s.    
?=