3
I4W^@"  ใ               @   sฌ   d dl mZ d dl mZ d dl mZ d dl mZ d dlmZmZ d dlm	Z	 d dl
jjZd dlmZ d dljZd dlZd dlZd	d
 Zdd Zdd ZG dd dejZdS )้    )ฺabsolute_import)ฺdivision)ฺprint_function)ฺunicode_literals)ฺcoreฺ	workspace)ฺCreatePythonOperatorN)ฺgivenc               C   s   t dd S )Nz!This is an intentional exception.)ฺRuntimeErrorฉ r   r   ๚=/tmp/pip-build-e66j97b2/torch/caffe2/python/python_op_test.pyฺ!SubFunctionThatThrowsRuntimeError   s    r   c             C   s   t  S )N)r   )ฺinputsฺ_r   r   r   ฺ$MainOpFunctionThatThrowsRuntimeError   s    r   c                sD   dgdkst dkst  d dk s.t  fdd}|S )Nr   ฺname้   gอฬฬฬฬฬ@g-C๋โ6?c                sV   | d j d d kstdks&tdks2t d dk sBtd  d7  < d S )Nr   r   r   gอฬฬฬฬฬ@g-C๋โ6?้   )ฺdataฺAssertionError)r   ฺoutputs)ฺextraฺindexฺ
iterationsr   r   r   ฺmy_op   s
    zop_builder.<locals>.my_op)r   )r   r   r   r   r   )r   r   r   r   r   ฺ
op_builder   s    r   c               @   s<  e Zd Zeej ddd Zdd Zeej ddd Zdd	 Z	eej dd
d Z
eej ddd Zeej ddd Zeej ddd Zeej ejdddejdddddd Zef ej ej dejdd Zef dejddiejdd Zef dejd diejd!d" Zd#S )$ฺPythonOpTest)ฺxc                s6    fdd}t |dgg }tjd tj| d S )Nc                sZ    j j| d j  j t| d jt  j t| d jtj tjj| d j d S )Nr   )	ฺassertEqualฺshapeฺtypeฺtupler   ฺnpฺndarrayฺtestingฺassert_almost_equal)r   r   )ฺselfr   r   r   ฺf)   s    z!PythonOpTest.test_feed.<locals>.fr   )r   r   ฺFeedBlobฺRunOperatorOnce)r&   r   r'   ฺopr   )r&   r   r   ฺ	test_feed'   s    zPythonOpTest.test_feedc          	   C   s4   t tg g }tj| td tj| W d Q R X d S )Nz!This is an intentional exception.)r   r   ฺsixฺassertRaisesRegexr
   r   r)   )r&   r*   r   r   r   ฺtest_exception2   s    zPythonOpTest.test_exceptionc                sD    fdd}t jd}|j|dgg  tjd tj| d S )Nc                sZ    j j| d j  j t| d jt  j t| d jtj tjj| d j d S )Nr   )	r   r   r    r!   r   r"   r#   r$   r%   )r   r   )r&   r   r   r   r'   9   s    z6PythonOpTest.test_feed_with_helper_function.<locals>.fฺtestr   )r   ฺNetฺPythonr   r(   Z
RunNetOnce)r&   r   r'   ฺnetr   )r&   r   r   ฺtest_feed_with_helper_function7   s
    
z+PythonOpTest.test_feed_with_helper_functionc             C   sภ   t jd}d}|jtddgddif|gg  |jtddgddif|gg  xltdD ]`}|jd| }tj|tj	d	g tj
| x.td
D ]"}tj|tj	|g tj| qW qXW d S )NZbuilder_templateฺiterr   r   r   gอฬฬฬฬฬ@้   z
builder_%dr   ้
   )r   r0   r1   r   ฺrangeZCloner   r(   r"   ฺarrayZ	CreateNetZRunNet)r&   r2   Z	iter_blobฺrepeatZ
cloned_netฺir   r   r   ฺtest_builder_tupleC   s    
  
zPythonOpTest.test_builder_tuplec                sN    fdd}t |dgg }tjd tj| ~tjd tj| d S )Nc                s,    j j| d j tjj| d j d S )Nr   )r   r   r"   r$   r%   r   )r   r   )r&   r   r   r   r'   V   s    z)PythonOpTest.test_feed_with_gc.<locals>.fr   )r   r   r(   r)   )r&   r   r'   r*   r   )r&   r   r   ฺtest_feed_with_gcT   s    
zPythonOpTest.test_feed_with_gcc                sP    fdd}t |dgdg}tjd tj| tjd}tjj| d S )Nc                sT   |d j | d j  jj| d j  jj|d j | d j|d jd< d S )Nr   .)ฺreshaper   r   r   )r   r   )r&   r   r   r   r'   b   s    z$PythonOpTest.test_reshape.<locals>.fr   ฺy)r   r   r(   r)   ฺ	FetchBlobr"   r$   r%   )r&   r   r'   r*   r>   r   )r&   r   r   ฺtest_reshape`   s    

zPythonOpTest.test_reshapec                sX    fdd}t jj }tjd}|jg dg  jd |j|ddg g  |j| dS )	zI
        Verify that python op can manipulate workspace directly
        c                s    |j d j }tjj|  d S )Nฺinternal)Zblobsฺfetchr"   r$   r%   )r   r   ฺwsZfetched)r   r   r   r'   s   s    z3PythonOpTest.test_workspace_manipulation.<locals>.fr/   rA   )ฺvaluesr   T)Zpass_workspaceN)	r   ฺCZ	Workspacer   r0   ZGivenTensorFillr   r1   ฺrun)r&   r   r'   rC   r2   r   )r   r   ฺtest_workspace_manipulationn   s    

