
    {-jP                    	   d dl mZ d dlZd dlZd dlmZmZ d dlm	Z	 d dl
mZmZmZmZmZ ddlmZmZmZmZ ddlmZ dd	lmZ dd
lmZ i ej        j        e	j        ej        j        e	j        ej        j         e	j!        ej        j"        e	j#        ej        j$        e	j$        ej        j%        e	j%        ej        j&        e	j&        ej        j'        e	j'        ej        j(        e	j(        ej        j)        e	j)        ej        j*        e	j*        ej        j+        e	j+        ej        j,        e	j-        ej        j.        e	j/        ej        j0        e	j1        ej        j2        e	j3        Z4d e45                                D             Z6i  ej7        d          e	j         ej7        d          e	j         ej7        d          e	j!         ej7        d          e	j$         ej7        d          e	j%         ej7        d          e	j&         ej7        d          e	j'         ej7        d          e	j#         ej7        d          e	j(         ej7        d          e	j)         ej7        d          e	j*         ej7        d          e	j+        ej8        e	j!        ej9        e	j        ej:        e	j        ej;        e	j$        ej<        e	j%        ej=        e	j&        ej>        e	j'        ej?        e	j#        ej@        e	j(        ejA        e	j)        ejB        e	j*        ejC        e	j+        de	j-        de	j/        i	ZDi e	j'        de	j!        de	j#        de	j        de	j        de	j)        de	j%        de	j$        de	j&        de	j(        de	jE        de	jF        de	jG        de	j*        de	j+        de	j-        de	j/        dZHi de	j        de	j        de	j!        de	j$        de	j%        de	j&        de	j'        de	j'        de	j#        de	j(        de	j)        de	j*        de	j+        de	j#        de	j-        de	j/        ZId/d ZJ e            aK et          L                                            e            aMd! ZNd" ZOd0d#ZPd$ ZQed0d%            ZR G d& d'          ZS	 d0d(ZTd0d)ZUd* ZVd+ ZWd, ZXed-             ZYd0d.ZZdS )1    )annotationsN)PlaceVarDesc)DataType)Programget_current_insertion_pointreset_insertion_point_to_startset_insertion_point set_insertion_point_to_block_end   )data	parameterset_parameterset_persistable_value)unique_nameset_static_op_arg_pre_cast_hook)signature_safe_contextmanagerc                    i | ]\  }}||	S  r   ).0kvs      O/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddle/pir/core.py
<dictcomp>r   6   s    DDD1q!DDD    float32float64float16int32int16int64bool_uint16uint8int8	complex64
complex128float8_e4m3fnfloat8_e5m2booluint32uint64bfloat16returnr   c                   t          | t                    r<|                                                                 }|t          v rt          |         S t          j        |           }|t          v rt          |         S t          d|           )z
    Convert the data type in numpy to the data type in Paddle.

    Args:
        np_dtype (np.dtype|str): The data type in numpy or valid data type
            string.

    Returns:
        DataType : The data type in Paddle.

    zNot supported numpy dtype )	
