
    |j                        U d dl mZ d dlZd dl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mZ ddlmZ erd dlZed	         Zd
ed<   g Z G d d          Z	 dddZdS )    )annotationsN)TYPE_CHECKINGLiteral)	TypeAlias   )EagerParamBasedefault_main_programin_dygraph_modeuse_pir_api   )lazy_init_helpersigmoidlinearconv1dconv2dconv3dconv1d_transposeconv_transpose1dconv2d_transposeconv_transpose2dconv3d_transposeconv_transpose3dtanhrelu
leaky_reluselur   _NonLinearityc                  N    e Zd ZdZddZ	 ddd
Z	 dddZ	 dddZddZddZ	dS )Initializera  Base class for parameter initializers

    Defines the common interface of parameter initializers.
    They add operations to the init program that are used
    to initialize parameter. Users should not use this class
    directly, but need to use one of its implementations.
    returnNonec                    d S N )selfs    q/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddle/nn/initializer/initializer.py__init__zInitializer.__init__A   s        Nparampaddle.Tensorblockpaddle.pir.Block | Nonec                    t                      j        s|                     ||          S |                     ||          S r$   )r   stateforward
_lazy_initr&   r*   r,   s      r'   __call__zInitializer.__call__D   s=      !!' 	.<<u---ue,,,r)   paddle.Tensor | Nonec                    t           )z;Add corresponding initialization operations to the network.)NotImplementedErrorr2   s      r'   r0   zInitializer.forwardL   s
     "!r)   c                    t                      sJ dd}|                    t          j        | j                             t          j        || j                  |_        |S )z+
        Apply lazy initialization
        r*   r+   r,   r-   c                    t                      r|}n|                    d|          }t                      5   | ||           d d d            d S # 1 swxY w Y   d S )NT)r,   )r   _to_static_varr   )r0   r*   r,   new_vars       r'   init_op_creatorz/Initializer._lazy_init.<locals>.init_op_creatorZ   s     }} B..t5.AA!## ( ('''( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( (s   AAAr*   r+   r,   r-   )r
   set_init_func	functoolspartialr0   _init_op_creator)r&   r*   r,   r;   s       r'   r1   zInitializer._lazy_initR   sp          		( 		( 		( 		( 	I-dl;;<<<!*!2T\"
 "
 r)   paddle.pir.Blockc                J    | t                                                      }|S r$   )r	   global_block)r&   r,   s     r'   _check_blockzInitializer._check_blockm   s#    =(**7799Er)   vartuple[int, int]c                   t          |t                    r|                                r|j        n|j        }|rt          |          dk    rdx}}ntt          |          dk    r|d         x}}nVt          |          dk    r|d         }|d         }n2t          j        |dd                   }|d         |z  }|d         |z  }||fS )a  Compute the fan_in and the fan_out for layers

        This method computes the fan_in and the fan_out
        for neural network layers, if not specified. It is
        not possible to perfectly estimate fan_in and fan_out.
        This method will estimate it correctly for matrix multiply and
        convolutions.

        Args:
            var: variable for which fan_in and fan_out have to be computed.

        Returns:
            tuple of two integers (fan_in, fan_out).
        r   r      N)
isinstancer   is_dist_local_shapeshapelennpprod)r&   rE   rL   fan_infan_outreceptive_field_sizes         r'   _compute_fanszInitializer._compute_fanss   s    " 3//47KKMMC 	
  	6E

a  FWWZZ1__$Qx'FWWZZ1__1XFAhGG $&759#5#5 1X 44FAh!55G  r)   )r!   r"   r$   r<   )r*   r+   r,   r-   r!   r4   )r,   r-   r!   rA   )rE   r+   r!   rF   )
__name__
__module____qualname____doc__r(   r3   r0   r1   rD   rS   r%   r)   r'   r    r    8   s             FJ- - - - - FJ" " " " " FJ    6   %! %! %! %! %! %!r)   r    nonlinearitystrr*   bool | float | Noner!   floatc                R   |d}n3t          |t          t          t          f          sJ t          |          }ddddddddddddt	          j        d          t	          j        dd|dz  z   z            dd}| |                                v r||          S t          d	|  d
          )a  
    Get the recommended ``gain`` value of some nonlinearity function. ``gain`` value can be used in some
    ``paddle.nn.initializer`` api to adjust the initialization value.

    Args:
        nonlinearity(str): name of nonlinearity activation function. If it is a linear function, such as:
            `linear/conv1d/conv2d/conv3d/conv1d_transpose/conv2d_transpose/conv3d_transpose` , 1.0 will be returned.
        param(bool|int|float|None, optional): optional parameter for some nonlinearity function. Now, it only applies to
            'leaky_relu'. Default: None, it will be calculated as 0.01 in the formula.

    Returns:
        A float value, which is the recommended gain for this nonlinearity function.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> gain = paddle.nn.initializer.calculate_gain('tanh')
            >>> print(gain)
            1.6666666666666667
            >>> # 5.0 / 3
            >>> gain = paddle.nn.initializer.calculate_gain('leaky_relu', param=1.0)
            >>> print(gain)
            1.0
            >>> # math.sqrt(2.0 / (1+param^2))
            >>> initializer = paddle.nn.initializer.Orthogonal(gain)

    Ng{Gz?r   g?g       @rH   g      ?r   znonlinearity function z is not supported now.)rI   boolintr[   mathsqrtkeys
ValueError)rX   r*   recommended_gains      r'   calculate_gainrd      s    @ }%$U!344444e	#iq5!8| 455 " ',,....--I\III
 
 	
r)   r$   )rX   rY   r*   rZ   r!   r[   )
__future__r   r>   r_   typingr   r   numpyrN   typing_extensionsr   base.frameworkr   r	   r
   r   	lazy_initr   paddler   __annotations____all__r    rd   r%   r)   r'   <module>rn      sK   # " " " " " "      ) ) ) ) ) ) ) )     ' ' ' ' ' '            ( ' ' ' ' ' MMM&	 M    $ `! `! `! `! `! `! `! `!H 59;
 ;
 ;
 ;
 ;
 ;
 ;
r)   