o
    soiL                     @  sT   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dZ	G dd	 d	eZ
dS )    )annotationsN)Base)expectmeanc                 C  s  | j }t|dkrtd|j }|d }|d }d }	|d ur?tj|tj|tjddd}	|d ur>t||kd|	jtj	d}	n|d urQt||kddjtj	d}	t|dkrf| 
||df} |
|df}| j d	 }
tj||
ftj	d}t|D ]"}t|
D ]}|| | |kr| | || |  |  || |< qqy|}t|dkr|
|}|	d ur|	| }|d
kr| |	  S |d
krt|}|S |dkrt|}|S )N   zUnsupported shaper   )dtypeclip)mode      r   sum)shapelenRuntimeErrornptakearrayint32whereastypefloat32reshapezerosranger   r   )inputtargetweight	reductionignore_indexinput_shapetarget_shapeNCgather_weightDneg_gather_element_inputidloss r*   i/home/ubuntu/.local/lib/python3.10/site-packages/onnx/backend/test/case/node/negativeloglikelihoodloss.py$compute_negative_log_likelihood_loss   sP   
"


r,   c                   @  s  e Zd Zed(ddZed(ddZed(ddZed(d	d
Zed(ddZed(ddZ	ed(ddZ
ed(ddZed(ddZed(ddZed(ddZed(ddZed(ddZed(ddZed(dd Zed(d!d"Zed(d#d$Zed(d%d&Zd'S ))NegativeLogLikelihoodLossreturnNonec                  C  s   d} t jjdddgdg| d}d\}}tjd tj||tj}tjj	d||fd	tj
}t||d | d
}t|||g|gdd d S )Nnoner-   r   r   r)   inputsoutputsr   r
      r   highsizer   r   test_nllloss_NCr2   r3   nameonnxhelper	make_noder   randomseedrandr   r   randintint64r,   r   )r   noder"   r#   r   r   negative_log_likelihood_lossr*   r*   r+   export_input_shape_is_NCN   s(   
z2NegativeLogLikelihoodLoss.export_input_shape_is_NCc            	      C     d} t jjdddgdg| d}d\}}}}tjd tj||||tj}tjj	d||||fd	tj
}t||d | d
}t|||g|gdd d S )Nr0   r-   r   r   r)   r1   r
   r5      rK   r   r6   r9   test_nllloss_NCd1d2r;   r=   	r   rF   r"   r#   dim1dim2r   r   rG   r*   r*   r+   export_input_shape_is_NCd1d2h   (    
z6NegativeLogLikelihoodLoss.export_input_shape_is_NCd1d2c            	      C  rI   )Nr   r-   r   r   r)   r1   rJ   r   r6   r9   "test_nllloss_NCd1d2_reduction_meanr;   r=   rM   r*   r*   r+   +export_input_shape_is_NCd1d2_reduction_mean   rQ   zENegativeLogLikelihoodLoss.export_input_shape_is_NCd1d2_reduction_meanc            	      C  rI   )Nr   r-   r   r   r)   r1   rJ   r   r6   r9   !test_nllloss_NCd1d2_reduction_sumr;   r=   rM   r*   r*   r+   *export_input_shape_is_NCd1d2_reduction_sum   rQ   zDNegativeLogLikelihoodLoss.export_input_shape_is_NCd1d2_reduction_sumc            
      C     d} t jjdg ddg| d}d\}}}}tjd tj||||tj}tjj	d||||fdtj
}tj|tj}t|||| d	}	t||||g|	gd
d d S )Nr0   r-   r   r   r   r)   r1   rJ   r   r6   r9   test_nllloss_NCd1d2_with_weightr;   r=   
r   rF   r"   r#   rN   rO   r   r   r   rG   r*   r*   r+   (export_input_shape_is_NCd1d2_with_weight   *    
zBNegativeLogLikelihoodLoss.export_input_shape_is_NCd1d2_with_weightc            
      C  rV   )Nr   r-   rW   r)   r1   rJ   r   r6   r9   .test_nllloss_NCd1d2_with_weight_reduction_meanr;   r=   rY   r*   r*   r+   7export_input_shape_is_NCd1d2_with_weight_reduction_mean   r[   zQNegativeLogLikelihoodLoss.export_input_shape_is_NCd1d2_with_weight_reduction_meanc            
      C  rV   )Nr   r-   rW   r)   r1   rJ   r   r6   r9   -test_nllloss_NCd1d2_with_weight_reduction_sumr;   r=   rY   r*   r*   r+   6export_input_shape_is_NCd1d2_with_weight_reduction_sum   r[   zPNegativeLogLikelihoodLoss.export_input_shape_is_NCd1d2_with_weight_reduction_sumc                  C  s   d} t d}tjjdg ddg| |d}d\}}}}t jd t j||||t j	}t jj
