U
    rhyU                     @  s`   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l	m
Z
mZmZmZ G dd deZdS )    )annotationsN)Base)expect)get_output_shape_auto_pad!get_output_shape_explicit_paddingget_pad_shapepoolc                   @  sb  e Zd ZeddddZeddddZeddddZeddd	d
ZeddddZeddddZ	eddddZ
eddddZeddddZeddddZeddddZeddddZeddddZeddddZedddd Zeddd!d"Zeddd#d$Zeddd%d&Zeddd'd(Zd)S )*MaxPoolNone)returnc                  C  s   t jjddgdgddgddddgd} tdddd	dgd
ddddgdddddgdddddgdddddggggtj}tdddddgdddddgdddddgdddddgdddddggggtj}t| |g|gdd d S )!winput_shape: [1, 1, 5, 5]
        output_shape: [1, 1, 5, 5]
        pad_shape: [4, 4] -> [2, 2, 2, 2] by axis
        r	   xy      inputsoutputskernel_shapepads                  	   
                                                Ztest_maxpool_2d_uint8r   r   nameN)onnxhelper	make_nodenparrayastypeZuint8r   noder   r    r7   G/tmp/pip-unpacked-wheel-xnis5xre/onnx/backend/test/case/node/maxpool.pyexport_maxpool_2d_uint8   s@    
zMaxPool.export_maxpool_2d_uint8c                  C  s   t jjddgdgddgddddgd} tdddd	dgd
ddddgdddddgdddddgdddddggggtj}tdddddgdddddgdddddgdddddgdddddggggtj}t| |g|gdd d S )!r   r	   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   Z test_maxpool_2d_precomputed_padsr-   Nr/   r0   r1   r2   r3   r4   float32r   r5   r7   r7   r8   "export_maxpool_2d_precomputed_pads>   s@    
z*MaxPool.export_maxpool_2d_precomputed_padsc                  C  s8  t jjddgddgddgddddgd} tddd	d
dgdddddgdddddgdddddgdddddggggtj}tdddddgdddddgdddddgdddddgdddddggggtj}tdddddgdddddgdddddgdddddgdddddggggtj}t| |g||gdd  d!S )"r   r	   r   r   zr   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   Z,test_maxpool_with_argmax_2d_precomputed_padsr-   N	r/   r0   r1   r2   r3   r4   r;   Zint64r   r6   r   r   r=   r7   r7   r8   .export_maxpool_with_argmax_2d_precomputed_padsh   sb    
z6MaxPool.export_maxpool_with_argmax_2d_precomputed_padsc                  C  s   t jjddgdgddgddgd} tddddd	gd
ddddgdddddgdddddgdddddggggtj}tddgddggggtj}t| |g|gdd d S )!Einput_shape: [1, 1, 5, 5]
        output_shape: [1, 1, 2, 2]
        r	   r   r   r   r   r   r   stridesr   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   Z#test_maxpool_2d_precomputed_stridesr-   Nr:   r5   r7   r7   r8   %export_maxpool_2d_precomputed_strides   s4        "   z-MaxPool.export_maxpool_2d_precomputed_stridesc                  C  s   t jjddgddgddgddgdd} tdddd	d
gdddddgdddddgdddddgdddddggggtj}tddgddggggtj}tddgddggggtj}t| |g||gdd  d!S )"rA   r	   r   r   r=   r   r   )r   r   r   rC   Zstorage_orderr   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   Z/test_maxpool_with_argmax_2d_precomputed_stridesr-   Nr>   r?   r7   r7   r8   1export_maxpool_with_argmax_2d_precomputed_strides   s8    ""z9MaxPool.export_maxpool_with_argmax_2d_precomputed_stridesc                  C  s   t jjddgdgddgddgdd} tdddd	d
gdddddgdddddgdddddgdddddggggtj}tdddgdddgdddggggtj}t| |g|gdd  d!S )"zwinput_shape: [1, 1, 5, 5]
        output_shape: [1, 1, 3, 3]
        pad_shape: [2, 2] -> [1, 1, 1, 1] by axis
        r	   r   r   r   r   
SAME_UPPER)r   r   r   rC   auto_padr   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   Z&test_maxpool_2d_precomputed_same_upperr-   Nr:   r5   r7   r7   r8   (export_maxpool_2d_precomputed_same_upper   s6    .   z0MaxPool.export_maxpool_2d_precomputed_same_upperc            
      C  s   t jjddgdgdgd} tjdddtj}t|}d	}dg}dg}t	||dd	 ||\}}|}t
|||||d
}	t| |g|	gdd d	S )zAinput_shape: [1, 3, 32]
        output_shape: [1, 3, 31]
        r	   r   r   r   r   r   r   r   r       NMAXZtest_maxpool_1d_defaultr-   r/   r0   r1   r2   randomrandnr4   r;   shaper   r   r   
r6   r   x_shaper   r   rC   	out_shape_paddedr   r7   r7   r8   export_maxpool_1d_default  s(    
 
  z!MaxPool.export_maxpool_1d_defaultc            
      C  s   t jjddgdgddgd} tjddddtj}t|}d	}d
}d}t	||dd	 ||\}}|}t
|||||d}	t| |g|	gdd d	S )zIinput_shape: [1, 3, 32, 32]
        output_shape: [1, 3, 31, 31]
        r	   r   r   r   rI   r   r   rJ   Nr   r   r   r   rK   Ztest_maxpool_2d_defaultr-   rL   rP   r7   r7   r8   export_maxpool_2d_default  s(    
 
  z!MaxPool.export_maxpool_2d_defaultc            
      C  s   t jjddgdgdddgd} tjdddddtj}t|}d	}dddg}dddg}t	||dd	 ||\}}|}t
|||||d
}	t| |g|	gdd d	S )zQinput_shape: [1, 3, 32, 32, 32]
        output_shape: [1, 3, 31, 31, 31]
        r	   r   r   r   rI   r   r   rJ   NrK   Ztest_maxpool_3d_defaultr-   rL   rP   r7   r7   r8   export_maxpool_3d_default4  s(    


 
  z!MaxPool.export_maxpool_3d_defaultc                  C  s  t jjddgdgddgdd} tjddd	d	tj}t|}d
}d}t	d|dd ||}t
d|dd |||}|d d }|d | }|d d }	|d |	 }
tj|dd||f|	|
ffdtjd}||	||
g}t|||||d|}t| |g|gdd dS )z{input_shape: [1, 3, 32, 32]
        output_shape: [1, 3, 32, 32]
        pad_shape: [1, 1] -> [0, 1, 0, 1] by axis
        r	   r   r   r   rF   r   r   r   rG   r   r   rJ   rV   rW   Nr   r   r   constantmodeZconstant_valuesrK   Ztest_maxpool_2d_same_upperr-   r/   r0   r1   r2   rM   rN   r4   r;   rO   r   r   padnanr   r   )r6   r   rQ   r   rC   rR   	pad_shapepad_top
pad_bottompad_left	pad_rightrT   r   r   r7   r7   r8   export_maxpool_2d_same_upperL  sJ    
 
   
   z$MaxPool.export_maxpool_2d_same_upperc                  C  s  t jjddgdgddgdd} tjddd	d	tj}t|}d
}d}t	d|dd ||}t
d|dd |||}|d d }|d | }|d d }	|d |	 }
tj|dd||f|
|	ffdtjd}||
||	g}t|||||d|}t| |g|gdd dS )z{input_shape: [1, 3, 32, 32]
        output_shape: [1, 3, 32, 32]
        pad_shape: [1, 1] -> [1, 0, 1, 0] by axis
        r	   r   r   r   Z
SAME_LOWERrZ   r   r   rJ   rV   rW   Nr   r[   r\   r]   rK   Ztest_maxpool_2d_same_lowerr-   r_   )r6   r   rQ   r   rC   rR   rb   rd   rc   rf   re   rT   r   r   r7   r7   r8   export_maxpool_2d_same_lowerr  sJ    
 
   
   z$MaxPool.export_maxpool_2d_same_lowerc               	   C  s   t jjddgdgddgddddgd} tjddddtj}t|}d	}d
}d } } }}||||g}	t	|	|dd ||\}
}	tj
|dd||f||ffdtjd}t|||||
d|	}t| |g|gdd dS )z{input_shape: [1, 3, 28, 28]
        output_shape: [1, 3, 30, 30]
        pad_shape: [4, 4] -> [2, 2, 2, 2] by axis
        r	   r   r   r   r   r   r      r   r   rW   Nr[   r\   r]   rK   Ztest_maxpool_2d_padsr-   )r/   r0   r1   r2   rM   rN   r4   r;   rO   r   r`   ra   r   r   )r6   r   rQ   r   rC   rd   rc   rf   re   r   rR   rT   r   r7   r7   r8   export_maxpool_2d_pads  s6    

 
  zMaxPool.export_maxpool_2d_padsc            	      C  s   t jjddgdgddgddgd} tjddddtj}t|}d	}d
}d}t	||dd	 ||\}}|}t
|||||d}t| |g|gdd d	S )zIinput_shape: [1, 3, 32, 32]
        output_shape: [1, 3, 10, 10]
        r	   r   r   r   r   rB   r   rJ   N)r   r   rj   r   rK   Ztest_maxpool_2d_stridesr-   rL   )	r6   r   rQ   r   r   rC   rR   rT   r   r7   r7   r8   export_maxpool_2d_strides  s*        
 
  z!MaxPool.export_maxpool_2d_stridesc               	   C  s   t jjddgdgddgddgdd} tdddd	gd
