o
    Mes4                    @   s  d dl mZ d dlZd dlmZ d dlmZ d dlZddlmZm	Z	 d dl
Z
d dlZd dlZd dlZd dlZd dlmZmZ d dlZd dlZd dlZd dlZd dlZdd	lmZ dd
lmZ ddlmZ ddlmZ d dlm Z! d dl"Z"d dl#Z#ddl$m%Z%m&Z& g dZ'e( Z)e* Z+e, Z-e. Z/e0 Z1da2e
j34dddka5da6da7d a8da9da:da;da<e= Z>e
j34dda?e
j34dd Z@dd ZAdd ZBdd ZCdd ZDdd ZEdaFdd  ZGeG  d!d" ZHd#d$ ZId%d& ZJedd'd(ZKd)aLd)aMd*ZNd+ZOedd,d-ZPedd.d/ZQdd0d1ZRdd2d3ZSd4d5 ZTd6d7 ZUd8d9 ZVd:d; ZWd<d= ZXd>d? ZYd@dA ZZe	eTZ[e	eUZ\e	eWZ]e	eYZ^e	eVZ_dBdC Z`dDdE ZadFdG ZbdHdI ZcdJdK ZddLdM ZedNdO ZfdPdQ ZgdRdS ZhdTdU ZidVdW ZjdXdY ZkdZd[ Zld\d] Zmd^d_ Znd`da Zodbdc ZpddddeZqddfdgZrddhdiZsddjdkZtddldmZuddndoZvG dpdq dqewZxex ayeddrdsZzdtdu Z{dvdw Z|dxdy Z}dzd{ Z~d|d} Zdd~dZejjjddddfddZdd ZG dd deZG dd deZeeG dd dewZdd ZG dd dewZG dd dewZG dd dewZi i fddZG dd dewZG dd deZG dd deZG dd dewZG dd dewZeeG dd deZG dd dejZeedrjejjZnewZG dd deZe ae adt_dd Zdd Zdd Zdd ZedddZdddZedd Zedd Zdd ZedddZdd ZedddZdd Zdd Zdd ZddĄ ZdS )    )print_functionN)defaultdict)Iterable   )signature_safe_contextmanagerwrap_decorator)
MethodTypeFunctionType   )compat)framework_pb2)core)unique_name)_getitem_impl__setitem_impl_)Programdefault_startup_programdefault_main_programprogram_guard
name_scopeipu_shard_guardset_ipu_shardcuda_places
cpu_places
xpu_places
mlu_placescuda_pinned_places_non_static_modein_dygraph_modeis_compiled_with_cinnis_compiled_with_cudais_compiled_with_rocmis_compiled_with_xpuis_compiled_with_npuVariablerequire_versiondevice_guard	set_flags	get_flagsZFLAGS_enable_eager_mode1FZFLAGS_USE_STANDALONE_EXECUTORZ#FLAGS_DY2ST_USE_STANDALONE_EXECUTORc                 C   s   ddl m}m} ddlm} ddlm} t| tsJ | r,|	  t
s+|  |  da
n|  ts:|  |  dat|  dS )zj
    Update monkey methods of VarBase or eager.Tensor while
    switching eager mode and legacy mode.
    r   )_C_ops_legacy_C_opsr   )monkey_patch_varbase)monkey_patch_math_varbaseTN)paddler*   r+   Zdygraph.varbase_patch_methodsr,   Zdygraphr-   
isinstanceboolZswitch_to_eager_ops_already_patch_eager_tensorZswitch_to_core_ops_already_patch_varbase_switch_tensor_bind_type)is_eagerr*   r+   r,   r-    r5   FD:\Projects\ConvertPro\env\Lib\site-packages\paddle/fluid/framework.py_update_monkey_methodsu   s"   r7   c                 C   s,   dd l }| rtjj|_ntj|_d|j_d S )Nr   Tensor)r.   r   eagerr8   VarBase__qualname__)r4   r.   r5   r5   r6   r3      s
   r3   c                   C      da tdd d S )NFr4   _in_eager_mode_r7   r5   r5   r5   r6   _enable_legacy_dygraph      r@   c                   C   r<   )NTr=   r>   r5   r5   r5   r6   _disable_legacy_dygraph   rA   rB   c                   C      t S N)r?   r5   r5   r5   r6   _in_eager_without_dygraph_check      rE   Tc                  C   sT   d} t  pt  pt  pt  }tr$|r$td dats"t	  d} d} da| S )NFzWe will fallback into legacy dygraph on NPU/XPU/MLU/IPU/ROCM devices. Because we only support new eager dygraph mode on CPU/GPU currently. T)
r   r"   r#   is_compiled_with_ipuis_compiled_with_mlur?   warningswarn_is_first_import_r@   )Zneed_fallbackZis_not_supportr5   r5   r6   _fallback_legacy_dygraph   s&   rL   c                   C   s   t duotS )aH  

    .. note::
        Dynamic graph mode is turn ON by default since paddle 2.0.0

    This API checks whether paddle runs in dynamic graph mode.

    You can turn ON static graph mode by `enable_static <../dygraph/base/disable_dygraph_en.html>`_ ,
    and turn OFF static graph mode by `disable_static <../dygraph/base/enable_dygraph_en.html>`_  .

    Returns:
        bool: Whether paddle runs in dynamic graph mode.

    Examples:
        .. code-block:: python

            import paddle
            print(paddle.in_dynamic_mode())  # True, dynamic mode is turn ON by default since paddle 2.0.0

            paddle.enable_static()
            print(paddle.in_dynamic_mode())  # False, Now we are in static mode

            paddle.disable_static()
            print(paddle.in_dynamic_mode())  # True, Now we are in dynamic mode

    N)_dygraph_tracer_r?   r5   r5   r5   r6   r      s   r   c                   C   s   t  otd uS rD   )r?   rM   r5   r5   r5   r6   _in_legacy_dygraph   s   rN   c                   C   s   t d uS rD   rM   r5   r5   r5   r6   r         r   c                 c   s<    t  }|s	t  zd V  W |st  d S d S |st  w w rD   )rL   rB   r@   )placeZalready_fallbackr5   r5   r6   _test_eager_guard   s   
rR   Z	ipu_indexZ	ipu_stagec                 c   s"    t }| a zd V  W |a d S |a w rD   )_enable_standalone_executor_)enableZ	original_r5   r5   r6   _enable_standalone_executor  s   rV   c                 c   sB    t  s	tdt}t}| a|az
dV  W |a|adS |a|aw )a  
    Used to shard the graph on IPUs. Set each Op run on which IPU in the sharding and which stage in the pipelining.

    Args:
        index(int, optional): Specify which ipu the Tensor is computed on, (such as '0, 1, 2, 3').
            The default value is -1, which means the Op only run on IPU 0.
        stage(int, optional): Specify the computation order of the sharded model(such as '0, 1, 2, 3').
            The sharded model will be computed from small to large. The default value is -1,
            which means no pipelining computation order and run Ops in terms of graph.

    Note:
        Only if the enable_manual_shard=True, the 'index' is able to be set not -1. Please refer
        to :ref:`api_paddle_static_IpuStrategy`.
        Only if the enable_pipelining=True, the 'stage' is able to be set not -1. Please refer
        to :ref:`api_paddle_static_IpuStrategy`.
        A index is allowed to match none stage or a stage. A stage is only allowed to match a new or
        duplicated index.

    Examples:
        .. code-block:: python

            # required: ipu

            import paddle
            paddle.enable_static()
            a = paddle.static.data(name='data', shape=[None, 1], dtype='int32')
            with paddle.static.ipu_shard_guard(index=0, stage=0):
                b = a + 1
            with paddle.static.ipu_shard_guard(index=1, stage=1):
                c = b + 1
            with paddle.static.ipu_shard_guard(index=0, stage=2):
                d = c + 1
    zECan not use this function since PaddlePaddle is not compiled with IPUN)r   rG   
ValueErrorglobal_ipu_indexglobal_ipu_stage)indexstageZprev_ipu_indexZprev_ipu_stager5   r5   r6   r     s   #r   c                    sl    fdd}ddl m} t| |st| r|| S tdG  fdddt| }t| j|_|| _| S )u  
    Shard the ipu with the given call function. Set every ops in call function to the given ipu sharding.

    Note:
        Only when enable_manual_shard=True to set the index to a value other than -1. please refer to :ref:`api_paddle_static_IpuStrategy` .
        Only when enable_pipelining=True to set stage to a value other than -1. please refer to :ref:`api_paddle_static_IpuStrategy` .
        An index supports a corresponding None stage or a stage, and a stage only supports a new index or a duplicate index.

    Args:
        call_func(Layer|function): Specify the call function to be wrapped.
        index(int, optional): Specify which ipu the Tensor is computed on, (such as ‘0, 1, 2, 3’).
            The default value is -1, which means the Op only run on IPU 0.
        stage(int, optional): Specify the computation order of the sharded model(such as ‘0, 1, 2, 3’).
            The sharded model will be computed from small to large. The default value is -1,
            which means no pipelining computation order and run Ops in terms of graph.

    Returns:
        The wrapped call function.

    Examples:
        .. code-block:: python

            # required: ipu

            import paddle
            paddle.enable_static()
            a = paddle.static.data(name='data', shape=[None, 1], dtype='float32')
            relu = paddle.nn.ReLU()
            relu = paddle.static.set_ipu_shard(relu, index=1, stage=1)
            relu(a)
    c                    s    fdd}|S )Nc                     s>   t d  | i |W  d    S 1 sw   Y  d S NrZ   r[   )r   argskwargs)funcrZ   r[   r5   r6   wrappers  s   $z0set_ipu_shard.<locals>.decorate.<locals>.wrapperr5   ra   rb   r]   ra   r6   decorater  s   zset_ipu_shard.<locals>.decorater   )Layerz:Unsupported type. Only accept paddle.nn.Layer or function.c                       s    e Zd Z fddZ  ZS )zset_ipu_shard.<locals>.BlockFnc                    sB   t d t j|i |W  d    S 1 sw   Y  d S r\   )r   super__call__)selfr_   r`   )	__class__rZ   r[   r5   r6   rh     s   $z'set_ipu_shard.<locals>.BlockFn.__call__)__name__
__module__r;   rh   __classcell__r5   r]   rj   r6   BlockFn  s    ro   )Zdygraph.layersrf   r/   callable	TypeErrortyperk   rj   )Z	call_funcrZ   r[   re   rf   ro   r5   r]   r6   r   Q  s   !
r   c           
      C   s  t | tstdt|  t |ttdfstdt| td| }|du s.| | kr4td|  |durNtd|}|du sH| |krNtd| tj	tj
tjtjg}g d}dd	 }|||d
kr|durxtd| |tjf  dS td| tjf  dS | d}||t|d  }|dur|d}||t|d  }	|||	d
ks|||d
k rtd| |tjf dS |||d
k rtd| tj| f dS )a  
    Check if the installed version of PaddlePaddle is in [min_version, max_version],
    if the installed version is lower than ``min_version`` or higher than ``max_version``,
    an exception will be thrown, NO returns if the installed version is satisfied.

    Args:
        min_version (str): the minimum version required (like '1.4.0').
        max_version (str, optional): the max version required (like '1.6.0'), default is None,
            meaning any version equal or higher than ``min_version`` is acceptable.

    Returns:
        None.

    Raises:
        TypeError: if the type of ``min_version`` is not str.
        TypeError: if the type of ``max_version`` is not str or type(None).
        ValueError: if the value of ``min_version`` is not in version format.
        ValueError: if the value of ``max_version`` is not in version format or None.
        Exception: if the installed version is lower than ``min_version`` or higher than ``max_version``.

    Examples:
        .. code-block:: python

            import paddle.fluid as fluid

            # any version >= 0.1.0 is acceptable.
            fluid.require_version('0.1.0')

            # if 0.1.0 <= version <= 10.0.0, it is acceptable.
            fluid.require_version(min_version='0.1.0', max_version='10.0.0')
    zJThe type of 'min_version' in require_version must be str, but received %s.NzXThe type of 'max_version' in require_version must be str or type(None), but received %s.z\d+(\.\d+){0,3}zrThe value of 'min_version' in require_version must be in format '\d+(\.\d+){0,3}', like '1.5.2.0', but received %szrThe value of 'max_version' in require_version must be in format '\d+(\.\d+){0,3}', like '1.5.2.0', but received %s)0rs   rs   rs   c                 S   sV   t jt| D ] }t| | t|| kr dS t| | t|| k r( dS qdS )Nr   rS   r   )sixmovesrangelenint)Zver_aZver_bir5   r5   r6   version_cmp  s   z$require_version.<locals>.version_cmpr   zPaddlePaddle version in [%s, %s] required, but %s installed. Maybe you are using a develop version, please make sure the version is good with your code.zPaddlePaddle version %s or higher is required, but %s installed, Maybe you are using a develop version, please make sure the version is good with your code..zJVersionError: PaddlePaddle version in [%s, %s] required, but %s installed.zVersionError: PaddlePaddle version %s or higher is required, but %s installed, please upgrade your PaddlePaddle to %s or other higher version.)r/   strrq   rr   rematchgrouprW   fluid_versionmajorminorpatchrcrI   rJ   Zfull_versionsplitrw   	Exception)
min_versionZmax_versionZcheck_formatZversion_installedZzero_versionrz   Zmin_version_splitZmin_version_to_checkZmax_version_splitZmax_version_to_checkr5   r5   r6   r%     s   
 




r%   c                        fdd}|S )Nc                     "   t  r
J d j  | i |S )Nz)We don't support %s in dynamic graph moder   rk   r^   rd   r5   r6   __impl__  s   z'_dygraph_not_support_.<locals>.__impl__r5   ra   r   r5   rd   r6   _dygraph_not_support_  s   r   c                    r   )Nc                     s"   t  s
J d j  | i |S )NpWe only support '%s()' in dynamic graph mode, please call 'paddle.disable_static()' to enter dynamic graph mode.r   r^   rd   r5   r6   r        z _dygraph_only_.<locals>.__impl__r5   r   r5   rd   r6   _dygraph_only_     r   c                    r   )Nc                     s4   ddl m} t s| sJ d j  | i |S )Nr   )in_declarative_moder   )Zdygraph.baser   r   rk   )r_   r`   r   rd   r5   r6   r     s   z#_non_static_only_.<locals>.__impl__r5   r   r5   rd   r6   _non_static_only_     	r   c                    r   )Nc                     r   )NzIn PaddlePaddle 2.x, we turn on dynamic graph mode by default, and '%s()' is only supported in static graph mode. So if you want to use this api, please call 'paddle.enable_static()' before this api to enter static graph mode.r   r^   rd   r5   r6   r   )  r   z_static_only_.<locals>.__impl__r5   r   r5   rd   r6   _static_only_(  r   r   c                 C   s   | a d S rD   )_current_pipeline_stage)r[   r5   r5   r6   _set_pipeline_stage3     r   c                    r   )Nc                     s   t d j jf )Na  '%s' only can be called by `paddle.Tensor` in dynamic graph mode. Suggestions:
  1. If you are in static graph mode, you can switch to dynamic graph mode by turning off `paddle.enable_static()` or calling `paddle.disable_static()`.
  2. If you are using `@paddle.jit.to_static`, you can turn off ProgramTranslator by calling `paddle.jit.ProgramTranslator().enable(False)`. If you have to translate dynamic graph to static graph, please use other API to replace '%s'.)AssertionErrorrk   r^   rd   r5   r6   r   @  s
   
z'_fake_interface_only_.<locals>.__impl__r5   r   r5   rd   r6   _fake_interface_only_?  r   r   c                    s   t   fdd}|S )Nc                     s8   d|v rt dt |d |d< |d  | i |S )NZ	stat_dictzJThe argument `stat_dict` has deprecated, please change it to `state_dict`.
state_dict)rI   rJ   DeprecationWarningpopr^   rd   r5   r6   rb   R  s   
z$deprecate_stat_dict.<locals>.wrapper)	functoolswrapsrc   r5   rd   r6   deprecate_stat_dictQ  s   
r   c                   C   rC   rD   rO   r5   r5   r5   r6   _dygraph_tracerg     r   c                   C   rC   rD   )_global_flags_r5   r5   r5   r6   _global_flagsk  r   r   c               
   C   sd  t d u rt r<zt } W n ty" } zd} W Y d }~nd }~ww | dkr1tt d a t S td t	 a t S t
 rtzt } W n tyZ } zd} W Y d }~nd }~ww | dkritt d a t S td t	 a t S t rzt } W n ty } zd} W Y d }~nd }~ww | dkrtt d a t S td t	 a t S t	 a t S )Nr   zoYou are using GPU version Paddle, but your CUDA device is not set properly. CPU device will be used by default.znYou are using XPU version Paddle, but your XPU device is not set properly. CPU device will be used by default.znYou are using MLU version Paddle, but your MLU device is not set properly. CPU device will be used by default.)_global_expected_place_r   r    get_cuda_device_countr   	CUDAPlace	_cuda_idsrI   rJ   CPUPlacer"   get_xpu_device_countXPUPlace_xpu_idsrH   get_mlu_device_countMLUPlace_mlu_ids)device_counter5   r5   r6   _current_expected_placeo  s`   !	r   c                 C   s   t d ur	| t _d S d S rD   )rM   Z_expected_placerQ   r5   r5   r6   "_set_dygraph_tracer_expected_place  s   
r   c                 C   s   | a t|  d S rD   r   r   r   r5   r5   r6   _set_expected_place  s   r   c                 C   s   t d |  S )z
    convert VarBase tp numpy

    Args:
        var_base(VarBase) : the VarBase to convert
    Returns (np.ndarray): the np.ndarray contain the value of VarBase
    zwpaddle.fluid.framework._var_base_to_np is deprecated, please use var_base.numpy() instead of _var_base_to_np(var_base).)rI   rJ   numpy)Zvar_baser5   r5   r6   _var_base_to_np  s   	r   c                  C   sX   dt j vr"t dkrtjdt t  t	dt jd< t j
d} t| S )NZCPU_NUMr   a  !!! The CPU_NUM is not specified, you should set CPU_NUM in the environment variable list.
CPU_NUM indicates that how many CPUPlace are used in the current task.
And if this parameter are set as N (equal to the number of physical CPU core) the program may be faster.

export CPU_NUM={} # for example, set CPU_NUM as number of physical CPU core which is {}.

!!! The default number of CPU_NUM=1.
)osenvironkeysmultiprocessing	cpu_countsysstderrwriteformatr|   getrx   )Zcpu_numr5   r5   r6   _cpu_num  s   	r   c                  C   :   t d} | rdd | dD }|S tjt }|S )NZFLAGS_selected_gpusc                 S      g | ]}t |qS r5   rx   .0sr5   r5   r6   
<listcomp>      z_cuda_ids.<locals>.<listcomp>,)r   getenvr   rt   ru   rv   r   r   )Zgpus_env
device_idsr5   r5   r6   r        
r   c                  C   r   )NZFLAGS_selected_xpusc                 S   r   r5   r   r   r5   r5   r6   r     r   z_xpu_ids.<locals>.<listcomp>r   )r   r   r   rt   ru   rv   r   r   )Zxpus_envr   r5   r5   r6   r     r   r   c                  C   r   )NZFLAGS_selected_npusc                 S   r   r5   r   r   r5   r5   r6   r     r   z_npu_ids.<locals>.<listcomp>r   )r   r   r   rt   ru   rv   r   Zget_npu_device_count)Znpus_envr   r5   r5   r6   _npu_ids  r   r   c                  C   r   )NZFLAGS_selected_mlusc                 S   r   r5   r   r   r5   r5   r6   r     r   z_mlu_ids.<locals>.<listcomp>r   )r   r   r   rt   ru   rv   r   r   )Zmlus_envr   r5   r5   r6   r     r   r   c                   C      t  S )z
    Whether this whl package can be used to run the model on XPU.

    Returns (bool): support xpu or not.

    Examples:
        .. code-block:: python

            import paddle.fluid as fluid
            support_xpu = fluid.is_compiled_with_xpu()
    )r   r"   r5   r5   r5   r6   r"        r"   c                   C   r   )z
    Whether this whl package can be used to run the model on NPU.

    Returns (bool): support npu or not.

    Examples:
        .. code-block:: python

            import paddle.fluid as fluid
            support_npu = fluid.is_compiled_with_npu()
    )r   r#   r5   r5   r5   r6   r#     r   r#   c                   C   s   t   dS )a  
    Reset signal handler registered by Paddle.

    Paddle installs signal handlers at C++ level to log debug information upon failing.
    However, conflicts can happen if another python module is making use of such signal.
    Such being the case, one may disblae paddle signal handler via this interface.

    Known frameworks that require disabling signal handler includes:
    1. TVM
    2. ADLIK

    Make sure you called paddle.disable_signal_handler() before using above mentioned frameworks.

    Returns: None

    Examples:
        .. code-block:: python

            import paddle
            paddle.disable_signal_handler()
    N)r   disable_signal_handlerr5   r5   r5   r6   r     s   r   c                   C   r   )a!  
    Whether this whl package can be used to run the model on CINN.

    Returns (bool): `True` if CINN is currently available, otherwise `False`.

    Examples:
        .. code-block:: python

            import paddle
            support_cinn = paddle.device.is_compiled_with_cinn()
    )r   r   r5   r5   r5   r6   r   %  r   r   c                   C   r   )a  
    Whether this whl package can be used to run the model on GPU.

    Returns (bool): `True` if CUDA is currently available, otherwise `False`.

    Examples:
        .. code-block:: python

            import paddle
            support_gpu = paddle.device.is_compiled_with_cuda()
    )r   r    r5   r5   r5   r6   r    4  r   r    c                   C   r   )a2  
    Whether this whl package can be used to run the model on AMD or Hygon GPU(ROCm).

    Returns (bool): `True` if ROCm is currently available, otherwise `False`.

    Examples:
        .. code-block:: python

            import paddle
            support_gpu = paddle.device.is_compiled_with_rocm()
    )r   r!   r5   r5   r5   r6   r!   C  r   r!   c                 C   B   t  sJ d| du rt } n