isinstancestrlowerstripstr_to_paddle_typenpdtypenp_type_to_paddle_type
ValueError)np_dtypekeyr7   s      r   convert_np_dtype_to_dtype_r<   ~   s     (C   +nn$$&&$$$%c**HXE&&&%e,,=e==>>>r   c                     t           S )a  
    Get default/global startup program.

    The :code:`paddle.nn` function will append the initialization operators into startup program.
    The :code:`startup_program` will initialize the parameters by the OPs.

    This method will return the default or the current startup program. Users can use
    :ref:`api_paddle_ir_core_program_guard`  to switch :ref:`api_paddle_ir_Program` .

    Returns:
        Program: current default startup program.

    Returns type:

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> paddle.enable_static()
            >>> x = paddle.static.data(name="x", shape=[-1, 784], dtype='float32')
            >>> out = paddle.static.nn.fc(name="fc", x=x, size=10, activation="relu")
            >>> print("main program is: {}".format(paddle.static.default_main_program()))
            >>> print("start up program is: {}".format(paddle.static.default_startup_program()))
    _startup_program_r   r   r   default_startup_programr@      s
    4 r   c                     t           S )a;  
    This API can be used to get ``default main program`` which store the
    descriptions of Ops and tensors.

    For example ``z = paddle.add(x, y)`` will create a new ``add``
    Op and a new ``z`` tensor, and they will be recorded in ``default main program`` .

    The ``default main program`` is the default value for ``Program`` parameter in
    a lot of APIs. For example, the :code:`Executor.run()` will execute the
    :code:`default_main_program` when the program is not specified.

    If you want to switch the ``default main program``, you can use :ref:`api_paddle_ir_core_program_guard` .

    Returns:
        Program: A ``Program`` which holding the descriptions of OPs and tensors in the network.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> paddle.enable_static()
            >>> # Sample Network:
            >>> x = paddle.static.data(name='x', shape=[100, 100], dtype='float32')
            >>> y = paddle.static.data(name='y', shape=[100, 100], dtype='float32')
            >>> out = paddle.add(x, y)

            >>> # print the number of blocks in the program, 1 in this case
            >>> print(paddle.static.default_main_program().num_blocks) # 1
            >>> # print the default_main_program
            >>> print(paddle.static.default_main_program())
    )_main_program_r   r   r   default_main_programrC      s    B r   c                    t           }t                      }| a | |k    r||}|'t          t                                                      nt	          |           ||fS )z
    Switch the main program to a new program.

    Args:
        program(Program): The new main program

    Returns:
        Program: The previous main program
    )rB   r   r   global_blockr
   )programinsertion_pointprev_programprev_insertion_points       r   switch_main_programrJ      sl     "L688N,?#:.()D)D)F)FGGGGO,,,---r   c                    t           }| a |S )z
    Switch the startup program to a new program
    Args:
        program(Program): The new startup program

    Returns:
        Program: The previous startup program
    r>   )rF   rH   s     r   switch_startup_programrL      s     %Lr   c              #  F  K   ddl m}  || dt          d           t          |           \  } }|" ||dt          d           t	          |          }	 dV  t          | |           |t	          |           dS dS # t          | |           |t	          |           w w xY w)a  
    :api_attr: Static Graph

    Change the global main program and startup program with ``with`` statement.
    Layer functions in the Python ``with`` block will append operators and
    Tensors to the new main programs.

    Args:
        main_program(Program): New main program inside ``with`` statement.
        startup_program(Program, optional): New startup program inside ``with``
            statement. :code:`None` means not changing startup program,
            default_startup_program is still used.
            Default: None.

    Examples:
        .. code-block:: python
            :name: code-example-1

            >>> import paddle

            >>> paddle.enable_static()
            >>> main_program = paddle.static.Program()
            >>> startup_program = paddle.static.Program()
            >>> with paddle.static.program_guard(main_program, startup_program):
            ...     data = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32')
            ...     hidden = paddle.static.nn.fc(x=data, size=10, activation='relu')

    Notes: The temporary :code:`Program` can be used if the user does not need
    to construct either of startup program or main program.

    Examples:
        .. code-block:: python
            :name: code-example-2

            >>> import paddle

            >>> paddle.enable_static()
            >>> main_program = paddle.static.Program()
            >>> # does not care about startup program. Just pass a temporary value.
            >>> with paddle.static.program_guard(main_program, paddle.static.Program()):
            ...     data = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32')
    r   )
check_typemain_programzpaddle.static.program_guardNstartup_program)base.data_feederrN   r   rJ   rL   )rO   rP   rN   rI   s       r   program_guardrR     s      X .-----Jng/L   *=\)J)J&L&"
)		
 	
 	
 1AA4L*>???&"?33333 '& 	L*>???&"?3333 's   A< <$B c                      e Zd Zd ZdS )ParameterMetac                "    || _         || _        d S N)shaper7   )selfrW   r7   s      r   __init__zParameterMeta.__init__H  s    



r   N)__name__
__module____qualname__rY   r   r   r   rT   rT   G  s#            r   rT   c                d   dvrt          d          | $t          | t                    st          |           } |}|st	          j        d          }t                      }t                      }t          |           }d}dv rd         rdv r
d         rd}fd}	t          |          5  d         }
 |
||
                                          }d|_        |r |	|           t          ||           d d d            n# 1 swxY w Y   |                    |           t          t                                5  t                       t          |          }d|_        |r |	|           d d d            n# 1 swxY w Y                       d	d          |_        |j         |_                            d
