3
I4W^If  ã            
   @   sô  d Z 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 ddlm	Z	m
Z
mZmZmZ ddlZddlZddlZddlZddlZddljZddlZddlZddlZddlZd	d
„ Zdd„ ZejjdKk�rBejj dejdej!j"gddej#j$d�ƒ ejj dejej!j"gddej#j$d�ƒ ejj dejej!j"gddej#j$d�ƒ nzejj dejdej!j"gdddej#j$d�ƒ ejj dejej!j"gdddej#j$d�ƒ ejj dejej!j"gdddej#j$d�ƒ ejj%eƒ �rÎdn
ej&ddƒƒ dLdd„Z'ej(dfdd „Z)ej(dfd!d"„Z*dd#ej(dfd$d%„Z+dd&ej(dfd'd(„Z,d)d*„ Z-dMd+d,„Z.dd#ej(dde-d-fd.d/„Z/dNd0d1„Z0dd#ej(ddd-e-ej1fd2d3„Z2d4d5„ Z3dd#ej(dfd6d7„Z4dd&ej(dfd8d9„Z5ej6ƒ Z7ej6ej8d:�Z9ej6ej:d:�Z;ej6e	j<d:�Z=e7ge	j>�röe9gng  Z?e?e	j@�re;gng  ZAe7gd;d<„ eBe	jCƒ ƒD ƒ ZDd=d>„ ZEd?d@„ ZFeGeFƒ eEƒ dA�ZHeGejIe7gƒejJe7gƒdA�ZKeGejIe9gƒejJe9gƒdA�ZLeGejIe=gƒejJe=gƒdA�ZMeGejIe?ƒejJe?ƒdA�ZNejOdOdCdD„ƒZPdPdEdF„ZQdQdGdH„ZRG dIdJ„ dJejSƒZTdS )Raq  
The Hypothesis library uses *property-based testing* to check
invariants about the code under test under a variety of random inputs.

 The key idea here is to express properties of the code under test
(e.g. that it passes a gradient check, that it implements a reference
function, etc), and then generate random instances and verify they
satisfy these properties.

The main functions of interest are exposed on `HypothesisTestCase`.
You can usually just add a short function in this to generate an
arbitrary number of test cases for your operator.

The key functions are:

- `assertDeviceChecks(devices, op, inputs, outputs)`. This asserts that the
  operator computes the same outputs, regardless of which device it is executed
  on.
- `assertGradientChecks(device, op, inputs, output_,
  outputs_with_grads)`. This implements a standard numerical gradient checker
  for the operator in question.
- `assertReferenceChecks(device, op, inputs, reference)`. This runs the
  reference function (effectively calling `reference(*inputs)`, and comparing
  that to the output of output.

`hypothesis_test_util.py` exposes some useful pre-built samplers.

- `hu.gcs` - a gradient checker device (`gc`) and device checker devices (`dc`)

- `hu.gcs_cpu_only` - a CPU-only gradient checker device (`gc`) and
  device checker devices (`dc`). Used for when your operator is only
  implemented on the CPU.
é    )Úabsolute_import)Údivision)Úprint_function)Úunicode_literals)Ú
caffe2_pb2)Ú	workspaceÚdevice_checkerÚgradient_checkerÚ	test_utilÚcoreNc               C   s(   t jdƒdkrdS t jdƒdkr$dS dS )NZ
SANDCASTLEÚ1TZTW_JOB_USERÚ
sandcastleF)ÚosÚgetenv© r   r   úC/tmp/pip-build-e66j97b2/torch/caffe2/python/hypothesis_test_util.pyÚis_sandcastle8   s
    r   c               C   s
   dt jkS )NZTRAVIS)r   Úenvironr   r   r   r   Ú	is_travis@   s    r   é   é8   r   Téd   )ÚderandomizeÚsuppress_health_checkÚdatabaseÚmax_examplesÚ	verbosityÚdevé
   )r   r   r   r   Údebugiè  é   )r   r   r   r   Úmin_satisfying_examplesr   )r   r   r   r!   r   ZCAFFE2_HYPOTHESIS_PROFILEé   c             C   s   t j| |d�S )N)Ú	min_valueÚ	max_value)ÚstÚintegers)r#   r$   r   r   r   Údims~   s    r'   c             C   sŽ   d }| t jt jt jfkr(tjd	dd�}nP| t jkrBtjddd�}n6| t jkr\tjddd�}n| t j	krptj