t| ttfs| g} dd | D S )aG  
    Note:
        For multi-card tasks, please use `FLAGS_selected_gpus` environment variable to set the visible GPU device.
        The next version will fix the problem with `CUDA_VISIBLE_DEVICES` environment variable.

    This function creates a list of :code:`paddle.CUDAPlace` objects.

    If :code:`device_ids` is None, environment variable of
    :code:`FLAGS_selected_gpus` would be checked first. For example, if
    :code:`FLAGS_selected_gpus=0,1,2`, the returned list would
    be [paddle.CUDAPlace(0), paddle.CUDAPlace(1), paddle.CUDAPlace(2)].
    If :code:`FLAGS_selected_gpus` is not set, all visible
    gpu places would be returned according to the :code:`CUDA_VISIBLE_DEVICES` environment variable.

    If :code:`device_ids` is not None, it should be the device
    ids of GPUs. For example, if :code:`device_ids=[0,1,2]`,
    the returned list would be
    [paddle.CUDAPlace(0), paddle.CUDAPlace(1), paddle.CUDAPlace(2)].

    Parameters:
        device_ids (list|tuple, optional): A list/tuple of int of GPU device ids.

    Returns:
        list of paddle.CUDAPlace: Created GPU place list.

    Examples:

        .. code-block:: python

            import paddle
            import paddle.static as static

            # required: gpu

            paddle.enable_static()

            cuda_places = static.cuda_places()

    Not compiled with CUDANc                 S      g | ]}t |qS r5   )r   r   r   Zdev_idr5   r5   r6   r         zcuda_places.<locals>.<listcomp>)r   r    r   r/   listtupler   r5   r5   r6   r   R  s   (r   c                 C   r   )a  
    **Note**:
        For multi-card tasks, please use `FLAGS_selected_xpus` environment variable to set the visible XPU device.
        This function creates a list of :code:`paddle.XPUPlace` objects.
        If :code:`device_ids` is None, environment variable of
        :code:`FLAGS_selected_xpus` would be checked first. For example, if
        :code:`FLAGS_selected_xpus=0,1,2`, the returned list would
        be [paddle.XPUPlace(0), paddle.XPUPlace(1), paddle.XPUPlace(2)].
        If :code:`FLAGS_selected_xpus` is not set, all visible
        xpu places would be returned.
        If :code:`device_ids` is not None, it should be the device
        ids of XPUs. For example, if :code:`device_ids=[0,1,2]`,
        the returned list would be
        [paddle.XPUPlace(0), paddle.XPUPlace(1), paddle.XPUPlace(2)].

    Parameters:
        device_ids (list or tuple of int, optional): list of XPU device ids.
    Returns:
        list of paddle.XPUPlace: Created XPU place list.
    Examples:
        .. code-block:: python

            # required: xpu

            import paddle
            import paddle.static as static

            paddle.enable_static()
            xpu_places = static.xpu_places()
    zNot compiled with XPUNc                 S   r   r5   )r   r   r   r5   r5   r6   r     r   zxpu_places.<locals>.<listcomp>)r   r"   r   r/   r   r   r   r5   r5   r6   r     s   r   c                 C   r   )a  
    **Note**:
        For multi-card tasks, please use `FLAGS_selected_npus` environment variable to set the visible NPU device.

    This function creates a list of :code:`paddle.NPUPlace` objects.
    If :code:`device_ids` is None, environment variable of
    :code:`FLAGS_selected_npus` would be checked first. For example, if
    :code:`FLAGS_selected_npus=0,1,2`, the returned list would
    be [paddle.NPUPlace(0), paddle.NPUPlace(1), paddle.NPUPlace(2)].
    If :code:`FLAGS_selected_npus` is not set, all visible
    npu places would be returned.
    If :code:`device_ids` is not None, it should be the device
    ids of NPUs. For example, if :code:`device_ids=[0,1,2]`,
    the returned list would be
    [paddle.NPUPlace(0), paddle.NPUPlace(1), paddle.NPUPlace(2)].

    Parameters:
        device_ids (list or tuple of int, optional): list of NPU device ids.
    Returns:
        list of paddle.NPUPlace: Created NPU place list.
    Examples:
        .. code-block:: python

            # required: npu

            import paddle
            import paddle.static as static

            paddle.enable_static()
            npu_places = static.npu_places()
    zNot compiled with NPUNc                 S   r   r5   )r   NPUPlacer   r5   r5   r6   r     r   znpu_places.<locals>.<listcomp>)r   r#   r   r/   r   r   r   r5   r5   r6   
npu_places  s    r   c                 C   s   | du rt  } t g|  S )aV  
    This function creates a list of :code:`paddle.CPUPlace` objects, and returns the created list.

    If :code:`device_count` is None, the device count would
    be determined by environment variable :code:`CPU_NUM`.
    If :code:`CPU_NUM` is not set, the default value is 1,
    i.e. CPU_NUM=1.
    :code:`CPU_NUM` indicates the number of devices used in the current task.
    The running of the program can be accelerated if :code:`CPU_NUM` is the same as the number of physical cores.

    Parameters:
        device_count (int, optional): device number. Default: None.

    Returns:
        list of paddle.CPUPlace: Created list of CPU places.

    Examples:

        .. code-block:: python

            import paddle
            import paddle.static as static

            paddle.enable_static()

            cpu_places = static.cpu_places()
    N)r   r   r   r   r5   r5   r6   r     s   r   c                 C   s0   t  sJ d| du rtt } t  g|  S )ap  
    This function creates a list of :code:`fluid.CUDAPinnedPlace` objects.

    If :code:`device_count` is None, the device count would
    be determined by environment variable :code:`CPU_NUM`.
    If :code:`CPU_NUM` is not set, the default value is 1,
    i.e. CPU_NUM=1.
    :code:`CPU_NUM` indicates the number of devices used in the current task.
    The running of the program can be accelerated if :code:`CPU_NUM` is the same as the number of physical cores.

    Parameters:
        device_count (int, optional): device number. Default: None.

    Returns:
        list of fluid.CUDAPinnedPlace: Created list of CUDA pinned places.

    Examples:
        .. code-block:: python

            import paddle.fluid as fluid
            cuda_pinned_places_cpu_num = fluid.cuda_pinned_places()
            # or
            cuda_pinned_places = fluid.cuda_pinned_places(1)

    r   N)r   r    rw   r   CUDAPinnedPlacer   r5   r5   r6   r     s   
r   c                 C   r   )a  
    This function creates a list of :code:`paddle.device.MLUPlace` objects.
    If :code:`device_ids` is None, environment variable of
    :code:`FLAGS_selected_mlus` would be checked first. For example, if
    :code:`FLAGS_selected_mlus=0,1,2`, the returned list would
    be [paddle.device.MLUPlace(0), paddle.device.MLUPlace(1), paddle.device.MLUPlace(2)].
    If :code:`FLAGS_selected_mlus` is not set, all visible
    mlu places would be returned.
    If :code:`device_ids` is not None, it should be the device
    ids of MLUs. For example, if :code:`device_ids=[0,1,2]`,
    the returned list would be
    [paddle.device.MLUPlace(0), paddle.device.MLUPlace(1), paddle.device.MLUPlace(2)].

    Note:
        For multi-card tasks, please use `FLAGS_selected_mlus` environment variable to set the visible MLU device.

    Parameters:
        device_ids (list or tuple of int, optional): list of MLU device ids.

    Returns:
        list of paddle.device.MLUPlace: Created MLU place list.

    Examples:
        .. code-block:: python

            # required: mlu

            import paddle
            import paddle.static as static

            paddle.enable_static()
            mlu_places = static.mlu_places()
    zNot compiled with MLUNc                 S   r   r5   )r   r   r   r5   r5   r6   r   :  r   zmlu_places.<locals>.<listcomp>)r   rH   r   r/   r   r   r   r5   r5   r6   r     s   "r   c                   @   s.   e Zd ZdddZdd Zdd Zd	d
 ZdS )	NameScope Nc                 C   s   t  | _|| _|| _d S rD   )dict	_children_name_parent)ri   nameparentr5   r5   r6   __init__>  s   
zNameScope.__init__c                 C   sT   || j vrt|| }|g| j |< |S t|dt| j |   | }| j | | |S )Nz_%d)r   r   rw   append)ri   prefix	new_childr5   r5   r6   childC  s   

zNameScope.childc                 C      | j S rD   )r   ri   r5   r5   r6   r   N     zNameScope.parentc                 C   r   rD   )r   r   r5   r5   r6   r   Q  r   zNameScope.name)r   N)rk   rl   r;   r   r   r   r   r5   r5   r5   r6   r   =  s
    
r   c                 c   sH    t  r	dV  dS | sJ dt| az
dV  W t adS t aw )ab  

    Generate hierarchical name prefix for the operators in Static Graph.

    Note:
        This should only used for debugging and visualization purpose.
        Don't use it for serious analysis such as graph/program transformations.
        Don't use it in dygraph, since it will cause memory leak.

    Args:
        prefix(str, optional): prefix. Default is none.

    Examples:

        .. code-block:: python

          import paddle
          paddle.enable_static()
          with paddle.static.name_scope("s1"):
             a = paddle.static.data(name='data', shape=[None, 1], dtype='int32')
             b = a + 1
             with paddle.static.name_scope("s2"):
                c = b * 1
             with paddle.static.name_scope("s3"):
                d = c / 1
          with paddle.static.name_scope("s1"):
                f = paddle.tensor.pow(d, 2.0)
          with paddle.static.name_scope("s4"):
                g = f - 1

          # Op are created in the default main program.
          for op in paddle.static.default_main_program().block(0).ops:
              # elementwise_add is created in /s1/
              if op.type == 'elementwise_add':
                  assert op.desc.attr("op_namescope") == '/s1/'
              # elementwise_mul is created in '/s1/s2'
              elif op.type == 'elementwise_mul':
                  assert op.desc.attr("op_namescope") == '/s1/s2/'
              # elementwise_div is created in '/s1/s3'
              elif op.type == 'elementwise_div':
                  assert op.desc.attr("op_namescope") == '/s1/s3/'
              # elementwise_sum is created in '/s4'
              elif op.type == 'elementwise_sub':
                  assert op.desc.attr("op_namescope") == '/s4/'
              # pow is created in /s1_1/
              elif op.type == 'pow':
                  assert op.desc.attr("op_namescope") == '/s1_1/'
    Nz"namescope prefix can not be empty.)r   _name_scoper   r   )r   r5   r5   r6   r   X  s   4

r   c                  C   s,   t } d}| r|  d | }|  } | s|S )Nr   /)r   r   r   )scoper   r5   r5   r6   _full_name_scope  s   r   c                  C   s   dd l } td t|    S )Nr   @)randomCONTROL_DEP_VAR_PREFIXr|   )r   r5   r5   r6   generate_control_dev_var_name  s   r   c                 C   s   | t  S )zD
    Returns:
        str: gradient name for a certain var name
    )GRAD_VAR_SUFFIX)var_namer5   r5   r6   grad_var_name     r   c                 C   s  t | }|t jkrtjjjS |t jkrtjjjS |t j	kr#tjjj
S |t jkr-tjjjS |t jkr7tjjjS |t jkrAtjjjS |t jkrKtjjjS |t jkrUtjjjS |t jkr_tjjjS |t jkritjjjS |t jkrstjjjS |t jkr}tjjjS td| )z
    Convert the data type in numpy to the data type in Paddle

    Args:
        np_dtype(np.dtype): the data type in numpy.

    Returns:
        core.VarDesc.VarType: the data type in Paddle.

    zNot supported numpy dtype %s)npdtypeZfloat32r   VarDescVarTypeFP32Zfloat64FP64Zfloat16FP16Zint32ZINT32Zint16ZINT16Zint64INT64Zbool_BOOLZuint16ZBF16Zuint8ZUINT8Zint8ZINT8Z	complex64Z	COMPLEX64Z
complex128Z
COMPLEX128rW   )Znp_dtyper   r5   r5   r6   convert_np_dtype_to_dtype_  s4   
























r  c                 C   s6   t | tjjst| } | tjjjtjjjtjjjfv S )z
    Check the data type is floating or not.
    Args:
        dtype(np.dtype|core.VarDesc.VarType): data type.
            Could be numpy format or Paddle format

    Returns(bool): True if data type is a float value

    )r/   r   r   r   r  r  r   r  )r   r5   r5   r6   dtype_is_floating  s   
r  c                 C   s,   t  }| |s|rtd|| |  S )aZ  
    Get the debug string of a protobuf message. The message could be not
    initialized.
    Args:
        proto(google.protobuf.message.Message): The protobuf message
        throw_on_error(bool): True if raise an error when the protobuf message
            is not initialized.

    Returns(str): The debug string of the protobuf message

    z-{0} are not initialized.
The message is {1}:
)r   ZIsInitializedrW   r   __str__)protothrow_on_errorZerror_fieldsr5   r5   r6   _debug_string_  s   r
  c                 K   s   |d urt |tjjst|}tr9tj|r|ntjjj|r"t	|ng || r(| ntjjj
|r0dnd}|  |S t|r?|ntjjj|rIt	|ng || rO| ntjjj
|rXdS dS NTF)r/   r   r   r   r  r?   r9   r8   r   r   
LOD_TENSORretain_gradsr:   )rr   r   shaper   persistabler`   Zeager_tensorr5   r5   r6   _varbase_creator  s,   
r  c                    s0   t | ttfs	J | sdS t fdd| D S )z
    Return True if type of each element is expected_type.

    NOTE: BuiltIn all() will always return True if vals is empty.
    Fc                 3   s    | ]}t | V  qd S rD   )r/   r   vexpected_typer5   r6   	<genexpr>-  s    z_all_is_type.<locals>.<genexpr>)r/   r   r   all)valsr  r5   r  r6   _all_is_type$  s   r  c                   @      e Zd Zedd ZdS )VariableMetaClassc                 C   s8   t |}t rt|tjjS t rt|tjS t|tS rD   )	rr   r   
issubclassr   r9   r8   rN   r:   r$   clsinstancetr5   r5   r6   __instancecheck__1  s   
z#VariableMetaClass.__instancecheck__Nrk   rl   r;   classmethodr   r5   r5   r5   r6   r  0      r  c                   @   r  )ParameterMetaClassc                 C   s2   t |}t rt|tS t rt|tS t|tS rD   )rr   r   r  EagerParamBaserN   	ParamBase	Parameterr  r5   r5   r6   r   =  s   


z$ParameterMetaClass.__instancecheck__Nr!  r5   r5   r5   r6   r$  <  r#  r$  c                   @   s0  e Zd ZdZejjjdddddddddddfddZdd Z	e
dd	 Ze
dcd
dZe
dd Ze
dd Ze
dd Zdd Zdd ZdcddZeZdd Zedd Zejdd Zedd Zejdd Zed d! Zejd"d! Zed#d$ Zed%d& Zejd'd$ Zed(d) Zed*d+ Zed,d- Zed.d/ Zed0d1 Z d2d3 Z!d4d5 Z"d6d7 Z#d8d9 Z$d:d; Z%d<d= Z&d>d? Z'd@dA Z(dcdBdCZ)dDdE Z*dFdG Z+dHdI Z,dJdK Z-dLdM Z.dddNdOZ/dddPdQZ0dRdS Z1dTdU Z2dVdW Z3dXdY Z4dZd[ Z5ed\d] Z6d^d_ Z7ed`da Z8e8jdbda Z8dS )er$   a  
    **Notes**:
        **The constructor of Variable should not be invoked directly.**

        **In Static Graph Mode: Please use** `Block.create_var` **to create a Static variable which has no data until being feed.**

        **In Dygraph Mode: Please use** :ref:`api_fluid_dygraph_to_variable` **to create a dygraph variable with real data**

    In Fluid, every input and output of an OP is a variable. In most
    cases, variables are used for holding different kinds of data or training
    labels. A variable belongs to a :ref:`api_guide_Block_en` . All variable has its own name and
    two variables in different :ref:`api_guide_Block_en` could have the same name.

    There are many kinds of variables. Each kind of them has its own attributes
    and usages. Please refer to the `framework.proto <https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/fluid/framework/framework.proto>`_ for details.

    Most of a Variable's member variables can be set to be None. It mean
    it is not available or will be specified later.

    Examples:
        In Static Graph Mode:

        .. code-block:: python

            import paddle.fluid as fluid
            cur_program = fluid.Program()
            cur_block = cur_program.current_block()
            new_variable = cur_block.create_var(name="X",
                                                shape=[-1, 23, 48],
                                                dtype='float32')

        In `Dygraph <../../user_guides/howto/dygraph/DyGraph.html>`_  Mode:

        .. code-block:: python

            import paddle.fluid as fluid
            import numpy as np

            with fluid.dygraph.guard():
                new_variable = fluid.dygraph.to_variable(np.arange(10))

    NFc                 K   s<  || _ |d u rtd}|d urt|tjjst|}|tjjjkr)tjjj}d }|tjjj	kr2d }|| _
