o
    }oil                     @   s*  d dl mZ d dlmZ d dlZd dlZd dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ g Zej r>ed	 ejd
ddd Z				ddedeeef dededee f
ddZejdddd ZejdeejdddgejdddgejjdededefddZdS )    )Path)UnionN)TranscriptionConfig)	OmegaConf)EncDecRNNTBPEModel)
Hypothesis)prepare_audio_datacudamodule)scopec                  C   s   t jddd} | S )N!stt_en_conformer_transducer_smallcpu)
model_namemap_location)r   from_pretrained)model r   g/home/ubuntu/.local/lib/python3.10/site-packages/tests/collections/asr/decoding/test_rnnt_alignments.py'stt_en_conformer_transducer_small_model"   s   r   TFstrategymanifest_pathr   loop_labelsreturnc                 C   s2  t t }d|jj_d|j_| |j_|jjdkr#||jj_	||jj_
t||_t|d d d }||}||j |j||j|jd|jd}|D ]J}	t|	j|	jD ]@\}
}t|
t|kscJ t|
dkskJ t|
D ]%\}}|t|
d k r|d  |jjksJ qo|d  |jjksJ qoqUqL|S )NTgreedy_batchr      )audio
batch_sizenum_workersreturn_hypotheseschannel_selector   )r   
structuredr   rnnt_decodingconfidence_cfgpreserve_frame_confidencepreserve_alignmentsr   greedyr   use_cuda_graph_decoderstrdataset_manifestr   tochange_decoding_strategy
transcriber   r   r   zip
alignmentsframe_confidencelen	enumerateitemdecoder	blank_idx)r   r   r   r   r'   devicecfg	filepathstranscriptionstranscription
align_elemr/   idxpredr   r   r   get_rnnt_alignments(   s:   




r=   )autousec                   C   s   dS )zOverriding global fixture to make sure it's not applied for this test.

    Otherwise, there will be errors in the CI in github.
    Nr   r   r   r   r   cleanup_local_folderR   s   r?   r5   r'   c                 C   s   |r|dkrt d | s|rt d td|||d}td| ||||d}t|t|ks0J t||D ]2\}}t|j|jD ]&\}	}
t|	t|
ksNJ t|	|
D ]\}}|d  |d  kseJ qSq@q5d S )	Nr	   z!CUDA decoder works only with CUDAzHFrame-Looping algorithm with CUDA graphs does not yet support alignmentsr&   )r   r   r5   r   )r   r'   r   r   r5   r    )pytestskipr=   r0   r-   r.   r2   )r   r'   r5   an4_val_manifest_correctedr   ref_transcriptionsr8   ref_transcriptionr9   ref_align_elemr:   ref_predr<   r   r   r   test_rnnt_alignments\   s6   

rG   )TFr	   ) pathlibr   typingr   r@   
torch.cudatorchexamples.asr.transcribe_speechr   	omegaconfr   nemo.collections.asr.modelsr   +nemo.collections.asr.parts.utils.rnnt_utilsr   1nemo.collections.asr.parts.utils.transcribe_utilsr   DEVICESr	   is_availableappendfixturer   r(   boollistr=   r?   markparametrizewith_downloadsrG   r   r   r   r   <module>   sR   



	


*
	