
    x-j\                         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
Z G d de
          ZdS )    N)_legacy_C_opsbase)LayerHelper)	ParamAttr)Layerinitializerresnet_basic_blockResNetBasicBlockFTc"                    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        d          }#|%                    | j        d          }(|%                    |&d          })|%                    |&d          }*||%                    |&d          n|}+||%                    |&d          n|},|%                    | j        d          }-|%                    | j        d          }.|%                    |&d          }/|%                    |&d          }0|	|%                    |&d          n|	}1|
|%                    |&d          n|
}2|%                    | j        d          }3|%                    |&d          }4|%                    |&d          }5||%                    |&d          n|}6||%                    |&d          n|}7|%                    |'d          }8|%                    |'d          }9|%                    |'d          }:|%                    |'d          };|%                    |'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:}?|%                    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XFilter1Scale1Bias1Mean1Var1Filter2Scale2Bias2Mean2Var2Filter3Scale3Bias3Mean3Var3)r   r   YConv1
SavedMean1SavedInvstd1Mean1OutVar1OutConv2
SavedMean2SavedInvstd2Mean2OutVar2Out
Conv2InputConv3
SavedMean3SavedInvstd3Mean3OutVar3Out)	MaxInput1
MaxFilter1	MaxInput2
MaxFilter2	MaxInput3
MaxFilter3)typeinputsoutputsattrs)r	   )r   	frameworkin_dygraph_moder   r	   r   localscoreVarDescVarTypeFP32"create_variable_for_type_inferencer    	append_op)@xfilter1scale1bias1mean1var1filter2scale2bias2mean2var2filter3scale3bias3mean3var3r   r   r   r   r   r   r   r   r   groupsr   epsr   r   r   trainingr   find_conv_maxrL   out_helperbn_param_dtype	max_dtypeconv1saved_mean1saved_invstd1running_mean1running_var1conv2conv2_inputsaved_mean2saved_invstd2running_mean2running_var2conv3saved_mean3saved_invstd3running_mean3running_var3conv1_input_maxconv1_filter_maxconv2_input_maxconv2_filter_maxconv3_input_maxconv3_filter_maxrJ   rK   s@                                                                   `/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddle/incubate/xpu/resnet_block.pyr	   r	      s   H ~%%'' ['
'
'
 '
 	'

 '
 '
 '
 '
 '
 '
 '
 '
 '
 '
 '
  !'
" #'
$ %'
& ''
( )'
* +'
, -'
. /'
0 1'
2 3'
4 5'
6 7'
8 9'
: ;'
< ='
> #?'
@ !A'
B C'
D LE'
F G'
H I'
J "K'
L M'
 '
B ,-
. /
 
 
1	
4 
::::FY&.3N	!).I

3
3gT 4  C 55gT 6  E ;;D <  K ==D >  M = 	11  	2 	
 	
 	
   < 	11  	2 	
 	
 	
   55gT 6  E ;;gT <  K ;;D <  K ==D >  M = 	11  	2 	
 	
 	
   < 	11  	2 	
 	
 	
   55gT 6  E ;;D <  K ==D >  M = 	11  	2 	
 	
 	
   < 	11  	2 	
 	
 	
   ??t @  O @@t A   ??t @  O @@t A   ??t @  O @@t A  Q7 	& 		
 	 	 	7 	& 	 	 	 	7 	& 	 	  	!F&77 	7 	H	
 	H 	H 	Y 	Y 	Y 	 	H 	3 	{ 	 	,  	 4!" 	x<#$ ,'  E,S 	k 		
 	M 	< 	 	k 	 	M 	< 	k 	 	k 	  	M!" 	<#$ %&$&$&/  G2 !&'     J    c                   l     e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd	Zd Z x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:: pycon

            >>> # 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)
            paddle.Size([2, 8, 16, 16])

       r   ?h㈵>NCHWFNr   Tc+                 	   t                                                       |
| _        || _        t          j                            |dd          | _        t          j                            |dd          | _        |&| _	        |'| _
        |#| _        |$| _        d| _        || _        || _        || _        || _        || _        || _        || _        |)| _        |*| _        |r<t          j                            |	dd          | _        |%| _        || _        |(| _        nd | _        d| _        d| _        d| _        dh}+||+vrt7          d|+ d|           d	 },t8          j        j        j        j         }-dd|g}.dd|g}/||||g}0||||g}1| !                    |0| |,|| j                  
          | _"        | !                    |.||-tG          j$        d                    | _%        | !                    |.||-d          | _&        | !                    tO          |tG          j$        d          d          |.|-          | _(        d| j(        _)        | !                    tO          |tG          j$        d          d          |.|-          | _*        d| j*        _)        | !                    |1| |,|| j                  
          | _+        | !                    |/||-tG          j$        d                    | _,        | !                    |/||-d          | _-        | !                    tO          |tG          j$        d          d          |/|-          | _.        d| j.        _)        | !                    tO          |tG          j$        d          d          |/|-          | _/        d| j/        _)        |rdd|g}2|||	|	g}3| !                    |3| |,|| j                  
          | _0        | !                    |2||-tG          j$        d                    | _1        | !                    |2| |-d          | _2        | !                    tO          |!tG          j$        d          d          |2|-          | _3        d| j3        _)        | !                    tO          |"tG          j$        d          d          |2|-          | _4        d| j4        _)        d S d | _0        d | _1        d | _2        d | _3        d | _4        d S )N   filter1_sizefilter2_sizer   filter3_sizer   zconv_format must be one of z, but got conv_format=c                 j    t          j        |          | z  }d|z  dz  }t          j        d|          S )Ng       @g      ?        )npprodINormal)channelskernel_sizefilter_elem_numstds       r   _get_default_param_initializerzAResNetBasicBlock.__init__.<locals>._get_default_param_initializer  s7     gk22X=O(S0C8C%%%r   )shapeattrdefault_initializerg      ?)r   r   r    r   T)r   r   r    is_biasr   F)namer   	trainable)r   r   r    )5super__init___stride1_stride2paddleutilsconvert_to_list_kernel1_size_kernel2_size
