o
    pi\                     @   sv   d dl Zd dlZd dlmZmZ d dlmZ d dlmZ d dl	m
Z
mZ ddgZ					dd
dZG dd de
ZdS )    N)_legacy_C_opsbase)LayerHelper)	ParamAttr)Layerinitializerresnet_basic_blockResNetBasicBlockFTc"           @      C   sH  t j rg d|d|d|d|d|d|d|d|d	|d
|d|d|d|d|d|d| d| ddd|!R }"tj| |||||||||	|
||||||||	|
||g|"R  \}#}$}$}$}$}$}$}$}$}$}$}$}$}$}$}$}$}$}$}$}$}$}$|#S td<i t }%t jjj	j
}&t jjj	j
}'|%j| jdd}#|%j| jdd}(|%j|&dd})|%j|&dd}*|d u r|%j|&ddn|}+|d u r|%j|&ddn|},|%j| jdd}-|%j| jdd}.|%j|&dd}/|%j|&dd}0|	d u r|%j|&ddn|	}1|
d u r|%j|&ddn|
}2|%j| jdd}3|%j|&dd}4|%j|&dd}5|d u r4|%j|&ddn|}6|d u rB|%j|&ddn|}7|%j|'dd}8|%j|'dd}9|%j|'dd}:|%j|'dd};|%j|'dd}<|%j|'dd}=i d| d|d|d|d|d|d|d|d |d!|	d"|
d#|d$|d%|d&|d'|}>i d|d|d|d|d|d|d|d|d	|d
|d|d|d|d|d|d| d| d|!d(}"i d)|#d*|(d+|)d,|*d-|+d.|,d/|-d0|/d1|0d2|1d3|2d4|.d5|3d6|4d7|5d8|6d9|7|8|9|:|;|<|=d:}?|%jd|>|?|"d; |#S )=Nstride1stride2stride3padding1padding2padding3	dilation1	dilation2	dilation3groupmomentumepsilondata_formathas_shortcutuse_global_statstrainable_statisticsis_testact_typerelufind_conv_input_maxr   T)dtypestop_gradientXZFilter1ZScale1ZBias1ZMean1ZVar1ZFilter2ZScale2ZBias2ZMean2ZVar2ZFilter3ZScale3ZBias3ZMean3ZVar3)r   r   YZConv1Z
SavedMean1ZSavedInvstd1ZMean1OutZVar1OutZConv2Z
SavedMean2ZSavedInvstd2ZMean2OutZVar2OutZ
Conv2InputZConv3Z
SavedMean3ZSavedInvstd3ZMean3OutZVar3Out)Z	MaxInput1Z
MaxFilter1Z	MaxInput2Z
MaxFilter2Z	MaxInput3Z
MaxFilter3)typeinputsoutputsattrs)r   )r   Z	frameworkZin_dygraph_moder   r   r   localscoreVarDescVarTypeFP32Z"create_variable_for_type_inferencer   Z	append_op)@xZfilter1Zscale1Zbias1Zmean1Zvar1Zfilter2Zscale2Zbias2Zmean2Zvar2Zfilter3Zscale3Zbias3Zmean3Zvar3r
   r   r   r   r   r   r   r   r   groupsr   epsr   r   r   trainingr   find_conv_maxr%   out_helperbn_param_dtypeZ	max_dtypeZconv1Zsaved_mean1Zsaved_invstd1Zrunning_mean1Zrunning_var1Zconv2Zconv2_inputZsaved_mean2Zsaved_invstd2Zrunning_mean2Zrunning_var2Zconv3Zsaved_mean3Zsaved_invstd3Zrunning_mean3Zrunning_var3Zconv1_input_maxZconv1_filter_maxZconv2_input_maxZconv2_filter_maxZconv3_input_maxZconv3_filter_maxr#   r$    r4   g/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/paddle/incubate/xpu/resnet_block.pyr      s  
$	
 !"#$%&A



	
	
	