dddgddddgddddggggtj}tddgddggggtj}t| |g|gdd dS )Einput_shape: [1, 1, 4, 4]
        output_shape: [1, 1, 2, 2]
        r	   r   r   r   r   Tr   r   r   rC   	ceil_moder   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   Ztest_maxpool_2d_ceilr-   Nr:   r5   r7   r7   r8   export_maxpool_2d_ceil  s*    



"zMaxPool.export_maxpool_2d_ceilc                  C  sx   t jjddgdgddgddgdd} tddgdd	ggggtj}tdggggtj}t| |g|gd
d dS )zEinput_shape: [1, 1, 2, 2]
        output_shape: [1, 1, 1, 1]
        r	   r   r   r   r   Trn   r   r   Z.test_maxpool_2d_ceil_output_size_reduce_by_oner-   Nr:   r5   r7   r7   r8   0export_maxpool_2d_ceil_output_size_reduce_by_one  s     "z8MaxPool.export_maxpool_2d_ceil_output_size_reduce_by_onec               	   C  s   t jjddgdgddgddgddgd} tddddgd	d
ddgddddgddddggggtj}tddgddggggtj}t| |g|gdd dS )rm   r	   r   r   r   r   r   r   r   rC   	dilationsr   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   Ztest_maxpool_2d_dilationsr-   Nr:   r5   r7   r7   r8   export_maxpool_2d_dilations  s*    