d||||fdt j}t d|d d d< t j|t j	}	t|||	| |d	}
t||||	g|
gd
d d S )Nr   r   r-   rW   r)   r2   r3   r   r   rJ   r6   r   r   r   0test_nllloss_NCd1d2_with_weight_reduction_sum_iir;   r   rE   r>   r?   r@   rA   rB   rC   r   r   rD   r,   r   )r   r   rF   r"   r#   rN   rO   r   r   r   rG   r*   r*   r+   9export_input_shape_is_NCd1d2_with_weight_reduction_sum_ii  s0   
 

zSNegativeLogLikelihoodLoss.export_input_shape_is_NCd1d2_with_weight_reduction_sum_iic            
      C  s   d} t d}tjjdddgdg| |d}d\}}}}t jd	 t j||||t j	}t jj
d	||||fd
t j}t d|d	 d	 d	< t||| |d}	t|||g|	gdd d S )Nr   r   r-   r   r   r)   r`   rJ   r   r6   r   r   /test_nllloss_NCd1d2_no_weight_reduction_mean_iir;   rc   )
r   r   rF   r"   r#   rN   rO   r   r   rG   r*   r*   r+   8export_input_shape_is_NCd1d2_no_weight_reduction_mean_ii%  s.   
 
zRNegativeLogLikelihoodLoss.export_input_shape_is_NCd1d2_no_weight_reduction_mean_iic                  C  s   d} t jjdddgdg| d}d\}}}tjd tj|||tj}tjj	d|||fd	tj
}t||d | d
}t|||g|gdd d S )Nr   r-   r   r   r)   r1   r
   r5   r   r   r6   r9   test_nllloss_NCd1r;   r=   )r   rF   r"   r#   d1r   r   rG   r*   r*   r+   export_input_shape_is_NCd1B  s(   

z4NegativeLogLikelihoodLoss.export_input_shape_is_NCd1c            	      C  s   d} t jjdg ddg| d}d\}}}tjd tj|||tj}tjj	d|||fdtj
}tj|tj}t|||| d	}t||||g|gd
d d S )Nr   r-   rW   r)   r1   rh   r   r6   r9   test_nllloss_NCd1_weightr;   r=   )	r   rF   r"   r#   rj   r   r   r   rG   r*   r*   r+   !export_input_shape_is_NCd1_weight\  s*   

z;NegativeLogLikelihoodLoss.export_input_shape_is_NCd1_weightc            	      C  s   d} t d}tjjdddgdg| |d}d\}}}t jd	 t j|||t j	}t jj
d	|||fd
t j}t d|d	 d	< t||d | |d}t|||g|gdd d S )Nr   r   r-   r   r   r)   r`   rh   r   r6   ra   test_nllloss_NCd1_iir;   rc   )	r   r   rF   r"   r#   rj   r   r   rG   r*   r*   r+   export_input_shape_is_NCd1_iiw  s.   



z7NegativeLogLikelihoodLoss.export_input_shape_is_NCd1_iic            
      C  s   d} t d}tjjdg ddg| |d}d\}}}t jd t j|||t j	}t jj
