o
    to™i5  ã                   @  s¢   d dl mZ d dlZd dlZd dlmZ d dlZd dlmZm	Z	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZ G d	d
„ d
ejƒZedkrOejdd dS dS )é    )ÚannotationsN)Úassert_allclose)ÚTensorProtoÚhelperÚnumpy_helper)Úonnx_opset_version)ÚReferenceEvaluator)Úupdate_model_dims)Ú(replace_initializer_by_constant_of_shapec                   @  sN   e Zd Zddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Z	dd„ Z
dS )ÚTestToolsFunctionsÚreturnÚNonec                 C  s4  t jdddgdgddgddgd}t  |gdt  dtjg d	¢¡t  dtjg d
¢¡gt  dtjg d¢¡g¡}t j|dd}t |g d¢g d
¢dœdg d¢i¡}t	j
 |¡ |  |jjd jjjjd jd¡ |  |jjd jjjjd jd¡ |  |jjd jjjjd jd¡ |  |jjd jjjjd jd¡ d S )NÚConvÚxÚWÚyé   é   )ÚinputsÚoutputsÚkernel_shapeÚstridesÚtest)é   r   é   r   )r   r   r   r   )r   r   r   r   )Úproducer_name)r   r   Úx1éÿÿÿÿ)r   r   )r   r   r   r   r   r   Úx_3Úy_2Úy_3)r   Ú	make_nodeÚ
make_graphÚmake_tensor_value_infor   ÚFLOATÚ
make_modelr	   Úupdate_inputs_outputs_dimsÚonnxÚcheckerÚcheck_modelÚassertEqualÚgraphÚinputÚtypeÚtensor_typeÚshapeÚdimÚ	dim_paramÚoutput)ÚselfÚnode_defÚ	graph_defÚ	model_defÚupdated_def© r8   úH/home/ubuntu/.local/lib/python3.10/site-packages/onnx/test/tools_test.pyÚtest_update_inputs_outputs_dim   sJ   ûþù	þÿú
ÿÿÿÿz1TestToolsFunctions.test_update_inputs_outputs_dimc                 C  sh  t j}t j dd¡ |¡}tj|dd}t jdg|d}tj|dd}t 	dt
jd d g¡}t 	d	t
jd g¡}t d
ddgdg¡}t dddgd	g¡}t ||gd|g|g||g¡}	t |	¡}
t  g d¢¡ t j¡ d¡}t|
ƒ}| d d|i¡d }t|
ƒ}dd„ |jjD ƒ}|  d|¡ t|ƒ}d|d d …d d …f< d|dd d …f< | d d|i¡d }t||ƒ d S )Nr   éd   ÚA©Únamer   ©ÚdtypeÚCÚXÚYÚMatMulÚAXÚSubÚlr©r   r   é   r   r   rI   ©r   r   r   c                 S  ó   h | ]}|j ’qS r8   ©Úop_type©Ú.0Únr8   r8   r9   Ú	<setcomp>P   ó    z>TestToolsFunctions.test_replace_initializer.<locals>.<setcomp>ÚConstantOfShapeç      @ç      à?©ÚnpÚfloat32ÚrandomÚrandnÚastyper   Ú
from_arrayÚarrayr   r#   r   r$   r!   r"   r%   Úreshaper   Úrunr
   r+   ÚnodeÚassertInr   )r3   r@   Úvaluer<   rA   rB   rC   Únode1Únode2r+   r6   r   Úoinf1Úy1ÚreplÚ
