U
    q±Ëh`6  ã                   @  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ršejdd dS )é    )ÚannotationsN)Úassert_allclose)ÚTensorProtoÚhelperÚnumpy_helper)Úonnx_opset_version)ÚReferenceEvaluator)Úupdate_model_dims)Ú(replace_initializer_by_constant_of_shapec                   @  sR   e Zd Zd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ÚNone)Úreturnc                 C  sL  t jdddgdgddgddgd}t  |gdt  dtjd	d	d
d
g¡t  dtjd	d	ddg¡gt  dtjd	d	ddg¡g¡}t j|dd}t |d	d	ddgd	d	ddgdœdd	d	ddg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 )NZConvÚxÚWÚyé   é   )ÚinputsÚoutputsZkernel_shapeÚstridesÚtesté   é   )Zproducer_nameÚx1éÿÿÿÿ)r   r   r   Zx_3Zy_2Zy_3)r   Ú	make_nodeÚ
make_graphÚmake_tensor_value_infor   ÚFLOATÚ
make_modelr	   Zupdate_inputs_outputs_dimsÚonnxÚcheckerZcheck_modelÚassertEqualÚgraphÚinputÚtypeZtensor_typeÚshapeZdimZ	dim_paramÚoutput)ÚselfZnode_defZ	graph_defÚ	model_defZupdated_def© r*   ú8/tmp/pip-unpacked-wheel-xnis5xre/onnx/test/tools_test.pyÚtest_update_inputs_outputs_dim   sT    ûþù	

þ 
ÿú
 ÿ ÿ ÿ ÿz1TestToolsFunctions.test_update_inputs_outputs_dimc                 C  sp  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  ddddddg¡ 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   c                 S  s   h | ]
}|j ’qS r*   ©Zop_type©Ú.0Únr*   r*   r+   Ú	<setcomp>P   s     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   )r(   r2   Úvaluer.   r3   r4   r5   Únode1Únode2r#   r)   r   Úoinf1Úy1ÚreplÚ
node_typesÚoinf2Úy2r*   r*   r+   Ú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  ddddddg¡ 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   r1   r3   r4   r5   ÚConstant©rP   r6   r7   r8   r9   r:   r   r;   r   c                 S  s   h | ]
}|j ’qS r*   r<   r=   r*   r*   r+   r@   k   s     z;TestToolsFunctions.test_replace_constant.<locals>.<setcomp>rA   rB   rC   rD   ©r(   r2   rP   r.   r3   r4   r5   Únode0rQ   rR   r#   r)   r   rS   rT   rU   rV   rW   rX   r*   r*   r+   Útest_replace_constantX   s.    
$z(TestToolsFunctions.test_replace_constantc                 C  st  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  ddddddg¡ 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   r1   r3   r4   r5   rZ   r[   r6   r7   r8   r9   r:   r   r;   r   T©Z	use_rangec                 S  s   h | ]
}|j ’qS r*   r<   r=   r*   r*   r+   r@   †   s     z8TestToolsFunctions.test_replace_range.<locals>.<setcomp>ÚRangerA   )rE   rF   rG   rH   rI   r   rJ   rK   r   r   r   r   r   r   r   rL   r   rM   r
   r#   rN   rO   ÚassertNotInr   r&   r\   r*   r*   r+   Ú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  ddddddg¡ 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   r1   r3   r4   r5   rZ   r[   r6   r7   r8   Ú ÚcustomÚunittest©Údomainr9   ©Ú	functionsÚopset_importsr:   r   r;   r   c                 S  s   h | ]
}|j ’qS r*   r<   r=   r*   r*   r+   r@   ±   s     zDTestToolsFunctions.test_replace_constant_function.<locals>.<setcomp>rA   rB   rC   )rE   rF   rG   rH   rI   r   rJ   rK   r   r   r   r   r   Úmake_opsetidr   Úmake_functionr   r   rL   r   rM   r
   ri   rN   rO   r   ©r(   r2   rP   r.   r3   r4   r5   ZnodeCr]   rQ   rR   rj   ZfctrN   r#   r)   r   rS   rT   rU   rV   rW   rX   r*   r*   r+   Útest_replace_constant_function   sP    
þ
ú	  ÿ$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  ddddddg¡ 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   r1   r3   r4   r5   rZ   r[   r6   r7   r8   rc   rd   re   rf   r9   rh   r:   r   r;   r   Tr_   c                 S  s   h | ]
}|j ’qS r*   r<   r=   r*   r*   r+   r@   Ý   s     zATestToolsFunctions.test_replace_range_function.<locals>.<setcomp>r`   rA   )rE   rF   rG   rH   rI   r   rJ   rK   r   r   r   r   r   rk   r   rl   r   r   rL   r   rM   r
   ri   rN   rO   ra   r   r&   rm   r*   r*   r+   Útest_replace_range_function¹   sN    
þ
ú	  ÿ$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   r1   Úzeror/   r4   r5   Ú	ReduceSumÚrsumÚGreaterÚcondÚthen_outr   é   rZ   Úcst1©r   r   rP   r0   Ú	then_bodyÚelse_outr   Úcst2Ú	else_bodyÚIf©Zthen_branchZelse_branchÚifrc   ©rj   rA   r;   rC   )rE   rK   rF   r   rJ   r   r   r    r   r   r   rI   r   r   rk   r   ra   ÚstrÚonesr   rM   r
   rO   Úcopyr   ©r(   rP   rp   r4   r5   rr   rt   ru   Zthen_cstZthen_const_nodery   rz   Zelse_cstZelse_const_noder|   Zif_noder#   Z
onnx_modelr   rS   rT   rU   rW   rX   r*   r*   r+   Útest_replace_constant_graphä   sr      ÿ    ÿ  ÿ    ÿ    ÿ ÿ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   r1   rp   r/   r4   r5   rq   rr   rs   rt   ru   r   rv   rZ   rw   rx   ry   rz   r   r{   r|   r}   r~   r   rc   r€   rA   r;   Tr_   r`   )rE   rK   rF   r   rJ   r   r   r    r   r   r   rI   r   r   rk   r   ra   r   r‚   r   rM   r
   rO   r   r&   r„   r*   r*   r+   Útest_replace_range_graph  sp      ÿ    ÿ  ÿ    ÿ    ÿ ÿz+TestToolsFunctions.test_replace_range_graphN)Ú__name__Ú
__module__Ú__qualname__r,   rY   r^   rb   rn   ro   r…   r†   r*   r*   r*   r+   r      s   *,+1r   Ú__main__r   )Ú	verbosity)Ú
__future__r   re   ZnumpyrE   Znumpy.testingr   r    r   r   r   Z	onnx.defsr   Zonnx.referencer   Z
onnx.toolsr	   Zonnx.tools.replace_constantsr
   ZTestCaser   r‡   Úmainr*   r*   r*   r+   Ú<module>   s     5