U
    ‰d7  ã                   @   sN   d dl mZmZmZ d dlmZmZ d dlmZ d dl	Z	G dd„ de	j
ƒZdS )é    )ÚbrewÚmodel_helperÚscope)ÚParameterSharingÚparameter_sharing_context)ÚInitializerNc                   @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚParameterSharingTestc              
   C   sr   t  d¡}|  |d¡ t d¡H t  d¡}|  |d¡ t d¡ t  d¡}|  |d¡ W 5 Q R X W 5 Q R X d S )NÚwr   zscope/wZscope_2zscope/scope_2/w)r   Úget_parameter_nameÚassertEqualsr   Ú	NameScope)ÚselfZparam_1Zparam_2Zparam_3© r   úQ/tmp/pip-unpacked-wheel-ua33x9lu/caffe2/python/modeling/parameter_sharing_test.pyÚ%test_parameter_sharing_default_scopes   s    


z:ParameterSharingTest.test_parameter_sharing_default_scopesc                 C   s*  t  d¡ tddiƒü t d¡}|  |d¡ t  d¡` tddiƒH t d¡}|  |d¡ t  d¡ t d¡}|  |d¡ W 5 Q R X W 5 Q R X W 5 Q R X t  d	¡` tddiƒH t d¡}|  |d
¡ t  d¡ t d¡}|  |d
¡ W 5 Q R X W 5 Q R X W 5 Q R X W 5 Q R X W 5 Q R X d S )NÚglobal_scopeÚmodel_bÚmodel_ar	   úglobal_scope/wZshared_scopeÚ zglobal_scope/model_a/wZmodel_czglobal_scope/model_c/w)r   r   r   r   r
   r   )r   Zparam_globalZparam_4Zparam_5r   r   r   Ú$test_parameter_sharing_nested_scopes   s4    
ÿÿ*ÿÿz9ParameterSharingTest.test_parameter_sharing_nested_scopesc                 C   sÌ   t ddiƒ¶ t d¡  t d¡}|  |d¡ t d¡ t d¡}|  |d¡ W 5 Q R X t d¡ t d¡}|  |d¡ W 5 Q R X t d	¡ t d¡}|  |d
¡ W 5 Q R X W 5 Q R X W 5 Q R X d S )Nzglobal_scope/bzglobal_scope/ar   r	   r   Úazglobal_scope/a/wÚbÚczglobal_scope/c/w)r   r   r   r   r
   r   )r   Zparam_6Zparam_7Zparam_8Zparam_9r   r   r   Ú test_parameter_sharing_subscopes9   s    



z5ParameterSharingTest.test_parameter_sharing_subscopesc              	   C   s–   t jdd}|jddgtdƒd}t d¡ |jddgtdƒd}W 5 Q R X |  | |¡d ¡ |  | |¡d ¡ |  | |¡| |¡¡ | ¡  d S )NÚtest©Únamer	   é   ÚConstantFill©ÚshapeZinitializerÚsome_global_scope)	r   ÚModelHelperÚcreate_paramr   r   r   ÚassertNotEqualÚget_param_infoÚValidate)r   ÚmodelÚp1Úp2r   r   r   Útest_create_paramI   s     ýýz&ParameterSharingTest.test_create_paramc                 C   sÂ   t jdd}tddiƒŽ t d¡x tddiƒ` t d¡J tddiƒ2 t d¡ |jd	d
gtdƒd}W 5 Q R X W 5 Q R X W 5 Q R X W 5 Q R X W 5 Q R X W 5 Q R X |  | |¡d ¡ d S )Nr   r   r   r   r   ÚdÚeÚfr	   r   r   r    )	r   r#   r   r   r   r$   r   r%   r&   )r   r(   Úpr   r   r   Útest_deep_hierarchy\   s    ýBz(ParameterSharingTest.test_deep_hierarchyc                 C   sÔ  t jdd}|j d¡}tj||dddd}|  t¡ tj||dddd}W 5 Q R X tƒ }t	 
d¡ t	 
d	¡" | tt ||d
dd¡ƒ¡ W 5 Q R X tdd	iƒÌ t	 
d¡¶ tdddœƒœ t	 
d¡" | tt ||d
dd¡ƒ¡ W 5 Q R X t	 
d¡" | tt ||d
dd¡ƒ¡ W 5 Q R X t	 
d¡" | tt ||d
dd¡ƒ¡ W 5 Q R X W 5 Q R X W 5 Q R X W 5 Q R X W 5 Q R X |  t|jƒd¡ |  t|ƒd¡ |  t|j ¡ ƒddddddg¡ | ¡  d S )Nr   r   ÚdataÚfc1é   )Zdim_inZdim_outr   r"   r   Úoutputr   r   )Úshared_1Úshared_2r5   r6   Zunsharedé   é   Zfc1_bZfc1_wz"some_global_scope/model_a/output_bz"some_global_scope/model_a/output_wz+some_global_scope/model_a/unshared/output_bz+some_global_scope/model_a/unshared/output_w)r   r#   ÚnetZAddExternalInputr   ZfcÚassertRaisesÚAssertionErrorÚsetr   r   ÚaddÚstrr   ÚassertEqualÚlenZ_parameters_infoÚsortedÚkeysr'   )r   r(   r1   r2   Ú_Zoutput_blobsr   r   r   Útest_parameter_sharing_brewl   sH    &ÿÿÿÿ6úz0ParameterSharingTest.test_parameter_sharing_brewN)	Ú__name__Ú
__module__Ú__qualname__r   r   r   r+   r0   rD   r   r   r   r   r      s   r   )Zcaffe2.pythonr   r   r   Z(caffe2.python.modeling.parameter_sharingr   r   Z#caffe2.python.modeling.initializersr   ZunittestZTestCaser   r   r   r   r   Ú<module>   s   