node_typesÚoinf2Úy2r8   r8   r9   Útest_replace_initializer>   s,   
z+TestToolsFunctions.test_replace_initializerc                 C  s|  t j}t j dd¡ |¡}tj|dd}t jdg|d}tj|dd}t 	dt
jd d g¡}t 	d	t
jd g¡}tjd
g dg|d}t dddgdg¡}t dddgd	g¡}	t |||	gd|g|g|g¡}
t |
¡}t  g d¢¡ t j¡ d¡}t|ƒ}| d d|i¡d }t|ƒ}dd„ |jjD ƒ}|  d|¡ t|ƒ}d|d d …d d …f< d|dd d …f< | d d|i¡d }t||ƒ d S )Nr   r;   r<   r=   r   r?   rA   rB   rC   ÚConstant©rb   rD   rE   rF   rG   rH   rJ   r   c                 S  rK   r8   rL   rN   r8   r8   r9   rQ   k   rR   z;TestToolsFunctions.test_replace_constant.<locals>.<setcomp>rS   rT   rU   rV   ©r3   r@   rb   r<   rA   rB   rC   Únode0rc   rd   r+   r6   r   re   rf   rg   rh   ri   rj   r8   r8   r9   Útest_replace_constantX   s.   
z(TestToolsFunctions.test_replace_constantc                 C  sl  t j}t j dd¡ |¡}tj|dd}t jdg|d}tj|dd}t 	dt
jd d g¡}t 	d	t
jd g¡}tjd
g dg|d}t dddgdg¡}t dddgd	g¡}	t |||	gd|g|g|g¡}
t |
¡}t  g d¢¡ t j¡ d¡}t|ƒ}| d d|i¡d }t|dd}dd„ |jjD ƒ}|  d|¡ |  d|¡ t|ƒ}| d d|i¡d }t|j|jƒ d S )Nr   r;   r<   r=   r   r?   rA   rB   rC   rl   rm   rD   rE   rF   rG   rH   rJ   r   T©Ú	use_rangec                 S  rK   r8   rL   rN   r8   r8   r9   rQ   †   rR   z8TestToolsFunctions.test_replace_range.<locals>.<setcomp>ÚRangerS   )rW   rX   rY   rZ   r[   r   r\   r]   r   r#   r   r$   r!   r"   r%   r^   r   r_   r
   r+   r`   ra   ÚassertNotInr   r/   rn   r8   r8   r9   Útest_replace_ranges   s,   
z%TestToolsFunctions.test_replace_rangec              
   C  sè  t j}t j dd¡ |¡}tj|dd}t jdg|d}tj|dd}t 	dt
jd d g¡}t 	d	t
jd g¡}tjd
g dg|d}tjd
g dg|d}t dddgdg¡}	t dddgd	g¡}
t dtƒ ¡t dd¡g}t dddgd	g|||	|
g|¡}tjddgd	gdd}t |gd|g|g|g¡}tj||g|d}t  g d¢¡ t j¡ d¡}t|ƒ}| d d|i¡d }t|ƒ}dd„ |jd jD ƒ}|  d|¡ t|ƒ}d|d d …d d …f< d|dd d …f< | d d|i¡d }t||ƒ d S )Nr   r;   r<   r=   r   r?   rA   rB   rC   rl   rm   rD   rE   rF   Ú ÚcustomÚunittest©ÚdomainrG   ©Ú	functionsÚopset_importsrH   rJ   r   c                 S  rK   r8   rL   rN   r8   r8   r9   rQ   ±   rR   zDTestToolsFunctions.test_replace_constant_function.<locals>.<setcomp>rS   rT   rU   )rW   rX   rY   rZ   r[   r   r\   r]   r   r#   r   r$   r!   Úmake_opsetidr   Úmake_functionr"   r%   r^   r   r_   r
   r|   r`   ra   r   ©r3   r@   rb   r<   rA   rB   rC   ÚnodeCro   rc   rd   r}   Úfctr`   r+   r6   r   re   rf   rg   rh   ri   rj   r8   r8   r9   Útest_replace_constant_function   sL   
þ
ú	ÿz1TestToolsFunctions.test_replace_constant_functionc              
   C  sØ  t j}t j dd¡ |¡}tj|dd}t jdg|d}tj|dd}t 	dt
jd d g¡}t 	d	t
jd g¡}tjd
g dg|d}tjd
g dg|d}t dddgdg¡}	t dddgd	g¡}
t dtƒ ¡t dd¡g}t dddgd	g|||	|
g|¡}tjddgd	gdd}t |gd|g|g|g¡}tj||g|d}t  g d¢¡ t j¡ d¡}t|ƒ}| d d|i¡d }t|dd}dd„ |jd jD ƒ}|  d|¡ |  d|¡ t|ƒ}| d d|i¡d }t|j|jƒ d S )Nr   r;   r<   r=   r   r?   rA   rB   rC   rl   rm   rD   rE   rF   rv   rw   rx   ry   rG   r{   rH   rJ   r   Trq   c                 S  rK   r8   rL   rN   r8   r8   r9   rQ   Ý   rR   zATestToolsFunctions.test_replace_range_function.<locals>.<setcomp>rs   rS   )rW   rX   rY   rZ   r[   r   r\   r]   r   r#   r   r$   r!   r~   r   r   r"   r%   r^   r   r_   r
   r|   r`   ra   rt   r   r/   r€   r8   r8   r9   Útest_replace_range_function¹   sJ   
