
    ϑi\                     |    S SK rS SKrS SKJrJr  S SKJr  S SKJr  S SK	J
r
Jr  SS/r    S
S jr " S	 S\
5      rg)    N)_legacy_C_opsbase)LayerHelper)	ParamAttr)Layerinitializerresnet_basic_blockResNetBasicBlockc"                 `   [         R                  R                  5       (       a  / SPUPSPUPSPUPSPUPSPUPSPUPSPUPSPUPS	PUPS
PUPSPUPSPUPSPUPSPUPSPUPSPU PSPU(       + PSPSPSPU!P7n"[        R                  " U UUUUUUUUU	U
UUUUUUUU	U
UU/U"Q76 u  n#n$n$n$n$n$n$n$n$n$n$n$n$n$n$n$n$n$n$n$n$n$n$U#$ [        S<0 [        5       D6n%[         R                  R                  R                  R                  n&[         R                  R                  R                  R                  n'U%R                  U R                  SS9n#U%R                  U R                  SS9n(U%R                  U&SS9n)U%R                  U&SS9n*Uc  U%R                  U&SS9OUn+Uc  U%R                  U&SS9OUn,U%R                  U R                  SS9n-U%R                  U R                  SS9n.U%R                  U&SS9n/U%R                  U&SS9n0U	c  U%R                  U&SS9OU	n1U
c  U%R                  U&SS9OU
n2U%R                  U R                  SS9n3U%R                  U&SS9n4U%R                  U&SS9n5Uc  U%R                  U&SS9OUn6Uc  U%R                  U&SS9OUn7U%R                  U'SS9n8U%R                  U'SS9n9U%R                  U'SS9n:U%R                  U'SS9n;U%R                  U'SS9n<U%R                  U'SS9n=0 SU _SU_SU_SU_SU_SU_SU_SU_S U_S!U	_S"U
_S#U_S$U_S%U_S&U_S'U_n>0 SU_SU_SU_SU_SU_SU_SU_SU_S	U_S
U_SU_SU_SU_SU_SU_SU _SU(       + _SU!S(.En"0 S)U#_S*U(_S+U)_S,U*_S-U+_S.U,_S/U-_S0U/_S1U0_S2U1_S3U2_S4U._S5U3_S6U4_S7U5_S8U6_S9U7_U8U9U:U;U<U=S:.En?U%R                  SU>U?U"S;9  U#$ )=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.13/site-packages/paddle/incubate/xpu/resnet_block.pyr	   r	      sC   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YY&&..33N		!!))..I

3
3ggT 4 C 55ggT 6 E ;;D < K ==D > M = 	11  	2 	
   < 	11  	2 	
   55ggT 6 E ;;ggT < K ;;D < K ==D > M = 	11  	2 	
   < 	11  	2 	
   55ggT 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                   x   ^  \ rS rSrSr                                 SU 4S jjrS rSrU =r$ )r
   iG  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])

c+                 	  > [         T4U ]  5         Xl        Xl        [        R
                  R                  USS5      U l        [        R
                  R                  USS5      U l        U&U l	        U'U l
        U#U l        U$U l        SU l        Xl        Xl        UU l        Xl        UU l        UU l        UU l        U)U l        U*U l        U(       a;  [        R
                  R                  U	SS5      U l        U%U l        Xl        U(U l        OS U l        SU l        SU l        SU l        S1n+UU+;  a  [7        SU+ SU 35      eS	 n,[8        R:                  R<                  R>                  R@                  n-SSU/n.SSU/n/X!X3/n0XTXf/n1U RC                  U0UU," XR                  5      S
9U l"        U RC                  U.UU-[F        RH                  " S5      S9U l%        U RC                  U.UU-SS9U l&        U RC                  [O        U[F        RH                  " S5      SS9U.U-S9U l(        SU RP                  l)        U RC                  [O        U[F        RH                  " S5      SS9U.U-S9U l*        SU RT                  l)        U RC                  U1UU," X@R                  5      S
9U l+        U RC                  U/UU-[F        RH                  " S5      S9U l,        U RC                  U/UU-SS9U l-        U RC                  [O        U[F        RH                  " S5      SS9U/U-S9U l.        SU R\                  l)        U RC                  [O        U[F        RH                  " S5      SS9U/U-S9U l/        SU R^                  l)        U(       a  SSU/n2UUU	U	/n3U RC                  U3UU," XpR.                  5      S
9U l0        U RC                  U2UU-[F        RH                  " S5      S9U l1        U RC                  U2U U-SS9U l2        U RC                  [O        U![F        RH                  " S5      SS9U2U-S9U l3        SU Rf                  l)        U RC                  [O        U"[F        RH                  " S5      SS9U2U-S9U l4        SU Rh                  l)        g S U l0        S U l1        S U l2        S U l3        S U l4        g )N   filter1_sizefilter2_size   filter3_sizeNCHWzconv_format must be one of z, but got conv_format=c                 r    [         R                  " U5      U -  nSU-  S-  n[        R                  " SU5      $ )Ng       @g      ?        )npprodINormal)channelskernel_sizefilter_elem_numstds       r   _get_default_param_initializerAResNetBasicBlock.__init__.<locals>._get_default_param_initializer  s5     ggk2X=O(S0C88C%%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   ResNetBasicBlock.__init__  s   Z 	#\\99!^
 $\\99!^
 $#!!!	'	)!1%9"+!'!=!=a"D &DN#M'DO!%DDNDMDO xl*-l^;QR]Q^_ 	& **2277a-a-$\P$\P-- >11! . 
 ,,!  !

3	 - 
 ++! 	 , 
 ++&JJsO
 "  , 
 %)!**%JJsO
 "  + 

 $(

 -- >11! . 
 ,,!  !

3	 - 
 ++! 	 , 
 ++&JJsO
 "  , 
 %)!**%JJsO
 "  + 

 $(

  ![1O	M !11#!$B!#5#5% 2 DM  00% $$%JJsO	 1 DL //%$	 0 DK //* !

3#
 &$ 0 DK )-DKK%..) !

3#
 &$ / DJ (,DJJ$ DMDLDKDKDJr   c                     [        UU R                  U R                  U R                  U R                  U R
                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                   U R"                  U R$                  U R&                  U R(                  U R*                  U R,                  U R.                  U R0                  U R2                  U R4                  U R6                  U R8                  U R:                  4U R<                  U R>                  U R@                  U RB                  S.6nU$ )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   forwardResNetBasicBlock.forward  s    MMLLKKKKJJMMLLKKKKJJMMLLKKKKJJMMMMMMNNNNNNOOOOOOLLNNII=#
> "33]]!%!;!;--E#
H 
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   r   r   r   r   r   r   r   )!r   r   r   r   g?gh㈵>r   FFFNNNNNNNNNNNNNNNr   r   r   r   r   r   FT)	__name__
__module____qualname____firstlineno____doc__r   r   __static_attributes____classcell__)r   s   @r   r
   r
   G  s    hj "W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      sM      & 0 ,
  !3
4D EgT	{u {r   