c                       sl   e Zd ZdZ																																				
d fdd	Zdd Z  ZS )r	   a  

    ResNetBasicBlock is designed for optimize the performance of the basic unit of ssd resnet block.
    If has_shortcut = True, it can calculate 3 Conv2D, 3 BatchNorm and 2 ReLU in one time.
    If has_shortcut = False, it can calculate 2 Conv2D, 2 BatchNorm and 2 ReLU in one time. In this
    case the shape of output is same with input.


    Args:
        num_channels (int): The number of input image channel.
        num_filter (int): The number of filter. It is as same as the output image channel.
        filter_size (int|list|tuple): The filter size. If filter_size
            is a tuple, it must contain two integers, (filter_size_height,
            filter_size_width). Otherwise, filter_size_height = filter_size_width =\
            filter_size.
        stride (int, optional): The stride size. It means the stride in convolution.
            If stride is a tuple, it must contain two integers, (stride_height, stride_width).
            Otherwise, stride_height = stride_width = stride. Default: stride = 1.
        act (str, optional): Activation type, if it is set to None, activation is not appended.
            Default: None
        momentum (float, optional): The value used for the moving_mean and
            moving_var computation. This should be a float number or a 0-D Tensor with
            shape [] and data type as float32. The updated formula is:
            :math:`moving\_mean = moving\_mean * momentum + new\_mean * (1. - momentum)`
            :math:`moving\_var = moving\_var * momentum + new\_var * (1. - momentum)`
            Default is 0.9.
        eps (float, optional): A value added to the denominator for
            numerical stability. Default is 1e-5.
        data_format (str, optional): Specify the data format of the input, and the data format of the output
            will be consistent with that of the input. Now is only support `"NCHW"`, the data is stored in
            the order of: `[batch_size, input_channels, input_height, input_width]`.
        has_shortcut (bool, optional): Whether to calculate CONV3 and BN3. Default: False.
        use_global_stats (bool, optional): Whether to use global mean and
            variance. In inference or test mode, set use_global_stats to true
            or is_test to true, and the behavior is equivalent.
            In train mode, when setting use_global_stats True, the global mean
            and variance are also used during train period. Default: False.
        is_test (bool, optional): A flag indicating whether it is in
            test phrase or not. Default: False.
        filter_attr (ParamAttr, optional): The parameter attribute for learnable parameters/weights
            of conv2d. If it is set to None or one attribute of ParamAttr, conv2d
            will create ParamAttr as param_attr. Default: None.
        scale_attr (ParamAttr, optional): The parameter attribute for Parameter `scale`
            of batch_norm. If it is set to None or one attribute of ParamAttr, batch_norm will create ParamAttr
            as param_attr, the name of scale can be set in ParamAttr. If the Initializer of the param_attr is not set,
            the parameter is initialized with Xavier. Default: None.
        bias_attr (ParamAttr, optional): The parameter attribute for the bias of batch_norm.
            If it is set to None or one attribute of ParamAttr, batch_norm
            will create ParamAttr as bias_attr, the name of bias can be set in ParamAttr.
            If the Initializer of the bias_attr is not set, the bias is initialized zero.
            Default: None.
        moving_mean_name (str, optional): The name of moving_mean which store the global Mean. If it
            is set to None, batch_norm will save global mean with a random name, otherwise, batch_norm
            will save global mean with the string. Default: None.
        moving_var_name (str, optional): The name of the moving_variance which store the global Variance.
            If it is set to None, batch_norm will save global variance with a random name, otherwise, batch_norm
            will save global variance with the string. Default: None.
        padding (int, optional): The padding size. It is only support padding_height = padding_width = padding.
            Default: padding = 0.
        dilation (int, optional): The dilation size. It means the spacing between the kernel
            points. It is only support dilation_height = dilation_width = dilation.
            Default: dilation = 1.
        trainable_statistics (bool, optional): Whether to calculate mean and var in eval mode. In eval mode, when
            setting trainable_statistics True, mean and variance will be calculated by current batch statistics.
            Default: False.
        find_conv_max (bool, optional): Whether to calculate max value of each conv2d. Default: True.


    Returns:
        A Tensor representing the ResNetBasicBlock, whose data type is the same with input.


    Examples:
        .. code-block:: python

            >>> # doctest: +REQUIRES(env: XPU)
            >>> import paddle
            >>> from paddle.incubate.xpu.resnet_block import ResNetBasicBlock

            >>> ch_in = 4
            >>> ch_out = 8
            >>> x = paddle.uniform((2, ch_in, 16, 16), dtype='float32', min=-1., max=1.)
            >>> resnet_basic_block = ResNetBasicBlock(num_channels1=ch_in,
            ...                                     num_filter1=ch_out,
            ...                                     filter1_size=3,
            ...                                     num_channels2=ch_out,
            ...                                     num_filter2=ch_out,
            ...                                     filter2_size=3,
            ...                                     num_channels3=ch_in,
            ...                                     num_filter3=ch_out,
            ...                                     filter3_size=1,
            ...                                     stride1=1,
            ...                                     stride2=1,
            ...                                     stride3=1,
            ...                                     act='relu',
            ...                                     padding1=1,
            ...                                     padding2=1,
            ...                                     padding3=0,
            ...                                     has_shortcut=True)
            >>> out = resnet_basic_block.forward(x)

            >>> print(out.shape)
            [2, 8, 16, 16]

       r   ?h㈵>NCHWFNr   Tc+           4         sH  t    |
