U
    ‰d"  ã                   @   s°   d dl Z d dlmZ d dlmZmZ d dlZd dl	m
Z
 d dlmZmZ d dlmZ d dlm  mZ d dlm  mZ e  ejj d¡G dd„ dejƒƒZed	kr¬e  ¡  dS )
é    N)ÚgivenÚsettings)Ú
caffe2_pb2)ÚcoreÚ	workspace)ÚoptimizeForMKLDNNzNo MKLDNN support.c                   @   s   e Zd Zef e dd¡e dd¡e dd¡e dd¡e dd¡e dd¡e dd¡e ¡ e ¡ e dd¡dœ
ej—Že	dd	d
dd„ ƒƒZ
e	dd	d
ef e dd¡e dd¡e dd¡e dd¡e dd¡e dd¡e ¡ e ¡ dœej—Ždd„ ƒƒZef de dd¡iej—Ždd„ ƒZd	S )ÚConvTesté   é   r   é   é   é
   é   )
ÚstrideÚpadÚkernelÚsizeÚinput_channelsÚoutput_channelsÚ
batch_sizeÚuse_biasÚtraining_modeÚgroupN)Zmax_examplesÚdeadlinec              
   C   sô   |	rdnd}t jd|r dddgnddgdg||||
|d}tj |||
 ||¡ tj¡d	 }tj ||
 |||¡ tj¡d	 }tj ||
 ¡ tj¡d	 }|r¨|||gn||g}|  |||dg¡ |	rðtt	|ƒƒD ]}| j
||||dgd
d qÒd S )Nr	   r   ÚConvÚXÚwÚbÚY)r   r   r   r   r   ç      à?ç{®Gáz„?©Ú	threshold©r   ÚCreateOperatorÚnpÚrandomÚrandÚastypeÚfloat32ZassertDeviceChecksÚrangeÚlenZassertGradientChecks)Úselfr   r   r   r   r   r   r   r   r   r   ÚgcÚdcÚtrainingÚopr   r   r   ÚinputsÚi© r3   úD/tmp/pip-unpacked-wheel-ua33x9lu/caffe2/python/ideep/conv_op_test.pyÚtest_convolution   s@    ø
   ÿÿÿÿÿzConvTest.test_convolutioné   é    )r   r   r   r   r   r   r   r   c              
   C   sð   |rdnd}d}d}t jd|r(dddgnddgdg|||||d	}tj ||||¡ tj¡d
 }tj ||||¡ tj¡d
 }tj |¡ tj¡d
 }|r¤|||gn||g}|  |
||dg¡ |rìtt	|ƒƒD ]}| j
|	|||dgdd qÎd S )Nr	   r   r
   r   r   r   r   r   )r   r   r   r   Ú	algorithmr   r    r!   r#   )r,   r   r   r   r   r   r   r   r   r-   r.   r/   Zconv3x3_winograd_algorithmr   r0   r   r   r   r1   r2   r3   r3   r4   Útest_winograd_convolution:   sN    ø
   ÿÿÿ   ÿþþz"ConvTest.test_winograd_convolutionr   c              
   C   sn  t jddddgdgdddd|d d	}t jddddgdgdddd|d d	}tj |d
dd¡ tj¡}tj d
ddd¡ tj¡}tj d
¡ tj¡}t dd¡ t 	d||d ¡ t 	d||d ¡ t 	d||d ¡ t 
|¡ t d¡}	t ¡  t 	d||d ¡ t 	d||d ¡ t 	d||d ¡ t  d¡}
t ¡ }|j |g¡ |
 ¡  |¡ t|
ƒ t 
|
 ¡ jd ¡ t d¡}tj|	|dddsÊt| ¡ ƒ t|	 ¡ ƒ tt t ||	 ¡¡ƒ |  d¡ t ¡  t 	d||d ¡ t 	d||d ¡ t 	d||d ¡ t 
|¡ t d¡}tj|	|dddsjt| ¡ ƒ t|	 ¡ ƒ tt t ||	 ¡¡ƒ |  d¡ d S )Nr   r   r   r   r   r	   r   é   )r   r   r   r   Zdevice_optioni   é   éˆ   Z_device_check_TÚnetr    )ZatolZrtolF)r   r$   r%   r&   r'   r(   r)   r   ZSwitchWorkspaceZFeedBlobZRunOperatorOnceZ	FetchBlobZResetWorkspaceZNetr   ZNetDefr0   ÚextendZProtoZCopyFromr   ZallcloseÚprintÚflattenÚmaxÚabsÚ
assertTrue)r,   r   r-   r.   r0   Zop1r   r   r   ZY0r=   Zold_netZY1ZY2r3   r3   r4   Útest_depthwise_convolutionb   sp    ø
ø







z#ConvTest.test_depthwise_convolution)Ú__name__Ú
__module__Ú__qualname__r   ÚstZintegersZbooleansÚmuZgcsr   r5   r9   rD   r3   r3   r3   r4   r      s<   






÷
ö






ùø	r   Ú__main__)ZunittestZhypothesis.strategiesZ
strategiesrH   Z
hypothesisr   r   Znumpyr%   Zcaffe2.protor   Zcaffe2.pythonr   r   Zcaffe2.python.transformationsr   Z"caffe2.python.hypothesis_test_utilÚpythonZhypothesis_test_utilÚhuZcaffe2.python.ideep_test_utilZideep_test_utilrI   ZskipIfÚCZ
use_mkldnnZHypothesisTestCaser   rE   Úmainr3   r3   r3   r4   Ú<module>   s    