o
    }oi1                  	   @   s   d dl mZ d dlZd dlZd dlZzd dlmZ d dlm	Z	 W n e
efy2   ejddd Y nw dgZej rCe	jrCed	 G d
d dZdS )    )ListN)GraphWTransducerLoss)k2z7k2 is not installed, skipping Graph-W-Transducer tests.T)allow_module_levelcpucudac                
   @   sr  e Zd Zejjejdeejdddgejdg dejddgejd	d
dgdd Zejjejdeejdddgdd Z	ejjejdeejdddgejd	d
dgdd Z
ejjejdeejdddgejd	dd
gdejdefddZejjejdeejd	dd
gejdddgdd ZdS )TestGraphWTransducerLossdeviceblank_firstTF
num_frames)         
vocab_sizer   last_blank_modeforce_finalallow_ignorec              	   C   s  |rdn|d }t ||d}|j||t|d}g }	t|D ]$}
t|D ]}||kr8|	|
|
d ||
dg q%|	|
|
||
dg q%q|}td|D ]}
|	d|
|ddg qK|d }|dkrd|d n|}td|d D ]}
|	|
|||
dg qm|	||d dddg |	|d g t|	}	ddd	 |	D }tj	j
|dd
}|j|jksJ |j|jksJ tj||dddsJ dtj| | dddsJ dd S )Nr   r   blankr   )r   r   r	   r   
c                 S      g | ]
}d  tt|qS  joinmapstr.0line r!   ^/home/ubuntu/.local/lib/python3.10/site-packages/tests/collections/asr/k2/test_w_transducer.py
<listcomp>J       zATestGraphWTransducerLoss.test_temporal_schema.<locals>.<listcomp>)num_aux_labelsTFlog_semiringtreat_epsilons_speciallyzTemporal schema mismatchz&Temporal schema output labels mismatch)r   get_temporal_schematorchr	   rangeappendsortedr   r   Fsafrom_strnum_arcsshapeis_rand_equivalentinvert)selfr	   r
   r   r   r   blank_idlosstemporal_schemaetalon_schema_fsttime_ilabel_ieps_from_first_stateeps_to_last_statelast_state_epsetalon_schema_fst_stretalon_temporal_schemar!   r!   r"   test_temporal_schema"   sN   	z-TestGraphWTransducerLoss.test_temporal_schemac              	   C   s  d}|rdn|d }|rg d}ng d}t |d}|jtj|t|d|d}g }t|D ]\}	}
||	|	d |
|
|	dg ||	|	|||	dg q/|t|t|||t|dg |dd||ddg |t|t||d |d t|dg |t|t|d d	d	d	dg |t|d g t|}d
	dd |D }t
jj|ddgd}|j|jksJ |j|jksJ t
j||dddsJ dt
j| | dddsJ d|j|j|_|_|j|j|_|_t
j| | dddsJ dd S )Nr   r   r   r   r      r   r   r   r   r   )r   r	   )units_tensorr   r   r   c                 S   r   r   r   r   r!   r!   r"   r#   t   r$   z=TestGraphWTransducerLoss.test_unit_schema.<locals>.<listcomp>
aux_labelsunit_positionsaux_label_namesTFr&   z!Unit schema input labels mismatchz"Unit schema output labels mismatchz#Unit schema unit positions mismatch)r   get_unit_schemar*   tensorr	   	enumerater,   lenr-   r   r   r.   r/   r0   r1   r2   r3   rG   rF   )r4   r	   r
   r   r5   labelsr6   unit_schemar8   r:   labelr>   etalon_unit_schemar!   r!   r"   test_unit_schemaY   sT   

"*"

z)TestGraphWTransducerLoss.test_unit_schemac              
   C   s  d}|rdn|d }|rg d}ng d}t |}d}t||d}	|	jtj|t|d||d	}
g }t|D ]C}t|d D ]:}||d  | }||k r^|d }||||| ||dg ||d k ry|d |d  | }||||||dg q?q7td|D ]}|d||d  |dddg q||d  d }|d
kr|}n
|dksJ |d }t|d D ]}||d |d  d ||d ||dg q|||d ||d |dg ||d |d ddddg ||d g t|}d	dd |D }t
jj|ddgd}|
j|jksJ |
j|jksJ t
j|
|ddds'J dt
j|
 | ddds9J d|
j|
j|
_|
_|j|j|_|_t
j|
 | ddds]J dd S )Nr   r   r   rA   rC      r   rD   )rE   r   r   r   r   rB   r   r   c                 S   r   r   r   r   r!   r!   r"   r#      r$   z=TestGraphWTransducerLoss.test_grid_schema.<locals>.<listcomp>rF   rG   rH   TFr&   z!Grid schema input labels mismatchz"Grid schema output labels mismatchz#Grid schema unit positions mismatch)rM   r   get_gridr*   rK   r	   r+   r,   r-   r   r   r.   r/   r0   r1   r2   r3   rG   rF   )r4   r	   r
   r   r   r5   rN   text_lengthr   r6   grid_schemar8   frame_ir:   statenext_state_labelnext_state_framelast_grid_statelast_eps_stater>   etalon_grid_schemar!   r!   r"   test_grid_schema   s   
 "



z)TestGraphWTransducerLoss.test_grid_schemac                 C   st   |j ||d}t|j|ddd}|jd }|jjd }||||j}	||||j}
t	j
|	|
ddds8J dd S )	NrD   TF)r   r   connect_composeduse_grid_implementationr   r   r&   z,Grid and composed graphs are not equivalent.)get_sample_small_randomr   r5   targetslogitsr1   rT   r   get_composed_latticer   r2   )r4   r	   r
   r   rnn_loss_sample_datasample_data	criteriontext_tensorr   
graph_gridgraph_composedr!   r!   r"   )test_small_random_grid_compose_equivalent   s    
zBTestGraphWTransducerLoss.test_small_random_grid_compose_equivalentr`   c           
      C   sp   |  }tdd||d}|||j|j|\}}	tj||j dds'J dtj|	|j	 dds6J dd	S )
z]
        With -inf eps penalty W-Transducer loss should be equivalent to RNN-T loss.
        r   g      Y)r   
eps_weightr   r`   gư>)rtolzcosts mismatch.)atolzgradient mismatch.N)
get_sample_smallr   wrap_and_callrc   rb   npallcloseexpected_costnumpyexpected_grads)
r4   r	   r   r`   rnnt_test_helperre   rf   
graph_rnnt
graph_costgraph_gradsr!   r!   r"   &test_small_grid_transducer_inf_penalty   s   
"z?TestGraphWTransducerLoss.test_small_grid_transducer_inf_penaltyN)__name__
__module____qualname__pytestmarkunitparametrizeDEVICESr@   rR   r^   r*   r	   boolrk   rz   r!   r!   r!   r"   r   !   s>    1.Kr   )typingr   rt   rq   r~   r*   *nemo.collections.asr.parts.k2.w_transducerr   nemo.core.utils.k2_guardr   ImportErrorModuleNotFoundErrorskipr   r   is_available	with_cudar,   r   r!   r!   r!   r"   <module>   s   