d|||fd	t j}t d|d d< t j|t j	}t|||| |d
}	t||||g|	gdd d S )Nr   r   r-   rW   r)   r`   rh   r   r6   ra   test_nllloss_NCd1_weight_iir;   rc   )
r   r   rF   r"   r#   rj   r   r   r   rG   r*   r*   r+   $export_input_shape_is_NCd1_weight_ii  s0   



z>NegativeLogLikelihoodLoss.export_input_shape_is_NCd1_weight_iic               	   C  s   d} t jjdg ddg| d}d\}}}}}}}tjd tj|||||||tj}	tjj	d|||||||fdtj
}
tj|tj}t|	|
|| d	}t||	|
|g|gd
d d S )Nr   r-   rW   r)   r1   r
   r5   rK   rK   r5   r
      r   r6   r9   %test_nllloss_NCd1d2d3d4d5_mean_weightr;   r=   )r   rF   r"   r#   rN   rO   dim3dim4dim5r   r   r   rG   r*   r*   r+   .export_input_shape_is_NCd1d2d3d4d5_mean_weight  s2    
zHNegativeLogLikelihoodLoss.export_input_shape_is_NCd1d2d3d4d5_mean_weightc               	   C  s   d} t jjdddgdg| d}d\}}}}}}}tjd tj|||||||tj}	tjj	d|||||||fd	tj
}
t|	|
| d
}t||	|
g|gdd d S )Nr0   r-   r   r   r)   r1   rr   r   r6   )r   (test_nllloss_NCd1d2d3d4d5_none_no_weightr;   r=   )r   rF   r"   r#   rN   rO   ru   rv   rw   r   r   rG   r*   r*   r+   1export_input_shape_is_NCd1d2d3d4d5_none_no_weight  s0    
zKNegativeLogLikelihoodLoss.export_input_shape_is_NCd1d2d3d4d5_none_no_weightc            
      C  s   d} t d}tjjdg ddg| |d}d\}}}t jd t j|||t j	}t jj
d|||fd	t j}d|d d< t j|t j	}t|||| |d
}	t||||g|	gdd d S )Nr   r   r-   rW   r)   r`   )r
   r5   rK   r   r6   ra   )test_nllloss_NCd1_mean_weight_negative_iir;   rc   )
r   r   rF   r"   r#   rN   r   r   r   rG   r*   r*   r+   2export_input_shape_is_NCd1_mean_weight_negative_ii  s0   



zLNegativeLogLikelihoodLoss.export_input_shape_is_NCd1_mean_weight_negative_iic                  C  s   d} t d}tjjdddgdg| |d}d\}}}}}t jd	 t j|||||t j	}t jj
d	|||||fd
t j}	d|	d	 d	 d	 d	< t||	| |d}
t|||	g|
gdd d S )Nr0   r-   r   r   r)   r`   )r
   r5   rK   rK   r5   r   r6   re   0test_nllloss_NCd1d2d3_none_no_weight_negative_iir;   rc   )r   r   rF   r"   r#   rN   rO   ru   r   r   rG   r*   r*   r+   9export_input_shape_is_NCd1d2d3_none_no_weight_negative_ii  s2   

zSNegativeLogLikelihoodLoss.export_input_shape_is_NCd1d2d3_none_no_weight_negative_iic            	      C  s   d} t d}tjjdg ddg| |d}d\}}t jd t j||t j	}t jj
d||d	t j}d|d< t j|t j	}t|||| |d
}t||||g|gdd d S )Nr   
   r-   rW   r)   r`   r4   r   r6   ra   (test_nllloss_NCd1d2d3_sum_weight_high_iir;   rc   )	r   r   rF   r"   r#   r   r   r   rG   r*   r*   r+   1export_input_shape_is_NCd1d2d3_sum_weight_high_ii,  s0   


zKNegativeLogLikelihoodLoss.export_input_shape_is_NCd1d2d3_sum_weight_high_iiN)r.   r/   )__name__
__module____qualname__staticmethodrH   rP   rS   rU   rZ   r]   r_   rd   rg   rk   rm   ro   rq   rx   rz   r|   r   r   r*   r*   r*   r+   r-   M   sJ    r-   )Nr   N)
__future__r   numpyr   r>   onnx.backend.test.case.baser   onnx.backend.test.case.noder   r,   r-   r*   r*   r*   r+   <module>   s   
@