
    {-j                        d dl mZ d dlZd dlmZ ddl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 G d de          ZdS )    )annotationsN)_C_ops   )core	framework)_current_expected_placein_dygraph_modein_dynamic_or_pir_mode   )Initializerc                  2     e Zd ZdZdd fd
Z	 dddZ xZS )ConstantInitializerzImplements the constant initializer

    Args:
        value (float32, optional): constant value to initialize the variable. Default: 0.0.

            Fvaluefloat	force_cpuboolreturnNonec                l    |J t                                                       || _        || _        d S N)super__init___value
_force_cpu)selfr   r   	__class__s      ^/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddle/nn/initializer/constant.pyr   zConstantInitializer.__init__(   s7       #    Nvarpaddle.Tensorblockpaddle.pir.Block | Nonepaddle.Tensor | Nonec                   |                      |          }t          |t          j        t          j        t
          j        j        t
          j        j        j	        f          sJ t          |t          j
        t
          j        j
        f          sJ t                      rKt                      }| j        rt          j                    }t                      rt          |t          j                  r|                                r~t#          j        |j        t)          | j                  |j        |          }t
          j        j        j                            ||j        |j                  }|                    |           n4t#          j        ||j        t)          | j                  |j        |           dS t#          j        |j        t)          | j                  |j        |          S |                     dd|i|j        tC          |j                  t)          | j                  tE          t)          | j                            | j        dd          }||_#        |S )aK  Initialize the input tensor with constant.

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

        Returns:
            The initialization op
        Nfill_constantOut)shapedtyper   	str_valuer   T)typeoutputsattrsstop_gradient)$_check_block
isinstancer   VariableEagerParamBasepaddlepirValuer   ParameterMetaBlockr
   r   r   CPUPlacer	   is_distr   full_local_shaper   r   r)   distributedauto_parallelapidtensor_from_localprocess_mesh
placements_share_underline_tensor_tofull_r(   	append_opintstrop)r   r    r"   placeout_varrG   s         r   forwardzConstantInitializer.forward.   s%     !!%(("(
 
-	
 
 	
 	
 
 %)/6:3C!DEEEEE!## '	+--E (   c9#;<<  $k(%*<*<ci G *8<OO#S%5s~  
 66s;;;;LSYdk(:(:CIu   t{IuT[1139e   $ Y ^^"4;//!$U4;%7%7!8!8!%  # !  B CFIr   )r   F)r   r   r   r   r   r   r   )r    r!   r"   r#   r   r$   )__name__
__module____qualname____doc__r   rJ   __classcell__r   s   @r   r   r       ss         $ $ $ $ $ $ $ *.D D D D D D D D Dr   r   c                  &     e Zd ZdZdd	 fdZ xZS )
Constanta  Implement the constant initializer.

    Args:
        value (float32|float64, optional): constant value to initialize the parameter. Default: 0.0.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> import paddle.nn as nn

            >>> paddle.seed(2023)
            >>> data = paddle.rand([30, 10, 2], dtype='float32')
            >>> linear = nn.Linear(2,
            ...                     4,
            ...                     weight_attr=nn.initializer.Constant(value=2.0))
            >>> res = linear(data)
            >>> print(linear.weight)
            Parameter containing:
            Tensor(shape=[2, 4], dtype=float32, place=Place(cpu), stop_gradient=False,
            [[2., 2., 2., 2.],
             [2., 2., 2., 2.]])

    r   r   r   r   r   c                p    |t          d          t                                          |d           d S )Nzvalue must not be none.F)r   r   )
ValueErrorr   r   )r   r   r   s     r   r   zConstant.__init__   s:    =6777u66666r   )r   )r   r   r   r   )rK   rL   rM   rN   r   rO   rP   s   @r   rR   rR   u   sL         27 7 7 7 7 7 7 7 7 7 7r   rR   )
__future__r   r3   r   baser   r   base.frameworkr   r	   r
   initializerr   __all__r   rR    r   r   <module>r[      s    # " " " " "        # # # # # # # #          % $ $ $ $ $
R R R R R+ R R Rj7 7 7 7 7" 7 7 7 7 7r   