ƒ }ntdƒ‚|d kr„|S |j|ƒS )Ng      ð?)r#   r$   r   é   é   r    é?   z*Unexpected dtype without elements providedg      ð¿l        iÿÿÿl            l   ÿÿÿÿ )ÚnpZfloat16Úfloat32Úfloat64r%   ZfloatsÚint32r&   Úint64ÚboolÚbooleansÚ
ValueErrorÚfilter)ÚdtypeZfilter_Úelemsr   r   r   Úelements_of_type‚   s    



r6   c             C   s$   |d krt |ƒ}tjjj|| |d�S )N)Úelements)r6   Ú
hypothesisÚextraÚnumpyÚarrays)r'   r4   r7   r   r   r   r;   ‘   s    r;   é   c                s*   t jtf |Ž| |d�}|j‡ ‡fdd„ƒS )N)Úmin_sizeÚmax_sizec                s   t | ˆ ˆƒS )N)r;   )r'   )r4   r7   r   r   Ú<lambda>¢   s    ztensor.<locals>.<lambda>)r%   Úlistsr'   Úflatmap)Úmin_dimÚmax_dimr4   r7   ÚkwargsÚdims_r   )r4   r7   r   Útensor›   s    rF   é@   c             C   s   t dd||| |d�S )Nr    )r#   r$   )rF   )Úmin_lenÚmax_lenr4   r7   r   r   r   Útensor1d¥   s    rJ   c             C   sh   | dkr t jtjdgtjd�ƒS |rDt| gtjt jƒ d�jdd„ ƒS t| gtjt jdd|  d�d�S d S )Nr   )Úshaper4   )r4   r7   c             S   s   t j| t jd�| d  S )N)r4   r   )r+   Zcumsumr.   )Úxr   r   r   r?   ±   s    zsegment_ids.<locals>.<lambda>r(   )r#   r$   )	r%   Újustr+   Úemptyr.   r;   r1   Úmapr&   )ÚsizeÚ	is_sortedr   r   r   Úsegment_ids©   s    
rR   c                s®   |d krd}|d krˆ }|dks$t ‚||ks0t ‚ˆ dkrX|dkrXtjtjdgtjd�ƒS |dksht dƒ‚tjt|d dƒ|d d�j‡ fdd„ƒj	‡ fdd„ƒj	t
ƒj	tjƒS )	Nr   )rK   r4   z(size is not 0, need at least one segmentr    )r#   r$   c                s    t jjjtj| tjdˆ d�d�S )Nr   )r#   r$   )r7   )r8   r9   r:   r;   r+   r.   r%   r&   )Znum_borders)rP   r   r   r?   Ê   s    
zlengths.<locals>.<lambda>c                s   t j| t jdˆ gt jd�ƒS )Nr   )r4   )r+   ÚappendÚarrayr.   )rL   )rP   r   r   r?   Ñ   s    )ÚAssertionErrorr%   rM   r+   rN   r.   r&   ÚmaxrA   rO   ÚsortedZdiff)rP   Úmin_segmentsÚmax_segmentsrD   r   )rP   r   Úlengths¹   s    rZ   Fc       
         sZ   |rt jƒ nt jdƒ}t jtf |Ž| |d�}	t j||	ƒjdd„ ƒ}	|	j‡ ‡‡‡fdd„ƒS )NF)r=   r>   c             S   s   | d rdgng | d  S )Nr   r    r   )Úpairr   r   r   r?   ã   s    z"segmented_tensor.<locals>.<lambda>c                s    t jt| ˆ ˆƒˆ| d ˆd�ƒS )Nr   )rQ   )r%   Útuplesr;   )Z	data_dims)r4   r7   rQ   Úsegment_generatorr   r   r?   ä   s   
)r%   r1   rM   r@   r'   r\   rO   rA   )
rB   rC   r4   rQ   r7   r]   Úallow_emptyrD   Ú	gen_emptyÚ
data_dims_r   )r4   r7   rQ   r]   r   Úsegmented_tensorÕ   s    