|	| _d}t|}| j jt|| _| jd u r\| j jt|| _d}|re| j| n| j |krytd| j| j ||d ur|r| j| n| j}t|}||krtd| j|||d ur|r| j| n| j}||krtd| j|||d ur|r| j| n|| jkrtd| j| j||d ur|r| j| n|| jkrtd| j| j||r|r| j | |d ur|r| j!| n	 | | j j"|< d | _#|
| _$|| _%d S )	NZ_generated_varFTzkVariable '{0}' has been created before. The previous type is {1}, the new type is {2}. They are not matchedznVariable '{0}' has been created before. The previous shape is {1}, the new shape is {2}. They are not matched.zvVariable '{0}' has been created before. The previous data type is {1}, the new data type is {2}. They are not matched.zuVariable '{0}' has been created before. The previous lod_level is {1}, the new lod_level is {2}. They are not matchedzxVariable '{0}' has been created before.The previous persistable is {1}, the new persistable is {2}. They are not matched)&blockr   generater/   r   r   r   r  STRINGSZ
SPARSE_COObelong_to_optimizer
error_clipcptto_textdescfind_varto_bytesvarset_typerr   rW   r   r   	set_shaper  r   	set_dtyper   Zset_lod_level	lod_levelset_persistabler  set_need_check_feedZset_capacityvarsopstop_gradientis_data)ri   r(  rr   r   r  r   r6  capacityr  r,  r;  r<  need_check_feedr+  r`   Z
is_new_varZ	old_shapeZ	old_dtyper5   r5   r6   r   u  s   








zVariable.__init__c                 C   sr   | j tjjjks| j tjjjksJ d| jjt	d| j
 | j| j | jdd}| jjdd| gid|gid |S )	a  
        Returns a new Variable, detached from the current graph.
        It will share data with origin Variable and without tensor copy.
        In addition, the detached Variable doesn't provide gradient propagation.

        Returns:
             ( :ref:`api_guide_Variable_en` | dtype is same as current Variable): The detached Variable.

        Examples:
            .. code-block:: python

                import paddle

                paddle.enable_static()

                # create a static Variable
                x = paddle.static.data(name='x', shape=[3, 2, 1])

                # create a detached Variable
                y = x.detach()
        zGonly support a variable with SELECTED_ROWS or LOD_TENSOR to be detachedZdetach_Tr   r   rr   r  r;  Z
share_dataXOutrr   inputsoutputs)rr   r   r   r   SELECTED_ROWSr  r(  
create_varr   generate_with_ignorable_keyr   r   r  	append_opri   outputr5   r5   r6   detach  s   zVariable.detachc                 C      dS )aj  
        **Notes**:
            **This API is ONLY available in Dygraph mode**

        Returns a numpy array shows the value of current :ref:`api_guide_Variable_en`

        Returns:
            ndarray: The numpy value of current Variable.

        Returns type:
            ndarray: dtype is same as current Variable

        Examples:
            .. code-block:: python

                import paddle.fluid as fluid
                from paddle.fluid.dygraph.base import to_variable
                from paddle.fluid.dygraph import Linear
                import numpy as np

                data = np.random.uniform(-1, 1, [30, 10, 32]).astype('float32')
                with fluid.dygraph.guard():
                    linear = Linear(32, 64)
                    data = to_variable(data)
                    x = linear(data)
                    print(x.numpy())

        Nr5   r   r5   r5   r6   r     s   zVariable.numpyc                 C   rL  )a$  
        **Notes**:
            **This API is ONLY available in Dygraph mode**

        Run backward of current Graph which starts from current Tensor.

        Args:
            retain_graph(bool, optional): If False, the graph used to compute grads will be freed. If you would
                like to add more ops to the built graph after calling this method( :code:`backward` ), set the parameter
                :code:`retain_graph` to True, then the grads will be retained. Thus, seting it to False is much more memory-efficient.
                Defaults to False.

        Returns:
            NoneType: None

        Examples:
            .. code-block:: python

                import numpy as np
                import paddle
                paddle.disable_static()

                x = np.ones([2, 2], np.float32)
                inputs = []
                for _ in range(10):
                    tmp = paddle.to_tensor(x)
                    # if we don't set tmp's stop_gradient as False then, all path to loss will has no gradient since
                    # there is no one need gradient on it.
                    tmp.stop_gradient=False
                    inputs.append(tmp)
                ret = paddle.add_n(inputs)
                loss = paddle.sum(ret)
                loss.backward()

        Nr5   )ri   Zretain_graphr5   r5   r6   backward4  s   %zVariable.backwardc                 C   rL  )a  
        **Notes**:
            **This API is ONLY available in Dygraph mode**

        Get the Gradient of Current Variable

        Returns:
            ndarray or tuple of ndarray: if Variable's type is LoDTensor, return numpy value of the gradient of current Variable, if Variable's type is SelectedRows, return tuple of ndarray, first element of tuple is numpy value of the gradient of current Variable, second element of tuple is numpy value of the rows of current Variable.

        Examples:
            .. code-block:: python

                import paddle.fluid as fluid
                import numpy as np

                # example1: return ndarray
                x = np.ones([2, 2], np.float32)
                with fluid.dygraph.guard():
                    inputs2 = []
                    for _ in range(10):
                        tmp = fluid.dygraph.base.to_variable(x)
                        tmp.stop_gradient=False
                        inputs2.append(tmp)
                    ret2 = fluid.layers.sums(inputs2)
                    loss2 = fluid.layers.reduce_sum(ret2)
                    loss2.backward()
                    print(loss2.gradient())

                # example2: return tuple of ndarray
                with fluid.dygraph.guard():
                    embedding = fluid.dygraph.Embedding(
                        size=[20, 32],
                        param_attr='emb.w',
                        is_sparse=True)
                    x_data = np.arange(12).reshape(4, 3).astype('int64')
                    x_data = x_data.reshape((-1, 3, 1))
                    x = fluid.dygraph.base.to_variable(x_data)
                    out = embedding(x)
                    out.backward()
                    print(embedding.weight.gradient())

        Nr5   r   r5   r5   r6   gradient[  s   ,zVariable.gradientc                 C   rL  )aF  
        **Notes**:
            **1. This API is ONLY available in Dygraph mode**

            **2. Use it only Variable has gradient, normally we use this for Parameters since other temporal Variable will be deleted by Python's GC**

        Clear  (set to ``0`` ) the Gradient of Current Variable

        Returns:  None

        Examples:
            .. code-block:: python

                import paddle.fluid as fluid
                import numpy as np

                x = np.ones([2, 2], np.float32)
                with fluid.dygraph.guard():
                    inputs2 = []
                    for _ in range(10):
                        tmp = fluid.dygraph.base.to_variable(x)
                        tmp.stop_gradient=False
                        inputs2.append(tmp)
                    ret2 = fluid.layers.sums(inputs2)
                    loss2 = fluid.layers.reduce_sum(ret2)
                    loss2.backward()
                    print(loss2.gradient())
                    loss2.clear_gradient()
                    print("After clear {}".format(loss2.gradient()))

        Nr5   r   r5   r5   r6   clear_gradient     !zVariable.clear_gradientc                 C   s   d S rD   r5   )ri   hookr5   r5   r6   register_hook  rF   zVariable.register_hookc                 C      |   S rD   _to_readable_coder   r5   r5   r6   r    rP   zVariable.__str__c                 C   s   t | jdd }| jtjjjks| jtjjjkr2t | jdd }dj	| j
|| j|| jd}ndj	| j
|d}| jrJ| jrEd| }n	d| }nd	| }| jrUd
| }ddlm} | }|| }|durp|dj	d|d7 }|S )a  
        Get readable debug string of Variable.

        .. note::
            If you want to get the debug string in protobuf format,
            please use :code:`to_string` method.

        Returns:
            string: The formatted Variable string.

        Examples:
            .. code-block:: python

                import paddle
                import paddle.static as static

                paddle.enable_static()

                cur_program = static.Program()
                cur_block = cur_program.current_block()
                new_variable = cur_block.create_var(name="X",
                                                    shape=[-1, 23, 48],
                                                    dtype='float32')
                print(new_variable._to_readable_code())
        r{   r   zJ{name} : {type}.shape{shape}.dtype({dtype}).stop_gradient({stop_gradient}))r   rr   r  r   r;  z{name} : {type}))r   rr   ztrainable param param zvar zpersist r   get_default_distributed_contextN, {name} = {value}	dist_attrr   value)r|   rr   r   r   r   r   rE  r  r   r   r   r  r;  is_parameter	trainabler  -paddle.distributed.auto_parallel.dist_contextrX  Zget_dist_tensor_for_program)ri   Ztype_strZ	dtype_strZvar_strrX  dist_contextZdist_tensorr5   r5   r6   rU    s6   


zVariable._to_readable_codec              	   C   sp   t |tr
t |tsJ | j }tjt|}t	||}|r6d}|D ]}|d|t
t| |f 7 }q%|S )a  
        Get debug string.

        Args:

            throw_on_error (bool): True if raise an exception when self is not initialized.

            with_details (bool): more details about variables and parameters (e.g. trainable, optimize_attr, ...) will be printed when with_details is True. Default value is False;

        Returns:
            str: The debug string.

        Examples:
            .. code-block:: python

                import paddle.fluid as fluid
                import paddle

                paddle.enable_static()
                cur_program = fluid.Program()
                cur_block = cur_program.current_block()
                new_variable = cur_block.create_var(name="X",
                                                    shape=[-1, 23, 48],
                                                    dtype='float32')
                print(new_variable.to_string(True))
                print("=============with detail===============")
                print(new_variable.to_string(True, True))
        r,  %s: %s
)r/   r0   r/  serialize_to_stringr   r   
FromStringrt   binary_typer
  r-  r.  getattr)ri   r	  with_detailsprotostrr  res_stradditional_attr	attr_namer5   r5   r6   	to_string  s   


zVariable.to_stringc                 C   
   | j  S )a  
        Returns the size in bytes of an element in the Tensor.

        Examples:
          .. code-block:: python

            import paddle
            paddle.enable_static()

            x = paddle.static.data(name='x1', shape=[3, 2], dtype='bool')
            x.element_size() # 1

            x = paddle.static.data(name='x2', shape=[3, 2], dtype='int16')
            x.element_size() # 2

            x = paddle.static.data(name='x3', shape=[3, 2], dtype='float16')
            x.element_size() # 2

            x = paddle.static.data(name='x4', shape=[3, 2], dtype='float32')
            x.element_size() # 4

            x = paddle.static.data(name='x5', shape=[3, 2], dtype='float64')
            x.element_size() # 8
        )r/  element_sizer   r5   r5   r6   rn  %     
zVariable.element_sizec                 C   rm  )a,  
        Indicating if we stop gradient from current Variable

        **Notes: This Property has default value as** ``True`` **in** `Dygraph <../../user_guides/howto/dygraph/DyGraph.html>`_ **mode, while Parameter's default value is False. However, in Static Graph Mode all Variable's default stop_gradient value is** ``False``

        Examples:
          .. code-block:: python

            import paddle.fluid as fluid
            import numpy as np

            with fluid.dygraph.guard():
                value0 = np.arange(26).reshape(2, 13).astype("float32")
                value1 = np.arange(6).reshape(2, 3).astype("float32")
                value2 = np.arange(10).reshape(2, 5).astype("float32")
                linear = fluid.Linear(13, 5, dtype="float32")
                linear2 = fluid.Linear(3, 3, dtype="float32")
                a = fluid.dygraph.to_variable(value0)
                b = fluid.dygraph.to_variable(value1)
                c = fluid.dygraph.to_variable(value2)
                out1 = linear(a)
                out2 = linear2(b)
                out1.stop_gradient = True
                out = fluid.layers.concat(input=[out1, out2, c], axis=1)
                out.backward()

                assert linear.weight.gradient() is None
                assert (out1.gradient() == 0).all()
        )r/  r;  r   r5   r5   r6   r;  @  s   
zVariable.stop_gradientc                 C      | j | d S rD   )r/  Zset_stop_gradient)ri   r   r5   r5   r6   r;  a     c                 C   rm  )a]  
        Indicating if we current Variable should be long-term alive


        **Notes: This Property will be deprecated and this API is just to help user understand concept**

            **1. All Variable's persistable is** ``False`` **except Parameters.**

            **2. In** `Dygraph <../../user_guides/howto/dygraph/DyGraph.html>`_ **mode, this property should not be changed**

        Examples:
          .. code-block:: python

            import paddle.fluid as fluid
            cur_program = fluid.Program()
            cur_block = cur_program.current_block()
            new_variable = cur_block.create_var(name="X",
                                                shape=[-1, 23, 48],
                                                dtype='float32')
            print("persistable of current Var is: {}".format(new_variable.persistable))
        )r/  r  r   r5   r5   r6   r  e  s   
zVariable.persistablec                 C   rp  rD   )r/  r7  ri   pr5   r5   r6   r  ~  rq  c                 C   rm  )a+  
        Indicating if current Variable is a Parameter

        Examples:
          .. code-block:: python

            import paddle
            new_parameter = paddle.static.create_parameter(name="X",
                                                shape=[10, 23, 48],
                                                dtype='float32')
            if new_parameter.is_parameter:
                print("Current var is a Parameter")
            else:
                print("Current var is not a Parameter")

            # Current var is a Parameter
        )r/  r]  r   r5   r5   r6   r]    s   
zVariable.is_parameterc                 C   rp  rD   )r/  Zset_is_parameterrr  r5   r5   r6   r]    rq  c                 C   s   t | j S )a  
        Indicating name of current Variable

        **Notes: If it has two or more Varaible share the same name in the same** :ref:`api_guide_Block_en` **, it means these Variable will share content in no-** `Dygraph <../../user_guides/howto/dygraph/DyGraph.html>`_ **mode. This is how we achieve Parameter sharing**

        Examples:
          .. code-block:: python

            import paddle.fluid as fluid
            cur_program = fluid.Program()
            cur_block = cur_program.current_block()
            new_variable = cur_block.create_var(name="X",
                                                shape=[-1, 23, 48],
                                                dtype='float32')
            print("name of current Var is: {}".format(new_variable.name))
        )r-  r.  r/  r   r   r5   r5   r6   r     s   zVariable.namec                 C   s
   | j d S )a  
        Indicating name of the gradient Variable of current Variable.

        **Notes: This is a read-only property. It simply returns name of
        gradient Variable from a naming convention but doesn't guarantee
        the gradient exists.**

        Examples:
          .. code-block:: python

          import paddle.fluid as fluid

          x = fluid.data(name="x", shape=[-1, 23, 48], dtype='float32')
          print(x.grad_name) # output is "x@GRAD"

        z@GRADr   r   r5   r5   r6   	grad_name     
zVariable.grad_namec                 C   rp  rD   )r/  set_name)ri   new_namer5   r5   r6   r     rq  c                 C      t | j S )a/  
        Indicating shape of current Variable

        **Notes: This is a read-only property**

        Examples:
          .. code-block:: python

            import paddle.fluid as fluid
            cur_program = fluid.Program()
            cur_block = cur_program.current_block()
            new_variable = cur_block.create_var(name="X",
                                                shape=[-1, 23, 48],
                                                dtype='float32')
            print("shape of current Var is: {}".format(new_variable.shape))

        )r   r/  r  r   r5   r5   r6   r    s   zVariable.shapec                 C   rm  )a2  
        Indicating data type of current Variable

        **Notes: This is a read-only property**

        Examples:
          .. code-block:: python

            import paddle.fluid as fluid
            cur_program = fluid.Program()
            cur_block = cur_program.current_block()
            new_variable = cur_block.create_var(name="X",
                                                shape=[-1, 23, 48],
                                                dtype='float32')
            print("Dtype of current Var is: {}".format(new_variable.dtype))
        )r/  r   r   r5   r5   r6   r     rv  zVariable.dtypec                 C   s6   | j tjjjkrtd| j tjjjkrdS | j S )a~  
        Indicating ``LoD`` info of current Variable, please refer to  :ref:`api_fluid_LoDTensor_en` to check the meaning
        of ``LoD``

        **Notes**:

            **1. This is a read-only property**

            **2. Don't support this property in** `Dygraph <../../user_guides/howto/dygraph/DyGraph.html>`_ **mode, it's value should be** ``0(int)``

        Examples:
          .. code-block:: python

            import paddle
            import paddle.fluid as fluid

            paddle.enable_static()
            cur_program = fluid.Program()
            cur_block = cur_program.current_block()
            new_variable = cur_block.create_var(name="X",
                                                shape=[-1, 23, 48],
                                                dtype='float32')
            print("LoD Level of current Var is: {}".format(new_variable.lod_level))
        zSelectedRows DO NOT supprt lodN)	rr   r   r   r   rE  r   r*  r/  r6  r   r5   r5   r6   r6    s
   
zVariable.lod_levelc                 C   rm  )a+  
        Indicating Type of current Variable

        **Notes: This is a read-only property**

        Examples:
          .. code-block:: python

            import paddle.fluid as fluid
            cur_program = fluid.Program()
            cur_block = cur_program.current_block()
            new_variable = cur_block.create_var(name="X",
                                                shape=[-1, 23, 48],
                                                dtype='float32')
            print("Type of current Var is: {}".format(new_variable.type))
        r/  rr   r   r5   r5   r6   rr     rv  zVariable.typec                 C   s   t | jdkr	| S g }tt | jD ]}|d| q| jjt| jd | j	| j
ddd}| jjt| jd | j	tjjjddd}| jjdd| gi|g|gdd	|id
 |S )aB  
        Permute current Variable with its dimensions reversed.

        If `n` is the dimensions of `x` , `x.T` is equivalent to `x.transpose([n-1, n-2, ..., 0])`.

        Examples:

            .. code-block:: python

                import paddle
                paddle.enable_static()

                x = paddle.ones(shape=[2, 3, 5])
                x_T = x.T

                exe = paddle.static.Executor()
                x_T_np = exe.run(paddle.static.default_main_program(), fetch_list=[x_T])[0]
                print(x_T_np.shape)
                # (5, 3, 2)
        r   r   z.tmpFr?  Z
transpose2r@  )rA  ZXShapeaxisrr   rC  rD  attrs)rw   r  rv   insertr(  rF  r   rG  r   r   rr   r   r   r   r  rH  )ri   permry   outZinput_shaper5   r5   r6   T%  s4   z
Variable.Tc                 C   sL   | j jt| jd | j| j| j| jd}| j j	dd| gid|gid |S )a  
        Returns a new static Variable, which is the clone of the original static
        Variable. It remains in the current graph, that is, the cloned Variable
        provides gradient propagation. Calling ``out = tensor.clone()`` is same
        as ``out = assign(tensor)`` .

        Returns:
            Variable: The cloned Variable.

        Examples:
            .. code-block:: python

                import paddle

                paddle.enable_static()

                # create a static Variable
                x = paddle.static.data(name='x', shape=[3, 2, 1])
                # create a cloned Variable
                y = x.clone()

        Z_cloner?  Zassignr@  rA  rB  )
r(  rF  r   rG  r   r   rr   r  r;  rH  rI  r5   r5   r6   cloneX  s   zVariable.clonec                 C   s
   || _ dS )z
        Set the error_clip.

        Args:
            error_clip(BaseErrorClipAttr) : The new error_clip.

        Returns:
            None
        Nra  )ri   r,  r5   r5   r6   _set_error_clip|  s   

zVariable._set_error_clipc                 C   s   t | dsi | _|| j|< dS )z
        Set key-value information for this variable.

        Args:
            key(str): Key for this information.
            value(object): The value associated to the key.

        Returns:
            None
        _infoNhasattrr  )ri   keyr\  r5   r5   r6   	_set_info  s   
zVariable._set_infoc                 C   s"   t | dr|| jv r| j| S dS )z
        Get the information of this variable corresponding to key.

        Args:
            key(str): Key for this information.

        Returns:
            object
        r  Nr  )ri   r  r5   r5   r6   	_get_info  s   

zVariable._get_infoc                 C   s   |j du rdn|j }|dk rtd|dkrtd|dk r dnd}|dk r*|d n|}|jdu r:|dk r7|n|}n|j}|dk rHt|| |nt||}|jdu r[|dk rX|n|}n|j}|dk rit|| |nt||}|||fS )zH
        Reference implementation for the slice.indices method.
        Nr   r   zlength should not be negativezslice step can not be zerorS   )steprW   startmaxminstop)ri   slicelengthr  lowerupperr  r  r5   r5   r6   _slice_indices  s"   

 
zVariable._slice_indicesc                 C   sT   d}d}t | j}t|D ]\}}|tu r |rtdd}|}q|r$|}q|||fS )NFr   z!Index can have one ellipsis only.T)rw   r  	enumerateEllipsisrW   )ri   itemhas_ellipsisr  endrZ   or5   r5   r6   _detectEllipsis  s   

zVariable._detectEllipsisc                 C   s   |  |\}}}|r>g }t|D ]	}|||  qt||D ]}|td d d  qt|t|D ]	}|||  q2|S d S rD   )r  rv   r   r  rw   )ri   r  r  r  r  newitemry   r5   r5   r6   _reconstructSliceinfo  s   zVariable._reconstructSliceinfoc           	      C   s
  g }g }t |D ]v\}}t|trTt|}|dkr || j| ks-|dk r1|| j|  dk r1td|dk r?t|| j|  dnt|| j| }|| ||d  qt|tr{| 	|| j| \}}}|dksm|dkrx|| || q dS tdd||gfS )Nr   invalid indexr   rS   )FNz+Valid index accept int or slice or ellipsisT)
r  r/   rx   r  
IndexErrorr  r  r   r  r  )	ri   r  startsendsrZ   r  r  r  r  r5   r5   r6   _detectContinuesSlice  s,   



zVariable._detectContinuesSlicec                 C   s"   |s| j jt| j| jdS | S )Nr   r   )r(  rF  r   rG  r   r   )ri   copyr5   r5   r6   	_cloneVar	  s   
zVariable._cloneVarc                 C   s4   |   }| jjdd| gid|gi|||dd |S )Nr  InputrA  )axesr  r  r|  r  r(  rH  )ri   r  r  r  new_varr5   r5   r6   	_sliceVar
	  s   
zVariable._sliceVarc                 C   s.   |   }| jjdd|id|gid|id |S )Nconcatr@  rA  r{  r|  r  )ri   rC  r{  r  r5   r5   r6   
_concatVar	  s   zVariable._concatVarc                 C   s^  t |trn| j| dk r| dS | || j| \}}}|dkr+| |g|g|gS g }|dkrM||k rL|| |g|g|d g ||7 }||k s5n||krh|| |g|g|d g ||7 }||ksQ| ||S t |tr| j| dk r| dS t|}|dkr|| j| ks|dk r|| j|  dk rt	d| |g|g|d gS t	d)Nr   Tr   r  z(Valid index accept int or slice or tuple)
r/   r  r  r  r  r  r   r  rx   r  )ri   r  r{  r  r  r  r9  rZ   r5   r5   r6   _sliceAndConcatVar 	  s>   



zVariable._sliceAndConcatVarc                 C   s
   t | |S rD   )r   )ri   r  r5   r5   r6   __getitem__B	     
zVariable.__getitem__c                 C   s   t | ||S rD   )r   )ri   r  r\  r5   r5   r6   __setitem__E	     zVariable.__setitem__c                 C   sp   ddl m} |durt|tjstdt||du r | }|| j	}|du r2t
d| j	| }|S )a  
        Get the value of variable in given scope.

        Args:
            scope(Scope, optional) : If `scope` is None, it will be set to global scope
                obtained through 'paddle.static.global_scope()'. Otherwise, use `scope`.
                Default: None

        Returns:
            Tensor: the value in given scope.

        Examples:
            .. code-block:: python

                import paddle
                import paddle.static as static
                import numpy as np

                paddle.enable_static()

                x = static.data(name="x", shape=[10, 10], dtype='float32')

                y = static.nn.fc(x, 10, name='fc')
                place = paddle.CPUPlace()
                exe = static.Executor(place)
                prog = paddle.static.default_main_program()
                exe.run(static.default_startup_program())
                inputs = np.ones((10, 10), dtype='float32')
                exe.run(prog, feed={'x': inputs}, fetch_list=[y, ])
                path = 'temp/tensor_'
                for var in prog.list_vars():
                    if var.persistable:
                        t = var.get_value()
                        paddle.save(t, path+var.name+'.pdtensor')

                for var in prog.list_vars():
                    if var.persistable:
                        t_load = paddle.load(path+var.name+'.pdtensor')
                        var.set_value(t_load)
        r   global_scopeNF`scope` should be None or `paddle.static.Scope` type, but received {}.(Can not find Variable '{}' in the Scope.)executorr  r/   r   _Scoperq   r   rr   r0  r   rW   
get_tensor)ri   r   r  var_tempr  r5   r5   r6   	get_valueH	  s    ,
zVariable.get_valuec           	      C   s  ddl m} t|tjst|dstdt||dur-t|t	j
s-tdt||du r4| }|| j}|du rFtd| j| }t|dryt|jttfr\| }n|j}t| t|krytd	| jt| t|| }| rt	 }n`| rt	 }nW| rt	 }||  t	| }n@| rt	 }||  t	| }n)|  rt	 }||  t	!|" }nt	 }||  t	#|$ }|%|| dS )
a  
        Set the value to the tensor in given scope.

        Args:
            value(Tensor/ndarray) : The value to be set.
            scope(Scope, optional) : If `scope` is None, it will be set to global scope
                obtained through 'paddle.static.global_scope()'. Otherwise, use `scope`.
                Default: None

        Returns:
            None

        Examples:
            .. code-block:: python

                import paddle
                import paddle.static as static
                import numpy as np

                paddle.enable_static()

                x = static.data(name="x", shape=[10, 10], dtype='float32')

                y = static.nn.fc(x, 10, name='fc')
                place = paddle.CPUPlace()
                exe = static.Executor(place)
                prog = paddle.static.default_main_program()
                exe.run(static.default_startup_program())
                inputs = np.ones((10, 10), dtype='float32')
                exe.run(prog, feed={'x': inputs}, fetch_list=[y, ])
                path = 'temp/tensor_'
                for var in prog.list_vars():
                    if var.persistable:
                        t = var.get_value()
                        paddle.save(t, path+var.name+'.pdtensor')

                for var in prog.list_vars():
                    if var.persistable:
                        t_load = paddle.load(path+var.name+'.pdtensor')
                        var.set_value(t_load)
        r   r  Z	__array__zB`value` should be `numpy.ndarray` or `LoDTensor`, but received {}.Nr  r  r  z5{} expected a shape {}, but the received shape is {}.)&r  r  r/   r   Zndarrayr  rq   r   rr   r   r  r0  r   rW   r  r  r   r	   r   Z_placeZis_cpu_placer   Zis_cuda_pinned_placer   Zis_xpu_placePlaceZ	set_placer   Zxpu_device_idZis_npu_placer   Znpu_device_idZis_mlu_placer   Zmlu_device_idr   Zgpu_device_idset)	ri   r\  r   r  r  r  Zvalue_shapers  rQ   r5   r5   r6   	set_value	  sh   .




zVariable.set_valuec                 C   sD   | j jt| jd tjjjd}| j j	dd| gid|gid |S )a	  
        Returns the number of elements for current Variable, which is a int64 Variable with shape [1]

        Returns:
            Variable: the number of elements for current Variable

        Examples:
            .. code-block:: python

                import paddle

                paddle.enable_static()

                # create a static Variable
                x = paddle.static.data(name='x', shape=[3, 2, 1])

                # get the number of elements of the Variable
                y = x.size()
        _sizer  sizer  rA  rB  )
r(  rF  r   rG  r   r   r   r   r  rH  rI  r5   r5   r6   r  	  s   zVariable.sizec                 C      |  || dS )z
        Set the value of attribute by attribute's name.

        Args:
            name(str): the attribute name.
            val(int|str|list): the value of the attribute.
        N_update_desc_attrri   r   valr5   r5   r6   	_set_attr
     zVariable._set_attrc                 C      | j |S )z
        Whether this Variable has the attribute with the name `name` or not.

        Args:
            name(str): the attribute name.

        Returns:
            bool: True if has this attribute.
        r/  has_attrri   r   r5   r5   r6   	_has_attr
     
zVariable._has_attrc                 C   rp  rD   r/  remove_attrr  r5   r5   r6   _remove_attr)
     zVariable._remove_attrc                 C      | j || dS )z
        Update the value of desc's attribute by attribute's name.

        Args:
            name(str): the attribute name.
            val(int|str|list): the value of the attribute.
        N)r/  r  r  r5   r5   r6   r  ,
     zVariable._update_desc_attrc                 C   rm  )z(Get the names of all attributes defined.r/  
attr_namesr   r5   r5   r6   r  6
  s   
zVariable.attr_namesc                 C   r  )z
        Get the attribute by name.

        Args:
            name(str): the attribute name.

        Returns:
            int|str|list: The attribute value. The return value
            can be any valid attribute type.
        r/  attrr  r5   r5   r6   	_get_attr;
     zVariable._get_attrc                 C      | j jS z=
        Get distributed attribute of this Variable.
        r/  rZ  r   r5   r5   r6   rZ  H
  r   zVariable.dist_attrc                 C      || j _dS z=
        Set distributed attribute of this Variable.
        Nr  ri   rZ  r5   r5   r6   rZ  O
     FrD   )9rk   rl   r;   __doc__r   r   r   r  r   rK  fake_interface_onlyr   rM  rN  rO  rR  r  rU  rl  __repr__rn  propertyr;  setterr  r]  r   ru  r  r   r6  rr   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rZ  r5   r5   r5   r6   r$   H  s    .
v)
&
-
"

C-
 












2$#
	
"

?m



r$   c                  C   s6   t  } g }| D ]}tjt|}|| q|S )zl
    Get all registered op proto from PaddlePaddle C++ end.

    Returns:
       list: list of OpProto.
    )r   get_all_op_protosr   ZOpProtord  rt   re  r   )Z	protostrsZ
ret_valuesZpbstrZop_protor5   r5   r6   r  W
  s   r  c                   @   s@   e Zd ZdZedd Zdd Zdd Zdd	 Ze	d
d Z
dS )OpProtoHolderzB
    A global variable to hold all OpProtos from C++ as a map
    c                 C   s   t | ds	|  | _| jS )N	_instance)r  r  )r  r5   r5   r6   r  k
  s   
zOpProtoHolder.instancec                 C   s:   t | jdr
J dt }i | _|D ]}|| j|j< qd S )Nr  z4Please use `instance()` to get OpProtoHolder object!)r  rj   r  op_proto_maprr   )ri   	op_protosr  r5   r5   r6   r   q
  s   zOpProtoHolder.__init__c                 C   s    || j vrtd| | j | S )z
        Get OpProto by a type string.
        Args:
            type(str): The type that operator registered in C++ side.

        Returns(framework_pb2.OpProto): The OpProto

        z&Operator "%s" has not been registered.)r  rW   )ri   rr   r5   r5   r6   get_op_protoz
  s   
	
zOpProtoHolder.get_op_protoc                 C   s<   t  }g }|D ]}|j| jvr|| j|j< ||j q|S rD   )r  rr   r  r   )ri   r  Zcustom_op_namesr  r5   r5   r6   update_op_proto
  s   zOpProtoHolder.update_op_protoc                   C   s,   t j t j t j t j t j hS rD   )r   op_proto_and_checker_makerkOpRoleAttrNamekOpRoleVarAttrNamekOpNameScopeAttrNamekOpCreationCallstackAttrNamekOpDeviceAttrNamer5   r5   r5   r6   generated_op_attr_names
  s   z%OpProtoHolder.generated_op_attr_namesN)rk   rl   r;   r  r"  r  r   r  r  staticmethodr  r5   r5   r5   r6   r  f
  s    
	
