
    {-j&                        d dl mZ d dlmZmZ d dlZd dlmZ ddlmZm	Z	m
Z
 ddlmZ ddlmZmZmZ d	d
lmZ erd dlmZ d dlmZ g Z G d de          Z G d de          ZdS )    )annotations)TYPE_CHECKINGAnyN)_C_ops   )core	frameworkunique_name)
check_type)_current_expected_placein_dygraph_modein_pir_mode   )Initializer)Sequencec                  0     e Zd ZdZd fdZ	 dddZ xZS )NumpyArrayInitializerzInit an parameter with an numpy array
    This api initialize the tensor by numpy array.

    Args:
        value (numpy): numpy array to initialize the tensor

    Returns:
        A Tensor initialized by numpy.

    valuenpt.NDArray[Any]returnNonec                    dd l }t          ||j                  sJ t                                                       || _        d S )Nr   )numpy
isinstancendarraysuper__init___value)selfr   r   	__class__s      \/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddle/nn/initializer/assign.pyr   zNumpyArrayInitializer.__init__3   sE    %/////    Nvarpaddle.Tensorblockpaddle.pir.Block | Nonepaddle.Tensor | Nonec           
     (
   t          |t          j                  r|                                r
J d            |                     |          }t          |t          j        t          j        j        j	        f          sJ t          |t          j
        t          j        j
        f          sJ |j        }|t          j        j        j        t          j        j        j        fv rt          j        j        j        }| j                            d          }|                    t)          j        d                    d|j        dg                    |j        |t          j        j        j        d          }n]|t          j        j        t          j        j        fv r.|}t          j        j        }| j                            d          }n|}|}| j        }|t          j        j        j        t          j        j        fv rd}d	 |j        D             }n|t          j        j        j        t          j        j         fv rd}d
 |j        D             }n|t          j        j        j!        t          j        j!        fv rd}d |j        D             }n~|t          j        j        j"        t          j        j        j#        t          j        j"        t          j        j#        fv rd}d |j        D             }ntI          d| j        j                   | j        j%        dk    rtI          d          tM                      rtO          j(        |tS          | j        j                  ||tU                                 |t          j        j        j        t          j        j        j        t          j        j        t          j        j        fv r+tO          j+        ||          }	|	,                    |           n|,                    |           dS t[                      rutO          j.        tS          | j        j                  ||tU                                }|t          j        j        t          j        j        fv rtO          j+        ||          }|S |/                    dd|id|dtS          | j        j                  ||id          }
|t          j        j        j        t          j        j        j        fv r%|/                    dd|id|i|j        |d           |
|_0        |
S )aS  Initialize the input tensor with Numpy array.

        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
        zCCurrently, assign initializer not support lazy init for dist param.float32.numpy_array_inittmpF)nameshapedtypetypepersistablevaluesc                ,    g | ]}t          |          S  float.0vs     r!   
<listcomp>z1NumpyArrayInitializer.forward.<locals>.<listcomp>m       6661eAhh666r"   c                ,    g | ]}t          |          S r4   r5   r7   s     r!   r:   z1NumpyArrayInitializer.forward.<locals>.<listcomp>p   r;   r"   c                ,    g | ]}t          |          S r4   intr7   s     r!   r:   z1NumpyArrayInitializer.forward.<locals>.<listcomp>s       444c!ff444r"   int8_valuesc                ,    g | ]}t          |          S r4   r>   r7   s     r!   r:   z1NumpyArrayInitializer.forward.<locals>.<listcomp>{   r@   r"   zUnsupported dtype i   @zXThe size of input is too big. Please consider saving it to file and 'load_op' to load itNassign_valueOutr/   r.   T)r0   outputsattrsstop_gradientcastX)in_dtype	out_dtype)r0   inputsrE   rF   )1r   r	   EagerParamBaseis_dist_check_blockVariablepaddlepirr   ParameterMetaBlockr/   VarDescVarTypeFP16BF16FP32r   astype
create_varr
   generatejoinr-   r.   DENSE_TENSORDataTypeFLOAT16BFLOAT16FLOAT32flatFP64FLOAT64INT32INT8UINT8
ValueErrorsizer   r   assign_value_listr   rH   _share_underline_tensor_tor   rC   	append_opop)r   r#   r%   origin_dtyperK   np_valueout_var
value_namer2   var_tmpro   s              r!   forwardzNumpyArrayInitializer.forward:   s    sI455	Q:=++--	Q 	QP	Q 	Q 
 !!%(()$fjo&CD
 
 	
 	
 
 %)/6:3C!DEEEEE yL %L %
 
 
 ,1I{)))44H&& )HH0#(EBCC  i\)6! '  GG dm3T]5KLLLG-I{)))44HHG$I{H-2DM4IJJJ!J66666FF4</4dm6KLLL!J66666FF4</5t}7JKKK!J44hm444FFL %L &MM	
 
 
 'J44hm444FFE$+2CEEFFF;000=  
  8	 T[&'''))   $)$)%&	    !+g|<<22377772237774]] %	)T[&'''))	 G  5t}7MNNN +g|<<N#(YT$+"344
 # ! 	 	B $)$)    >"CL$+M%1 	      CFIr"   )r   r   r   r   N)r#   r$   r%   r&   r   r'   )__name__
