
    ͑idZ                        S SK Jr  S SKrS SKJr  S SKrS SKrSSKJ	r	J
r
  SSKJrJrJrJrJrJrJr  SSKJrJr  SSKJrJr  \(       a  S S	KJr  / r " S
 S5      rg)    )annotationsN)TYPE_CHECKING   )coreunique_name)Variable_current_expected_placedefault_main_programdefault_startup_programin_dygraph_modein_dynamic_or_pir_modein_pir_mode)_global_bias_initializer_global_weight_initializer)	ParamAttrWeightNormParamAttr)_DTypeLiteralc                  2   \ rS rSr% SrS\S'   S r\S 5       r\S 5       r	\S 5       r
\S	 5       r\S
 5       r\S 5       rSS jrS rSSSS\R$                  R&                  R(                  S4S jr S SS jjr SS jr SS jrS rSS jrS rS rSrg)LayerHelperBase*   float32r   _LayerHelperBase__dtypec                    X l         Xl        g N_layer_type_name)selfname
layer_types      ]/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/base/layer_helper_base.py__init__LayerHelperBase.__init__.   s    %
    c                    U R                   $ r   )r   r   s    r!   r   LayerHelperBase.name2   s    zzr$   c                    U R                   $ r   )r   r&   s    r!   r    LayerHelperBase.layer_type6   s    r$   c                    [        5       $ r   )r
   r&   s    r!   main_programLayerHelperBase.main_program:   s    #%%r$   c                    [        5       $ r   )r   r&   s    r!   startup_programLayerHelperBase.startup_program>   s    &((r$   c                    Xl         g r   r   )clsdtypes     r!   set_default_dtype!LayerHelperBase.set_default_dtypeB   s    r$   c                    U R                   $ r   r1   )r2   s    r!   get_default_dtype!LayerHelperBase.get_default_dtypeF   s    {{r$   Nc                v   [        U[        R                  5      (       a:  [        R                  R                  U[        5       SSU(       a  US5      $ SS5      $ [        U[        [        R                  R
                  [        R                  R                  45      (       a  U$ [        S[        U5       35      e)aX  
The API will create a ``Variable`` object from numpy\.ndarray or Variable object.

Parameters:
    value(ndarray): The numpy\.ndarray object that needs to be converted, it can be multi-dimension, and the data type is one of numpy\.{float16, float32, float64, int16, int32, int64, uint8, uint16}.
    name(str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`

Returns:
    Variable: ``Tensor`` created from the specified numpy\.ndarray object, data type and shape is the same as ``value`` .

Examples:

    .. code-block:: python

        >>> import numpy as np
        >>> import paddle.base as base

        >>> with base.dygraph.guard():
        ...     x = np.ones([2, 2], np.float32)
        ...     y = base.dygraph.to_variable(x)
        ...
FNTz[The type of input value is invalid, expected type is 'ndarray' or 'Variable', but received )
isinstancenpndarrayr   eagerTensorr	   r   paddlepirValue	TypeErrortype)r   valuer   s      r!   to_variableLayerHelperBase.to_variableJ   s    . eRZZ((::$$') 
 #'  $***;*;VZZ=M=MNOOLmnrsxnymz{ r$   c                  ^ ^^^^^ S SS ST R                   R                  5       4UU 4S jjmS T R                   R                  5       4UU 4S jjmS T R                   R                  5       4UU 4S jjmS S T R                   R                  5       4UUUUU 4S jjmUU 4S jn[        R                  " U5      nUR                  S-   Ul        S	/[        U5      -  nUR                  b  X!R                     XaR                  '   [        R                  " U5      nUR                  S
-   Ul        UnT R                   R                  5       R                  " STUS.UR                  SS9D6n	T R                   R                  5       R                  " STUS.UR                  SS9D6n
T" U
U	UR                  T R                   R                  5       S9  T" U	UU	T R                   R                  5       S9  T R                  R                  5       R                  " STUS.UR                  5       D6n	T R                  R                  5       R                  " STUS.UR                  5       D6n
U" XUR                  S9nU$ )N   Fc           	     f  > UcO  UR                  T
R                  R                  R                  SR	                  T
R
                  S/5      5      T	SS9nUR                  T
R                  R                  R                  SR	                  T
R
                  S/5      5      T	SS9nUR                  SSU 0SU0S	9  UR                  T
R                  R                  R                  SR	                  T
R
                  S
/5      5      T	SS9nUR                  SSU0SU0S[        U5      0S9  UR                  T
R                  R                  R                  SR	                  T
R
                  S/5      5      T	SS9nUR                  SSU0SU0UUUc  SOSS.S9  UR                  SSU0SU0SSU-  0S9  U$ )N.weight_norm_normFr   r3   persistableweight_norm_absabsXOut)rC   inputsoutputsweight_norm_powpowfactorrC   rR   rS   attrsweight_norm_sum
reduce_sumT)dimkeep_dim
reduce_allg      ?)
create_varr+   _name_generatorgenerate_with_ignorable_keyjoinr   	append_opfloat)xoutpr[   r\   blockabs_outpow_outsum_outr3   r   s            r!   	__norm_op;LayerHelperBase._create_weight_normalize.<locals>.__norm_opt   s    {&&**::VV$))-?!@A   % '  &&&&66RRHHdii):;< ! ' G OOC8eW5E   &&&&66RRHHdii):;< ! ' G OOW~(q*	   &&&&66RRHHdii):;< ! ' G OO!W~( (*-+$5	  	 OOW~q)	   Jr$   c                  > UcO  UR                  TR                  R                  R                  SR	                  TR
                  S/5      5      TSS9nUR                  SU R                  S9nUR                  SSU 0S	U0X$S
.S9  U$ )NrJ   weight_norm_reshapeFrL   Xshape)r   r3   reshape2rP   shape)rQ   XShape)rC   rR   rX   rS   )r^   r+   r_   r`   ra   r   r3   rb   )rd   rq   re   rg   x_shaper3   r   s        r!   __reshape_op>LayerHelperBase._create_weight_normalize.<locals>.__reshape_op   s     {&&**::VV$))-B!CD   % '  &&HAGG&DGOOQx& #7	   Jr$   c                   > UcO  UR                  TR                  R                  R                  SR	                  TR
                  S/5      5      TSS9nUR                  SSU 0SU0SU0S	9  U$ )
NrJ   weight_norm_transposeFrL   	transposerP   rQ   axisrW   )r^   r+   r_   r`   ra   r   rb   )rd   ry   re   rg   r3   r   s       r!   __transpose_op@LayerHelperBase._create_weight_normalize.<locals>.__transpose_op   s     {&&**::VV$))-D!EF   % '  OO Qxtn	   Jr$   c                  > UcO  UR                  TR                  R                  R                  SR	                  TR
                  S/5      5      TSS9nUc	  T
" XX#S9  U$ US:X  aU  U R                  S   /S/[        U R                  5      S-
  -  -   nT" X R                  S   S/US	9nT
" US/US9nT" XaXCS
9  U$ U[        U R                  5      S-
  :X  aV  S/[        U R                  5      S-
  -  U R                  S   /-   nT" U SU R                  S   /US	9nT
" US/US9nT" XaXCS
9  U$ [        [        [        U R                  5      5      5      nUSsUS'   Xr'   T" XUS9nUR                  S   /S/[        UR                  5      S-
  -  -   nT" XR                  S   S/US	9nT
" US/US9nT" XdUS	9n	T" XXS9  U$ )z0Computes the norm over all dimensions except dimrJ   rK   FrL   r[   rg   r   r   )rq   rg   )re   rq   rg   )rg   re   rg   )
r^   r+   r_   r`   ra   r   rq   lenlistrange)rd   re   r[   rg   	out_shapereshapenormpermrx   rp   _LayerHelperBase__norm_op_LayerHelperBase__reshape_op_LayerHelperBase__transpose_opr3   r   s             r!   __norm_except_dimCLayerHelperBase._create_weight_normalize.<locals>.__norm_except_dim   s    {&&**::VV$))-?!@A   % '  {!c70 J/ WWQZLA3#agg,2B+CC	&qR0@N qc?T)I& J% AGGq((C3qww<!#34}D	&qQWWR[0AO qc?T)I J E#agg,/0%(!"Q*1%@	&__Q/0A3	(1,4 	 'ooa&8"%=U !qc?'UKx3DJr$   c                  > T" XTR                   R                  5       S9n[        R                  " XS9n[        R                  R
                  R                  UUc  UO![        R                  " XAR                  U   /S9Uc  SOUS9nU$ )z%Calculations for weight normalizationr}   )rd   y)rd   rq   r~   )rd   r   ry   )	r+   current_blockr?   dividetensormath_multiply_with_axisr   rq   )gvr[   r   scalew!_LayerHelperBase__norm_except_dimr   s         r!   __weight_normalizeDLayerHelperBase._create_weight_normalize.<locals>.__weight_normalize  s    $$"3"3"A"A"CD MME ""66 { %~F;RC 7 A Hr$   _gr   _vr3   rq   with_initializerT)rd   re   r[   rg   r   )r[    )
r.   global_blockcopydeepcopyr   r   r[   create_parameter
_to_kwargsr+   )r   attrrq   r3   "_LayerHelperBase__weight_normalizeg_param_attrg_param_shapev_param_attrv_param_shapeg_paramv_paramw_paramr   r   r   r   s   `  `        @@@@r!   _create_weight_normalize(LayerHelperBase._create_weight_normalizeq   sl   
 &&335>	 >	B d&:&:&G&G&I	 	* T%9%9%F%F%H	 	( T)=)=)J)J)L%	 %	N	0 }}T* II,c%j(88&+HHoM((#}}T* II, &&335FF 

 %%u%=

 &&335FF 

 %%t%<

 	&&335		
 	&&335		
 ##002CC 
}
0<0G0G0I
 ##002CC 
}
0<0G0G0I
 %W488Dr$   Fc	                
   [         R                  " U5      n[        R                  " U5      nU(       d  g[	        U[        5      (       d   e[        U5       H  u  pU
S:  a  M   SU	 SU
 35       e   U(       d  U R                  n[	        U[        R                  5      (       a'  [        R                  R                  R                  U   nU(       a  Sn[        5       b
  [        5       OUnOSn[        5       b
  [        5       OUnUR                  c  [        5       (       a7  [         R"                  " SR%                  U R                  U/5      5      Ul        OEU R&                  R(                  R#                  SR%                  U R                  U/5      5      Ul        UGcE  UR*                  Gc7  [	        U[        R,                  R.                  5      (       a  U[        R,                  R.                  R0                  :w  a  U[        R,                  R.                  R2                  :w  a  U[        R,                  R.                  R4                  :w  a[  U[        R,                  R.                  R6                  :w  a3  U[        R,                  R.                  R8                  :w  a  [;        S5      eOUS	;  a  [;        S
5      eU(       a  UR=                  5         O"UR?                  5         OURA                  U5        [	        U[B        5      (       a3  U RE                  XU5      n[B        RF                  RI                  U5        U$ [K        5       (       a  [         RL                  " UR                  5      nU(       a  [O        SUR                   S35      eU R&                  RQ                  5       RR                  " SUUUUS.URU                  SS9D6nUb  URW                  U5      nU$ [Y        5       (       a  [	        U[        R,                  R.                  5      (       a'  [        R                  R                  RZ                  U   n[        R                  R                  RR                  " SUUS.URU                  SS9D6nUb  URW                  U5      nU$ U R\                  RQ                  5       RR                  " SUUUS.URU                  SS9D6  U R&                  RQ                  5       RR                  " SX2US.URU                  5       D6$ )a  Create parameters for this layers.

   Args:
       attr: [ParamAttr] should be the parameter attribute for this parameter
       shape: shape of the parameter
       dtype: data type of this parameter
       is_bias: if this is a bias parameter
       default_initializer: set the default initializer for this parameter
       device: device where this parameter will be placed

Returns created parameter Variable.
Nr   zOExpected every dim's size to be larger than or equal to 0, but the size of the z-th dim is br   rJ   zCan not create parameter with default initializer when dtype is not ['float16', 'float32', 'float64', 'bfloat16'] type. Set default_initializer to fit the parameter dtype!)float16r   float64bfloat16rc   int8zCan not create parameter with default initializer when dtype is not ['float16', 'float32', 'float64', 'bfloat16', 'float'] type. Set default_initializer to fit the parameter dtype!zparameter name [z] have be been used. In dygraph mode, the name of parameter can't be same.Please check the parameter attr value passed to self.create_parameter or constructor of dygraph Layers)r3   rq   rC   stop_gradientTr   r   )r3   rq   rC   r   )/r   r   r   _to_attrr:   	enumerater   r   DataTyper?   r@   datatype_to_vartyper   r   r   r   r   generatera   r+   r_   initializerVarDescVarTypeFP32FP64FP16BF16INT8rB   _set_default_bias_initializer_set_default_param_initializer_set_default_initializerr   r   params_with_weight_normappendr   dygraph_parameter_name_checker
ValueErrorr   r   r   tor   vartype_to_datatyper.   )r   r   rq   r3   is_biasdefault_initializerr   rC   deviceisizesuffixparamis_useds                 r!   r    LayerHelperBase.create_parameterN  sI   0 }}T"!!$'$	**** 'GA19 ''(cTF<9 ( LLEeT]]++JJOO77>EF ,-9 )*(   F ./; +,(  
 99%'''00499f:M1NO	 --==FFHHdii01	 &4+;+;+C%!5!566T\\11666!5!5!:!::!5!5!:!::!5!5!:!::!5!5!:!::# F   !  $ O  224335))*=> d/0011$uEE77>>uEL "@@KG &tyyk 24 4  %%224EE +	
 //4/8E !(L}}eT\\%9%9::"JJOO??FE

88  ooto<
 %!HHV,E  --/@@  //4/8	 $$113DD t7;7H r$   c           	     @   U(       d  U R                   nU R                  R                  5       R                  U R                  R                  R                  SR                  U R                  S/5      5      UU[        R                  R                  R                  SUS9$ )a  Create a temporary variable that should be type inferred layer.

Note:
    The default type will be set to DENSE_TENSOR. However, when
    the var is used as operator output, its type will be updated
    based on operator's `VarTypeInference` implementation in
    infer_var_type.
rJ   tmpFr   r3   rq   rC   rM   r   )r   r+   r   r^   r_   r`   ra   r   r   r   r   DENSE_TENSORr   r3   r   rq   s       r!   "create_variable_for_type_inference2LayerHelperBase.create_variable_for_type_inference  s     LLE  ..0;;""22NN$))U+, %%22' < 	
 		
r$   c           	     (   U(       d  U R                   nU R                  R                  5       R                  U R                  R                  R                  SR                  U R                  S/5      5      UU[        R                  R                  R                  SUS9nU R                  R                  nSU R                  l        [        R                  R                  R!                  UR"                  USSUS9  X$l        XPR                  l        U$ )a  Create a global variable that should be type inferred layer.

Note:
    The default type will be set to DENSE_TENSOR. However, when
    the var is used as operator output, its type will be updated
    based on operator's `VarTypeInference` implementation in
    infer_var_type.
rJ   r   Fr   r   g        )	force_cpure   )r   r+   r   r^   r_   r`   ra   r   r   r   r   r   current_block_idxr?   r   creationfill_constantrq   r   )r   r3   r   rq   outputsaved_block_ids         r!   *_create_global_variable_for_type_inference:LayerHelperBase._create_global_variable_for_type_inference  s     LLE""//1<<""22NN$))U+, %%22' = 	
 **<<./+,,LL%6 	- 	
  -.<+r$   c           	     @   U(       d  U R                   nU R                  R                  5       R                  U R                  R                  R                  SR                  U R                  S/5      5      UU[        R                  R                  R                  SUS9$ )a  Create a temporary sparse variable that should be type inferred layer.

Note:
    The default type will be set to SPARSE_COO. However, when
    the var is used as operator output, its type will be updated
    based on operator's `VarTypeInference` implementation in
    infer_var_type.
rJ   r   Fr   )r   r+   r   r^   r_   r`   ra   r   r   r   r   
SPARSE_COOr   s       r!   )create_sparse_variable_for_type_inference9LayerHelperBase.create_sparse_variable_for_type_inference  s     LLE  ..0;;""22NN$))U+, %%00' < 	
 		
r$   c                V    U R                   R                  5       R                  " U0 UD6$ )z;Create Variable for this layers.
Returns created Variable.
)r+   r   r^   )r   argskwargss      r!   create_variableLayerHelperBase.create_variable/  s)       ..0;;TLVLLr$   c                Z    U R                   R                  5       R                  " USU0UD6$ )a  
create global variable, note that there is no initializer for this global variable.
Args:
    persistable(bool): True if it is a checkpoint value.
    *args: See create_var's documentation
    **kwargs: See create_var's documentation

Returns(Variable): the created variable.
rM   )r+   r   r^   )r   rM   r   r   s       r!   create_global_variable&LayerHelperBase.create_global_variable5  s9       --/::
*
.4
 	
r$   c                    U R                   R                  5       R                  U5      (       a+  U R                   R                  5       R                  U5      S4$ U R                  " USU0UD6S4$ )z~
Creates a global variable if not exists and returns the variable and
a boolean flag which is true when it is a new variable.
Fr   T)r+   r   has_varvarr   )r   r   r   r   s       r!   create_or_get_global_variable-LayerHelperBase.create_or_get_global_variableC  si    
 ))+33D99$$11377=uDD..4JDJ6JDPPr$   c           	     :   [        U[        5      (       d   e[        5       (       a!  U" XR                  R	                  5       5        gU R
                  R	                  5       R                  UR                  UR                  UR                  UR                  SUS9  g)zfSet target Variable's initializer

Args:
    var: target Variable
    initializer: initializer to use
T)r   rC   r3   rq   rM   r   N)r:   r   r   r+   r   r.   r^   r   rC   r3   rq   )r   r   r   s      r!   set_variable_initializer(LayerHelperBase.set_variable_initializerM  s{     #x((((..;;=>  --/::XXXXiiii ' ; r$   r   r   )FN)returnzpaddle.Tensor)F)__name__
__module____qualname____firstlineno__r   __annotations__r"   propertyr   r    r+   r.   classmethodr4   r7   rE   r   r   r   r   r   r   r   r   r   r   r   r   r   __static_attributes__r   r$   r!   r   r   *   s    &G]&       & & ) )    %NZB  \\!!..L^ 15
	
4 15D 15
2M
Qr$   r   )
__future__r   r   typingr   numpyr;   r?    r   r   	frameworkr   r	   r
   r   r   r   r   r   r   r   
param_attrr   r   paddle._typing.dtype_liker   __all__r   r   r$   r!   <module>r     sH    #         N 67
u ur$   