o
    }oib%                     @   sv  d dl Z d dlZd dlmZ d dlmZmZmZ ddlm	Z	m
Z
mZ ddlmZmZmZmZmZ ddlmZmZ G dd	 d	Ze jd
ddge jdddge jdejejfejejfejejfejejfejejfgG dd deZe jd
ddge jdddge jdddge jde
je
jfejejfe	je	jfgG dd deZG dd dZdS )    N)TopKClassificationAccuracy)DatasetPunctuationErrorRateOccurancePunctuationErrorRatepunctuation_error_rate   )ALL_NUM_MEASUREMENTS_ARE_ZERONO_ZERO_NUM_MEASUREMENTSSOME_NUM_MEASUREMENTS_ARE_ZERO)NO_PROBS_NO_LOGITSONLY_LOGITS1ONLY_LOGITS100
ONLY_PROBSPROBS_AND_LOGITS)
LossTesterPerplexityTesterc                   @   sh   e Zd Zeg dg dg dgZejjdd Z	ejjdd Z
ejjdd	 Zejjd
d ZdS )TestCommonMetrics)皙?333333?皙?        )g?333333?r   r   )r   r   g?r   c                 C   sx   t jg dt jd}td d}|| j|d}|jjt dgks"J |jjt dgks.J t	|d d dk s:J d S )	Nr   r      dtypetop_klogitslabelsr   r   MbX?MbP?
torchtensorlongr   top_k_logitscorrect_counts_kshapeSizetotal_counts_kabs)selfr   accuracyacc r/   Y/home/ubuntu/.local/lib/python3.10/site-packages/tests/collections/common/test_metrics.pytest_top_1_accuracy#   s   
z%TestCommonMetrics.test_top_1_accuracyc                 C   s   t jg dt jd}tddgd}|| j|d\}}|jjt dgks&J |jjt dgks2J t	|d dk s<J t	|d	 dk sFJ d S )
N)r   r   r   r   r   r   r   r   r   r!   Zd;O?r"   )r,   r   r-   top1_acctop2_accr/   r/   r0   test_top_1_2_accuracy.   s   z'TestCommonMetrics.test_top_1_2_accuracyc                 C   sJ  t jg dg dgt jd}td d}|| j|d d}|j|j}}|  |t j| jdgd|d d}|j|j}}t 	||g}	t 	||g}
|	j
t d	dgksWJ |
j
t d	dgkscJ t|d d
 dk soJ t|d d dk s{J |  t |	 g|_t |
 g|_| }|d }t|d dk sJ d S )Nr   )r   r   r   r   r   r   r   r   dimsr   r    r!   r2         ?)r#   r$   r%   r   r&   r'   r*   resetflipstackr(   r)   r+   sumcompute)r,   r   r-   	proc1_acccorrect1total1	proc2_acccorrect2total2correcttotalacc_topkacc_top1r/   r/   r0   test_top_1_accuracy_distributed;   s&   
z1TestCommonMetrics.test_top_1_accuracy_distributedc                 C   s:  t d d}|| jtg dd}|j|j}}|tj| jdgdd dd d f tddgd}|j|j}}t||g}t||g}	|jt	ddgksSJ |	jt	ddgks_J t
|d d d	k skJ t
|d d
 d	k swJ t| g|_t|	 g|_| }
|
d }t
|d d	k sJ d S )Nr   r   r   r   r6   r   r   r    r!   r8   r   )r   r&   r#   r$   r'   r*   r:   r;   r(   r)   r+   r<   r=   )r,   r-   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   r/   r/   r0   ,test_top_1_accuracy_distributed_uneven_batchY   s&   
 z>TestCommonMetrics.test_top_1_accuracy_distributed_uneven_batchN)__name__
__module____qualname__r#   r$   r&   pytestmarkunitr1   r5   rH   rI   r/   r/   r/   r0   r      s    



r   ddpTFdist_sync_on_stepzprobs, logitsc                   @   s   e Zd Zejjdd ZdS )TestPerplexityc                 C   s   | j ||||d d S )N)rP   probsr   rQ   )run_class_perplexity_test)r,   rP   rQ   rS   r   r/   r/   r0   test_perplexity   s   
zTestPerplexity.test_perplexityN)rJ   rK   rL   rM   rN   pleasefixmerU   r/   r/   r/   r0   rR   x   s    rR   take_avg_lossz!loss_sum_or_avg, num_measurementsc                   @   s   e Zd Zdd ZdS )TestLossc                 C   s   | j |||||d d S )N)rP   loss_sum_or_avgnum_measurementsrQ   rW   )run_class_loss_test)r,   rP   rQ   rY   rZ   rW   r/   r/   r0   	test_loss   s   
zTestLoss.test_lossN)rJ   rK   rL   r\   r/   r/   r/   r0   rX      s    rX   c                   @   s8  e Zd ZdZdZg dZdddddddddddddddddddddZdddddddddddddddddddddZdZdZ	dZ
dZdZd	d	d	d
dd	d
d	d	dd
d	d	d	dd	d	d
d	ddZd	d	d
d	dd	d	d	d	dd	d	d	d	dd	d	d	d	ddZejjdd Zejjdd Zejjdd ZdS )TestPunctuationErrorRatez!Hi, dear! Nice to see you. What'sz!Hi dear! Nice to see you! What's?).,!?r   r   )Correct	Deletions
InsertionsSubstitutionsg      ?g      ?r   g      ?c                 C   s$   t | jg| jg| j| jksJ d S N)r   	reference
hypothesispunctuation_markspunct_er)r,   r/   r/   r0   test_punctuation_error_rate   s   $z4TestPunctuationErrorRate.test_punctuation_error_ratec                 C   s   t | j}|| j| j\}}}|| jksJ || jksJ |j| jks&J |j| jks.J |j	| j	ks6J |j
| j
ks>J |j| jksFJ |j| jksNJ |j| jksVJ d S rf   )r   ri   r=   rg   rh   operation_amountssubstitution_amountscorrect_ratedeletions_rateinsertions_ratesubstitutions_raterj   operation_ratessubstitution_rates)r,   oper_objrl   rm   punctuation_ratesr/   r/   r0   "test_OccurancePunctuationErrorRate   s   
z;TestPunctuationErrorRate.test_OccurancePunctuationErrorRatec                 C   s   t | jg| jg| j}|  |j| jksJ |j| jksJ |j| jks'J |j| jks/J |j	| j	ks7J |j
| j
ks?J |j| jksGJ d S rf   )r   rg   rh   ri   r=   rn   ro   rp   rq   rj   rr   rs   )r,   dper_objr/   r/   r0    test_DatasetPunctuationErrorRate   s   z9TestPunctuationErrorRate.test_DatasetPunctuationErrorRateN)rJ   rK   rL   rg   rh   ri   rl   rm   rn   ro   rp   rq   rj   rr   rs   rM   rN   rO   rk   rv   rx   r/   r/   r/   r0   r]      sF    

r]   )rM   r#   7nemo.collections.common.metrics.classification_accuracyr   (nemo.collections.common.metrics.punct_err   r   r   loss_inputsr   r   r	   perplexity_inputsr
   r   r   r   r   pl_utilsr   r   r   rN   parametrizerS   r   rR   rY   rZ   rX   r]   r/   r/   r/   r0   <module>   s@   Z








