o
    óôj &  ã                   @   sÜ   d dl Z 	 e  d¡fdede jde jfdd„Zde  d¡fded	ed
ede jde jf
dd„Zde  d¡fded	ed
ede jde jf
dd„Z	dde jde jdedededededefdd„Zdde jdede jfdd„Z	dS ) é    NÚcpuÚsizeÚdeviceÚreturnc                 C   s0   t j| |d�}| | | ¡}| d¡}||k}|S )a  Create mask for subsequent steps (size, size).

    This mask is used only in decoder which works in an auto-regressive mode.
    This means the current step could only do attention with its left steps.

    In encoder, fully attention is used when streaming is not necessary and
    the sequence is not long. In this  case, no attention mask is needed.

    When streaming is need, chunk-based attention is used in encoder. See
    subsequent_chunk_mask for the chunk-based attention mask.

    Args:
        size (int): size of mask
        str device (str): "cpu" or "cuda" or torch.Tensor.device
        dtype (torch.device): result dtype

    Returns:
        torch.Tensor: mask

    Examples:
        >>> subsequent_mask(3)
        [[1, 0, 0],
         [1, 1, 0],
         [1, 1, 1]]
    ©r   éÿÿÿÿ)ÚtorchÚarangeÚexpandÚ	unsqueeze)r   r   r	   Úmask© r   úJ/var/www/html/academy/tools/voices/clone/CosyVoice/cosyvoice/utils/mask.pyÚsubsequent_mask5   s
   
r   r   Ú
chunk_sizeÚnum_left_chunksc                 C   sp   t j| | |t jd�}t| ƒD ]'}|dk rd}nt|| | | dƒ}t|| d | | ƒ}d||||…f< q|S )á7  Create mask for subsequent steps (size, size) with chunk size,
       this is for streaming encoder

    Args:
        size (int): size of mask
        chunk_size (int): size of chunk
        num_left_chunks (int): number of left chunks
            <0: use full chunk
            >=0: use num_left_chunks
        device (torch.device): "cpu" or "cuda" or torch.Tensor.device

    Returns:
        torch.Tensor: mask

    Examples:
        >>> subsequent_chunk_mask(4, 2)
        [[1, 1, 0, 0],
         [1, 1, 0, 0],
         [1, 1, 1, 1],
         [1, 1, 1, 1]]
    )r   Údtyper   é   T)r   ÚzerosÚboolÚrangeÚmaxÚmin)r   r   r   r   ÚretÚiÚstartZendingr   r   r   Ú subsequent_chunk_mask_deprecatedY   s   r   c                 C   s>   t j| |d�}t j||dd�d | }| d¡| d¡k }|S )r   r   Útrunc)Úrounding_moder   r   )r   r	   Údivr   )r   r   r   r   Zpos_idxZblock_valuer   r   r   r   Úsubsequent_chunk_mask   s   r!   TÚxsÚmasksÚuse_dynamic_chunkÚuse_dynamic_left_chunkÚdecoding_chunk_sizeÚstatic_chunk_sizeÚnum_decoding_left_chunksÚenable_full_contextc                 C   s<  |r[|   d¡}|dk r|}	d}
n6|dkr|}	|}
n-t d|d¡ ¡ }	d}
|	|d kr/|r/|}	n|	d d }	|rF|d |	 }t d|d¡ ¡ }
t|   d¡|	|
| jƒ}| d¡}||@ }n|dkrv|}
t|   d¡||
| jƒ}| d¡}||@ }n|}|jtjks€J ‚|j	dd�dk 	¡  ¡ dkrœt
dƒ d	||j	dd�dk< |S )
a¯   Apply optional mask for encoder.

    Args:
        xs (torch.Tensor): padded input, (B, L, D), L for max length
        mask (torch.Tensor): mask for xs, (B, 1, L)
        use_dynamic_chunk (bool): whether to use dynamic chunk or not
        use_dynamic_left_chunk (bool): whether to use dynamic left chunk for
            training.
        decoding_chunk_size (int): decoding chunk size for dynamic chunk, it's
            0: default for training, use random dynamic chunk.
            <0: for decoding, use full chunk.
            >0: for decoding, use fixed chunk size as set.
        static_chunk_size (int): chunk size for static chunk training/decoding
            if it's greater than 0, if use_dynamic_chunk is true,
            this parameter will be ignored
        num_decoding_left_chunks: number of left chunks, this is for decoding,
            the chunk size is decoding_chunk_size.
            >=0: use num_decoding_left_chunks
            <0: use all left chunks
        enable_full_context (bool):
            True: chunk size is either [1, 25] or full context(max_len)
            False: chunk size ~ U[1, 25]

    Returns:
        torch.Tensor: chunk mask of the input xs.
    r   r   r   )r   é   é   )Údimzoget chunk_masks all false at some timestep, force set to true, make sure they are masked in futuer computation!T)r   r   ÚrandintÚitemr!   r   r   r   r   ÚsumÚprint)r"   r#   r$   r%   r&   r'   r(   r)   Úmax_lenr   r   Zmax_left_chunksZchunk_masksr   r   r   Úadd_optional_chunk_mask¡   sP   #
ÿÿþ

þ

r2   Úlengthsr1   c                 C   s`   |   d¡}|dkr|n|  ¡  ¡ }tjd|tj| jd�}| d¡ ||¡}|  d¡}||k}|S )a¯  Make mask tensor containing indices of padded part.

    See description of make_non_pad_mask.

    Args:
        lengths (torch.Tensor): Batch of lengths (B,).
    Returns:
        torch.Tensor: Mask tensor containing indices of padded part.

    Examples:
        >>> lengths = [5, 3, 2]
        >>> make_pad_mask(lengths)
        masks = [[0, 0, 0, 0 ,0],
                 [0, 0, 0, 1, 1],
                 [0, 0, 1, 1, 1]]
    r   )r   r   r   )	r   r   r.   r   r	   Úint64r   r   r
   )r3   r1   Ú
batch_sizeZ	seq_rangeZseq_range_expandZseq_length_expandr   r   r   r   Úmake_pad_maskï   s   
ý
r6   )T)r   )
r   r   ÚintÚTensorr   r   r!   r   r2   r6   r   r   r   r   Ú<module>   sj   %þÿþ
ý'üÿþýü
û)üÿþýü
û)
ùÿþýüûú
ù N