þ
ú	ÿz.TestToolsFunctions.test_replace_range_functionc                 C  s  t jdgt jd}tj|dd}t dtjj	d d g¡}t dtjj	d g¡}t 
ddgdg¡}t 
d	ddgd
g¡}t dtjj	d ¡}t t  dgd ¡ t j¡¡}tj
dg dg|dd}	t |	gdg |g¡}
t dtjj	d ¡}t t  dgd ¡ t j¡¡}tj
dg dg|dd}t |gdg |g¡}tjj
dd
gdg|
|d}t |||gd|g|g|g¡}tj|t dtƒ ¡gd}|  dt|ƒ¡ t jdt jd}t|ƒ}| d d|i¡d }t|ƒ}|  dt|ƒ¡ t|ƒ}| d d|i¡d }| ¡ }d|d d …< t||ƒ d S )Nr   r?   Úzeror=   rB   rC   Ú	ReduceSumÚrsumÚGreaterÚcondÚthen_outr   é   rl   Úcst1©r   r   rb   r>   Ú	then_bodyÚelse_outr   Úcst2Ú	else_bodyÚIf©Úthen_branchÚelse_branchÚifrv   ©r}   rS   rJ   rU   )rW   r]   rX   r   r\   r   r#   r'   r   r$   r!   r[   r"   r%   r~   r   rt   ÚstrÚonesr   r_   r
   ra   Úcopyr   ©r3   rb   r…   rB   rC   r‡   r‰   rŠ   Úthen_cstÚthen_const_noderŽ   r   Úelse_cstÚelse_const_noder‘   Úif_noder+   Ú
onnx_modelr   re   rf   rg   ri   rj   r8   r8   r9   Útest_replace_constant_graphä   sP   
ÿÿ
ÿÿÿÿz.TestToolsFunctions.test_replace_constant_graphc                 C  s  t jdgt jd}tj|dd}t dtjj	d d g¡}t dtjj	d g¡}t 
ddgdg¡}t 
d	ddgd
g¡}t dtjj	d ¡}t t  dgd ¡ t j¡¡}tj
dg dg|dd}	t |	gdg |g¡}
t dtjj	d ¡}t t  dgd ¡ t j¡¡}tj
dg dg|dd}t |gdg |g¡}tjj
dd
gdg|
|d}t |||gd|g|g|g¡}tj|t dtƒ ¡gd}|  dt|ƒ¡ t jdt jd}t|ƒ}| d d|i¡d }t|dd}|  dt|ƒ¡ |  dt|ƒ¡ t|ƒ}| d d|i¡d }t|j|jƒ d S ) Nr   r?   r…   r=   rB   rC   r†   r‡   rˆ   r‰   rŠ   r   r‹   rl   rŒ   r   rŽ   r   r   r   r‘   r’   r“   r–   rv   r—   rS   rJ   Trq   rs   )rW   r]   rX   r   r\   r   r#   r'   r   r$   r!   r[   r"   r%   r~   r   rt   r˜   r™   r   r_   r
   ra   r   r/   r›   r8   r8   r9   Útest_replace_range_graph  sN   
ÿÿ
ÿÿÿÿz+TestToolsFunctions.test_replace_range_graphN)r   r   )Ú__name__Ú
__module__Ú__qualname__r:   rk   rp   ru   rƒ   r„   r¢   r£   r8   r8   r8   r9   r      s    
*,+1r   Ú__main__r   )Ú	verbosity)Ú
__future__r   rx   ÚnumpyrW   Únumpy.testingr   r'   r   r   r   Ú	onnx.defsr   Úonnx.referencer   Ú
onnx.toolsr	   Úonnx.tools.replace_constantsr
   ÚTestCaser   r¤   Úmainr8   r8   r8   r9   Ú<module>   s      5ÿ