ra   c             O   s"   t jt| |d�}t|d|i|—ŽS )N)rX   rY   r]   )Ú	functoolsÚpartialrZ   ra   )rX   rY   ÚargsrD   Úgenr   r   r   Úlengths_tensorê   s    rf   c                s\   |rt jƒ nt jdƒ}	t jtf |Ž| |d�}
t j|	|
ƒjdd„ ƒ}|j‡ ‡‡‡‡fdd„ƒS )NF)r=   r>   c             S   s:   t jt j| d ƒ| d s.t jd| d d d�nt jdƒƒS )Nr    r   )r#   r$   )r%   r\   rM   r&   )r[   r   r   r   r?   ÷   s   z)sparse_segmented_tensor.<locals>.<lambda>c          	      sJ   t jt| d ˆ ˆƒt| d ˆt jd| d d d d�d�ˆ| d ˆd�ƒS )Nr   r    )r#   r$   )r4   r7   )rQ   )r%   r\   r;   r&   )r'   )r4   r7   rQ   Úityper]   r   r   r?   ü   s   )r%   r1   rM   r@   r'   r\   rA   )rB   rC   r4   rQ   r7   r^   r]   rg   rD   r_   r`   Z	all_dims_r   )r4   r7   rQ   rg   r]   r   Úsparse_segmented_tensorð   s
    
rh   c              K   s   t f dti| —ŽS )Nr]   )rh   rZ   )rD   r   r   r   Úsparse_lengths_tensor  s    ri   c                s,   t jtf |Ž||d�}|j‡ ‡‡fdd„ƒS )N)r=   r>   c                s   t jt| ˆ ˆƒˆˆd�S )N)r=   r>   )r%   r@   r;   )r'   )r4   r7   Únr   r   r?     s   
ztensors.<locals>.<lambda>)r%   r@   r'   rA   )rj   rB   rC   r4   r7   rD   rE   r   )r4   r7   rj   r   Útensors  s    rk   c          	   C   s   t | dd||||d�S )Nr    )r#   r$   )rk   )rj   rH   rI   r4   r7   r   r   r   Ú	tensors1d  s    rl   )Údevice_typec             C   s   g | ]}t jtj|d �‘qS ))rm   Z	device_id)r   ÚDeviceOptionr   ÚGpuDeviceType)Ú.0Úir   r   r   ú
<listcomp>"  s   rr   c               C   s
   t jtƒS )N)r%   rM   Údevice_optionsr   r   r   r   Údevice_checker_device_options&  s    rt   c               C   s
   t jtƒS )N)r%   Úsampled_fromrs   r   r   r   r   Úgradient_checker_device_option*  s    rv   )ÚgcÚdcó   temp_wsc             c   s0   t jƒ }t j| dƒ d V  t jƒ  t j|ƒ d S )NT)r   ZCurrentWorkspaceZSwitchWorkspaceZResetWorkspace)ÚnameZold_ws_namer   r   r   Útemp_workspace9  s
    r{   c       
      C   s´   t j|ƒ}|jj| ƒ tjƒ }|jj|gƒ |jr8|jnd|_t	ƒ �f |pVt
j|ƒd }x0t|j|ƒD ] \}}tj|||j|| ƒd� qfW tj|ƒ tj|jd|dƒ}	W d Q R X |	S )NÚtestr   )Údevice_optionr    T)ÚcopyÚdeepcopyr}   ÚCopyFromr   ZNetDefÚopÚextendrz   r{   r   ÚInferOpBlobDevicesAsDictÚzipÚinputr   ÚFeedBlobÚgetZ	CreateNetZBenchmarkNet)
r}   r�   ÚinputsÚinput_device_optionsZ
iterationsÚnetÚ_input_device_optionsrj   ÚbÚretr   r   r   ÚrunOpBenchmarkB  s     