__module____qualname____doc__r   ru   __classcell__r    s   @r!   r   r   '   sp        	 	      DHB B B B B B B B Br"   r   c                  (     e Zd ZdZ	 d
d fd	Z xZS )Assigna  Init an parameter with a numpy array, list, or tensor.

    Args:
        value (Tensor|numpy.ndarray|list|tuple): numpy array, list, tuple, or tensor to initialize the parameter.
        name(str|None, optional): Normally there is no need for user to set this
            property. For more information, please refer to :ref:`api_guide_Name`. Default is None.

    Returns:
        A parameter initialized by the input numpy array, list, or tensor.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> import numpy as np

            >>> # numpy array
            >>> data_1 = paddle.ones(shape=[1, 2], dtype='float32')
            >>> weight_attr_1 = paddle.ParamAttr(
            ...     name="linear_weight_1",
            ...     initializer=paddle.nn.initializer.Assign(np.array([2, 2])))
            >>> bias_attr_1 = paddle.ParamAttr(
            ...     name="linear_bias_1",
            ...     initializer=paddle.nn.initializer.Assign(np.array([2])))
            >>> linear_1 = paddle.nn.Linear(2, 2, weight_attr=weight_attr_1, bias_attr=bias_attr_1)
            >>> print(linear_1.weight.numpy())
            [2. 2.]
            >>> print(linear_1.bias.numpy())
            [2.]

            >>> res_1 = linear_1(data_1)
            >>> print(res_1.numpy())
            [6.]

            >>> # python list
            >>> data_2 = paddle.ones(shape=[1, 2], dtype='float32')
            >>> weight_attr_2 = paddle.ParamAttr(
            ...     name="linear_weight_2",
            ...     initializer=paddle.nn.initializer.Assign([2, 2]))
            >>> bias_attr_2 = paddle.ParamAttr(
            ...     name="linear_bias_2",
            ...     initializer=paddle.nn.initializer.Assign([2]))
            >>> linear_2 = paddle.nn.Linear(2, 2, weight_attr=weight_attr_2, bias_attr=bias_attr_2)
            >>> print(linear_2.weight.numpy())
            [2. 2.]
            >>> print(linear_2.bias.numpy())
            [2.]

            >>> res_2 = linear_2(data_2)
            >>> print(res_2.numpy())
            [6.]

            >>> # tensor
            >>> data_3 = paddle.ones(shape=[1, 2], dtype='float32')
            >>> weight_attr_3 = paddle.ParamAttr(
            ...     name="linear_weight_3",
            ...     initializer=paddle.nn.initializer.Assign(paddle.full([2], 2)))
            >>> bias_attr_3 = paddle.ParamAttr(
            ...     name="linear_bias_3",
            ...     initializer=paddle.nn.initializer.Assign(paddle.full([1], 2)))
            >>> linear_3 = paddle.nn.Linear(2, 2, weight_attr=weight_attr_3, bias_attr=bias_attr_3)
            >>> print(linear_3.weight.numpy())
            [2. 2.]
            >>> print(linear_3.bias.numpy())
            [2.]

            >>> res_3 = linear_3(data_3)
            >>> print(res_3.numpy())
            [6.]
    Nr   0npt.NDArray[Any] | Sequence[int] | paddle.Tensorr-   
str | Noner   r   c                   dd l }t          |d|j        t          t          t
          j        j        fd           t          |t          t          f          r|	                    |          }t          |t
          j        j                  r|                     d          }t                                          |           d S )Nr   r   r~   F)r   r   r   rl   tuplerQ   staticrP   r   arrayr   r   )r   r   r-   r   r    s       r!   r   zAssign.__init__  s    
 	]D%)?@		
 	
 	
 edE]++ 	'KK&&E eV]344 	'KK&&Er"   rv   )r   r   r-   r   r   r   )rw   rx   ry   rz   r   r{   r|   s   @r!   r~   r~      sT        E ET                       r"   r~   )
__future__r   typingr   r   rQ   r   baser   r	   r
   base.data_feederr   base.frameworkr   r   r   initializerr   collections.abcr   numpy.typingnpt__all__r   r~   r4   r"   r!   <module>r      se   # " " " " " % % % % % % % %        0 0 0 0 0 0 0 0 0 0 * * * * * *         
 % $ $ $ $ $ ((((((
U U U U UK U U Up]  ]  ]  ]  ] " ]  ]  ]  ]  ] r"   