
    bi                         S SK Jr  S SKJrJrJr  S SKrS SKJr  S SKJ	r	  / r
\	   SS\S\S\\   S	\\   S
\S\\\4   4S jj5       r\ " S S5      5       rSS\S\S
\S\\   4S jjrg)    )	dataclass)ListOptionalTupleN)Tensor)fail_if_no_align	log_probstargetsinput_lengthstarget_lengthsblankreturnc                    XA;   a  [        SU S35      e[        R                  " U5      U R                  S   :  a  [        S5      eUcQ  U R	                  S5      U R	                  S5      pe[        R
                  " U4U[        R                  U R                  S9nUcQ  UR	                  S5      UR	                  S5      pe[        R
                  " U4U[        R                  UR                  S9nUc   eUc   e[        R                  R                  R                  XX#U5      u  pxXxSS2[        R                  " UR                  S   5      US   4   4$ )	a2  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      )dtypedevice)
ValueErrortorchmaxshapesizefullint64r   ops
torchaudioforced_alignarange)	r	   r
   r   r   r   
batch_sizelengthpathsscoress	            Z/home/james-whalen/.local/lib/python3.13/site-packages/torchaudio/functional/_alignment.pyr   r      s+   Z OPWyXYZ[[yyY__R00MNN&^^A.	q0AF

J=&T]TdTde$\\!_gll1oFZM6U\UcUcd $$$%%%II((55i-inoMEELLa958CDDD    c                   X    \ rS rSr% Sr\\S'    \\S'    \\S'    \\S'    S\4S jrS	r	g
)	TokenSpanL   zSTokenSpan()
Token with time stamps and score. Returned by :py:func:`merge_tokens`.
tokenstartendscorer   c                 4    U R                   U R                  -
  $ )zReturns the time span)r+   r*   )selfs    r$   __len__TokenSpan.__len__[   s    xx$**$$r%    N)
__name__
__module____qualname____firstlineno____doc__int__annotations__floatr/   __static_attributes__r1   r%   r$   r'   r'   L   s5     JJ;	H9L&% %r%   r'   tokensr#   c                    U R                   S:w  d  UR                   S:w  a  [        S5      e[        U 5      [        U5      :w  a  [        S5      e[        R                  " U [        R
                  " S/U R                  S9[        R
                  " S/U R                  S9S9n[        R                  " US:g  5      R                  5       R                  5       nU R                  5       n [        USS USS 5       VVs/ s H8  u  pVX   =or:w  d  M  [        XuXaXV R                  5       R                  5       S	9PM:     nnnU$ s  snnf )
as  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   N)r)   r*   r+   r,   )ndimr   lenr   difftensorr   nonzerosqueezetolistzipr'   meanitem)	r;   r#   r   rA   changes_wo_blankr*   r+   r)   spanss	            r$   merge_tokensrK   `   s&   " {{a6;;!+CDD
6{c&k!IJJ::bT&--@WYVZcicpcpIqD }}dai199;BBD]]_F .s35Eab5IJJJE]"E, 	\	%;L;Q;Q;S;X;X;Z[J 
 
 Ls   ;D?,D?)NNr   )r   )dataclassesr   typingr   r   r   r   r   torchaudio._extensionr   __all__r7   r   r'   rK   r1   r%   r$   <module>rP      s    ! ( (   2
  '+'+=E=E=E F#=E V$	=E
 =E 66>=E =E@ % % %&       DO  r%   