r  c                   @   sX  e Zd ZdZh dZ	dHddZdd Zdd	 ZdIddZdd Z	e	Z
edd Zdd Zdd Zdd Zedd Zedd Zedd Zdd Zedd  Zed!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zed/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Z d9d: Z!d;d< Z"d=d> Z#d?d@ Z$dAdB Z%dCdD Z&edEdF Z'e'j(dGdF Z'dS )JOperatora  
    In Fluid, all the operation are represented by Operator, and Operator
    is regarded as a build in an instruction of a Block. Users can use the
    build in instructions to describe their neural network.

    Args:
        block(Block): The block has the current operator.
        desc(core.OpDesc): The protobuf description of Operator.
        type(str): The type of operator. Default None.
        inputs(dict): The input of this Operator. it is a dictionary, for every
            element, key is the input parameter name, and value is a list of
            variables. Default None.
        outputs(dict): The output of this Operator. it is a dictionary, for
            every element, key is the input parameter name, and value is a list
            of variables. Default None.
        attrs(dict): The attributes of this Operator. it is a dictionary, for
            every element, key is attribute name, and value is the attribute value.
            The attribute type should be as same as the type registered in C++ side.
            Default None.

    Returns:
        Operator: The initialized Operator.

    Raises:
        ValueError: If the passed input, output and attrs doesn't match the
            initializing Operator's that registered in C++ side.

    Notes:
        The constructor of operator should not be invoked directly. Use
        Block.append_op or Block._prepend_op instead.

    Examples:
        .. code-block:: python

            import paddle.fluid as fluid
            cur_program = fluid.Program()
            cur_block = cur_program.current_block()
            # var1 += var2 + var3
            cur_block.append_op(type="sum",
                                inputs={"X": [var1, var2, var3]},
                                outputs={"Out": [var1]})
    >   Zc_comm_initwhileZc_gen_bkcl_idZcheckpoint_notifyselectZc_gen_hccl_idZc_sync_calc_streamZncclInitZc_gen_cncl_idZfl_listen_and_servZheter_listen_and_servgoZc_wait_commZcopy_cross_scopeZgen_bkcl_iddequeueZc_comm_init_hcclenqueueZgen_nccl_idrecvZc_gen_nccl_idc_sync_comm_streamZlisten_and_servZ	recurrentfeedZc_wait_computeZconditional_blocksendZqueue_generatorfetchZrnn_memory_helper_gradNc              	   C   s6  zt  |}i | _|jD ]	}|j| j|j< qW n	 ty"   Y nw t r=|d u r.td|| _	|r8|| _d S i | _d S || _
|| _|}	|	d u rLt }	~t| _tj}
|
 |	vrb| j
jj|	|
 < |
 }t| j
jjdkrz||	vrz| j
jj|	|< ||	v rt|	| dkr|	|= t| j dkr| j D ]}||r||jd u r| ||_qd S |d u rtd|
 }g |	|< t D ] }|	| d|d |d |d  |	| d|d  q| j | t  |}|
! }t" |	|< t#d ur4| $|r|
% }t#|	|< nt&'d|  d	|	v r4|d
kr&|	d	 d ks-|	d	 dkr4t&'d|  t(d urEdt)  }| *|t( dd }|d ur|j+D ]}|||j}|sh|j,shJ d|j|r||j }t-|t.t/fs{|g}|j0st|dkrtd|jt|f g }t1|D ]?\}}t-|t2j3r|| qt-|t2j4r||5  qt-|t6tj7fr|t89|j qt:d|j||f | j;|j| qQ| j;|jg  qQ|d ur|j<D ]"}|j|vr|j,rq|j|v s|j,std||jf q|j<D ]k}|j|vr#q||j }t-|t.s1|g}|j0sGt|dkrGtd|jt|f g }|D ]/}t-|t2j3rZ|| n	|t89|j t syt-|t2j3rv| ||_qK| |_qK| j=|j| qt>|}|	d urt-|	tst:d|jD ]}|j}||	vs|	| d u rq|	| }| *|| q|? D ]!}||	vs|	| d u r| *|||  q| *||	|  qt@ rtAdkr| *tBtA tCdkr| *tDtC | jE  | $|r| jF| j
j | jG| j
j d S d S )Nz2`type` to initialized an Operator can not be None.r   z  File "{}", line {}, in {}r   r
   z    {}   z(The Op(%s) is not support to set device.Z	force_cpu	less_thanFzThe Attr(force_cpu) of Op(%s) will be deprecated in the future, please use 'device_guard' instead. 'device_guard' has higher priority when they are used at the same time.Zpipeline_stagec                 S   s(   | D ]}| | d ur||kr dS qdS r  r5   )var_listr   r   r5   r5   r6   	find_nameV  s
   z$Operator.__init__.<locals>.find_namezInput {} not foundz2Input %s expects only one input, but %d are given.zThe type of '%s' in operator %s should be one of [basestring(), str, Varibale] in python2, or one of [str, bytes, Variable] in python3.but received : %szCIncorrect setting for output(s) of operator "%s", should set: [%s].z4Output %s expects only one output, but %d are given.z'attrs' should be a dict.)Hr  r  r  _attr_typesr}  rr   r   rW   r   _typer(  r/  r   _current_cuda_graph_modeZ_cuda_graph_attrr   r  r  program_op_roler  rw   _op_role_varoutput_arg_nameshas_varr2  r:  r  	tracebackextract_stackr   r   r3  r  r   _current_device_has_kernelr  rI   rJ   r   ZkAutoParallelSuffixr  rC  Zdispensabler/   r   r   Z
duplicabler  rt   string_typesre  decoder$   r:   r-  r.  rq   	set_inputrD  
set_outputget_op_extra_attrsr   rG   rX   ipu_index_attr_namerY   ipu_stage_attr_nameZcheck_attrsZinfer_var_typeZinfer_shape)ri   r(  r/  rr   rC  rD  r}  r  r  Zop_attrsop_makerZrole_var_nameargZcallstack_var_nameframeZnamescope_var_nameZ	op_deviceZpipeline_attr_namer  Zin_protofoundZin_argsZin_arg_namesrZ   mZ	out_protoZout_argsZout_arg_namesextra_attrs_maprk  Zattr_valr5   r5   r6   r   
  sl  
























zOperator.__init__c                 C   s
   || j vS rD   )OP_WITHOUT_KERNEL_SET)ri   op_typer5   r5   r6   r    r  zOperator._has_kernelc                 C   s&   | j  }tjt|}t||S )z
        Get debug string.

        Args:
            throw_on_error(bool): Whether to raise exception if self is not
                initialized.

        Returns:
            str: The debug string.

        )r/  rc  r   OpDescrd  rt   re  r
  )ri   r	  rh  r  r5   r5   r6   rl    s   

zOperator.to_stringTc                 C   s  t |tsJ dt|d}tdt| jD ]*}|dj| j| d7 }| | j| }|dj|d7 }|t| jd krB|d	7 }q|d
7 }d}tdt| jD ]*}|dj| j| d7 }| 	| j| }|dj|d7 }|t| jd kr{|d	7 }qQ|d
7 }t
| j}d}tdt|D ]}|| }|r|dkrq| j|d}	|	tjjkr| j|d }
dj||	|
d}||7 }|t|d kr|d	7 }q|	tjjkrdd | j|dD }dj||	d|d}||7 }|t|d kr|d	7 }q|	tjjkrdj||	| |d}||7 }|t|d kr|d	7 }q|	tjjkrAdj||	| |d}||7 }|t|d kr@|d	7 }qt r{| jdkr{|dkr{| j|}t|}t }||}| }|d}ddd |D }d| }n| j|}dj||	|d}||7 }|t|d kr|d	7 }qddlm } | }|!| }|dur|djd|d7 }|d krd!j|| j||d"}|S d#j| j||d$}|S )%a  
        Get readable debug string of Operator.

        .. note::
            If you want to get the debug string in protobuf format,
            please use :code:`to_string` method.

        Args:
            skip_op_callstack(bool): whether to skip parsing Operator's attribute
                op_callstack, default value is True

        Returns:
            string: The formatted Operator string.

        Examples:
            .. code-block:: python

            import paddle.fluid as fluid

            cur_program = fluid.Program()
            cur_block = cur_program.current_block()
            var = cur_block.create_var(name="X",
                                       shape=[-1, 23, 48],
                                       dtype='float32')
            new_op = cur_block.append_op(type="abs",
                                inputs={"X": [var]},
                                outputs={"Out": [var]})
            print(new_op._to_readable_code())
        Eskip_op_callstack parameter's type is error, expect bool, received {}{r   z{name}=rt  z{value})r\  r   z, }r   op_callstackTz{name} = Var['{value}'])r   rr   r\  c                 S   s   g | ]}d |   qS )z'%s'rt  r   r2  r5   r5   r6   r   &  s    z.Operator._to_readable_code.<locals>.<listcomp>z{name} = Vars[{value}]r   z{name} = block[{value}]z{name} = blocks{value}Zcinn_launchZcompilation_key
c                 S   s   g | ]}d | qS )z      r5   )r   liner5   r5   r6   r   O  r   z{name} = {value}rW  NrY  rZ  r[  z{}z/{outputs} = {op_type}(inputs={inputs}, {attrs}))rD  r  rC  r}  z#{op_type}(inputs={inputs}, {attrs}))r  rC  r}  )"r/   r0   r   rr   rv   rw   output_namesrJ  input_namesinputsortedr  r/  	attr_typer   AttrTypeVARr  r   VARSjoinBLOCK_block_attr_idBLOCKS_blocks_attr_idsr   Zget_serialize_comile_keyr   parse_from_stringrU  r   r_  rX  Zget_dist_op_for_program)ri   skip_op_callstackZoutputs_strry   r  Z
inputs_strr  Z	attrs_strr   r*  attr_var_nameaZattr_var_namesr  r  progr   linesr\  rX  r`  Zdist_opZop_strr5   r5   r6   rU    s   









zOperator._to_readable_codec                 C   rS  rD   rT  r   r5   r5   r6   r  t  rP   zOperator.__str__c                 C   rm  rD   rz  r   r5   r5   r6   rr   y     
zOperator.typec                 C   r  )a  
        Get the input arguments according to the input parameter name.

        Args:
            name(str): The input parameter name.

        Returns:
            list: return the list of argument names that associated with \
                the specific parameter name.
        )r/  r(  r  r5   r5   r6   r(  }  r  zOperator.inputc                 C   r  )z
        Rename the `old_name` to `new_name`.

        Args:
            old_name(str): The old name of the Operator's input.
            new_name(str): The new name of the Operator's input.

        Returns:
            None
        N)r/  _rename_inputri   Zold_namerx  r5   r5   r6   r:       zOperator._rename_inputc                 C   r  )z
        Rename the `old_name` to `new_name`.

        Args:
            old_name(str): The old name of the Operator's output.
            new_name(str): The new name of the Operator's output.

        Returns:
            None
        N)r/  _rename_outputr;  r5   r5   r6   r=    r<  zOperator._rename_outputc                 C   rm  rD   )r/  r'  r   r5   r5   r6   r'    r9  zOperator.input_namesc                 C   rm  rD   )r/  input_arg_namesr   r5   r5   r6   r>    r9  zOperator.input_arg_namesc                 C   rm  rD   )r/  r	  r   r5   r5   r6   r	    r9  zOperator.output_arg_namesc                 C   r  )a
  
        Get output arguments by the output parameter name.

        Args:
            name(str): The output parameter name.

        Returns:
            list: return the list of argument names associated with \
                the specific parameter name.
        )r/  rJ  r  r5   r5   r6   rJ    r  zOperator.outputc                 C   rm  rD   )r/  r&  r   r5   r5   r6   r&    r9  zOperator.output_namesc                 C   s.   t | jjD ]\}}|| kr|  S qtd)Nz@Can't find op itself in it's block. It could be a bug of Paddle.)r  r(  opsrW   )ri   ry   r:  r5   r5   r6   idx  s   zOperator.idxc                 C   r  )z
        Whether this Operator has the attribute with name or not.

        Args:
            name(str): the attribute name.

        Returns:
            bool: True if has this attribute.

        r  r  r5   r5   r6   r    r  zOperator.has_attrc                 C   s   | j |dS )z
        Get the type of attribute by attribute's name.

        Args:
            name(str): the attribute name.

        Returns:
            core.AttrType: the attribute type.
        T)r/  r*  r  r5   r5   r6   r*    s   
zOperator.attr_typec                 C   r  )a(  
        Set the value of attribute by attribute's name.

        Args:
            name(str): the attribute name.
            val(bool|int|str|float|list): the value of the attribute.

        Raises:
            ValueError: If the type of value doesn't match with desc.attr_type(name).
        Nr  r  r5   r5   r6   r       zOperator._set_attrc                 C   rp  rD   r  r  r5   r5   r6   r    r  zOperator._remove_attrc                 C   s   t |tr| j||j dS t |tr't|tr'| j|dd |D  dS t |tr6| j||j dS t |trP|rPt|trP| j	|dd |D  dS t |t
js\t |t
jrg| j||  dS | || dS )a2  
        Update the value of desc's attribute by attribute's name.

        Args:
            name(str): the attribute name.
            val(bool|int|str|float|list): the value of the attribute.

        Raises:
            ValueError: If the type of value doesn't match with desc.attr_type(name).
        c                 S      g | ]}|j qS r5   r/  r  r5   r5   r6   r         z.Operator._update_desc_attr.<locals>.<listcomp>c                 S   rB  r5   rC  r  r5   r5   r6   r     rD  N)r/   r$   r/  set_var_attrr   r  set_vars_attrBlockset_block_attrset_blocks_attrr   	BlockDescProgramDescset_serialized_attrrc  _update_desc_plain_attrr  r5   r5   r6   r    s   

zOperator._update_desc_attrc                 C   sx  | j }t| dr|| jvr||| d S | j| }|tjjkr(||| d S |tjjkr6|	|| d S |tjj
krD||| d S |tjjkrR||| d S |tjjkr`||| d S |tjjkrn||| d S |tjjkr|||| d S |tjjkr||| d S |tjjkr||| d S |tjjkr||| d S |tjjkr||| d S ||| d S )Nr  )r/  r  r  r  r   r+  r  _set_bool_attrINTZ_set_int32_attrLONGZ_set_int64_attrFLOATZ_set_float32_attrSTRINGZ_set_str_attrZBOOLSZ_set_bools_attrZINTSZ_set_int32s_attrZLONGSZ_set_int64s_attrZFLOATSZ_set_float32s_attrZFLOAT64SZ_set_float64s_attrr*  Z_set_strs_attr)ri   r   r  r/  Z
type_indexr5   r5   r6   rM    s8   
z Operator._update_desc_plain_attrc                 C   s   | j dS NTr  r   r5   r5   r6   r  0  s   zOperator.attr_namesc                 C   r  )z
        Get the attribute by name.

        Args:
            name(str): the attribute name.

        Returns:
            bool|int|str|float|list: The attribute value. The return value
            can be any valid attribute type.
        r  r  r5   r5   r6   r  4  r  zOperator.attrc                 C   r  )z
        Get the block attribute's id by name.

        Args:
            name(str): the attribute name.

        Returns:
            int: the block index.
        )r/  r0  r  r5   r5   r6   r0  A  r  zOperator._block_attr_idc                 C   s6   |  |}|dkr|t| jjjk sJ | jjj| S )z
        Get the block attribute  by name.

        Args:
            name(str): the attribute name.

        Returns:
            block: the block attribute.
        r   )r0  rw   r(  r  blocks)ri   r   idr5   r5   r6   _block_attrM  s   
zOperator._block_attrc                 C   sJ   g }|  |D ]}|dkr|t| jjjk sJ || jjj|  q|S )z
        Get the blocks attribute  by name.

        Args:
            name(str): the attribute name.

        Returns:
            list: list of the blocks attribute.
        r   )r2  rw   r(  r  rT  r   )ri   r   r}  ry   r5   r5   r6   _blocks_attr\  s
   
zOperator._blocks_attrc                 C   r  )z
        Get the blocks attribute's ids by name.

        Args:
            name(str): the attribute name.

        Returns:
            list: list of the blocks ids.
        )r/  r2  r  r5   r5   r6   r2  m  r  zOperator._blocks_attr_idsc                 C   sH   | j |d}|tjjksJ d||| j |d }| j	|S )z
        Get the Variable attribute  by name.

        Args:
            name(str): the attribute name.

        Returns:
            Variable: the Variable attribute.
        Tz3Required type attr({}) is Variable, but received {})
r/  r*  r   r+  r,  r   r  r   r(  _var_recursive)ri   r   r*  r5  r5   r5   r6   	_var_attrz  s   
zOperator._var_attrc                    sJ    j |d}|tjjksJ d|| fdd j |dD }|S )z
        Get the Variables attribute  by name.

        Args:
            name(str): the attribute name.

        Returns:
            Variables: the Variables attribute.
        Tz9Required type attr({}) is list[Variable], but received {}c                    s   g | ]
} j | qS r5   )r(  rX  r   r#  r   r5   r6   r     s    z'Operator._vars_attr.<locals>.<listcomp>)r/  r*  r   r+  r-  r   r  )ri   r   r*  Z	attr_varsr5   r   r6   
_vars_attr  s   

zOperator._vars_attrc                 C   s   | j }i }|D ]H}| j|d}|tjjkr| |||< q|tjjkr,| |||< q|tjj	kr:| 
|||< q|tjjkrH| |||< q| |||< q|S )zx
        Get the attribute dict.

        Returns:
            dict: The Operator's attribute dict, name->attr.
        T)r  r/  r*  r   r+  r/  rV  r1  rW  r,  rY  r-  rZ  r  )ri   r  Zattr_mapnr*  r5   r5   r6   	all_attrs  s   zOperator.all_attrsc                 C   H   t j}t jjj}| j| sdS | j| }|t|@ r"dS dS NFT)	r   r  OpRoleOptimizer/  r  r  r  rx   )ri   r  ZOPTIMIZEop_roler5   r5   r6   _is_optimize_op     
zOperator._is_optimize_opc                 C   r]  r^  )	r   r  r_  Backwardr/  r  r  r  rx   )ri   r  ZBACKWARDra  r5   r5   r6   _is_backward_op  rc  zOperator._is_backward_opc                 C   r  r  r  r   r5   r5   r6   rZ    r   zOperator.dist_attrc                 C   r  r  r  r  r5   r5   r6   rZ    r  )NNNNT))rk   rl   r;   r  r  r   r  rl  rU  r  r  r  rr   r(  r:  r=  r'  r>  r	  rJ  r&  r@  r  r*  r  r  r  rM  r  r  r0  rV  rW  r2  rY  rZ  r\  rb  re  rZ  r  r5   r5   r5   r6   r  
  sd    +#
 a
 





#

r  c                   @   s  e Zd ZdZdd Zdd Zd=ddZd>d
dZeZe	dd Z
e	dd Zdd Ze	dd Ze	dd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd=d&d'Zd(d) Zd*d+ Zd,d- Zd.d/ Zd=d0d1Zd2d3 Zd4d5 Zd6d7 Zd8d9 Z d=d:d;Z!d<S )?rG  a  
    In Fluid, a Program is consistence of multi-Block, and Block stores
    VarDesc and OpDesc. In a specific Block, a VarDesc have a unique name.
    One block could have some child blocks, and child block's name scopes
    should inherit the parent's so that OpDesc in child block can reference
    a VarDesc that is stored in the parent block.
    Please reference the framework.proto for details.

    Args:
        program(Program): The Program that the Block belongs to.
        idx(int): The block's id in the Program.

    Notes:
        The constructor of Block should not be invoked directly. Please
        use `Program._create_block()` to create a block.

    Examples:
        .. code-block:: python

            import paddle.fluid as fluid

            cur_program = fluid.Program()
            cur_block = cur_program.current_block()
            var = cur_block.create_var(name="X",
                                       shape=[-1, 23, 48],
                                       dtype='float32')
            cur_block.append_op(type="abs",
                                inputs={"X": [var]},
                                outputs={"Out": [var]})
    c                 C   s4   |j || _ t | _t | _|| _t | _d S rD   )	r/  r(  collectionsOrderedDictr9  r   r?  r  Zremoved_vars)ri   r  r@  r5   r5   r6   r     s
   
zBlock.__init__c                 C   rS  rD   rT  r   r5   r5   r6   r    rP   zBlock.__str__Tc                 C   s   t |tsJ dt|d}|d| j7 }t| j D ]}|d| 7 }q|d7 }| j	D ]}|d||7 }q2|d7 }|S )a  
        Get readable debug string of Block.

        .. note::
            If you want to get the debug string in protobuf format,
            please use :code:`to_string` method.

        Args:
            skip_op_callstack(bool): whether to skip parsing Operator's attribute
                op_callstack, default value is True

        Returns:
            string: The formatted Block string.

        Examples:
            .. code-block:: python

            import paddle.fluid as fluid

            cur_program = fluid.Program()
            cur_block = cur_program.current_block()
            new_var = cur_block.create_var(name="X",
                                           shape=[-1, 23, 48],
                                           dtype='float32')
            new_op = cur_block.append_op(type="abs",
                                inputs={"X": [new_var]},
                                outputs={"Out": [new_var]})
            print(cur_block._to_readable_code())
        r  z{ // block z{}
z    {}
r$  r!  )
r/   r0   r   rr   r@  r   r9  valuesrU  r?  )ri   r4  Z	block_strr2  r:  r5   r5   r6   rU    s$   
zBlock._to_readable_codeFc           	   
   C   s   t |tr
t |tsJ |rLtd}d| j| jf }t| j D ]}|d|	d|
|| 7 }q"| jD ]}|d|	d|
| 7 }q6|d7 }|S | j }tjt|}t||}|S )a  
        Get debug string.

        Args:
            throw_on_error(bool): raise exception when self is not initialized
                when throw_on_error is True.
            with_details(bool): more details about variables and parameters
                (e.g. trainable, optimize_attr, ...) will be printed when
                with_details is True. Default False.

        Returns:
            str: The debug string.
        z\n(.)z#blocks {
  idx: %d
  parent_idx: %dz
  vars {
    %s  }z\n    \1z
  ops {
    %s  }z
})r/   r0   r}   compiler@  
parent_idxr   r9  ri  subrl  r?  r/  rc  r   rJ  rd  rt   re  r
  )	ri   r	  rg  Zre_add_indentri  r2  r:  rh  r  r5   r5   r6   rl  =  s2   






zBlock.to_stringc                 C   r  rD   )r/  r   r   r5   r5   r6   rk  g  r   zBlock.parent_idxc                 C   rm  rD   )r/  Zget_forward_block_idxr   r5   r5   r6   forward_block_idxk  r9  zBlock.forward_block_idxc                 C      | j | dS )z
        Set the forward block Idx.

        Args:
            idx(int): the block index.

        Returns:
            None
        N)r/  _set_forward_block_idx)ri   r@  r5   r5   r6   ro  o     
zBlock._set_forward_block_idxc                 C   s,   | j }| jjD ]}|j|kr|j   S qdS )NrS   )r@  r  rT  rm  )ri   Zcur_block_idxr(  r5   r5   r6   backward_block_idx{  s   

zBlock.backward_block_idxc                 C   r  rD   )r/  rU  r   r5   r5   r6   r@    r   z	Block.idxc                 C   sB   t |tjstdt| | j|d}|du rtd| |S )aT  
        Get a Variable by name from this block.

        Args:
            name(str): the Variable's name.

        Raises:
            ValueError: The If input's type is not str, or this block
                doesn't have a Variable with the giving name.

        Returns:
            Variable: the Variable with the giving name.
        z4var require string as parameter, but get %s instead.Nzvar %s not in this block)r/   rt   r  rq   rr   r9  r   rW   )ri   r   r  r5   r5   r6   r2    s   z	Block.varc                 C   s   t  }t }||  | j}t|dkrX|d }|dd }t||v r%q||r/||S |jdkr=||	|j |j
