o
    #j)                     @   s  d dl Zd dlmZ d dlmZ d dlmZmZ ddl	m
Z
mZ ddlmZ ddlmZ 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 iZ!e"d	eje"d
eje"deje"deje"deje"deje"deje"deje"deje"deje"deje"dej iZ#dd Z$e a%et% e a&dd Z'dd Z(dd Z)dd Z*ed'dd Z+G d!d" d"Z,	d'd#d$Z-d%d& Z.dS )(    N)VarDesc)DataType)Programset_global_program   )	parameterset_parameter)unique_name)signature_safe_contextmanagerZfloat32Zfloat64Zfloat16Zint32Zint16Zint64Zbool_uint16Zuint8Zint8Z	complex64Z
complex128c                 C   sH   t | tr| dkrtd}nt| }|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:
        core.DataType : The data type in Paddle.

    Zbfloat16r   zNot supported numpy dtype %s)
isinstancestrnpdtypenp_type_to_paddle_typekeys
ValueError)Znp_dtyper    r   P/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/pir/core.pyconvert_np_dtype_to_dtype_9   s   
r   c                   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   r   default_startup_program[   s   r   c                   C   r   )a7  
    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   r   default_main_programx   s   !r   c                 C   s   t }| a tt  |S )z
    Switch the main program to a new program.

    Args:
        program(Program): The new main program

    Returns:
        Program: The previous main program
    )r   r   programZprev_programr   r   r   switch_main_program   s   r   c                 C   s   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   r   r   r   r   switch_startup_program   s   
r   c                 c   s    ddl m} || dtd t| } |dur!||dtd t|}zdV  W t|  |dur4t| dS dS t|  |durCt| w 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)Zbase.data_feederr    r   r   r   )r!   r"   r    r   r   r   program_guard   s0   ,
r#   c                   @   s   e Zd Zdd ZdS )ParameterMetac                 C   s   || _ || _d S N)shaper   )selfr&   r   r   r   r   __init__   s   
zParameterMeta.__init__N)__name__
__module____qualname__r(   r   r   r   r   r$      s    r$   c                 K   s   d|vrt d| d urt| tst| } |}|std}t }t }t|| }t	| |d }|||
 }	d|	_t|	| W d    n1 sKw   Y  || t	t  t|| |}
|dd}| |
_d|
_W d    |
S 1 syw   Y  |
S )NinitializerzRinitializer is None, if you want to create parameter, please pass its initializer.r   T	trainable)r   r   r   r   r	   generater   r   r$   r#   Zglobal_blockZpersistabler   Zmove_parameters_fromr   getZstop_gradient)r   r&   namekwargsZop_result_namer"   r!   Zparameter_metar,   Zinit_resultparamr-   r   r   r   create_parameter  s>   





r3   c                 C   s|   ddl }ddlm}m} ddlm} t| |jjr<| j	
| j}d}|dur0t||js.J |S ||jj | }|S | S )z'
    Convert Tensor into OpResult.
    r   N)core	framework)_global_parameter_recorderT)paddleZpaddle.baser4   r5   Z+paddle.jit.pir_dy2static.parameter_recorderr6   r   eagerZTensorblockZ_find_var_recursiver0   Variabler/   Zpirr   )Ztensorr7   r4   r5   r6   Znew_varZis_persistabler   r   r   _convert_into_opresult*  s   r;   r%   )/numpyr   Zpaddle.base.corer   Zpaddle.base.libpaddler   Zpaddle.base.libpaddle.pirr   r   Z_pir_opsr   r   baser	   Zbase.wrapped_decoratorr
   ZVarTypeZFP32ZFLOAT32ZFP64ZFLOAT64ZFP16ZFLOAT16ZBF16ZBFLOAT16ZINT32ZINT16ZINT64ZBOOLZUINT8ZINT8Z	COMPLEX64Z
COMPLEX128Zvartype_to_datatyper   r   r   r   r   r   r   r   r   r#   r$   r3   r;   r   r   r   r   <module>   s^   











$A	
&