"z#MaxPool.export_maxpool_2d_dilationsc                  C  s6  t jjddgdgdddgdddgdddgd} tddddgd	d
ddgddddgddddggddddgd	d
ddgddddgddddggddddgd	d
ddgddddgddddggddddgd	d
ddgddddgddddgggggtj}tddgddggddgddgggggtj}t| |g|gdd dS )Kinput_shape: [1, 1, 4, 4, 4]
        output_shape: [1, 1, 2, 2, 2]
        r	   r   r   r   r   rr   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   Ztest_maxpool_3d_dilationsr-   Nr:   r5   r7   r7   r8   export_maxpool_3d_dilations  sN    















 *z#MaxPool.export_maxpool_3d_dilationsc                  C  sf  dddg} dddg}dddg}d}t jjddgdgdddgdddg| d}tdddd	gd
dddgddddgddddggdddd	gd
dddgddddgddddggdddd	gd
dddgddddgddddggdddd	gd
dddgddddgddddgggggtj}|jdd }td|||| |d\}}|}	t	|	d||||d|| d}
t
||g|
gdd dS )ru   r   r   Fr	   r   r   rr   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   Nro   rK   rs   Z&test_maxpool_3d_dilations_use_ref_implr-   )r   r   )r/   r0   r1   r2   r3   r4   r;   rO   r   r   r   )rs   r   rC   ro   r6   r   rQ   rR   r   rT   r   r7   r7   r8   (export_maxpool_3d_dilations_use_ref_implR  s    


















!     
   z0MaxPool.export_maxpool_3d_dilations_use_ref_implc               
   C  s   d} d}d}d}d}t jjddgdg||||d	}tjjd|  tj}td | ||||d\}}tj	|dd|d |d f|d
 |d f|d |d ffddd}	t
|	d| |||d||d}
t||g|
gdd d S )N)rJ   rJ   rJ   )r   r   r   )r   r   r   )r   r   r   Tr	   r   r   )r   r   r   rC   rs   ro   r   rw   r[   r   r   r   r   r   r\   r]   rK   rx   Z,test_maxpool_3d_dilations_use_ref_impl_larger-   )r   r   )r   r   )r/   r0   r1   r2   rM   rN   r4   r;   r   r`   r   r   )rQ   rs   r   rC   ro   r6   r   rR   r   rT   r   r7   r7   r8   .export_maxpool_3d_dilations_use_ref_impl_large  sd    
     
z6MaxPool.export_maxpool_3d_dilations_use_ref_impl_largeN)__name__
__module____qualname__staticmethodr9   r<   r@   rD   rE   rH   rU   rX   rY   rg   rh   rk   rl   rp   rq   rt   rv   ry   rz   r7   r7   r7   r8   r	      sL   ));# %% 2Er	   )
__future__r   Znumpyr2   r/   Zonnx.backend.test.case.baser   Zonnx.backend.test.case.noder   Z!onnx.reference.ops.op_pool_commonr   r   r   r   r	   r7   r7   r7   r8   <module>   s   