U
    d                     @   s   d dl mZ d dlmZ d dlZd dlZd dlmZm	Z	 e
e	jj dG dd deZddd	ejfd
dZedkr~e  dS )    )TestCase)
caffe2_pb2N)core	workspacezNo MKLDNN support.c                   @   sT   e Zd Z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d ZdS )TestReShapeOpsc              	   C   s   t tjd}t | tdtjddddggtj	d tdtjddgtj
dt tjd tdtjdtj	d tt d	ddgd
dg td
tdk stW 5 Q R X d S )Nr   resZdtypeshape      input)   r   Reshapeoutput	old_shape)r   DeviceOptionr   IDEEPDeviceScoper   FeedBlobnparrayfloat32int32CPUzerosRunOperatorOnceCreateOperator	FetchBloballAssertionError)self
device_opt r"   G/tmp/pip-unpacked-wheel-ua33x9lu/caffe2/python/ideep/reshape_op_test.pytest_reshape_ops   s    "(  zTestReShapeOps.test_reshape_opsc                 C   s   t ddd t dddd d S )Nr   r   r
   r   r   r   	new_shapeFr   r(   	arg_shape_test_reshaper    r"   r"   r#   test_basic_reshape   s    z!TestReShapeOps.test_basic_reshapec                 C   s   t dddtjd d S )Nr%   r&   F)r   r(   r*   shape_dtype)r,   r   Zint64r-   r"   r"   r#   test_int64_reshape_input   s    z'TestReShapeOps.test_int64_reshape_inputc                 C   s   t ddd t dddd d S )Nr%      r'   Fr)   r+   r-   r"   r"   r#   test_missing_dim!   s    zTestReShapeOps.test_missing_dimc                 C   s"   t dddd t ddddd d S )Nr%   r1   T)r   r(   in_placeF)r   r(   r5   r*   r+   r-   r"   r"   r#   test_in_place%   s
     zTestReShapeOps.test_in_placec                 C   s@   t dddd t ddddd t dddd t ddddd d S )Nr%   )r   r   r   r   r(   expected_shapeFr   r(   r8   r*   )r   r   r
   r+   r-   r"   r"   r#   test_zero_dim*   s      zTestReShapeOps.test_zero_dimc                 C   s@   t dddd t ddddd t dddd t ddddd d S )	Nr%   )r   r2   r   r7   Fr9   )r      r   )r2   r   )r3   r;   r+   r-   r"   r"   r#   test_zero_dim_and_missing_dim4   s      z,TestReShapeOps.test_zero_dim_and_missing_dimc           	   	   C   s.  t tjd}t |
 d}d}tjj| tj	}tjj| tj	}t 
d}|jg d|| d |jg d|| d |jdgdd	g|d
 |ddgd |dg t| td}td}tjj| |||  dd tj|j|j tjj|||dd W 5 Q R X d S )Nr   r%   )r
   r3   netX)r	   valuesYZX_outr   r	   ZX_gradgh㈵>)Zrtol)r   r   r   r   r   r   randomrandastyper   NetZGivenTensorFillflattenr   ZMulZAddGradientOperatorsr   Z
RunNetOncer   testingassert_allcloseZsqueezereshapeZassert_array_equalr	   )	r    r!   r   r(   r>   r@   r=   rB   rC   r"   r"   r#   test_backprop>   s,    



  zTestReShapeOps.test_backpropc              
   C   s   t tjd}t | tdtjtj	
dddtjd t d}|jdddgd	d
\}}t| t| tdtjtj	
dddtjd t| W 5 Q R X d S )Nr   Z
input_blob
      r   ZmynetZ	z_reshapeZ
dummy_size)r2   rM   rA   (   )r   r   r   r   r   r   r   r   r   rD   rE   r   rG   r   Z	CreateNetZRunNet)r    r!   r=   z_r"   r"   r#   test_input_shape_changes^   s$    



z'TestReShapeOps.test_input_shape_changesN)__name__
__module____qualname__r$   r.   r0   r4   r6   r:   r<   rL   rR   r"   r"   r"   r#   r      s   

 r   TFc              
   C   s   t tjdg}|D ]}t | |d kr0|}tjj|  tj	}d}	|rP|	n|	d }
|rvt j
d|	g|
dg|d}n8t 
d|	dg|
dg}tdtj||dt tjd t|	| t| t|
}tj||| W 5 Q R X qd S )	Nr   r>   Z_outr   r   rA   r(   r   )r   r   r   r   r   r   rD   rE   rF   r   r   r   r   Zasarrayr   r   r   rI   rJ   rK   )r   r(   r8   r*   r5   r/   Zdevicesr!   r>   Zblob_inZblob_outopr@   r"   r"   r#   r,   p   s2    

r,   __main__)Zcaffe2.python.test_utilr   Zcaffe2.protor   ZunittestZnumpyr   Zcaffe2.pythonr   r   ZskipIfCZ
use_mkldnnr   r   r,   rS   mainr"   r"   r"   r#   <module>   s   b 