rŽ   c             C   sà   t j|ƒ}|jj| ƒ tƒ �º t|jƒt|ƒkrBtd|j|f ƒ‚|pRtj	|ƒd }x0t
|j|ƒD ] \}}tj|||j|| ƒd� qbW tj|ƒ ttt|jƒƒƒ}g }x*|D ]"}	|j|	 }
tj|
ƒ}|j|ƒ q¬W |S Q R X d S )Nz7must supply an input for each input on the op: %s vs %sr   )r}   )r~   r   r}   r€   r{   Úlenr…   r2   r   rƒ   r„   r   r†   r‡   ÚRunOperatorOnceÚlistÚrangeÚoutputÚ	FetchBlobrS   )r}   r�   rˆ   r‰   r‹   rj   rŒ   Úoutputs_to_checkÚoutsÚoutput_indexÚoutput_blob_namer“   r   r   r   ÚrunOpOnInput]  s,    




r™   c               @   s^   e Zd ZdZddd„Zddd	„Zddd„Zddd„Zddd„Zddd„Z	de
fdfdd„ZdS )ÚHypothesisTestCasez‰
    A unittest.TestCase subclass with some helper functions for
    utilizing the `hypothesis` (hypothesis.readthedocs.io) library.
    Nç{®Gáz„?c             C   s(   t j||d�}| j|j||||ƒƒ dS )al  
        Asserts that the operator computes the same outputs, regardless of
        which device it is executed on.

        Useful for checking the consistency of GPU and CPU
        implementations of operators.

        Usage example:

            @given(inputs=hu.tensors(n=2), in_place=st.booleans(), **hu.gcs)
            def test_sum(self, inputs, in_place, gc, dc):
                op = core.CreateOperator("Sum", ["X1", "X2"],
                                                ["Y" if not in_place else "X1"])
                X1, X2 = inputs
                self.assertDeviceChecks(dc, op, [X1, X2], [0])
        )rs   N)r   ZDeviceCheckerÚ
assertTrueÚCheckSimple)Úselfrs   r�   rˆ   r•   r‰   Ú	thresholdrx   r   r   r   ÚassertDeviceChecksƒ  s
    z%HypothesisTestCase.assertDeviceChecksç{®Gázt?çš™™™™™©?c
             C   sb   t j|||t|ƒ|	d�}
|
j||||||	d�\}}}| j|j|jƒ | j|dt|j| ƒ ƒ dS )aj  
        Implements a standard numerical gradient checker for the operator
        in question.

        Useful for checking the consistency of the forward and
        backward implementations of operators.

        Usage example:

            @given(inputs=hu.tensors(n=2), in_place=st.booleans(), **hu.gcs)
            def test_sum(self, inputs, in_place, gc, dc):
                op = core.CreateOperator("Sum", ["X1", "X2"],
                                                ["Y" if not in_place else "X1"])
                X1, X2 = inputs
                self.assertGradientChecks(gc, op, [X1, X2], 0, [0])
        )ÚstepsizerŸ   r}   Zworkspace_namer‰   )Úgrad_opsr‰   z Gradient check failed for input N)r	   ZGradientCheckerÚstrr�   ÚassertEqualrK   rœ   r…   )rž   r}   r�   rˆ   r•   Zoutputs_with_gradsr¤   rŸ   r£   r‰   rw   ÚresZgradZgrad_estimatedr   r   r   ÚassertGradientChecks¤  s    z'HypothesisTestCase.assertGradientChecksç-Cëâ6?c             C   s  |d }t jj|g||iƒ\}}	tj|ƒ}
||
||ƒ}tj|tj|ƒƒ tj|ƒ | jt|ƒt|ƒƒ x°t	|j
|ƒD ] \}}|	j|ƒ}|s–| j|ƒ qtt|t jƒr°|}d }|}n|\}}|j}tjt|ƒƒ}tjj||||dj|ƒd� |d k	rttjt|jƒƒ}tjj||ddd� qtW d S )NZ_gradz2Gradient {0} (x) is not matching the reference (y))ÚatolÚrtolÚerr_msgg-Cëâ6?)rª   r«   )r   ZGradientRegistryZGetBackwardPassr   r”   r†   ZRunOperatorsOncer¦   r�   r„   r…   r‡   ZassertIsNoneÚ
isinstanceZBlobReferenceÚvaluesr¥   r+   ÚtestingÚassert_allcloseÚformatÚindices)rž   r�   rˆ   Zref_outputsÚoutput_to_gradÚgrad_referencerŸ   Zgrad_blob_namer¤   Zgrad_mapZoutput_gradZgrad_ref_outputsrj   ÚrefZ
grad_namesZref_valsZref_indicesZval_nameÚvalsr²   r   r   r   Ú_assertGradReferenceChecksÒ  s<    	




