U
    ‰d  ã                   @   sp   d dl mZ d dlmZmZmZmZmZmZm	Z	 d dl
mZ d dlZG dd„ deddƒƒZG dd	„ d	ejƒZdS )
é    )Ú
namedtuple)ÚcoreÚlayer_model_instantiatorÚlayer_model_helperÚschemaÚ	test_utilÚ	workspaceÚutils)Ú
caffe2_pb2Nc                       s   e Zd Zd‡ fdd„	Z‡  ZS )ÚOpSpecNc                    s   t t| ƒ | ||||¡S ©N)Úsuperr   Ú__new__)ÚclsZop_typeZop_inputZ	op_outputZop_arg©Ú	__class__© úA/tmp/pip-unpacked-wheel-ua33x9lu/caffe2/python/layer_test_util.pyr      s     ÿzOpSpec.__new__)N)Ú__name__Ú
__module__Ú__qualname__r   Ú__classcell__r   r   r   r   r      s   r   ztype input output argc                       sz   e Zd Z‡ fdd„Zdd„ Zddd„Zdd	„ Zddd„Zdd„ Zdd„ Z	dd„ Z
ddd„Zdd„ Zdd„ Zdd„ Z‡  ZS )ÚLayersTestCasec                    s   t t| ƒ ¡  |  ¡  d S r   )r   r   ÚsetUpÚsetup_example©Úselfr   r   r   r   "   s    zLayersTestCase.setUpc                 C   s   t  ¡  |  ¡  dS )z„
        This is undocumented feature in hypothesis,
        https://github.com/HypothesisWorks/hypothesis-python/issues/59
        N)r   ZResetWorkspaceÚreset_modelr   r   r   r   r   &   s    zLayersTestCase.setup_exampleNc                 C   s@   |pt  dt  tjdf¡f¡}|p(t  ¡ }tjd||d| _d S )NZfloat_features)é    Z
test_model)Úinput_feature_schemaÚtrainer_extra_schema)r   ÚStructZScalarÚnpZfloat32r   ZLayerModelHelperÚmodel)r   r   r    r   r   r   r   .   s    ÿýzLayersTestCase.reset_modelc                 C   s   t  | jj|¡S r   )r   Z	NewRecordr#   Únet)r   Z
schema_objr   r   r   Ú
new_record8   s    zLayersTestCase.new_recordFc                 C   sH   t  d¡}|r| j d¡}n
t  d¡}| jjD ]}| ||¡ q.||fS )zÌ
        We don't use
        layer_model_instantiator.generate_training_nets_forward_only()
        here because it includes initialization of global constants, which make
        testing tricky
        Ú	train_netÚtrain_init_net)r   ZNetr#   Zcreate_init_netZlayersZadd_operators)r   Zadd_constantsr&   r'   Zlayerr   r   r   Úget_training_nets;   s    

z LayersTestCase.get_training_netsc                 C   s   t  | j¡S r   )r   Zgenerate_eval_netr#   r   r   r   r   Úget_eval_netK   s    zLayersTestCase.get_eval_netc                 C   s   t  | j¡S r   )r   Zgenerate_predict_netr#   r   r   r   r   Úget_predict_netN   s    zLayersTestCase.get_predict_netc                 C   s4   t  ¡ | j_t | j¡\}}t |¡ t |¡ d S r   )r   r!   r#   Úoutput_schemar   Zgenerate_training_netsr   Ú
RunNetOnce)r   r'   r&   r   r   r   Úrun_train_netQ   s
    
ÿ
zLayersTestCase.run_train_neté   c                 C   sX   t  ¡ | j_t | j¡\}}t |¡ |dks6tdƒ‚t 	|¡ tj
| ¡ j|d d S )Nr   znum_iter must be larger than 0)Únum_iter)r   r!   r#   r+   r   Z#generate_training_nets_forward_onlyr   r,   ÚAssertionErrorZ	CreateNetZRunNetÚProtoÚname)r   r/   r'   r&   r   r   r   Úrun_train_net_forward_onlyX   s    ÿÿ

z)LayersTestCase.run_train_net_forward_onlyc                 C   sN   |dkrdS |   t|ƒt|ƒ¡ t||ƒD ]\}}|dkr<q*|   ||¡ q*dS )zæ
        spec_blobs can either be None or a list of blob names. If it's None,
        then no assertion is performed. The elements of the list can be None,
        in that case, it means that position will not be checked.
        N)ÚassertEqualÚlenÚzip)r   Z
spec_blobsZop_blobsZ	spec_blobZop_blobr   r   r   ÚassertBlobsEqualb   s    zLayersTestCase.assertBlobsEqualc                    sB   |   t|ƒt|ƒ¡ dd„ |D ƒ‰ ‡ fdd„}|   ||ƒ|¡ d S )Nc                 S   s   g | ]
}|j ‘qS r   )r2   )Ú.0Úar   r   r   Ú
<listcomp>r   s     z2LayersTestCase.assertArgsEqual.<locals>.<listcomp>c                    s<   t  ¡ }ˆ D ](}| | }t ||¡}|j ¡  |¡ q|jS r   )r
   ZOperatorDefr	   ZMakeArgumentÚargÚaddZCopyFrom)ÚargsÚoperatorÚkÚvr;   ©Úkeysr   r   Ú
parse_argst   s    z2LayersTestCase.assertArgsEqual.<locals>.parse_args)r4   r5   )r   Z	spec_argsZop_argsrC   r   rA   r   ÚassertArgsEqualp   s    	zLayersTestCase.assertArgsEqualc                 C   s€   |  ¡ j}|  t|ƒt|ƒ¡ t||ƒD ]R\}}|  |j|j¡ |  |j|j¡ |  |j|j¡ |j	dk	r(|  
|j	|j	¡ q(|S )zW
        Given a net and a list of OpSpec's, check that the net match the spec
        N)r1   Úopr4   r5   r6   Útyper7   ÚinputÚoutputr;   rD   )r   r$   Zop_specsÚopsrE   Zop_specr   r   r   ÚassertNetContainOps   s    

z"LayersTestCase.assertNetContainOps)NN)F)r.   )r   r   r   r   r   r   r%   r(   r)   r*   r-   r3   r7   rD   rJ   r   r   r   r   r   r       s   




r   )Úcollectionsr   Zcaffe2.pythonr   r   r   r   r   r   r	   Zcaffe2.protor
   Znumpyr"   r   ZTestCaser   r   r   r   r   Ú<module>   s
   $	