o
    + i,                     @  sx   d dl mZ d dlZd dlZd dlmZmZ ddlmZm	Z	m
Z
 ddlmZmZmZ ddlmZ g ZG d	d
 d
eZdS )    )annotationsN)_C_opspir   )core	frameworkunique_name)_current_expected_placein_dygraph_modein_pir_mode   )Initializerc                      s0   e Zd ZdZd fddZ	ddddZ  ZS )Bilinearae  
    This initializer can be used in transposed convolution operator to
    act as upsampling. Users can upsample a feature map with shape of
    (B, C, H, W) by any integer factor.

    Returns:
        Bilinear initializer instance objects.

    Examples:

        .. code-block:: python

            >>> import math

            >>> import paddle
            >>> import paddle.nn as nn
            >>> from paddle.regularizer import L2Decay

            >>> factor = 2
            >>> C = 2
            >>> B = 8
            >>> H = W = 32
            >>> w_attr = paddle.ParamAttr(learning_rate=0.,
            ...                           regularizer=L2Decay(0.),
            ...                           initializer=nn.initializer.Bilinear())
            >>> data = paddle.rand([B, 3, H, W], dtype='float32')
            >>> conv_up = nn.Conv2DTranspose(3,
            ...                              out_channels=C,
            ...                              kernel_size=2 * factor - factor % 2,
            ...                              padding=int(
            ...                                  math.ceil((factor - 1) / 2.)),
            ...                              stride=factor,
            ...                              weight_attr=w_attr,
            ...                              bias_attr=False)
            >>> x = conv_up(data)

    Where, `out_channels=C` and `groups=C` means this is channel-wise transposed
    convolution. The filter shape will be (C, 1, K, K) where K is `kernel_size`,
    This initializer will set a (K, K) interpolation kernel for every channel
    of the filter identically. The resulting shape of the output feature map
    will be (B, C, factor * H, factor * W). Note that the learning rate and the
    weight decay are set to 0 in order to keep coefficient values of bilinear
    interpolation unchanged during training.

    returnNonec                   s   t    dS )z$Constructor for BilinearInitializer.N)super__init__)self	__class__ j/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/paddle/nn/initializer/bilinear.pyr   P   s   zBilinear.__init__Nvarpaddle.Tensorblockpir.Block | Nonepaddle.Tensor | Nonec              	   C  sP  t |tjr| rJ d| |}t |tjtjjfs!t	dt |tj
tj
fs.t	d|j}t|dkr;t	d|d |d krGt	dtjt|jd	d
}|d }t|d }d| d |d  d|  }tt|D ]"}|| }	|| | }
dt|	| |  dt|
| |   ||< qrt||}|jtjjjtjjjtjjjfv rtjjj}|jtdd|jdg|j|tjjj dd}n|jtj!j"tj!j#tj!j$fv rtj!j%}|}n|j}|}|tjjjtj!j%fv rd}dd |j&D }nt'd|j t|dkrt	dt( rEt)*|t+|||t,  |jtjjjtjjjtjjjfv r>t)-||j}|.| dS |.| dS t/ rlt)0t+|||t, }|jtj!j"tj!j#tj!j$fv rjt)-||j}|S |j1dd|gid|dt+|||id}|jtjjjtjjjtjjjfv r|j1dd|id|i|j|jd d! ||_2|S )"a_  Initialize the input tensor with Bilinear initialization.

        Args:
            var(Tensor): Tensor that needs to be initialized.
            block(Block|None, optional): The block in which initialization ops
                   should be added. Used in static graph only, default None.

        Returns:
            The initialization op
        zECurrently, Bilinear initializer not support lazy init for dist param.z9var must be framework.Variable or pir.core.ParameterMeta.z+block must be framework.Block or pir.Block.   zthe length of shape must be 4.   r   z#shape[2] must be equal to shape[3].Zfloat32)dtypeg       @r   .Zbilinear_inittmpF)nameshaper   typeZpersistablevaluesc                 S  s   g | ]}t |qS r   )float).0vr   r   r   
<listcomp>   s    z$Bilinear.forward.<locals>.<listcomp>zUnsupported dtype i   zThe size of input is too big. Nassign_valueZOutr   r#   )r$   outputsattrscastX)Zin_dtype	out_dtype)r$   Zinputsr+   r,   )3
isinstancer   ZEagerParamBaseZis_distZ_check_blockVariabler   r   ZParameterMeta
ValueErrorZBlockr#   lennpZzerosprodceilrangeabsZreshaper   ZVarDescZVarTypeZFP16ZBF16ZFP64ZFP32Z
create_varr   generatejoinr"   ZDENSE_TENSORZDataTypeZFLOAT16ZBFLOAT16ZFLOAT64ZFLOAT32Zflat	TypeErrorr
   r   Zassign_value_listr	   r-   Z_share_underline_tensor_tor   r*   Z	append_opop)r   r   r   r#   weightsizefcixyr/   Zout_varZ
value_namer%   Zvar_tmpr=   r   r   r   forwardT   s   

.
	


zBilinear.forward)r   r   )N)r   r   r   r   r   r   )__name__
__module____qualname____doc__r   rE   __classcell__r   r   r   r   r   !   s
    .r   )
__future__r   numpyr4   Zpaddler   r   baser   r   r   Zbase.frameworkr	   r
   r   Zinitializerr   __all__r   r   r   r   r   <module>   s   