z-HypothesisTestCase._assertGradReferenceChecksFc             C   sŒ  | j | p||kdj|ƒƒ ||kr(d S tj|ƒ}t|ƒtjkr¶|jtjdƒkrZtj	j
}qÂ|jtjdƒkrttj	j}qÂ|jtjdƒkrŽtj	j}qÂ|jtjdƒkr¨tj	j}qÂdjtjƒ}ntt|ƒƒ}y~tjjtj|| ƒjtjƒtj|jƒjtjƒdj||| |jƒd� |tj	jk�rd S tjj|| |d	j||| |ƒd� W nF tk
�r† } z(tjt|ƒƒ tjd
ƒdk�rv|‚W Y d d }~X nX d S )NzShape for {0} was not inferredr-   r,   r.   r/   z
unknown {}zShape {} mismatch: {} vs. {})r¬   zType {} mismatch: {} vs. {}ÚCAFFE2_ASSERT_SHAPEINFERENCEr   )rœ   r±   r   r”   Útyper+   Zndarrayr4   r   ZTensorProtoZDOUBLEÚFLOATZINT32ZINT64r¥   r¯   Úassert_array_equalrT   Zastyper.   rK   Zassert_equalrU   ÚloggingÚwarningr   r   )rž   rz   ÚshapesÚtypesr“   Úensure_output_is_inferredZcorrect_typeÚer   r   r   Ú_assertInferTensorChecks  sH    




z+HypothesisTestCase._assertInferTensorChecksc          $   C   sZ  t j|ƒ}|jj|ƒ tƒ ��2 t|jƒt|ƒkrDtd|j|f ƒ‚|pTtj	|ƒd }x0t
|j|ƒD ] \}}tj|||j||ƒd� qdW tjdƒ}|jƒ jj|gƒ d}ytj|gƒ\}}d}W nD tk
�r } z&tjt|ƒƒ tjdƒdkrô|‚W Y d	d	}~X nX tj|ƒ ||Ž }t|tƒ�p,t|tƒ�s8td
ƒ‚|
�sf| jt|ƒt|jƒƒ ttt|jƒƒƒ}
g }x˜t
|
|ƒD ]Š\}}|j| }tj |ƒ}|j!j"dk�r°t#j$j%||ƒ n*|	d	k�r¾|}	t#j$j&|||	|dj'|ƒd� |�rô| j(|||||d� |j)|ƒ �qvW |d	k	�rL|d	k	�s t*dƒ‚tj+|ƒ� | j,||||||d� W d	Q R X |S Q R X d	S )a  
        This runs the reference Python function implementation
        (effectively calling `reference(*inputs)`, and compares that
        to the output of output, with an absolute/relative tolerance
        given by the `threshold` parameter.

        Useful for checking the implementation matches the Python
        (typically NumPy) implementation of the same functionality.

        Usage example:

            @given(X=hu.tensor(), inplace=st.booleans(), **hu.gcs)
            def test_softsign(self, X, inplace, gc, dc):
                op = core.CreateOperator(
                    "Softsign", ["X"], ["X" if inplace else "Y"])

                def softsign(X):
                    return (X / (1 + np.abs(X)),)

                self.assertReferenceChecks(gc, op, [X], softsign)
        z7must supply an input for each input on the op: %s vs %sr   )r}   ZopnetFTr¸   r   NzlYou are providing a wrong reference implementation. A proper one should return a tuple/list of numpy arrays.ÚSÚOz(Output {0} is not matching the reference)rª   r«   r¬   )rÀ   z=If grad_reference is set,output_to_grad has to be set as well)rŸ   )rÃ   rÄ   )-r~   r   r}   r€   r{   r�   r…   r2   r   rƒ   r„   r   r†   r‡   ZNetZProtor�   r‚   ZInferShapesAndTypesÚRuntimeErrorr¼   r½   r¥   r   r   Ú
RunNetOncer­   Útupler‘   r¦   r“   r’   r”   r4   Úkindr+   r¯   r»   r°   r±   rÂ   rS   rU   ZDeviceScoper·   )rž   r}   r�   rˆ   Ú	referencer‰   rŸ   r³   r´   rª   r•   Zensure_outputs_are_inferredr‹   rj   rŒ   rŠ   Ztest_shape_inferencer¾   r¿   rÁ   Zreference_outputsr–   r—   rµ   r˜   r“   r   r   r   ÚassertReferenceChecks1  sv    #