ddi          |_                            dd           |_                            dd           |_                            dd          |_        d|_        d|_        |S )NinitializerRinitializer is None, if you want to create parameter, please pass its initializer.r   F
placementsprocess_meshTc                   dd l }dd lm} 	d         }|j        j                            	d         t          
                    \  }}|j        j        j	        
                    |||          }|j        j        j	                            |                                 |          }|                     |           |j        j        j	                            |g |g          }||                                 _        d S )Nr   ra   r`   )paddlepaddle.distributeddistributedauto_parallelplacement_type
to_dim_maplenbase	libpaddlepircreate_tensor_dist_attributecvt_to_dist_typetypeset_typecreate_op_dist_attributeget_defining_op	dist_attr)valuerc   distra   dim_mappartial_statusrs   	dist_typeop_dist_attrkwargsrW   s            r   to_distz!create_parameter.<locals>.to_distj  s    ))))))n-"&"4"C"N"N< #e**#
 #
 K)-JJ'>
 
	 K)->>JJLL)
 
	 	y!!!{,0II"yk
 
 -9)))r   	trainableoptimize_attrlearning_rateg      ?regularizerdo_model_average	need_clip)r9   r1   r   r<   r   generater@   rC   rT   rR   rE   persistabler   set_parameters_fromr	   r   getr|   stop_gradientr}   r   r   r   is_distributedis_parameter)r7   rW   namerz   
value_namerP   rO   parameter_metais_distr{   r^   init_resultparams    ` `         r   create_parameterr   M  s    F""`
 
 	
 %** 	6.u55EJ 7 )+66
-//O'))L"5%00NG<  	f$$>" % 9 9 9 9 9 9( 
	'	' 	/ 	/]+!kO88::
 
 #' 	!GK   k:...	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ $$_555	+--	.	.  &(((*%%  	GENNN               jjd33EO#o-E **_6LMME

=$77E#ZZ(:DAAEjjd33EO EELs%   1ADDD2FFFc                   dvrt          d          | $t          | t                    st          |           } |}|st	          j        d          }dv od         }fd}t                      }t                      }t          |          5  d         }	t          ||           }
 |	|
|
                                          }d|_        |r ||           t          ||           ddd           n# 1 swxY w Y   t          t                                5  t                       t          ||| t                                }d|_        |r ||           ddd           n# 1 swxY w Y   |S )z
    Create Value that is persistable in startup program and main program. The Value is initialized in startup program and
    used in main program.

    Returns:
        Value: The created Value from main program
    r^   r_   Npersistable_valuers   c                   dd l }d         }|j        j        j                            |                                 |          }|                     |           |j        j        j                            |j        g |g          }| 	                                }||_
        |                    d          r|                    d| j                   d S d S )Nr   rs   rW   )rc   rj   rk   rl   rn   ro   rp   rq   ra   rr   rs   has_attrset_int_array_attr_local_shape)rt   rc   rs   rx   ry   	define_oprz   s         r   r{   z)create_persistable_value.<locals>.to_dist  s    ;'	K)->>JJLL)
 
	 	y!!!{,0II"B
 
 ))++	*	g&& 	F((%2DEEEEE	F 	Fr   T)r9   r1   r   r<   r   r   r@   rC   rR   rT   rE   r   r   r	   r   r   )r7   rW   r   rz   r   r   r{   rP   rO   r^   r   r   persist_values      `         r   create_persistable_valuer     s;    F""`
 
 	
 %** 	6.u55EJ ? )*=>>
V#;{(;GF F F F F  .//O'))L		'	' 	7 	7]+&ue44!kO88::
 
 #' 	!GK   k:666	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 
+--	.	. # #&(((Zuww??$(! 	#GM"""# # # # # # # # # # # # # # # s&   AC66C:=C:AE))E-0E-c                    t          |           5  t          |j        |j        |j        t                                }d|_        ddd           n# 1 swxY w Y   |S )z`
    Get a persistable value from a target program by using value that is in other program.
    TN)rR   r   r   rW   r7   r   r   )target_program