_dilation1
_dilation2	_padding1	_padding2_groups	_momentum_eps_data_format_act_has_shortcut_use_global_stats_is_test_trainable_statistics_find_conv_max_kernel3_size	_padding3_stride3
_dilation3
ValueErrorr   rP   rQ   rR   rS   create_parameterfilter_1r   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)5selfnum_channels1num_filter1r   num_channels2num_filter2r   num_channels3num_filter3r   r   r   r   actr   rg   r   r   r   r   filter1_attrscale1_attr
bias1_attrmoving_mean1_namemoving_var1_namefilter2_attrscale2_attr
bias2_attrmoving_mean2_namemoving_var2_namefilter3_attrscale3_attr
bias3_attrmoving_mean3_namemoving_var3_namer   r   r   r   r   r   r   ri   valid_formatr   rm   bn1_param_shapebn2_param_shapefilter1_shapefilter2_shapebn3_param_shapefilter3_shape	__class__s5                                                       r   r   zResNetBasicBlock.__init__  s   Z 	#\99!^
 
 $\99!^
 
 $#!!!	'	)!1%9"+ 	 !'!=!=a" "D &DN#DM'DOO!%DDNDMDO xl**_l__R]__  	& 	& 	& *27a-a-$m\<P$m\<P-- > >t1! ! . 
 
 ,,!  !
3	 - 
 
 ++! 	 , 
 
 ++&JsOO  
 "  , 
 
 %)!**%JsOO  
 "  + 
 

 $(
 -- > >t1! ! . 
 
 ,,!  !
3	 - 
 
 ++! 	 , 
 
 ++&JsOO  
 "  , 
 
 %)!**%JsOO  
 "  + 
 

 $(
  4	 ![1O	M !11#!$B$B!4#5% % 2  DM  00% $$%JsOO	 1  DL //%$	 0  DK //* !
3#  
 &$ 0  DK )-DK%..) !
3#  
 &$ /  DJ (,DJ$$$ DMDLDKDKDJJJr   c                     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   rh   r   ri   )"r	   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rh   r   r   )r   rV   rj   s      r   forwardzResNetBasicBlock.forward  s     MLKKJMLKKJMLKKJMMMNNNOOOLNI=#
> "3]!%!;-E#
 #
 #
H 
r   )!r   r   r   r   r   r   r   FFFNNNNNNNNNNNNNNNr   r   r   r   r   r   FT)__name__
__module____qualname____doc__r   r   __classcell__)r   s   @r   r
   r
   G  s        h hj "Wi i i i i iV% % % % % % %r   )NFFT)numpyr   r   r   r   paddle.base.layer_helperr   paddle.base.param_attrr   	paddle.nnr   r   r   __all__r	   r
    r   r   <module>r      s         & & & & & & & & 0 0 0 0 0 0 , , , , , ,       
  !3
4D Eg g g gT	{ { { { {u { { { { {r   