z(HypothesisTestCase.assertReferenceChecksTc             C   s  |r<t tt|jƒt|jƒ ƒƒt |jƒt |jƒ ks<tdƒ‚tj|ƒ}|jj	|ƒ t
ƒ �¬ |pjtj|ƒd }x0t|j|ƒD ] \}	}
tj|	|
|j|	|ƒd� qzW |r¬tj|ƒ tj|ƒ dd„ |jD ƒ}|rô|f ttt|jƒt|jƒ || ƒƒŽ n|||d� W d Q R X d S )Nz0in-place ops are not supported in as_kwargs moder   )r}   c             S   s   g | ]}t j|ƒ‘qS r   )r   r”   )rp   rj   r   r   r   rr   ¹  s    z=HypothesisTestCase.assertValidationChecks.<locals>.<listcomp>)rˆ   Úoutputs)r�   Úsetr‘   r…   r“   rU   r~   r   r}   r€   r{   r   rƒ   r„   r   r†   r‡   rÆ   r�   Údict)rž   r}   r�   rˆ   Z	validatorr‰   Z	as_kwargsZinit_netr‹   rj   rŒ   rË   r   r   r   ÚassertValidationChecksœ  s,    



"z)HypothesisTestCase.assertValidationChecksc       
      C   sž   t j|ƒ}|jj|ƒ tƒ �x |p.tj|ƒd }x0t|j|ƒD ] \}}	t	j
||	|j||ƒd� q>W |d kr|| j|t	j|ƒ ntj| ||t	j|ƒ W d Q R X d S )Nr   )r}   )r~   r   r}   r€   r{   r   rƒ   r„   r…   r   r†   r‡   ZassertRaisesr�   ÚsixÚassertRaisesRegex)
rž   r}   r�   rˆ   r‰   Ú	exceptionÚregexpr‹   rj   rŒ   r   r   r   ÚassertRunOpRaisesÀ  s    	
z$HypothesisTestCase.assertRunOpRaises)Nr›   )Nr¡   r¢   N)r©   )F)Nr©   NNNNF)NTN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r    r¨   r·   rÂ   rÊ   rÎ   Ú	ExceptionrÓ   r   r   r   r   rš   }  s,    
!   
+
)
5      
e  
!rš   )r   r   r   )r    r"   )NN)NN)ry   )Nr   )N)Ur×   Ú
__future__r   r   r   r   Zcaffe2.protor   Zcaffe2.pythonr   r   r	   r
   r   Ú
contextlibr~   rb   r8   Zhypothesis.extra.numpyZhypothesis.strategiesZ
strategiesr%   r¼   r:   r+   r   rÏ   r   r   ÚversionÚ__version_info__ÚsettingsZregister_profileZHealthCheckZtoo_slowZ	VerbosityÚverboseZload_profiler   r'   r,   r6   r;   rF   rJ   rR   rZ   ra   rf   r/   rh   ri   rk   rl   rn   Zcpu_doZCUDAZcuda_doZHIPZhip_doro   Zgpu_doZhas_cuda_supportZ_device_options_no_hipZhas_hip_supportrs   r’   ZNumGpuDevicesZexpanded_device_optionsrt   rv   rÍ   Zgcsru   rM   Zgcs_cpu_onlyZgcs_cuda_onlyZgcs_gpu_onlyZ
gcs_no_hipÚcontextmanagerr{   rŽ   r™   ZTestCaserš   r   r   r   r   Ú<module>#   sê   




	
 

