U
    CŒÑhW  ã                
   @   s¢   d dl mZ d dlmZmZmZ d dlZd dlmZ d dlm	Z	 g Z
e	deeee ee eeeef dœdd„ƒZeG d	d
„ d
ƒƒZdeeeee dœdd„ZdS )é    )Ú	dataclass)ÚListÚOptionalÚTupleN)ÚTensor)Úfail_if_no_align)Ú	log_probsÚtargetsÚinput_lengthsÚtarget_lengthsÚblankÚreturnc           	      C   sÚ   ||krt d|› d�ƒ‚t |¡| jd kr4t dƒ‚|dkrj|  d¡|  d¡ }}tj|f|tj| jd�}|dkr | d¡| d¡ }}tj|f|tj|jd�}|dk	s¬t‚|dk	s¸t‚tj	j
 | ||||¡\}}||fS )	aš  Align a CTC label sequence to an emission.

    .. devices:: CPU CUDA

    .. properties:: TorchScript

    Args:
        log_probs (Tensor): log probability of CTC emission output.
            Tensor of shape `(B, T, C)`. where `B` is the batch size, `T` is the input length,
            `C` is the number of characters in alphabet including blank.
        targets (Tensor): Target sequence. Tensor of shape `(B, L)`,
            where `L` is the target length.
        input_lengths (Tensor or None, optional):
            Lengths of the inputs (max value must each be <= `T`). 1-D Tensor of shape `(B,)`.
        target_lengths (Tensor or None, optional):
            Lengths of the targets. 1-D Tensor of shape `(B,)`.
        blank_id (int, optional): The index of blank symbol in CTC emission. (Default: 0)

    Returns:
        Tuple(Tensor, Tensor):
            Tensor: Label for each time step in the alignment path computed using forced alignment.

            Tensor: Log probability scores of the labels for each time step.

    Note:
        The sequence length of `log_probs` must satisfy:


        .. math::
            L_{\text{log\_probs}} \ge L_{\text{label}} + N_{\text{repeat}}

        where :math:`N_{\text{repeat}}` is the number of consecutively repeated tokens.
        For example, in str `"aabbc"`, the number of repeats are `2`.

    Note:
        The current version only supports ``batch_size==1``.
    z4targets Tensor shouldn't contain blank index. Found Ú.éÿÿÿÿz2targets values must be less than the CTC dimensionNr   é   )ZdtypeÚdevice)Ú
ValueErrorÚtorchÚmaxÚshapeÚsizeÚfullZint64r   ÚAssertionErrorÚopsZ
torchaudioÚforced_align)	r   r	   r
   r   r   Z
batch_sizeÚlengthÚpathsÚscores© r   úN/tmp/user/2202/pip-unpacked-wheel-p89lpdx5/torchaudio/functional/_alignment.pyr      s    -r   c                   @   s@   e Zd ZU dZeed< eed< eed< eed< edœdd„Zd	S )
Ú	TokenSpanz[TokenSpan()
    Token with time stamps and score. Returned by :py:func:`merge_tokens`.
    ÚtokenÚstartÚendÚscore)r   c                 C   s   | j | j S )zReturns the time span)r#   r"   )Úselfr   r   r   Ú__len__[   s    zTokenSpan.__len__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚintÚ__annotations__Úfloatr&   r   r   r   r   r    L   s   
r    )Útokensr   r   r   c                    s°   ˆj dksˆj dkrtdƒ‚tˆƒtˆƒkr4tdƒ‚tjˆtjdgˆjd�tjdgˆjd�d�}t |dk¡ ¡  	¡ }ˆ 	¡ ‰‡ ‡‡‡fdd	„t
|d
d… |dd
… ƒD ƒ}|S )a£  Removes repeated tokens and blank tokens from the given CTC token sequence.

    Args:
        tokens (Tensor): Alignment tokens (unbatched) returned from :py:func:`forced_align`.
            Shape: `(time, )`.
        scores (Tensor): Alignment scores (unbatched) returned from :py:func:`forced_align`.
            Shape: `(time, )`. When computing the token-size score, the given score is averaged
            across the corresponding time span.

    Returns:
        list of TokenSpan

    Example:
        >>> aligned_tokens, scores = forced_align(emission, targets, input_lengths, target_lengths)
        >>> token_spans = merge_tokens(aligned_tokens[0], scores[0])
    r   z(`tokens` and `scores` must be 1D Tensor.z.`tokens` and `scores` must be the same length.r   )r   )ÚprependÚappendr   c              	      s@   g | ]8\}}ˆ|  ‰ˆ krt ˆ||ˆ||…  ¡  ¡ d �‘qS ))r!   r"   r#   r$   )r    ZmeanÚitem)Ú.0r"   r#   ©r   r   r!   r.   r   r   Ú
<listcomp>{   s   þz merge_tokens.<locals>.<listcomp>N)Úndimr   Úlenr   ÚdiffZtensorr   ZnonzeroZsqueezeÚtolistÚzip)r.   r   r   r7   Zchanges_wo_blankZspansr   r3   r   Úmerge_tokens`   s      ÿþr:   )NNr   )r   )Zdataclassesr   Útypingr   r   r   r   r   Ztorchaudio._extensionr   Ú__all__r+   r   r    r:   r   r   r   r   Ú<module>   s(      û
ú@