| _|| _tj|dd| _tj|dd| _|&| _	|'| _
|#| _|$| _d| _|| _|| _|| _|| _|| _|| _|| _|)| _|*| _|r\tj|	dd| _|%| _|| _|(| _nd | _d| _d| _d| _dh}+||+vrytd|+ d| d	d
 },tjjjj }-dd|g}.dd|g}/||||g}0||||g}1| j!|0||,|| jd| _"| j!|.||-t#$dd| _%| j!|.||-dd| _&| j!t'|t#$ddd|.|-d| _(d| j(_)| j!t'|t#$ddd|.|-d| _*d| j*_)| j!|1||,|| jd| _+| j!|/||-t#$dd| _,| j!|/||-dd| _-| j!t'|t#$ddd|/|-d| _.d| j._)| j!t'|t#$ddd|/|-d| _/d| j/_)|rdd|g}2|||	|	g}3| j!|3||,|| jd| _0| j!|2||-t#$dd| _1| j!|2| |-dd| _2| j!t'|!t#$ddd|2|-d| _3d| j3_)| j!t'|"t#$ddd|2|-d| _4d| j4_)d S d | _0d | _1d | _2d | _3d | _4d S )N   filter1_sizefilter2_sizer6   filter3_sizer9   zconv_format must be one of z, but got conv_format=c                 S   s&   t ||  }d| d }td|S )Ng       @g      ?        )npprodIZNormal)ZchannelsZkernel_sizeZfilter_elem_numZstdr4   r4   r5   _get_default_param_initializer  s   zAResNetBasicBlock.__init__.<locals>._get_default_param_initializer)shapeattrdefault_initializerg      ?)rC   rD   r   rE   T)rC   rD   r   Zis_biasr>   F)namer   Z	trainable)rD   rC   r   )5super__init___stride1_stride2paddleutilsZconvert_to_listZ_kernel1_sizeZ_kernel2_size
_dilation1
_dilation2	_padding1	_padding2_groups	_momentum_eps_data_formatZ_act_has_shortcut_use_global_statsZ_is_test_trainable_statistics_find_conv_maxZ_kernel3_size	_padding3_stride3
_dilation3
ValueErrorr   r'   r(   r)   r*   Zcreate_parameterfilter_1rA   Constantscale_1bias_1r   mean_1r   var_1filter_2scale_2bias_2mean_2var_2filter_3scale_3bias_3mean_3var_3)4selfZnum_channels1Znum_filter1r;   Znum_channels2Znum_filter2r<   Znum_channels3Znum_filter3r=   r
   r   r   Zactr   r-   r   r   r   r   Zfilter1_attrZscale1_attrZ
bias1_attrZmoving_mean1_nameZmoving_var1_nameZfilter2_attrZscale2_attrZ
bias2_attrZmoving_mean2_nameZmoving_var2_nameZfilter3_attrZscale3_attrZ
bias3_attrZmoving_mean3_nameZmoving_var3_namer   r   r   r   r   r   r   r/   Zvalid_formatrB   r3   Zbn1_param_shapeZbn2_param_shapeZfilter1_shapeZfilter2_shapeZbn3_param_shapeZfilter3_shape	__class__r4   r5   rH     s\  
-

				
		
zResNetBasicBlock.__init__c                 C   s   t || j| j| j| j| j| j| j| j| j	| j
| j| j| j| j| j| j| j| j| j| j| j| j| j| j| j| j| j| j| jf| j| j| j | j!d}|S )N)r   r.   r   r/   )"r   r]   r_   r`   ra   rb   rc   rd   re   rf   rg   rh   ri   rj   rk   rl   rI   rJ   rZ   rO   rP   rY   rM   rN   r[   rQ   rR   rS   rT   rU   rV   r.   rW   rX   )rm   r+   r0   r4   r4   r5   forward  sL   $zResNetBasicBlock.forward)!r6   r6   r6   r   r7   r8   r9   FFFNNNNNNNNNNNNNNNr   r   r   r6   r6   r6   FT)__name__
__module____qualname____doc__rH   rp   __classcell__r4   r4   rn   r5   r	   G  sL    u l)NFFT)numpyr?   rK   r   r   Zpaddle.base.layer_helperr   Zpaddle.base.param_attrr   Z	paddle.nnr   r   rA   __all__r   r	   r4   r4   r4   r5   <module>   s   "
  ,