
    {-j,                        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
e          ZdS )    )annotationsN)_C_opspir   )core	frameworkunique_name)_current_expected_placein_dygraph_modein_pir_mode   )Initializerc                  0     e Zd ZdZd fdZ	 dddZ x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                H    t                                                       dS )z$Constructor for BilinearInitializer.N)super__init__)self	__class__s    ^/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddle/nn/initializer/bilinear.pyr   zBilinear.__init__P   s        Nvarpaddle.Tensorblockpir.Block | Nonepaddle.Tensor | Nonec           
     
   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    rt          d          t          j        t          j        |j                  d	
          }|d         }t          j        |dz            }d|z  dz
  |dz  z
  d|z  z  }t#          t          j        |                    D ]C}||z  }	||z  |z  }
dt%          |	|z  |z
            z
  dt%          |
|z  |z
            z
  z  ||<   Dt          j        ||          }|j        t          j        j        j        t          j        j        j        t          j        j        j        fv rwt          j        j        j        }|                    t9          j        d                    d|j        dg                    |j        |t          j        j        j         d          }nV|j        t          j!        j"        t          j!        j#        t          j!        j$        fv rt          j!        j%        }|}n	|j        }|}|t          j        j        j        t          j!        j%        fv rd}d |j&        D             }ntO          d|j                   t          j        |          dk    rt          d          tQ                      rtS          j*        |tW          |          ||tY                                 |j        t          j        j        j        t          j        j        j        t          j        j        j        fv r0tS          j-        ||j                  }|.                    |           n|.                    |           dS t_                      rtS          j0        tW          |          ||tY                                }|j        t          j!        j"        t          j!        j#        t          j!        j$        fv rtS          j-        ||j                  }|S |1                    dd|gid|dtW          |          ||i          }|j        t          j        j        j        t          j        j        j        t          j        j        j        fv r*|1                    dd|id|i|j        |j        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].float32)dtypeg       @r   .bilinear_inittmpF)nameshaper#   typepersistablevaluesc                ,    g | ]}t          |          S  )float).0vs     r   
<listcomp>z$Bilinear.forward.<locals>.<listcomp>   s    4441eAhh444r   zUnsupported dtype i   zThe size of input is too big. Nassign_valueOutr#   r(   )r)   outputsattrscastX)in_dtype	out_dtype)r)   inputsr4   r5   )3
isinstancer   EagerParamBaseis_dist_check_blockVariabler   r   ParameterMeta
ValueErrorBlockr(   lennpzerosprodceilrangeabsreshaper#   VarDescVarTypeFP16BF16FP64FP32
create_varr	   generatejoinr'   DENSE_TENSORDataTypeFLOAT16BFLOAT16FLOAT64FLOAT32flat	TypeErrorr   r   assign_value_listr
   r6   _share_underline_tensor_tor   r2   	append_opop)r   r   r   r(   weightsizefcixyr9   out_var
value_namer+   var_tmpr`   s                    r   forwardzBilinear.forwardT   s6    sI455	
:=++--	
 	
 T	
 	
 

 !!%((#	 2CH4JKLL 	K   %)/39!=>> 	LJKKK	u::??=>>>8uQxBCCC"'#),,I>>>QxGD3JUQYQ37+rwu~~&& 	D 	DADATT!ASQ^^+CA	NN0BCF1IIFE** 9L %L %L %
 
 

 ,1I&& )HHosx?@@  i\)6! '  GG YM!M"M!
 
 

 -IGG	IG-2DM4IJJJ!J44444FF<<<===75>>K''=>>> 8	 U'))   y$)$)$)  
 !+gsy9922377772237774]] &	)U'))	 G y%&%  
 !+gsy99N#	*YT%[[ !  B y$)$)$)  
 >"CL'.}39MM	      CFIr   )r   r   )N)r   r   r   r   r   r   )__name__
__module____qualname____doc__r   rk   __classcell__)r   s   @r   r   r   !   sp        , ,\     
 =AJ J J J J J J J Jr   r   )
__future__r   numpyrD   paddler   r   baser   r   r	   base.frameworkr
   r   r   initializerr   __all__r   r-   r   r   <module>rx      s    # " " " " "              0 0 0 0 0 0 0 0 0 0         
 % $ $ $ $ $
} } } } }{ } } } } }r   