dkrK||	|j
 |t| t|dksdS )z
        Get a Variable by name from this block recursively.

        Args:
            name(str): the Variable's name.

        Returns:
            Variable: the Variable with the giving name. Or None if not found.
        r   r   NrS   )r   r  r   r  rw   rU  r
  r2  rk  r(  rm  add)ri   r   Zfrontiervisitedr7  curr5   r5   r6   _find_var_recursive  s$   





zBlock._find_var_recursivec                 C   s    |  |}|r	|S td|)aS  
        Get a Variable by name from this block recursively.

        Args:
            name(str): the Variable's name.

        Raises:
            ValueError: this block and this parent block doesn't
                have a Variable with the giving name.

        Returns:
            Variable: the Variable with the giving name.
        z Var {0} is not found recursively)ru  rW   r   )ri   r   r2  r5   r5   r6   rX    s   
zBlock._var_recursivec                 C   s   t |  S rD   )r   iter_parametersr   r5   r5   r6   all_parameters  r  zBlock.all_parametersc                 C   s   dd t | jD S )Nc                 s   s&    | ]}t |d  tr|d  V  qdS )r   Nr/   r'  )r   r  r5   r5   r6   r    s    
z(Block.iter_parameters.<locals>.<genexpr>)rt   	iteritemsr9  r   r5   r5   r6   rv    s   
zBlock.iter_parametersc                 O   sD   t  rt|i |}|S t|d| i|}d|v r |d ||  |S )Nr(  initializer)r   r  r$   )ri   r_   r`   r2  r5   r5   r6   rF    s   zBlock.create_varc                 C   s
   || j v S rD   )r9  r  r5   r5   r6   r
    r  zBlock.has_varc                 C   s|  t |}t |}| |std| | |}t|tkr2d}|j}|j}|j	}|j
}|j}	nt|tkrAd}|j}	|j}ntdt||j}
| jt |t | | jt |}|dkrt rzt| | |
||||||	d	}n5t rt| | |
||||||	d	}n t| | | |
||||||	d
}n|dkrt| |
||	|d}|| j|< | j|= |   |S )a  
        Rename variable in vars and ops' inputs and outputs

        Args:
            name(str): the name that need to be renamed.
            new_name(str): the name that need to rename to.

        Raises:
            ValueError: If this block doesn't have this the giving name,
                or the type of the var with the giving name is not Parameter
                or Variable.

        Returns:
            Variable: the Variable with the giving name.
        zvar %s is not in current blockr'  r$   zunsupported var type: %s)rr   r   r;  r^  optimize_attrregularizerr,  )rr   r   r,  r;  )r-  r.  r
  rW   r2  rr   r'  r;  r^  r{  r|  r,  r$   r/  _rename_varr1  r0  r   r%  r  r   rN   r&  r9  _sync_with_cpp)ri   r   rx  r  var_typer;  r^  r{  r|  r,  Zorig_var_typedr2  r5   r5   r6   r}    s   





zBlock._rename_varc                 C   s.   |dkr|    | jt| | j|= d S rS  )r~  r/  _remove_varr-  r1  r9  )ri   r   syncr5   r5   r6   r  K  s   zBlock._remove_varc           	      O   s   | j  }d }t rt|i |}nt rt|i |}nt|g|R i |}d|v rZdd }|d }|||}t|}|dkrNtd|j	 d t
| |dkrU	 |S |||  |S )Nrz  c                 S   s6   g }| j D ]}|j|jv r|jdv rq|| q|S )N)Zc_broadcastr  Zcoalesce_tensor)r?  r   r	  rr   r   )r(  r2  init_opsr:  r5   r5   r6   _is_inited_by^  s   


z-Block.create_parameter.<locals>._is_inited_byr   rV  z  is inited by multiple init ops )r  global_blockr   r%  rN   r&  r'  rw   RuntimeErrorr   r|   )	ri   r_   r`   r  paramr  rz  r  Zinit_ops_lenr5   r5   r6   create_parameterQ  s6   


zBlock.create_parameterc                 O   s:  t  rD|di }|dd}|dd}td| t t| d|dd|d}t ||di |di |r9|ni |d	d
| |S ddlm	} | j
 }|dd}	|dd}
||	. ||
 t| ||dd|	|
|ddd}W d   n1 sw   Y  W d   n1 sw   Y  | j| |S )z
        Appends a new Operator according to the giving arguments.

        Returns:
            Operator: the append Operator.
        r}  inplace_mapNrr   zOp `%s` is executed through `append_op` under the dynamic mode, the corresponding API implementation needs to be upgraded to using `_C_ops` method.)r(  r/  rr   rC  rD  r}  rC  rD  r;  Fr   )param_guard)r   r   rI   rJ   r   r  r   trace_opZpaddle.fluid.dygraph.baser  r/  rH  r?  r   )ri   r_   r`   r}  r  rr   r:  r  op_descrC  rD  r5   r5   r6   rH    sZ   






 
zBlock.append_opc                 O   s    |    | j|g|R i |S )z
        Insert a Operator according to the giving arguments.

        Args:
            index(int): the place that the operator to insert.

        Returns:
            Operator: the insert Operator.
        )r~  _insert_op_without_sync)ri   rZ   r_   r`   r5   r5   r6   
_insert_op  s   
zBlock._insert_opc                 O   s2   | j |}t|| |d|}| j|| |S )a  
        Insert an Operator according to the giving arguments,
        without sync_with_cpp to meke the compilation faster.

        Args:
            index(int): the place that the operator to insert.

        Returns:
            Operator: the insert Operator.
        r(  r/  )r/  r  r  r?  r~  )ri   rZ   r_   r`   r  r:  r5   r5   r6   r    s   zBlock._insert_op_without_syncc                 C   s.   |dkr|    | j||d  | j|= dS )z
        Remove the specific position operator.

        Args:
            index(int): the position that the operator to insert.

        Returns:
            None
        Tr   N)r~  r/  
_remove_opr?  )ri   rZ   r  r5   r5   r6   r    s   
zBlock._remove_opc                 C   s   | j || S )z
        Return the Operator between start and end.

        Args:
            start(int): the start position.
            end(int): the end position.

        Returns:
            list: the Operators between start and end.
        )r?  )ri   r  r  r5   r5   r6   
_slice_ops  s   zBlock._slice_opsc              
   O   s   t  r5|dd }|di }t| d |d d |d}t ||di |di |r+|ni |dd |S | j }t| ||dd |dd |dd |dd d}| jd| |S )	Nrr   r}  r|  rC  rD  r;  Fr   )	r   r   r  r   r  r/  _prepend_opr?  r~  )ri   r_   r`   rr   r}  r:  r  r5   r5   r6   r    s2   








zBlock._prepend_opc              	   C   s  | j  D ]<}| | sAd}| r| }| r4| r4| j| ||	 |
 | |d q| j| ||	 |d qt| j D ]}| j t|sZ| j| qIg }td| j  D ]}|| j | qet| jdkr| jd j }| jt| jd  j }d}d}tt|D ]}	|||	 kr|	}|||	 kr|	}q|dusJ |dusJ ||ksJ nd}d}t|d d ddD ]}	||	 }
t| |
}| jd| qt|d t|D ]}	||	 }
t| |
}| j| q|dkr<|t| jk r<d}d}|t| jk r<|t|k r<| j| j || kr%| j|= n|d7 }|d7 }|t| jk r<|t|k st| jt|ksHJ tt| jD ]}	| j|	 j ||	 ks^J qOdS )z
        Sync from the desc on the c++ end. This method is used to synchronize
        the c++ desc instance generated by backward.
        F)r   r/  rr   r  r   r;  )r   r/  rr   r;  r   r   NrS   )r/  all_varsr
  r   Zhas_stop_gradientr;  Zhas_is_parameterr]  r  rr   r  r   rF  r   r9  r   r0  r-  r1  r   rv   op_sizer   r:  rw   r?  r  r~  )ri   r2  Zis_stop_gradientZ
ops_in_cppop_idxZfirst_op_in_pythonZlast_op_in_pythonZstart_indexZ	end_indexrZ   r  r:  Zops_in_cpp_indexZops_in_python_indexr5   r5   r6   r~    s   	




zBlock._sync_with_cppc                 C   s   t |ts	td| D ]}t |tsJ | j|jd}|du r#qt |ts*J d}t	 rHt
|j|j|j|j|j|j|j|j|j|jd
}n?t rdt|j|j|j|j|j|j|j|j|j|jd
}n#t| |j|j|j|jtjjjkrw|jnd|j|j|j|j|j|jd}|| j|j< qdS )a:  
        Copy the information of parameters from the other block.

        Args:
            other(Block): the other block.

        Raises:
            ValueError: If type of input is not Block, or the `other` and this
                block is not in the same topology.

        Returns:
            None
        z2_copy_param_info_from should be invoked with BlockN)
r  r   rr   r6  r;  r^  r{  r|  r,  r   )r(  r  r   rr   r6  r;  r^  r{  r|  r,  r   )r/   rG  rq   rv  r'  r9  r   r   r$   r   r%  r  r   rr   r6  r;  r^  r{  r|  r,  rN   r&  r   r   r   r  )ri   otherrs  r  Znew_pr5   r5   r6   _copy_param_info_fromk  sl   
zBlock._copy_param_info_fromc              
   C   s   t |tsJ d}|jtjjjkr| j|j|j	|jd}|S |jtjjj
kr3| j|j|j	|jd}|S |jtjjjkrV| j|j|j|j|j|rIdn|j	|j|j d}|S | j|j|j|j|j|j|rfdn|j	|j|j d}|S )a  
        Clone a variable into current block.

        Args:
            var: the variable to be cloned.
            force_persistable(bool): True means setting the result variable to being persistable.
                                     False means setting the persistable the same with that of input var.
                                     default: True.

        Returns:
            Variable: the new  variable cloned from 'var' in current block.
        N)r   r  rr   T)r   r  r   rr   r  r<  r>  )r   r  r   rr   r6  r  r<  r>  )r/   r$   rr   r   r   r   ZSTEP_SCOPESrF  r   r  ZRAWrE  r  r   r<  r/  r>  r6  )ri   r2  Zforce_persistableZret_varr5   r5   r6   _clone_variable  sD   
zBlock._clone_variableNrf  r  )"rk   rl   r;   r  r   r  rU  rl  r  r  rk  rm  ro  rq  r@  r2  ru  rX  rw  rv  rF  r
  r}  r  r  rH  r  r  r  r  r  r~  r  r  r5   r5   r5   r6   rG    sF    

/(



$	
^/>
WGrG  c                 C   sf   t |ts	J dt |tsJ dt| j}t|j}t|||||}| | || |S )Nzpass_attrs must be dictzpass_attr_types must be dict)r/   r   r   rK  r/  Z
apply_pass_rebuild_from_desc)main_programstartup_programZ	pass_nameZ
pass_attrsZpass_attr_typesZtmp_main_programZtmp_startup_programr}  r5   r5   r6   _apply_pass  s   

r  c                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zed$d% Zed&d' Zd(S ))IrNodezN
    Python IrNode. Beneath it is a core.Node, which is used for Ir Pass.
    c                 C   s   t |tjs
J d|| _dS )zl
        Construct an IrNode using core.Node.

        Args:
            node(core.Node): C++ Node.
        z'node must be the instance of core.Node.N)r/   r   Nodenoderi   r  r5   r5   r6   r      s   
zIrNode.__init__c                 C   rm  )zU
        Return the node name.

        Returns:
            str: node name.
        )r  r   r   r5   r5   r6   r        
zIrNode.namec                 C   rm  )z
        Return the node type.

        Returns:
            core.Node.Type: node type(core.Node.Type.Operation or core.Node.Type.Variable).
        )r  	node_typer   r5   r5   r6   r    r  zIrNode.node_typec                 C   rm  )z~
        Return the node variable description.

        Returns:
            core.VarDesc: node variable description.
        )r  r2  r   r5   r5   r6   r2    r  z
