a
    –¥»a‹*  ã                   @   sz   d dl mZ d dlmZ d dlmZ d dlm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 dd„Zd	S )!é    )Úscope)ÚParameterTags)Ú
caffe2_pb2)ÚinitializersÚNCHWFc           
      K   s„   d|v r|d nt  ¡ }|du p*|jtjk}|rB|sBd|d< |}n|d| d g}| jj||fd|i|¤Ž}	|rx|sx|	S |	d S dS )	ÚLRNZdevice_optionNÚCUDNNÚengineÚ_Z_scaleÚorderr   )r   ZCurrentDeviceScopeZdevice_typer   ZCPUÚnetr   )
ÚmodelÚblob_inÚblob_outr   Ú	use_cudnnÚkwargsÚdevZis_cpuZ	blobs_outÚlrn© r   ú]/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/caffe2/python/helpers/normalization.pyr      s&    ÿþýür   Nc                 K   sB   |rd|d< |dur*| j j||fi |¤ŽS | j j|fi |¤ŽS dS )zSoftmax.r   r	   N)r   ZSoftmax)r   r   r   r   r   r   r   r   Úsoftmax%   s
    r   c                    sÄ   ˆ pˆj  ¡ ‰ ‡ ‡‡fdd„}|ddƒ|ddƒ }}ˆ |tj¡ ˆ |tj¡ ˆ ˆ d ˆ d g}	d	|v r˜|d	 r˜ˆj j|||gˆ gfd
|i|¤Ž}
|
S ˆj j|||g|	fd
|i|¤Ž}
|
d S d S )Nc                    s   ˆj jg ˆ d | ˆg| d�S )Nr
   )ÚshapeÚvalue)Zparam_init_netÚConstantFill)r   Úsuffix©r   Údim_inr   r   r   Ú	init_blob6   s    ÿz instance_norm.<locals>.init_blobç      ð?Úsç        ÚbÚ_smÚ_sivÚis_testr   r   )r   ÚNextNameZAddParameterr   ÚWEIGHTÚBIASZInstanceNorm)r   r   r   r   r   r   r   ÚscaleÚbiasÚ	blob_outsÚblob_outputsr   r   r   Úinstance_norm/   s.    ÿþþ
ÿþþr,   r   r    c                 K   sª  |p| j  ¡ }| jrŽdd|if}dd|if}dddif}dddif}t ||di f¡}t ||di f¡}t ||di f¡}t |	|di f¡}	n t ¡ }t ¡ }t ¡ }t ¡ }	| j|d |g|tjd�}| j|d |g|tj	d�}| j|d |g|tj
d�}| j|d	 |g|	tj
d�}||||d
 |d g}d|v �rr|d �rr| j j||||d |d g|gfd|
i|¤Ž}|S | j j||||d |d g|fd|
i|¤Ž}|d S d S )Nr   r   r    r   Ú_s©Ú
param_namer   ZinitializerÚtagsÚ_bÚ_rmÚ_rivr"   r#   r$   é   é   r   r   )r   r%   Úinit_paramsr   Úupdate_initializerÚExternalInitializerÚcreate_paramr   r&   r'   ÚCOMPUTED_PARAMZ	SpatialBN)r   r   r   r   Ú
init_scaleÚ	init_biasÚScaleInitializerÚBiasInitializerÚRunningMeanInitializerÚRunningVarianceInitializerr   r   Ú
scale_initÚ	bias_initÚrm_initÚriv_initr(   r)   Úrunning_meanÚrunning_inv_varr*   r+   r   r   r   Ú
spatial_bnK   s„    	
ÿ
ÿ
ÿ
ÿüüüüÿÿþþÿþþrG   c                 K   sÌ   |p| j  ¡ }| jrRdd|if}dd|if}t ||di f¡}t ||di f¡}nt ¡ }t ¡ }| j|d |g|tjd�}| j|d |g|tj	d�}||d |d g}| j j
|||g|fi |¤Ž}|d S )	zK
    Group normalizes the input, cf. https://arxiv.org/abs/1803.08494.
    r   r   r-   r.   r1   Ú_meanÚ_stdr   )r   r%   r6   r   r7   r8   r9   r   r&   r'   Z	GroupNorm)r   r   r   r   r;   r<   r=   r>   r?   r@   r   r   rA   rB   r(   r)   r*   r+   r   r   r   Ú
spatial_gnœ   sD    	
ÿ
ÿüüÿþýrJ   r4   ç-Cëâ6?c                 C   s¢   | j d |¡t|tƒr|gn|tjd|d�tjd�}| j d |¡t|tƒrP|gn|tjd|d�tjd�}	| j	j
|||	g||d |d g||dd	�\}
}}|
||fS )
a>  
    Layer normalizes the input, cf. https://arxiv.org/pdf/1607.06450.pdf.

    Args:
        blob_in: The input blob to layer normalize.
        blob_out: The layer normalized output blob.
        dim_in: The dimension of the scale and bias. For example, if blob_in is
            a 2D design matrix and axis is 1, this would be the number of
            columns.
        axis: (optional) The axis to normalize. Typically the feature axis.
            Defaults to 1.
        epsilon: (optional) A small value used for numerical stability in
            calculation. Defaults to 1e-4.
        initial_scale: (optional) The initial value for the learned scale
            parameter. Defaults to 1.0
        initial_bias: (optional) The initial value for the learned bias
            parameter of the layerwise standard deviation. Defaults to 0.0.

    Returns:
        A 3-tuple consisting of:
            - The layer normalized input blob.
            - The mean of the input blob across the given axis.
            - The standard deviation of the input blob acress the given axis.
    z{}_scaler   )r   r.   z{}_biasrH   rI   T)ÚaxisÚepsilonZelementwise_affine)r9   ÚformatÚ
isinstanceÚintr   ZInitializerr   r&   r'   r   Z	LayerNorm)r   r   r   r   rL   rM   Zinitial_scaleZinitial_biasr(   r)   Ú
normalizedÚmeanZstdr   r   r   Ú
layer_normÓ   s4    $þùþù
ûrS   c                 K   sö   | j rDdddif}dddif}	t ||di f¡}t ||	di f¡}nt ¡ }t ¡ }| j|d |g|tjd�}
| j|d |g|tjd�}|d |d	 g}|d
krÆ| jj|g|fg d¢|ddœ|¤Ž}n,|dkrò| jj|g|fg d¢|ddœ|¤Ž}|S )Nr   r   r    r   r2   r.   r3   r"   Z_svr   )r   r5   é   F)Zaxesr   ZkeepdimsZNHWC)r   r4   r5   )	r6   r   r7   r8   r9   r   r:   r   ZMoments)r   r   r   r   r?   r@   r   r   rC   rD   rE   rF   r*   r+   r   r   r   Úmoments_with_running_stats  sV    
ÿ
ÿüüÿýýÿýýrU   )r   F)NF)r   )r   r    NNNNr   )r   r    NNNNr   )r4   rK   r   r    )NNr   )Zcaffe2.pythonr   Z%caffe2.python.modeling.parameter_infor   Zcaffe2.protor   Zcaffe2.python.modelingr   r   r   r,   rG   rJ   rS   rU   r   r   r   r   Ú<module>   s0   



    ü
R    ü
<    ø
D  þ