o
    + i                     @  sx   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
 d
eZG dd deZdS )    )annotationsN)_C_ops   )core	framework)_current_expected_placein_dygraph_modein_dynamic_or_pir_mode   )Initializerc                      s2   e Zd ZdZdd fd
dZ	ddddZ  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                   s&   |d usJ t    || _|| _d S N)super__init___value
_force_cpu)selfr   r   	__class__ j/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/paddle/nn/initializer/constant.pyr   (   s   

zConstantInitializer.__init__Nvarpaddle.Tensorblockpaddle.pir.Block | Nonepaddle.Tensor | Nonec              	   C  s:  |  |}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 rzt }| jr0t }t rmt|tjr]| r]t|jt| j|j|}tjjj||j|j}|| dS t||jt| j|j| dS t|jt| j|j|S |j dd|i|jt!|jt| jt"t| j| jd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
        NZfill_constantZOut)shapedtyper   Z	str_valuer   T)typeZoutputsattrsZstop_gradient)$Z_check_block
isinstancer   VariableZEagerParamBasepaddleZpirValuer   ZParameterMetaZBlockr	   r   r   ZCPUPlacer   Zis_distr   fullZ_local_shaper   r   r$   distributedZauto_parallelapiZdtensor_from_localZprocess_meshZ
placementsZ_share_underline_tensor_toZfull_r#   Z	append_opintstrop)r   r   r    ZplaceZout_varr0   r   r   r   forward.   sZ   
	


zConstantInitializer.forward)r   F)r   r   r   r   r   r   r   )r   r   r    r!   r   r"   )__name__
__module____qualname____doc__r   r1   __classcell__r   r   r   r   r       s
    	r   c                      s$   e Zd ZdZd	d
 fddZ  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                   s$   |d u rt dt j|dd d S )Nzvalue must not be none.F)r   r   )
ValueErrorr   r   )r   r   r   r   r   r      s   zConstant.__init__)r   )r   r   r   r   )r2   r3   r4   r5   r   r6   r   r   r   r   r7   u   s    r7   )
__future__r   r)   r   baser   r   Zbase.frameworkr   r   r	   Zinitializerr   __all__r   r7   r   r   r   r   <module>   s   U