o
    i1                     @   s~   d dl mZmZ d dlZd dlZ			 		 		ddeeef deej d	ed
ede	dedede
dede
dedejfddZdS )    )OptionalTupleNstatic        FTshapepadding_mask	mask_probmask_length	mask_type
mask_other	min_masks
no_overlap	min_spacerequire_same_masksmask_dropoutreturnc              	      s&  | \}}t ||fd}t|| t| t j  }t||}g }t|D ]}|durN|||  	 
  }t|| t| t j  }t||}n|}|}|dkr]t ||nA|dkrot jj||d d |dn/|dkrt jj|||dd	d
 D n|dkrt jj||ddd
 D ntd| t	dkrt||d d< |rg fdd}d|fg}t}tddD ]> t  fdd|D t j}t 	|}|dkr n#|t 	| }t jjt||d}||\}}|||| | qt n-t}|| |kr|| d }t jj|| |ddt fdd
ttD |t |k   q&tdd
 |D }t|D ]<\}t|krk|	rkt jj|dd|
dkrt t|
 t}t jjt| ddd||f< qT|S )a  
    Computes random mask spans for a given shape

    Args:
        shape: the the shape for which to compute masks.
            should be of size 2 where first element is batch size and 2nd is timesteps
        padding_mask: optional padding mask of the same size as shape, which will prevent masking padded elements
        mask_prob: probability for each token to be chosen as start of the span to be masked. this will be multiplied by
            number of timesteps divided by length of mask span to mask approximately this percentage of all elements.
            however due to overlaps, the actual number will be smaller (unless no_overlap is True)
        mask_type: how to compute mask lengths
            static = fixed size
            uniform = sample from uniform distribution [mask_other, mask_length*2]
            normal = sample from normal distribution with mean mask_length and stdev mask_other. mask is min 1 element
            poisson = sample from possion distribution with lambda = mask length
        min_masks: minimum number of masked spans
        no_overlap: if false, will switch to an alternative recursive algorithm that prevents spans from overlapping
        min_space: only used if no_overlap is True, this is how many elements to keep unmasked between spans
        require_same_masks: if true, will randomly drop out masks until same amount of masks remains in each sample
        mask_dropout: randomly dropout this percentage of masks in each example
    FNr   uniform      )sizenormalc                 S   s   g | ]}t d tt|qS )r   )maxintround.0x r   U/home/ubuntu/.local/lib/python3.10/site-packages/funasr/models/data2vec/data_utils.py
<listcomp>O   s    z(compute_mask_indices.<locals>.<listcomp>poissonc                 S   s   g | ]}t t|qS r   )r   r   r   r   r   r   r   R   s    zunknown mask selection r   c                    s   t j| ||   fddt|D  g } |   |kr,||   d f |  |  |krA| |  |f |S )Nc                 3   s    | ]} | V  qd S )Nr   )r   i
span_startr   r   	<genexpr>^   s    z8compute_mask_indices.<locals>.arrange.<locals>.<genexpr>r   )nprandomrandintextendrangeappend)selengthkeep_length	new_parts)mask_idcr   r"   r   arrange\   s   z%compute_mask_indices.<locals>.arrangeT)reversec                 3   s0    | ]\}}||   kr|| nd V  qdS )r   Nr   )r   r+   r,   )r-   r   r   r   r$   k   s   . z'compute_mask_indices.<locals>.<genexpr>)p)replacec                    s*   g | ]}t  | D ]}| | q
qS r   )r)   )r   joffset)lengthsr0   r   r   r   ~   s   * c                 S   s   g | ]}t |qS r   )len)r   mr   r   r   r      s    )r%   fullr   floatr&   randr   r)   longsumitemr'   r   r    	Exceptionminsortedfromiterint32choicer8   popr(   asarrayr*   unique	enumeraterintastype)r   r   r   r	   r
   r   r   r   r   r   r   bszall_szmaskall_num_mask	mask_idcsr!   sznum_maskr1   parts
min_lengthlensl_sumprobscr+   r,   min_len	num_holesr   )r-   r7   r0   r   r   compute_mask_indices   s   #



r[   )r   r   r   Fr   Tr   )typingr   r   numpyr%   torchr   Tensorr;   strboolndarrayr[   r   r   r   r   <module>   sF   
	