IrNode.varc                 C   rm  )z}
        Return the node operator description.

        Returns:
            core.OpDesc: node operator description.
        )r  r:  r   r5   r5   r6   r:  '  r  z	IrNode.opc                 C   rm  )zQ
        Return the node id.

        Returns:
            int: node id.
        )r  rU  r   r5   r5   r6   rU  0  r  z	IrNode.idc                 C   rm  )z
        If the node is an operator, then return true.

        Returns:
            bool: indicate whether the node is an operator.
        )r  is_opr   r5   r5   r6   r  9  r  zIrNode.is_opc                 C   rm  )z
        If the node is a variable, then return true.

        Returns:
            bool: indicate whether the node is a variable.
        )r  is_varr   r5   r5   r6   r  B  r  zIrNode.is_varc                 C   rm  )z
        If the node is a control dependence variable, then return true.

        Returns:
            bool: indicate whether the node is a control dependence variable.
        )r  is_ctrl_varr   r5   r5   r6   r  K  r  zIrNode.is_ctrl_varc                 C      | j   dS )z|
        Clear the node inputs. After executing the `clear_inputs` function,
        the node inputs will be empty.
        N)r  clear_inputsr   r5   r5   r6   r  T     zIrNode.clear_inputsc                 C   rn  )z}
        Remove a node from inputs by the given node id.

        Args:
            node_id(int): the given node id.
        Nr  remove_inputri   Znode_idr5   r5   r6   remove_input_by_id[     zIrNode.remove_input_by_idc                 C      | j |j  dS )zm
        Remove a node from inputs.

        Args:
            node(IrNode): the node being removed.
        Nr  r  r5   r5   r6   r  d     zIrNode.remove_inputc                 C   r  )zl
        Append a node in inputs.

        Args:
            node(IrNode): the node being appended.
        N)r  append_inputr  r5   r5   r6   r  m  r  zIrNode.append_inputc                 C   r  )z
        Clear the node outputs. After executing the `clear_outputs` function,
        the node outputs will be empty.
        N)r  clear_outputsr   r5   r5   r6   r  v  r  zIrNode.clear_outputsc                 C   rn  )z~
        Remove a node from outputs by the given node id.

        Args:
            node_id(int): the given node id.
        Nr  remove_outputr  r5   r5   r6   remove_output_by_id}  r  zIrNode.remove_output_by_idc                 C   r  )zn
        Remove a node from outputs.

        Args:
            node(IrNode): the node being removed.
        Nr  r  r5   r5   r6   r    r  zIrNode.remove_outputc                 C   r  )zm
        Append a node in outputs.

        Args:
            node(IrNode): the node being appended.
        N)r  append_outputr  r5   r5   r6   r    r  zIrNode.append_outputc                 C      dd | j jD S )zt
        Return the node inputs.

        Returns:
            list(IrNode): node inputs wrapped by IrNode.
        c                 S   r   r5   r  r   r[  r5   r5   r6   r     r   z!IrNode.inputs.<locals>.<listcomp>r  rC  r   r5   r5   r6   rC    r  zIrNode.inputsc                 C   r  )zv
        Return the node outputs.

        Returns:
            list(IrNode): node outputs wrapped by IrNode.
        c                 S   r   r5   r  r  r5   r5   r6   r     r   z"IrNode.outputs.<locals>.<listcomp>r  rD  r   r5   r5   r6   rD    r  zIrNode.outputsN)rk   rl   r;   r  r   r   r  r2  r:  rU  r  r  r  r  r  r  r  r  r  r  r  r  rC  rD  r5   r5   r5   r6   r    s.    														
	r  c                       s`   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d Zdd Z	e
dd Ze
dd Z  ZS )	IrVarNodezO
    Python IrVarNode. Beneath it is a core.Node, it inherits from IrNode.
    c                    6   t |tjr
| sJ dtt| | || _dS )zo
        Construct an IrVarNode using core.Node.

        Args:
            node(core.Node): C++ Node.
        zFnode must be the instance of core.Node and it must be a variable node.N)r/   r   r  r  rg   r  r   r  r  rn   r5   r6   r        

zIrVarNode.__init__c                 C   s*   | j  dusJ d| j  | dS )zg
        Set the node variable shape.

        Args:
            shape(list): shape to be set.
        N.The node variable description can not be None.)r  r2  r4  )ri   r  r5   r5   r6   r4    s   zIrVarNode.set_shapec                 C   $   | j  dusJ d| j   S )z
        If the variable node is a persistable variable, then return true.

        Returns:
            bool: indicate whether the variable is persistable.
        Nr  )r  r2  r  r   r5   r5   r6   r       zIrVarNode.persistablec                 C   r  )zr
        Return the variable type.

        Returns:
            core.VarDesc.VarType: the variable type.
        Nr  )r  r2  rr   r   r5   r5   r6   rr     r  zIrVarNode.typec                 C   r  )z|
        Return the variable data type.

        Returns:
            core.VarDesc.VarType: the variable data type.
        Nr  )r  r2  r   r   r5   r5   r6   r     r  zIrVarNode.dtypec                 C   r  )zd
        Return the variable shape.

        Returns:
            list: the variable shape.
        Nr  )r  r2  r  r   r5   r5   r6   r    r  zIrVarNode.shapec                 C   r  )zx
        Return the node inputs.

        Returns:
            list(IrOpNode): node inputs wrapped by IrOpNode.
        c                 S   r   r5   IrOpNoder  r5   r5   r6   r     r   z$IrVarNode.inputs.<locals>.<listcomp>r  r   r5   r5   r6   rC    r  zIrVarNode.inputsc                 C   r  )zz
        Return the node outputs.

        Returns:
            list(IrOpNode): node outputs wrapped by IrOpNode.
        c                 S   r   r5   r  r  r5   r5   r6   r     r   z%IrVarNode.outputs.<locals>.<listcomp>r  r   r5   r5   r6   rD    r  zIrVarNode.outputs)rk   rl   r;   r  r   r4  r  rr   r   r  r  rC  rD  rm   r5   r5   rn   r6   r    s    
	r  c                       s   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zedd Zedd Z  ZS )r  zN
    Python IrOpNode. Beneath it is a core.Node, it inherits from IrNode.
    c                    r  )zn
        Construct an IrOpNode using core.Node.

        Args:
            node(core.Node): C++ Node.
        zFnode must be the instance of core.Node and it must be a operator node.N)r/   r   r  r  rg   r  r   r  r  rn   r5   r6   r     r  zIrOpNode.__init__c                 C   ,   | j  dusJ d| j  || dS )z
        Rename the input of this node.

        Args:
            old_input_name(str): the old input name.
            new_input_name(str): the new input name.
        N.The node operator description can not be None.)r  r:  r:  )ri   Zold_input_nameZnew_input_namer5   r5   r6   rename_input"     	zIrOpNode.rename_inputc                 C   r  )z
        Rename the output of this node.

        Args:
            old_output_name(str): the old output name.
            new_output_name(str): the new output name.
        Nr  )r  r:  r=  )ri   Zold_output_nameZnew_output_namer5   r5   r6   rename_output/  r  zIrOpNode.rename_outputc                 C   &   | j  dusJ d| j  |S )z
        Get the argument name list by the parameter name for input.

        Args:
            name(str): the parameter name.

        Returns:
            list(str): the argument name list.
        Nr  )r  r:  r(  r  r5   r5   r6   r(  <     zIrOpNode.inputc                 C   r  )z
        Get the argument name list by the parameter name for output.

        Args:
            name(str): the parameter name.

        Returns:
            list(str): the argument name list.
        Nr  )r  r:  rJ  r  r5   r5   r6   rJ  K  r  zIrOpNode.outputc                 C   r  )z
        Change the operator type into new type.

        Args:
            new_type(str): new operator type to be set.
        Nr  )r  r:  r3  )ri   new_typer5   r5   r6   r3  Z  s   zIrOpNode.set_typec                 C   r  )z
        Set the value of attribute by attribute's name.

        Args:
            name(str): the attribute name.
            val(bool|int|str|float|list): the value of the attribute.
        Nr  r  r5   r5   r6   set_attrf  r  zIrOpNode.set_attrc                 C   s   | j  dusJ d| j  }t|tr|||j dS t|tr5t|tr5||dd |D  dS t|t	rC|
||j dS t|tr\|r\t|t	r\||dd |D  dS t|tjsht|tjrr|||  dS ||| dS )zR
        Update the value of the op desc's attribute by attribute's name.
        Nr  c                 S   rB  r5   rC  r  r5   r5   r6   r   {  rD  z.IrOpNode._update_desc_attr.<locals>.<listcomp>c                 S   rB  r5   rC  r  r5   r5   r6   r     rD  )r  r:  r/   r$   rE  r/  r   r  rF  rG  rH  rI  r   rJ  rK  rL  rc  r  )ri   r   r  r/  r5   r5   r6   r  p  s"   


zIrOpNode._update_desc_attrc                 C   r  )z
        Return input arguments' names of this op node.

        Returns:
            list(str): input arguments' names of this op node.
        Nr  )r  r:  r>  r   r5   r5   r6   r>    r  zIrOpNode.input_arg_namesc                 C   r  )z
        Return output arguments' names of this op node.

        Returns:
            list(str): output arguments' names of this op node.
        Nr  )r  r:  r	  r   r5   r5   r6   r	    r  zIrOpNode.output_arg_namesc                 C   r  )zz
        Return the node inputs.

        Returns:
            list(IrVarNode): node inputs wrapped by IrVarNode.
        c                 S   r   r5   r  r  r5   r5   r6   r     r   z#IrOpNode.inputs.<locals>.<listcomp>r  r   r5   r5   r6   rC    r  zIrOpNode.inputsc                 C   r  )z|
        Return the node outputs.

        Returns:
            list(IrVarNode): node outputs wrapped by IrVarNode.
        c                 S   r   r5   r  r  r5   r5   r6   r     r   z$IrOpNode.outputs.<locals>.<listcomp>r  r   r5   r5   r6   rD    r  zIrOpNode.outputs)rk   rl   r;   r  r   r  r  r(  rJ  r3  r  r  r>  r	  r  rC  rD  rm   r5   r5   rn   r6   r    s     

	r  c                   @   s   e Zd ZdZd=ddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
d=ddZd=ddZdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd>d5d6Zd7d8 Zd9d: Zd;d< Zd3S )?IrGraphz
    Python IrGraph. Beneath it is a core.Graph, which is used for
    creating a c++ Ir Pass Graph. An IrGraph is just a graph view of
    a Program. In an IrGraph, both Variables and Operators are graph
    nodes.
    Fc                 C   s$   t |tjs
J d|| _|| _dS )z
        Construct an IrGraph using core.Graph.

        Args:
            graph(core.Graph): C++ Graph.
            for_test(bool): True for the test graph and false for the train graph.
        z)graph must be the instance of core.Graph.N)r/   r   ZGraphgraph	_for_test)ri   r  for_testr5   r5   r6   r     s   
zIrGraph.__init__c                 C   s   | j  }t|| jS )z
        Create a new and duplicated IrGraph.

        Warns:
            The method only clones the graph structure, not its attributes.

        Returns:
            IrGraph: A new and duplicated graph.
        )r  r  r  r  )ri   gr5   r5   r6   r    s   

zIrGraph.clonec                 C   r   )zh
        If the graph is used for testing, the function returns true. Otherwise, returns false.
        )r  r   r5   r5   r6   is_test  s   zIrGraph.is_testc                 C      dd | j  D S )zB
        Return all nodes included in the graph as a set.
        c                 S      h | ]}t |qS r5   r  r   r  r5   r5   r6   	<setcomp>  r   z$IrGraph.all_nodes.<locals>.<setcomp>r  nodesr   r5   r5   r6   	all_nodes     zIrGraph.all_nodesc                 C   r  )zK
        Return all variable nodes included in the graph as a set.
        c                 S      h | ]
}|  rt|qS r5   )r  r  r  r5   r5   r6   r        z(IrGraph.all_var_nodes.<locals>.<setcomp>r  r   r5   r5   r6   all_var_nodes  r  zIrGraph.all_var_nodesc                 C   sN   t  }| j D ]}| r| dur|  r|| qdd |D S )zW
        Return all persistable variable nodes included in the graph as a set.
        Nc                 S   r  r5   r  )r   rs  r5   r5   r6   r    r   z0IrGraph.all_persistable_nodes.<locals>.<setcomp>)r  r  r  r  r2  r  rr  )ri   Zpersistable_nodesr  r5   r5   r6   all_persistable_nodes  s   

zIrGraph.all_persistable_nodesc                 C   r  )zK
        Return all operator nodes included in the graph as a set.
        c                 S   r  r5   )r  r  r  r5   r5   r6   r    r  z'IrGraph.all_op_nodes.<locals>.<setcomp>r  r   r5   r5   r6   all_op_nodes  r  zIrGraph.all_op_nodesc                    s    fddt j D S )zL
        Return all sub_graphs included in the main graph as a set.
        c                    s    g | ]}t j| d qS )r  r  r  get_sub_graphr   ry   r  ri   r5   r6   r         z*IrGraph.all_sub_graphs.<locals>.<listcomp>)rv   r  Zsub_graph_size)ri   r  r5   r  r6   all_sub_graphs  s   zIrGraph.all_sub_graphsc                 C   s   t | j||dS )z:
        Return i-th sub_graph in the main graph.
        r  r  )ri   ry   r  r5   r5   r6   r    r  zIrGraph.get_sub_graphc                 C   sB   t |}|| || || |d t| j|S )a6  
        Create a persistable variable node in the graph. In IrGraph,
        it can not distinguish between persistable variables and parameters.

        Args:
            name(str): the name of the persistable variable node.
            vart_type(core.VarDesc.VarType): the type of the persistable variable node.
            shape(list): the shape of the persistable variable node.
            var_dtype(core.VarDesc.VarType): the data type of the persistable variable node.

        Returns:
            IrVarNode: the created persistable variable node.
        T)	r   r   r3  r4  r5  r7  r  r  create_var_noderi   r   r  r  Z	var_dtypevar_descr5   r5   r6   create_persistable_node  s   




zIrGraph.create_persistable_nodec                 C   s8   t |}|| || || t| j|S )a  
        Create a variable node in the graph. The created variable node is
        not persistable.

        Args:
            name(str): the name of the variable node.
            vart_type(core.VarDesc.VarType): the type of the variable node.
            shape(list): the shape of the variable node.
            var_dtype(core.VarDesc.VarType): the data type of the variable node.

        Returns:
            IrVarNode: the created variable node.
        )r   r   r3  r4  r5  r  r  r  r  r5   r5   r6   r  "  s
   



zIrGraph.create_var_nodec                 C   ry  )z&
        create a control var
        )r  r  create_control_dep_varr   r5   r5   r6   r  7  s   zIrGraph.create_control_dep_varc                 C      t | j|S )a?  
        Create a variable node by using an existing VarDesc in the graph.
        Depend on the giving VarDesc, the created variable node may be persistable.

        Args:
            var_desc(core.VarDesc): the giving variable description.

        Returns:
            IrVarNode: the created variable node.
        )r  r  r  )ri   r  r5   r5   r6   create_var_node_from_desc=  rA  z!IrGraph.create_var_node_from_descc                 C   s   t  }|| t|D ]\}}| ||| qt|D ]\}}	t|	ts+|	g}	||dd |	D  qt|D ]\}
}	t|	tsH|	g}	|	|
dd |	D  q<t
| j|S )aw  
        Create a operator node in the graph.

        Args:
            op_type(str): the type of the operator node.
            attrs(dict): the attributes of the operator node.
            inputs(dict): the inputs of the operator node.
            outputs(dict): the outputs of the operator node.

        Returns:
            IrOpNode: the created operator node.
        c                 S      g | ]}|  qS r5   rt  r   Zvar_noder5   r5   r6   r   _  r   z*IrGraph.create_op_node.<locals>.<listcomp>c                 S   r  r5   rt  r  r5   r5   r6   r   e  r   )r   r  r3  rt   ry  r  r/   r   r  r  r  r  create_op_node)ri   r  r}  rC  rD  r  r  r\  Z
input_name	var_nodesZoutput_namer5   r5   r6   r  J  s"   


zIrGraph.create_op_nodec                 C   r  )z
        Create a operator node by using an existing OpDesc in the graph.

        Args:
            op_desc(core.VarDesc): the giving operator description.

        Returns:
            IrOpNode: the created operator node.
        )r  r  r  )ri   r  r5   r5   r6   create_op_node_from_desci  rp  z IrGraph.create_op_node_from_descc                 C   x   |j | j v r|j | j v r|j | j v sJ d|| || || || || |  dS )a@  
        Update the input's link of a operator node.

        Args:
            old_input_node(IrNode): the old input node of the giving op_node.
            new_input_node(IrNode): the new input node of the giving op_node.
            op_node(IrOpNode): the operator node that is needed to update input's link.
        zVThe three arguments(old_input_node&new_input_node&op_node) must be in the graph nodes.N)	r  r  r  r  r  r  r  r  r   )ri   Zold_input_nodeZnew_input_nodeop_noder5   r5   r6   update_input_linku     




zIrGraph.update_input_linkc                 C   r  )aF  
        Update the output's link of an operator node.

        Args:
            old_output_node(IrNode): the old output node of the giving op_node.
            new_output_node(IrNode): the new output node of the giving op_node.
            op_node(IrOpNode): the operator node that is needed to update input's link.
        zZThe three arguments(old_output_node &new_output_node &op_node) must be in the graph nodes.N)	r  r  r  r  r  r  r  r  r   )ri   Zold_output_nodeZnew_output_noder  r5   r5   r6   update_output_link  r  zIrGraph.update_output_linkc                 C   s\   |j | j v sJ d|j   |j | j v s"J d|j   || || dS )z
        Connect two nodes.

        Args:
            node_in(IrNode): the input node.
            node_out(IrNode): the output node.
        z'node_in(%s) must be in the graph nodes.z(node_out(%s) must be in the graph nodes.N)r  r  r  r   r  r  )ri   Znode_inZnode_outr5   r5   r6   link_to  s   
zIrGraph.link_toc                 C   sD   t |tst |trt|}n|h}dd |D }t| j| dS )z
        Remove nodes safely since links connected to these removed nodes are
        also removed.

        Args:
            remove_nodes(set): the nodes prepared to be removed.
        c                 S      h | ]}|j qS r5   r  r  r5   r5   r6   r    rD  z,IrGraph.safe_remove_nodes.<locals>.<setcomp>N)r/   r  r   r   Zgraph_safe_remove_nodesr  )ri   Zremove_nodesZoriginal_nodesr5   r5   r6   safe_remove_nodes  s   


zIrGraph.safe_remove_nodesc                 C   s   t | j}t }|D ]G}| rR| d urR|  D ]}||vr-| |j|g||< q| 	 D ]}||vrE| |j
|g||< q4|| | |j
| q4q| j| d S rD   )r   topology_sortr  r   r  r:  r>  _find_node_by_namerC  r	  rD  r   resolve_hazard)ri   ordered_nodesr  r  Zeach_var_namer5   r5   r6   r    s$   
zIrGraph.resolve_hazardc                 C      t | jS )z
        Check if the graph has a circle.

        Returns:
            bool: True if the graph has a circle else False.
        )r   
has_circler  r   r5   r5   r6   r       zIrGraph.has_circlec                 C   r   )z
        Count the number of unconnected graphs in this graph.

        Returns:
            int: the number of unconnected graphs.
        )r   	graph_numr  r   r5   r5   r6   r    r  zIrGraph.graph_numc                 C   s   t | j}dd |D S )z
        Perform the topology sort operation on the graph.

        Notes: the `graph` can not contain a circle.

        Returns:
            list(IrNode): nodes in topology order.
        c                 S   r   r5   r  r  r5   r5   r6   r     r   z)IrGraph.topology_sort.<locals>.<listcomp>)r   r  r  )ri   r  r5   r5   r6   r    s   	zIrGraph.topology_sortc                 C   s@   t | j}t }t|D ]\}}dd |D |t|< q|S )z
        Build an adjacency list of operations for the `graph`.

        Returns:
            dict{IrNode: set(IrNode)}: the adjacency list.
        c                 S   r  r5   r  r  r5   r5   r6   r    r   z/IrGraph.build_adjacency_list.<locals>.<setcomp>)r   build_adjacency_listr  r   rt   ry  r  )ri   Zadj_listZwrapped_adj_listkr  r5   r5   r6   r    s
   zIrGraph.build_adjacency_listNTc           
      C   s  dd }t  }|r|  D ]}| r|| q| | tdt|   |durdt	|t s?t	|t
r<t |}n|h}dd |D }dd |D }|| }| jdr]| jd | j d| tj|sot| tj||d	 }td
}	|	 d| |	| j || dS )a  
        Draw the graph. If `dot` command is installed, the drawn graph
        will be saved as pdf file type, otherwise dot file type is used.

        Args:
            save_path(str): the save path of drawn graph.
            name(str): the name of drawn graph.
            marked_nodes(set(IrNode)): nodes that are needed to be marked.
            Default value is None.
            remove_ctr_var(bool): If it is set True, all control variable nodes
            in the graph will be removed. Default value is True.
        c                 S   sT   t j| d d }tjd|  d | dd}|dkr(td td|  d S d S )	Nr   z.pdfz
dot -Tpdf z -o T)shellz1The dot command is needed for creating pdf files.z$The {} is saved as the dot filetype.)r   pathsplitext
subprocesscallprintr   )Zdot_file_pathZpdf_save_pathZexited_coder5   r5   r6   _convert_to_pdf  s   z%IrGraph.draw.<locals>._convert_to_pdfzTotal ops num = {}.Nc                 S   r  r5   r  r  r5   r5   r6   r  &  rD  zIrGraph.draw.<locals>.<setcomp>c                 S   r  r5   r  r  r5   r5   r6   r  '  rD  Z__graphviz__marked_node__z.dotZgraph_viz_passZgraph_viz_path)r  r  r  rr  r  r  r   rw   r  r/   r   r  hasZeraser   r  existsmakedirsr.  r   get_passapply)
ri   Z	save_pathr   Zmarked_nodesZremove_ctr_varr  Zremove_ctr_varsr  Zviz_dot_pathZviz_passr5   r5   r6   draw  s6   






zIrGraph.drawc                 C   s8   t d}t  }|d| || j t|}|S )a1  
        Convert the graph into a Program.

        WARN: When the graph includes backward operator nodes, the
        conversion process may be failed. Usually, this function is
        only used to convert a test graph.

        Returns:
            Program: a program converted from the graph.
        Zgraph_to_program_passr  )r   r  rK  Zset_not_ownedr  r  r   _construct_from_desc)ri   Zconvert_passr/  r  r5   r5   r6   
to_program4  s   

zIrGraph.to_programc                 C   s6   d}|D ]
}|  |kr|}q|dusJ d| |S )zB
        Find a node in the giving nodes set by the name.
        Nz2Cannot find the target node (%s)in the giving set.rt  )ri   r  Z	node_nameZtarget_noder[  r5   r5   r6   r  F  s   
zIrGraph._find_node_by_namec                 C   s   t |tr|||j dS t |tr%t|tr%||dd |D  dS t |tr3|||j dS t |trL|rLt|trL|	|dd |D  dS t |t
jsXt |t
jrb|||  dS ||| dS )zK
        Update the value of desc's attribute by attribute's name.
        c                 S   rB  r5   rC  r  r5   r5   r6   r   Z  rD  z-IrGraph._update_desc_attr.<locals>.<listcomp>c                 S   rB  r5   rC  r  r5   r5   r6   r   ^  rD  N)r/   r$   rE  r/  r   r  rF  rG  rH  rI  r   rJ  rK  rL  rc  r  )ri   r/  r   r  r5   r5   r6   r  S  s   

zIrGraph._update_desc_attrr  rS  ) rk   rl   r;   r  r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r5   r5   r5   r6   r    s<    



		
6r  c                   @   s  e Zd ZdZdd Zdd Zdd ZdSd	d
Zedd Z	e	j
dd Z	edd Zedd Zedd ZedTddZdd ZdUddZdTddZdd Zd d! ZdTd"d#Zd$d% Zd&d' ZdUd(d)ZdUd*d+Zed,d- Zed.d/ Zed0d1 Zed2d3 Zej
d4d1 Zd5d6 Zd7d8 Z d9d: Z!d;d< Z"dVd>d?Z#d@dA Z$dBdC Z%dDdE Z&dFdG Z'dVdHdIZ(dJdK Z)dLdM Z*dWdOdPZ+dVdQdRZ,d=S )Xr   a  
    Create Python Program.  It has at least one :ref:`api_guide_Block_en`, when the
    control flow op like conditional_block, while :ref:`api_paddle_fluid_layers_While` is included,
    it will contain nested block.

    Please reference the
    `framework.proto <https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/fluid/framework/framework.proto>`_
    for details.

    A set of Program usually contains startup program and main program.
    A startup program is set to contain some initial work, eg. initialize the ``Parameter``, and the main
    program will contain the network structure and vars for train.

    A set of Program can be used for test or train, in train program ,
    Paddle will contain all content to build a train network,  in test
    program Paddle will prune some content which is irrelevant to test, eg.
    backward ops and vars.

    **Notes**:
        **we have** :ref:`api_paddle_fluid_framework_default_startup_program` **and** :ref:`api_paddle_fluid_framework_default_main_program`
        **by default, a pair of them will shared the parameters. The** :ref:`api_paddle_fluid_framework_default_startup_program` **only run once to initialize parameters,**
        :ref:`api_paddle_fluid_framework_default_main_program` **run in every mini batch and adjust the weights.**

    Returns:
        Program: An empty Program.

    Examples:
        .. code-block:: python

            import paddle
            import paddle.static as static

            paddle.enable_static()

            main_program = static.Program()
            startup_program = static.Program()
            with static.program_guard(main_program=main_program, startup_program=startup_program):
                x = static.data(name="x", shape=[-1, 784], dtype='float32')
                y = static.data(name="y", shape=[-1, 1], dtype='int32')
                z = static.nn.fc(name="fc", x=x, size=10, activation="relu")

            print("main program is: {}".format(main_program))
            print("start up program is: {}".format(startup_program))

    c                 C   s   t  | _t| dg| _d| _t| _t jj	j
| _g | _d| _d| _d | _g | _d | _g | _d | _d| _d| _d| _d| _d| _d | _d | _d | _d | _d| _td| _ d | _!d| _"d S )Nr   Fr   Z__auto_checkpoint_program__)#r   rK  r/  rG  rT  current_block_idxglobal_prog_seed_seedr  r_  Forward_current_role_Program__op_role_var_is_distributed	_is_chief_parameters_on_pservers
_endpoints_ps_endpointZ_trainers_endpoints_distributed_lookup_tableZ_enable_dgcZ	_use_lambZ_nccl_comm_numZ_use_hierarchical_allreduceZ$_hierarchical_allreduce_inter_nranksZ
_fleet_optZ_program_configZ_pipeline_optZ_heter_pipeline_opt_appending_grad_timesr   r)  Z_auto_checkpoint_nameZ_graph_is_start_up_program_r   r5   r5   r6   r     s8   

zProgram.__init__c                 C   s  dd }| j }g }| }t|D ]}|t| jd kr |   ||}|g  |d }| D ]}	| j| 	|	
 rI| j| |	
 }
nd }
|	 |	
 ||	dtjjjtjjjtjjjg||	dtjjjtjjjtjjjg||	dtjjjtjjjg|
d ur|
jnd |
d ur|
jnd|
d ur|
jnd|	 |
d ur|
jndd	
}t|
tr||
j|
j|
j|
j|
j|
j |
j!d
 |tt"#|d q2|	$ |d< |t%t"#|d q2q|S )Nc                 S   s   |   |v rt| | S d S rD   )rr   rf  )r  rk  Zallowed_typesr5   r5   r6   get_var_desc_attr_or_none  s   zAProgram._find_var_class_kwargs.<locals>.get_var_desc_attr_or_noner   rS   r  r   r6  F)
rr   r   r  r   r6  r,  r;  r<  r>  r+  )r^  r{  r|  do_model_average	need_clipis_distributedr]  )classr`   r  )&r/  
num_blocksrv   rw   rT  _create_blockr(  r   r  r
  r   r2  rr   r   r   r   r  rE  ZLOD_TENSOR_ARRAYr,  r;  r<  r>  r+  r/   r'  updater^  r{  r|  r$  r%  r&  r]  r  deepcopyr  r$   )ri   Znew_descr#  Zold_descall_new_vars	block_numr@  new_block_descZblock_new_varsZnew_var_descZold_varr`   r5   r5   r6   _find_var_class_kwargs  s   

			
,MzProgram._find_var_class_kwargsc                 C   s8  |  |}| }|t|ksJ || j ksJ t|D ]}| j| }|j  |j  qt|D ]}| j| j}|	|}|
| q4~t|D ]!}| j| }|| D ]}|d }	|d }
||
d< |	di |
 qWqLt|D ]'}| j| }| j	|}t| D ]}||}t||d}|j| qqrd S )Nr'  r`   r(  r  r5   )r/  r(  rw   r/  rv   rT  r9  clearr?  r(  Z
_move_fromr  r:  r  r   )ri   r/  r,  r-  r@  r(  Z
block_descr.  r  Zclazzr`   r  r  r:  r5   r5   r6   r  0  s<   






zProgram._rebuild_from_descr   c                 C   s   |a t | _dS )ag  
        Set global seed for Program

        Returns:
            None.

        Examples:
            .. code-block:: python

                import paddle
                import paddle.static as static

                paddle.enable_static()

                prog = static.default_main_program()
                print(prog.random_seed)
                ## 0
                ## the default random seed is 0

                prog.global_seed(102)
                prog1 = static.default_main_program()
                print(prog1.random_seed)
                ## 102
                ## the random seed is 102
        N)r  r  ri   seedr5   r5   r6   global_seedU  s   
zProgram.global_seedc                 C   r   )a  
        The operator role. In a enum {Forward, Backward, Optimize}.

        Notes: this is a low level API. It is used only for ParallelExecutor to
        duplicate or schedule operator to devices.

        For example, the forward operator should be executed on every device.
        The backward operator should be executed on every device and the
        parameter gradient of backward (use :code:`_op_role_var` to get this
        variable) operator should be merged to one device. The optimization
        operators should be executed on only one device and broadcast the
        optimization result, i.e., the new parameter, to every other device.
        r  r   r5   r5   r6   r  s  s   zProgram._op_rolec                 C   
   || _ d S rD   r4  )ri   Zroler5   r5   r6   r    r9  c                 C   r   )z
        The auxiliary variables for :code:`_op_role` property.

        See Also: :code:`Program._op_role`'s documentation for details.

        Notes: This is a very low-level API. Users should not use it directly.
        )r  r   r5   r5   r6   r    s   	zProgram._op_role_varc                 c   s4    | j }tjj}|j| _ z	d V  W || _ d S || _ w rD   )r  r   r  r_  rd  )ri   tmp_roler_  r5   r5   r6   _backward_role_guard  s   zProgram._backward_role_guardc                 c   sV    | j }| j}tjj}|j| _ dd |D | _zdV  W || _|| _ dS || _|| _ w )a  
        A with guard to set :code:`Optimization` :code:`OpRole` and
        :code:`OpRoleVar` automatically.

        Notes: This is a very low level API. Users should not use it directly.

        Args:
            param_and_grads(list): The variables (names) to be optimized.

        Examples:

            >>> import paddle.fluid as fluid
            >>> p, g = backward(...)
            >>> with program._optimized_guard([p,g]):
            >>>     p = p - 0.001 * g
        c                 S   s    g | ]}t |tr|jn|qS r5   )r/   r$   r   r#  r5   r5   r6   r     r  z,Program._optimized_guard.<locals>.<listcomp>N)r  r  r   r  r_  r`  )ri   Zparam_and_gradsr6  tmp_varr_  r5   r5   r6   _optimized_guard  s   
