o
    i>(                     @   s   d dl Z d dlZd dlm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					ddejd	ejd
ee d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G dd dej
jZdS )    N)Sequence)Union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*   Y/home/ubuntu/.local/lib/python3.10/site-packages/funasr/models/specaug/mask_along_axis.pymask_along_axis   sB   



r,   lfr_ratec                 C   s  |   }|  dkr| d|  d|  d} | jd }| j| | }	tj|d |d ||f| jdd}
|dkrA|
d|d}
tjdt	d|	|
	  ||f| jdd}|dkr|
 }tj|ddf| jtjd}t|D ]}||	|  }tj||fdd	}qp| j| }	tj|	| jdd
d
d
d
f }||k|||
 k  }|jdd	}|dkr|d}n	|dkr|d}|rd}n|  }| jr| ||} n| ||} | j| } | |fS )u  Apply 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
        lfr_rate：low frame rate
    r   r   r   r   r   r   r   )r   dtyper   Nr   )r   r   r   r   r   r   r   r   repeatr   clonezerosint32rangecatr   r   r   r   r    r!   )r   r	   r
   r   r   r   r-   r"   r#   r$   r%   r&   mask_pos_rawi
mask_pos_ir'   r(   r)   r*   r*   r+   mask_along_axis_lfrF   sT   



r8   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 |tr	d|f}t|dkrtd| |d |d ks J t |tr7|dkr,d}n|dkr3d}ntd|dkr?d| _n|dkrGd| _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)
isinstanceintlen	TypeErrorstr
ValueError	mask_axissuper__init__r
   r   r   r   )selfr
   r   r   r   	__class__r*   r+   rG      s.   



zMaskAlongAxis.__init__c                 C      d| j  d| j d| j S Nzmask_width_range=, num_mask=, axis=r
   r   rE   rH   r*   r*   r+   
extra_repr      
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   rH   r   r	   r*   r*   r+   forward   s   zMaskAlongAxis.forward)r   r   r:   TN__name__
__module____qualname__r   r@   r   rC   boolrG   rQ   r   TensorrV   __classcell__r*   r*   rI   r+   r9      s     
# r9   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 |tr	d|f}t|dkrtd| |d |d ks J t |tr7|dkr,d}n|dkr3d}ntd|dkr?d| _n|dkrGd| _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=   r>   )r?   floatrA   rB   rC   rD   rE   rF   rG   ra   r   r   r   )rH   ra   r   r   r   rI   r*   r+   rG      s2   



z&MaskAlongAxisVariableMaxWidth.__init__c                 C   rK   )Nzmask_width_ratio_range=rM   rN   )ra   r   rE   rP   r*   r*   r+   rQ      rR   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 )rS   r   r   rT   )
r   r   mathfloorra   r   minr,   r   r   )rH   r   r	   max_seq_lenmin_mask_widthmax_mask_widthr*   r*   r+   rV      s   z%MaskAlongAxisVariableMaxWidth.forward)r`   r   r:   TrW   )rY   rZ   r[   __doc__r   rb   r   r@   rC   r\   rG   rQ   r   r]   rV   r^   r*   r*   rI   r+   r_      s"    
$ r_   c                       sr   e Zd Z					ddeeee f dedeeef d	ed
ef
 fddZdd Z	dde
jde
jfddZ  ZS )MaskAlongAxisLFRr   r   r:   Tr   r
   r   r   r   r-   c                    s   t |tr	d|f}t|dkrtd| |d |d ks J t |tr9|dkr.d}d}n|dkr5d}ntd|dkrCd| _d}n|dkrKd| _nd| _t   || _	|| _
|| _|| _|| _d S r;   )r?   r@   rA   rB   rC   rD   rE   rF   rG   r
   r   r   r   r-   )rH   r
   r   r   r   r-   rI   r*   r+   rG     s4   



zMaskAlongAxisLFR.__init__c                 C   rK   rL   rO   rP   r*   r*   r+   rQ   ;  rR   zMaskAlongAxisLFR.extra_reprNr   r	   c              	   C   s    t ||| j| j| j| j| jdS )rS   )r
   r   r   r   r-   )r8   r
   r   r   r   r-   rU   r*   r*   r+   rV   A  s   zMaskAlongAxisLFR.forward)r   r   r:   Tr   rW   rX   r*   r*   rI   r+   rj     s&    
' rj   )r   r   r   T)r   r   r   Tr   )rc   r   typingr   r   r]   r@   r\   r,   r8   nnModuler9   r_   rj   r*   r*   r*   r+   <module>   sX    
B
H;J