o
    }oi@                     @   s  d dl Z d dlmZ d dlZd dlZd dlZd dlmZ d dl	m
Z d dlmZmZ er6d dlZd dlmZ er>d dlmZ dZd	Zd
gZerTej rTejrTed ejje ddG dd dZejje ddejjej  ddG dd dZdS )    N)List)rnnt_logprobs_torch)RNNTLoss)K2_AVAILABLETRITON_AVAILABLE)GraphRnntLoss)rnnt_logprobs_tritonư>-C6?cpucudaz/k2 is not installed, skipping Graph-RNNT tests.)reasonc                   @   s  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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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dd ZejjejdeejdddgdefddZejjejdeejdddgdefddZejjejdeejdddgdejdefddZdS )TestGraphRnntdeviceblank_firstTF
num_frames)         
vocab_sizer   c              	   C   s:  |rdn|d }t |d}|j||t|d}g }t|D ]$}	t|D ]}
|
|kr7||	|	d |
|	dg q$||	|	|
|	dg q$q|||d dddg ||d g t|}ddd |D }tj	j
|dd	}|j|jksuJ |j|jks}J tj||d
ddsJ dtj| | d
ddsJ dd S )Nr   r   blank)r   r   r   
c                 S      g | ]
}d  tt|qS  joinmapstr.0line r$   b/home/ubuntu/.local/lib/python3.10/site-packages/tests/collections/asr/k2/test_graph_transducer.py
<listcomp>F       z6TestGraphRnnt.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   k2Fsafrom_strnum_arcsshapeis_rand_equivalentinvert)selfr   r   r   r   blank_idlosstemporal_schemaetalon_schema_fsttime_ilabel_ietalon_schema_fst_stretalon_temporal_schemar$   r$   r%   test_temporal_schema.   s@   
z"TestGraphRnnt.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 |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sJ 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&   l   r'   z2TestGraphRnnt.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/   lenr0   r   r1   r2   r3   r4   r5   r6   r7   rH   rG   )r8   r   r   r   r9   labelsr:   unit_schemar<   r>   labelr?   etalon_unit_schemar$   r$   r%   test_unit_schemaU   sP   

""
zTestGraphRnnt.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 rx|d |d  | }|
|||||dg q>q6||d  d }|
||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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   rB   rD      r   rE   )rF   r   r   rC   r   r   c                 S   r   r   r   r!   r$   r$   r%   r&      r'   z2TestGraphRnnt.test_grid_schema.<locals>.<listcomp>rG   rH   rI   TFr)   z!Grid schema input labels mismatchz"Grid schema output labels mismatchz#Grid schema unit positions mismatch)rN   r   get_gridr-   rL   r   r.   r/   r0   r   r1   r2   r3   r4   r5   r6   r7   rH   rG   )r8   r   r   r   r9   rO   text_lengthr   r:   grid_schemar<   frame_ir>   statenext_state_labelnext_state_framelast_grid_stater?   etalon_grid_schemar$   r$   r%   test_grid_schema   sn   



zTestGraphRnnt.test_grid_schemaconnect_composedc           
      C   s~   |r|  }n| }t|j|dd}|||j|j|\}}	tj||j	
 tds.J dtj|	|j
 dds=J dd S )NFr   r_   use_grid_implementationrtolcosts mismatch.r	   atolgradient mismatch.)get_sample_smallget_sample_small_blank_lastr   r9   wrap_and_calllogitstargetsnpallcloseexpected_costnumpyEPS_SM_INPUTexpected_grads)
r8   r   r_   r   rnnt_test_helperrnn_loss_sample_datasample_data
graph_rnnt
graph_costgraph_gradsr$   r$   r%   test_small_compose_transducer   s   
"z+TestGraphRnnt.test_small_compose_transducerc                 C   sl   |  }tddd}|||j|j|\}}tj||j t	ds%J dtj||j
 dds4J dd S )	Nr   T)r   ra   rb   rd   r	   re   rg   )rh   r   rj   rk   rl   rm   rn   ro   rp   rq   rr   )r8   r   rs   rt   ru   rv   rw   rx   r$   r$   r%   test_small_grid_transducer   s   "z(TestGraphRnnt.test_small_grid_transducer
use_tritonc           	      C   s   |r|dkrt d | }tdd|d}|||j|j|\}}tj||j	
 tds1J dtj||j
 dd	s@J d
