o
    i                     @   sB   d dl Z 	 	dde jde jde jde jdeded	e jfd
dZdS )    N	log_probstargetsinput_lengthstarget_lengthsblank	ignore_idreturnc                 C   s  ||||k< |   \}}}tj||jd}	tjtjt|||fddjddt|ddddf |fdd}
tjtjddgg|jd	|d|
ddddf |
dddd	f kfdd}tj
td
| j| jd}d}||
 d }tj||f|| j| jd}| ddd|f |dd|d f< | |	d|
dddf f |dd|d f< tj|||f| j|jd}td|D ]P}t|ddddf |ddddf t||dddd	f |f}|jdd\}}| dd|f d|
| |dd|df< ||dd||df< q|dtj||d  d ||d  fdd}tj||f|jtjd}||d  d |jdd ||	|d f< t|d ddD ]"}|dd|f }||	||f }|dd|d f  || 7  < qK|
jd|| jddd}|S )a-  Align a CTC label sequence to an emission.
    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):
            Lengths of the inputs (max value must each be <= `T`). 1-D Tensor of shape `(B,)`.
        target_lengths (Tensor):
            Lengths of the targets. 1-D Tensor of shape `(B,)`.
        blank_id (int, optional): The index of blank symbol in CTC emission. (Default: 0)
        ignore_id (int, optional): The index of ignore symbol in CTC emission. (Default: -1)
    )devicer   )dim   )	start_dimNF   z-inf)r
   dtyper   )min)r   index)sizetorcharanger
   catstack	full_likeflatten	as_tensorexpandtensorfloatr   fullzerosrangewheremaxgatherlongargmaxclamp)r   r   r   r   r   r   
batch_sizeinput_time_size_bsz_indices_t_a_r_g_e_t_s_diff_labelsneg_infpadding_numpadded_t
best_scorebackpointerstprevprev_max_valueprev_max_idxl1l2pathtarget_indices
alignments r:   a/home/ubuntu/.local/lib/python3.10/site-packages/funasr/models/sense_voice/utils/ctc_alignment.pyctc_forced_align   sP    &"*B,&($r<   )r   r   )r   Tensorintr<   r:   r:   r:   r;   <module>   s$    