zProgram._optimized_guardFc                 c   sf    | j }| j}tjj}|j| _ |rt|jt|jB | _ g | _zdV  W || _|| _ dS || _|| _ w )a  
        A with guard to set :code:`LRSched` :code:`OpRole` and
        :code:`OpRoleVar` automatically. The :code:`OpRoleVar` is
        set to the target learning rate.

        Notes: This is a very low level API. Users should not use it directly.

        Args:
            is_with_opt: Only set to true if these ops a in the middle
                 of a bunch of optimize ops so that it can be treated
                 correctly. For example, sgd->lr_op->sgd->lr_op->sgd.

        Examples:

            >>> import paddle.fluid as fluid
            >>> p, g = backward(...)
            >>> with program.lr_schedule_guard():
            >>>     lr = lr * decay
        N)r  r  r   r  r_  ZLRSchedrx   r`  )ri   Zis_with_optr6  r8  r_  r5   r5   r6   _lr_schedule_guard  s   
zProgram._lr_schedule_guardc                 C   rS  )z
        Get the protobuf debug string of this Program.

        Returns:
            (str): The protobuf debug string.

        Raises:
            ValueError: If any of required fields is not set.
        rT  r   r5   r5   r6   r    s   
zProgram.__str__Tc                 C   sF   t |tsJ dt|d}| jD ]}|||7 }|d7 }q|S )a%  
        Get readable debug string of Program.

        .. note::
            If you want to get the debug string in protobuf format,
            please use :code:`to_string` method.

        Args:
            skip_op_callstack(bool): whether to skip parsing Operator's attribute
                op_callstack, default value is True

        Returns:
            string: The formatted Program string.

        Examples:
            .. code-block:: python

            import paddle
            import paddle.static as static

            paddle.enable_static()

            cur_program = static.Program()
            cur_block = cur_program.current_block()
            new_var = cur_block.create_var(name="X",
                                           shape=[-1, 23, 48],
                                           dtype='float32')
            new_op = cur_block.append_op(type="abs",
                                inputs={"X": [new_var]},
                                outputs={"Out": [new_var]})
            print(cur_program._to_readable_code())
        r  r   r$  )r/   r0   r   rr   rT  rU  )ri   r4  Zprogram_strr(  r5   r5   r6   rU    s   !

zProgram._to_readable_codec                 C   s   t |tsJ dt|t |tsJ dt||r0d}| jD ]
}||||7 }q#|S | j }tj	
t|}t||}|S )a  
        To debug string.

        Args:

            throw_on_error (bool): raise Value error when any of required fields is not set.

            with_details (bool): True if more details about variables and parameters, e.g., :code:`trainable`, :code:`optimize_attr`, need to print.

        Returns:
            str: The debug string describe current Program.

        Raises:
            ValueError: If any of required fields is not set and throw_on_error is True.

        Examples:
            .. code-block:: python

                import paddle
                import paddle.static as static

                paddle.enable_static()

                prog = static.default_main_program()
                x = static.data(name="X", shape=[2,3], dtype="float32")
                pred = static.nn.fc(x, size=3)
                prog_string = prog.to_string(throw_on_error=True, with_details=False)
                prog_string_with_details = prog.to_string(throw_on_error=False, with_details=True)
                print("program string without detail: {}".format(prog_string))
                print("program string with detail: {}".format(prog_string_with_details))
        zNThe type of throw_on_error parameter is wrong, expected bool, but received {}.zLThe type of with_details parameter is wrong, expected bool, but received {}.r   )r/   r0   r   rr   rT  rl  r/  rc  r   rK  rd  rt   re  r
  )ri   r	  rg  ri  r(  rh  r  r5   r5   r6   rl    s2    


zProgram.to_stringc                 C   r   )z
        Get the C++ side of `ProgramDesc` object pointer. The C++ object is
        exposed by :code:`pybind`.

        Notes: This is a very low level API. Users should not use this API
        directly.
        rC  r   r5   r5   r6   	_get_descS  s   zProgram._get_descc                 C   rm  rD   )r/  _versionr   r5   r5   r6   r<  ]  r  zProgram._versionc                    s   |    d}|r0t  t| j\ _} fddtj j D  _	     j
ddn<t | j_| j_t| j_fddtj| j D _	| j_| j_| j_t| drh| j_   |  | | |  S )ae"  
        .. note:::
            1. :code:`Program.clone()` method DOES NOT clone :ref:`api_paddle_io_DataLoader` .
            2. Recommend you to use :code:`clone` before using :code:`Opimizer.minimize` .
            3. This API has no effect in Dygraph Mode.

        Create a new Program with forward content of original one when ``for_test=True``.
        Create a new Program as same as the original one when ``for_test=False``.

        Some operators, e.g., :ref:`api_paddle_fluid_layers_batch_norm` , behave differently between
        training and testing. They have an attribute, :code:`is_test`, to
        control this behaviour. This method will change the :code:`is_test`
        attribute of them to :code:`True` when :code:`for_test=True`.

        * Set for_test to False when you want to clone the program for training.
        * Set for_test to True when you want to clone the program for testing.
          We will prune the backward and optimize part of the program when you
          use :code:`clone` after :code:`Opimizer.minimize`, but we still
          recommend you to use :code:`clone` before using :code:`Opimizer.minimize`.

        For Example:
          ::

            import paddle
            import paddle.static as static

            paddle.enable_static()

            img = static.data(name='image', shape=[None, 784])
            pred = static.nn.fc(x=img, size=10, actvation='relu')
            loss = paddle.mean(pred)
            # Here we use clone before Momentum
            test_program = static.default_main_program().clone(for_test=True)
            optimizer = paddle.optimizer.Momentum(learning_rate=0.01, momentum=0.9)
            optimizer.minimize(loss)

        Args:

            for_test (bool): True if change the :code:`is_test` attribute of operators to :code:`True`
                and prune the backward and optimize part of the program. The default value is :code:`False` .

        Returns:
            Program: A new Program with forward content of original one when ``for_test=True``.  A new Program as same as the original one when ``for_test=False``


        Examples:

            .. note::
                The Program's order maybe different after :code:`clone` and
                this will not affect your training or testing progress. In the following
                example we give you an simple method :code:`print_prog(program)` to
                print Program Descs inorder to make sure you have same print result
                after :code:`clone`:

            .. code-block:: python

                import six

                def print_prog(prog):
                    for name, value in sorted(six.iteritems(prog.block(0).vars)):
                        print(value)
                    for op in prog.block(0).ops:
                        print("op type is {}".format(op.type))
                        print("op inputs are {}".format(op.input_arg_names))
                        print("op outputs are {}".format(op.output_arg_names))
                        for key, value in sorted(six.iteritems(op.all_attrs())):
                            if key not in ['op_callstack', 'op_role_var']:
                                print(" [ attrs: {}:   {} ]".format(key, value))


            1. To clone a test program, the sample code is:
                .. code-block:: python

                    import six
                    import paddle
                    import paddle.static as static
                    import paddle.utils as utils
                    import paddle.nn.functional as F

                    paddle.enable_static()

                    def print_prog(prog):
                        for name, value in sorted(six.iteritems(prog.block(0).vars)):
                            print(value)
                        for op in prog.block(0).ops:
                            print("op type is {}".format(op.type))
                            print("op inputs are {}".format(op.input_arg_names))
                            print("op outputs are {}".format(op.output_arg_names))
                            for key, value in sorted(six.iteritems(op.all_attrs())):
                                if key not in ['op_callstack', 'op_role_var']:
                                    print(" [ attrs: {}:   {} ]".format(key, value))

                    train_program = static.Program()
                    startup_program = static.Program()

                    # startup_program is used to do some parameter init work,
                    # and main program is used to hold the network
                    with static.program_guard(train_program, startup_program):
                        with utils.unique_name.guard():
                            img = static.data(name='image', shape=[None, 784])
                            hidden = static.nn.fc(x=img, size=200, activation='relu')
                            hidden = F.dropout(hidden, p=0.5)
                            loss = F.cross_entropy(
                                input=static.nn.fc(x=hidden, size=10, activation='softmax'),
                                label=static.data(name='label', shape=[1], dtype='int64'))
                            avg_loss = paddle.mean(loss)
                            test_program = train_program.clone(for_test=True)
                    print_prog(test_program)

                    # Due to parameter sharing usage for train and test, so we need to use startup program of train
                    # instead of using test startup program, while nothing is in test's startup program

                    # In Paddle we will share weights by using the same Tensor name. In train and test program
                    # all parameters will have the same name and this can make train and test program sharing parameters,
                    # that's why we need to use startup program of train. And for startup program of test, it has nothing,
                    # since it is a new program.

                    with static.program_guard(train_program, startup_program):
                        with utils.unique_name.guard():
                            sgd = paddle.optimizer.SGD(learning_rate=1e-3)
                            sgd.minimize(avg_loss)


            2. The clone method can be avoid if you create program for training and program for testing individually.
                .. code-block:: python

                    import six
                    import paddle
                    import paddle.static as static
                    import paddle.utils as utils
                    import paddle.nn.functional as F

                    paddle.enable_static()

                    def print_prog(prog):
                        for name, value in sorted(six.iteritems(prog.block(0).vars)):
                            print(value)
                        for op in prog.block(0).ops:
                            print("op type is {}".format(op.type))
                            print("op inputs are {}".format(op.input_arg_names))
                            print("op outputs are {}".format(op.output_arg_names))
                            for key, value in sorted(six.iteritems(op.all_attrs())):
                                if key not in ['op_callstack', 'op_role_var']:
                                    print(" [ attrs: {}:   {} ]".format(key, value))

                    def network():
                        img = static.data(name='image', shape=[None, 784])
                        hidden = static.nn.fc(x=img, size=200, activation='relu')
                        hidden = F.dropout(hidden, p=0.5)
                        loss = F.cross_entropy(
                            input=static.nn.fc(x=hidden, size=10, activation='softmax'),
                            label=static.data(name='label', shape=[1], dtype='int64'))
                        avg_loss = paddle.mean(loss)
                        return avg_loss

                    train_program_2 = static.Program()
                    startup_program_2 = static.Program()
                    test_program_2 = static.Program()
                    with static.program_guard(train_program_2, startup_program_2):
                        with utils.unique_name.guard():
                            avg_loss = network()
                            sgd = paddle.optimizer.SGD(learning_rate=1e-3)
                            sgd.minimize(avg_loss)
                    # the test startup program is not used.
                    with static.program_guard(test_program_2, startup_program_2):
                        with utils.unique_name.guard():
                            avg_loss = network()
                    print_prog(test_program_2)

            The two code snippets above will generate and print same programs.
        Nc                       g | ]}t  |qS r5   rG  r  )forward_progr5   r6   r     s    z!Program.clone.<locals>.<listcomp>F)prune_read_opc                    r=  r5   r>  r  rs  r5   r6   r   "      
lr_sheduler)r~  r   r   Zprune_backwardr/  rt   ru   rv   r(  rT  _inference_optimizer  r  rK  r  r  r!  r  rC  r  _copy_data_info_from_copy_dist_param_info_from)ri   r  pruned_origin_block_id_mapr5   )r?  rs  r6   r  `  s<    0





zProgram.clonec                 C   s   |  g |S )a  
        Prune operators and variables which are not needed to generate
        :code:`targets`.

        Notes: This is a very low level API. Users should not use this API
        directly. This API is in flux and not stable.

        Args:
            targets(list|Variable|Operator): A list of variables, operators, or variable names
                need to be pruned

        Returns:
            Program:  A new, pruned program.
        )_prune_with_input)ri   targetsr5   r5   r6   _prune5  s   zProgram._prunec                    s  |    t|ts|g}t|ts|g}|D ]}t|tjs&tdt| qt }t| 	 j
D ]0\}}d}|jD ]}| 	 |sDq:| 	 |jrMq:|||vrXd} nq:|ra||j}q1g }	|D ]e}
t|
tst|
trv|
j}nt|
tjrt|
}ntdt|
 ||v r||vrqfd}| 	 }t|j
D ]\}}||jv r| r||vrq|}q|dur|	|jj|jg qf|	|
jj|
jg qft  t| jt||	\ _} fddtj j D  _      !|   "| |  #|   S )a  
        Prune operators and variables which are not needed to generate
        :code:`targets`. Prune operators and variables which are needed
        to generate feeded_var

        Notes: This is a very low level API. Users should not use this API
        directly. This API is in flux and not stable.

        Args:
            feeded_var_names(list|str): A list of variable names from where
                pruning start. If it is set as [], this API works just like _prune()
            targets(list|Variable|Operator): A list of variables, operators, or variable names
                need to be pruned

        Returns:
            Program:  A new, pruned program.
        zUAll feeded_var_names of Program._prune_with_input() can only be str, but received %s.TFz]All targets of Program._prune_with_input() can only be Variable or Operator, but received %s.Nc                    r=  r5   r>  r  resr5   r6   r     rB  z-Program._prune_with_input.<locals>.<listcomp>)$r~  r/   r   rt   r  rW   rr   r  r  r  r?  r>  r
  r2  r  unionr	  r  r$   r   r|   rb  r   r(  r@  r   r   Zpruner/  ru   rv   r(  rT  r  rE  rF  )ri   Zfeeded_var_namesrI  r2  Zgeneratable_varsr@  r:  Zrunnable_opr   Ztargets_idxr  Z	target_opr  rG  r5   rK  r6   rH  F  s   





	




zProgram._prune_with_inputc           	         sB  t   t| j _d} jd}|rR	 || ks$|| dkr%n|d7 }q|| k r8|d|d  |	 D ]}| tj
jjkrQ|t|  q<tj j D ].} j|}tj| D ]}||}|dr}|dd | dkr|d qkq[ fdd	tj j D  _    S )
a  
        This method will create a new program and do following adjustments on it:
        1. Remove all reader variables and their creator ops if exist.

        2. Remove the :code:`read_op` if exists.

        3. change the :code:`is_test`
        attribute of operators to :code:`True`. All the :code:`Parameter`
        information will be lost.

        Args:
            prune_read_op(bool): remove the read ops that are added by py_reader
                                 for cpp inference library

        Notes: This API is a very low level API. Use
        :code:`Program.clone(for_test=True)` instead.

        Returns:
            Program: The new program.
        r   Treadr   r  Z
batch_normZReserveSpacec                    r=  r5   r>  r  rK  r5   r6   r     rB  z/Program._inference_optimize.<locals>.<listcomp>)r   r   rK  r/  r(  r  r:  rr   r  r  r   r   READERr  r-  r1  r   rt   ru   rv   r(  r  rN  r  rT  r~  )	ri   r@  Zread_op_idxZ
root_blockr2  ry   r(  jr:  r5   rK  r6   rD    s@   



zProgram._inference_optimizec              	      sr  t   t| j _ fddtj j D  _ 	  ddg}tj j D ]	} j
|}| D ]
}|  |  q9|sGq,td| D ]}||}| t jvr_qNt| }t | }	g }
| D ]!}d}|	jD ]}|j|krq||jr|
| d} |s|
| qug }| D ]!}d}|	jD ]}|j|krq|jr|| d} |s|| qtj }|| v rt |!|nd}|dd	d
dddg}|" D ]}|#| qg }| D ];}|r||v rq|$drqt%|dkr||v r|#| qd}|	j&D ]}|j|krqd} |s)|| q|D ]}|#| q,qNq, S )aO  
        This method will create a new program and do following adjustments on it:
        1. Remove all variable's `is_parameter` attribute if exist.

        2. Remove all variable's `stop_gradient` attribute if exist.

        Notes: This API is a very low level API.

        Returns:
            Program: The new program.
        c                    r=  r5   r>  r  rK  r5   r6   r     rB  z1Program._remove_training_info.<locals>.<listcomp>r"  Zwith_quant_attrr   FTZquantization_typeZ
skip_quantZactivation_bits
bit_lengthZquantize_weight_bitsZweight_quant_scaleZ
_threshold)'r   r   rK  r/  rt   ru   rv   r(  rT  r~  r(  r  Zclear_is_parameterZclear_stop_gradientr  r:  rr   r  r  r  r  r  r'  rC  r   extrar   r&  rD  r  ZkOpWithQuantAttrNamer  r0   r  r   r  endswithrw   r}  )ri   Z
clip_extraZcommon_clipped_attrs_listry   r(  r2  r  r:  r  r  Zremove_input_listr   findZinput_protoZremove_output_listZoutput_protoZop_quant_nameZquantZquant_attrsZextra_attr_nameZremove_attr_listZ
attr_protor5   rK  r6   _remove_training_info  s   










	




PzProgram._remove_training_infoc                    s@   t   t|  _ fddtj j D  _ 	   S )a  
        .. note::
            1. All information about parameters will be lost after serialization;
            2. This API has no effect in Dygraph mode.

        Deserialize a Program from  `protobuf <https://en.wikipedia.org/wiki/Protocol_Buffers>`_  binary string.
        This method always use to save and load model

        Args:

            binary_str_type (str): the binary prootbuf string.

        Returns:
            Program: A deserialized Program.

        Examples:
            .. code-block:: python

                import paddle
                import paddle.static as static

                paddle.enable_static()

                startup_prog = static.Program()
                main_prog = static.Program()
                with static.program_guard(startup_prog, main_prog):
                    x = static.data(name='X', shape=[1000, 784], dtype='float32')

                    y = static.data(name='Y', shape=[784, 100], dtype='float32')

                    z = paddle.matmul(x=x, y=y)

                    binary_str = static.default_main_program().desc.serialize_to_string()
                    prog_restored = static.default_main_program().parse_from_string(binary_str)

                    print(static.default_main_program())
                    print(prog_restored)
        c                    r=  r5   r>  r  rA  r5   r6   r     r   z-Program.parse_from_string.<locals>.<listcomp>)
r   r   rK  r/  rt   ru   rv   r(  rT  r~  )Z
binary_strr5   rA  r6   r3  ]  s
   ("zProgram.parse_from_stringc                    s:   t   |  _ fddtj j D  _    S )z
        Construct a program from program desc.

        Args:
            desc(core.ProgramDesc): The program desc for constructing.

        Returns:
            Program: A program.
        c                    r=  r5   r>  r  rA  r5   r6   r     r   z0Program._construct_from_desc.<locals>.<listcomp>)r   r/  rt   ru   rv   r(  rT  r~  rC  r5   rA  r6   r    s
   "zProgram._construct_from_descc                 C   r   )a6  
        The default random seed for random operators in Program. ``0`` means get
        the random seed from random device.

        .. note::
            It must be set before the operators have been added.

        Returns:
            int64: Random seed in current Program


        Examples:
            .. code-block:: python

                import paddle
                import paddle.static as static
                import paddle.nn.functional as F

                paddle.enable_static()

                prog = static.default_main_program()
                random_seed = prog.random_seed
                x_var = static.data(name="X", shape=[3,3], dtype="float32")
                print(random_seed)
                ## 0
                ## the default random seed is 0

                # Here we need to set random seed before we use paddle.nn.functional.dropout
                prog.random_seed = 1
                z_var = F.dropout(x_var, 0.7)

                print(prog.random_seed)
                ## 1
                ## the random seed is change to 1
        )r  r   r5   r5   r6   random_seed  s   %zProgram.random_seedc                 C   rm  )aj  
        The number of :ref:`api_guide_Block_en`  in this Program.

        .. note::
            This API has no effect in Dygraph mode.

        Returns:
            int(Platform-dependent size): num of :ref:`api_guide_Block_en`  in current Program


        Examples:
            .. code-block:: python

                import paddle
                import paddle.static as static

                paddle.enable_static()

                prog = static.default_main_program()
                num_blocks = prog.num_blocks
                print(num_blocks)

                # print result:
                # 1
        )r/  r(  r   r5   r5   r6   r(    s   
zProgram.num_blocksc                 C   s$   t |tstdt| || _d S )NzEProgram.random_seed's input seed must be an integer, but received %s.)r/   rx   rW   rr   r  r1  r5   r5   r6   rV    s   

c                 C   rS  rD   )r  r   r5   r5   r6   r    rP   zProgram.__repr__c                 C   s
   | j d S )a3  
        .. note::
            This API has no effect in Dygraph mode.

        Get the first :ref:`api_guide_Block_en` of this Program.

        Returns:
            :ref:`api_guide_Block_en`: The first :ref:`api_guide_Block_en`  of this Program.


        Examples:
            .. code-block:: python

                import paddle
                import paddle.static as static

                paddle.enable_static()

                prog = static.default_main_program()
                gb_block = prog.global_block()
                print(gb_block)

        r   rT  r   r5   r5   r6   r    s   
zProgram.global_blockc                 C   s
   | j | S )am  
        .. note::
            This API has no effect in Dygraph mode.

        Get the :code:`index`  :ref:`api_guide_Block_en`  of this Program

        Args:
            index (int) - The index of  :ref:`api_guide_Block_en`  to get

        Returns:
            :ref:`api_guide_Block_en`: The :code:`index` block

        Examples:
            .. code-block:: python

                import paddle
                import paddle.static as static

                paddle.enable_static()

                prog = static.default_main_program()
                block_0 = prog.block(0)
                print(block_0)
        rW  )ri   rZ   r5   r5   r6   r(    ro  zProgram.blockc                 C   s   | j | j S )a  
        .. note::
            This API has no effect in Dygraph mode.

        Get the current  :ref:`api_guide_Block_en` . The :code:`current`  :ref:`api_guide_Block_en`
        is the  :ref:`api_guide_Block_en`  to append operators.

        Returns:
             :ref:`api_guide_Block_en`: The :code:`index`  :ref:`api_guide_Block_en`

        Examples:
            .. code-block:: python

                import paddle
                import paddle.static as static

                paddle.enable_static()

                prog = static.default_main_program()
                current_blk = prog.current_block()
                print(current_blk)
        )rT  r  r   r5   r5   r6   current_block!  s   zProgram.current_blockNc                 C   sT   t | j}|du r|  n| |}| j|j || _| jt| | j |  S )z
        Create a new block with the :code:`parent_idx` and change the current block
        to new block.

        Args:

            parent_idx(int): The parent block index.

        Returns:
            Block: The new block.
        N)	rw   rT  rX  r(  r/  Zappend_blockr  r   rG  )ri   rk  Znew_block_idxr   r5   r5   r6   r)  :  s   
zProgram._create_blockc                 C   s   |   j| _dS )zk
        Exit a code block, i.e., roll back to the parent block.
        Returns:
            None
        N)rX  rk  r  r   r5   r5   r6   	_rollbackQ  s   zProgram._rollbackc                 C   sF   t t| j| j D ]}| jt| | q| jD ]}|  qdS )a  
        Synchronize Python instance to its binding C++ object instance.
        If the program is modified in C++ space, this method should be invoked.

        Notes: This is a very low level API. Users should not invoke it
        directly.

        Returns:
            None
        N)rv   rw   rT  r/  r(  r   rG  r~  )ri   Z	block_idxr(  r5   r5   r6   r~  Y  s
   

zProgram._sync_with_cppc                 C   s0   t |tstdt| |  |  dS )z
        Copy the information of parameters from other program.

        Notes: This is a very low level API. Users should not invoke it
        directly.

        Args:
            other(Program): Other program

        Returns:
            None
        [Function Program._copy_param_info_from() needs to pass in a source Program, but received %sN)r/   r   rq   rr   r  r  ri   r  r5   r5   r6   r  i  s   
zProgram._copy_param_info_fromc                 C   sN   t |tstdt| |j| _|j| _|j| _|j| _|j| _|j	| _	dS )z
        Copy the information of distributed information from other program.

        Args:
            other(Program): Other program

        Returns:
            None
        rZ  N)
r/   r   rq   rr   r  r  r  r  r  r   r[  r5   r5   r6   rF  ~  s   

z"Program._copy_dist_param_info_fromc                 C   s   t |tstdt| |sdd tj| j D }t	| j
D ]2\}}|j
||  }t|j D ]}||j}|jrBd|_|j rM|jd |jrSd|_q4q"dS )aJ  
        Copy the information of data variables from other program.

        Notes: This is a very low level API. Users should not invoke it
        directly.

        Args:
            other(Program): Other program
            pruned_origin_block_id_map(dict{int:int}): A dict which maps the block id in program
            self to the block id in program other. For example, {0:0, 1:1, 2:3} means block 0 in self is
            cloned from block 0 in other, etc. Default is None, which means default mapped,
            {0:0, 1:1,..., n:n}.

        Returns:
            None
        rZ  c                 S   s   i | ]}||qS r5   r5   r  r5   r5   r6   
<dictcomp>  s    z0Program._copy_data_info_from.<locals>.<dictcomp>TN)r/   r   rq   rr   rt   ru   rv   r/  r(  r  rT  r   r9  ri  r2  r   r<  r>  r8  r;  )ri   r  rG  ry   r(  Zother_blockr2  Z	other_varr5   r5   r6   rE    s.   

zProgram._copy_data_info_fromc                 c   s,    | j D ]}t|j D ]}|V  qqdS )aF  
        Get all Tensors from this Program. A iterable object is returned.

        Returns:
            iterable Tensors: The Generator will yield every Tensor in this program.

        Examples:
            .. code-block:: python

                import paddle
                import paddle.static as static

                paddle.enable_static()

                prog = static.default_main_program()
                img = static.data(name='img', shape=[None, 1,28,28], dtype='float32')
                label = static.data(name='label', shape=[None,1], dtype='int64')
                for var in prog.list_vars():
                    print(var)

                # var img : LOD_TENSOR.shape(-1, 1, 28, 28).dtype(float32).stop_gradient(True)
                # var label : LOD_TENSOR.shape(-1, 1).dtype(int64).stop_gradient(True)
        N)rT  r   r9  ri  )ri   
each_blockZeach_varr5   r5   r6   	list_vars  s   
zProgram.list_varsc                 C   s"   g }| j D ]	}||  q|S )a  
        Get all :ref:`api_guide_parameter_en` from this Program. A list object is returned.

        Returns:
            list[ :ref:`api_guide_parameter_en` ]: The list contians all parameters in this program.

        Examples:
            .. code-block:: python

                import paddle
                import paddle.static as static

                paddle.enable_static()

                program = static.default_main_program()
                data = static.data(name='x', shape=[None, 13], dtype='float32')
                hidden = static.nn.fc(x=data, size=10)
                loss = paddle.mean(hidden)
                paddle.optimizer.SGD(learning_rate=0.01).minimize(loss)

                for param in program.all_parameters():
                    print(param)

                # Here will print all parameters in current program, in this example,
                # the result is like:
                #
                # persist trainable param fc_0.w_0 : LOD_TENSOR.shape(13, 10).dtype(float32).stop_gradient(False)
                # persist trainable param fc_0.b_0 : LOD_TENSOR.shape(10,).dtype(float32).stop_gradient(False)
                #
                # Here print(param) will print out all the properties of a parameter,
                # including name, type and persistable, you can access to specific
                # property of a parameter, such as param.name, param.type
        )rT  extendrw  )ri   