z(PythonOpTest.test_workspace_manipulationc             C   s2   dd }t |dgdg}tjd| tj| d S )Nc             S   s&   yt dW n t k
r    Y nX d S )NzException in handler)ฺ	Exception)r   r   r   r   r   r'      s    z>PythonOpTest.test_caught_exception_doesnt_terminate.<locals>.fr   r>   )r   r   r(   r)   )r&   r   r'   r*   r   r   r   ฺ&test_caught_exception_doesnt_terminate}   s    z3PythonOpTest.test_caught_exception_doesnt_terminater   ้   )Z	min_valueZ	max_value)r   ฺnฺwc       
         sพ   dd   fddt |D }tjd}|j jj| d|j _||j _d}tjd}|j	tj
d	|| tjd
| tj|j j  x,t |D ] }tjt|}	tjj||	 qW d S )Nc             S   s,   |d j | d j | d j|d jd< d S )Nr   .)r=   r   r   )r   r   r   r   r   r'      s    z5PythonOpTest.test_multithreaded_evaluation.<locals>.fc                s    g | ]}t  d gt|gqS )r   )r   ฺstr)ฺ.0r:   )r'   r   r   ๚
<listcomp>   s    z>PythonOpTest.test_multithreaded_evaluation.<locals>.<listcomp>r2   Zdag้d   ฺplanz	test-stepr   )r7   r   r0   ZProtor*   ฺextendr    Znum_workersZPlanZAddStepZExecutionStepr   r(   ZRunPlanZSerializeToStringr?   rM   r"   r$   r%   )
r&   r   rK   rL   ฺopsr2   ZitersrQ   r:   r>   r   )r'   r   ฺtest_multithreaded_evaluation   s    



z*PythonOpTest.test_multithreaded_evaluation)r   ฺin_placec             C   sZ   dd }dd }t |dg|r dndg|d}| j|||gddg | j|||gdg d S )	Nc             S   s0   |d j | d j | d jd |d jd< d S )Nr   r5   .)r=   r   r   )r   r   r   r   r   r'       s    z%PythonOpTest.test_gradient.<locals>.fc             S   s0   | d }|d }|j |j |jd |jd< d S )Nr5   r   .)r=   r   r   )r   r   ฺgrad_outputฺ
grad_inputr   r   r   ฺgrad_fค   s    z*PythonOpTest.test_gradient.<locals>.grad_fr   r>   )rX   r   )r   ฺassertGradientChecksฺassertDeviceChecks)r&   r   rU   ฺgcฺdcr'   rX   r*   r   r   r   ฺtest_gradient   s    zPythonOpTest.test_gradientr   r5   )rK   c       
         s|   |\}} fdd} fdd}t |ddgddg|d	}x&dD ]}	 j||||g|	d
dg q>W  j||||gd
dg d S )Nc                sT   xNdD ]F} j t| | jt || j| | j | | jd || jd< qW d S )Nr   r   r5   .)r   r   )r   r    r   r!   r=   r   )r   r   ฺidx)r&   r   r   r'   ต   s    
z.PythonOpTest.test_gradient_multiple.<locals>.fc                sb    j t| d  j t|d x<d
D ]4\}}| | }|| }|j|j |jd |jd< q&W d S )N้   r5   ้   r   r   r   .ฉr`   r   ฉr   r   )ra   rb   )r   ฺlenr=   r   r   )r   r   ฺgrad_output_idxฺgrad_input_idxrV   rW   )r&   r   r   rX   ป   s    z3PythonOpTest.test_gradient_multiple.<locals>.grad_fฺx1ฺx2ฺy1ฺy2)rX   r   r   )r   r   )r   rY   rZ   )
r&   r   r[   r\   rf   rg   r'   rX   r*   r^   r   )r&   r   ฺtest_gradient_multipleฑ   s    

z#PythonOpTest.test_gradient_multiple้   c       
         s   |\}}} fdd} fdd}t |dddgdd	d
g|ddgdgd}	 j||	|||gdddg  j||	|||gdddg d S )Nc                sT   xNdD ]F} j t| | jt || j| | j | | jd || jd< qW d S )Nr   r   r5   .)r   r   r5   )r   r    r   r!   r=   r   )r   r   r^   )r&   r   r   r'   ฯ   s    
z;PythonOpTest.test_gradient_multiple_with_indices.<locals>.fc                sb    j t| d  j t|d x<dD ]4\}}| | }|| }|j|j |jd |jd< q&W d S )	N้   r   r_   r   r5   .ฉr_   r   )rm   )r   rc   r=   r   r   )r   r   rd   re   rV   rW   )r&   r   r   rX   ี   s    z@PythonOpTest.test_gradient_multiple_with_indices.<locals>.grad_frf   rg   ฺx3rh   ri   Zy3r   r5   )rX   Zgrad_output_indicesZgrad_input_indicesr   )r   rY   rZ   )
r&   r   r[   r\   rf   rg   rn   r'   rX   r*   r   )r&   r   ฺ#test_gradient_multiple_with_indicesห   s    


z0PythonOpTest.test_gradient_multiple_with_indicesN)ฺ__name__ฺ
__module__ฺ__qualname__r	   ฺhuZtensorr+   r.   r3   r;   r<   r@   rG   rI   ฺstZintegersrT   ZbooleansZgcsr]   Ztensorsrj   ro   r   r   r   r   r   &   s   &$r   )ฺ
__future__r   r   r   r   Zcaffe2.pythonr   r   Zcaffe2.python.corer   Z"caffe2.python.hypothesis_test_utilฺpythonZhypothesis_test_utilrs   Z
hypothesisr	   Zhypothesis.strategiesZ
strategiesrt   Znumpyr"   r,   r   r   r   ZHypothesisTestCaser   r   r   r   r   ฺ<module>   s   