value_infotarget_values      r   _get_persistable_valuer     s     
~	&	& ( (OZ-z/?
 
 $( 	( ( ( ( ( ( ( ( ( ( ( ( ( ( (
 s   5AAAc                T   |                      t                                 t          |           5  t          |j                  }d|_        |j        |_        ddd           n# 1 swxY w Y   t          |d          r|j        |_        t          |d          r|j	        |_	        |S )z\
    Get a parameter from a target program by using parameter that is in other program.
    TNr   r   )
r   r@   rR   r   r   r   r   hasattrr   r   )r   
param_infotarget_params      r   _get_parameterr     s     &&'>'@'@AAA	~	&	& > > 11#' %/%="> > > > > > > > > > > > > > >
 z=)) :#-#9 z;'' 6!+!5s   (A%%A),A)c                    ddl }ddlm} t          | |j                  rF|                    |j        j                                        |           }d}|| j	        v rd|_
        |S | S )z$
    Convert Tensor into Value.
    r   N)_global_parameter_recorder__non_persistableF)rc   'paddle.jit.dy2static.parameter_recorderr   r1   Tensorr   rl   corerC   r   r   )tensorrc   r   rt   NON_PERSISTABLE_VAR_NAME_SUFFIXs        r   _convert_into_valuer     s     MMM      &&-(( 
*..JO0022F
 
 +>' +fk99 %EMr   c              #  x   K   t          |           }	 dV  t          |           dS # t          |           w xY w)zA
    Set a hook function to cast the arguments of static op.
    Nr   )hookoriginal_callbacks     r   static_op_arg_cast_guardr     sO       8==;'(9:::::'(9::::s   ( 9c           	        t          |t                    s t          dt          |           d          d|v rdnd}|rGi }|                                D ]/\  }}|dk    r||d         v r|d         |         }|||<   *|||<   0n|}|                                D ]~\  }}t          |t
          j        j        j                  r*t          |t          j
                  rt          j        |          ||<   \t          d| dt          |           d          |t
          j                                        }|                     ||           dS )	a  
    Set parameters and persistable buffers in state_dict to program.
    An exception will throw if shape or dtype of the parameters is not match.

    .. note::
        This function MUST called after run start_up_program

    Args:
        state_dict(dict): the dict store parameters and persistable buffers.
            The key is the name of the parameter or the name of the buffer.
            The value is the tensor of this variable in the given scope.
        scope(Scope, optional) : If scope is None, state_dict will be set to global scope
            obtained through 'paddle.static.global_scope()'. Otherwise, value will be set to scope.
            Default: None

    Returns:
        None

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> import paddle.static as static

            >>> paddle.enable_static()

            >>> x = static.data(name="x", shape=[10, 10], dtype='float32')
            >>> y = static.nn.fc(x, 10)
            >>> z = static.nn.fc(y, 10)

            >>> place = paddle.CPUPlace()
            >>> exe = static.Executor(place)
            >>> exe.run(static.default_startup_program())
            >>> prog = static.default_main_program()

            >>> path = "./temp/model.pdparams"
            >>> paddle.save(prog.state_dict(), path)
            >>> state_dict_load = paddle.load(path)
            >>> prog.set_state_dict(state_dict_load)
    z2Type of `state_dict` should be dict, but received .zStructuredToParameterName@@TFzThe type of `z*` should be Tensor, ndarray, but received N)r1   dict	TypeErrorro   itemsrc   rj   rk   DenseTensorr6   ndarray	to_tensorstaticglobal_scopeset_state_dict)rF   
state_dictscope	conditionclear_state_dictr   rt   s          r   r   r     s   R j$'' 
TjAQAQTTT
 
 	
 6CCI &%++-- 	/ 	/KD%444z"?@@@!"?@F). &&). &&	/ &'--//  eeV[2>?? 	rz** 	%+%5e%<%<T""^^^PTUZP[P[^^^   }**,,+U33333r   )r/   r   rV   )[
__future__r   numpyr6   rc   paddle.base.corer   r   paddle.base.libpaddler   paddle.base.libpaddle.pirr   r   r	   r
   r   _pir_opsr   r   r   r   rj   r   	base.corer   base.wrapped_decoratorr   VarTypeFP32FLOAT32FP64FLOAT64FP16FLOAT16BF16BFLOAT16INT32INT16INT64BOOLUINT8INT8	COMPLEX64
COMPLEX128
FP8_E4M3FNFLOAT8_E4M3FNFP8_E5M2FLOAT8_E5M2STRINGPSTRINGRAW	ALL_DTYPEvartype_to_datatyper   datatype_to_vartyper7   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r8   UINT16UINT32UINT64_PADDLE_PIR_DTYPE_2_NUMPY_DTYPEr5   r<   rB   rE   r?   r@   rC   rJ   rL   rR   rT   r   r   r   r   r   r   r   r   r   r   <module>r      s   # " " " " "      + + + + + + + + * * * * * *              M L L L L L L L L L L L       7 7 7 7 7 7 B B B B B BO(*O(* O(* O(+	
 O8> O8> O8> O(- O8> O(- Ox1 O 3 O 6 Oh2 OH,  O+! & ED(;(A(A(C(CDDD BHY)BHY) BHY) BHWx~	
 BHWx~ BHWx~ BHWx} BHX) BHWx~ BHVhm BH[8- BH\H/ J  J  J   Hhn!" Hhn#$ HhnHhmIx HhnGX]L($M8&X+8'5  :#M6#i# x# i	#
 i# M6# NG# NG# NG# NG# OX# OX# OX# # #  O!#" -## *xx x X^	
 X^ X^ HM X] h X^ HM # (% ! X+  8'! (? ? ? ?2     !<!<!>!> ? ? ?GII   :! ! !H. . . ..   >4 >4 >4 >4B        
M M M M`9 9 9 9x	 	 	  "  0 	; 	; 	;G4 G4 G4 G4 G4 G4r   