a
    ==ic*                     @   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   t/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/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rd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  