U
    d#                     @   sL   d dl mZmZ d dlmZ d dlmZmZ d dlm	Z	 G dd de	Z
dS )    )corescope)ParameterSharing)AdagradOptimizerAdamOptimizer)LayersTestCasec                   @   sD   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S )ParameterSharingTestc              
   C   s   d}t d | j| jjj|}| | jjd jd | | d t dj | j||}| | jjd jd | | d | j||}| | jjd jd	 | | d
 W 5 Q R X W 5 Q R X d S )N   global_scopeglobal_scope/fc/wzglobal_scope/fc/outputZnested_scopezglobal_scope/nested_scope/fc/wz#global_scope/nested_scope/fc/outputz%global_scope/nested_scope/fc_auto_0/wz*global_scope/nested_scope/fc_auto_0/output)	r   	NameScopemodelFCinput_feature_schemafloat_featuresassertEqualslayersw)selfoutput_dims
fc1_output
fc2_outputZ
fc3_output r   N/tmp/pip-unpacked-wheel-ua33x9lu/caffe2/python/layer_parameter_sharing_test.pytest_layer_parameter_name   s:    z.ParameterSharingTest.test_layer_parameter_namec                 C   s   d}t d tddi t d> | j| jjj|}| | jjd j	d | | d W 5 Q R X t d> | j| jjj|}| | jjd j	d | | d W 5 Q R X W 5 Q R X W 5 Q R X d S )	Nr	   r
   scope_1scope_0r   zglobal_scope/scope_0/fc/wzglobal_scope/scope_0/fc/outputzglobal_scope/scope_1/fc/output
r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   5test_layer_shared_parameter_name_different_namescopes-   s2    zJParameterSharingTest.test_layer_shared_parameter_name_different_namescopesc              
   C   s   d}t dr tddiZ | j| jjj| | | jjd j	d | j| jjj| | | jjd j	d W 5 Q R X W 5 Q R X d S )Nr	   r
   	fc_auto_0fcr   r   r   r   r   r   r   r   6test_layer_shared_parameter_name_within_same_namescopeE   s"    zKParameterSharingTest.test_layer_shared_parameter_name_within_same_namescopec              
   C   s   d}t dz tddib | jj| jjj|dd | | jjd j	d | jj| jjj|dd | | jjd j	d W 5 Q R X W 5 Q R X d S )Nr	   r
   new_fc	shared_fcnamer   global_scope/shared_fc/wr   r"   r   r   r   Ftest_layer_shared_parameter_name_within_same_namescope_customized_nameW   s&    z[ParameterSharingTest.test_layer_shared_parameter_name_within_same_namescope_customized_namec                 C   s   d}t dx tddi` | j| jjj| | | jjd j	d | 
td | j| jjj|d  W 5 Q R X W 5 Q R X W 5 Q R X d S )	Nr	   r
   r    r!   r   r   zGot inconsistent shapes .*   )r   r   r   r   r   r   r   r   r   r   assertRaisesRegex
ValueErrorr"   r   r   r   1test_layer_shared_parameter_name_different_shapesk   s    zFParameterSharingTest.test_layer_shared_parameter_name_different_shapesc              
   C   s   d}t dN tddi6 | jj| jjj|dd | jj| jjj|dd W 5 Q R X W 5 Q R X td}td}| jj	D ]}|
|| q~g }|jjD ]}||j q| t|dd	g d S )
Nr	   r
   r$   r%   r&   train_init_net	train_netzglobal_scope/shared_fc/br(   )r   r   r   r   r   r   r   r   ZNetr   Zadd_operatorsZ_netopextendoutputr   sorted)r   r   r.   r/   ZlayerZ
op_outputsr0   r   r   r   $test_layer_duplicated_parameter_init|   s0    

z9ParameterSharingTest.test_layer_duplicated_parameter_initc                 C   s`  d}t ddd}|| j_td tddix td" | jj| jjj|| jj	d}W 5 Q R X td0 | 
t | j| jjj|}W 5 Q R X W 5 Q R X W 5 Q R X W 5 Q R X td	 tddi| td | jj| jjj|d
d}W 5 Q R X td8 | 
t" | jj| jjj|| jj	d}W 5 Q R X W 5 Q R X W 5 Q R X W 5 Q R X t ddd}t }|| j_td tddd td | jj| jjj|d
d}W 5 Q R X td4 | 
t | jj| jjj||d}W 5 Q R X W 5 Q R X td4 | 
t | jj| jjj||d}W 5 Q R X W 5 Q R X W 5 Q R X W 5 Q R X d
S )zo
        This test is to cover the _validate_param_optim function in
        layer_model_helper class.
        r	   gMbp?g{Gz?)alphaepsilonZglobal_scope_0r   r   )Zweight_optimZglobal_scope_1Ng{Gzt?Zglobal_scope_2)r   scope_2r7   )r   r   Zdefault_optimizerr   r   r   r   r   r   ZNoOptimassertRaises	Exceptionr   )r   r   Zadagrad_optimr   r   Zadagrad_optim_2Z
adam_optimr   r   r   +test_layer_shared_parameter_optim_validator   sv    ,.z@ParameterSharingTest.test_layer_shared_parameter_optim_validatorN)
__name__
__module____qualname__r   r   r#   r)   r-   r4   r:   r   r   r   r   r      s   r   N)Zcaffe2.pythonr   r   Z(caffe2.python.modeling.parameter_sharingr   Zcaffe2.python.optimizerr   r   Zcaffe2.python.layer_test_utilr   r   r   r   r   r   <module>   s   