d S )Nr   Triton does not support CPU yetr   Tr   ra   r{   rb   rd   r	   re   rg   )pytestskipget_sample_mediumr   rj   rk   rl   rm   rn   ro   rp   rq   rr   )	r8   r   r{   rs   rt   ru   rv   rw   rx   r$   r$   r%   test_medium_grid_transducer   s   
"z)TestGraphRnnt.test_medium_grid_transducerc                 C   s   |r|dkrt d |jdd}tdd|d}|j||j |j||j|j	d\}}t
dd}	|j|	|j |j||j|j	d\}
}tj| |
td	sQJ d
tj||dds]J dd S )Nr   r|   T)r   r   r}   )input_lengthstarget_lengthsr   rb   rd   r
   re   rg   )r~   r   !get_sample_medium_random_var_sizer   rj   rk   detachrl   r   r   RNNTLoss_Numpyrm   rn   sumrq   )r8   r   r{   rs   rt   ru   rv   rw   rx   etalon_rnntetalon_costetalon_gradsr$   r$   r%   test_medium_random_var_size   s.   



z)TestGraphRnnt.test_medium_random_var_sizec           
      C   sr   |j ||d}t|jddd}|jd }|jjd }||||j}||||j}	t	j
||	ddds7J dd S )	NrE   TFr`   r   r   r)   z,Grid and composed graphs are not equivalent.)get_sample_small_randomr   r9   rl   rk   r5   rU   r   get_composed_latticer1   r6   )
r8   r   r   rt   ru   	criteriontext_tensorr   
graph_gridgraph_composedr$   r$   r%   )test_small_random_grid_compose_equivalent  s   
z7TestGraphRnnt.test_small_random_grid_compose_equivalentN)__name__
__module____qualname__r~   markunitparametrizeDEVICESrA   rS   r^   ry   rz   boolr   r   r-   r   r   r$   r$   r$   r%   r   ,   sF    "*:	r   z6Triton is not installed, skipping RNNT Log Probs testszCUDA is unavailablec                   @   sp   e Zd Zejdg dejdejgej	 r!ej
 r!ejgng  dededededejf
dd	Zd
S )TestRnntLogProbsz/batch_size,num_frames,num_text_units,vocab_size))r      rC   r   )rC   r   rC   rT   )rC            )r      A   i   float_dtype
batch_sizer   num_text_unitsr   c                    sV  t d}t d t j fddt|D |t jd}t j|| d d g||dd}t|t j	|d	\}	}
|
  }|d t||d	\}}d
|dddf< d
|	dddf< t j||
ddsiJ t j||	ddssJ t j|dd}t j|dd}||	 ||
   }|| ||   }|  |  t j|j|jddsJ dS )zo
        Test Triton-based implementation using etalon Torch-based implementation for RNN-T log-probs.
        r   i	  c                    s"   g | ]}fd dt  D qS )c                    s   g | ]
}t d  d qS )r   r   )random	randrange)r"   i)r   r$   r%   r&   1  r'   zITestRnntLogProbs.test_rnnt_logprobs_random.<locals>.<listcomp>.<listcomp>)r.   )r"   jr   r   r$   r%   r&   1  s   " z>TestRnntLogProbs.test_rnnt_logprobs_random.<locals>.<listcomp>)r   dtyper   T)r   r   requires_grad)rk   rl   r9   g        .r   Ngh㈵>re   F)r   )r-   r   manual_seedrL   r.   longrandr   tofloat32cloner   requires_grad_r   rn   	rand_liker   backwardgrad)r8   r   r   r   r   r   r   rl   rk   target_scores_etalonblank_scores_etalonlogits2target_scoresblank_scorestarget_scalesblank_scalesloss_etalonr:   r$   r   r%   test_rnnt_logprobs_random  s<   



z*TestRnntLogProbs.test_rnnt_logprobs_randomN)r   r   r   r~   r   r   r-   r   r   is_availableis_bf16_supportedbfloat16intr   r   r$   r$   r$   r%   r     s(    	&r   )r   typingr   rp   rm   r~   r-   +nemo.collections.asr.parts.k2.rnnt_logprobsr   5nemo.collections.asr.parts.numba.rnnt_loss.rnnt_numpyr   r   nemo.core.utils.optional_libsr   r   r1   .nemo.collections.asr.parts.k2.graph_transducerr   2nemo.collections.asr.parts.k2.rnnt_logprobs_tritonr   rq   EPS_L_INPUTr   r   r   	with_cudar/   r   skipifr   r   r$   r$   r$   r%   <module>   s0   
 k