parametersr]  r5   r5   r6   rw    s   "
zProgram.all_parametersr  c           	         s   ddl m} |durt|tjstdt||du r | }tts.tdtdd dd	 fd
d  fdd}t	|| 
 }t }|D ]}||j}|du rdtd|j| ||j< qP|S )a  
        Get parameters and persistable buffers of program as a dict. 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.

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

        Args:
            mode(str, optional): Source of the obtained parameters and buffers.
                    'opt' :  The return value only contains the variable in the optimizer.
                    'param' : The return value only contains the variable in the network, not the variable in the optimizer.
                    'all' : The return value contains the variable in the network and optimizer.
                    Default: 'all'
            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

        Retruns:
            dict: a dict contains the parameters and persistable buffers.

        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)
        r   r  NzG`scope` should be None or `paddle.static.Scope'` type, but received {}.z1Type of `mode` should be string, but received {}.c                 S   s
   t | tS rD   rx  r2  r5   r5   r6   r]  ?  r  z(Program.state_dict.<locals>.is_parameterc                 S   sF   | j  tjjjks| j  tjjjks| j  tjjjkr dS | jS NF)	r/  rr   r   r   r   ZFEED_MINIBATCHZ
FETCH_LISTrO  r  ra  r5   r5   r6   is_persistableB  s
   z*Program.state_dict.<locals>.is_persistablec                    s    t | ts| j s | S dS rb  )r/   r'  r/  r>  ra  )rc  r5   r6   is_belong_to_optimizerK  s   z2Program.state_dict.<locals>.is_belong_to_optimizerc                    sF   dkr| S dkr | S dkr| p | S t d)Nr  optr  zA`mode` string should be 'param', 'opt' or 'all', but received {}.)rW   r   ra  )rd  r]  moder5   r6   	conditionP  s   z%Program.state_dict.<locals>.conditionzDCan not find Variable '{}' in the scope. Make sure it is initialized)r  r  r/   r   r  rq   r   rr   r|   filterr^  r   r0  r   rW   r  )	ri   rf  r   r  rg  r  r   r2  r  r5   )rd  r]  rc  rf  r6   r      s>   ,
	zProgram.state_dictc                 C   s  t |tstdt|dd |  D }d|v rdnd}| D ]i\}}|r:|dkr.q#||d v r:|d | }||v rz
|| || W q# tyf } zt	
d|t|  W Y d}~q#d}~w ty } zt	
d|t|  W Y d}~q#d}~ww t	
d	| q#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)
        z5Type of `state_dict` should be dict, but received {}.c                 S   s   i | ]}|j |qS r5   rt  r#  r5   r5   r6   r\    r   z*Program.set_state_dict.<locals>.<dictcomp>zStructuredToParameterName@@TFzSkip loading for '{}'. Nz9Skip loading for '{0}'. Because '{0}' not in the program.)r/   r   rq   r   rr   r^  itemsr  rW   rI   rJ   r|   )ri   r   r   Z	vars_dictrg  r   r\  errr5   r5   r6   set_state_dictn  sF   
*zProgram.set_state_dict)r   r  rf  rD   )r  N)-rk   rl   r;   r  r   r/  r  r3  r  r  r  r  r   r7  r9  r:  r  rU  rl  r;  r<  r  rJ  rH  rD  rU  r  r3  r  rV  r(  r  r  r(  rX  r)  rY  r~  r  rF  rE  r^  rw  r   rk  r5   r5   r5   r6   r   g  sj    .9a
%






 $

,7

 V
m
9q
-

&



)
'nr   c                   @   s8   e Zd ZdZejjjfddZdd Z	d
ddZ
e	Zd	S )r'  a  
    Parameter is derived from Variable. A parameter is a persistable
    Variable, and will be updated by optimizers after each iteration.
    The training of a neural network is essentially the updating of
    its parameters.

    Relative to a general Variable, a Parameter has several its own
    member variables:

    Args:
        trainable(bool): True if the parameter need to be updated after
            iterations.
        optimize_attr(map): Parameter attributes related with optimizing.
            Currently, it only contains 'learning_rate'.
            Default: {'learning_rate': 1.0}
        regularizer(WeightDecayRegularizer): The Regularizer which will
            be applied on the parameter. Default: None
        do_model_average(bool): True if the model average strategy will
            be applied on this parameter.
        need_clip (bool): Whether the parameter gradient need to be cliped
            in optimizer. Default is True.
    c                 K   s   |d u rt d|d u rt dt|dkrt d|D ]}|dk r*t dt| qtj| |fd|||d| |dd| _|d	d
di| _|dd | _|dd | _	|dd| _
d| _d| _d S )N)The shape of Parameter should not be None)The dtype of Parameter should not be Noner   <The dimensions of shape for Parameter must be greater than 0MEach dimension of shape for Parameter must be greater than 0, but received %sT)r  r  r   rr   r^  r{  learning_rate      ?r|  r$  r%  F)rW   rw   r   r$   r   r   r^  r{  r|  r$  r%  r&  r]  )ri   r(  r  r   rr   r`   eachr5   r5   r6   r     sD   	
zParameter.__init__c                 C   rS  rD   rT  r   r5   r5   r6   r    rP   zParameter.__str__Fc              	   C   sj   t |tr
t |tsJ |r,t| |d}d}|D ]}|d|tt| |f 7 }q|S t| |d}|S )a  
        To debug string.

        Args:
            throw_on_error(bool): raise exception when self is not initialized
                when throw_on_error is True
            with_details(bool): more details about variables and parameters
                (e.g. trainable, optimize_attr, ...) will be printed when with_details is True

        Returns(str): The debug string.

        Examples:
            .. code-block:: python

                import paddle.fluid as fluid

                prog = fluid.default_main_program()
                rlt = fluid.layers.data("fake_data", shape=[1,1], dtype='float32')
                debug_str = prog.to_string(throw_on_error=True, with_details=False)
                print(debug_str)
        T)r^  r{  r|  r$  r%  rb  F)r/   r0   r$   rl  r-  r.  rf  )ri   r	  rg  ri  rj  rk  r5   r5   r6   rl  
  s   
zParameter.to_stringNr  )rk   rl   r;   r  r   r   r   r  r   r  rl  r  r5   r5   r5   r6   r'    s    
0
+r'  c                       s^   e Zd ZdZe fddZedd Zejdd Z fddZ	d	d
 Z
dd Ze	Z  ZS )r&  a  
    ParamBase is derived from Tensor( Which is the concept in Dygraph Mode).
    A ParamBase is a persistable Tensor, and will be updated by optimizers
    after each iteration.
    The training of a neural network is essentially the updating of
    its ParamBase.

    Relative to a general Tensor, a ParamBase has several its own
    member variables:

    Args:
        trainable(bool): True if the ParamBase need to be updated after
            iterations.
        optimize_attr(map): ParamBase attributes related with optimizing.
            Currently, it only contains 'learning_rate'.
            Default: {'learning_rate': 1.0}
        regularizer(WeightDecayRegularizer): The Regularizer which will
            be applied on the ParamBase. Default: None
        do_model_average(bool): True if the model average strategy will
            be applied on this ParamBase.
        need_clip (bool): Whether the parameter gradient need to be cliped
            in optimizer. Default is True.
    c                    s   |d u rt d|d u rt dt|dkrt d|D ]}|dk r*t dt| q|d ur:t|tjjs:t|}|dt	
d}tt| |rL|ntjjj|rVt|ng |tjjjd |d	d}| | _|d
ddi| _|dd | _|dd | _|dd| _|dd| _d S )Nrl  rm  r   rn  ro  r   Z_param_baseTr^  r{  rp  rq  r|  r$  r%  r&  F)rW   rw   r   r/   r   r   r   r  r   r   r)  rg   r&  r   r   r  r;  r{  r|  r$  r%  r&  ri   r  r   r`   rr  r   r^  rn   r5   r6   r   Q  sD   
zParamBase.__init__c                 C      | j  S rD   r;  r   r5   r5   r6   r^    r   zParamBase.trainablec                 C   $   t |tr| | _d S tdt|Nz4The type of trainable MUST be bool, but the type is r/   r0   r;  rW   rr   ri   r^  r5   r5   r6   r^       
c                       dj tt|  dS )ag  
        Convert a ParamBase object to a readable string.

        Returns(str): A readable string.

        Examples:
            .. code-block:: python

                import paddle
                linear = paddle.nn.Linear(3, 3)
                print(linear.weight)
                # Parameter containing:
                # Tensor(shape=[3, 3], dtype=float32, place=CUDAPlace(0), stop_gradient=False,
                #        [[ 0.48948765,  0.05829060, -0.25524026],
                #         [-0.70368278,  0.52986908, -0.68742192],
                #         [-0.54217887,  0.48439729,  0.34082305]])
        Parameter containing:
{tensor}Ztensor)r   rg   r&  r  r   rn   r5   r6   r       zParamBase.__str__c                 C   T   t | j|}| jtd |d< t| j| jfi |}||t	| < |
| d |S a  
        Deep copy parameter, it will always performs Tensor copy.

        Examples:
            .. code-block:: python

                import paddle
                import copy
                linear = paddle.nn.Linear(1, 3)
                linear_copy = copy.deepcopy(linear)

                print(linear.weight)
                # Parameter containing:
                # Tensor(shape=[1, 3], dtype=float32, place=CPUPlace, stop_gradient=False,
                #     [[-0.30929261, -0.90929240, -1.07851017]])

                print(linear_copy.weight)
                # Parameter containing:
                # Tensor(shape=[1, 3], dtype=float32, place=CPUPlace, stop_gradient=False,
                #     [[-0.30929261, -0.90929240, -1.07851017]])

        Z	_deepcopyr   T)r  r+  __dict__r   r   r)  r&  r  r   rU  copy_ri   memostate	new_paramr5   r5   r6   __deepcopy__     zParamBase.__deepcopy__c                 C   s6   t | j}t| j| jfi |}t| ||| |S rD   )r  r+  r  r&  r  r   r   Zvarbase_copyri   deviceblockingr  r  r5   r5   r6   _copy_to  s   zParamBase._copy_to)rk   rl   r;   r  dygraph_onlyr   r  r^  r  r  r  r  r  rm   r5   r5   rn   r6   r&  8  s    .

	r&  r9   c                       sz   e Zd ZdZe fddZdd Zedd Zedd	 Z	e	j
d
d	 Z	dd Z fddZdd Zdd ZeZ  ZS )r%  a  
    EagerParamBase is derived from Tensor( Which is the concept in Eager-Dygraph Mode).
    A EagerParamBase is a persistable Tensor, and will be updated by optimizers
    after each iteration.
    The training of a neural network is essentially the updating of
    its EagerParamBase.

    Relative to a general Tensor, a EagerParamBase has several its own
    member variables:

    Args:
        trainable(bool): True if the EagerParamBase need to be updated after
            iterations.
        optimize_attr(map): EagerParamBase attributes related with optimizing.
            Currently, it only contains 'learning_rate'.
            Default: {'learning_rate': 1.0}
        regularizer(WeightDecayRegularizer): The Regularizer which will
            be applied on the EagerParamBase. Default: None
        do_model_average(bool): True if the model average strategy will
            be applied on this EagerParamBase.
        need_clip (bool): Whether the parameter gradient need to be cliped
            in optimizer. Default is True.
    c                    sJ  |d u rt d|d u rt dt|dkrt d|D ]}|dk r*t dt| q|d ur:t|tjjs:t|}|dt	
d}t|tjjrN| }tt| |rW|ntjjj|rat|ng |tjjjd |   |d	d}| | _|d
ddi| _|dd | _|dd | _|dd| _|dd| _d | _d | _d S )Nrl  rm  r   rn  ro  r   Z_eager_param_baseTr^  r{  rp  rq  r|  r$  r%  r&  F)rW   rw   r   r/   r   r   r   r  r   r   r)  r9   r8   r   rg   r%  r   r   r  r  r;  r{  r|  r$  r%  r&  
_init_func_init_op_creatorrs  rn   r5   r6   r     sN   

zEagerParamBase.__init__c                 C   r5  rD   r  )ri   objr5   r5   r6   set_init_func  r  zEagerParamBase.set_init_funcc                 C   s$   | j d us	J d|    d | _ d S )Nz8Required self._init_func is not None, but received None.r  r   r5   r5   r6   
initialize"  s
   
zEagerParamBase.initializec                 C   rt  rD   ru  r   r5   r5   r6   r^  +  r   zEagerParamBase.trainablec                 C   rv  rw  rx  ry  r5   r5   r6   r^  /  rz  c                 C   s    | j dus	J d|  | dS )zY
        Call init_op_creator function to create initializer operation in block.
        Nz>Required self._init_op_creator is not None, but received None.)r  )ri   r(  r5   r5   r6   _create_init_op9  s   zEagerParamBase._create_init_opc                    r{  )al  
        Convert a EagerParamBase object to a readable string.

        Returns(str): A readable string.

        Examples:
            .. code-block:: python

                import paddle
                linear = paddle.nn.Linear(3, 3)
                print(linear.weight)
                # Parameter containing:
                # Tensor(shape=[3, 3], dtype=float32, place=CUDAPlace(0), stop_gradient=False,
                #        [[ 0.48948765,  0.05829060, -0.25524026],
                #         [-0.70368278,  0.52986908, -0.68742192],
                #         [-0.54217887,  0.48439729,  0.34082305]])
        r|  r}  )r   rg   r%  r  r   rn   r5   r6   r  B  r~  zEagerParamBase.__str__c                 C   r  r  )r  r+  r  r   r   r)  r%  r  r   rU  r  r  r5   r5   r6   r  X  r  zEagerParamBase.__deepcopy__c                 C   s8   t | j}t| j| jfi |}tj| ||| |S rD   )	r  r+  r  r%  r  r   r   r9   Ztensor_copyr  r5   r5   r6   r  v  s   zEagerParamBase._copy_to)rk   rl   r;   r  r  r   r  r  r  r^  r  r  r  r  r  r  rm   r5   r5   rn   r6   r%    s     4


		r%  c                   C   rC   )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_fluid_framework_program_guard`  to switch :ref:`api_paddle_fluid_framework_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_r5   r5   r5   r6   r     s   r   c                   C   rC   )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_fluid_framework_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='x', 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_r5   r5   r5   r6   r     rP  r   c                 C      t }| a |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  Zprev_programr5   r5   r6   switch_main_program  s   r  c                 C   r  )z
    Switch the startup program to a new program
    Args:
        program(Program): The new startup program

    Returns:
        Program: The previous startup program
    r  r  r5   r5   r6   switch_startup_program  s   
r  c                 c   s    ddl m} || dtd t| } |dur$||dtd d|_t|}zdV  W t|  |dur7t| dS dS t|  |durFt| 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

          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

          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_typer  zpaddle.static.program_guardNr  T)Zdata_feederr  r   r  r"  r  )r  r  r  r5   r5   r6   r     s2   +
r   c                 C   s8   |du rt  }t| tsJ t|tsJ | | S )z
    Get a variable by name from the global block of a program.

    Args:
        name(str): name of the variable
        program(Program|None): program object.
        If None, default_global_program() will be used.

    Returns:
        Variable
    N)r   r/   r|   r   r  r2  )r   r  r5   r5   r6   _get_var(  s
   r  c              	   c   s@    t }| a t|  zd V  W t| |a d S t| |a w rD   )rM   r   Z_switch_tracer)ZtracerZ
tmp_tracerr5   r5   r6   _dygraph_guard<  s   


r  c                 c   s:    t }| a t|  zd V  W |a tt  d S |a tt  w rD   r   )rQ   Z	tmp_placer5   r5   r6   _dygraph_place_guardJ  s   
r  c                 C   r  rD   )r  )r  
pre_devicer5   r5   r6   switch_deviceX     r  c                 c   s~    d}| rd| v r|  d\} }| dkrtd| dvr"td|  |r+d| |g} t| }z
dV  W t| dS t| w )aL  

    Note:
        The API only supports static mode.

    A context manager that specifies the device on which the OP will be placed.

    Args:
        device(str|None): Specify the device to use in the context. It should be ``cpu``,
            ``gpu`` or ``gpu:x``, where ``x`` is the index of the GPUs.
            When it is set to 'cpu' or 'gpu', all OPs created in the context will be
            placed on CPUPlace or CUDAPlace. When 'gpu' is set and the program runs on
            single-card, the device index will be the same as the device on which the
            executor runs. Default: None, OPs in this context will be automatically
            assigned devices.

    Examples:

        .. code-block:: python

            # required: gpu
            import paddle

            paddle.enable_static()
            support_gpu = paddle.is_compiled_with_cuda()
            place = paddle.CPUPlace()
            if support_gpu:
                place = paddle.CUDAPlace(0)

            # if GPU is supported, the three OPs below will be automatically assigned to CUDAPlace(0)
            data1 = paddle.full(shape=[1, 3, 8, 8], fill_value=0.5, dtype='float32')
            data2 = paddle.full(shape=[1, 3, 64], fill_value=0.5, dtype='float32')
            shape = paddle.shape(data2)

            with paddle.static.device_guard("cpu"):
                # Ops created here will be placed on CPUPlace
                shape = paddle.slice(shape, axes=[0], starts=[0], ends=[4])
            with paddle.static.device_guard('gpu'):
                # if GPU is supported, OPs created here will be placed on CUDAPlace(0), otherwise on CPUPlace
                out = paddle.reshape(data1, shape=shape)

            exe = paddle.static.Executor(place)
            exe.run(paddle.static.default_startup_program())
            result = exe.run(fetch_list=[out])
    N:cpuz!Should not set device id for cpu.)r  gpuZnpuZxpuZmlur   NzThe Attr(device) should be 'cpu' 'npu' 'xpu' 'mlu' or 'gpu', and it can also be empty string or None when there is no need to specify device. But received %s)r   rW   r.  r  )r  rZ   r  r5   r5   r6   r&   _  s$   0r&   c                 C   r  rD   )r  Zcuda_graph_attrZpre_moder5   r5   r6   _switch_cuda_graph_mode  r  r  c                 c   sH    t  rJ dt sJ dt| }z
dV  W t| dS t| w )a^  

    Note:
        The API only supports static mode.

    A context manager that specifies the cuda_graph_mode which indicating the cuda graph capture under static mode.

    Args:
        cuda_graph_attr(str|None): The cuda graph attr with the format of:
                                   cuda_graph_capture_mode;memory_pool_id;cuda_graph_id
    z-cuda_graph_guard only works under static modezKcuda_graph_guard context can be only used when Paddle is compiled with cudaN)r   r   r    r  r  r5   r5   r6   _cuda_graph_guard  s   r  c                 C   sJ   t | ts	td|  D ]\}}t |r|t |< qtd| dS )aR  
    This function sets the GFlags value in Paddle.
    For FLAGS please refer to :ref:`en_guides_flags_flags`

    Args:
        flags (dict): A dict contains flags and its value.

    Examples:
            .. code-block:: python

                import paddle
                paddle.set_flags({'FLAGS_eager_delete_tensor_gb': 1.0})
    z#flags in set_flags should be a dictz3Flag %s cannot set its value through this function.N)r/   r   rq   ri  r   	is_publicrW   )flagsr  r\  r5   r5   r6   r'     s   
r'   c                 C   s   i }t | ttfr*| D ]}t |r"t | }||i}|| qtd| |S t | trKt | rEt |  }| |i}|| |S td|  td)a1  
    This function gets the GFlags value in Paddle.
    For FLAGS please refer to :ref:`en_guides_flags_flags`

    Args:
        flags(list|tuple|str): A list/tuple of string or a string which is the flag's name.

    Returns:
        flag's value in Paddle.

    Examples:
        .. code-block:: python

            import paddle

            flags = ['FLAGS_eager_delete_tensor_gb', 'FLAGS_check_nan_inf']
            res = paddle.get_flags(flags)
            print(res)
            # {'FLAGS_eager_delete_tensor_gb': 0.0, 'FLAGS_check_nan_inf': False}
    z3Flag %s cannot get its value through this function.z5Flags in get_flags should be a list, tuple or string.)	r/   r   r   r   r  r*  rW   r|   rq   )r  Zflags_valuer  r\  tempr5   r5   r6   r(     s.   



r(   c                 C   s8  | du r| S t | tjtjtjtjtjtjtjtj	tj
f	r| S t | ts(td|  } | dkr4t S | dkr<t S td| }| dksL| dksL|r{t sWtd|| dkr_t S | dkrhtd	S | d
d}|d }t|}t|S td| }|rt std|| d
d}|d }t|}t|S td| }|rt std|| d
d}|d }t|}t|S td| }|rt std|| d
d}|d }t|}t|S td| }|rt std|| d
d}|d }t|}t	|S td| )z"convert the string to paddle PlaceNz5place only support string which is 'Place' and so on.r  r  zgpu:\d+Z
gpu_pinnedr  zIThe device should not be {}, since PaddlePaddle is not compiled with CUDAr   r  r   zxpu:\d+zHThe device should not be {}, since PaddlePaddle is not compiled with XPUznpu:\d+zHThe device should not be {}, since PaddlePaddle is not compiled with NPUzipu:\d+zHThe device should not be {}, since PaddlePaddle is not compiled with IPUzmlu:\d+zHThe device should not be {}, since PaddlePaddle is not compiled with MLUzpPaddle supports CPUPlace, CUDAPlace,CUDAPinnedPlace, XPUPlace, IPUPlace, MLUPlace and NPUPlace, but received {}.)r/   r   r  r   r   r   r   r   ZIPUPlacer   ZCustomPlacer|   rW   r  r}   r~   r    r   r   rx   r"   r#   rG   rH   )rQ   Zavaliable_gpu_placeZplace_info_listZ	device_idZavaliable_xpu_placeZavaliable_npu_placeZavaliable_ipu_placeZavaliable_mlu_placer5   r5   r6   _get_paddle_place  s   







r  c                 C   s:   t | ttfstdg }| D ]}t|}|| q|S )Nzplaces must to be List or Tuple)r/   r   r   rq   r  r   )Zplacesretrs  r5   r5   r6   _get_paddle_place_listx  s   r  rD   rf  )rS   rS   )
__future__r   rg  r   collections.abcr   
contextlibwrapped_decoratorr   r   r   r}   r  rt   r  typesr   r	   r   r   r	  r   r   loggingr   r   r-  r  r   r   r   Zpaddle.versionversionr   rI   r   Zvariable_indexr   r   __all__ZkEmptyVarNameZEMPTY_VAR_NAMEZkTempVarNameZTEMP_VAR_NAMEZkGradVarSuffixr   ZkZeroVarSuffixZZERO_VAR_SUFFIXZkControlDepVarNamer   rM   r   r   r?   r   r  r  r   r1   r2   r  globalsr   rT   Z"_dy2st_enable_standalone_executor_r7   r3   r@   rB   rE   rK   rL   r   rN   r   rR   rX   rY   r  r  rV   r   r   r%   r   r   r   r   r   r   r   Zdygraph_not_supportr  Zstatic_onlyr  Znon_static_onlyr   r   r   r   r   r   r   r   r   r   r   r"   r#   r   r   r    r!   r   r   r   r   r   r   objectr   r   r   r   r   r   r  r  r
  r   r   r  r  r  rr   r  r$  add_metaclassr$   r  r  r  rG  r  r  r  r  r  r   r'  r:   r&  r  r9   r8   Z_core_eager_eagertensorr%  r  r  r"  r   r   r  r  r   r  r  r  r  r&   r  r  r'   r(   r  r  r5   r5   r5   r6   <module>   s  "


4
=x
-				

0
'
(
"
 *?
*

           6      N      
 3c %   6            cy 
 0$
B

B/m