
    {-j!                        d dl m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
 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pir   )core	frameworkunique_name)check_variable_and_dtype)_current_expected_placein_dygraph_modein_pir_mode   )Initializerc                  >     e Zd ZdZ	 	 	 	 	 	 dd fdZ	 dddZ xZS )UniformInitializeraJ  Implements the random uniform distribution initializer

    Args:
        low (float, optional): Lower boundary of the uniform distribution. Default is :math:`-1.0`.
        high (float, optional): Upper boundary of the uniform distribution. Default is :math:`1.0`.
        seed (int, optional): Random seed. Default is 0.
        diag_num (int, optional): the number of diagonal elements to initialize.
            If set to 0, diagonal initialization will be not performed. Default is 0.
        diag_step (int, optional): Step size between two diagonal elements,
            which is generally the width of the square matrix. Default is 0.
        diag_val (float, optional): the value of the diagonal element to be initialized,
            default 1.0. It takes effect only if the diag_num is greater than 0. Default is :math:`1.0`.

                ?r   lowfloathighseedintdiag_num	diag_stepdiag_valreturnNonec                   |J |J ||k    sJ |J |J |J |J |dk    s|dk    r|dk    r|dk    sJ t                                                       || _        || _        || _        || _        || _        || _        d S )Nr   )super__init___low_high_seed	_diag_num
_diag_step	_diag_val)selfr   r   r   r   r   r   	__class__s          ]/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddle/nn/initializer/uniform.pyr    zUniformInitializer.__init__0   s     s{{{{###$$$###a<<9q==a<<IMMM1	

!#!    Nvarpaddle.Tensorblockpir.Block | Nonepaddle.Tensor | Nonec                   t          |t          j                  r|                                r
J d            |                     |          }t          |t          j        t          j        f          sJ t                      st          |dg dd           | j	        dk    r|j
        j        | _	        |j        t          j        j        j        k    rwt          j        j        j        }|                    t'          j        d                    d|j        dg                    |j        |t          j        j        j        d	          }n	|j        }|}t                      rt3          j        |j        || j        | j        | j	        |j                                        r|j        nt?                                }|j        t          j        j        j        k    r0t3          j         ||j                  }|!                    |           n|!                    |           d
S tE                      r|j        t          j#        j$        k    rt          j#        j%        }n|j        }t3          j        |j        || j        | j        | j	        t?                                }|j        t          j#        j$        k    r4|j        t          j#        j$        k    rt3          j         ||j                  S |S |&                    di d|i|j        || j        | j        | j	        | j'        | j(        | j)        dd          }|j        t          j        j        j        k    r*|&                    dd|id|i|j        |j        d           ||_*        |S )a\  Initialize the input tensor with Uniform distribution.

        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
        zDCurrently, uniform initializer not support lazy init for dist param.Out)uint16float16float32float64uniform_randomr   .tmpF)nameshapedtypetypepersistableN)r:   r;   minmaxr   r   r   r   T)r<   inputsoutputsattrsstop_gradientcastX)in_dtype	out_dtype)r<   r@   rA   rB   )+
isinstancer   EagerParamBaseis_dist_check_blockBlockr   r   r
   r#   programrandom_seedr;   r   VarDescVarTypeFP16FP32
create_varr	   generatejoinr9   r:   DENSE_TENSORr   uniformr!   r"   place_typer   rD   _share_underline_tensor_tor   DataTypeFLOAT16FLOAT32	append_opr$   r%   r&   op)r'   r+   r-   rG   out_varvar_tmpr_   s          r)   forwardzUniformInitializer.forwardJ   s^    sI455	
:=++--	
 	
 S	
 	
 

 !!%((%)/39!=>>>>>   	$;;; 	   :??2DJ 9,111,1I&& )HH.%@AA  i\)6! '  GG 	IG =	n		

 Y__..M		4K4M4M G yDL0555 +gsy9922377772237774]] .	yDM111 M1		I	n		

')) G 	T]222MT]%:::{7CI666N%( Y&9: J $!% $	 	 # !  B" yDL0555>"CL'.}39MM	      CFIr*   )r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   )N)r+   r,   r-   r.   r   r/   )__name__
__module____qualname____doc__r    rb   __classcell__r(   s   @r)   r   r       s         " " " " " " " "6 =Am m m m m m m m mr*   r   c                  (     e Zd ZdZ	 dd fdZ xZS )Uniforma  The uniform distribution initializer.

    Args:
        low (float, optional): Lower boundary of the uniform distribution. Default is :math:`-1.0`.
        high (float, optional): Upper boundary of the uniform distribution. Default is :math:`1.0`.
        name (str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.

    Returns:
        A parameter initialized by uniform distribution.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> paddle.seed(1)
            >>> data = paddle.ones(shape=[3, 1, 2], dtype='float32')
            >>> weight_attr = paddle.framework.ParamAttr(
            ...     name="linear_weight",
            ...     initializer=paddle.nn.initializer.Uniform(low=-0.5, high=0.5))
            >>> bias_attr = paddle.framework.ParamAttr(
            ...     name="linear_bias",
            ...     initializer=paddle.nn.initializer.Uniform(low=-0.5, high=0.5))
            >>> linear = paddle.nn.Linear(2, 2, weight_attr=weight_attr, bias_attr=bias_attr)
            >>> print(linear.weight)
            Parameter containing:
            Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
            [[-0.48212373,  0.26492310],
             [ 0.17605734, -0.45379421]])

            >>> print(linear.bias)
            Parameter containing:
            Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=False,
            [-0.11236754,  0.46462214])

            >>> res = linear(data)
            >>> print(res)
            Tensor(shape=[3, 1, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
            [[[-0.41843393,  0.27575102]],
             [[-0.41843393,  0.27575102]],
             [[-0.41843393,  0.27575102]]])
    r   r   Nr   r   r   r9   
str | Noner   r   c                    |
J d            |
J d            ||k    s
J d            t                                          ||dddd           d S )Nzlow should not be Nonezhigh should not be Nonez%high should greater or equal than lowr   r   )r   r   r   r   r   r   )r   r    )r'   r   r   r9   r(   s       r)   r    zUniform.__init__   sv      8!:s{{{C{{{$Qa# 	 	
 	
 	
 	
 	
r*   )r   r   N)r   r   r   r   r9   rk   r   r   )rc   rd   re   rf   r    rg   rh   s   @r)   rj   rj      sT        ( (V HL
 
 
 
 
 
 
 
 
 
 
r*   rj   )
__future__r   paddler   r   baser   r   r	   base.data_feederr
   base.frameworkr   r   r   initializerr   __all__r   rj    r*   r)   <module>ru      s   # " " " " "          0 0 0 0 0 0 0 0 0 0 8 8 8 8 8 8         
 % $ $ $ $ $
W W W W W W W Wt3
 3
 3
 3
 3
  3
 3
 3
 3
 3
r*   